0
我使用babel和pytz来获取时区。然而,对于美国大部分地区而言,它映射到的下拉框不太有用:更好的Python日期时间显示?
“America/New_York”显示“Eastern Time”, “America/Nipigon”还显示“Eastern Time”。
有没有办法做这种转换来添加城市信息?其他时区似乎没有问题,如“亚洲/雅加达”转换为“印度尼西亚(雅加达)时间”
我使用babel和pytz来获取时区。然而,对于美国大部分地区而言,它映射到的下拉框不太有用:更好的Python日期时间显示?
“America/New_York”显示“Eastern Time”, “America/Nipigon”还显示“Eastern Time”。
有没有办法做这种转换来添加城市信息?其他时区似乎没有问题,如“亚洲/雅加达”转换为“印度尼西亚(雅加达)时间”
适用于Babel 0.9.5和Pytz 2010b。
py.tz
#!/usr/bin/env python
import pytz
import babel.dates
tz = pytz.timezone('America/New_York')
print babel.dates.get_timezone_location(tz)
输出
$ python tz.py
United States (New York) Time
你是如何运行的呢?什么版本?
如果您遇到您拥有的版本,那么为什么不仅使用洲/城市条目?
这是您的起点。它决定了大陆和城市,所以你可以根据需要进行格式化。
tzs.py
#!/usr/bin/env python
import pytz
import babel.dates
import re
country_timezones = {}
for (country, tzlist) in pytz.country_timezones.iteritems():
country_name = pytz.country_names[country]
cities = []
for timezone in tzlist:
# remove continent
city = re.sub(r'^[^/]*/', r'', timezone)
# Argentina has an extra "Argentina/" on my system (pytz 2010b)
city = re.sub(country_name + '/', '', city)
# Indiana and North Dakota have different rules by country
# change Indiana/Location to Location, Indiana
city = re.sub(r'^([^/]*)/(.*)', r'\2, \1', city)
# change underscores to spaces
city = re.sub(r'_', r' ', city)
cities.append(city)
country_timezones[country_name] = cities
for country in sorted(country_timezones):
print country
for city in sorted(country_timezones[country]):
print "\t%s" % (city)
输出
Aaland Islands
Mariehamn
Afghanistan
Kabul
...
Indonesia
Jakarta
Jayapura
Makassar
Pontianak
...
United States
Adak
Anchorage
Boise
Center, North Dakota
Chicago
Denver
Detroit