为什么从shell更改时区即使重启后也不会影响gettimeofday()? [英] Why changing timezone from shell does not affect gettimeofday() even after reboot?
问题描述
我已在Ubuntu时区上使用dpkg-reconfigure tzdata
从UTC + 2更改为UTC + 0,但即使重新启动后,运行C代码gettimeofday()仍在先前的时区中显示tz_minuteswest
和tv_sec
.仅在一次 gettimeofday()以下运行C代码之后,才开始显示UTC + 0时间:
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);
}
是否存在某种时区的gcc/libc独立配置?如何在整个系统中从shell更改时区?
Is there some kind of gcc/libc independent configuration of timezone? How to change timezone from shell for the whole system?
谢谢.
推荐答案
GNU系统不支持使用struct timezone
表示时区信息.这是4.3 BSD的过时功能.而是使用时区功能.
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.
这篇关于为什么从shell更改时区即使重启后也不会影响gettimeofday()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!