【问题标题】:Behaviour of library call localtime() on different Linux platforms不同 Linux 平台上库调用 localtime() 的行为
【发布时间】:2013-11-28 12:42:55
【问题描述】:

如果localtime 在不同平台上的时区设置相同,我想知道两者的工作方式是否不同。如果是的话,除了时区之外的依赖参数是什么?

我在 2 个平台上运行它:

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

int main()
{
    time_t t,t1;//,result;
    double d1;
    struct tm *tm;
    t1 = 1384496356;

    tm = localtime(&t1);

    printf("tm->tm_hour %d tm->tm_min %d\n",tm->tm_hour,tm->tm_min);
    return 0;
}

输出 tm-&gt;tm_mday 15 tm-&gt;tm_hour 11 tm-&gt;tm_min 33

tm-&gt;tm_mday 15 tm-&gt;tm_hour 6 tm-&gt;tm_min 3

【问题讨论】:

  • 依赖硬件时钟?
  • 当我运行 hwclock --debug 它输出 hwclock from util-linux-ng 2.16.1 hwclock: Open of /dev/rtc failed, errno=2: No such file or directory. No usable clock interface found. Cannot access the Hardware Clock via any known method.
  • 相差5小时30分钟,这是IST(印度标准时间)与UTC的偏移量。两个系统上设置的 TZ 值是多少?通常有一个系统范围的设置,有时在/etc/timezone 中,它提供了时区的默认设置。我还注意到您显示的代码不会给出您显示的输出。问题中的这种差异会引起警告;你还有什么不告诉我们的?
  • @JonathanLeffler :当我执行date +%Z 时,它会给出UTC。当我执行env 时,它不会显示TZ。它在哪里?在哪里找到它的价值?
  • @Arya 然后,您的一台机器上的时间似乎配置错​​误 - 也许有人在一台机器上将其设置为 UTC,而在另一台机器上设置为当地时间。你在运行 ntp 吗?

标签: c linux time timezone embedded-linux


【解决方案1】:

您的代码中的 t1 是自 1970 年 1 月 1 日 00:00:00 UTC 以来的时间(以秒为单位)。如果您在不同的机器上得到不同的时间,这意味着这些机器上的实时时钟不同步,假设它们被配置为相同的时区。

【讨论】:

    【解决方案2】:

    硬件时钟和时区定义本地时间。

    【讨论】:

    • 当我运行 hwclock --debug 它输出 hwclock from util-linux-ng 2.16.1 hwclock: Open of /dev/rtc failed, errno=2: No such file or directory. No usable clock interface found. Cannot access the Hardware Clock via any known method.
    • 执行man hwclock 和阅读可能会告诉您使用选项-f--rtc= 允许您指定/dev/rtc 的替代项,例如/dev/rtc0/dev/rtc1 或... .
    猜你喜欢
    • 2011-09-12
    • 1970-01-01
    • 2023-04-06
    • 2020-11-24
    • 2011-07-16
    • 2018-10-14
    • 2023-03-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多