C Standard Library - <time.h>
Introduction
time.h The header file defines four variable types, two macros and various functions for operating date and time.
Library variables
The following are the variable types defined in the header file time.h:
Serial number | Variables & Description |
---|---|
1 | size_t is an unsigned integer type, which is the result of the sizeof keyword . |
2 | clock_t This is a type suitable for storing processor time. |
3 | time_t is This is a type suitable for storing calendar time. |
4 | struct tm This is a structure used to save time and date. |
tm The definition of the structure is as follows:
struct tm { int tm_sec; /* 秒,范围从 0 到 59 */ int tm_min; /* 分,范围从 0 到 59 */ int tm_hour; /* 小时,范围从 0 到 23 */ int tm_mday; /* 一月中的第几天,范围从 1 到 31 */ int tm_mon; /* 月,范围从 0 到 11 */ int tm_year; /* 自 1900 年起的年数 */ int tm_wday; /* 一周中的第几天,范围从 0 到 6 */ int tm_yday; /* 一年中的第几天,范围从 0 到 365 */ int tm_isdst; /* 夏令时 */};
Library macros
The following are the macros defined in the header file time.h:
Serial Number | Macro & Description |
---|---|
1 | NULL This macro is the value of a null pointer constant. |
2 | CLOCKS_PER_SEC This macro represents the number of processor clocks per second. |
Library functions
The following are the functions defined in the header file time.h:
Serial number | Function & Description |
---|---|
1 | char *asctime(const struct tm *timeptr) Returns a pointer to a string, It represents the date and time of the structure timeptr. |
2 | clock_t clock(void) Returns the time used by the processor clock since program execution (usually the beginning of the program). |
3 | char *ctime(const time_t *timer) Returns a string representing the local time. The local time is based on the parameter timer. |
4 | double difftime(time_t time1, time_t time2) Returns the number of seconds difference between time1 and time2 (time1-time2). |
5 | struct tm *gmtime(const time_t *timer) The value of timer is decomposed into a tm structure, and the Coordinated Universal Time (UTC) is also Represented as Greenwich Mean Time (GMT). |
6 | struct tm *localtime(const time_t *timer) The value of timer is decomposed into a tm structure and represented by the local time zone. |
7 | time_t mktime(struct tm *timeptr) Convert the structure pointed to by timeptr into a time_t value based on the local time zone. |
8 | size_t strftime(char *str, size_t maxsize, const char *format, const struct tm *timeptr) According to the formatting defined in format Rule, formats the time represented by the structure timeptr and stores it in str. |
9 | time_t time(time_t *timer) Calculate the current calendar time and encode it into time_t format. |