繁体   English   中英

如何在 C++ 中获取 Windows 纪元时间戳?

[英]How can I get the windows epoch timestamp in c++?

Windows 纪元时间是自 1601 年 1 月 1 日以来的 100 纳秒间隔。Linux 纪元通过自 1970 年 1 月 1 日 00:00:00 UTC 以来经过的秒数来衡量时间。

我的问题是:如何使用 C++ 在 Windows 系统中获取 Windows 时间戳纪元?

我找到的解决方案是

static const __int64 NC_SEC_FILETIME = 10000000;
static const __int64 NC_SEC_TO_UNIX_EPOCH = 11644473600; //windows epoch starts(1601-01-01T00:00:00Z); UNIX/Linux epoch starts (1970-01-01T00:00:00Z)

const auto p1 = std::chrono::system_clock::now();
__int64 time_linux =    p1.time_since_epoch().count();
time_windows = NC_SEC_FILETIME * time_linux + NC_SEC_FILETIME + NC_SEC_TO_UNIX_EPOCH;

效果很好。

我发现的智能解决方案:

#include <chrono>
#include <boost/date_time/posix_time/posix_time.hpp>
    __int64 getEpochTime()
    {
        FILETIME ft;
        GetSystemTimeAsFileTime(&ft);
        const __int64 time = (__int64(ft.dwHighDateTime) << 32) + ft.dwLowDateTime;
        return time;
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM