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