]> git.ipfire.org Git - thirdparty/babel.git/commitdiff
Added fallback support for local timezones
authorArmin Ronacher <armin.ronacher@active-4.com>
Sat, 6 Jul 2013 11:45:57 +0000 (13:45 +0200)
committerArmin Ronacher <armin.ronacher@active-4.com>
Sat, 6 Jul 2013 11:45:57 +0000 (13:45 +0200)
babel/dates.py
babel/localtime/__init__.py
babel/localtime/_win32.py

index 6730e15b0210d07f3f63b50533ba7263e69fbdf0..ee072f2ba87c10c84f0afede4bf879416fc5512e 100644 (file)
@@ -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))
index e2a45d5ffcb786d846a3ca0f9b4778b280d40438..842a03efdf92d76e923b39c4fd56191bac6f19d1 100644 (file)
@@ -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()
index 1636240ea94efa8a436beb91fbb35b5fa8952e7f..1f6ecc7c0da80b610b5b9fba48fe10a45f2efff5 100644 (file)
@@ -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())