#C 语言标准库函数 localtime_r
/*********************************************
* @brief 将时间戳转换为结构体 struct tm
* @param t 要被转换的时间
* @param[out] buf 返回转换后的时间结构体
* @return 转换后的时间结构体
********************************************/
struct tm* localtime_r(const time_t* t, struct tm* buf);
说明
将日历时间从时间戳 time_t
转换为本地时间的结构体 tm 。
参数
t
- 要被转换的日历时间buf
- 返回转换后的时间结构体
返回值
- 返回
buf
#示例
#include <stdio.h>
#include <time.h>
int main(void)
{
time_t t = time(NULL);
struct tm tm;
localtime_r(&t, &tm);
printf("本地时间为 %s", asctime(&tm));
return 0;
}
运行结果:
本地时间为 Thu Oct 16 10:49:25 2025
#推荐阅读
#参考标准
- C23 standard (ISO/IEC 9899:2024):
- 7.29.3.4 The localtime function (p: TBD)