【发布时间】:2022-12-16 17:27:26
【问题描述】:
Windows 纪元时间是从 1601 年 1 月 1 日开始的 100 纳秒间隔。 Linux 纪元通过自 1970 年 1 月 1 日 00:00:00 UTC 以来经过的秒数来衡量时间。
我的问题是:如何使用 C++ 在 Windows 系统中获取 Windows 时间戳纪元?
【问题讨论】:
-
在任何时间系统中,epoch 显然为 0
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;
}
【讨论】: