现在的位置: 首页 > 综合 > 正文

《WindowsAPI巡礼》—GetSystemTime和GetFileTime

2013年08月26日 ⁄ 综合 ⁄ 共 2608字 ⁄ 字号 评论关闭

FILETIME结构包含了文件或目录的日期和时间信息:(自160111日以来,单位为100纳秒)

typedef struct _FILETIME {

  DWORD dwLowDateTime; //32

  DWORD dwHighDateTime; //32

} FILETIME, *PFILETIME;

 

SYSTEMTIME结构包含了用户可识别的系统日期信息:

typedef struct _SYSTEMTIME {

  WORD wYear;

  WORD wMonth;

  WORD wDayOfWeek;

  WORD wDay;

  WORD wHour;

  WORD wMinute;

  WORD wSecond;

  WORD wMilliseconds;

} SYSTEMTIME, *PSYSTEMTIME;

 

=======================================================

函数FileTimeToSystemTime用来将文件时间格式转换为系统时间格式:

BOOL WINAPI FileTimeToSystemTime(

  __in   const FILETIME *lpFileTime, //文件时间

  __out  LPSYSTEMTIME lpSystemTime //系统时间

);

函数SystemTimeToFileTime则是将系统时间转换成文件时间格式:

BOOL WINAPI SystemTimeToFileTime(

  __in   const SYSTEMTIME *lpSystemTime,

  __out  LPFILETIME lpFileTime

);

 

将文件时间转换为系统时间的实例如下:

#include <windows.h>

 

// SetFileToCurrentTime - sets last write time to current system time

// Return value - TRUE if successful, FALSE otherwise

// hFile  - must be a valid file handle

 

BOOL SetFileToCurrentTime(HANDLE hFile)

{

    FILETIME ft;

    SYSTEMTIME st;

    BOOL f;

 

    GetSystemTime(&st);              // Gets the current system time

    SystemTimeToFileTime(&st, &ft);  // Converts the current system time to file time format

    f = SetFileTime(hFile,           // Sets last-write time of the file

        (LPFILETIME) NULL,           // to the converted current system time

        (LPFILETIME) NULL,

        &ft);   

 

    return f;

}

=======================================================

GetSystemTime函数用来获得系统时间:

void WINAPI GetSystemTime(

  __out  LPSYSTEMTIME lpSystemTime

);

 

GetFileTime函数用来获得一个文件或目录的创建的时间、最后访问的时间以及最后修改的时间:

BOOL WINAPI GetFileTime(

  __in       HANDLE hFile, //文件或目录句柄

  __out_opt  LPFILETIME lpCreationTime, //返回的创建的日期和时间信息

  __out_opt  LPFILETIME lpLastAccessTime, //返回的最后访问的日期和时间信息

  __out_opt  LPFILETIME lpLastWriteTime //返回的最后修改的日期和时间信息

);

返回值:成功时,返回非零值;失败时,返回零值。

 

下面的代码使用GetFileTime函数来获得文件的最后写入时间:

#include <windows.h>

#include <tchar.h>

#include <strsafe.h>

 

// GetLastWriteTime - Retrieves the last-write time and converts

//                    the time to a string

//

// Return value - TRUE if successful, FALSE otherwise

// hFile      - Valid file handle

// lpszString - Pointer to buffer to receive string

 

BOOL GetLastWriteTime(HANDLE hFile, LPTSTR lpszString, DWORD dwSize)

{

    FILETIME ftCreate, ftAccess, ftWrite;

    SYSTEMTIME stUTC, stLocal;

    DWORD dwRet;

 

    // Retrieve the file times for the file.

    if (!GetFileTime(hFile, &ftCreate, &ftAccess, &ftWrite))

        return FALSE;

 

    // Convert the last-write time to local time.

    FileTimeToSystemTime(&ftWrite, &stUTC);

    SystemTimeToTzSpecificLocalTime(NULL, &stUTC, &stLocal);

 

    // Build a string showing the date and time.

    dwRet = StringCchPrintf(lpszString, dwSize,

        TEXT("%02d/%02d/%d  %02d:%02d"),

        stLocal.wMonth, stLocal.wDay, stLocal.wYear,

        stLocal.wHour, stLocal.wMinute);

 

    if( S_OK == dwRet )

        return TRUE;

抱歉!评论已关闭.