Windows Time (systemtime and filetime ).

Source: Internet
Author: User
Tags filetime

The previous article introduced the time processing functions in the C Runtime Library. This article describes the time functions provided in the Windows SDK. There is no essential difference between the two time systems (in fact, the CRT time is implemented by Windows Time, of course this is implemented by VC), and the conversion between local time and UTC time is also provided. However, the TM time in the CRT corresponds to the system time in the SDK, And the time_t time in the CRT corresponds to the file time in the SDK ), that "special time" is also the midnight of June.

Of course, you must first understand the filetime and systemtime definitions.

View plaincopy to clipboardprint?
  1. Typedef struct _ filetime {
  2. DWORD dwlowdatetime;
  3. DWORD dwhighdatetime;
  4. } Filetime, * pfiletime;
  5. Typedef struct _ systemtime {
  6. Word wyear;
  7. Word wmonth;
  8. Word wdayofweek;
  9. Word wday;
  10. Word whour;
  11. Word wminute;
  12. Word wsecond;
  13. Word wmilliseconds;
  14. } Systemtime, * psystemtime;

Typedef struct _ filetime {<br/> DWORD dwlowdatetime; <br/> DWORD dwhighdatetime; <br/>} filetime, * pfiletime; <br/> typedef struct _ systemtime {<br/> word wyear; <br/> word wmonth; <br/> word wdayofweek; <br/> word wday; <br/> word whour; <br/> word wminute; <br/> word wsecond; <br/> word wmilliseconds; <br/>} systemtime, * psystemtime;

Obviously, filetime is similar to time_t and is a 64-bit integer, but filetime is measured in 100 nanoseconds (NS. Systemtime is similar to TM, but wmilliseconds is added. It can be seen that the SDK time provides higher accuracy than the CRT time. At the same time, the SDK provides more functions to process time.

View plaincopy to clipboardprint?
  1. Void getsystemtime (
  2. Lpsystemtime );
  3. Void getlocaltime (
  4. Lpsystemtime );

Void getsystemtime (<br/> lpsystemtime); <br/> void getlocaltime (<br/> lpsystemtime); <br/>

These two functions get the current time in the form of systemtime. However, the getsystemtime function gets the current UTC time and the getlocaltime gets the current local time. As you can imagine, there is a time difference between the two functions. Similar to the conversion between TM and time_t in CRT, the SDK also provides two functions to convert the systemtime and filetime.

View plaincopy to clipboardprint?
  1. Bool systemtimetofiletime (
  2. Const systemtime * lpsystemtime,
  3. Lpfiletime );
  4. Bool filetimetosystemtime (
  5. Const filetime * lpfiletime,
  6. Lpsystemtime );

Bool systemtimetofiletime (<br/> const systemtime * lpsystemtime, <br/> lpfiletime); <br/> bool filetimetosystemtime (<br/> const filetime * lpfiletime, <br/> lpsystemtime); <br/>

The function name is self-explanatory.

The SDK also provides two interesting functions.

View plaincopy to clipboardprint?
  1. Bool localfiletimetofiletime (
  2. Const filetime * lplocalfiletime,
  3. Lpfiletime );
  4. Bool filetimetolocalfiletime (
  5. Const filetime * lpfiletime,
  6. Lpfiletime lplocalfiletime );

Bool localfiletimetofiletime (<br/> const filetime * lplocalfiletime, <br/> lpfiletime); <br/> bool filetimetolocalfiletime (<br/> const filetime * lpfiletime, <br/> lpfiletime lplocalfiletime); <br/>

The localfiletimetofiletime function converts the local filetime to the corresponding UTC filetime. In my opinion, this function only converts the local time minus the UTC time difference. For example, the local time in the UTC zone is converted to the corresponding UTC time, you only need to subtract 8*60*60*1000*1000*10 (unit: 100ns) from the local time ). Similarly, the filetimetolocalfiletime function converts the UTC time to the local time, which only replaces the time difference minus the time difference with the plus time difference.

After learning about these functions, let's talk with the code.

View plaincopy to clipboardprint?
  1. # Include <stdlib. h>
  2. # Include <stdio. h>
  3. # Include <time. h>
  4. # Include <windows. h>
  5. Int main ()
  6. {
  7. Systemtime stlocal, stutc, stutc2;
  8. Filetime ftlocal, ftutc, FT;
  9. Ularge_integer Uli;
  10. Getlocaltime (& stlocal );
  11. Getsystemtime (& stutc );
  12. Printf ("local system time (YYYY-MM-DD hh: mm: SS): % d-% d: % d/N", stlocal. wyear, stlocal. wmonth,
  13. Stlocal. wday, stlocal. whour, stlocal. wminute, stlocal. wsecond );
  14. Printf ("UTC system time (YYYY-MM-DD hh: mm: SS): % d-% d: % d/N", stutc. wyear, stutc. wmonth,
  15. Stutc. wday, stutc. whour, stutc. wminute, stutc. wsecond );
  16. Systemtimetofiletime (& stlocal, & ftlocal );
  17. Uli. lowpart = ftlocal. dwlowdatetime;
  18. Uli. highpart = ftlocal. dwhighdatetime;
  19. Printf ("local file time: % LlU/N", Uli. quadpart );
  20. Localfiletimetofiletime (& ftlocal, & ftutc );
  21. Uli. lowpart = ftutc. dwlowdatetime;
  22. Uli. highpart = ftutc. dwhighdatetime;
  23. Printf ("UTC file time: % LlU/N", Uli. quadpart );
  24. Filetimetosystemtime (& ftutc, & stutc2 );
  25. Printf ("UTC system time2 (YYYY-MM-DD hh: mm: SS): % d-% d: % d/N", stutc2.wyear, stutc2.wmonth,
  26. Stutc2.wday, stutc2.whour, stutc2.wminute, stutc2.wsecond );
  27. Return exit_success;
  28. }

# Include <stdlib. h> <br/> # include <stdio. h> <br/> # include <time. h> <br/> # include <windows. h> </P> <p> int main () <br/>{< br/> systemtimestlocal, stutc, stutc2; <br/> filetimeftlocal, ftutc, FT; <br/> ularge_integeruli; </P> <p> getlocaltime (& stlocal); <br/> getsystemtime (& stutc ); <br/> printf ("local system time (YYYY-MM-DD hh: mm: SS): % d-% d: % d/N ", stlocal. wyear, stlocal. wmonth, <br/> stlocal. wday, stlocal. whour, stlocal. wminute, stlocal. wsecond); <br/> printf ("UTC system time (YYYY-MM-DD hh: mm: SS): % d-% d: % d/N ", stutc. wyear, stutc. wmonth, <br/> stutc. wday, stutc. whour, stutc. wminute, stutc. wsecond); </P> <p> systemtimetofiletime (& stlocal, & ftlocal); <br/> Uli. lowpart = ftlocal. dwlowdatetime; <br/> Uli. highpart = ftlocal. dwhighdatetime; <br/> printf ("local file time: % LlU/N", Uli. quadpart); </P> <p> localfiletimetofiletime (& ftlocal, & ftutc); <br/> Uli. lowpart = ftutc. dwlowdatetime; <br/> Uli. highpart = ftutc. dwhighdatetime; <br/> printf ("UTC file time: % LlU/N", Uli. quadpart); </P> <p> filetimetosystemtime (& ftutc, & stutc2); <br/> printf ("UTC system time2 (YYYY-MM-DD hh: mm: SS ): % d-% d: % d/N ", stutc2.wyear, stutc2.wmonth, <br/> stutc2.wday, stutc2.whour, stutc2.wminute, stutc2.wsecond ); </P> <p> return exit_success; <br/>}

The program output result is as follows:

The getlocaltime function of line 13 of the Code obtains the current local systemtime, And the systemtime of the corresponding UTC value is obtained in line 14, as shown in the first two lines of the output result, the difference between the two is 8 hours (I am still writing a blog in the early morning, praising myself ...).

The 20-row systemtimetofiletime function converts the local systemtime to the local filetime format time that is convenient for calculation, as shown in the third row of the output result.

Line 25: The localfiletimetofiletime function converts the local filetime to the corresponding UTC filetime, as shown in the fourth row of the output result. As described above, if you subtract the number displayed in the third and fourth rows of the output result and divide it by 10*1000*1000*60*60, you will get 8, you can try it. Remember that filetime is in 100 nanoseconds.

The last 30 lines of filetimetosystemtime convert filetime to systemtime. It can be seen that the fifth line in the output result is the same as the second line, which is required because both are the UTC time corresponding to the current local time.

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.