/*********************************************
* @brief 将时间戳转换为结构体 struct tm
* @param t 要被转换的时间
* @return 转换后的时间
********************************************/
struct tm* localtime(const time_t* t);
!subtitle:说明
将日历时间从时间戳 time_t 转换为本地时间的结构体 tm 。
!subtitle:参数
t - 要被转换的日历时间
!subtitle:返回值
返回转换后的结果(指向静态数据的指针)
#include <stdio.h>
#include <time.h>
int main(void)
{
time_t t = time(NULL);
struct tm* tm_local = localtime(&t);
printf("本地时间为 %s", asctime(tm_local));
struct tm* tm_utc = gmtime(&t);
printf("UTC 时间为 %s", asctime(tm_utc));
return 0;
}
运行结果:
本地时间为 Thu Oct 16 02:51:25 2025
UTC 时间为 Thu Oct 16 02:51:25 2025
通过
export TZ='Asia/Shanghai'设为北京时间。
C23 standard (ISO/IEC 9899:2024):
7.29.3.4 The localtime function (p: TBD)
C17 standard (ISO/IEC 9899:2018):
7.27.3.4 The localtime function (p: 288)
C11 standard (ISO/IEC 9899:2011):
7.27.3.4 The localtime function (p: 394)
C99 standard (ISO/IEC 9899:1999):
7.23.3.4 The localtime function (p: 343)
C89/C90 standard (ISO/IEC 9899:1990):
4.12.3.4 The localtime function