` Printed Icetips Article

Icetips Article



Par2: Convert time to UTC
2000-06-12 -- Rick Smith
 
>> How would I convert the time from Eastern Standard Time to the
>> Universal Time Clock (Greenwhich) on the fly??
>>
>All you need to know is which timezone the user is in and if daylight 
>savings time is in effect. There is an api to get that information.
>DWORD GetTimeZoneInformation(
>  LPTIME_ZONE_INFORMATION lpTimeZoneInformation   // address of 
>                                                  // time-zone settings
>);
>
>typedef struct _TIME_ZONE_INFORMATION { // tzi 
>    LONG       Bias; 
>    WCHAR      StandardName[ 32 ]; 
>    SYSTEMTIME StandardDate; 
>    LONG       StandardBias; 
>    WCHAR      DaylightName[ 32 ]; 
>    SYSTEMTIME DaylightDate; 
>    LONG       DaylightBias; 
>} TIME_ZONE_INFORMATION; 
> Unfotunately the wchar means wide string so you have to convert normal 
>cstrings to wide strings to get full info.  In my ole articles for 
>clarion mag back a ways, there is a class that includes string 
>conversion to wide. On the other hand if you dont care about the 
>timezone name because it will always be eastern then prototype the 
>wchar as string(64) and ignore it.  Sytemtime is ulong.  All you really 
>need for what you want is the bias (in minutes):
> UTC = local time + bias 

Jim is absolutely correct.  This method will give you UTC from the
local time.  Want an easier method?  A few weeks back I was
researching the method Jim describes because I needed to do exactly
what Lajos needs to do.  Imagine my surprise and delight when I
discovered an API call that will return UTC directly without having to
bother with calculating the bias and then adding that to local time.
Like most discoveries of this type, it was almost an accidental
discovery.  I was looking in an API book to verify the
GetTimeZoneInformation prototype details when I accidentally turned to
the wrong page.  That's when I made the discovery.  It completely
eliminated the hassle for me.  

(Note:  In some cases, GetTimeZoneInformation may still be
appropriate. One such case might be figuring out the time difference
between the current time zone and a non-UTC time zone.)

Anyway, here are the details for GetSystemTime.

(Note 2:  The SYSTEMTIME group could conceivably be declared in an
include file.)

SYSTEMTIME          GROUP,TYPE
wYear                           WORD
wMonth                        WORD
wDayOfWeek               WORD
wDay                            WORD
wHour                           WORD
wMinute                        WORD
wSecond                      WORD
wMilliseconds               WORD
                                  END

SYSTIME                  LIKE(SYSTEMTIME)

MODULE('Windows32')
     GetSystemTime(*SYSTEMTIME),PASCAL,RAW
END

And here's how I implemented the code to make this work.  The beauty
of using this method is the fact that GetSystemTime returns not only
the time for UTC but the date as well.  This means you don't have to
worry about issues like your time zone being before midnight and UTC
being after midnight.  So date rollover is automatically handled by
the API.  You just have to get the info.


   DO GetGMT
   SomeDATE = DATE(SYSTIME.WMONTH,SYSTIME.WDAY,SYSTIME.WYEAR)
   SomeTIME =  GMT

GetGMT Routine
   GetSystemTime(SYSTIME)
   GMT = (SYSTIME.WHOUR * 360000) + (SYSTIME.WMINUTE * 6000) + |
(SYSTIME.WSECOND * 100)

(Note 3:  The values returned must be multiplied by the factors as
shown since Clarion needs time in 100ths of a second past midnight.)

In any case, this method has worked quite well in the application I've
tested it with and it elminates several of the challenges involved
with using GetTimeZoneInformation.



Printed May 6, 2024, 1:26 am
This article has been viewed/printed 35127 times.