Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert between timezones with win32 API?

Tags:

c

windows

winapi

I have date strings such as 2009-02-28 15:40:05 AEDST and want to convert it into SYSTEMTIME structure. So far I have:

SYSTEMTIME st;
FILETIME ft;
SecureZeroMemory(&st, sizeof(st));
sscanf_s(contents, "%u-%u-%u %u:%u:%u",
    &st.wYear,
    &st.wMonth,
    &st.wDay,
    &st.wHour,
    &st.wMinute,
    &st.wSecond);
// Timezone correction
SystemTimeToFileTime(&st,  &ft);
LocalFileTimeToFileTime(&ft, &ft);
FileTimeToSystemTime(&ft, &st);

However my local timezone is not AEDST. So I need to be able to specify the timezone when converting to UTC.

like image 776
grom Avatar asked Feb 28 '09 05:02

grom


People also ask

How do you change from one time zone to another?

Changing Timezones of ZonedDateTime To convert a ZonedDateTime instance from one timezone to another, follow the two steps: Create ZonedDateTime in 1st timezone. You may already have it in your application. Convert the first ZonedDateTime in second timezone using withZoneSameInstant() method.

How to convert timezone in c#?

DateTime currentTime = TimeZoneInfo. ConvertTime(DateTime. Now, TimeZoneInfo. FindSystemTimeZoneById("Central Standard Time"));

How do you convert UTC time to local time?

Add the local time offset to the UTC time. For example, if your local time offset is -5:00, and if the UTC time is shown as 11:00, add -5 to 11. The time setting when adjusted for offset is 06:00 (6:00 A.M.). Note The date also follows UTC format.


1 Answers

Take a look at this:

https://web.archive.org/web/20140205072348/http://weseetips.com:80/2008/05/28/how-to-convert-local-system-time-to-utc-or-gmt/

 // Get the local system time.
 SYSTEMTIME LocalTime = { 0 };
 GetSystemTime( &LocalTime );

 // Get the timezone info.
 TIME_ZONE_INFORMATION TimeZoneInfo;
 GetTimeZoneInformation( &TimeZoneInfo );

 // Convert local time to UTC.
 SYSTEMTIME GmtTime = { 0 };
 TzSpecificLocalTimeToSystemTime( &TimeZoneInfo,
                                  &LocalTime,
                                  &GmtTime );

 // GMT = LocalTime + TimeZoneInfo.Bias
 // TimeZoneInfo.Bias is the difference between local time
 // and GMT in minutes. 

 // Local time expressed in terms of GMT bias.
 float TimeZoneDifference = -( float(TimeZoneInfo.Bias) / 60 );
 CString csLocalTimeInGmt;
 csLocalTimeInGmt.Format( _T("%ld:%ld:%ld + %2.1f Hrs"),
                          GmtTime.wHour,
                          GmtTime.wMinute,
                          GmtTime.wSecond,
                          TimeZoneDifference );

Question: How do you get the TIME_TIMEZONE_INFORMATION for a specific timezone?

Well unfortunately you cannot do that with the win32 API. Refer to MSDN and How do I get a specific TIME_ZONE_INFORMATION struct in Win32?

You will either need to create an empty variable and fill it in manually, or use the standard C time library.

like image 115
uzbones Avatar answered Sep 27 '22 18:09

uzbones