假设您在 Windows 上,问题是 c# DateTime 从 0001 年 1 月 1 日开始,而 c++ FILETIME 从 1601 年 1 月 1 日开始,因此要获得具有 C# 值的 SYSTEMTIME,您需要这样的东西...
ULARGE_INTEGER uliTime;
uliTime.QuadPart = 634400640022968750; // Your sample value
SYSTEMTIME stSytemTime;
memset(&stSytemTime,0,sizeof(SYSTEMTIME));
FILETIME stFileTime;
memset(&stFileTime,0,sizeof(FILETIME));
// Fill FILETIME with your value
stFileTime.dwLowDateTime = uliTime.LowPart;
stFileTime.dwHighDateTime = uliTime.HighPart;
// Convert FILETIME so SYSTEMTIME
FileTimeToSystemTime(&stFileTime, &stSytemTime);
stSytemTime.wYear -= 1600; // Remove the "start" diference
将 SYSTEMTIME 转换为 time_t
void ConvertSystemTimeToTimeT(const SYSTEMTIME &stSystemTime, time_t &stTimeT)
{
// time_t min value is 1 January 1970
LARGE_INTEGER liJanuary1970 = {0};
liJanuary1970.QuadPart = 116444736000000000;
FILETIME stFileTime = {0};
SystemTimeToFileTime(&stSystemTime, &stFileTime);
ULARGE_INTEGER ullConverter;
ullConverter.LowPart = stFileTime.dwLowDateTime;
ullConverter.HighPart = stFileTime.dwHighDateTime;
// time_t resolution is 1 second, FILETIME is 100 nanoseconds, so convert to seconds and remove the 1970 value
stTimeT = (time_t)(ullConverter.QuadPart - liJanuary1970.QuadPart) / 10000000;
}