windows上获得高精度时间戳 GetSystemTimeAsFileTime

如果你需要高精度时间戳(绝对时间点),而不是计算相对时间,那么可以考虑这个API:

GetSystemTimeAsFileTime()

该函数返回自1601年1月1日起,到现在的时间(以 100ns 为单位)。

引用自: https://stackoverflow.com/questions/1695288/getting-the-current-time-in-milliseconds-from-the-system-clock-in-windows

The easiest (and most direct) way is to call GetSystemTimeAsFileTime(), which returns a FILETIME, a struct which stores the 64-bit number of 100-nanosecond intervals since midnight Jan 1, 1601.

At least at the time of Windows NT 3.1, 3.51, and 4.01, the GetSystemTimeAsFileTime() API was the fastest user-mode API able to retrieve the current time. It also offers the advantage (compared with GetSystemTime() -> SystemTimeToFileTime()) of being a single API call, that under normal circumstances cannot fail.

To convert a FILETIME ft_now; to a 64-bit integer named ll_now, use the following:

ll_now = (LONGLONG)ft_now.dwLowDateTime + ((LONGLONG)(ft_now.dwHighDateTime) << 32LL);

You can then divide by the number of 100-nanosecond intervals in a millisecond (10,000 of those) and you have milliseconds since the Win32 epoch.

To convert to the Unix epoch, subtract 116444736000000000LL to reach Jan 1, 1970.

You mentioned a desire to find the number of milliseconds into the current day. Because the Win32 epoch begins at a midnight, the number of milliseconds passed so far today can be calculated from the filetime with a modulus operation. Specifically, because there are 24 hours/day * 60 minutes/hour * 60 seconds/minute * 1000 milliseconds/second = 86,400,000 milliseconds/day, you could user the modulus of the system time in milliseconds modulus 86400000LL.

For a different application, one might not want to use the modulus. Especially if one is calculating elapsed times, one might have difficulties due to wrap-around at midnight. These difficulties are solvable, the best example I am aware is Linus Torvald’s line in the Linux kernel which handles counter wrap around.

Keep in mind that the system time is returned as a UTC time (both in the case of GetSystemTimeAsFileTime() and simply GetSystemTime()). If you require the local time as configured by the Administrator, then you could use GetLocalTime().

例如,想要获得从1601年1月1日以来,到目前为止所经历过的 微秒(micro seconds)数,可以这样做:

FILETIME fileTime;
long long now_long;
GetSystemTimeAsFileTime(&fileTime);
now_long = (long long(fileTime.dwHighDateTime) << 32) + fileTime.dwLowDateTime;
std::cout << "micro time from 1601.1.1:00:00:00 is: " << (now_long / 10) << std::endl;

猜你喜欢

转载自blog.csdn.net/gw569453350game/article/details/79638176