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:
Time_t time (<br/> time_t * timer); </P> <p>
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.
Struct TM {<br/> int tm_sec;/* seconds after the minute-[0, 59] */<br/> int tm_min; /* minutes after the hour-[0, 59] */<br/> int tm_hour;/* hours since midnight-[0, 23] */<br/> int tm_mday; /* day of the month-[] */<br/> int tm_mon;/* months since January-[] */<br/> int tm_year; /* years since 1900 */<br/> int tm_wday;/* Days since Sunday-[] */<br/> int tm_yday; /* Days since January 1-[0,365] */<br/> int tm_isdst;/* daylight savings time flag */<br/> };
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.
Errno_t localtime_s (<br/> struct TM * _ TM, <br/> 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.
Errno_t gmtime_s (<br/> struct TM * _ TM, <br/> 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.
Time_t mktime (<br/> struct TM * timeptr); <br/>
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:
# Include <stdlib. h> <br/> # include <stdio. h> <br/> # include <time. h> </P> <p> int main () <br/>{< br/> struct tmtmlocal, tmutc; <br/> time_ttnow; <br/> // get current calendar time <br/> time (& tnow); <br/> printf ("time now from time (): % LlU/N ", tnow); <br/> // get current local time <br/> localtime_s (& tmlocal, & tnow); <br/> printf ("local time (YYYY-MM-DD hh: MM: SS): % d-% d: % d/N ", tmlocal. tm_year + 1900, tmlocal. tm_mon, <br/> tmlocal. tm_mday, tmlocal. tm_hour, tmlocal. tm_min, tmlocal. tm_sec); <br/> // get UTC time corresponding to current local time, And tmlocal. tm_hour-tmutc. tm_hour = 8 <br/> gmtime_s (& tmutc, & tnow); <br/> printf ("UTC time (YYYY-MM-DD hh: mm: SS ): % d-% d: % d/N ", tmutc. tm_year + 1900, tmutc. tm_mon, <br/> tmutc. tm_mday, tmutc. tm_hour, tmutc. tm_min, tmutc. tm_sec); <br/> // convert tmlocal to calendar time <br/> tnow = mktime (& tmlocal); <br/> printf ("time now from mktime (): % LlU/N ", tnow); </P> <p> return exit_success; <br/>}
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...