Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

C Standard Library – <time.h>


May 11, 2021 C


Table of contents


C Standard Library - slt;time.h.gt;

Brief introduction

The time.h header file defines four variable types, two macros, and functions for various operating dates and times.

Library variables

Here are the types of variables defined in the header file time.h:

Serial number Variable & description
1 size_t
Is the symbolic integer type, it is sizeof The result of the keyword.
2 clock_t
This is a type suitable for storage processor time.
3 time_t
This is a suitable storage calendar time type.
4 struct tm
This is a structure used to save time and date.

The tm structure is defined 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

Here are the macros defined in the header file time.h:

Serial number Macro & Description
1 NULL
This macro is a value of an empty pointer.
2 CLOCKS_PER_SEC
This macro represents the number of processor clocks per second.

The library function

Here 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 string, which represents the date and time of structure TimePtr.
2 clock_t clock(void)
The return program is executed (generally the beginning of the program), the time used by the processor clock.
3 char *ctime(const time_t *timer)
Returns a string representing local time, local time is based on parameter Timer.
4 double difftime(time_t time1, time_t time2)
Returns the number of seconds between TIME1 and TIME2 (TIME1-TIME2).
5 struct tm *gmtime(const time_t *timer)
The value of Timer is broken down into a TM structure, and is also known as the GMT (GMT) representation with coordination of the world (UTC).
6 struct tm *localtime(const time_t *timer)
The value of Timer is broken down into a TM structure and is represented by local time zone.
7 time_t mktime(struct tm *timeptr)
Converting the structure points to the TimePtr to a TIME_T value in accordance with the local time zone.
8 size_t strftime(char *str, size_t maxsize, const char *format, const struct tm *timeptr)
Format the time indicated by TimePtr represented by formatting rules defined in Format, and store it in the STR.
9 time_t time(time_t *timer)
Calculate the current calendar time and encode it into a Time_T format.