From ebdcccff93df0d8d413d7a1f58736f15206ac2e4 Mon Sep 17 00:00:00 2001 From: Armin Ronacher Date: Sat, 6 Jul 2013 13:45:57 +0200 Subject: [PATCH] Added fallback support for local timezones --- babel/dates.py | 8 ++++---- babel/localtime/__init__.py | 6 +++++- babel/localtime/_win32.py | 6 +++++- 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/babel/dates.py b/babel/dates.py index 6730e15b..ee072f2b 100644 --- a/babel/dates.py +++ b/babel/dates.py @@ -41,7 +41,7 @@ date_ = date datetime_ = datetime time_ = time -def get_timezone(zone): +def get_timezone(zone=None): """Looks up a timezone by name and returns it. The timezone object returned comes from ``pytz`` and corresponds to the `tzinfo` interface and can be used with all of the functions of Babel that operate with dates. @@ -58,7 +58,7 @@ def get_timezone(zone): except _pytz.UnknownTimeZoneError: raise LookupError('Unknown timezone %s' % zone) -def get_next_timezone_transition(zone, dt=None): +def get_next_timezone_transition(zone=None, dt=None): """Given a timezone it will return a :class:`TimezoneTransition` object that holds the information about the next timezone transition that's going to happen. For instance this can be used to detect when the next DST @@ -80,8 +80,8 @@ def get_next_timezone_transition(zone, dt=None): if not hasattr(zone, '_utc_transition_times'): raise TypeError('Given timezone does not have UTC transition ' 'times. This can happen because the operating ' - 'system local timezone is used of a custom ' - 'timezone object') + 'system fallback local timezone is used or a ' + 'custom timezone object') try: idx = max(0, bisect_right(zone._utc_transition_times, dt)) diff --git a/babel/localtime/__init__.py b/babel/localtime/__init__.py index e2a45d5f..842a03ef 100644 --- a/babel/localtime/__init__.py +++ b/babel/localtime/__init__.py @@ -49,6 +49,7 @@ class _FallbackLocalTimezone(tzinfo): tt = time.localtime(stamp) return tt.tm_isdst > 0 + def get_localzone(): """Returns the current underlying local timezone object. Generally this function does not need to be used, it's a @@ -57,4 +58,7 @@ def get_localzone(): return _get_localzone() -LOCALTZ = get_localzone() +try: + LOCALTZ = get_localzone() +except pytz.UnknownTimeZoneError: + LOCALTZ = _FallbackLocalTimezone() diff --git a/babel/localtime/_win32.py b/babel/localtime/_win32.py index 1636240e..1f6ecc7c 100644 --- a/babel/localtime/_win32.py +++ b/babel/localtime/_win32.py @@ -10,7 +10,7 @@ from babel.core import get_global import pytz -tznames = get_global('windows_zone_mapping') +tz_names = get_global('windows_zone_mapping') def valuestodict(key): @@ -22,6 +22,7 @@ def valuestodict(key): dict[data[0]] = data[1] return dict + def get_localzone_name(): # Windows is special. It has unique time zone names (in several # meanings of the word) available, but unfortunately, they can be @@ -82,4 +83,7 @@ def get_localzone_name(): def _get_localzone(): + if winreg is None: + raise pytz.UnknownTimeZoneError( + 'Runtime support not available') return pytz.timezone(get_localzone_name()) -- 2.47.2