c - 在 C 中获取时区 GMT 偏移量

标签 c datetime timezone libc

我使用的是标准 mktimestruct 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, &lt);

  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/

相关文章:

python - 在python中减去两次

java - 如何在Java中显示夏令时开始后的时差?

sql - 带时区的 Oracle DateTime 查询

c# - 想了解多重递归的概念

c - C 中的 OpenSSL 编程

android - 如何在 Flutter 中格式化日期时间

python-3.x - 与日期列的逻辑比较在Pandas数据框中不起作用

java - 时区 EST 与 EST5EDT 有何不同?

c++ - c标准库的LuaBind绑定(bind)?

c - typedef'ing 指针类型被认为是不好的做法吗?