《WindowsAPI巡礼》---GetSystemTime和GetFileTime

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;

    else return FALSE;

}

 

int _tmain(int argc, TCHAR *argv[])

{

    HANDLE hFile;

    TCHAR szBuf[MAX_PATH];

 

    if( argc != 2 )

    {

        printf("This sample takes a file name as a parameter/n");

        return 0;

    }

    hFile = CreateFile(argv[1], GENERIC_READ, FILE_SHARE_READ, NULL,

        OPEN_EXISTING, 0, NULL);

 

    if(hFile == INVALID_HANDLE_VALUE)

    {

        printf("CreateFile failed with %d/n", GetLastError());

        return 0;

    }

    if(GetLastWriteTime( hFile, szBuf, MAX_PATH ))

        _tprintf(TEXT("Last write time is: %s/n"), szBuf);

       

    CloseHandle(hFile);   

}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值