2

如果我调用win32timezone.TimeZoneInfo.local().timeZoneName,它会给我当前语言环境中的时区名称(例如,在日本机器上,它会返回u"東京 (標準時)")。

我想将此名称映射到 Olsen 数据库时区名称,以便与pytz. CLDR windowZones.xml帮助我映射英文名称,但无法处理日文名称。

如何将名称转换回英文(Tokyo Standard Time在这种情况下应该是)?

4

1 回答 1

3

dict(win32timezone.TimeZoneInfo._get_indexed_time_zone_keys())准确返回我需要的从当前语言环境名称到英文名称的映射。下面的代码解决了它:

  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)

win32timezone.TimeZoneInfo不过,如果它可以在对象中访问,而不是调用私有方法,那就太好了。

于 2010-07-22T07:55:44.077 回答