曲径通幽论坛

标题: FileTimeToLocalFileTime() -- 将文件时间转换为本地文件时间 [打印本页]

作者: beyes    时间: 2012-2-16 23:34
标题: FileTimeToLocalFileTime() -- 将文件时间转换为本地文件时间
FileTimeToLocalFileTime() 原型如下:
[C++] 纯文本查看 复制代码
BOOL WINAPI FileTimeToLocalFileTime(
  __in   const FILETIME *lpFileTime,
  __out  LPFILETIME lpLocalFileTime
);

该函数用来将文件时间转换为本地时间。在转换之前,文件时间是 UTC 时间,而像中国的北京时间又比 UTC 时间领先了 8 个小时,因此在查看一些文件时间的时候,需要先进行相应的转换。

测试代码
[C++] 纯文本查看 复制代码
#include "stdafx.h"

DWORD ChangeTime (PFILETIME lptime)
{
    FILETIME ftLocal;    //文件时间结构

    SYSTEMTIME st;        //系统时间结构

    FileTimeToSystemTime(lptime, &st);

    _tprintf (TEXT("%4d年%2d月%2d日  %2d:%2d:%2d\n"), st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond);

    return 0;
}

int _tmain(int argc, _TCHAR* argv[])
{
    setlocale(LC_ALL, "chs");

    WIN32_FILE_ATTRIBUTE_DATA wfad;

    if (!GetFileAttributesEx(argv[1], GetFileExInfoStandard, &wfad)) {
        _tprintf (TEXT("获取文件属性错误:%d\n"), GetLastError());
        return (-1);
    }
    ChangeTime (&wfad.ftCreationTime);

    return 0;
}

按照上面代码,直接将文件时间转换为系统时间,那么将显示为:
D:\WinAPI\GetFileAttEx\Debug>GetFileAttEx.exe D:\special.php
2011年10月25日   6: 9:19
如果在资源管理器里查看该时间,会看到:
[attach]217[/attach]
如果使用 FileTimeToLocalFileTime(lptime, &ftLocal); 先进行转换,那么时间就显示一致了:
D:\WinAPI\GetFileAttEx\Debug>GetFileAttEx.exe D:\special.php
2011年-10月25日  14: 9:19





欢迎光临 曲径通幽论坛 (http://www.groad.net/bbs/) Powered by Discuz! X3.2