我有一个函数可以返回城市列表+他们的UTC偏移量
NOW = datetime.datetime.utcnow()
ZERO = datetime.timedelta(0)
result_list = {}
allowed_city = ['Europe/London', 'Europe/Amsterdam', 'Europe/Helsinki', 'Europe/Moscow',
'Asia/Yekaterinburg', 'Asia/Novosibirsk', 'Asia/Shanghai', 'Asia/Tokyo']
for tname in allowed_city:
tzone = pytz.timezone(tname)
std_date = None
try:
for utcdate, info in zip(
tzone._utc_transition_times, tzone._transition_info):
utcoffset, dstoffset, tzname = info
if dstoffset == ZERO:
std_date = utcdate
if utcdate > NOW:
break
except AttributeError:
std_date = NOW
std_date = tzone.localize(std_date)
key = std_date.strftime('%z')
result_list[tname] = "%s UTC %s" % (key, tname)
并按抵消顺序排列:
collections.OrderedDict(sorted(result_list.items(), key=lambda x: x[1]))
获取下一个清单:
+0000 UTC Europe/London
+0200 UTC Europe/Amsterdam
...
+0900 UTC Asia/Shanghai
+1000 UTC Asia/Tokyo
但是例如上海是UTC / GMT +8小时。是日光还是问题?如何根据日光返回正确的时区列表?
答案 0 :(得分:2)
将UTC时间传递给tzone.localize()
是不正确的,除非tzone
是UTC。请改用tzone.fromutc(std_date)
。