]> git.ipfire.org Git - thirdparty/babel.git/commitdiff
Improved timezone name support
authorArmin Ronacher <armin.ronacher@active-4.com>
Sat, 13 Jul 2013 17:54:41 +0000 (18:54 +0100)
committerArmin Ronacher <armin.ronacher@active-4.com>
Sat, 13 Jul 2013 17:54:41 +0000 (18:54 +0100)
ChangeLog
babel/dates.py
tests/test_dates.py

index ceea1bfd9af0d5420993d0497e0fb31ad0c4668d..ef0bef51dedc1926a61ce71e1f69b1ffa0e595ad 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -70,6 +70,7 @@ http://svn.edgewall.org/repos/babel/tags/1.0.0/
    for things like timezone transitions and an overall nicer API.
  * Added support for explicit charset to PO file reading.
  * Added experimental Python 3 support.
+ * Added better support for returning timezone names.
 
 
 Version 0.9.6
index a69adc84bbdee27fc0f7e02458f69bb6d48c9fc7..58d159917a53c46b4608a8156196fbbf98f2b9e7 100644 (file)
@@ -397,7 +397,7 @@ def get_timezone_location(dt_or_tzinfo=None, locale=LC_TIME):
     })
 
 def get_timezone_name(dt_or_tzinfo=None, width='long', uncommon=False,
-                      locale=LC_TIME):
+                      locale=LC_TIME, zone_variation=None):
     r"""Return the localized display name for the given timezone. The timezone
     may be specified using a ``datetime`` or `tzinfo` object.
 
@@ -435,6 +435,11 @@ def get_timezone_name(dt_or_tzinfo=None, width='long', uncommon=False,
     >>> get_timezone_name(tz, locale='de_DE')
     u'Neufundland-Zeit'
 
+    Note that short format is currently not supported for all timezones.
+
+    .. versionmodified:: 1.0
+       Added `zone_variation` support.
+
     :param dt_or_tzinfo: the ``datetime`` or ``tzinfo`` object that determines
                          the timezone; if a ``tzinfo`` object is used, the
                          resulting display name will be generic, i.e.
@@ -442,6 +447,12 @@ def get_timezone_name(dt_or_tzinfo=None, width='long', uncommon=False,
                          current date in UTC is assumed
     :param width: either "long" or "short"
     :param uncommon: deprecated and ignored
+    :param zone_variation: defines the zone variation to return.  By default the
+                           variation is defined from the datetime object
+                           passed in.  If no datetime object is passed in, the
+                           ``'generic'`` variation is assumed.  The following
+                           values are valid: ``'generic'``, ``'daylight'`` and
+                           ``'standard'``.
     :param locale: the `Locale` object, or a locale string
     :return: the timezone display name
     :rtype: `unicode`
@@ -474,35 +485,34 @@ def get_timezone_name(dt_or_tzinfo=None, width='long', uncommon=False,
     else:
         zone = tzinfo.tzname(dt)
 
+    if zone_variation is None:
+        if dt is None:
+            zone_variation = 'generic'
+        else:
+            dst = tzinfo.dst(dt)
+            if dst:
+                zone_variation = 'daylight'
+            else:
+                zone_variation = 'standard'
+    else:
+        if zone_variation not in ('generic', 'standard', 'daylight'):
+            raise ValueError('Invalid zone variation')
+
     # Get the canonical time-zone code
     zone = get_global('zone_aliases').get(zone, zone)
 
     info = locale.time_zones.get(zone, {})
     # Try explicitly translated zone names first
     if width in info:
-        if dt is None:
-            field = 'generic'
-        else:
-            dst = tzinfo.dst(dt)
-            if dst is None:
-                field = 'generic'
-            elif dst == 0:
-                field = 'standard'
-            else:
-                field = 'daylight'
-        if field in info[width]:
-            return info[width][field]
+        if zone_variation in info[width]:
+            return info[width][zone_variation]
 
     metazone = get_global('meta_zones').get(zone)
     if metazone:
         metazone_info = locale.meta_zones.get(metazone, {})
         if width in metazone_info:
-            if dt is None:
-                field = 'generic'
-            else:
-                field = tzinfo.dst(dt) and 'daylight' or 'standard'
-            if field in metazone_info[width]:
-                return metazone_info[width][field]
+            if zone_variation in metazone_info[width]:
+                return metazone_info[width][zone_variation]
 
     # If we have a concrete datetime, we assume that the result can't be
     # independent of daylight savings time, so we return the GMT offset
index 65f7999de8f21c0c78760da5c52cae0a47585e65..1e51f8834bcbd5eaa4f54515801a66f05a7d2ef4 100644 (file)
@@ -419,6 +419,20 @@ def test_get_timezone_name():
     tz = timezone('America/St_Johns')
     assert dates.get_timezone_name(tz, locale='de_DE') == u'Neufundland-Zeit'
 
+    tz = timezone('America/Los_Angeles')
+    assert dates.get_timezone_name(tz, locale='en', width='short',
+                                   zone_variation='generic') == u'PT'
+    assert dates.get_timezone_name(tz, locale='en', width='short',
+                                   zone_variation='standard') == u'PST'
+    assert dates.get_timezone_name(tz, locale='en', width='short',
+                                   zone_variation='daylight') == u'PDT'
+    assert dates.get_timezone_name(tz, locale='en', width='long',
+                                   zone_variation='generic') == u'Pacific Time'
+    assert dates.get_timezone_name(tz, locale='en', width='long',
+                                   zone_variation='standard') == u'Pacific Standard Time'
+    assert dates.get_timezone_name(tz, locale='en', width='long',
+                                   zone_variation='daylight') == u'Pacific Daylight Time'
+
 
 def test_format_date():
     d = date(2007, 4, 1)