From 48b3e05c07da14ea74b0eb7233e03a93be609333 Mon Sep 17 00:00:00 2001 From: Xia Jiang Date: Mon, 12 Oct 2026 18:29:13 +0800 Subject: [PATCH 1/2] Support the CLDR 49 ordinal day of month pattern field CLDR 49 adds the "ddd" pattern field for ordinal days of the month ("Sept 13th") and the matching data. PATTERN_CHARS already accepts "ddd", so formatting "ddd" zero-padded the day to three digits ("001"). "ddd" now formats the day with the locale's ordinal forms, keyed by the ordinal plural category, and falls back to a single "d" for locales that have none, as the CLDR spec prescribes. scripts/import_cldr.py imports the dayOfMonth patterns. Refs #1379 --- babel/core.py | 16 ++++++++++++++++ babel/dates.py | 25 ++++++++++++++++++++++++- scripts/import_cldr.py | 20 ++++++++++++++++++++ tests/test_dates.py | 24 ++++++++++++++++++++++++ 4 files changed, 84 insertions(+), 1 deletion(-) diff --git a/babel/core.py b/babel/core.py index ed271f97c..c78ee070c 100644 --- a/babel/core.py +++ b/babel/core.py @@ -1103,6 +1103,22 @@ 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. + + Introduced in CLDR 49. + + .. 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..2cc2f6449 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,29 @@ 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 (CLDR 49 and later), e.g. "28th" in + English. Locales without ordinal forms fall back to a single "d". + """ + 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..7ecb46302 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,25 @@ 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'): + if day_of_month_context.attrib.get('type') != 'format': + continue + for day_of_month_width in day_of_month_context.findall('dayOfMonthWidth'): + if day_of_month_width.attrib.get('type') != 'abbreviated': + 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..5b9c99c5b 100644 --- a/tests/test_dates.py +++ b/tests/test_dates.py @@ -838,6 +838,30 @@ def test_parse_pattern(): assert dates.parse_pattern("hh' o''clock'").format == "%(hh)s o'clock" +def test_format_ordinal_day_of_month(): + # CLDR 49 and later provide one ordinal pattern per ordinal plural category + def format_day(day: int) -> str: + return dates.format_datetime(datetime(2016, 2, day), "MMM ddd, y", locale='en') + + assert format_day(1) == 'Feb 1st, 2016' + assert format_day(2) == 'Feb 2nd, 2016' + assert format_day(3) == 'Feb 3rd, 2016' + assert format_day(4) == 'Feb 4th, 2016' + assert format_day(11) == 'Feb 11th, 2016' + assert format_day(22) == 'Feb 22nd, 2016' + + +def test_format_ordinal_day_of_month_falls_back_without_forms(): + # Locales without ordinal forms format the day like a single "d" would, + # without zero padding (CLDR 49 has no forms for German, for instance). + day = datetime(2016, 2, 1) + assert ( + dates.format_datetime(day, "MMM ddd, y", locale='de') + == dates.format_datetime(day, "MMM d, y", locale='de') + ) + assert dates.format_datetime(day, "dd.MM.y", locale='de') == '01.02.2016' + + def test_lithuanian_long_format(): assert ( dates.format_date(date(2015, 12, 10), locale='lt_LT', format='long') == From b2b25e6f2ce79c41334f37373aca794ac26dabc1 Mon Sep 17 00:00:00 2001 From: Xia Jiang Date: Fri, 6 Nov 2026 02:06:57 +0800 Subject: [PATCH 2/2] Address review comments on ordinal day support Remove the CLDR version note from the property docstring. Document the format character count. Warn on unsupported CLDR element types. Extend the tests to French and the fallback path. Co-Authored-By: pi --- babel/core.py | 2 -- babel/dates.py | 7 +++++-- scripts/import_cldr.py | 16 ++++++++++++++-- tests/test_dates.py | 31 ++++++++++++++++--------------- 4 files changed, 35 insertions(+), 21 deletions(-) diff --git a/babel/core.py b/babel/core.py index c78ee070c..2e5d98874 100644 --- a/babel/core.py +++ b/babel/core.py @@ -1110,8 +1110,6 @@ def ordinal_day_formats(self) -> localedata.LocaleDataDict: The keys are `ordinal_form` plural categories, and ``{0}`` is replaced with the day of the month. - Introduced in CLDR 49. - .. note:: The format of the value returned may change between Babel versions. diff --git a/babel/dates.py b/babel/dates.py index 2cc2f6449..9e92fe093 100644 --- a/babel/dates.py +++ b/babel/dates.py @@ -1745,8 +1745,11 @@ def format_day_of_month(self, num: int) -> str: '28' 'd': Day of month - Use one or two for the minimum number of digits, or - three for the ordinal form (CLDR 49 and later), e.g. "28th" in - English. Locales without ordinal forms fall back to a single "d". + 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( diff --git a/scripts/import_cldr.py b/scripts/import_cldr.py index 7ecb46302..2982631b6 100755 --- a/scripts/import_cldr.py +++ b/scripts/import_cldr.py @@ -755,10 +755,22 @@ def parse_calendar_ordinal_day_formats(data, calendar): # 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'): - if day_of_month_context.attrib.get('type') != 'format': + 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'): - if day_of_month_width.attrib.get('type') != 'abbreviated': + 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') diff --git a/tests/test_dates.py b/tests/test_dates.py index 5b9c99c5b..169f349c4 100644 --- a/tests/test_dates.py +++ b/tests/test_dates.py @@ -839,27 +839,28 @@ def test_parse_pattern(): def test_format_ordinal_day_of_month(): - # CLDR 49 and later provide one ordinal pattern per ordinal plural category - def format_day(day: int) -> str: - return dates.format_datetime(datetime(2016, 2, day), "MMM ddd, y", locale='en') + 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) == 'Feb 1st, 2016' - assert format_day(2) == 'Feb 2nd, 2016' - assert format_day(3) == 'Feb 3rd, 2016' - assert format_day(4) == 'Feb 4th, 2016' - assert format_day(11) == 'Feb 11th, 2016' - assert format_day(22) == 'Feb 22nd, 2016' + 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, - # without zero padding (CLDR 49 has no forms for German, for instance). - day = datetime(2016, 2, 1) + # 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(day, "MMM ddd, y", locale='de') - == dates.format_datetime(day, "MMM d, y", locale='de') + dates.format_datetime(datetime(2016, 2, 1), "MMM ddd, y", locale=locale) + == 'Feb. 1, 2016' ) - assert dates.format_datetime(day, "dd.MM.y", locale='de') == '01.02.2016' def test_lithuanian_long_format():