Monday, September 22, 2008

Getting Time Zone Information

Here's some code that retrieves local time zone information:
local lcTimeZone, ;
lnID, ;
lnStandardOffset, ;
lnDaylightOffset

* Declare the time zone information API function and get the time zone
* information.

#define TIME_ZONE_SIZE 172
declare integer GetTimeZoneInformation in kernel32 ;
string @lpTimeZoneInformation
lcTimeZone = replicate(chr(0), TIME_ZONE_SIZE)
lnID = GetTimeZoneInformation(@lcTimeZone)

* Determine the standard and daylight time offset.

lnStandardOffset = ctobin(substr(lcTimeZone, 1, 4), '4RS')
lnDaylightOffset = ctobin(substr(lcTimeZone, 169, 4), '4RS')

* Determine the total offset based on whether the computer is on daylight
* time or not. Get the description for the time zone.

if lnID = 2 && daylight time
lcTimeZoneDesc = strtran(strconv(substr(lcTimeZone, 89, 64), ;
6), chr(0), '')
lnTimeZoneOffset = (lnStandardOffset + lnDaylightOffset) * 60
else && standard time
lcTimeZoneDesc = strtran(strconv(substr(lcTimeZone, 5, 64), ;
6), chr(0), '')
lnTimeZoneOffset = lnStandardOffset * 60
endif lnID = 2

You can then add lnTimeZoneOffset to a local time value to get a time in Greenwich Mean Time (GMT) or subtract lnTimeZoneOffset from a time value in GMT to obtain a local time value.

No comments: