为什么命令'date -u'的结果命令'date'比linux(centos 5.1)中25秒的结果快?

Why the results of the command 'date -u' command 'date' faster than the result of 25 seconds in linux(centos 5.1)?

在CentOS release 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)。

感谢任何人回答

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

引自维基百科:

Since this system of correction was implemented in 1972, 25 such leap seconds have been inserted.

据推测,您的时区是 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/ 时区用于一般用途 - 它们与大多数 producers/consumers 时间的报告时间不匹配。