一、方法1
FileTime与Systime 的定义
1. typedef struct _FILETIME {
2. DWORD dwLowDateTime;
3. DWORD dwHighDateTime;
4. } FILETIME, *PFILETIME;
5. typedef struct _SYSTEMTIME {
6. WORD wYear;
7. WORD wMonth;
8. WORD wDayOfWeek;
9. WORD wDay;
10. WORD wHour;
11. WORD wMinute;
12. WORD wSecond;
13. WORD wMilliseconds;
14. } SYSTEMTIME, *PSYSTEMTIME;
比较一下,很明显,FILETIME与time_t类似,是64位整型,不过FILETIME是以100纳秒(ns)为单位。SYSTEMTIME与tm类似,不过多了一项wMilliseconds。可以看出,SDK时间比CRT的时间提供了更高的精度。同时SDK提供了更丰富的函数来处理时间。
这两个函数获得SYSTEMTIME形式的当前时间,不过GetSystemTime函数获得当前的UTC时间,GetLocalTime获得当前的本地时间,可以想象,获得的两个时间存在着时差。类似于CRT中提供tm与time_t之间的转换,SDK也提供了两个函数来转换SYSTEMTIME时间与FILETIME时间。
1. BOOL SystemTimeToFileTime(
2. const SYSTEMTIME* lpSystemTime,
3. LPFILETIME lpFileTime);
4. BOOL FileTimeToSystemTime(
5. const FILETIME* lpFileTime,
6. LPSYSTEMTIME lpSystemTime);
SDK还提供了两个很有趣的函数。
1. BOOL LocalFileTimeToFileTime(
2. const FILETIME* lpLocalFileTime,
3. LPFILETIME lpFileTime);
4. BOOL FileTimeToLocalFileTime(
5. const FILETIME* lpFileTime,
6. LPFILETIME lpLocalFileTime);
LocalFileTimeToFileTime函数将本地的FILETIME时间转换为对应的UTC的FILETIME时间。我觉得,这个函数只是通过将本地时间减去与UTC时间的时间差来实现转换,比如在东八区的本地时间转换为对应的UTC时间,只需要将本地时间减去8*60*60*1000*1000*10(单位100ns)。类似,FileTimeToLocalFileTime函数是将UTC时间转换为本地时间,它只是将减去时间差换成加上时间差。
1. SYSTEMTIME stLocal, stUTC, stUTC2;
2. FILETIME ftLocal, ftUTC, ft;
3. ULARGE_INTEGER uli;
4.
5. GetLocalTime(&stLocal);
6. GetSystemTime(&stUTC);
7. printf("Local System Time(YYYY-MM-DD HH:MM:SS): %d-%d-%d %d:%d:%d/n", stLocal.wYear, stLocal.wMonth,
8. stLocal.wDay, stLocal.wHour, stLocal.wMinute, stLocal.wSecond);
9. printf("UTC System Time (YYYY-MM-DD HH:MM:SS): %d-%d-%d %d:%d:%d/n", stUTC.wYear, stUTC.wMonth,
10. stUTC.wDay, stUTC.wHour, stUTC.wMinute, stUTC.wSecond);
11.
12. SystemTimeToFileTime(&stLocal, &ftLocal);
13. uli.LowPart = ftLocal.dwLowDateTime;
14. uli.HighPart = ftLocal.dwHighDateTime;
15. printf("Local File Time: %llu/n", uli.QuadPart);
16.
17. LocalFileTimeToFileTime(&ftLocal, &ftUTC);
18. uli.LowPart = ftUTC.dwLowDateTime;
19. uli.HighPart = ftUTC.dwHighDateTime;
20. printf("UTC File Time: %llu/n", uli.QuadPart);
21.
22. FileTimeToSystemTime(&ftUTC, &stUTC2);
23. printf("UTC System Time2 (YYYY-MM-DD HH:MM:SS): %d-%d-%d %d:%d:%d/n", stUTC2.wYear, stUTC2.wMonth,
24. stUTC2.wDay, stUTC2.wHour, stUTC2.wMinute, stUTC2.wSecond);
利用GetFileTime()函数获取文件时间
Handle hFile;
int iRet = hFile.Open(strPath.c_str(), CFile::modeRead);
FILETIME fCreateTime, fAccessTime, fWriteTime;
iRet = GetFileTime(hhFile.m_hFile, &fCreateTime, &fAccessTime, &fWriteTime);
1、但是这种获取的时间需要用FileTimeToSystemTime()进行转换。而且必须是转换为SYSTEMTIME 类型时间否则转换其他如CTime 则会导致时间变成一个无法想象的时间值
SYSTEMTIME sysTime;
ZeroMemory(&sysTime, sizeof(SYSTEMTIME));
FileTimeToSystemTime(&fCreateTime, &sysTime);
2、通过这种转换的时间也会存在另外一个问题,SYSTEMTIME 用的是格林尼治时间,而中国用的是东八区时间,转换后的时间会比原来时间少8个小时,因此需要在转换为系统时间之前进一步转换,转换为本地文件时间
FILETIME temp;
FileTimeToLocalFileTime(&fCreateTime ,&temp );
SYSTEMTIME sysTime;
ZeroMemory(&sysTime, sizeof(SYSTEMTIME));
FileTimeToSystemTime(&temp, &sysTime);
二、方法二
BOOL GetStatus(
CFileStatus& rStatus
) const;
static BOOL PASCAL GetStatus(
LPCTSTR lpszFileName,
CFileStatus& rStatus
);
1、CTime m_ctime The date and time the file was created. (文件创建时间)
2、CTime m_mtime The date and time the file was last modified.
3、CTime m_atime The date and time the file was last accessed for reading.
4、ULONGLONG m_size The logical size of the file in bytes, as reported by the DIR command. 逻辑大小
5、BYTE m_attribute The attribute byte of the file. 属性子节
6、char m_szFullName[_MAX_PATH] The absolute filename in the Windows character set. 文件名全称
CFileStatus fileStatus;
CFile::GetStatus(strPath.c_str(), fileStatus);
tsRecvDate = Format(_T("%d%02d%02d"),fileStatus.m_ctime.GetYear(), fileStatus.m_ctime.GetMonth(), fileStatus.m_ctime.GetDay());
三、方法三
TCHAR DirSpec[MAX];
WIN32_FIND_DATA FindFileData;
hFind=FindFirstFile(DirSpec,&FindFileData);
FindFileData.ftLastWriteTime 是一种FILETIME 类型的时间 可以通过方法一进行转换后得到所需要的时间;