Why changing timezone from shell does not affect gettimeofday() even after reboot?

空扰寡人 提交于 2019-12-12 03:06:58

问题


I have changed on Ubuntu timezone using dpkg-reconfigure tzdata from UTC+2 to UTC+0 but running C code gettimeofday() still showing tz_minuteswest and tv_sec in previous timezone even after reboot. Only after running C code below once gettimeofday() starts to showing UTC+0 time:

#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);
}

Is there some kind of gcc/libc independent configuration of timezone? How to change timezone from shell for the whole system?

Thank you.


回答1:


GNU systems do not support using struct timezone to represent time zone information; that is an obsolete feature of 4.3 BSD. Instead, use the facilities described in Time Zone Functions.



来源:https://stackoverflow.com/questions/28375324/why-changing-timezone-from-shell-does-not-affect-gettimeofday-even-after-reboo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!