Python:给定时区名称的所有可能的时区缩写(反之亦然) [英] Python: All possible Timezone Abbreviations for given Timezone Name (and vise versa)
问题描述
使用pytz
,我知道如何获得时区名称的列表,但我想获得所有可能的时区每个时区名称的缩写:
导入pytz列表(pytz.common_timezones)['非洲/阿比让'、'非洲/阿克拉'、'非洲/亚的斯亚贝巴'、...]
我正在寻找的是任何时区缩写,例如PST或PDT,忽略当前日期时间(例如现在),返回所有可能的时区名称,在本例中是一个包含America/Los_Angeles的列表.
谢谢
我喜欢 unutbu 的回答,这让我找到了对我有用的答案.我有用户的语言环境,所以我发现我可以用它来消除时区缩写之间的歧义.
换句话说,这解决了以下问题:
In [242]: 'Asia/Shanghai' in tzones['CST']输出[242]:真在 [243] 中:tzones['CST'] 中的美国/中部"输出[243]:真
此功能需要两个字母的国家/地区代码:
def GetTimeZoneName(timezone, country_code):#看看它是否已经是一个有效的时区名称如果 pytz.all_timezones 中的时区:返回时区#如果是数字值,则使用 Etc/GMT 代码尝试:偏移量 = 整数(时区)如果偏移>0:偏移 = '+' + str(偏移)别的:偏移= str(偏移)返回 'Etc/GMT' + 偏移量除了值错误:经过#查找缩写country_tzones = 无尝试:country_tzones = pytz.country_timezones[country_code]除了:经过set_zones = 设置()如果 country_tzones 不是 None 并且 len(country_tzones) >0:对于 country_tzones 中的名称:tzone = pytz.timezone(名称)对于 getattr(tzone, '_transition_info', [[None, None, datetime.datetime.now(tzone).tzname()]]) 中的 utcoffset、dstoffset、tzabbrev:如果 tzabbrev.upper() == timezone.upper():set_zones.add(名称)如果 len(set_zones) >0:返回 min(set_zones, key=len)# 没有匹配的,至少在正确的国家选择一个返回 min(country_tzones, key=len)#无效国家/地区,只需尝试将时区缩写与任何时区匹配对于 pytz.all_timezones 中的名称:tzone = pytz.timezone(名称)对于 getattr(tzone, '_transition_info', [[None, None, datetime.datetime.now(tzone).tzname()]]) 中的 utcoffset、dstoffset、tzabbrev:如果 tzabbrev.upper() == timezone.upper():set_zones.add(名称)返回 min(set_zones, key=len)
这将返回 CST 的正确时区:
<预><代码>>>>GetTimeZoneName('CST','CN')'亚洲/上海'>>>GetTimeZoneName('CST','US')'美国/底特律'Using pytz
, I know how to get a listing a Timezone names, but I would like to get all possible Timezone abbreviations for each Timezone name:
import pytz
list(pytz.common_timezones)
['Africa/Abidjan', 'Africa/Accra', 'Africa/Addis_Ababa',...]
What I am looking for is given any Timezone abbreviation, example PST or PDT, ignoring current datetime (e.g. now), return the all possible Timezone name, in this case a list that would include America/Los_Angeles.
Thanks
I like unutbu's answer, which led me to the answer that worked for me. I have the locale of the user, so I found that I can use that to remove the ambiguity between time zone abbreviations.
In other words, this fixes the problem with the following:
In [242]: 'Asia/Shanghai' in tzones['CST']
Out[242]: True
In [243]: 'US/Central' in tzones['CST']
Out[243]: True
This function requires a two letter country code:
def GetTimeZoneName(timezone, country_code):
#see if it's already a valid time zone name
if timezone in pytz.all_timezones:
return timezone
#if it's a number value, then use the Etc/GMT code
try:
offset = int(timezone)
if offset > 0:
offset = '+' + str(offset)
else:
offset = str(offset)
return 'Etc/GMT' + offset
except ValueError:
pass
#look up the abbreviation
country_tzones = None
try:
country_tzones = pytz.country_timezones[country_code]
except:
pass
set_zones = set()
if country_tzones is not None and len(country_tzones) > 0:
for name in country_tzones:
tzone = pytz.timezone(name)
for utcoffset, dstoffset, tzabbrev in getattr(tzone, '_transition_info', [[None, None, datetime.datetime.now(tzone).tzname()]]):
if tzabbrev.upper() == timezone.upper():
set_zones.add(name)
if len(set_zones) > 0:
return min(set_zones, key=len)
# none matched, at least pick one in the right country
return min(country_tzones, key=len)
#invalid country, just try to match the timezone abbreviation to any time zone
for name in pytz.all_timezones:
tzone = pytz.timezone(name)
for utcoffset, dstoffset, tzabbrev in getattr(tzone, '_transition_info', [[None, None, datetime.datetime.now(tzone).tzname()]]):
if tzabbrev.upper() == timezone.upper():
set_zones.add(name)
return min(set_zones, key=len)
This returns the correct time zones for CST:
>>> GetTimeZoneName('CST','CN')
'Asia/Shanghai'
>>> GetTimeZoneName('CST','US')
'America/Detroit'
这篇关于Python:给定时区名称的所有可能的时区缩写(反之亦然)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!