如何将 GetLocalTime 返回的 SYSTEMTIME 结构转换为时间戳(自 1970 年以来的秒数,ULONG)?
一种方法是将引用时间和目标时间都转换为 FILETIME
结构,这样您就可以对它们的值执行简单的算术运算。这些以 100ns 为单位测量,因此您需要将最终结果除以 10000000 以获得秒数。
// reference time, convert to FILETIME
SYSTEMTIME stRef{};
stRef.wYear = 1970;
stRef.wMonth = 1;
stRef.wDay = 1;
FILETIME ftRef;
SystemTimeToFileTime(&stRef, &ftRef);
// target time, convert to FILETIME
SYSTEMTIME stTarget;
GetLocalTime(&stTarget);
FILETIME ftTarget;
SystemTimeToFileTime(&stTarget, &ftTarget);
// convert both to ULARGE_INTEGER
ULARGE_INTEGER ulRef, ulTarget;
ulRef.HighPart = ftRef.dwHighDateTime;
ulRef.LowPart = ftRef.dwLowDateTime;
ulTarget.HighPart = ftTarget.dwHighDateTime;
ulTarget.LowPart = ftTarget.dwLowDateTime;
// subtract reference time from target time
ulTarget.QuadPart -= ulRef.QuadPart;
// convert to seconds (divide by 10000000)
DWORD dwSecondsSinceRefTime = ulTarget.QuadPart / 10000000i64;
我是一名优秀的程序员,十分优秀!