12

Ideally, what I'd like to be able to do is take the name of a time zone and ask Windows for its corresponding time zone info (offset from UTC, DST offset, dates for DST switch, etc.). It looks like Windows uses a TIME_ZONE_INFORMATION struct to hold this sort of info. So, presumably, I want a function which takes a string with the time zone's name and returns a TIME_ZONE_INFORMATION struct.

However, all I can find are functions such as GetTimeZoneInformation() which give me the TIME_ZONE_INFORMATION for the local time. What I need is a function which will give me that information for an arbitrary time zone regardless of what the local time zone is.

The only way that I see to get that information is to go grab it directly from the registry, which is less than ideal. The TIME_ZONE_INFORMATION page shows where it is in the registry, so it should be possible to fetch the information from there, but I'd much prefer a proper system function for doing it. Does such a function exist, or do I have to go registry diving to get the time zone info for an arbitrary time zone?

Jonathan M Davis
  • 37,181
  • 17
  • 72
  • 102
  • Since http://msdn.microsoft.com/en-us/library/ms725473%28v=VS.85%29.aspx does not list it, I'd say there's none. What's so bad about reading it from the registry? – Martin Ba Sep 02 '10 at 06:42
  • It's way more of a pain than just calling a function which you pass a string. If that's what I have to do, then that's what I have to do, but I'd prefer a simpler solution. – Jonathan M Davis Sep 02 '10 at 13:47

3 Answers3

13

The time zone information is contained as binary data in the registry under HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones\(zone name)\TZI. The structure of the data is given in the TIME_ZONE_INFORMATION documentation:

struct STimeZoneFromRegistry
{
 long  Bias;
 long  StandardBias;
 long  DaylightBias;
 SYSTEMTIME StandardDate;
 SYSTEMTIME DaylightDate;
};

And here's example code to read the key:

TIME_ZONE_INFORMATION tz = {0};
STimeZoneFromRegistry binary_data;
DWORD size = sizeof(binary_data);
HKEY hk = NULL;
TCHAR zone_key[] = _T("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Time Zones\\Central Standard Time");
if ((RegOpenKeyEx(HKEY_LOCAL_MACHINE, zone_key, 0, KEY_QUERY_VALUE, &hk) == ERROR_SUCCESS) &&
 (RegQueryValueEx(hk, "TZI", NULL, NULL, (BYTE *) &binary_data, &size) == ERROR_SUCCESS))
{
 tz.Bias = binary_data.Bias;
 tz.DaylightBias = binary_data.DaylightBias;
 tz.DaylightDate = binary_data.DaylightDate;
 tz.StandardBias = binary_data.StandardBias;
 tz.StandardDate = binary_data.StandardDate;
}

Edit: Sorry, this answer is redundant - I'm sure you could have figured all this out using the documentation you linked to in the question. I've only had to do this once, and this is the only method I could find.

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
2

Have you look at this:

http://msdn.microsoft.com/en-us/library/system.timezoneinfo.getsystemtimezones.aspx

gantastic
  • 21
  • 2
1

Here is an example without the registry. The timezone information depends on the year, so I added a parameter to hold that.

TIME_ZONE_INFORMATION GmtTimezone( const wstring& name, SYSTEMTIME utc )
{
    DYNAMIC_TIME_ZONE_INFORMATION dynamicTimezone = {};
    DWORD result=0;
    for( DWORD i = 0; result!=ERROR_NO_MORE_ITEMS; ++i )
    {
        result = ::EnumDynamicTimeZoneInformation( i, &dynamicTimezone );
        if( result==ERROR_SUCCESS && name==dynamicTimezone.StandardName )
            break;
    }
    if( result!=ERROR_SUCCESS )
        throw L"Could not find timezone "+name;

    TIME_ZONE_INFORMATION tz;
    if( !GetTimeZoneInformationForYear(static_cast<USHORT>(utc.wYear), &dynamicTimezone, &tz) )
        throw "GetTimeZoneInformationForYear failed"+std::to_string(GetLastError());
    return tz;
    //SYSTEMTIME localTime;
    //SystemTimeToTzSpecificLocalTime( &tz, &utc, &localTime );
    //auto offsetHours = localTime.wHour-utc.wHour;
}
John Duffy
  • 164
  • 4
  • 10