windows FILETIME时间从1601/01/01 零时零分零秒开始计时,windows每个时钟滴答将计数加一,每个时钟滴答的间隔是100 nanoseconds(纳秒,1秒=10的九次方纳秒),即每个时钟滴答= 10 (-7)秒。
unix、linux时间是从1970/01/01零时零分零秒开始计数,每秒计数加1. 197001/01与1601/01/01的时间间隔是11644473600秒。
所以从windows滴答时间转化为unix时间的代码为:
#define WINDOWS_TICK 10000000 //10的7次方 #define SEC_TO_UNIX_EPOCH 11644473600LL //1601与1970的时间间隔 unsigned WindowsTickToUnixSeconds(long long windowsTicks) { return (unsigned)(windowsTicks / WINDOWS_TICK - SEC_TO_UNIX_EPOCH); }
从timeval转化为FILETIME的代码为:
void FileTimeToTimeval(FILETIME *pFileTime, struct timeval *pTimeval) { CalcOffsetFrom1601To1970(); //11644473600LL*10^7 ULONGLONG ul64FileTime = 0; ul64FileTime |= pFileTime->dwHighDateTime; ul64FileTime <<= 32; ul64FileTime |= pFileTime->dwLowDateTime; ul64FileTime -= g_ullOffsetFrom1601To1970; pTimeval->tv_sec = (long)(ul64FileTime / 10000000); pTimeval->tv_usec = (long)((ul64FileTime % 10000000) / 10); } void TimevalToFileTime(struct timeval *pTimeval, FILETIME *pFileTime) { CalcOffsetFrom1601To1970(); ULONGLONG x = pTimeval->tv_sec * NSPERSEC + pTimeval->tv_usec * 10; x += g_ullOffsetFrom1601To1970; pFileTime->dwHighDateTime = (ULONG)(x >> 32); pFileTime->dwLowDateTime = (ULONG)x; }