在Windows python环境中,我可以像这样得到本地时区,但它不适用于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'
将输出转换为时区名称pytz.timezone()的好方法是什么?
以下是使用tzlocal
的答案(感谢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>
答案 0 :(得分:11)
不要根据Windows时区ID的名称做出任何假设。例如,US Mountain Standard Time
实际上是亚利桑那州大多数的Windows时区,它永久在MST中,因为它不实现夏令时。但其余山区时区的Windows ID为Mountain Standard Time
- 在Mountain Daylight Time期间遵循夏令时,但时区ID不会更改!这两个区域ID之间的唯一区别是“US”前缀。在IANA / Olson数据库中,这两个区域非常不同 - America/Phoenix
和America/Denver
。
您需要的是从Windows CLDR项目提供的Windows到Olson时区ID的映射。阅读TimeZone tag wiki获取信息和链接。我不确定是否已经有一个用Python实现这个的库 - 你可能需要做一些研究,或者自己从原始数据中实现它。
<强>更新强>
进行了一些搜索,我发现了一个名为tzlocal的Python库,它具有CLDR映射。它甚至可以包含一个脚本,该脚本将从CLDR网站获取当前映射并自行更新。我自己没有尝试过,但似乎有正确的方法。它主要侧重于在适合与pytz一起使用的IANA / Olson id中返回当前系统时区。 Here is the author's blog post describing its usage.
答案 1 :(得分:4)
Anurag Uniyal has posted另一种发现时区名称的方法,它与计算机报告的tzname和utcoffset一致。
跟进Matt Johnson的解决方案,以下是如何从Windows时区ID加载Unicode Common Locale Data Repository (CLDR) mapping到Olson时区名称:
import lxml.etree as ET
import collections
import pprint
result = {}
doc = ET.parse('http://unicode.org/repos/cldr/trunk/common/supplemental/windowsZones.xml')
for zone in doc.xpath('//mapZone'):
attrib = zone.attrib
if attrib['territory'] == '001':
result[attrib['other']] = attrib['type']
pprint.pprint(dict(result))
产量
{'AUS Central Standard Time': 'Australia/Darwin',
'AUS Eastern Standard Time': 'Australia/Sydney',
'Afghanistan Standard Time': 'Asia/Kabul',
'Alaskan Standard Time': 'America/Anchorage',
'Arab Standard Time': 'Asia/Riyadh',
'Arabian Standard Time': 'Asia/Dubai',
'Arabic Standard Time': 'Asia/Baghdad',
'Argentina Standard Time': 'America/Buenos_Aires',
'Atlantic Standard Time': 'America/Halifax',
'Azerbaijan Standard Time': 'Asia/Baku',
'Azores Standard Time': 'Atlantic/Azores',
'Bahia Standard Time': 'America/Bahia',
'Bangladesh Standard Time': 'Asia/Dhaka',
'Canada Central Standard Time': 'America/Regina',
'Cape Verde Standard Time': 'Atlantic/Cape_Verde',
'Caucasus Standard Time': 'Asia/Yerevan',
'Cen. Australia Standard Time': 'Australia/Adelaide',
'Central America Standard Time': 'America/Guatemala',
'Central Asia Standard Time': 'Asia/Almaty',
'Central Brazilian Standard Time': 'America/Cuiaba',
'Central Europe Standard Time': 'Europe/Budapest',
'Central European Standard Time': 'Europe/Warsaw',
'Central Pacific Standard Time': 'Pacific/Guadalcanal',
'Central Standard Time': 'America/Chicago',
'Central Standard Time (Mexico)': 'America/Mexico_City',
'China Standard Time': 'Asia/Shanghai',
'Dateline Standard Time': 'Etc/GMT+12',
'E. Africa Standard Time': 'Africa/Nairobi',
'E. Australia Standard Time': 'Australia/Brisbane',
'E. Europe Standard Time': 'Asia/Nicosia',
'E. South America Standard Time': 'America/Sao_Paulo',
'Eastern Standard Time': 'America/New_York',
'Egypt Standard Time': 'Africa/Cairo',
'Ekaterinburg Standard Time': 'Asia/Yekaterinburg',
'FLE Standard Time': 'Europe/Kiev',
'Fiji Standard Time': 'Pacific/Fiji',
'GMT Standard Time': 'Europe/London',
'GTB Standard Time': 'Europe/Bucharest',
'Georgian Standard Time': 'Asia/Tbilisi',
'Greenland Standard Time': 'America/Godthab',
'Greenwich Standard Time': 'Atlantic/Reykjavik',
'Hawaiian Standard Time': 'Pacific/Honolulu',
'India Standard Time': 'Asia/Calcutta',
'Iran Standard Time': 'Asia/Tehran',
'Israel Standard Time': 'Asia/Jerusalem',
'Jordan Standard Time': 'Asia/Amman',
'Kaliningrad Standard Time': 'Europe/Kaliningrad',
'Korea Standard Time': 'Asia/Seoul',
'Magadan Standard Time': 'Asia/Magadan',
'Mauritius Standard Time': 'Indian/Mauritius',
'Middle East Standard Time': 'Asia/Beirut',
'Montevideo Standard Time': 'America/Montevideo',
'Morocco Standard Time': 'Africa/Casablanca',
'Mountain Standard Time': 'America/Denver',
'Mountain Standard Time (Mexico)': 'America/Chihuahua',
'Myanmar Standard Time': 'Asia/Rangoon',
'N. Central Asia Standard Time': 'Asia/Novosibirsk',
'Namibia Standard Time': 'Africa/Windhoek',
'Nepal Standard Time': 'Asia/Katmandu',
'New Zealand Standard Time': 'Pacific/Auckland',
'Newfoundland Standard Time': 'America/St_Johns',
'North Asia East Standard Time': 'Asia/Irkutsk',
'North Asia Standard Time': 'Asia/Krasnoyarsk',
'Pacific SA Standard Time': 'America/Santiago',
'Pacific Standard Time': 'America/Los_Angeles',
'Pacific Standard Time (Mexico)': 'America/Santa_Isabel',
'Pakistan Standard Time': 'Asia/Karachi',
'Paraguay Standard Time': 'America/Asuncion',
'Romance Standard Time': 'Europe/Paris',
'Russian Standard Time': 'Europe/Moscow',
'SA Eastern Standard Time': 'America/Cayenne',
'SA Pacific Standard Time': 'America/Bogota',
'SA Western Standard Time': 'America/La_Paz',
'SE Asia Standard Time': 'Asia/Bangkok',
'Samoa Standard Time': 'Pacific/Apia',
'Singapore Standard Time': 'Asia/Singapore',
'South Africa Standard Time': 'Africa/Johannesburg',
'Sri Lanka Standard Time': 'Asia/Colombo',
'Syria Standard Time': 'Asia/Damascus',
'Taipei Standard Time': 'Asia/Taipei',
'Tasmania Standard Time': 'Australia/Hobart',
'Tokyo Standard Time': 'Asia/Tokyo',
'Tonga Standard Time': 'Pacific/Tongatapu',
'Turkey Standard Time': 'Europe/Istanbul',
'US Eastern Standard Time': 'America/Indianapolis',
'US Mountain Standard Time': 'America/Phoenix',
'UTC': 'Etc/GMT',
'UTC+12': 'Etc/GMT-12',
'UTC-02': 'Etc/GMT+2',
'UTC-11': 'Etc/GMT+11',
'Ulaanbaatar Standard Time': 'Asia/Ulaanbaatar',
'Venezuela Standard Time': 'America/Caracas',
'Vladivostok Standard Time': 'Asia/Vladivostok',
'W. Australia Standard Time': 'Australia/Perth',
'W. Central Africa Standard Time': 'Africa/Lagos',
'W. Europe Standard Time': 'Europe/Berlin',
'West Asia Standard Time': 'Asia/Tashkent',
'West Pacific Standard Time': 'Pacific/Port_Moresby',
'Yakutsk Standard Time': 'Asia/Yakutsk'}
如果您不希望您的程序依赖于lxml或网络连接,您可以将此dict粘贴到模块中并从那里使用它。