轉載請標明出處,原文地址:http://blog.csdn.net/morewindows/article/details/8654298
歡迎關注微博:http://weibo.com/MoreWindows
前面的《Windows 各種計時函數總結》介紹了Windows系統常用的5種計時函數——標準C/C++下的time()及clock(),在Windows系統下的API介面timeGetTime()、GetTickCount()及QueryPerformanceCounter()。下面來介紹下Windows系統中表示時間的兩個結構體——FILETIME和SYSTEMTIME及相關函數。
先來看看這兩個結構體的定義:
1. FILETIME
//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).
2. SYSTEMTIME
//By MoreWindows-(http://blog.csdn.net/MoreWindows)
typedef struct _SYSTEMTIME {
WORDwYear;
WORDwMonth;
WORDwDayOfWeek;
WORDwDay;
WORDwHour;
WORDwMinute;
WORDwSecond;
WORDwMilliseconds;
} SYSTEMTIME, *PSYSTEMTIME, *LPSYSTEMTIME;
這個就不用解釋了,和大家平常表示時間的方法一樣,都是日期(年-月-日)加時間(小時:分鐘:秒)
與這兩個結構體相關的函數主有6個——GetSystemTime、GetLocalTime、SystemTimeToFileTime、FileTimeToSystemTime、LocalFileTimeToFileTime、FileTimeToLocalFileTime。下面來看看這些函數的用法:
一.得到當前UTC時間
//By MoreWindows-(http://blog.csdn.net/MoreWindows)
void GetSystemTime(LPSYSTEMTIMElpSystemTime);
二.得到當地時間
void GetLocalTime(LPSYSTEMTIMElpSystemTime);
三.SYSTEMTIME轉成FILETIME
//By MoreWindows-(http://blog.csdn.net/MoreWindows)
BOOLSystemTimeToFileTime(
const SYSTEMTIME* lpSystemTime,
LPFILETIMElpFileTime
);
四.FILETIME轉成SYSTEMTIME
BOOLFileTimeToSystemTime(
const FILETIME* lpFileTime,
LPSYSTEMTIMElpSystemTime
);
五.當地時間轉成UTC時間
//By MoreWindows-(http://blog.csdn.net/MoreWindows)
BOOLLocalFileTimeToFileTime(
const FILETIME* lpLocalFileTime,
LPFILETIMElpFileTime
);
六.UTC時間轉成當地時間
BOOLFileTimeToLocalFileTime(
const FILETIME* lpFileTime,
LPFILETIMElpLocalFileTime
);
下面再給出一個樣本,這個樣本主要使用兩個功能:
1.對擷取當前當地系統時間
2.開啟一個檔案,並將檔案的建立時間,修改時間,訪問時間從FILETIME類型轉成SYSTEMTIME類型。
完整代碼如下:
// 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;}
程式運行結果如下:
轉載請標明出處,原文地址:http://blog.csdn.net/morewindows/article/details/8654298
歡迎關注微博:http://weibo.com/MoreWindows