问题
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