First, we should clarify two concepts:
1. The calendar time is represented by the time_t data type. The time expressed by time_t (calendar time) is from a time point (for example, January 1, 1970 00:00:00) the number of seconds. In time. H, we can also see that time_t is a long integer.
2. In Standard C/C ++, we can obtain the date and time through the TM structure. The definition of the TM structure in time. H is as follows:
Struct TM {
Int tm_sec;/* Second-value range: [0, 59] */
Int tm_min;/* minute-value range: [0, 59] */
Int tm_hour;/* hour-value range: [0, 23] */
Int tm_mday;/* date in a month-value range: [] */
Int tm_mon;/* month (from January 1, 0 represents January 1,)-value range: [] */
Int tm_year;/* year, whose value is equal to the actual year minus 1900 */
Int tm_wday;/* day-value range: [0, 6], where 0 indicates Sunday, 1 indicates Monday, and so on */
Int tm_yday;/* Number of days from January 1, 0,365 each year-value range: [1,]. 0 indicates January 1, and so on */
Int tm_isdst;/* identifier of the sequence. When the sequence is executed, the sequence is positive. When the sequence is not implemented, the tm_isdst value is 0. If the sequence is unknown, the tm_isdst () value is negative. */
};
The following functions are explained:
1. asctime (represents the time and date in string format)
Char * asctime (const struct TM * timeptr ):
Function Description
Asctime () converts the information in the TM structure referred to by the timeptr parameter to the time and date representation method used in the real world, and then returns the result in string form. This function has been converted from the time zone to the local time. The string format is "wed Jun 30 21:49:08 1993 \ n"
Return Value
If you call the related time and date functions again, this string may be damaged. The difference between this function and ctime is that the input parameters are different structures.
2. char * ctime (const time_t * timep );
(Time and date are represented in string format ):
Function Description: converts the information in the time_t structure referred to by the timep parameter to the time and date representation method used in the real world, and then returns the result in string form.
3. Int gettimeofday (struct timeval * TV, struct timezone * tz ):
Function Description: gettimeofday () will return the current time with the structure indicated by TV, and put the information of the local time zone in the TZ structure.
Timeval structure:
Struct timeval {
Long TV _sec;/* seconds */
Long TV _usec;/* microseconds */
};
Timezone structure:
Struct timezone {
Int tz_minuteswest;/* the time difference between Greenwich and */
Int tz_dsttime;/* state of daylight saving time */
};
4. struct TM * gmtime (const time_t * timep );
Function Description:
Gmtime () converts the information in the time_t structure referred to by the timep parameter to the time and date representation method used in the real world, and then returns the result from the structure TM.
5. struct TM * localtime (const time_t * timep );
Function Description: sets the timep parameter.
The information in the time_t structure is converted to the time and date representation method used in the real world, and the result is returned by the structure TM. The time date returned by this function has been converted to the local time zone.
6. mktime (the number of seconds after the time structure data is converted)
Time_t mktime (strcut TM * timeptr );
Function Description: mktime () is used to convert the TM structure data referred to by the timeptr parameter to the number of seconds that have elapsed since 00:00:00, January 1, January 1, 1970 AD.
7. settimeofday (set the current time)
Int settimeofday (const struct timeval * TV, const struct timezone * tz );
Function Description: settimeofday () sets the current time to the structure information indicated by TV, and the local time zone information to the structure indicated by tz. For more information, see gettimeofday (). Note: Only the root permission can use this function to modify the time.
8. Time (obtain the current time)
Time_t time (time_t * t );
Function Description: This function returns the number of seconds that have elapsed since 00:00:00 UTC on January 1, January 1, 1970 AD. If t
If it is not a null pointer, this function also saves the returned value to the memory indicated by the T pointer.