【问题标题】:"Canonical" offset from UTC using pytz?使用 pytz 从 UTC 偏移“规范”?
【发布时间】:2015-02-14 00:25:47
【问题描述】:

我很好奇如何调用某些时区选择器上提供的“规范”时区偏移量(如果甚至有这样的东西,作为规范偏移量,我什至不确定)。

例如,在 Windows XP 上,您可以看到 Eastern Time (US & Canada) 的下拉菜单始终显示 GMT-05:00 这实际上对于全年来说是不正确的,因为当夏令时生效时,@ 的偏移量987654326@ 只是-4:00。另一方面,每个人都提到US/Eastern 与 UTC 相比 5 小时。我想知道-5:00 是如何被调用的。电子邮件时间标识符?规范时区偏移?

另外,如果我使用pytz 创建一个US/Eastern 时区,是否有办法获取-5:00,而不管实际现在 时间是否在夏令时?我的意思是......我想知道是否有一个功能,或者......什么东西可以得到-5:00,不管我是在今天还是在八月中旬运行它(当DST被启用并且实际偏移量只是-4:00)

图片来自http://www.microsoft.com/library/media/1033/windowsxp/images/using/setup/tips/67446-change-time-zone.gif

提前谢谢你。

【问题讨论】:

    标签: python utc timezone-offset pytz


    【解决方案1】:

    如果我们采用“规范”来表示不在 DST 中的日期的 utcoffset,那么问题就归结为查找不是 DST 的日期(针对每个时区)。

    我们可以先试试当前日期。如果不是 DST,那么我们很幸运。如果是,那么我们可以遍历 utc 转换日期列表(存储在 tzone._utc_transition_times 中),直到找到不是 DST 的日期:

    import pytz
    import datetime as DT
    utcnow = DT.datetime.utcnow()
    
    canonical = dict()
    for name in pytz.all_timezones:
        tzone = pytz.timezone(name)
        try:
            dstoffset = tzone.dst(utcnow, is_dst=False)
        except TypeError:
            # pytz.utc.dst does not have a is_dst keyword argument
            dstoffset = tzone.dst(utcnow)
        if dstoffset == DT.timedelta(0):
            # utcnow happens to be in a non-DST period
            canonical[name] = tzone.localize(utcnow, is_dst=False).strftime('%z') 
        else:
            # step through the transition times until we find a non-DST datetime
            for transition in tzone._utc_transition_times[::-1]:
                dstoffset = tzone.dst(transition, is_dst=False) 
                if dstoffset == DT.timedelta(0):
                    canonical[name] = (tzone.localize(transition, is_dst=False)
                                       .strftime('%z'))
                    break
    
    for name, utcoffset in canonical.iteritems():
        print('{} --> {}'.format(name, utcoffset)) 
    
    # All timezones have been accounted for
    assert len(canonical) == len(pytz.all_timezones)
    

    产量

    ...
    Mexico/BajaNorte --> -0800
    Africa/Kigali --> +0200
    Brazil/West --> -0400
    America/Grand_Turk --> -0400
    Mexico/BajaSur --> -0700
    Canada/Central --> -0600
    Africa/Lagos --> +0100
    GMT-0 --> +0000
    Europe/Sofia --> +0200
    Singapore --> +0800
    Africa/Tripoli --> +0200
    America/Anchorage --> -0900
    Pacific/Nauru --> +1200
    

    请注意,上面的代码访问私有属性tzone._utc_transition_times。这是 pytz 中的一个实现细节。由于它不是公共 API 的一部分,因此不保证在 pytz 的未来版本中存在。事实上,在当前版本的 pytz 中,它甚至不存在于所有时区——特别是,它不存在于没有 DST 转换时间的时区,例如 'Africa/Bujumbura'。 (这就是为什么我首先要检查 utcnow 是否恰好处于非 DST 时间段。)

    如果您想要一种不依赖私有属性的方法,我们可以简单地将utcnow 前进一天,直到我们找到非 DST 时间段内的一天。该代码会比上面的代码慢一点,但是由于您实际上只需要运行此代码一次即可收集所需的信息,因此这并不重要。

    这是不使用_utc_transition_times 的代码:

    import pytz
    import datetime as DT
    utcnow = DT.datetime.utcnow()
    
    canonical = dict()
    for name in pytz.all_timezones:
        tzone = pytz.timezone(name)
        try:
            dstoffset = tzone.dst(utcnow, is_dst=False)
        except TypeError:
            # pytz.utc.dst does not have a is_dst keyword argument
            dstoffset = tzone.dst(utcnow)
        if dstoffset == DT.timedelta(0):
            # utcnow happens to be in a non-DST period
            canonical[name] = tzone.localize(utcnow, is_dst=False).strftime('%z') 
        else:
            # step through the transition times until we find a non-DST datetime
            date = utcnow
            while True:
                date = date - DT.timedelta(days=1)
                dstoffset = tzone.dst(date, is_dst=False) 
                if dstoffset == DT.timedelta(0):
                    canonical[name] = (tzone.localize(date, is_dst=False)
                                       .strftime('%z'))
                    break
    
    for name, utcoffset in canonical.iteritems():
        print('{} --> {}'.format(name, utcoffset)) 
    
    # All timezones have been accounted for
    assert len(canonical) == len(pytz.all_timezones)
    

    【讨论】:

      猜你喜欢
      • 2018-02-12
      • 2019-04-08
      • 2013-03-11
      • 2021-06-04
      • 2013-08-10
      • 2014-02-04
      • 2015-05-27
      • 1970-01-01
      • 2010-11-24
      相关资源
      最近更新 更多