我使用的是标准 mktime
将 struct tm
转换为纪元时间值的函数。 tm
字段在本地填充,我需要获取格林威治标准时间的纪元时间。 tm
有一个 gmtoff
字段,允许您为此目的以秒为单位设置本地 GMT 偏移量。
但我不知道如何获取该信息。某处肯定有一个标准函数可以返回偏移量吗? localtime
是怎么做到的?
最佳答案
只需执行以下操作:
#define _GNU_SOURCE /* for tm_gmtoff and tm_zone */
#include <stdio.h>
#include <time.h>
/* Checking errors returned by system calls was omitted for the sake of readability. */
int main(void)
{
time_t t = time(NULL);
struct tm lt = {0};
localtime_r(&t, <);
printf("Offset to GMT is %lds.\n", lt.tm_gmtoff);
printf("The time zone is '%s'.\n", lt.tm_zone);
return 0;
}
注意:自 time()
返回的纪元以来的秒数是按照格林威治标准测量的。
关于c - 在 C 中获取时区 GMT 偏移量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13804095/