Getting timezone on Windows with C ++

I want to synchronize the clock of Windows and Linux. Windows receives its system clock (with the GetSystemTimeAsFileTime function) and sends it to Linux. Linux then sets its clock accordingly (with settimeofday function).

I also need to pass the Windows timezone and convert it to the Linux standard. How can I get windows timezone in C ++?

best wishes, Mustafa

+6
source share
4 answers

GetTimeZoneInformation is probably what you are looking for.

+9
source

Even if you do not synchronize with standard time, but by time between machines, you should use NTP.

NTP is a mature, reliable protocol that has solved the whole set of problems that you are going to find, or have already found: detection, messaging, latency and jitter, time zone differences, drift control so that you do not confuse other processes using the same same computer (s), actually setting the time correctly, permissions, etc.

Simply configure the NTP server on the computer you want as a wizard, and configure the NTP client on another computer by requesting a wizard. Simple and painless.

It's been a while since I installed NTP servers; I assume that you can use the NTP utilities that come standard with operating systems to perform minimal configurations if you have administrator privileges in the mailboxes.

+3
source

GetDynamicTimeZoneInformation is a more useful function. it also provides a registry key for the time zone.

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

+1
source

GetDynamicTimeZoneInformation does not always work. The minimum supported versions are Windows Vista, Windows Server 2008, and Windows Phone 8. So for anything below, GetTimeZoneInformation better.

However, another problem sometimes returns StandardName or DaylightName empty. In this case, you must use the Windows registry. Here is a function taken from gnu cash that has also been 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); } 
0
source

Source: https://habr.com/ru/post/895049/


All Articles