GetSystemTime和GetFileTime

本文详细介绍了Windows系统中用于操作时间的各种API,包括获取系统时间、文件时间的函数,以及不同时间格式之间的转换方法。通过具体示例展示了如何使用这些API进行实际应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

typedefstruct _FILETIME {

  DWORD dwLowDateTime; //低32位

  DWORD dwHighDateTime; //高32位

} FILETIME, *PFILETIME;

 

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

typedefstruct _SYSTEMTIME {

  WORD wYear;

  WORD wMonth;

  WORD wDayOfWeek;

  WORD wDay;

  WORD wHour;

  WORD wMinute;

  WORD wSecond;

  WORD wMilliseconds;

} SYSTEMTIME, *PSYSTEMTIME;

 

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

 

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 //返回的最后修改的日期和时间信息

);

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

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

函数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> 
 
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;
 
}

下面的代码使用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、付费专栏及课程。

余额充值