Python:基于DST获取时间戳的时区

2024-10-01 15:41:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我写了这样一个代码来获取特定纪元时间的基于DST的时区:

def getTimeZoneFromEpoch(epoch)
    if time.daylight and time.gmtime(epoch).tm_isdst==1:
        return -time.altzone/3600.0
    else:
        return -time.timezone/3600.0

但我不确定它是正确的,事实上,目前我错了1个小时。也许我该换个时区和时区 在这段代码中,但这不是我从python的帮助(时间模块)中了解到的:

^{pr2}$

我是否误解了什么?在


Tags: and代码returniftimedef时间dst
3条回答

感谢您的帮助,除了您建议的两种方法外,我还找到了一个更灵活(可能更兼容)的版本,它还可以接受时区对象(或只使用本地区)并返回UTC偏移量

只是这个模棱两可的错误部分让我很困惑,但我做了些什么使它(某种程度上)在所有情况下都有效。在

from datetime import datetime
import pytz
from tzlocal import get_localzone

def getUtcOffsetByEpoch(epoch, tz=None):
    if not tz:
        tz = get_localzone()
    delta = 0
    while True:
        try:
            return tz.utcoffset(datetime.fromtimestamp(epoch + delta)).total_seconds()
        except pytz.exceptions.AmbiguousTimeError:## FIXME
            #d = datetime.fromtimestamp(epoch+3600)
            #print('AmbiguousTimeError', d.year, d.month, d.day, d.hour, d.minute, d.second)
            delta += 3600
            print('delta = %s'%delta)
        except (
            ValueError,
            OverflowError,
        ):
            return tz._utcoffset.total_seconds()

我已经测试了这段代码以获得VM的locale UTC offset。顺便说一句,只有在被测量的那一刻才真正有效。我不确定你的代码是否等价。在

def local_ephemeral_UTC_offset(epoch_time=None):
  u"Returns a datetime.timedelta object representing the local time offset from UTC at the moment"
  if epoch_time == None:
    epoch_time = time()
  return datetime.fromtimestamp(epoch_time) - datetime.utcfromtimestamp(epoch_time)

简而言之,使用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落基山脉”时区,DST从2011年3月13日开始,到2011年11月6日结束,结果是:

^{2}$

我的时钟设置为“UTC+01:00西欧大陆”时区,DST从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

相关问题 更多 >

    热门问题