0

我已经使用dpkg-reconfigure tzdata从 UTC+2 到 UTC+0 的 Ubuntu 时区进行了更改,但运行 C 代码 gettimeofday()即使在重新启动后仍然显示tz_minuteswest并且在以前的时区中。tv_sec只有在 gettimeofday() 开始显示 UTC+0 时间后运行下面的 C代码

#include <stdlib.h>
#include <stdio.h>
#include <sys/time.h>

int main()
{
  struct timeval tv;
  struct timezone tz;

  setenv("TZ", "UTC", 1);
  tzset();

  gettimeofday(&tv, &tz);
  tv.tv_sec -= 7200;
  tz.tz_minuteswest = 0;
  settimeofday(&tv, &tz);

  gettimeofday(&tv, &tz);
  printf("time: %llu, offset: %d\n",
    (long long unsigned)tv.tv_sec, tz.tz_minuteswest);
}

是否有某种 gcc/libc 独立的时区配置?如何从 shell 更改整个系统的时区?

谢谢你。

4

1 回答 1

1

GNU 系统不支持使用struct timezone来表示时区信息;这是 4.3 BSD 的一个过时特性。相反,请使用时区功能中描述的工具。

于 2015-02-06T22:32:49.237 回答