Pytz 正确偏移时区与日光

Pytz correct offset of timezones with daylight

我有一个函数 return 城市列表 + 他们的 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 小时。是白天的东西还是哪里有问题?我如何 return 根据日光获得正确的时区列表?

将 UTC 时间传递给 tzone.localize() 是不正确的,除非 tzone 是 UTC。请改用 tzone.fromutc(std_date)