How can I map non-English Windows timezone names to Olsen names in Python?
If I call win32timezone.TimeZoneInfo.local().timeZoneName
, it gives me the time zone name in the current locale (for example, on a Japanese machine, it returns u"東京 (標準時)"
).
I would like to map this name to an Olsen database timezone name for use with pytz
. CLDR windowZones.xml helps me to map English names, but can't handle the Japanese name.
How can I convert the name back 开发者_Python百科to English (it should be Tokyo Standard Time
in this case)?
dict(win32timezone.TimeZoneInfo._get_indexed_time_zone_keys())
returns exactly the mapping I need from the current locale's name to the English name. The following code solves it:
import win32timezone
win32tz_name = win32timezone.TimeZoneInfo.local().timeZoneName
win32timezone_to_en = dict(win32timezone.TimeZoneInfo._get_indexed_time_zone_keys())
win32timezone_name_en = win32timezone_to_en.get(win32tz_name, win32tz_name)
olsen_name = win32timezones.get(win32timezone_name_en, None)
if not olsen_name:
raise ValueError(u"Could not map win32 timezone name %s (English %s) to Olsen timezone name" % (win32tz_name, win32timezone_name_en))
return pytz.timezone(olsen_name)
It would be nice if this was accessible in the win32timezone.TimeZoneInfo
object, though, instead of having to call a private method.
精彩评论