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

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

Thank you.

  • Timezone can be overridden with TZ environment variable (that is exactly what you do in your code). Maybe TZ is set to UTC-2 before the program is started? – StenSoft Feb 06 '15 at 21:49
  • You mean I need to run `dpkg-reconfigure tzdata` and run this sample code together to adjust timezone for the whole system? – Javier Hernández Feb 06 '15 at 21:59
  • No. tzdata sets only the default timezone. Every user and every program is free to override it for their purposes using environment variable TZ. – StenSoft Feb 06 '15 at 22:02
  • How to check env variables from shell? `set | grep -i TZ` does not showing anything even after running this sample code and running it again already without setenv() call when it shows already correct time. – Javier Hernández Feb 06 '15 at 22:12

1 Answers1

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.

StenSoft
  • 9,369
  • 25
  • 30
  • 1
    How to see/change from shell value of TZ variable set by setenv() call? – Javier Hernández Feb 06 '15 at 22:44
  • Each program (including shell) has its own environment. When you change it in the program with `setenv()`, you won't see it anywhere outside the program but you can check it with `getenv()` in the program. – StenSoft Feb 08 '15 at 09:16