Linux Getting timezone in Windows with C++

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7062984/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 05:43:36  来源:igfitidea点击:

Getting timezone in Windows with C++

c++windowslinuxtimezone

提问by mustafa

I want to synchronize Windows and Linux clocks. Windows gets its system clock (with GetSystemTimeAsFileTime function) and sends it to Linux. Then, Linux sets its clock accordingly (with settimeofday function).

I want to synchronize Windows and Linux clocks. Windows gets its system clock (with GetSystemTimeAsFileTime function) and sends it to Linux. Then, Linux sets its clock accordingly (with settimeofday function).

I also need to transmit the time zone of Windows, and convert it to Linux standard. How can I get the timezone of Windows in C++?

I also need to transmit the time zone of Windows, and convert it to Linux standard. How can I get the timezone of Windows in C++?

best wishes, Mustafa

best wishes, Mustafa

采纳答案by Delan Azabani

GetTimeZoneInformationis probably what you're looking for.

GetTimeZoneInformationis probably what you're looking for.

回答by Subhash Makkena

GetDynamicTimeZoneInformation is more useful function. it gives the Registry Key for timezone also..

GetDynamicTimeZoneInformation is more useful function. it gives the Registry Key for timezone also..

http://msdn.microsoft.com/en-us/library/windows/desktop/ms724318(v=vs.85).aspx

http://msdn.microsoft.com/en-us/library/windows/desktop/ms724318(v=vs.85).aspx

回答by mzamora

Even if you're not synching to standard time, but to time between machines, you should use NTP.

Even if you're not synching to standard time, but to time between machines, you should use NTP.

NTP is a mature, robust protocol that has solved the whole stack of problems you're going to find, or have found already: discovery, comms transport, latency and jitter, timezone differences, managing drift so you don't confuse other processes sharing the same machine(s), actually setting the time correctly, permissions, etc.

NTP is a mature, robust protocol that has solved the whole stack of problems you're going to find, or have found already: discovery, comms transport, latency and jitter, timezone differences, managing drift so you don't confuse other processes sharing the same machine(s), actually setting the time correctly, permissions, etc.

Simply set up an NTP server on the machine you want as a master, and set up the NTP client on the other machine, querying the master. Simple and painless.

Simply set up an NTP server on the machine you want as a master, and set up the NTP client on the other machine, querying the master. Simple and painless.

It's been a while since I set up NTP servers; I assume that you can use the NTP utilities that come standard with the operating systems to do the job with minimum configuration, as long as you have admin privileges on the boxes.

It's been a while since I set up NTP servers; I assume that you can use the NTP utilities that come standard with the operating systems to do the job with minimum configuration, as long as you have admin privileges on the boxes.

回答by atkawa7

GetDynamicTimeZoneInformationdoesn't always work. The minimum supported versions are Windows Vista, Windows Server 2008 and Windows Phone 8. So for anything below that GetTimeZoneInformationis better.

GetDynamicTimeZoneInformationdoesn't always work. The minimum supported versions are Windows Vista, Windows Server 2008 and Windows Phone 8. So for anything below that GetTimeZoneInformationis better.

However another issue is both sometimes return StandardNameor DaylightNameempty. In that case you have to use the windows registry. Here is the function taken from gnu cash which was also modified from glib.

However another issue is both sometimes return StandardNameor DaylightNameempty. In that case you have to use the windows registry. Here is the function taken from gnu cash which was also modified from glib.

static std::string
windows_default_tzname(void)
{
    const char *subkey =
        "SYSTEM\CurrentControlSet\Control\TimeZoneInformation";
    constexpr size_t keysize{128};
    HKEY key;
    char key_name[keysize]{};
    unsigned long tz_keysize = keysize;
    if (RegOpenKeyExA(HKEY_LOCAL_MACHINE, subkey, 0,
                      KEY_QUERY_VALUE, &key) == ERROR_SUCCESS)
    {
        if (RegQueryValueExA(key, "TimeZoneKeyName", nullptr, nullptr,
                             (LPBYTE)key_name, &tz_keysize) != ERROR_SUCCESS)
        {
            memset(key_name, 0, tz_keysize);
        }
        RegCloseKey(key);
    }
    return std::string(key_name);
}