How do you get info for an arbitrary time zone in Windows?

后端 未结 3 1377
一整个雨季
一整个雨季 2020-12-17 19:08

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 swi

3条回答
  •  庸人自扰
    2020-12-17 19:42

    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(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;
    }
    

提交回复
热议问题