diff options
author | Yaron de Leeuw <me@jarondl.net> | 2014-12-10 20:32:04 +0200 |
---|---|---|
committer | Yaron de Leeuw <me@jarondl.net> | 2014-12-10 20:32:04 +0200 |
commit | 3732a814b2aece82d2d23367ee521f234a433a78 (patch) | |
tree | d2b7d589bd7246f1e62429d4670288dcd22f8e33 | |
parent | b8e944b5111350c79e2abfa77076d5a749f1de4d (diff) | |
download | dateutil-3732a814b2aece82d2d23367ee521f234a433a78.tar.gz |
Windows: Be more lenient about missing timezones, like on posix
-rw-r--r-- | dateutil/tz.py | 5 | ||||
-rw-r--r-- | dateutil/tzwin.py | 38 |
2 files changed, 21 insertions, 22 deletions
diff --git a/dateutil/tz.py b/dateutil/tz.py index 50a1836..31879e8 100644 --- a/dateutil/tz.py +++ b/dateutil/tz.py @@ -960,7 +960,10 @@ def gettz(name=None): else: tz = None if tzwin is not None: - tz = tzwin(name) + try: + tz = tzwin(name) + except WindowsError: + tz = None if not tz: from dateutil.zoneinfo import gettz tz = gettz(name) diff --git a/dateutil/tzwin.py b/dateutil/tzwin.py index 5e6864b..e8a82d7 100644 --- a/dateutil/tzwin.py +++ b/dateutil/tzwin.py @@ -83,11 +83,11 @@ class tzwin(tzwinbase): def __init__(self, name): self._name = name - handle = winreg.ConnectRegistry(None, winreg.HKEY_LOCAL_MACHINE) - tzkey = winreg.OpenKey(handle, "%s\%s" % (TZKEYNAME, name)) - keydict = valuestodict(tzkey) - tzkey.Close() - handle.Close() + # multiple contexts only possible in 2.7 and 3.1, we still support 2.6 + with winreg.ConnectRegistry(None, winreg.HKEY_LOCAL_MACHINE) as handle: + with winreg.OpenKey(handle, + "%s\%s" % (TZKEYNAME, name)) as tzkey: + keydict = valuestodict(tzkey) self._stdname = keydict["Std"].encode("iso-8859-1") self._dstname = keydict["Dlt"].encode("iso-8859-1") @@ -124,25 +124,21 @@ class tzwinlocal(tzwinbase): def __init__(self): - handle = winreg.ConnectRegistry(None, winreg.HKEY_LOCAL_MACHINE) - - tzlocalkey = winreg.OpenKey(handle, TZLOCALKEYNAME) - keydict = valuestodict(tzlocalkey) - tzlocalkey.Close() + with winreg.ConnectRegistry(None, winreg.HKEY_LOCAL_MACHINE) as handle: - self._stdname = keydict["StandardName"].encode("iso-8859-1") - self._dstname = keydict["DaylightName"].encode("iso-8859-1") + with winreg.OpenKey(handle, TZLOCALKEYNAME) as tzlocalkey: + keydict = valuestodict(tzlocalkey) - try: - tzkey = winreg.OpenKey(handle, "%s\%s" % (TZKEYNAME, - self._stdname)) - _keydict = valuestodict(tzkey) - self._display = _keydict["Display"] - tzkey.Close() - except OSError: - self._display = None + self._stdname = keydict["StandardName"].encode("iso-8859-1") + self._dstname = keydict["DaylightName"].encode("iso-8859-1") - handle.Close() + try: + with winreg.OpenKey( + handle, "%s\%s" % (TZKEYNAME, self._stdname)) as tzkey: + _keydict = valuestodict(tzkey) + self._display = _keydict["Display"] + except OSError: + self._display = None self._stdoffset = -keydict["Bias"]-keydict["StandardBias"] self._dstoffset = self._stdoffset-keydict["DaylightBias"] |