【发布时间】: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