C Standard Library -
Introduction
time.h The header file defines four variable types, two macros, and various functions for operating on dates and times.
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 the time, format the time, and calculate time differences, among other things.
Constants and macros
| Constant/Macro | Description |
|---|---|
CLOCKS_PER_SEC |
The number of clock ticks per second. |
NULL |
Null pointer constant. |
TIME_UTC |
Represents UTC time (C11). |
Library variables
The following are the variable types defined in the time.h header:
| No. | Variable & Description |
|---|---|
| 1 | **size_t ** |
| is an unsigned integer type; it is sizeof The result of the 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 hold 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
The following are the macros defined in the time.h header:
| No. | 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 clock ticks per second. |
Library functions
The following are the functions defined in the time.h header:
| No. | Function & Description |
|---|---|
| 1 | char *asctime(const struct tm *timeptr) |
| Returns a pointer to a string representing the date and time of the timeptr structure. | |
| 2 | clock_t clock(void) |
| Returns the processor clock time used since the start of the program (usually the beginning of the program). | |
| 3 | char *ctime(const time_t *timer) |
| Returns a string representing local time, based on the argument 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 expressed in Coordinated Universal Time (UTC), also known as Greenwich Mean Time (GMT). | |
| 6 | struct tm *localtime(const time_t *timer) |
| The value of timer is broken down into a tm structure and expressed in the local time zone. | |
| 7 | time_t mktime(struct tm *timeptr) |
| Converts 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) |
| Formats the time represented by the timeptr structure according to the formatting rules defined in format, and stores it in str. | |
| 9 | time_t time(time_t *timer) |
| Calculates the current calendar time and encodes it in time_t format. | |
| 10 | int timespec_get(struct timespec *ts, int base); |
| Gets the current time (C11). |
Example
The following is how to use Examples of some functions in stdlib.h.
Get the current time:
Example
#include
#include
int
main
(
)
{
time_t current_time
;
time
(
&
current_time
)
;
printf
(
"Current time: %s"
,
ctime
(
&
current_time
)
)
;
return
0
;
}
Format the time:
Example
#include
#include
int
main
(
)
{
time_t current_time
;
struct
tm
time_info
;
char
buffer
[
80
]
;
time
(
&
current_time
)
;
time_info
=
localtime
(
&
current_time
)
;
strftime
(
buffer
,
sizeof
(
buffer
)
,
"%Y-%m-%d %H:%M:%S"
,
time_info
)
;
printf
(
"Formatted time: %s\n"
,
buffer
)
;
return
0
;
}
Calculate the time difference:
Example
#include
#include
int
main
(
)
{
time_t start_time
,
end_time
;
double
time_diff
;
time
(
&
start_time
)
;
// Simulate a time-consuming operation
for
(
int
i
=
0
;
i
<
100000000
;
i
++
)
;
time
(
&
end_time
)
;
time_diff
=
difftime
(
end_time
,
start_time
)
;
printf
(
"Time difference: %.2f seconds\n"
,
time_diff
)
;
return
0
;
}
Using struct tm:
Example
#include
#include
int
main
(
)
{
struct
tm time_info
;
char
buffer
[
80
]
;
time_info.
tm_year
=
2024
1900
;
// The year is counted from 1900
time_info.
tm_mon
=
5
;
// June, ranging from 0 to 11
time_info.
tm_mday
=
12
;
time_info.
tm_hour
=
12
;
time_info.
tm_min
=
0
;
time_info.
tm_sec
=
0
;
time_info.
tm_isdst
=
1
;
// Automatically determine whether daylight saving time is in effect
mktime
(
&
time_info
)
;
strftime
(
buffer
,
sizeof
(
buffer
)
,
"%Y-%m-%d %H:%M:%S"
,
&
time_info
)
;
printf
(
"Custom time: %s\n"
,
buffer
)
;
return
0
;
}
Notes
- Use
localtimeandgmtimeWhen , the returned pointer points to thestruct tmstructure, which is stored statically and overwritten on each call. - Use
mktimefunction, you must ensure that thestruct tmfields in are valid. - Use
strftimeWhen using the function for time formatting, make sure the destination buffer is large enough to avoid a buffer overflow.
By understanding and using provides functions that make it easy to manipulate time and dates, allowing you to write more feature-rich and efficient C programs.