在 Windows 下使用 C++ 以毫秒为单位的 UTC 时间戳
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2864450/
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
UTC timestamp in millisecond using C++ under Windows
提问by Siddiqui
How do I get the UTC time in milliseconds under the Windows platform?
在Windows平台下如何获取以毫秒为单位的UTC时间?
I am using the standard library which give me UTC time in seconds. I want to get the time in milliseconds. Is there a reference of another library which give me the accurate UTC time in milliseconds?
我正在使用标准库,它以秒为单位为我提供 UTC 时间。我想以毫秒为单位获取时间。是否有另一个库的参考,它以毫秒为单位为我提供准确的 UTC 时间?
回答by avakar
Use GetSystemTime
API function, or perhaps GetSystemTimeAsFileTime
if you want a single number.
使用GetSystemTime
API 函数,或者GetSystemTimeAsFileTime
如果您想要一个数字。
回答by Hans Passant
GetSystemTime() produces a UTC time stamp with millisecond resolution. Accuracy however is far worse, the clock usually updates at 15.625 millisecond intervals on most Windows machines. There isn't much point in chasing improved accuracy, any clock that provides an absolute time stamp is subject to drift. You'd need dedicated hardware, usually a GPS radio clock, to get something better. Which are hard to use properly on a non-realtime multi-tasking operating system. Worst-case latency can be as much as 200 milliseconds.
GetSystemTime() 生成具有毫秒分辨率的 UTC 时间戳。然而,精度要差得多,在大多数 Windows 机器上,时钟通常以 15.625 毫秒的间隔更新。追求更高的精度没有多大意义,任何提供绝对时间戳的时钟都会发生漂移。您需要专用硬件,通常是 GPS 无线电时钟,以获得更好的效果。在非实时多任务操作系统上很难正确使用。最坏情况下的延迟可能高达 200 毫秒。