细节备忘——时间戳和夏令时

在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;

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值