本文介绍了如何在 Python 中获得人类可读的时区名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我正在处理的 Python 项目中,我希望能够获得 America/New_York 形式的人类可读"时区名称,对应于系统本地时区, 显示给用户.我见过的每段访问时区信息的代码都只返回数字偏移量 (-0400) 或字母代码 (EDT) 或有时两者兼而有之.是否有一些 Python 库可以访问这些信息,或者如果没有,将偏移/字母代码转换为人类可读的名称?

如果与特定时区对应的人类可读名称不止一个,则可能的结果列表或其中任何一个都可以,如果没有与当前时区对应的人类可读名称,我将采用异常或 None[] 或其他任何东西.

澄清:我不记得我最初写这个问题时的确切想法,但我认为我真正想要的是一种将时区变成人类可读的方法姓名.我不认为这个问题旨在专门关注如何获取系统本地时区,但是对于我想到的特定用例,恰好本地时区是我想要的名称.我不会编辑有关本地时区的问题,因为有两个方面的答案.

解决方案

最初写这个问题时可能还没有这个问题,但这里有一个片段来获得官方指定的时区:

>>>东部 = 时区('美国/东部')>>>东区'美国/东部'

此外,这可以与非原始日期时间对象(也就是使用 pytz.<timezone>.localize(<datetime_object>) 设置实际时区的日期时间一起使用)或datetime_object.astimezone(pytz.)如下:

>>>导入日期时间,pytz>>>todaynow = datetime.datetime.now(tz=pytz.timezone('美国/夏威夷'))>>>todaynow.tzinfo # 变成字符串,可以拆分/解析<DstTzInfo 'US/Hawaii' HST-1 天,14:00:00 STD>>>>todaynow.strftime("%Z")'HST'>>>todaynow.tzinfo.zone'美国/夏威夷'

这当然是为了教育那些登陆这里的搜索引擎用户.... 在 pytz 模块网站上查看更多信息.

In a Python project I'm working on, I'd like to be able to get a "human-readable" timezone name of the form America/New_York, corresponding to the system local timezone, to display to the user. Every piece of code I've seen that accesses timezone information only returns either a numeric offset (-0400) or a letter code (EDT) or sometimes both. Is there some Python library that can access this information, or if not that, convert the offset/letter code into a human-readable name?

If there's more than one human-readable name corresponding to a particular timezone, either a list of the possible results or any one of them is fine, and if there is no human-readable name corresponding to the current time zone, I'll take either an exception or None or [] or whatever.


A clarification: I don't remember exactly what I had in mind when I originally wrote this question, but I think what I really wanted was a way to turn a timezone into a human-readable name. I don't think this question was meant to focus on how to get the system local timezone specifically, but for the specific use case I had in mind, it just happened that the local timezone was the one I wanted the name for. I'm not editing the bit about the local timezone out of the question because there are answers focusing on both aspects.

解决方案

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.

这篇关于如何在 Python 中获得人类可读的时区名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 23:14