Introduction
\ntime.h header file defines four variable types, two macros, and various functions for manipulating dates and times.
\n<time.h> is a header file in the C standard library that provides functions and types for handling and manipulating dates and times. The functions in this header are used to get the current time, set time, format time, calculate time differences, and more.
\n\nConstants & Macros
\n| Constant/Macro | Description |
|---|---|
CLOCKS_PER_SEC | Number of clock ticks per second. |
NULL | Null pointer constant. |
TIME_UTC | Represents UTC time (C11). |
Library Variables
\nThe following are the variable types defined in the time.h header:
\n| No. | Variable & Description |
|---|---|
| 1 | size_t is an unsigned integer type, which is the result of the sizeof operator. |
| 2 | clock_t is a type suitable for storing processor time. |
| 3 | time_t is a type suitable for storing calendar time. |
| 4 | struct tm is a structure used to store time and date. |
The tm structure is defined as follows:
\nstruct tm { int tm_sec; /* seconds, range from 0 to 59*/ int tm_min; /* minutes, range from 0 to 59*/ int tm_hour; /* hours, range from 0 to 23*/ int tm_mday; /* day of the month, range from 1 to 31*/ int tm_mon; /* month, range from 0 to 11*/ int tm_year; /* years since 1900*/ int tm_wday; /* day of the week, range from 0 to 6*/ int tm_yday; /* day of the year, range from 0 to 365*/ int tm_isdst; /* daylight saving time*/};\n\nLibrary Macros
\nThe following are the macros defined in the time.h header:
\n| No. | Macro & Description |
|---|---|
| 1 | NULL This macro holds the value of a null pointer constant. |
| 2 | CLOCKS_PER_SEC This macro represents the number of processor clock ticks per second. |
Library Functions
\nThe following are the functions defined in the time.h header:
\n| No. | Function & Description |
|---|---|
| 1 | char *asctime(const struct tm *timeptr) Returns a pointer to a string representing the date and time stored in the timeptr structure. |
| 2 | clock_t clock(void) Returns the amount of processor time used by the program since its execution (generally from the start). |
| 3 | char *ctime(const time_t *timer) Returns a string representing local time, based on the timer argument. |
| 4 | double difftime(time_t time1, time_t time2) Returns the difference in seconds between time1 and time2 (time1-time2). |
| 5 | struct tm *gmtime(const time_t *timer) Breaks down the timer value into a tm structure and represents it in Coordinated Universal Time (UTC), also known as Greenwich Mean Time (GMT). |
| 6 | struct tm *localtime(const time_t *timer) Breaks down the timer value into a tm structure and represents it in the local timezone. |
| 7 | time_t mktime(struct tm *timeptr) Converts the structure pointed to by timeptr into a time_t value according to the local timezone. |
| 8 | size_t strftime(char *str, size_t maxsize, const char *format, const struct tm *timeptr) Formats the time represented by the timeptr structure according to the rules defined in format, and stores it in str. |
| 9 | time_t time(time_t *timer) Calculates the current calendar time and encodes it as a time_t object. |
| 10 | int timespec_get(struct timespec *ts, int base); Gets the current time (C11). |
Examples
\nThe following are examples using some functions from <time.h>.
\n\nGet Current Time:
\nExample
\n#include \n\n#include \n\nFormat Time:
\nExample
\n#include \n\n#include \n\nCalculate Time Difference:
\nExample
\n#include \n\n#include \n\nUsing struct tm:
\nExample
\n#include \n\n#include \n\nNotes
\n- \n
- When using
localtimeandgmtime, the returned pointer points to a statically allocatedstruct tm. Each call will overwrite the previous contents. \n - When using the
mktimefunction, ensure that all fields within thestruct tmare valid. \n - When using the
strftimefunction for time formatting, ensure the destination buffer is large enough to avoid buffer overflow. \n
By understanding and using the functions provided by <time.h>, you can conveniently manipulate dates and times, thereby writing more feature-rich and efficient C programs.
YouTip