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?
- Time_t time (
- 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?
- Struct TM {
- Int tm_sec;/* seconds after the minute-[0, 59] */
- Int tm_min;
/* Minutes after the hour-[0, 59] */
- Int tm_hour;/* hours since midnight-[0, 23] */
- Int tm_mday;
/* Day of the month-[1, 31] */
- Int tm_mon;/* months since January-[0, 11] */
- Int tm_year;
/* Years since 1900 */
- Int tm_wday;/* Days since Sunday-[0, 6] */
- Int tm_yday;
/* Days since January 1-[0,365] */
- Int tm_isdst;/* daylight savings time flag */
- };
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?
- Errno_t localtime_s (
- Struct TM * _ TM,
- 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?
- Errno_t gmtime_s (
- Struct TM * _ TM,
- 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?
- Time_t mktime (
- 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?
- # Include <stdlib. h>
- # Include <stdio. h>
- # Include <time. h>
- Int main ()
- {
- Struct TM tmlocal, tmutc;
- Time_t tnow;
- // Get current calendar time
- Time (& tnow );
- Printf ("time now from time (): % LlU/N", tnow );
- // Get current local time
- Localtime_s (& tmlocal, & tnow );
- Printf ("local time (YYYY-MM-DD hh: mm: SS): % d-% d: % d/N", tmlocal. tm_year + 1900, tmlocal. tm_mon,
- Tmlocal. tm_mday, tmlocal. tm_hour, tmlocal. tm_min, tmlocal. tm_sec );
- // Get UTC time corresponding to current local time, And tmlocal. tm_hour-tmutc. tm_hour = 8
- Gmtime_s (& tmutc, & tnow );
- Printf ("UTC time (YYYY-MM-DD hh: mm: SS): % d-% d: % d/N", tmutc. tm_year + 1900, tmutc. tm_mon,
- Tmutc. tm_mday, tmutc. tm_hour, tmutc. tm_min, tmutc. tm_sec );
- // Convert tmlocal to calendar time
- Tnow = mktime (& tmlocal );
- Printf ("time now from mktime (): % LlU/N", tnow );
- Return exit_success;
- }
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?
- Typedef struct _ filetime {
- DWORD dwlowdatetime;
- DWORD dwhighdatetime;
- } Filetime, * pfiletime;
- Typedef struct _ systemtime {
- Word wyear;
- Word wmonth;
- Word wdayofweek;
- Word wday;
- Word whour;
- Word wminute;
- Word wsecond;
- Word wmilliseconds;
- } 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?
- Void getsystemtime (
- Lpsystemtime );
- Void getlocaltime (
- 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?
- Bool systemtimetofiletime (
- Const systemtime * lpsystemtime,
- Lpfiletime );
- Bool filetimetosystemtime (
- Const filetime * lpfiletime,
- Lpsystemtime );
The function name is self-explanatory.
The SDK also provides two interesting functions.
[CPP]
View plaincopyprint?
- Bool localfiletimetofiletime (
- Const filetime * lplocalfiletime,
- Lpfiletime );
- Bool filetimetolocalfiletime (
- Const filetime * lpfiletime,
- 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?
- # Include <stdlib. h>
- # Include <stdio. h>
- # Include <time. h>
- # Include <windows. h>
- Int main ()
- {
- Systemtime stlocal, stutc, stutc2;
- Filetime ftlocal, ftutc, FT;
- Ularge_integer Uli;
- Getlocaltime (& stlocal );
- Getsystemtime (& stutc );
- Printf ("local system time (YYYY-MM-DD hh: mm: SS): % d-% d: % d/N", stlocal. wyear, stlocal. wmonth,
- Stlocal. wday, stlocal. whour, stlocal. wminute, stlocal. wsecond );
- Printf ("UTC system time (YYYY-MM-DD hh: mm: SS): % d-% d: % d/N", stutc. wyear, stutc. wmonth,
- Stutc. wday, stutc. whour, stutc. wminute, stutc. wsecond );
- Systemtimetofiletime (& stlocal, & ftlocal );
- Uli. lowpart = ftlocal. dwlowdatetime;
- Uli. highpart = ftlocal. dwhighdatetime;
- Printf ("local file time: % LlU/N", Uli. quadpart );
- Localfiletimetofiletime (& ftlocal, & ftutc );
- Uli. lowpart = ftutc. dwlowdatetime;
- Uli. highpart = ftutc. dwhighdatetime;
- Printf ("UTC file time: % LlU/N", Uli. quadpart );
- Filetimetosystemtime (& ftutc, & stutc2 );
- Printf ("UTC system time2 (YYYY-MM-DD hh: mm: SS): % d-% d: % d/N", stutc2.wyear, stutc2.wmonth,
- Stutc2.wday, stutc2.whour, stutc2.wminute, stutc2.wsecond );
- Return exit_success;
- }
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.