简体   繁体   English

Python 中的时间不一致

[英]Time inconsistencies in Python

I am facing some problems in a cross platform program: when I open a python shell in Linux and in Windows, I don't get the same time from the Epoch. I am facing some problems in a cross platform program: when I open a python shell in Linux and in Windows, I don't get the same time from the Epoch.

In Linux, I tried to do dpkg-reconfigure tzdata .在 Linux 中,我尝试执行dpkg-reconfigure tzdata

Currently, in linux I get that avec the dpkg-reconfigure tzdata:目前,在 linux 中,我得到了 avec dpkg-reconfigure tzdata:

Current default time zone: 'Europe/London'
Local time is now:      Mon May 30 10:29:52 BST 2011.
Universal Time is now:  Mon May 30 09:29:52 UTC 2011.

Then, in the python console, I create this script:然后,在 python 控制台中,我创建了这个脚本:

import time
print time.tzname, time.timezone, time.altzone, time.daylight
print time.localtime()
print time.localtime(0)

On linux that returns:在 linux 上返回:

('GMT', 'BST') 0 -3600 1
time.struct_time(tm_year=2011, tm_mon=5, tm_mday=30, tm_hour=11, tm_min=35, tm_sec=8, tm_wday=0, tm_yday=150, tm_isdst=1)
time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=1, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=1, tm_isdst=0)

On Windows that returns:在返回的 Windows 上:

('GMT', 'GMT (heure d\x92\xe9t\xe9)') 0 -3600 1
time.struct_time(tm_year=2011, tm_mon=5, tm_mday=30, tm_hour=11, tm_min=40, tm_sec=1, tm_wday=0, tm_yday=150, tm_isdst=1)
time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=1, tm_isdst=0)

Then I tried this script:然后我尝试了这个脚本:

myTime = 0
dst = 0
while myTime < time.time():
   l = time.localtime(myTime)
   if l[8] is not dst:
       dst = l[8]
       print dst, l
myTime += 24*60*60

The first lines returned in Linux are: Linux 中返回的第一行是:

    1 time.struct_time(tm_year=1972, tm_mon=3, tm_mday=27, tm_hour=1, tm_min=0, tm_sec=0, tm_wday=0, tm_yday=80, tm_isdst=1)
    0 time.struct_time(tm_year=1972, tm_mon=10, tm_mday=30, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=0, tm_yday=304, tm_isdst=0)
    1 time.struct_time(tm_year=1973, tm_mon=3, tm_mday=19, tm_hour=1, tm_min=0, tm_sec=0, tm_wday=0, tm_yday=78, tm_isdst=1)
    0 time.struct_time(tm_year=1973, tm_mon=10, tm_mday=29, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=0, tm_yday=302, tm_isdst=0)
    IT CONTINUES UNTIL TODAY..................

The first lines returned in Windows are: Windows 返回的第一行是:

    1 time.struct_time(tm_year=1970, tm_mon=3, tm_mday=30, tm_hour=1, tm_min=0, tm_sec=0, tm_wday=0, tm_yday=89, tm_isdst=1)
    0 time.struct_time(tm_year=1970, tm_mon=10, tm_mday=26, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=0, tm_yday=299, tm_isdst=0)
    1 time.struct_time(tm_year=1971, tm_mon=3, tm_mday=29, tm_hour=1, tm_min=0, tm_sec=0, tm_wday=0, tm_yday=88, tm_isdst=1)
    0 time.struct_time(tm_year=1971, tm_mon=11, tm_mday=1, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=0, tm_yday=305, tm_isdst=0)
    1 time.struct_time(tm_year=1972, tm_mon=3, tm_mday=27, tm_hour=1, tm_min=0, tm_sec=0, tm_wday=0, tm_yday=87, tm_isdst=1)
    0 time.struct_time(tm_year=1972, tm_mon=10, tm_mday=30, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=0, tm_yday=304, tm_isdst=0)
    1 time.struct_time(tm_year=1973, tm_mon=3, tm_mday=26, tm_hour=1, tm_min=0, tm_sec=0, tm_wday=0, tm_yday=85, tm_isdst=1)
    0 time.struct_time(tm_year=1973, tm_mon=10, tm_mday=29, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=0, tm_yday=302, tm_isdst=0)
    IT CONTINUES UNTIL TODAY..................

So in Windows, we get the date from 1970 whereas in Linux, it starts on 1972!所以在 Windows 中,我们从 1970 年得到日期,而在 Linux 中,它从 1972 年开始!

Then if I write that in my bash console:然后,如果我在我的 bash 控制台中写下它:

zdump -v /usr/share/zoneinfo/Europe/London | grep 197

I get that:我明白了:

/usr/share/zoneinfo/Europe/London  Sun Oct 31 01:59:59 1971 UTC = Sun Oct 31 02:59:59 1971 BST isdst=0 gmtoff=3600
/usr/share/zoneinfo/Europe/London  Sun Oct 31 02:00:00 1971 UTC = Sun Oct 31 02:00:00 1971 GMT isdst=0 gmtoff=0
/usr/share/zoneinfo/Europe/London  Sun Mar 19 01:59:59 1972 UTC = Sun Mar 19 01:59:59 1972 GMT isdst=0 gmtoff=0
/usr/share/zoneinfo/Europe/London  Sun Mar 19 02:00:00 1972 UTC = Sun Mar 19 03:00:00 1972 BST isdst=1 gmtoff=3600
/usr/share/zoneinfo/Europe/London  Sun Oct 29 01:59:59 1972 UTC = Sun Oct 29 02:59:59 1972 BST isdst=1 gmtoff=3600
/usr/share/zoneinfo/Europe/London  Sun Oct 29 02:00:00 1972 UTC = Sun Oct 29 02:00:00 1972 GMT isdst=0 gmtoff=0
/usr/share/zoneinfo/Europe/London  Sun Mar 18 01:59:59 1973 UTC = Sun Mar 18 01:59:59 1973 GMT isdst=0 gmtoff=0
/usr/share/zoneinfo/Europe/London  Sun Mar 18 02:00:00 1973 UTC = Sun Mar 18 03:00:00 1973 BST isdst=1 gmtoff=3600
/usr/share/zoneinfo/Europe/London  Sun Oct 28 01:59:59 1973 UTC = Sun Oct 28 02:59:59 1973 BST isdst=1 gmtoff=3600
IT CONTINUES UNTIL 1979 !..........

As you can see, we don't see any thing for 1970!如您所见,我们看不到 1970 年的任何东西!

How can I change the timezone of Debian, into my own one??如何将 Debian 的时区更改为我自己的时区?

Old message: I am facing some problems in a cross platform program: when I open a python shell in Linux and in Windows, I don't get the same time from the Epoch. Old message: I am facing some problems in a cross platform program: when I open a python shell in Linux and in Windows, I don't get the same time from the Epoch.

In linux (debian squeeze):在 linux(debian 压缩)中:

>>> import time
>>> time.localtime()
time.struct_time(tm_year=2011, tm_mon=5, tm_mday=30, tm_hour=10, tm_min=1, tm_sec=57, tm_wday=0, tm_yday=150, tm_isdst=1)
>>> time.localtime(0)
time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=1, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=1, tm_isdst=0)

In Windows (7 64 bits Professional):在 Windows(7 64 位专业版)中:

>>> import time
>>> time.localtime()
time.struct_time(tm_year=2011, tm_mon=5, tm_mday=30, tm_hour=10, tm_min=1, tm_sec=59, tm_wday=0, tm_yday=150, tm_isdst=1)
>>> time.localtime(0)
time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=1, tm_isdst=0)

As you can see there is an hour of difference.如您所见,有一个小时的差异。 Why?为什么?



Thank you for your answer that is what I did then: in Windows:感谢您的回答,这就是我当时所做的:在 Windows 中:

>>> time.tzname
('GMT', 'GMT (heure d\x92\xe9t\xe9)')
>>> time.timezone
0
>>> time.altzone
-3600
>>> time.daylight
1

in Linux:在 Linux 中:

>>> time.tzname
('UTC', 'UTC')
>>> time.timezone
0
>>> time.altzone
0
>>> time.daylight
0

I don't want to use gmtime if I don't know why there is a problem.如果我不知道为什么会出现问题,我不想使用 gmtime。 I tried to change the time in Linux to get the same as in Windows (I started the development of my software in Windows and it works perfectly, that's why I want to change the system time in Linux and not in windows). I tried to change the time in Linux to get the same as in Windows (I started the development of my software in Windows and it works perfectly, that's why I want to change the system time in Linux and not in windows). In Linux, I tried to do dpkg-reconfigure tzdata .在 Linux 中,我尝试执行dpkg-reconfigure tzdata But whatever I choose the time is still wrong.但无论我选择的时间仍然是错误的。

Currently, in linux I get that avec the dpkg-reconfigure tzdata:目前,在 linux 中,我得到了 avec dpkg-reconfigure tzdata:

Current default time zone: 'Europe/London'
Local time is now:      Mon May 30 10:29:52 BST 2011.
Universal Time is now:  Mon May 30 09:29:52 UTC 2011.

I don't understand which time used in Windows.我不明白 Windows 使用了哪个时间。 I am using 'UTC'.我正在使用“UTC”。



I create this script:我创建了这个脚本:

import time
print time.tzname, time.timezone, time.altzone, time.daylight
print time.localtime()
print time.localtime(0)

On linux that returns:在 linux 上返回:

('GMT', 'BST') 0 -3600 1
time.struct_time(tm_year=2011, tm_mon=5, tm_mday=30, tm_hour=11, tm_min=35, tm_sec=8, tm_wday=0, tm_yday=150, tm_isdst=1)
time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=1, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=1, tm_isdst=0)

On Windows that returns:在返回的 Windows 上:

('GMT', 'GMT (heure d\x92\xe9t\xe9)') 0 -3600 1
time.struct_time(tm_year=2011, tm_mon=5, tm_mday=30, tm_hour=11, tm_min=40, tm_sec=1, tm_wday=0, tm_yday=150, tm_isdst=1)
time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=1, tm_isdst=0)

And as you can see, there is still the problem of time since the Epoch (it was 1 am at the epoch...)正如你所看到的,自纪元以来仍然存在时间问题(纪元是凌晨 1 点......)



I create another topic because it seems not to be anymore due to python: Timezone issues我创建了另一个主题,因为它似乎不再是由于 python: 时区问题

Is the time or time zone set differently on either of your PCs?您的两台 PC 上的时间或时区设置是否不同?

http://docs.python.org/library/time.html#time.gmtime http://docs.python.org/library/time.html#time.gmtime

The link above suggests using time.gmtime() to get time since the epoch in UTC, instead of local time.上面的链接建议使用 time.gmtime() 来获取自 UTC 纪元以来的时间,而不是本地时间。

This is almost certainly due to different time zone configurations between the two environments.这几乎可以肯定是由于两个环境之间的时区配置不同。

Examine time.tzname, time.timezone, time.altzone, time.daylight检查time.tzname, time.timezone, time.altzone, time.daylight

I realise this is an old question, but I was looking at some issues in the same area and thought I would share:我意识到这是一个老问题,但我正在研究同一领域的一些问题,并认为我会分享:

From 1968-1972 The UK kept BST throughout the year从 1968 年到 1972 年,英国全年保持 BST

This means there were no DST changes for Europe/London in those years - so your Linux results are accurate there.这意味着那些年欧洲/伦敦没有 DST 变化 - 所以你的 Linux 结果在那里是准确的。 There is no doubt a difference between 'GMT' and 'Europe/London', since GMT applies to all countries in that timezone not jus the UK.毫无疑问,“GMT”和“Europe/London”之间是有区别的,因为 GMT 适用于该时区的所有国家,而不仅仅是英国。

See, for example: http://wwp.greenwichmeantime.co.uk/daylight-saving-time/history-william-willetts.htm例如,参见: http://wwp.greenwichmeantime.co.uk/daylight-saving-time/history-william-willetts.htm

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM