获取时区列表

# -*- coding: utf-8 -*-

from dateutil import zoneinfo


def main():
zonefile = zoneinfo.get_zonefile_instance()
print(zonefile.zones.keys())
// dict_keys(['UTC', 'Asia/Shanghai'])

if __name__ == '__main__':
main()

utc时间转东八区时间

# -*- coding: utf-8 -*-

from datetime import datetime
import pytz


def main():
# 获取utc时间
now = datetime.utcnow()
print(now)
# 2022-09-23 06:28:42.898894

# 关键:强制更改 时区属性
now = now.replace(tzinfo=pytz.utc)

# utc转东八区
now_new = now.astimezone(pytz.timezone('Asia/Shanghai'))

now_new_str = now_new.strftime('%Y-%m-%d %H:%M:%S')
print(now_new_str)
# 2022-09-23 14:28:42


if __name__ == '__main__':
main()

参考
​​​python标准库 datetime的 astimezone设置时区时遇到的坑​

​​​python datetime 时区(timezone) dateutil