getUTCDate()

Share your advanced PureBasic knowledge/code with the community.
ozzie
Enthusiast
Enthusiast
Posts: 429
Joined: Sun Apr 06, 2008 12:54 pm
Location: Brisbane, Qld, Australia
Contact:

getUTCDate()

Post 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.
Last edited by ozzie on Thu Aug 08, 2013 9:02 am, edited 1 time in total.
Little John
Addict
Addict
Posts: 4519
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: getUTCDate()

Post 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.
ozzie
Enthusiast
Enthusiast
Posts: 429
Joined: Sun Apr 06, 2008 12:54 pm
Location: Brisbane, Qld, Australia
Contact:

Re: getUTCDate()

Post by ozzie »

Thanks, LittleJohn. Good to know the meaning of this value.
Little John
Addict
Addict
Posts: 4519
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: getUTCDate()

Post by Little John »

You're welcome.

BTW, I just saw a small glitch:
It should read Procedure.q, shouldn't it? :-)
ozzie
Enthusiast
Enthusiast
Posts: 429
Joined: Sun Apr 06, 2008 12:54 pm
Location: Brisbane, Qld, Australia
Contact:

Re: getUTCDate()

Post 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.
Post Reply