]> git.ipfire.org Git - thirdparty/babel.git/commitdiff
dates: Use format context and fallback through widths
authorAarni Koskela <akx@iki.fi>
Fri, 8 Apr 2016 05:14:06 +0000 (08:14 +0300)
committerAarni Koskela <akx@iki.fi>
Fri, 8 Apr 2016 06:13:04 +0000 (09:13 +0300)
`zh_Hant` locale data does not have names for the
`am` and `pm` day periods in the `format`/`abbreviated` context,
so fallback logic is added to deal with that eventuality.

Fixes #378

babel/dates.py
tests/test_dates.py

index 3c75b385e1b2bc59e4db3a8829e5310ed57d507b..4a0bbd3e001a062ddc9e3be6bd3b4bedc2c70776 100644 (file)
@@ -1239,6 +1239,7 @@ class DateTimeFormat(object):
         elif char in ('E', 'e', 'c'):
             return self.format_weekday(char, num)
         elif char == 'a':
+            # TODO: Add support for the rest of the period formats (a*, b*, B*)
             return self.format_period(char)
         elif char == 'h':
             if self.value.hour % 12 == 0:
@@ -1381,7 +1382,11 @@ class DateTimeFormat(object):
 
     def format_period(self, char):
         period = {0: 'am', 1: 'pm'}[int(self.value.hour >= 12)]
-        return get_period_names(locale=self.locale)[period]
+        for width in ('wide', 'narrow', 'abbreviated'):
+            period_names = get_period_names(context='format', width=width, locale=self.locale)
+            if period in period_names:
+                return period_names[period]
+        raise ValueError('Could not format period %s in %s' % (period, self.locale))
 
     def format_frac_seconds(self, num):
         """ Return fractional seconds.
index d99baf30857fe4fd2924a88e79b2557ee2add602..3bb9e8b227f3edbf54f8cb4ee3ec530116a79e28 100644 (file)
@@ -742,6 +742,11 @@ def test_lithuanian_long_format():
     )
 
 
+def test_zh_TW_format():
+    # Refs GitHub issue #378
+    assert dates.format_time(datetime(2016, 4, 8, 12, 34, 56), locale='zh_TW') == u'\u4e0b\u534812:34:56'
+
+
 def test_format_current_moment(monkeypatch):
     import datetime as datetime_module
     frozen_instant = datetime.utcnow()