1508

8 分钟

#C 语言标准库函数 gmtime

/********************************************* * @brief 将时间戳转换为结构体 struct tm * @param t 要被转换的时间 * @return 转换后的时间 ********************************************/ struct tm* gmtime(const time_t* t);

说明

将日历时间从时间戳 time_t 转换为 UTC 时间的结构体 tm

参数

  • t - 要被转换的日历时间

返回值

  • 返回转换后的结果(指向静态数据的指针)

#示例

#include <stdio.h> #include <time.h> int main(void) { time_t t = time(NULL); struct tm* tm_utc = gmtime(&t); printf("UTC 时间为 %s", asctime(tm_utc)); struct tm* tm_local = localtime(&t); printf("本地时间为 %s", asctime(tm_local)); return 0; }

运行结果:

UTC 时间为 Thu Oct 16 02:33:47 2025 本地时间为 Thu Oct 16 10:33:47 2025

通过 export TZ='Asia/Shanghai' 设为北京时间。

#推荐阅读

#参考标准

  • C23 standard (ISO/IEC 9899:2024):
    • 7.29.3.3 The gmtime function (p: TBD)
  • C17 standard (ISO/IEC 9899:2018):
    • 7.27.3.3 The gmtime function (p: 288)
  • C11 standard (ISO/IEC 9899:2011):
    • 7.27.3.3 The gmtime function (p: 393-394)
  • C99 standard (ISO/IEC 9899:1999):
    • 7.23.3.3 The gmtime function (p: 343)
  • C89/C90 standard (ISO/IEC 9899:1990):
    • 4.12.3.3 The gmtime function

创建于 2025/10/16

更新于 2025/10/16