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

发布于 2024-11-06 20:10:19 字数 233 浏览 3 评论 0原文

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

pytz 是否在某个地方给了我这些标准名称,或者我必须通过表格手动执行此操作?这可能会变得混乱,因为例如某个季节中的地点为 EST,而在其他季节则改为显示 EDT。

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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

獨角戲 2024-11-13 20:10:19

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

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

>>> 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'
>>> 

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.

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'
>>> 
惯饮孤独 2024-11-13 20:10:19

如果您正在寻找缩写,那么您会想到以下几种方法。

首先是:

>>> from pytz import timezone
>>> eastern = timezone('US/Eastern')
>>> eastern._tzname
'EST'

虽然它引用了前面带有单下划线的属性,但它可能被认为是私有的,并且可能不是获取它的最佳位置。另一个来自本地化的日期时间对象。

>>> from datetime import datetime
>>> loc_dt = eastern.localize(datetime.now())
>>> loc_dt.strftime('%Z')
'EST'

If you are looking for the abbreviations then there are a few ways that come to mind.

First would be:

>>> from pytz import timezone
>>> eastern = timezone('US/Eastern')
>>> eastern._tzname
'EST'

Although since that references a property with the preceding single underscore it may be considered private and might not be the best place to grab it. The other would be from a localized datetime object.

>>> from datetime import datetime
>>> loc_dt = eastern.localize(datetime.now())
>>> loc_dt.strftime('%Z')
'EST'
热情消退 2024-11-13 20:10:19

当这个问题最初编写时,这可能还没有出现,但这里有一个获取时区官方名称的片段:

>>> eastern = timezone('US/Eastern')
>>> eastern.zone
'US/Eastern'

此外,这可以与非天真的日期时间对象(又名已设置实际时区的日期时间)一起使用使用 pytz..localize()datetime_object.astimezone(pytz.) 如下:

>>> import datetime, pytz
>>> todaynow = datetime.datetime.now(tz=pytz.timezone('US/Hawaii'))
>>> todaynow.tzinfo # turned into a string, it can be split/parsed
<DstTzInfo 'US/Hawaii' HST-1 day, 14:00:00 STD>
>>> todaynow.strftime("%Z")
'HST'
>>> todaynow.tzinfo.zone
'US/Hawaii'

当然,这是,为那些登陆此处的搜索引擎用户提供启发...请访问 pytz 模块站点

This may not have been around when this question was originally written, but here is a snippet to get the time zone official designation:

>>> eastern = timezone('US/Eastern')
>>> eastern.zone
'US/Eastern'

Further, this can be used with a non-naive datetime object (aka a datetime where the actual timezone has been set using pytz.<timezone>.localize(<datetime_object>) or datetime_object.astimezone(pytz.<timezone>) as follows:

>>> import datetime, pytz
>>> todaynow = datetime.datetime.now(tz=pytz.timezone('US/Hawaii'))
>>> todaynow.tzinfo # turned into a string, it can be split/parsed
<DstTzInfo 'US/Hawaii' HST-1 day, 14:00:00 STD>
>>> todaynow.strftime("%Z")
'HST'
>>> todaynow.tzinfo.zone
'US/Hawaii'

This is, of course, for the edification of those search engine users who landed here. ... See more at the pytz module site.

留蓝 2024-11-13 20:10:19

您可以使用 tzinfo 实例的 tzname() 方法:

>>> tz = timezone('America/St_Johns')
>>> normal = datetime(2009, 9, 1)
>>> tz.tzname(normal, is_dst=False)
'NDT'

You can use the tzname() method of tzinfo instances:

>>> tz = timezone('America/St_Johns')
>>> normal = datetime(2009, 9, 1)
>>> tz.tzname(normal, is_dst=False)
'NDT'
最终幸福 2024-11-13 20:10:19

请注意,我们可以使用内置包 zoneinfo 在 Python 3.9 之后发布,以获取 IANA 时区标识符的缩写。

from datetime import datetime
from zoneinfo import ZoneInfo


tz_identifier = "America/Chicago"
tz = ZoneInfo(tz_identifier)
current_time = datetime.now(tz)
return current_time.tzname()

Notice that we can use built-in package zoneinfo shipped after Python 3.9 to get abbreviation of an IANA timezone identifier.

from datetime import datetime
from zoneinfo import ZoneInfo


tz_identifier = "America/Chicago"
tz = ZoneInfo(tz_identifier)
current_time = datetime.now(tz)
return current_time.tzname()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文