Skip to content

Commit

Permalink
Merge pull request #19 from dave42/improve_windows_territory_handling
Browse files Browse the repository at this point in the history
Improve windows timezone mapping
  • Loading branch information
mithro committed Sep 7, 2015
2 parents 6606481 + f5fa0f2 commit 6e65ea1
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 18 deletions.
4 changes: 3 additions & 1 deletion datetime_tz/detect_windows.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import ctypes
import warnings
import locale

import pytz

Expand Down Expand Up @@ -115,7 +116,8 @@ def _detect_timezone_windows():
win32timezone.TimeZoneInfo._get_indexed_time_zone_keys("Std"))
win32tz_key_name = win32timezone_to_en.get(win32tz_name, win32tz_name)

olson_name = win32tz_map.win32timezones.get(win32tz_key_name, None)
territory = locale.getdefaultlocale()[0].split("_", 1)[1]
olson_name = win32tz_map.win32timezones.get((win32tz_key_name, territory), win32tz_map.win32timezones.get(win32tz_key_name, None))
if not olson_name:
return None
if not isinstance(olson_name, str):
Expand Down
25 changes: 15 additions & 10 deletions datetime_tz/update_win32tz_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,30 +65,31 @@ def create_win32tz_map(windows_zones_xml):
"""
coming_comment = None
win32_name = None
territory = None
parser = genshi.input.XMLParser(StringIO(windows_zones_xml))
map_zones = {}
zone_comments = {}

for kind, data, _ in parser:
if kind == genshi.core.START and str(data[0]) == "mapZone":
attrs = data[1]
win32_name, olson_name = (
attrs.get("other"), attrs.get("type").split(" ")[0])
win32_name, territory, olson_name = (
attrs.get("other"), attrs.get("territory"), attrs.get("type").split(" ")[0])

map_zones[win32_name] = olson_name
map_zones[(win32_name, territory)] = olson_name
elif kind == genshi.core.END and str(data) == "mapZone" and win32_name:
if coming_comment:
zone_comments[win32_name] = coming_comment
zone_comments[(win32_name, territory)] = coming_comment
coming_comment = None
win32_name = None
elif kind == genshi.core.COMMENT:
coming_comment = data.strip()
elif kind in (genshi.core.START, genshi.core.END, genshi.core.COMMENT):
coming_comment = None

for win32_name in sorted(map_zones):
yield (win32_name, map_zones[win32_name],
zone_comments.get(win32_name, None))
for win32_name, territory in sorted(map_zones):
yield (win32_name, territory, map_zones[(win32_name, territory)],
zone_comments.get((win32_name, territory), None))


def update_stored_win32tz_map():
Expand Down Expand Up @@ -120,9 +121,13 @@ def update_stored_win32tz_map():
source_hash))

map_file.write("win32timezones = {\n")
for win32_name, olson_name, comment in map_zones:
map_file.write(" %r: %r, # %s\n" % (
win32_name, olson_name, comment or ""))
for win32_name, territory, olson_name, comment in map_zones:
if territory == '001':
map_file.write(" %r: %r, # %s\n" % (
str(win32_name), str(olson_name), comment or ""))
else:
map_file.write(" %r: %r, # %s\n" % (
(str(win32_name), str(territory)), str(olson_name), comment or ""))
map_file.write("}\n")

map_file.close()
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def run(self):

data = dict(
name='python-datetime-tz',
version='0.5',
version='0.5.1',
author='Tim Ansell',
author_email='mithro@mithis.com',
url='http://github.com/mithro/python-datetime-tz',
Expand Down
15 changes: 9 additions & 6 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,12 +403,13 @@ def testWindowsTimezones(self):
self.assertNotEqual(detect_windows._detect_timezone_windows(), None)

class kernel32_old(object):
STANDARD_NAME = "South Africa Standard Time"

@staticmethod
def GetTimeZoneInformation(tzi_byref):
@classmethod
def GetTimeZoneInformation(cls, tzi_byref):
tzi = tzi_byref._obj
tzi.bias = -120
tzi.standard_name = "South Africa Standard Time"
tzi.standard_name = cls.STANDARD_NAME
tzi.standard_start = detect_windows.SYSTEMTIME_c()
tzi.standard_start.year = 0
tzi.standard_start.month = 0
Expand Down Expand Up @@ -450,15 +451,15 @@ class windll(object):

self.assertTimezoneEqual(
detect_windows._detect_timezone_windows(),
pytz.timezone("Etc/GMT-2"))
pytz.timezone("Africa/Johannesburg"))

windll.kernel32 = kernel32_old
if win32timezone is None:
self.assertEqual(detect_windows._detect_timezone_windows(), None)
else:
self.assertTimezoneEqual(
detect_windows._detect_timezone_windows(),
pytz.timezone("Etc/GMT-2"))
pytz.timezone("Africa/Johannesburg"))

class _win32timezone_mock(object):

Expand All @@ -469,10 +470,12 @@ def _get_indexed_time_zone_keys(*unused_args, **unused_kwargs):
return {"South Africa Standard Time": "AUS Eastern Standard Time"}

self.mocked("detect_windows.win32timezone", _win32timezone_mock)
detect_windows.win32timezone_to_en = {}
self.assertTimezoneEqual(
detect_windows._detect_timezone_windows(),
pytz.timezone("Australia/Sydney"))

kernel32_old.STANDARD_NAME = "DoesNotExist"
self.assertEqual(detect_windows._detect_timezone_windows(), None)

class TestDatetimeTZ(TestTimeZoneBase):

Expand Down

0 comments on commit 6e65ea1

Please sign in to comment.