0

タイムゾーンを取得するためにbabelとpytzを使用しています。ただし、アメリカのほとんどの場合、ドロップダウン ボックスではあまり役に立たないものにマップされます。

"America/New_York" は "Eastern Time" を表示し、"America/Nipigon" も "Eastern Time" を表示します。

都市情報を追加するためにこの変換を行う方法はありますか? 「アジア/ジャカルタ」が「インドネシア (ジャカルタ) 時間」に変換されるなど、他のタイムゾーンは問題ないようです。

4

1 に答える 1

2

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

どのように実行していますか?バージョンは?


お持ちのバージョンにこだわるなら、Continent/City エントリだけを使用しないのはなぜですか?

ここがあなたの出発点です。大陸と都市の両方を決定するため、必要に応じてフォーマットできます。

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
于 2011-02-08T08:44:09.320 に答える