如何将 Windows 时区转换为 pytz 理解的时区? [英] How can I convert windows timezones to timezones pytz understands?

查看:30
本文介绍了如何将 Windows 时区转换为 pytz 理解的时区?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 windows python 环境中,我可以像这样获取本地时区,但它不能与 pytz 一起使用:

<预><代码>>>>导入 win32 时区>>>win32timezone.TimeZoneInfo.local()TimeZoneInfo(u'美国山区标准时间', True)>>>win32timezone.TimeZoneInfo.local().timeZoneNameu'美国山区标准时间'>>>tz = pytz.timezone(win32timezone.TimeZoneInfo.local().timeZoneName)回溯(最近一次调用最后一次):文件<stdin>",第 1 行,在 <module> 中文件C:\Python27\lib\site-packages\pytz\__init__.py",第 185 行,在时区引发 UnknownTimeZoneError(zone)pytz.exceptions.UnknownTimeZoneError: '美国山区标准时间'

将输出转换为 pytz.timezone() 会理解的时区名称的好方法是什么?

<小时>

这是使用 tzlocal 的答案(感谢 Matt):

<预><代码>>>>从 tzlocal.win32 导入 get_localzone_name>>>get_localzone_name()'美国/凤凰'>>>tz = pytz.timezone(get_localzone_name())>>>茨<DstTzInfo 'America/Phoenix' MST-1 天,17:00:00 STD>

解决方案

不要根据名称对 Windows 时区 ID 的含义做出任何假设.例如,US Mountain Standard Time 实际上是亚利桑那州大部分地区的 Windows 时区,该地区永久处于 MST,因为它不实施夏令时.但是山区时区其余部分的 Windows ID 是 Mountain Standard Time - 它确实遵循山区夏令时的夏令时,但时区 ID 不会改变!这两个区域的 ID 之间的唯一区别是US"前缀.在 IANA/Olson 数据库中,这是两个截然不同的区域 - America/PhoenixAmerica/Denver.

您需要的是 Unicode CLDR 项目提供的从 Windows 到 Olson 时区 ID 的映射.阅读 TimeZone 标签维基了解信息和链接.我不确定是否已经有一个库可以在 Python 中实现它——你可能需要做一些研究,或者从原始数据中自己实现它.

更新

经过一番搜索,我发现了一个名为 tzlocal 的 Python 库,它具有 CLDR 映射.它甚至可以包含一个脚本,该脚本将从 CLDR 网站获取当前映射并自行更新.我自己没有尝试过,但它似乎有正确的方法.它主要专注于返回当前系统时区,在适合与 pytz 一起使用的 IANA/Olson id 中.这是作者描述其用法的博客文章.

In a windows python environment I can get the local timezone like this, but it's not usable with pytz:

>>> import win32timezone
>>> win32timezone.TimeZoneInfo.local()
TimeZoneInfo(u'US Mountain Standard Time', True)
>>> win32timezone.TimeZoneInfo.local().timeZoneName
u'US Mountain Standard Time'
>>> tz = pytz.timezone(win32timezone.TimeZoneInfo.local().timeZoneName)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python27\lib\site-packages\pytz\__init__.py", line 185, in timezone
    raise UnknownTimeZoneError(zone)
pytz.exceptions.UnknownTimeZoneError: 'US Mountain Standard Time'

What's a good way to convert that output to a timezone name pytz.timezone() will understand?


Here's the answer using tzlocal (thanks to Matt):

>>> from tzlocal.win32 import get_localzone_name
>>> get_localzone_name()
'America/Phoenix'
>>> tz = pytz.timezone(get_localzone_name())
>>> tz
<DstTzInfo 'America/Phoenix' MST-1 day, 17:00:00 STD>

解决方案

Don't make any assumptions about what a Windows time zone ID means based on its name. For example US Mountain Standard Time is actually the Windows time zone for the majority of Arizona, which is permanently in MST because it does not implement daylight savings. But the Windows ID for the rest of the mountain time zone is Mountain Standard Time - which does follow daylight savings during Mountain Daylight Time, yet the time zone ID does not change! The only difference between these two zone's IDs is the "US" prefix. In the IANA/Olson database, these are two very distinct zones - America/Phoenix and America/Denver.

What you need are the mappings from Windows to Olson time zone IDs that are provided by the Unicode CLDR project. Read the TimeZone tag wiki for info and links. I am uncertain to if there is already a library that implements this in Python - you may need to do some research, or implement it yourself from the raw data.

UPDATE

A bit of searching, and I found a Python library called tzlocal that has the CLDR mappings. It even is kind enough to include a script that will go fetch the current mappings from the CLDR website and update itself. I haven't tried it myself, but it seems to have the correct approach. It is primarily focused on returning the current system timezone, in an IANA/Olson id that is suitable for use with pytz. Here is the author's blog post describing its usage.

这篇关于如何将 Windows 时区转换为 pytz 理解的时区?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆