diff --git a/babel/core.py b/babel/core.py index ed271f97c..2e5d98874 100644 --- a/babel/core.py +++ b/babel/core.py @@ -1103,6 +1103,20 @@ def ordinal_form(self) -> PluralRule: """ return self._data.get('ordinal_form', _default_plural_rule) + @property + def ordinal_day_formats(self) -> localedata.LocaleDataDict: + """Locale patterns for ordinal days of the month, e.g. "13th" in English. + + The keys are `ordinal_form` plural categories, and ``{0}`` is replaced + with the day of the month. + + .. note:: The format of the value returned may change between + Babel versions. + + .. versionadded:: 2.19 + """ + return self._data.get('ordinal_day_formats', localedata.LocaleDataDict({})) + @property def measurement_systems(self) -> localedata.LocaleDataDict: """Localized names for various measurement systems. diff --git a/babel/dates.py b/babel/dates.py index 6f810c1d5..9e92fe093 100644 --- a/babel/dates.py +++ b/babel/dates.py @@ -1597,7 +1597,7 @@ def __getitem__(self, name: str) -> str: elif char in ('w', 'W'): return self.format_week(char, num) elif char == 'd': - return self.format(self.value.day, num) + return self.format_day_of_month(num) elif char == 'D': return self.format_day_of_year(num) elif char == 'F': @@ -1735,6 +1735,32 @@ def format_weekday(self, char: str = 'E', num: int = 4) -> str: context = "stand-alone" if char == "c" else "format" return get_day_names(width, context, self.locale)[weekday] + def format_day_of_month(self, num: int) -> str: + """ + Return day of month from parsed datetime according to format pattern. + + >>> from datetime import date + >>> format = DateTimeFormat(date(2016, 2, 28), Locale.parse('en_US')) + >>> format.format_day_of_month(1) + '28' + + 'd': Day of month - Use one or two for the minimum number of digits, or + three for the ordinal form, e.g. "28th" in English. Locales + without ordinal forms fall back to a single "d". + + :param num: count of format character + + """ + if num == 3: + ordinal_pattern = self.locale.ordinal_day_formats.get( + self.locale.ordinal_form(self.value.day), + ) + if ordinal_pattern: + return ordinal_pattern.replace('{0}', str(self.value.day)) + # No ordinal forms for this locale; format like a single "d" would. + num = 1 + return self.format(self.value.day, num) + def format_day_of_year(self, num: int) -> str: return self.format(self.get_day_of_year(), num) diff --git a/scripts/import_cldr.py b/scripts/import_cldr.py index 331b312de..2982631b6 100755 --- a/scripts/import_cldr.py +++ b/scripts/import_cldr.py @@ -449,6 +449,7 @@ def _process_local_datas(sup, srcdir, destdir, force=True, dump_json=False, zone parse_calendar_quarters(data, calendar) parse_calendar_eras(data, calendar) parse_calendar_periods(data, calendar) + parse_calendar_ordinal_day_formats(data, calendar) parse_calendar_date_formats(data, calendar) parse_calendar_time_formats(data, calendar) parse_calendar_datetime_skeletons(data, calendar) @@ -749,6 +750,37 @@ def parse_calendar_periods(data, calendar): dest_dict[period_type] = str(day_period.text) +def parse_calendar_ordinal_day_formats(data, calendar): + # Ordinal days of the month, e.g. "1st" in English (CLDR 49 and later). + # These are keyed by the ordinal plural categories of the locale. + ordinal_day_formats = data.setdefault('ordinal_day_formats', {}) + for day_of_month_context in calendar.findall('dayOfMonths/dayOfMonthContext'): + context_type = day_of_month_context.attrib.get('type') + if context_type != 'format': + log.warning( + '%s: unsupported dayOfMonthContext type %r', + data['locale_id'], + context_type, + ) + continue + for day_of_month_width in day_of_month_context.findall('dayOfMonthWidth'): + width_type = day_of_month_width.attrib.get('type') + if width_type != 'abbreviated': + log.warning( + '%s: unsupported dayOfMonthWidth type %r', + data['locale_id'], + width_type, + ) + continue + for elem in day_of_month_width.findall('dayOfMonth'): + ordinal = elem.attrib.get('ordinal') + if ordinal is None or elem.text is None: + continue + if _should_skip_elem(elem, ordinal, ordinal_day_formats): + continue + ordinal_day_formats[ordinal] = str(elem.text) + + def parse_calendar_date_formats(data, calendar): date_formats = data.setdefault('date_formats', {}) for format in calendar.findall('dateFormats'): diff --git a/tests/test_dates.py b/tests/test_dates.py index 76a002bc3..169f349c4 100644 --- a/tests/test_dates.py +++ b/tests/test_dates.py @@ -838,6 +838,31 @@ def test_parse_pattern(): assert dates.parse_pattern("hh' o''clock'").format == "%(hh)s o'clock" +def test_format_ordinal_day_of_month(): + def format_day(day: int, locale: str) -> str: + return dates.format_datetime(datetime(2016, 2, day), "MMM ddd, y", locale=locale) + + assert format_day(1, 'en') == 'Feb 1st, 2016' + assert format_day(2, 'en') == 'Feb 2nd, 2016' + assert format_day(3, 'en') == 'Feb 3rd, 2016' + assert format_day(4, 'en') == 'Feb 4th, 2016' + assert format_day(11, 'en') == 'Feb 11th, 2016' + assert format_day(22, 'en') == 'Feb 22nd, 2016' + + assert format_day(1, 'fr') == 'févr. 1er, 2016' + assert format_day(2, 'fr') == 'févr. 2, 2016' + + +def test_format_ordinal_day_of_month_falls_back_without_forms(): + # Locales without ordinal forms format the day like a single "d" would. + locale = Locale('de') + assert not locale.ordinal_day_formats + assert ( + dates.format_datetime(datetime(2016, 2, 1), "MMM ddd, y", locale=locale) + == 'Feb. 1, 2016' + ) + + def test_lithuanian_long_format(): assert ( dates.format_date(date(2015, 12, 10), locale='lt_LT', format='long') ==