如果我们采用“规范”来表示不在 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)