Time issues

2019-04-16 19:40发布

问题:

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.

Currently, in linux I get that avec the 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 :

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

On linux that returns :

('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 :

('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 :

    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 UTNIL TODAY..................

The first lines returned in Windows are :

    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 UTNIL TODAY..................

So in windows, we get the date from 1970 whereas in Linux, it starts on 1972 !!

Then if I write that in my bash console :

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 CONTINNUES UNTIL 1979 !..........

As you can see, we don't see any thing for 1970 !!!!

Does anyone know how can I change the timezone of Debian, into my own one !?

Thank you,

Mr_Kaz

Old message : Hello, 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) :

>>> 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 Professionnal) :

>>> 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. Does any one know why ?

Thank you,

Mr_Kaz



Thank you for your answer that is what i did then : in Windows :

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

in 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. 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. But whatever I choose the time is still wrong.

Currently, in linux I get that avec the 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. I am using '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 :

('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 :

('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...)



I create another topic because it seems not to be anymore due to python : Timezone issues

回答1:

Is the time or time zone set differently on either of your PCs?

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.



回答2:

This is almost certainly due to different time zone configurations between the two environments.

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



回答3:

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

This means there were no DST changes for Europe/London in those years - so your Linux results are accurate there. There is no doubt a difference between 'GMT' and 'Europe/London', since GMT applies to all countries in that timezone not jus the UK.

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