From 1f20ee62e8a9430c34821dc52ee460c3551b2437 Mon Sep 17 00:00:00 2001 From: YouKnowChatta <273857501+YouKnowChatta@users.noreply.github.com> Date: Fri, 4 Dec 2026 19:04:58 -0500 Subject: [PATCH] Don't truncate "uu" extended years to two digits UTS #35 gives two-letter truncation to "yy" and "YY" only. For "u" every field length is a minimum number of digits, so "uu" formats 2024 as 2024 and year 5 as 05. --- babel/dates.py | 2 +- tests/test_date_time_format.py | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/babel/dates.py b/babel/dates.py index d75bb4ea4..006b97421 100644 --- a/babel/dates.py +++ b/babel/dates.py @@ -1652,7 +1652,7 @@ def format_year(self, char: str, num: int) -> str: elif month == 12 and self.value.day > 25 and self.get_week_of_year() <= 2: value += 1 year = self.format(value, num) - if num == 2: + if num == 2 and char != 'u': # "uu" is a minimum width, not a truncation year = year[-2:] return year diff --git a/tests/test_date_time_format.py b/tests/test_date_time_format.py index 47e50c0c5..f5f5893fd 100644 --- a/tests/test_date_time_format.py +++ b/tests/test_date_time_format.py @@ -18,6 +18,21 @@ def test_quarter_format(): assert fmt['QQQQQ'] == '4' +def test_extended_year_is_not_truncated(): + # UTS #35: "For 'u', all field lengths specify a minimum number of digits; + # there is no special interpretation for 'uu'." + fmt = DateTimeFormat(date(2006, 6, 8), locale='en_US') + assert fmt['yy'] == '06' + assert fmt['YY'] == '06' + assert fmt['u'] == '2006' + assert fmt['uu'] == '2006' + assert fmt['uuu'] == '2006' + fmt = DateTimeFormat(date(5, 6, 8), locale='en_US') + assert fmt['u'] == '5' + assert fmt['uu'] == '05' + assert fmt['uuuu'] == '0005' + + def test_month_context(): d = date(2006, 2, 8) assert DateTimeFormat(d, locale='mt_MT')['MMMMM'] == 'F' # narrow format