如何获取 pytz 时区的通用名称,例如.美国/纽约的 EST/EDT

2022-01-16 00:00:00 python django timezone pytz

问题描述

给定特定用户的 pytz 时区(从他的偏移量计算),我想显示该时区的通用名称.我假设人们更习惯于看到 EST 或 PST,而不是像 America/NewYork 这样的拼写.

Given a pytz timezone for a particular user(calculated from his offset), i want to display the common name for that timezone. I'm assuming people are more accustomed to seeing EST or PST instead of spelled out like America/NewYork.

pytz 是否在某处给了我这些标准名称,还是我必须通过表格手动执行此操作?这可能会变得混乱,因为例如某个季节的地点是美国东部标准时间,而其他地点则转向显示美国东部时间.

Does pytz give me those standard names somewhere, or will i have to manually do this via a table? This could potentially get messy, since for example places are EST in a season and shift to showing EDT during others.


解决方案

给定特定用户的 pytz 时区(从他的偏移量计算),我想显示该时区的通用名称.我假设人们更习惯于看到 EST 或 PST,而不是像 America/NewYork 这样拼写出来.

Given a pytz timezone for a particular user(calculated from his offset), i want to display the common name for that timezone. I'm assuming people are more accustomed to seeing EST or PST instead of spelled out like America/NewYork.

如果您需要从使用 pytz 本地化的 datetime 对象派生...

If you need this derived from a datetime object localized with pytz...

>>> import pytz as tz
>>> from datetime import datetime as dt
>>> CT = tz.timezone('America/Chicago')
>>>
>>> summer_day = dt(2010, 7, 4, 0, 1, 1)
>>> bar = CT.localize(summer_day, is_dst=None)
>>> bar.tzname()
'CDT'
>>>
>>> christmas = dt(2010, 12, 25, 0, 1, 1)
>>> foo = CT.localize(christmas, is_dst=None)
>>> foo.tzname()
'CST'
>>> 

相关文章