【问题标题】:Why the results of the command 'date -u' command 'date' faster than the result of 25 seconds in linux(centos 5.1)?为什么命令'date -u'命令'date'的结果比linux(centos 5.1)中25秒的结果快?
【发布时间】:2015-09-13 05:45:37
【问题描述】:

在 CentOS 5.10 版本中,命令 'date -u' 的结果比命令 'date' 的结果快 25 秒。

结果如下:

[a@MG11ZA1 b]$ lsb_release -a 
LSB Version:    :core-4.0-amd64:core-4.0-ia32:core-4.0-noarch:graphics-4.0-amd64:graphics-4.0-  ia32:graphics-4.0-noarch:printing-4.0-amd64:printing-4.0-ia32:printing-4.0-noarch
Distributor ID: CentOS
Description:    CentOS release 5.10 (Final)
Release:        5.10
Codename:       Final
[a@MG11ZA1 b]$ uname -a
Linux MG11ZA1 2.6.18-371.el5 #1 SMP Tue Oct 1 08:35:08 EDT 2013 x86_64 x86_64 x86_64 GNU/Linux
[a@MG11ZA1 b]$ date && date -u && /usr/sbin/hwclock --show
Fri Jun 26 17:47:42 CST 2015
Fri Jun 26 09:48:07 UTC 2015
Fri 26 Jun 2015 05:47:18 PM CST  -0.235359 seconds  

而且下面代码的结果是不对的

#include <time.h>
#include <stdio.h>
int main(int argc, char** argv)
{
    time_t now = time(NULL);
    struct tm today;
    localtime_r(&now, &today);
    printf(
        "seconds:%d\n"
        "minutes:%d\n"
        "hours:%d\n"
        "day of the month:%d\n"
        "month:%d\n"
        "year:%d\n"
        "day of the week:%d\n"
        "day in the year:%d\n"
        "daylight saving time:%d\n"
            ,today.tm_sec
            ,today.tm_min
            ,today.tm_hour
            ,today.tm_mday
            ,today.tm_mon
            ,today.tm_year
            ,today.tm_wday
            ,today.tm_yday
            ,today.tm_isdst);
    time_t weekstart = now - today.tm_wday * 24*60*60;
    printf("weekstart:%u\n", (unsigned int)weekstart);
    struct tm start;
    localtime_r(&weekstart,&start);
    start.tm_hour = 0;
    start.tm_min  = 0;
    start.tm_sec  = 0;
    unsigned int version = mktime(&start);
    printf("version:%u\n", version);
    return 0;
}

以上代码的结果是:

seconds:53
minutes:54
hours:17
day of the month:26
month:5
year:115
day of the week:5
day in the year:176
daylight saving time:0
weekstart:1434880518
version:1434816025

版本应该是 1434816000 而不是 1434816025,(现在是 2015-06-26)。

感谢有人回答

【问题讨论】:

  • 由于leap seconds?引述:“自 1972 年实施这一校正系统以来,已插入 25 个这样的闰秒。”下周再试一次,在 6 月 30 日之后添加另一个闰秒,看看是否相差 26 秒。

标签: linux date time utc localtime


【解决方案1】:

猜测,您的时区是right/PRC,这是一个明确调整为闰秒的时区。您可以在这里看到不同之处:

这是right/PRC,它不会将(当前)25 秒应用于所报告的时间:

env TZ=right/PRC date
Fri Jun 26 19:13:18 CST 2015

这是PRC,它已将(当前)25 秒应用于报告的时间:

env TZ=PRC date
Fri Jun 26 19:13:43 CST 2015

这是UTC,与date -u报告的时区相同:

env TZ=UTC date
Fri Jun 26 11:13:43 UTC 2015

最后还有right/UTC,也就是没有(当前)25秒调整的时间:

env TZ=right/UTC date
Fri Jun 26 11:13:18 UTC 2015

您不应该真正将 right/ 时区用于一般用途 - 它们与大多数时间生产者/消费者报告的时间不匹配。

【讨论】:

    【解决方案2】:

    我不确定我是否正确理解了您的问题,但我认为您看到的 25 秒是“正常”时间和“UTC”时间之间的差异,因为 leap seconds。顺便说一句,下个月应该是 26 秒。

    引用自维基百科:

    自 1972 年实施该校正系统以来,已有 25 次这样的飞跃 已插入秒数。

    【讨论】:

    • 谢谢你的回复。但是为什么代码的版本是1434816025?它应该是1434816000,而在其他linux机器上确实是1434816000。
    猜你喜欢
    • 1970-01-01
    • 2017-08-28
    • 1970-01-01
    • 1970-01-01
    • 2016-12-16
    • 2018-11-26
    • 1970-01-01
    • 2022-01-25
    • 1970-01-01
    相关资源
    最近更新 更多