For time, C describes the following concepts:
1. Clock Tick: CPU Clock timing unit
2. Calender Time: Calendar Time
3. Broken-down Time: Decomposition Time
4. Local Time: Local Time
5. Coordinate Universal Time: coordinates the Universal Time.
The first three types can be understood as metadata types, which correspond to clock_t, time_t, and tm respectively.
The latter two types are only the first three types of regional representation.
First, Clock Tick (CPU Clock timing unit)
It is a time measurement unit, which can be converted to seconds.
Clock_t clock_tick = clock (); // indicates that the statement has been executed since the program runs.
Number of CPU clock timing units
Long seconds = clock_tick/CLOCKS_PER_SECOND; // converts it to seconds.
Second, Calender Time (calendar Time)
It indicates the number of seconds between a time point and the original time point, that is, the time interval in seconds.
At this original time point, different systems are represented differently, generally at 00:00:00 on January 1, January 1, 1970, while
In. Net2005, It is December 30, 1899 00:00:00.
// The number of seconds between the current time point and the original time point
Time_t calender_time = time (NULL );
// Auxiliary functions
// Double difftime (time_t time2, time_t time1)
// Calculate the difference in seconds between the two
Third, Broken-down Time (Decomposition Time)
It indicates the struct, including year, month, day, minute, and second (others omitted ).
// Obtain the current calendar time first
Time_t calender_time = time (NULL );
// Convert the calendar time to the Decomposition Time of the current time zone
Tm * local_time = localtime (& calender_time );
// The Decomposition Time of the current time zone, that is, the local time.
// Varies depending on the time zone,
// For example, Beijing is located in dongba district.
// Greenwich Mean Time is the unified world time, that is, the coordinated world time mentioned above.
// Convert the calendar time to the Greenwich Mean Decomposition Time.
Tm * utc_time = gmtime (& calender_time );
The three time meta types give us good flexibility, but there are also noteworthy points:
1. You can directly obtain the current calendar time.
Time_t calender_time = time (NULL );
2. Only the current decomposition time can be obtained indirectly and converted using the localtime () function
Time_t calender_time = time (NULL );
Tm * local_date = localtime (& calender_time );
3. The calendar time can be converted into a string representation of the date.
Time_t calender_time;
// The Value assignment is omitted.
Char * szDate = ctime (calender_time );
4. The decomposition time can be converted into a string representation of the date.
Tm * date;
// The Value assignment is omitted.
Char * szDate = asctime (date );
5. The decomposition time can be directly converted to the calendar time.
Tm date;
// The Value assignment is omitted.
Time_t time = mktime (& date );
6. The calendar time can be converted to the decomposition time.
Time_t time;
// The Value assignment is omitted.
Tm * date = localtime (& time );
7. The decomposition time can be formatted and Output
// Use strftime and printf Functions