Page 1 of 1

getUTCDate()

Posted: Thu Aug 08, 2013 2:19 am
by ozzie
I had a need to get the equivalent of UTC Date() and after much research ended up with this:

Code: Select all

Procedure.q getUTCDate()
  Protected UTCSystemTime.SYSTEMTIME
  Protected UTCFileTime.FILETIME
  Protected qDate.q
  
  GetSystemTime_(UTCSystemTime)
  SystemTimeToFileTime_(UTCSystemTime, UTCFileTime)
  
  qDate = (PeekQ(@UTCFileTime) - 116444736000000000) / 10000000
  ProcedureReturn qDate
EndProcedure

UTCDate = getUTCDate()
LOCDate = Date()
Debug "UTCDate=" + Str(UTCDate)
Debug "LOCDate=" + Str(LOCDate)
Debug "LOCDate - UTCDate=" + Str(LOCDate - UTCDate)
The procedure getUTCDate() returns the number of seconds since 1 Jan 1970 UTC.

I don't actually know the reason for "116444736" in the above. I originally found this in Mistrel's posting ElapsedMilliseconds for C/C++, and on Googling 116444736 found this constant used in many time functions. Maybe someone can explain it's significance. I just know the above works. (My timezone is UTC+10.)

Edit: changed Procedure.i to Procedure.q to handle dates after 2038.

Re: getUTCDate()

Posted: Thu Aug 08, 2013 4:32 am
by Little John
ozzie wrote:The procedure getUTCDate() returns the number of seconds since 1 Jan 1970 UTC.

I don't actually know the reason for "116444736" in the above. I originally found this in Mistrel's posting ElapsedMilliseconds for C/C++, and on Googling 116444736 found this constant used in many time functions. Maybe someone can explain it's significance.
The Win API function SystemTimeToFileTime_() yields a value representing the number of 100-nanosecond intervals since January 1, 1601.
[http://msdn.microsoft.com/en-us/library ... 85%29.aspx]

11644473600 is the number of seconds between January 1, 1601 and January 1, 1970.

Re: getUTCDate()

Posted: Thu Aug 08, 2013 4:44 am
by ozzie
Thanks, LittleJohn. Good to know the meaning of this value.

Re: getUTCDate()

Posted: Thu Aug 08, 2013 8:31 am
by Little John
You're welcome.

BTW, I just saw a small glitch:
It should read Procedure.q, shouldn't it? :-)

Re: getUTCDate()

Posted: Thu Aug 08, 2013 8:59 am
by ozzie
Little John wrote: BTW, I just saw a small glitch:
It should read Procedure.q, shouldn't it? :-)
Probably would be better to return the quad value, but primarily I was trying to return a UTC equivalent to Date() and that returns an integer. That will be valid until early 2038, as also mentioned in the PB documentation. I will however, change it to quad in my program, and amend the procedure above.