zoukankan      html  css  js  c++  java
  • WINDOWS API ——GETFILETIME——获取文件时间

    GetSystemTime(LPSYSTEMTIME lpSystemTime)
    得到系统时间,这个时间是标准的UTC时间,也就是没有包含任何时区的时间的
    GetLocalTime(LPSYSTEMTIME lpSystemTime)
    得到当前时区的时间,它获取的是系统设置的地区的当地时间

    FILETIME结构包含了文件或目录的日期和时间信息:(自1601年1月1日以来,单位为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 //系统时间
    
    );

    函数FileTimeToLocalTime用来将文件时间格式转换为本地文件时间:

     BOOL WINAPI FileTimeToLocalFileTime(
      __in          const FILETIME* lpFileTime,//文件时间
       __out         LPFILETIME lpLocalFileTime//本地文件时间
    );

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

    BOOL WINAPI SystemTimeToFileTime(
       __in   const SYSTEMTIME *lpSystemTime,//系统时间
       __out  LPFILETIME lpFileTime//文件时间
     );

    函数SystemTimeToTzSpecificLocalTime是将标准系统时间转换为本地系统时间

    BOOL WINAPI SystemTimeToTzSpecificLocalTime(
     __in          LPTIME_ZONE_INFORMATION lpTimeZone,//时区结构
     __in          LPSYSTEMTIME lpUniversalTime,//系统时间
     __out         LPSYSTEMTIME lpLocalTime//本地时间
    );

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

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

    实例:

    CString strPath("D:\test.txt");
    HANDLE hFile = CreateFile(strPath,              
                              GENERIC_WRITE| GENERIC_READ,  //必须有GENERIC_READ属性才能得到时间     
                              FILE_SHARE_READ,                      
                              NULL,                   
                              TRUNCATE_EXISTING,         
                              FILE_ATTRIBUTE_NORMAL,
                              NULL);                 
    
    if (hFile != INVALID_HANDLE_VALUE) 
    { 
        SYSTEMTIME sysTime;
        GetSystemTime(&sysTime);//这里得到的时间是标准系统时间,也就是0时区的时间。
        GetLocalTime(&sysTime);//这里得到的是本地时间,也就是标准时间+时区时间
    
        FILETIME fCreateTime, fAccessTime, fWriteTime;
    
        GetFileTime(&hFile, &fCreateTime, &fAccessTime, &fWriteTime);//获取文件时间
    
        CString strTime;
    
    //将文件时间转换为本地系统时间的两种方式:
    //(1)
        FileTimeToLocalFileTime(&fCreateTime,&localTime);//将文件时间转换为本地文件时间
        FileTimeToSystemTime(&localTime, &sysTime);//将文件时间转换为本地系统时间
    
    //(2)
        FileTimeToSystemTime(&fCreateTime, &sysTime);//将文件时间转换为标准系统时间
        SystemTimeToTzSpecificLocalTime(&sysTime, &sysTime)//将标准系统时间转换为本地系统时间
    
            strTime.Format(_T("%4d年%2d月%2d日,%2d:%2d:%2d"),
            sysTime.wYear,
            sysTime.wMonth,
            sysTime.wDay,
            sysTime.wHour,
            sysTime.wMinute,
            sysTime.wSecond
            );
    }

     修文件创建时间,例子:

    #include <Windows.h>  
    #include <stdio.h>  
      
    bool ConvertFileTimeToLocalTime(const FILETIME *lpFileTime, SYSTEMTIME *lpSystemTime)  
    {  
        if (!lpFileTime || !lpSystemTime) {  
            return false;  
        }  
        FILETIME ftLocal;  
        FileTimeToLocalFileTime(lpFileTime, &ftLocal);  
        FileTimeToSystemTime(&ftLocal, lpSystemTime);  
        return true;  
    }  
      
    bool ConvertLocalTimeToFileTime(const SYSTEMTIME *lpSystemTime, FILETIME *lpFileTime)  
    {  
        if (!lpSystemTime || !lpFileTime) {  
            return false;  
        }  
      
        FILETIME ftLocal;  
        SystemTimeToFileTime(lpSystemTime, &ftLocal);  
        LocalFileTimeToFileTime(&ftLocal, lpFileTime);  
        return true;  
    }  
      
    int main()  
    {  
        HANDLE hFile;  
        FILETIME ftCreate, ftAccess, ftWrite;  
        SYSTEMTIME stCreate, stAccess, stWrite;  
        int year, month, day;  
      
        hFile = CreateFile(L"C:\1.txt", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);  
        if (INVALID_HANDLE_VALUE == hFile) {  
            printf("CreateFile error: %d", GetLastError());  
            ExitProcess(0);  
        }  
        GetFileTime(hFile, &ftCreate, &ftAccess, &ftWrite);  
        ConvertFileTimeToLocalTime(&ftCreate, &stCreate);  
        ConvertFileTimeToLocalTime(&ftAccess, &stAccess);  
        ConvertFileTimeToLocalTime(&ftWrite, &stWrite);  
      
        printf("yyyy-MM-dd:");  
        scanf("%d-%d-%d", &year, &month, &day);  
        stAccess.wYear = stWrite.wYear = year;  
        stAccess.wMonth = stWrite.wMonth = month;  
        stAccess.wDay = stWrite.wDay = day;  
      
        ConvertLocalTimeToFileTime(&stAccess, &ftAccess);  
        ConvertLocalTimeToFileTime(&stWrite, &ftWrite);  
      
        SetFileTime(hFile, &ftCreate, &ftAccess, &ftWrite);  
        CloseHandle(hFile);  
        return 0;  
    }  
  • 相关阅读:
    Martix工作室考核题 —— 打印一个菱形
    Martix工作室考核题 —— 打印一个菱形
    Martix工作室考核题 —— 打印九九乘法表
    Martix工作室考核题 —— 打印九九乘法表
    Martix工作室考核题 —— 打印九九乘法表
    Martix工作室考核题 —— 201938 第三题
    Martix工作室考核题 —— 201938 第三题
    Martix工作室考核题 —— 201938 第三题
    Martix工作室考核题 —— 201938 第一题
    fiddler模拟发送post请求
  • 原文地址:https://www.cnblogs.com/renyuan/p/7612718.html
Copyright © 2011-2022 走看看