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 numberVariables & Description
1size_t
is an unsigned integer type, which is the result of the sizeof keyword .
2clock_t
This is a type suitable for storing processor time.
3time_t is
This is a type suitable for storing calendar time.
4struct 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 NumberMacro & Description
1NULL
This macro is the value of a null pointer constant.
2CLOCKS_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 numberFunction & Description
1char *asctime(const struct tm *timeptr)
Returns a pointer to a string, It represents the date and time of the structure timeptr.
2clock_t clock(void)
Returns the time used by the processor clock since program execution (usually the beginning of the program).
3char *ctime(const time_t *timer)
Returns a string representing the local time. The local time is based on the parameter timer.
4double difftime(time_t time1, time_t time2)
Returns the number of seconds difference between time1 and time2 (time1-time2).
5struct 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).
6struct tm *localtime(const time_t *timer)
The value of timer is decomposed into a tm structure and represented by the local time zone.
7time_t mktime(struct tm *timeptr)
Convert the structure pointed to by timeptr into a time_t value based on the local time zone.
8size_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.
9time_t time(time_t *timer)
Calculate the current calendar time and encode it into time_t format.