windows上获得高精度时间戳 GetSystemTimeAsFileTime

如果你需要高精度时间戳(绝对时间点),而不是计算相对时间,那么可以考虑这个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);
}

引用自: 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;
  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值