2011-02-08 9 views
0

나는 표준 시간대를 얻기 위해 바벨 (babel)과 피츠 (pytz)를 사용하고 있습니다. 그러나 대부분의 미국에서는 드롭 다운 상자에서 도움이되지 않는 것으로 매핑됩니다.더 나은 파이썬 datetime 디스플레이?

"America/New_York"은 "동부 표준시", 을 표시합니다. "America/Nipigon"도 "동부 표준시"를 표시합니다.

도시 정보를 추가하기 위해이 변환을 수행 할 수있는 방법이 있습니까? "아시아/자카르타"가 "인도네시아 (자카르타) 시간"으로 변환되는 것처럼 다른 시간대는 괜찮아 보입니다.

답변

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 

당신은 어떻게 그것을 실행하는? 어떤 버전?


보유하고있는 버전에 문제가 있다면 왜 대륙/도시 항목을 사용하지 않을까요?

여기가 출발점입니다. 대륙과 도시를 모두 결정하므로 원하는 형식으로 서식을 지정할 수 있습니다.

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