//By MoreWindows-(http://blog.csdn.net/MoreWindows) typedef struct _FILETIME { DWORDdwLowDateTime; DWORDdwHighDateTime; } FILETIME, *PFILETIME, *LPFILETIME;它在MSDN上的说明——Contains a 64-bit value representing the number of 100-nanosecond intervals since January 1, 1601 (UTC时间).
//By MoreWindows-(http://blog.csdn.net/MoreWindows) typedef struct _SYSTEMTIME { WORD wYear; WORD wMonth; WORD wDayOfWeek; WORD wDay; WORD wHour; WORD wMinute; WORD wSecond; WORD wMilliseconds; } SYSTEMTIME, *PSYSTEMTIME, *LPSYSTEMTIME;这个就不用解释了,和大家平常表示时间的方法一样,都是日期(年-月-日)加时间(小时:分钟:秒)
//By MoreWindows-(http://blog.csdn.net/MoreWindows) void GetSystemTime(LPSYSTEMTIMElpSystemTime);
void GetLocalTime(LPSYSTEMTIMElpSystemTime);
//By MoreWindows-(http://blog.csdn.net/MoreWindows) BOOLSystemTimeToFileTime( const SYSTEMTIME* lpSystemTime, LPFILETIMElpFileTime );
BOOLFileTimeToSystemTime( const FILETIME* lpFileTime, LPSYSTEMTIMElpSystemTime );
//By MoreWindows-(http://blog.csdn.net/MoreWindows) BOOLLocalFileTimeToFileTime( const FILETIME* lpLocalFileTime, LPFILETIMElpFileTime );
BOOLFileTimeToLocalFileTime( const FILETIME* lpFileTime, LPFILETIMElpLocalFileTime );
完整代码如下:
// Windows系统时间(FILETIME和SYSTEMTIME) //By MoreWindows-(http://blog.csdn.net/morewindows/article/details/8654298) #include <windows.h> #include <stdio.h> #include <conio.h> class CWindowsDateAndTime { public: static void GetCurrentLocalSystemTime(char *pstrDate, char *pstrTime); static void FileTimeToLocalSystemTime(FILETIME &ft, char *pstrDate, char *pstrTime); }; //得到当前当地时间 void CWindowsDateAndTime::GetCurrentLocalSystemTime(char *pstrDate, char *pstrTime) { SYSTEMTIME st; GetLocalTime(&st); if (pstrDate != NULL) sprintf(pstrDate, "%d-%d-%d", st.wYear, st.wMonth, st.wDay); if (pstrTime != NULL) sprintf(pstrTime, "%02d:%02d:%02d", st.wHour, st.wMinute, st.wSecond); } //文件时间转成当地时间 void CWindowsDateAndTime::FileTimeToLocalSystemTime(FILETIME &ft, char *pstrDate, char *pstrTime) { FILETIME localft; FileTimeToLocalFileTime(&ft, &localft); SYSTEMTIME st; FileTimeToSystemTime(&localft, &st); if (pstrDate != NULL) sprintf(pstrDate, "%d-%d-%d", st.wYear, st.wMonth, st.wDay); if (pstrTime != NULL) sprintf(pstrTime, "%02d:%02d:%02d", st.wHour, st.wMinute, st.wSecond); } int main(int argc, char *argv[]) { printf(" Windows系统时间(FILETIME和SYSTEMTIME) \n"); printf(" -- By MoreWindows( http://blog.csdn.net/morewindows/article/details/8654298 ) --\n\n"); const int MAX_LEN = 30; char strDate[MAX_LEN], strTime[MAX_LEN]; CWindowsDateAndTime::GetCurrentLocalSystemTime(strDate, strTime); printf("当前系统时间: %s %s\n", strDate, strTime); const char* pstrFileName = "D:\\MoreWindows.txt"; printf("文件%s:\n", pstrFileName); HANDLE handleFile = CreateFile(pstrFileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL); FILETIME ftCreationTime, ftLastAccessTime, ftLastWriteTime; GetFileTime(handleFile, &ftCreationTime, &ftLastAccessTime, &ftLastWriteTime); CWindowsDateAndTime::FileTimeToLocalSystemTime(ftCreationTime, strDate, strTime); printf("创建时间: %s %s\n", strDate, strTime); CWindowsDateAndTime::FileTimeToLocalSystemTime(ftLastAccessTime, strDate, strTime); printf("访问时间: %s %s\n", strDate, strTime); CWindowsDateAndTime::FileTimeToLocalSystemTime(ftLastWriteTime, strDate, strTime); printf("修改时间: %s %s\n", strDate, strTime); getch(); return 0; }程序运行结果如下: