/*********************************************
* @brief 将时间戳转换为结构体 struct tm
* @param t 要被转换的时间
* @param[out] buf 返回转换后的时间结构体
* @return 转换后的时间结构体
********************************************/
struct tm* localtime_s(const time_t* restrict t, struct tm* restrict buf);
!subtitle:说明
将日历时间从时间戳 time_t 转换为本地时间的结构体 tm 。
!subtitle:参数
t - 要被转换的日历时间
buf - 返回转换后的时间结构体
!subtitle:返回值
返回 buf
#include <stdio.h>
#include <time.h>
int main(void)
{
time_t t = time(NULL);
struct tm tm;
localtime_s(&t, &tm);
printf("本地时间为 %s", asctime(&tm));
return 0;
}
运行结果:
本地时间为 Thu Oct 16 10:44:44 2025
C23 standard (ISO/IEC 9899:2024):
K.3.8.2.4 The localtime_s function (p: TBD)
C17 standard (ISO/IEC 9899:2018):
K.3.8.2.4 The localtime_s function (p: 454-455)
C11 standard (ISO/IEC 9899:2011):
K.3.8.2.4 The localtime_s function (p: 626-627)