如果你需要高精度时间戳(绝对时间点),而不是计算相对时间,那么可以考虑这个API:
GetSystemTimeAsFileTime()
该函数返回自1601年1月1日起,到现在的时间(以 100ns 为单位)。
其实,Windows上的 std::clock()
函数就是通过 GetSystemTimeAsFileTime()
来实现的,只不过 std::clock()
返回的是从程序启动到当前的时间(毫秒数),如下:
clock_t __cdecl clock (
void
)
{
unsigned __int64 current_tics;
FILETIME ct;
GetSystemTimeAsFileTime( &ct );
current_tics = (unsigned __int64)ct.dwLowDateTime +
(((unsigned __int64)ct.dwHighDateTime) << 32);
/* calculate the elapsed number of 100 nanosecond units */
current_tics -= start_tics; // start_tics 在程序启动时初始化
/* return number of elapsed milliseconds */
return (clock_t)(current_tics / 10000);
}
The easiest (and most direct) way is to call
GetSystemTimeAsFileTime()
, which returns aFILETIME
, 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 withGetSystemTime() -> SystemTimeToFileTime()
) of being a single API call, that under normal circumstances cannot fail.To convert a
FILETIME
ft_now
; to a 64-bit integer namedll_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;