为啥 gmtime() 和 localtime() 相差不为整?

7 views
Skip to first unread message

vvoody

unread,
Feb 9, 2009, 2:22:16 AM2/9/09
to python-cn`CPyUG`华蟒用户组
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(123456789))
'Thu, 29 Nov 1973 21:33:07'
>>> strftime("%a, %d %b %Y %H:%M:%S", localtime(123456789))
'Fri, 30 Nov 1973 05:33:07'

为啥不是差整 8 小时?

====
运行结果截图:
http://arm.in/Mf

Liu Qishuai

unread,
Feb 9, 2009, 2:50:23 AM2/9/09
to pyth...@googlegroups.com
今年是全球第24次增加闰秒。

smallfish

unread,
Feb 9, 2009, 2:52:59 AM2/9/09
to pyth...@googlegroups.com
gmtime()返回的是0时区的值,localtime返回的是当前时区的值
我本地的是

>>> from time import strftime, gmtime, localtime
>>> from time import timezone
>>> timezone
-28800
>>> timezone/3600
-8 # 我本地win时区就是GMT8

>>> strftime("%a, %d %b %Y %H:%M:%S", gmtime(1234567890))
'Fri, 13 Feb 2009 23:31:30'

>>> strftime("%a, %d %b %Y %H:%M:%S", localtime(1234567890))
'Sat, 14 Feb 2009 07:31:30'

可以看出来了把
--
blog - http://hi.baidu.com/smallfish7788

vvoody

unread,
Feb 9, 2009, 2:58:18 AM2/9/09
to python-cn`CPyUG`华蟒用户组
On Feb 9, 3:52 pm, smallfish <perluo...@gmail.com> wrote:
> gmtime()返回的是0时区的值,localtime返回的是当前时区的值
> 我本地的是>>> from time import strftime, gmtime, localtime
> >>> from time import timezone
> >>> timezone
> -28800
> >>> timezone/3600
>
> -8 # 我本地win时区就是GMT8>>> strftime("%a, %d %b %Y %H:%M:%S", gmtime(1234567890))
>
> 'Fri, 13 Feb 2009 23:31:30'>>> strftime("%a, %d %b %Y %H:%M:%S", localtime(1234567890))
>
> 'Sat, 14 Feb 2009 07:31:30'
>
> 可以看出来了把
>

朋友你没看清我的问题啊 ;-)

vvoody

unread,
Feb 9, 2009, 2:59:48 AM2/9/09
to python-cn`CPyUG`华蟒用户组
On Feb 9, 3:50 pm, Liu Qishuai <lqs.b...@gmail.com> wrote:
> 今年是全球第24次增加闰秒。
>

查了下确是增加了 24 秒,加上去就对了。
不过,为啥我的没给自动加上,别人的却加上了呢?

Liu Qishuai

unread,
Feb 9, 2009, 3:16:28 AM2/9/09
to pyth...@googlegroups.com
试下 915148800 这个时间,加上后应该是  'Thu, 31 Dec 1998 23:59:60'

vvoody

unread,
Feb 9, 2009, 3:18:58 AM2/9/09
to python-cn`CPyUG`华蟒用户组
On 2月9日, 下午4时16分, Liu Qishuai <lqs.b...@gmail.com> wrote:
> 试下 915148800 这个时间,加上后应该是 'Thu, 31 Dec 1998 23:59:60'
>

>>> strftime("%a, %d %b %Y %H:%M:%S", gmtime(915148800))
'Thu, 31 Dec 1998 23:59:39'
>>>

天。。。

> 2009/2/9 vvoody <wxj.g...@gmail.com>

Liu Qishuai

unread,
Feb 9, 2009, 3:25:10 AM2/9/09
to pyth...@googlegroups.com
那就试试 915148821 吧,wikipedia 那篇文章好像有问题

vvoody

unread,
Feb 9, 2009, 3:29:36 AM2/9/09
to python-cn`CPyUG`华蟒用户组
On 2月9日, 下午4时25分, Liu Qishuai <lqs.b...@gmail.com> wrote:
> 那就试试 915148821 吧,wikipedia 那篇文章好像有问题
>


>>> strftime("%a, %d %b %Y %H:%M:%S", gmtime(915148821))


'Thu, 31 Dec 1998 23:59:60'
>>>


看来 gmtime 加了闰秒了。但为啥后来的 1234567890 就没给加上呢?糊涂了

vvoody

unread,
Feb 12, 2009, 9:57:33 AM2/12/09
to python-cn`CPyUG`华蟒用户组
我又测试了 C 库函数:
vvoody@slackware:~/lab$ cat gmtime.c
#include <stdio.h>
#include <time.h>

int main(void)
{
struct tm *foo;
time_t bar = 1234567890;
foo = gmtime(&bar);
printf("tm_sec: %d\n", foo->tm_sec);

char egg[100];
strftime(egg, 100, "%a, %b %d %Y %H:%M:%S", foo);
printf("1234567890 is at:\n%s\n", 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 -d @1234567890
Sat Feb 14 07:31:30 CST 2009

问题依然存在。最后我重新运行了下 timeconfig,就好了。
Reply all
Reply to author
Forward
0 new messages