From: Aarni Koskela Date: Fri, 8 Apr 2016 05:14:06 +0000 (+0300) Subject: dates: Use format context and fallback through widths X-Git-Tag: 2.3.2^2~2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=9f223b42e90fb80eedf33a6c09ccd2237c3eb344;p=thirdparty%2Fbabel.git dates: Use format context and fallback through widths `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 --- diff --git a/babel/dates.py b/babel/dates.py index 3c75b385..4a0bbd3e 100644 --- a/babel/dates.py +++ b/babel/dates.py @@ -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. diff --git a/tests/test_dates.py b/tests/test_dates.py index d99baf30..3bb9e8b2 100644 --- a/tests/test_dates.py +++ b/tests/test_dates.py @@ -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()