Note that there are some explanatory texts on larger screens.

plurals
  1. POReal-time awareness of timezone change in localtime() vs localtime_r()
    primarykey
    data
    text
    <p>Working on an Ubuntu 12.04.3 LTS box, I just noticed that localtime() and localtime_r() behave differently when the system's timezone changes during the lifetime of a process: localtime() picks up the timezone change immediately, whereas localtime_r() does not, it seems to stick to what was the timezone at the launch of the process. Is this expected behavior? I haven't seen this covered anywhere.</p> <p>More precisely, when I use the following code ...</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;sys/time.h&gt; #include &lt;time.h&gt; #include &lt;unistd.h&gt; int main() { while (1) { time_t t = time(NULL); struct tm *tm = localtime(&amp;t); printf("localtime:%02d/%02d/%02d-%02d:%02d:%02d\n", tm-&gt;tm_mon + 1, tm-&gt;tm_mday, tm-&gt;tm_year + 1900, tm-&gt;tm_hour, tm-&gt;tm_min, tm-&gt;tm_sec); sleep(1); } return 0; } </code></pre> <p>... and change the timezone from UTC via ...</p> <pre><code># echo 'Europe/Berlin' &gt; /etc/timezone # sudo dpkg-reconfigure --frontend noninteractive tzdata </code></pre> <p>... then the code produces the following, ...</p> <pre><code>localtime:10/04/2013-01:11:33 localtime:10/04/2013-01:11:34 localtime:10/04/2013-01:11:35 localtime:10/03/2013-23:11:36 localtime:10/03/2013-23:11:37 localtime:10/03/2013-23:11:38 </code></pre> <p>... but if I use:</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;sys/time.h&gt; #include &lt;time.h&gt; #include &lt;unistd.h&gt; int main() { while (1) { time_t t = time(NULL); struct tm local_tm; struct tm *tm = localtime_r(&amp;t, &amp;local_tm); printf("localtime_r:%02d/%02d/%02d-%02d:%02d:%02d\n", tm-&gt;tm_mon + 1, tm-&gt;tm_mday, tm-&gt;tm_year + 1900, tm-&gt;tm_hour, tm-&gt;tm_min, tm-&gt;tm_sec); sleep(1); } return 0; } </code></pre> <p>... then there's no change when doing a similar timezone change:</p> <pre><code>localtime_r:10/04/2013-01:15:37 localtime_r:10/04/2013-01:15:38 localtime_r:10/04/2013-01:15:39 localtime_r:10/04/2013-01:15:40 localtime_r:10/04/2013-01:15:41 localtime_r:10/04/2013-01:15:42 </code></pre> <p>UPDATE: adding a call to tzset() before invoking localtime_r() produces the expected behavior. Whether that's clear from the spec/manpage or not (see discussion below) is a question for mentalhealth.stackexchange.com...</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload