在windows系统上,如果我们把时区设置在有冬夏令时分别的时区,就会发现,每次到了冬夏令时交替的时候,windows系统上的文件显示的最后修改时间都会改变。同样,vc提供的很多获取文件时间戳的函数,也不是365天恒定的。比如GetFileTime, _findfirst等,而unix上面获取的文件时间戳往往是恒定的,没有冬夏令时交替的windows上时间戳也是恒定的。测试发现,vc提供的FindFirstFile可以通过一系列转换,获得一个恒定的时间戳。示例代码如下:
// --------------------------------------------------------------------
// Function : GetFileUTCTime
//
// Description : Get the last modified time based on UTC.
//
//
// Returns : long
// return the time the file was last modified.
//
// Parameters :
// szFileName
// [in]Filename.The file must be exist.
// --------------------------------------------------------------------
long GetFileUTCTime(const char *szFileName)
{
//Get the file attributes .
WIN32_FIND_DATA FindFileData ={0};
BOOL bNext = TRUE;
HANDLE hFind =FindFirstFile(szFileName,
&FindFileData);
FindClose(hFind);
//convert filetime to UINT64
UINT64 llCptRt = 0;
int iaTemp[2];
iaTemp[1] = FindFileData.ftLastWriteTime.dwHighDateTime;
iaTemp[0] = FindFileData.ftLastWriteTime.dwLowDateTime;
memcpy(&llCptRt, iaTemp, 8);
//convert the number of 100-nanosecond intervals since January 1, 1601 (UTC)
//to the number of second intervals since January 1, 1970 (UTC)
llCptRt /= 10000000;
llCptRt -= (UINT64)11644473600;
return (long)llCptRt;
}