我的功能可以返回城市 他们的UTC Offsets
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小时。是日光的东西还是问题在哪里?如何根据日光返回正确的时区列表?
除非tzone
是UTC,否则将UTC时间传递给tzone.localize()
是不正确的。改用tzone.fromutc(std_date)
。