)在本地时间()VS则localtime_r时区变化的实时意识( [英] Real-time awareness of timezone change in localtime() vs localtime_r()

查看:1943
本文介绍了)在本地时间()VS则localtime_r时区变化的实时意识(的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个Ubuntu 12.04.3 LTS箱工作,我只注意到本地时间()和则localtime_r()的行为不同,当一个进程的生命周期内系统的时区的变化:本地时间()立即拿起时区的变化,而则localtime_r ()不,它似乎坚持什么是在启动过程的时区。这是预期的行为?我还没有看到这个覆盖的任何地方。

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.

更多precisely,当我用下面的code ...

More precisely, when I use the following code ...

#include <stdio.h>
#include <sys/time.h>
#include <time.h>
#include <unistd.h>

int main() {
  while (1) {
    time_t t = time(NULL);
    struct tm *tm = localtime(&t);
    printf("localtime:%02d/%02d/%02d-%02d:%02d:%02d\n",
           tm->tm_mon + 1, tm->tm_mday, tm->tm_year + 1900,
           tm->tm_hour, tm->tm_min, tm->tm_sec);
    sleep(1);
  }
  return 0;
}

...并通过...

... and change the timezone from UTC via ...

# echo 'Europe/Berlin' > /etc/timezone 
# sudo dpkg-reconfigure --frontend noninteractive tzdata

...然后code生成以下,...

... then the code produces the following, ...

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

...但如果​​我使用:

... but if I use:

#include <stdio.h>
#include <sys/time.h>
#include <time.h>
#include <unistd.h>

int main() {
  while (1) {
    time_t t = time(NULL);
    struct tm local_tm;
    struct tm *tm = localtime_r(&t, &local_tm);    
    printf("localtime_r:%02d/%02d/%02d-%02d:%02d:%02d\n",
           tm->tm_mon + 1, tm->tm_mday, tm->tm_year + 1900,
           tm->tm_hour, tm->tm_min, tm->tm_sec);
    sleep(1);
  }
  return 0;
}

...然后有做类似的时区变化时没有变化:

... then there's no change when doing a similar timezone change:

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

更新:增加一个调用tzset()调用则localtime_r()之前产生预期的行为。无论是十分明显的,从规范/手册页或没有(参见下文)是mentalhealth.stackexchange.com ...

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...

推荐答案

看到这个下列文件:

在本地时间()函数将日历时间timep到
  分解时间再presentation,前pressed相对于用户的
  指定的时区。的功能作用,就好像它称为tzset(3)和
  设置外部变量TZNAME有关的当前信息
  时区,时区与协调世界之间的差异
  时间(UTC)和当地标准时间(秒)和日光到
  非零值,如果夏时制规则的某些部分过程中应用
  那一年。返回值指向一个静态分配结构
  这可能是由后续调用被重写到任何的日期和
  时间的函数。该则localtime_r()函数的作用是相同的,但店
  在用户提供的结构的数据。它不需要设置TZNAME,时区,
  和日光。

The localtime() function converts the calendar time timep to broken-down time representation, expressed relative to the user's specified timezone. The function acts as if it called tzset(3) and sets the external variables tzname with information about the current timezone, timezone with the difference between Coordinated Universal Time (UTC) and local standard time in seconds, and daylight to a nonzero value if daylight savings time rules apply during some part of the year. The return value points to a statically allocated struct which might be overwritten by subsequent calls to any of the date and time functions. The localtime_r() function does the same, but stores the data in a user-supplied struct. It need not set tzname, timezone, and daylight.

来源: http://linux.die.net/man/3/localtime_r

所以,据我所知,现在看来,code正在工作,我期望。

So as far as I can tell, it appears that the code is working as I'd expect.

编辑,以增加更多的来自同一文档:

Edited to add more from the same documentation:

据POSIX.1-2004,本地时间()来仿佛表现
  tzset(3)中被调用,而则localtime_r()不具有此
  需求。对于便携式code tzset(3)应该被称为前
  则localtime_r()。

According to POSIX.1-2004, localtime() is required to behave as though tzset(3) was called, while localtime_r() does not have this requirement. For portable code tzset(3) should be called before localtime_r().

这篇关于)在本地时间()VS则localtime_r时区变化的实时意识(的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆