WINDOWS api--getfiletime--Get file time

Source: Internet
Author: User
Tags filetime local time

The FILETIME structure contains the date and time information for the file or directory: (since January 1, 1601, the unit is 100 nanoseconds)

struct _filetime {  // Low 32-bit    // High 32-bit  *pfiletime;

The SYSTEMTIME structure contains user-identifiable system date information:

typedefstruct_systemtime {WORD wyear;//yearsWORD wmonth;//MonthWORD Wdayofweek;//the day of the weekWORD wday;//DayWORD Whour;//hoursWORD Wminute;//pointsWORD Wsecond;//secondsWORD wmilliseconds;//milliseconds} SYSTEMTIME,*psystemtime;

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

The function filetimetosystemtime is used to convert the file time format to the standard system time format:

BOOL WINAPI filetimetosystemtime (  __in   const// file time   __out   // System Time );

The function filetimetolocaltime is used to convert the file time format to a local file time:

BOOL WINAPI filetimetolocalfiletime (  __in          const filetime* lpfiletime,// file Time   __out         lpfiletime lplocalfiletime// Local file time );

The function systemtimetofiletime is the conversion of standard system time into a file time format:

BOOL WINAPI systemtimetofiletime (   __in   const SYSTEMTIME *lpsystemtime,//  System time   __out  lpfiletime lpfiletime// file time );

The function systemtimetotzspecificlocaltime is to convert the standard system time to the local system time

BOOL WINAPI systemtimetotzspecificlocaltime (__in          lptime_zone_information lptimezone,// time zone structure __in          lpsystemtime lpuniversaltime,// system time __out         lpsystemtime Lplocaltime// local time );

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

The GetSystemTime function is used to obtain the system time:

void WINAPI getsystemtime (   __out  lpsystemtime lpsystemtime);

The getfiletime function is used to obtain the time of creation of a file or directory, the last access time, and the last modified time:

BOOL WINAPI getfiletime (  __in       // file or directory handle  __out_opt  // returns the date and time information  that was created __out_opt  // returns the last accessed date and time information  __out_opt  // returns the last modified date and time information );

Instance:

CString strpath ("D:\\test.txt"); HANDLE hfile=CreateFile (strpath, Generic_write| Generic_read,//must have Generic_read attribute to get timeFile_share_read, NULL,                          Truncate_existing, File_attribute_normal,                 NULL); if(hfile! =Invalid_handle_value)    {SYSTEMTIME sysTime; GetSystemTime (&systime);//the time we get here is the standard system time, which is 0 time zone. Getlocaltime (&systime);//this gets the local time, which is the standard Time + time zone.FILETIME fcreatetime, Faccesstime, Fwritetime; Getfiletime (&hfile, &fcreatetime, &faccesstime, &fwritetime);//Get file TimeCString strtime;//two ways to convert the file time to the local system time://(1)FileTimeToLocalFileTime (&fcreatetime,&localtime);//convert file time to local file timeFileTimeToSystemTime (&localtime, &systime);//convert file time to Local system time//(2)FileTimeToSystemTime (&fcreatetime, &systime);//convert file time to standard system timeSystemtimetotzspecificlocaltime (&systime, &systime)//Convert standard system time to local system timeStrtime.format (_t ("%4d year%2d month%2d day,%2d:%2d:%2d"), Systime.wyear, Systime.wmonth, Systime.wday, Systime.whour, Systime.wminute, Systime.wsecond);}

Repair file creation time, example:

#include <Windows.h>#include<stdio.h>BOOLConvertfiletimetolocaltime (ConstFILETIME *lpfiletime, SYSTEMTIME *lpsystemtime) {      if(!lpfiletime | |!lpsystemtime) {          return false;      } FILETIME ftlocal; FileTimeToLocalFileTime (Lpfiletime,&ftlocal); FileTimeToSystemTime (&ftlocal, Lpsystemtime); return true; }    BOOLConvertlocaltimetofiletime (ConstSYSTEMTIME *lpsystemtime, FILETIME *lpfiletime) {      if(!lpsystemtime | |!lpfiletime) {          return false;      } FILETIME ftlocal; SystemTimeToFileTime (Lpsystemtime,&ftlocal); LocalFileTimeToFileTime (&ftlocal, Lpfiletime); return true; }    intMain () {HANDLE hfile;      FILETIME ftcreate, ftaccess, Ftwrite;      SYSTEMTIME stcreate, staccess, stwrite; intYear , 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; }  

WINDOWS api--getfiletime--Get file time

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.