1234567890

February 11th, 2009 | by vvoody |

这个数字最先是从王聪大哥的饭否那儿得知。后来陆续的就有相关报道,不知道这个代表啥的请看这篇文章。图新鲜,想亲手把这数转成具体时间看看。当时不知道 date 命令能很方便的转换,就用了 Python,于是问题随之而来。

vvoody@slackware:~$ python
Python 2.5.2 (r252:60911, Sep 11 2008, 13:43:31)
[GCC 4.2.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from time import strftime, gmtime, localtime
>>> strftime("%a, %d %b %Y %H:%M:%S", gmtime(1234567890))
'Fri, 13 Feb 2009 23:31:06'
>>> strftime("%a, %d %b %Y %H:%M:%S", localtime(1234567890))
'Sat, 14 Feb 2009 07:31:30' 
>>> strftime("%a, %d %b %Y %H:%M:%S", gmtime(915148821))
'Thu, 31 Dec 1998 23:59:60'

1234567890 对应的时间应该是 23:31:30 ,而我的 Python 里的 gmtime 运行出来是 23:31:06,少了 24 秒。问了很多人,平台相同的都是正确的,就我的不对。后来经讨论得知那 24 秒和闰秒有关,Python 没有加上这 24 秒。但 gmtime(915148821) 这个数确是有闰秒的操作,因此结果就有了第 60 秒。

后来,索性直接看看调用 C 库函数和 date 命令的结果。如下代码:

vvoody@slackware:~/lab$ cat gmtime.c
#include
#include 
 
int main(void)
{
  struct tm *foo;
  time_t bar = 1234567890;
  foo = gmtime(&bar);
  printf("tm_sec: %dn", foo->tm_sec);
 
  char egg[100];
  strftime(egg, 100, "%a, %b %d %Y %H:%M:%S", foo);
  printf("1234567890 is at:n%sn", egg);
  return 0;
}
vvoody@slackware:~/lab$ gcc -Wall gmtime.c
vvoody@slackware:~/lab$ ./a.out
tm_sec: 6
1234567890 is at:
Fri, Feb 13 2009 23:31:06
vvoody@slackware:~/lab$ date -ud @1234567890
Fri Feb 13 23:31:06 UTC 2009

最后经 cppgx@newsmth 大牛的指点,重新运行了下 timeconfig,就好了。据他说可能是时区文件被胡乱编辑了,就引起了上面遇到的问题。timeconfig 会把你指定的系统自带的某个时区文件复制到/etc去。

呼,终于好了。不过还是不清楚到底是哪个程序动了我的配置文件。

相关讨论:
http://www.newsmth.net/bbscon.php?bid=284&id=51926
http://groups.google.com/group/python-cn/browse_thread/thread/2d0e9ce534d2d93c?hl=en

  1. 2 Responses to “1234567890”

  2. By bitstream on Feb 11, 2009

    Mozilla Firefox 3.0.6 Fedora Linux Mozilla Firefox 3.0.6 Fedora Linux

    哎,闰秒这个问题还真没考虑过~~不过我看了你写的更糊涂了,到底哪些闰了?哪些没闰? :^)

    Reply

    vvoody Reply:

    Mozilla Firefox 3.0.6 Linux

    对于 1234567890 这个数,gmtime 出来是 23:31:06,而正确应该是 23:31:30秒,就少了那 24 秒的闰秒。
    你看 23:59:60 这种就是闰了。

    Reply

Post a Comment

To submit your comment, click the image below where it asks you to...
根据下面图中的提示点击后提交留言... Clickcha - The One-click Captcha