C time (time.h) 库
C 时间函数
<time.h> 头文件提供了用于在 C 语言中处理日期、时间和测量持续时间的函数。
下表列出了常用的 time.h 函数:
| 函数 | 描述 |
|---|---|
| time() | 返回当前日历时间,以 time_t 值表示(自 1970 年 1 月 1 日以来的秒数) |
| localtime() | 将 time_t 值转换为本地时间,并返回指向 struct tm 的指针 |
gmtime() | 将 time_t 值转换为 UTC 时间(也返回 struct tm) |
| ctime() | 将 time_t 值转换为可读字符串(例如 Thu Jun 26 10:30:00 2025) |
asctime() | 将 struct tm 转换为标准日期/时间格式的字符串 |
| strftime() | 将 struct tm 格式化为自定义日期和时间字符串 |
difftime() | 计算两个 time_t 值之间的差值(以秒为单位) |
| mktime() | 将已填充的struct tm转换为time_t值 |
| clock() | 返回程序使用的处理器时钟周期数(可用于测量执行时间) |
什么是 struct tm?
一些函数会返回或使用一个名为 struct tm 的特殊结构体,它存储日期和时间的各个组成部分,例如年、月、小时和分钟。
您可以使用此结构体分别格式化和访问每个时间组成部分。

