Current Date As UTC Number aka universal datestamp

Share your advanced PureBasic knowledge/code with the community.
User avatar
Rescator
Addict
Addict
Posts: 1769
Joined: Sat Feb 19, 2005 5:05 pm
Location: Norway

Current Date As UTC Number aka universal datestamp

Post by Rescator »

You might wonder what use GetSystemTime_() is, myself I'd use it for database entries or filenames etc. as it's guaranteed to be unaffected by timezones and offsets.

The benefit of this over FormatDate() is that this is UTC thus not affected by timezone/Daylight savings.
The resulting number is also human readable while also being machine readable something that is very important. You could use GetSystemTimeAsFileTime_() or some other API but these return nanoseconds since or milliseconds since or seconds since a epoch date, while machine readable those are not directly human readable as you need code to do calender calculations.
The resulting number can also be re-parsed into it's components fairly easily to get year and month and day for example, thus the number is very "portable".
In number form you need to store it as a 64bit number and in theory the max is um. well very high, GetSystemTime_() year in SYSTEMTIME structure maxes at 30827 and that can easily be contained in a 64bit integer.
In string form it may be a tad long (17 characters), but you could always base convert it to hex to shorten it (14 characters) if you want though.

Code: Select all

EnableExplicit

Procedure.q CurrentDateAsUTCNumber()
	Protected st.SYSTEMTIME
	GetSystemTime_(st)
	ProcedureReturn (st\wYear*10000000000000)+(st\wMonth*100000000000)+(st\wDay*1000000000)+(st\wHour*10000000)+(st\wMinute*100000)+(st\wSecond*1000)+(st\wMilliseconds)
EndProcedure

Procedure.s CurrentDateAsUTCString()
	ProcedureReturn Str(CurrentDateAsUTCNumber())
EndProcedure

Debug CurrentDateAsUTCNumber()
Debug CurrentDateAsUTCString()
I guess you could call this a universal datestamp.