Windows system time (filetime and systemtime)

Source: Internet
Author: User
Tags filetime

Reprinted please indicate the source, original address: http://blog.csdn.net/morewindows/article/details/8654298

Welcome to Weibo: http://weibo.com/MoreWindows

 

The previous summary of various timing functions in Windows introduces five common timing functions-time () and clock () in Standard C/C ++ (), in Windows, the API interfaces are timegettime (), gettickcount (), and queryperformancecounter (). The following describes the two structures of time in Windows: filetime and systemtime and related functions.

 

Let's take a look at the definitions of these two struct types:

1. filetime

// By morewindows-(http://blog.csdn.net/MoreWindows)

Typedef struct _ filetime {

Dworddwlowdatetime;

Dworddwhighdatetime;

} Filetime, * pfiletime, * lpfiletime;

Its Description on msdn -- contains a 64-bit value representing the number of 100-nanosecond intervals since January 1, 1601 (UTC ).

2. systemtime

// By morewindows-(http://blog.csdn.net/MoreWindows)

Typedef struct _ systemtime {

Wordwyear;

Wordwmonth;

Wordwdayofweek;

Wordwday;

Wordwhour;

Wordwminute;

Wordwsecond;

Wordwmilliseconds;

} Systemtime, * psystemtime, * lpsystemtime;

This does not need to be explained. Like the method that we usually use to represent time, it is a date (year-month-day) plus time (hour: minute: second)

 

The two struct-related functions mainly include getsystemtime, getlocaltime, systemtimetofiletime, filetimetosystemtime, localfiletimetofiletime, and filetimetolocalfiletime. Let's take a look at the usage of these functions:

1. Get the current UTC time

// By morewindows-(http://blog.csdn.net/MoreWindows)

Void getsystemtime (lpsystemtimelpsystemtime );

2. Get the local time

Void getlocaltime (lpsystemtimelpsystemtime );

 

3. Convert systemtime to filetime

// By morewindows-(http://blog.csdn.net/MoreWindows)

Boolsystemtimetofiletime (

Const systemtime * lpsystemtime,

Lpfiletimelpfiletime

);

4. Convert filetime to systemtime

Boolfiletimetosystemtime (

Const filetime * lpfiletime,

Lpsystemtimelpsystemtime

);

 

5. convert local time to UTC time

// By morewindows-(http://blog.csdn.net/MoreWindows)

Boollocalfiletimetofiletime (

Const filetime * lplocalfiletime,

Lpfiletimelpfiletime

);

6. Convert UTC time to local time

Boolfiletimetolocalfiletime (

Const filetime * lpfiletime,

Lpfiletimelplocalfiletime

);

 

An example is provided below. This example mainly uses two functions:

1. Obtain the current local system time

2. Open a file and change the file creation time, modification time, and access time from the filetime type to the systemtime type.

The complete code is as follows:

// Windows system time (filetime and systemtime) // By morewindows-(http://blog.csdn.net/morewindows/article/details/8654298) # include <windows. h> # include <stdio. h> # include <conio. h> class cwindowsdateandtime {public: static void getcurrentlocalsystemtime (char * pstrdate, char * pstrtime); static void filetimetolocalsystemtime (filetime & ft, char * pstrdate, char * pstrtime );}; // obtain the current local time void cwindowsdateandtime: getcurre Ntlocalsystemtime (char * pstrdate, char * pstrtime) {systemtime st; getlocaltime (& St); If (pstrdate! = NULL) sprintf (pstrdate, "% d-% d", st. wyear, st. wmonth, st. wday); If (pstrtime! = NULL) sprintf (pstrtime, "% 02d: % 02d: % 02d", St. whour, St. wminute, St. wsecond);} // convert the file time to the local time void cwindowsdateandtime: filetimetolocalsystemtime (filetime & ft, char * pstrdate, char * pstrtime) {filetime localft; filetimetolocalfiletime (& ft, & localft); systemtime st; filetimetosystemtime (& localft, & St); If (pstrdate! = NULL) sprintf (pstrdate, "% d-% d", st. wyear, st. wmonth, st. wday); If (pstrtime! = NULL) sprintf (pstrtime, "% 02d: % 02d: % 02d", St. whour, St. wminute, St. wsecond);} int main (INT argc, char * argv []) {printf ("Windows system time (filetime and systemtime) \ n "); printf ("-- by morewindows (http://blog.csdn.net/morewindows/article/details/8654298) -- \ n"); const int max_len = 30; char strdate [max_len], strtime [max_len]; cwindowsdateandtime :: getcurrentlocalsystemtime (strdate, strtime); printf ("Current System Time: % S % s \ n", strdate, strtime); const char * pstrfilename = "d :\\ morewindows.txt "; printf ("file % s: \ n", pstrfilename); handle handlefile = createfile (pstrfilename, generic_read, file_assist_read, null, open_existing, 0, null); filetime ftcreationtime, encrypted, ftlastwritetime; getfiletime (handlefile, & ftcreationtime, & expires, & ftlastwritetime); cwindowsdateandtime: filetimetolocalsystemtime (ftcreationtime, strdate, strtime); printf ("Creation Time: % S % s \ n ", strdate, strtime); cwindowsdateandtime: filetimetolocalsystemtime (ftlastaccesstime, strdate, strtime); printf (" Access time: % S % s \ n ", strdate, strtime); cwindowsdateandtime: filetimetolocalsystemtime (ftlastwritetime, strdate, strtime); printf ("modification time: % S % s \ n", strdate, strtime); getch (); return 0 ;}

The program running result is as follows:

 

Reprinted please indicate the source, original address: http://blog.csdn.net/morewindows/article/details/8654298

Welcome to Weibo: http://weibo.com/MoreWindows

 

Related Article

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.