开发者

Getting timezone in Windows with C++

开发者 https://www.devze.com 2023-03-27 22:29 出处:网络
I want to synchronize Windows and Linux clocks. Windows gets its system clock (with GetSystemTimeAsFileTime function) and sends it to Lin开发者_Go百科ux. Then, Linux sets its clock accordingly (with s

I want to synchronize Windows and Linux clocks. Windows gets its system clock (with GetSystemTimeAsFileTime function) and sends it to Lin开发者_Go百科ux. 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++?

best wishes, Mustafa


GetTimeZoneInformation is probably what you're looking for.


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.

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.


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


Here is sample code to get time zone in C++ I applied in my project.

I share for whom concern.

TIME_ZONE_INFORMATION TimeZoneInfo;
GetTimeZoneInformation(&TimeZoneInfo);
int TimeZoneDifference = -(int(TimeZoneInfo.Bias) / 60);


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

However another issue is both sometimes return StandardName or DaylightName empty. 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);
}


This is what works for me and ports between windows and linux

#include "time.h"
...
time_t now = time(NULL);
struct tm utctm;
utctm = *gmtime(&now);
utctm.tm_isdst = -1;
time_t utctt = mktime(&utctm);
// diff is the offset in seconds
long diff = now - utctt;
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号