Date() returns different time when run in Windows Service

Just starting out? Need help? Post your questions and find answers here.
swhite
Enthusiast
Enthusiast
Posts: 727
Joined: Thu May 21, 2009 6:56 pm

Date() returns different time when run in Windows Service

Post by swhite »

Hi

I have been testing the Date() function to try to find out why my logs are 5 hours ahead on my Windows 2016 server. I discovered if I use

Code: Select all

    WriteStringN(1,FormatDate("%yyyy.%mm.%dd:%hh:%ii:%ss",Date()))
in a regular Windows application created in PB I get the expected time values. If I use the same code in a windows service the time is 5 hours in the future.

Is this a potential bug?

Simon
Simon White
dCipher Computing
User avatar
Bisonte
Addict
Addict
Posts: 1233
Joined: Tue Oct 09, 2007 2:15 am

Re: Date() returns different time when run in Windows Servic

Post by Bisonte »

maybe this is a time zone problem ?

Is your normal Time 5 hours from UTC ?
PureBasic 6.10 LTS (Windows x86/x64) | Windows10 Pro x64 | Asus TUF X570 Gaming Plus | R9 5900X | 64GB RAM | GeForce RTX 3080 TI iChill X4 | HAF XF Evo | build by vannicom​​
English is not my native language... (I often use DeepL to translate my texts.)
swhite
Enthusiast
Enthusiast
Posts: 727
Joined: Thu May 21, 2009 6:56 pm

Re: Date() returns different time when run in Windows Servic

Post by swhite »

Hi

I am 4 hours behind UTC time as I am in the Eastern standard time. My logs should say 16:12 now but they actually read 21:12.
However, since it works just fine using a PB generated application I do not think that can be the problem.

Simon
Simon White
dCipher Computing
User avatar
Sicro
Enthusiast
Enthusiast
Posts: 538
Joined: Wed Jun 25, 2014 5:25 pm
Location: Germany
Contact:

Re: Date() returns different time when run in Windows Servic

Post by Sicro »

Can you test, please, if the same problem exists with the Date64 module?
The syntax of the Date64 module commands are the same as the PB-native commands, so you can use the macros at the beginning of your code to automatically replace the native date commands with the date64 module commands:

Code: Select all

IncludeFile "Date64.pbi"

Macro Date(Year=-1, Month=1, Day=1, Hour=0, Minute=0, Second=0)
  Date64::Date64(Year, Month, Day, Hour, Minute, Second)
EndMacro
Macro AddDate(Date, Type, Value) : Date64::AddDate64(Date, Type, Value) : EndMacro
Macro FormatDate(Mask, Date)     : Date64::FormatDate64(Mask, Date)     : EndMacro
Macro ParseDate(Mask, Date)      : Date64::ParseDate64(Mask, Date)      : EndMacro
Macro Year(Date)                 : Date64::Year64(Date)                 : EndMacro
Macro Month(Date)                : Date64::Month64(Date)                : EndMacro
Macro Day(Date)                  : Date64::Day64(Date)                  : EndMacro
Macro Hour(Date)                 : Date64::Hour64(Date)                 : EndMacro
Macro Minute(Date)               : Date64::Minute64(Date)               : EndMacro
Macro Second(Date)               : Date64::Second64(Date)               : EndMacro
Macro DayOfWeek(Date)            : Date64::DayOfWeek64(Date)            : EndMacro
Macro DayOfYear(Date)            : Date64::DayOfYear64(Date)            : EndMacro
Last edited by Sicro on Sun May 19, 2019 7:09 pm, edited 1 time in total.
Image
Why OpenSource should have a license :: PB-CodeArchiv-Rebirth :: Pleasant-Dark (syntax color scheme) :: RegEx-Engine (compiles RegExes to NFA/DFA)
Manjaro Xfce x64 (Main system) :: Windows 10 Home (VirtualBox) :: Newest PureBasic version
BarryG
Addict
Addict
Posts: 3330
Joined: Thu Apr 18, 2019 8:17 am

Re: Date() returns different time when run in Windows Servic

Post by BarryG »

Sicro wrote:The syntax of the Date64 module commands are the same as the PB-native commands, so you can use the macros at the beginning of your code to automatically replace the native date commands with the date64 module commands
Hi, I was hoping this Date64 module would be a direct drop-in replacement for the native Date commands without me changing anything in my source code, but it causes macro errors if your variable names contain the macro keywords. For example, this fails:

Code: Select all

date$=LCase(LSet(Hex(Date()),8,"0"))
So the user must go through their entire source and search/replace such variable names. Not complaining; just posting this as a heads-up. Cheers!
BarryG
Addict
Addict
Posts: 3330
Joined: Thu Apr 18, 2019 8:17 am

Re: Date() returns different time when run in Windows Servic

Post by BarryG »

Also, the Date64 module doesn't work with the CalendarGadget. Try the below, then click January 20 (or any later date) and it fails. Can it be made to work with CalendarGadget (and I guess the DateGadget too)?

Code: Select all

IncludeFile "Date64.pbi"

Macro Date       : Date64::Date64       : EndMacro
Macro Year       : Date64::Year64       : EndMacro
Macro Month      : Date64::Month64      : EndMacro
Macro Day        : Date64::Day64        : EndMacro 
Macro Hour       : Date64::Hour64       : EndMacro
Macro Minute     : Date64::Minute64     : EndMacro
Macro Second     : Date64::Second64     : EndMacro
Macro DayOfWeek  : Date64::DayOfWeek64  : EndMacro
Macro DayOfYear  : Date64::DayOfYear64  : EndMacro
Macro AddDate    : Date64::AddDate64    : EndMacro
Macro FormatDate : Date64::FormatDate64 : EndMacro
Macro ParseDate  : Date64::ParseDate64  : EndMacro

If OpenWindow(0, 0, 0, 250, 200, "CalendarGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)

  CalendarGadget(0, 10, 10, 230, 180)
  d = Date(2038, 1, 19, 3, 14, 7) ; Maximum date supported by PureBasic.
  SetGadgetState(0, d)
  Debug DayOfYear(d)

  Repeat
    Event = WaitWindowEvent()
    If Event = #PB_Event_Gadget
      d = GetGadgetState(0)
      Debug DayOfYear(d)
    EndIf
  Until Event = #PB_Event_CloseWindow

EndIf
swhite
Enthusiast
Enthusiast
Posts: 727
Joined: Thu May 21, 2009 6:56 pm

Re: Date() returns different time when run in Windows Servic

Post by swhite »

Hi

I will give it a try next week.

Simon

Sicro wrote:Can you test, please, if the same problem exists with the Date64 module?
The syntax of the Date64 module commands are the same as the PB-native commands, so you can use the macros at the beginning of your code to automatically replace the native date commands with the date64 module commands:

Code: Select all

IncludeFile "Date64.pbi"

Macro Date       : Date64::Date64       : EndMacro
Macro Year       : Date64::Year64       : EndMacro
Macro Month      : Date64::Month64      : EndMacro
Macro Day        : Date64::Day64        : EndMacro  
Macro Hour       : Date64::Hour64       : EndMacro
Macro Minute     : Date64::Minute64     : EndMacro
Macro Second     : Date64::Second64     : EndMacro
Macro DayOfWeek  : Date64::DayOfWeek64  : EndMacro
Macro DayOfYear  : Date64::DayOfYear64  : EndMacro
Macro AddDate    : Date64::AddDate64    : EndMacro
Macro FormatDate : Date64::FormatDate64 : EndMacro
Macro ParseDate  : Date64::ParseDate64  : EndMacro
Simon White
dCipher Computing
User avatar
Sicro
Enthusiast
Enthusiast
Posts: 538
Joined: Wed Jun 25, 2014 5:25 pm
Location: Germany
Contact:

Re: Date() returns different time when run in Windows Servic

Post by Sicro »

BarryG wrote:I was hoping this Date64 module would be a direct drop-in replacement for the native Date commands without me changing anything in my source code
It doesn't work that easy. The macros replace the native date functions of PB, but if the variables that receive the return values of the new Date64 functions are not of type Quad, then the programmer has to adapt this in his code himself. In addition, the macros only work in the main code. If date functions are also used in modules, they are not replaced there. To do this, the macros have to be inserted additionally inside each module at the beginning.
BarryG wrote:but it causes macro errors if your variable names contain the macro keywords.
That's one reason why variables shouldn't preferably have the same name as functions. I improved the macros in my previous post.
BarryG wrote:Also, the Date64 module doesn't work with the CalendarGadget. Try the below, then click January 20 (or any later date) and it fails. Can it be made to work with CalendarGadget (and I guess the DateGadget too)?
The Date64 module does not currently provide a 64 version of the SetGadgetState() and GetGadgetState() functions. This is on my ToDo list (in the best case for all OS). I saw that you got an alternative solution code in the other forum thread. Maybe this is enough for you.
Image
Why OpenSource should have a license :: PB-CodeArchiv-Rebirth :: Pleasant-Dark (syntax color scheme) :: RegEx-Engine (compiles RegExes to NFA/DFA)
Manjaro Xfce x64 (Main system) :: Windows 10 Home (VirtualBox) :: Newest PureBasic version
User avatar
Kuron
Addict
Addict
Posts: 1626
Joined: Sat Oct 17, 2009 10:51 pm
Location: Pacific Northwest

Re: Date() returns different time when run in Windows Servic

Post by Kuron »

swhite wrote:Hi

I am 4 hours behind UTC time as I am in the Eastern standard time.
Eastern Standard Time (EST) is 5 hours behind UTC. You are NOT in Eastern Standard Time right now. This time of year you are in Eastern Daylight Time (EDT) which is 4 hours behind UTC.
BarryG
Addict
Addict
Posts: 3330
Joined: Thu Apr 18, 2019 8:17 am

Re: Date() returns different time when run in Windows Servic

Post by BarryG »

Sicro wrote:The Date64 module does not currently provide a 64 version of the SetGadgetState() and GetGadgetState() functions. This is on my ToDo list (in the best case for all OS). I saw that you got an alternative solution code in the other forum thread. Maybe this is enough for you.
Hi Sicro, no, the other solution didn't work out for me, so I'd love to see your module updated for SetGadgetState and GetGadgetState. Thank you.
User avatar
kenmo
Addict
Addict
Posts: 1967
Joined: Tue Dec 23, 2003 3:54 am

Re: Date() returns different time when run in Windows Servic

Post by kenmo »

Kuron wrote:Eastern Standard Time (EST) is 5 hours behind UTC. You are NOT in Eastern Standard Time right now. This time of year you are in Eastern Daylight Time (EDT) which is 4 hours behind UTC.
Correct, small detail but important.


If you're writing a program for a server, you may want to handle all times as UTC, and convert to local time when displaying to the user.

Code: Select all

ImportC ""
  time.i(*second = #Null)
EndImport

Procedure.i UTCtoLocal(UTCTime.i)
  ProcedureReturn UTCTime + (Date() - time())
EndProcedure

Debug FormatDate("UTC Time:  %yyyy-%mm-%dd  %hh:%ii", time())
Debug FormatDate("Local Time:  %yyyy-%mm-%dd  %hh:%ii", UTCtoLocal(time()))
Debug FormatDate("Verify:  %yyyy-%mm-%dd  %hh:%ii", Date())
swhite
Enthusiast
Enthusiast
Posts: 727
Joined: Thu May 21, 2009 6:56 pm

Re: Date() returns different time when run in Windows Servic

Post by swhite »

Hi

The comment about Day Light Savings time is correct.

So my guess now is that the Date() function returns the UTC time zone without adjusting for Day Light Savings time when called from a Windows Service. I can easily adjust for that.

Simon
Simon White
dCipher Computing
User avatar
Kuron
Addict
Addict
Posts: 1626
Joined: Sat Oct 17, 2009 10:51 pm
Location: Pacific Northwest

Re: Date() returns different time when run in Windows Servic

Post by Kuron »

kenmo wrote:If you're writing a program for a server, you may want to handle all times as UTC, and convert to local time when displaying to the user.
+1
Best wishes to the PB community. Thank you for the memories. ♥️
Post Reply