使用 C++ 在 Windows 中获取时区
我想同步 Windows 和 Linux 时钟。 Windows 获取其系统时钟(使用 GetSystemTimeAsFileTime 函数)并将其发送到 Linux。然后,Linux 相应地设置其时钟(使用 settimeofday 函数)。
我还需要传输Windows的时区,并将其转换为Linux标准。如何在 C++ 中获取 Windows 的时区?
最好的祝愿, 穆斯塔法
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++?
best wishes,
Mustafa
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
GetTimeZoneInformation 可能就是您想要的寻找。
GetTimeZoneInformation is probably what you're looking for.
即使您不同步到标准时间,而是同步到机器之间的时间,您也应该使用 NTP。
NTP 是一个成熟、强大的协议,它解决了您将要发现或已经发现的所有问题:发现、通信传输、延迟和抖动、时区差异、管理漂移,这样您就不会混淆其他进程共享同一台机器,实际上正确设置时间、权限等。
只需在您想要作为主机的机器上设置 NTP 服务器,并在另一台机器上设置 NTP 客户端,查询主机。简单且无痛。
我已经有一段时间没有设置 NTP 服务器了;我假设您可以使用操作系统标配的 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.
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 是更有用的函数。它也提供了时区的注册表项..
http://msdn.microsoft.com/en-us/library/windows/desktop/ms724318(v=vs.85).aspx
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
GetDynamicTimeZoneInformation
并不总是有效。支持的最低版本是 Windows Vista、Windows Server 2008 和 Windows Phone 8。因此,对于低于该版本的任何版本,GetTimeZoneInformation
更好。然而,另一个问题是有时返回
StandardName
或DaylightName
为空。在这种情况下,您必须使用 Windows 注册表。这是取自 gnu cash 的函数,它也是从 glib 修改而来的。GetDynamicTimeZoneInformation
doesn't always work. The minimum supported versions are Windows Vista, Windows Server 2008 and Windows Phone 8. So for anything below thatGetTimeZoneInformation
is better.However another issue is both sometimes return
StandardName
orDaylightName
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.这对我有用,并且在 windows 和 linux 之间进行端口
This is what works for me and ports between windows and linux
这是我在项目中应用的 C++ 获取时区的示例代码。
我为谁分忧。
Here is sample code to get time zone in C++ I applied in my project.
I share for whom concern.