如何获取 pytz 时区的通用名称,例如。美国/纽约 EST/EDT
给定特定用户的 pytz 时区(根据他的偏移量计算),我想显示该时区的通用名称。我假设人们更习惯于看到EST或PST,而不是像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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您需要从使用
pytz
本地化的datetime
对象派生此值...If you need this derived from a
datetime
object localized withpytz
...如果您正在寻找缩写,那么您会想到以下几种方法。
首先是:
虽然它引用了前面带有单下划线的属性,但它可能被认为是私有的,并且可能不是获取它的最佳位置。另一个来自本地化的日期时间对象。
If you are looking for the abbreviations then there are a few ways that come to mind.
First would be:
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.
当这个问题最初编写时,这可能还没有出现,但这里有一个获取时区官方名称的片段:
此外,这可以与非天真的日期时间对象(又名已设置实际时区的日期时间)一起使用使用
pytz..localize()
或datetime_object.astimezone(pytz.)
如下:当然,这是,为那些登陆此处的搜索引擎用户提供启发...请访问 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:
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>)
ordatetime_object.astimezone(pytz.<timezone>)
as follows:This is, of course, for the edification of those search engine users who landed here. ... See more at the pytz module site.
您可以使用
tzinfo
实例的tzname()
方法:You can use the
tzname()
method oftzinfo
instances:请注意,我们可以使用内置包
zoneinfo
在 Python 3.9 之后发布,以获取 IANA 时区标识符的缩写。Notice that we can use built-in package
zoneinfo
shipped after Python 3.9 to get abbreviation of an IANA timezone identifier.