简而言之,使用time.localtime()
代替time.gmtime()
.
问题在于您使用gmtime()
,正如以下程序的结果所示。
from time import *
def getTimeZoneFromEpoch(epoch):
if daylight and gmtime(epoch).tm_isdst==1:
return -altzone/3600.0
else:
return -timezone/3600.0
print " tm_isdst of tm_isdst of time zone's\n" + \
' epoch gmtime(epoch) localtime(epoch) offset'
for d in ('13/03/2011', # DST start date in USA
'14/03/2011',
'',
'06/11/2011', # DST end date in USA
'07/11/2011',
'',
'27/03/2011', # DST start date in Europe
'28/03/2011',
'',
'30/10/2011', # DST end date in Europe
'31/10/2011'):
if d:
ds = strptime(d,'%d/%m/%Y')
epoch = mktime(ds)
lt = localtime(epoch)
gt = gmtime(epoch)
print '%s %s %12s %11s %7s %17s' % (d,ds.tm_isdst,epoch,gt.tm_isdst,lt.tm_isdst,getTimeZoneFromEpoch(epoch))
else:
print
将我的时钟设置为“UTC-07:00 落基山脉”时区,夏令时从 2011 年 3 月 13 日开始,到 2011 年 11 月 6 日结束,结果是:
tm_isdst of tm_isdst of time zone's
epoch gmtime(epoch) localtime(epoch) offset
13/03/2011 -1 1299999600.0 0 0 -7.0
14/03/2011 -1 1300082400.0 0 1 -7.0
06/11/2011 -1 1320559200.0 0 1 -7.0
07/11/2011 -1 1320649200.0 0 0 -7.0
27/03/2011 -1 1301205600.0 0 1 -7.0
28/03/2011 -1 1301292000.0 0 1 -7.0
30/10/2011 -1 1319954400.0 0 1 -7.0
31/10/2011 -1 1320040800.0 0 1 -7.0
将我的时钟设置为“UTC+01:00 West Continental Europe”时区,夏令时从 2011 年 3 月 27 日开始,到 2011 年 10 月 30 日结束,结果是:
tm_isdst of tm_isdst of time zone's
epoch gmtime(epoch) localtime(epoch) offset
13/03/2011 -1 1299970800.0 0 0 1.0
14/03/2011 -1 1300057200.0 0 0 1.0
06/11/2011 -1 1320534000.0 0 0 1.0
07/11/2011 -1 1320620400.0 0 0 1.0
27/03/2011 -1 1301180400.0 0 0 1.0
28/03/2011 -1 1301263200.0 0 1 1.0
30/10/2011 -1 1319925600.0 0 1 1.0
31/10/2011 -1 1320015600.0 0 0 1.0