Time in Windows (systemtime and filetime) & time in CRT (time_t and TM)

Source: Internet
Author: User
Tags filetime

Http://blog.csdn.net/bokee/article/details/5330791

Http://blog.csdn.net/bokee/article/details/5330682

 

Problems frequently encountered in actual projects during time processing. Here we will introduce the most common time processing functions.

First, we will introduce the basic concept of time. Generally, there are two types of time: local time, Coordinated Universal Time, and UTC. The difference between the local time and UTC time is the time difference. For example, Beijing Time (UTC + 8) is 8 hours later than UTC time.

The processing time functions in the C Runtime Library mainly include the following four:

[CPP]
View plaincopyprint?
  1. Time_t time (
  2. Time_t * timer );

The time_t type is 32-bit or 64-bit integer. The specific type is determined by the compilation system. This function is used to obtain the time elapsed from midnight, January 1, January 1, 1970 (which may vary in different CRT implementations) to the current time, in seconds. The time difference is called the calendar time ).

This is of course confusing me: this special time -- January 1, 1970 00:00:00 -- refers to the local time, or UTC time? I think it is the local time, that is, the January 1, 1970 00:00:00 of each time zone. We can imagine that if every computer in the 24-hour time zone in the world calls the time function at 00:01:00, January 1, January 1, 1970, the return value is 60. Note: This is called in sequence (in fact every one hour), rather than calling at the same time as imagined, because the same local time in the adjacent time zone is always 1 hour different.

Of course, the time of the time_t type is easy for computers to process, but ordinary users cannot understand such numbers. Therefore, we usually need to convert the time_t type time to the form we normally see. The TM structure is defined in CRT.

[CPP]
View plaincopyprint?
  1. Struct TM {
  2. Int tm_sec;/* seconds after the minute-[0, 59] */
  3. Int tm_min;
    /* Minutes after the hour-[0, 59] */
  4. Int tm_hour;/* hours since midnight-[0, 23] */
  5. Int tm_mday;
    /* Day of the month-[1, 31] */
  6. Int tm_mon;/* months since January-[0, 11] */
  7. Int tm_year;
    /* Years since 1900 */
  8. Int tm_wday;/* Days since Sunday-[0, 6] */
  9. Int tm_yday;
    /* Days since January 1-[0,365] */
  10. Int tm_isdst;/* daylight savings time flag */
  11. };

The annotations describe the usage of each field in detail. Obviously, the fields in this structure are more meaningful to users. We usually use the localtime_s function to convert time_t time to TM time.

[CPP]
View plaincopyprint?
  1. Errno_t localtime_s (
  2. Struct TM * _ TM,
  3. Const time_t * time );

The second parameter is the input time_t time, and the first parameter is the returned TM time. From the function name, we can see that the returned TM time represents the local time. Of course, we sometimes need to get the corresponding UTC time, and then we need the gmtime function.

[CPP]
View plaincopyprint?
  1. Errno_t gmtime_s (
  2. Struct TM * _ TM,
  3. Const time_t * time );

We will see the difference later.

We know how to convert time_t time to TM time. Similarly, we need to convert the time represented by TM to time_t. Then we need the mktime function.

[CPP]
View plaincopyprint?
  1. Time_t mktime (
  2. Struct TM * timeptr );

This function returns the calendar time from the "special time" to the time indicated by the parameter. In addition, it can also be used to modify the value range of each field in the TM structure. For example. tm_mon is set to 1, TM. tm_day is set to 33, and then the mktime function is called as the parameter. This function will set TM. tm_mon is corrected to 2, TM. tm_day is corrected to 2. For detailed usage, refer to msdn.

Let's analyze the following sample code:

[CPP]
View plaincopyprint?
  1. # Include <stdlib. h>
  2. # Include <stdio. h>
  3. # Include <time. h>
  4. Int main ()
  5. {
  6. Struct TM tmlocal, tmutc;
  7. Time_t tnow;
  8. // Get current calendar time
  9. Time (& tnow );
  10. Printf ("time now from time (): % LlU/N", tnow );
  11. // Get current local time
  12. Localtime_s (& tmlocal, & tnow );
  13. Printf ("local time (YYYY-MM-DD hh: mm: SS): % d-% d: % d/N", tmlocal. tm_year + 1900, tmlocal. tm_mon,
  14. Tmlocal. tm_mday, tmlocal. tm_hour, tmlocal. tm_min, tmlocal. tm_sec );
  15. // Get UTC time corresponding to current local time, And tmlocal. tm_hour-tmutc. tm_hour = 8
  16. Gmtime_s (& tmutc, & tnow );
  17. Printf ("UTC time (YYYY-MM-DD hh: mm: SS): % d-% d: % d/N", tmutc. tm_year + 1900, tmutc. tm_mon,
  18. Tmutc. tm_mday, tmutc. tm_hour, tmutc. tm_min, tmutc. tm_sec );
  19. // Convert tmlocal to calendar time
  20. Tnow = mktime (& tmlocal );
  21. Printf ("time now from mktime (): % LlU/N", tnow );
  22. Return exit_success;
  23. }

The output result is as follows:

In the above Code, the 11-line time function obtains the calendar time from the "special time" to the current time, such as the 1267192581 s displayed in the first line in the output result.

The 14-line localtime_s function converts the calendar time to the local TM time, for example, the second line of the output result.

The 18-row gmtime_s function converts the calendar time to the corresponding TM time in UTC, as shown in the third row of the output result. It is easy to see that the output time of the second and third rows is 8 hours different because I am in the GMT + 8. If you modify the time zone of your computer (in date and time on the control panel), run the program again, and compare the two running results, you can better understand.

The mktime function of Row 22 converts the TM time to the calendar time. The result displayed in the fourth row of the output result is the same as that in the first row. This is required...

// Certificate //-----------------------------------------------------------------------------------------------------------------------------------------

This section describes 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.

[CPP]
View plaincopyprint?
  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;

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.

[C-sharp]
View plaincopyprint?
  1. Void getsystemtime (
  2. Lpsystemtime );
  3. Void getlocaltime (
  4. Lpsystemtime );

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.

[CPP]
View plaincopyprint?
  1. Bool systemtimetofiletime (
  2. Const systemtime * lpsystemtime,
  3. Lpfiletime );
  4. Bool filetimetosystemtime (
  5. Const filetime * lpfiletime,
  6. Lpsystemtime );

The function name is self-explanatory.

The SDK also provides two interesting functions.

[CPP]
View plaincopyprint?
  1. Bool localfiletimetofiletime (
  2. Const filetime * lplocalfiletime,
  3. Lpfiletime );
  4. Bool filetimetolocalfiletime (
  5. Const filetime * lpfiletime,
  6. Lpfiletime lplocalfiletime );

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.

[CPP]
View plaincopyprint?
  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. }

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.