From 61d9d62293273b6807a6ef68e7e8f9516d20a0ee Mon Sep 17 00:00:00 2001 From: Goldloli <39011904+Goldloli@users.noreply.github.com> Date: Fri, 20 Nov 2026 13:36:17 +0800 Subject: [PATCH 1/2] Support the short localized GMT offset ('O') date pattern --- babel/dates.py | 30 +++++++++++++++++++++++++++++- tests/test_dates.py | 13 +++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/babel/dates.py b/babel/dates.py index 6f810c1d5..05d2787cc 100644 --- a/babel/dates.py +++ b/babel/dates.py @@ -541,6 +541,34 @@ def get_timezone_gmt( return pattern % (sign, hours, seconds // 60) +def _get_timezone_gmt_short( + datetime: _Instant = None, + locale: Locale | str | None = None, +) -> str: + """Return the offset from GMT for the given `datetime` in the short + localized GMT format, e.g. ``GMT-8`` or ``GMT+5:30``. + + The hours are given without a leading zero and the minutes are only + included when nonzero. The offset is wrapped in the locale's GMT format, + so e.g. French locales render as ``UTC-8``. + + This is the format used by the ``O`` date pattern field. (UTS #35) + """ + datetime = _ensure_datetime_tzinfo(_get_datetime(datetime)) + locale = Locale.parse(locale or LC_TIME) + + offset = datetime.tzinfo.utcoffset(datetime) + seconds = offset.days * 24 * 60 * 60 + offset.seconds + sign = '-' if seconds < 0 else '+' + hours, seconds = divmod(abs(seconds), 3600) + minutes = seconds // 60 + if minutes: + offset_string = f"{sign}{hours}:{minutes:02d}" + else: + offset_string = f"{sign}{hours}" + return locale.zone_formats['gmt'] % offset_string + + def get_timezone_location( dt_or_tzinfo: _DtOrTzinfo = None, locale: Locale | str | None = None, @@ -1851,7 +1879,7 @@ def format_timezone(self, char: str, num: int) -> str: elif char == 'O': if num == 4: return get_timezone_gmt(value, width, locale=self.locale) - # TODO: To add support for O:1 + return _get_timezone_gmt_short(value, locale=self.locale) elif char == 'v': return get_timezone_name(value.tzinfo, width, locale=self.locale) elif char == 'V': diff --git a/tests/test_dates.py b/tests/test_dates.py index 76a002bc3..6287a2aad 100644 --- a/tests/test_dates.py +++ b/tests/test_dates.py @@ -75,6 +75,7 @@ def test_format_datetime_timezone_formats_los_angeles(timezone_getter): assert dates.format_datetime(dt, 'ZZZ', locale='en') == '-0800' assert dates.format_datetime(dt, 'ZZZZ', locale='en') == 'GMT-08:00' assert dates.format_datetime(dt, 'ZZZZZ', locale='en') == '-08:00' + assert dates.format_datetime(dt, 'O', locale='en') == 'GMT-8' assert dates.format_datetime(dt, 'OOOO', locale='en') == 'GMT-08:00' assert dates.format_datetime(dt, 'VV', locale='en') == 'America/Los_Angeles' assert dates.format_datetime(dt, 'VVV', locale='en') == 'Los Angeles' @@ -98,6 +99,7 @@ def test_format_datetime_timezone_formats_utc(timezone_getter): assert dates.format_datetime(dt, 'ZZZ', locale='en') == '+0000' assert dates.format_datetime(dt, 'ZZZZ', locale='en') == 'GMT+00:00' assert dates.format_datetime(dt, 'ZZZZZ', locale='en') == 'Z' + assert dates.format_datetime(dt, 'O', locale='en') == 'GMT+0' assert dates.format_datetime(dt, 'OOOO', locale='en') == 'GMT+00:00' assert dates.format_datetime(dt, 'VV', locale='en') == 'Etc/UTC' assert dates.format_datetime(dt, 'VVV', locale='en') == 'UTC' @@ -119,6 +121,7 @@ def test_format_datetime_timezone_formats_kolkata(timezone_getter): assert dates.format_datetime(dt, 'zzzz', locale='en') == 'India Standard Time' assert dates.format_datetime(dt, 'ZZZZ', locale='en') == 'GMT+05:30' assert dates.format_datetime(dt, 'ZZZZZ', locale='en') == '+05:30' + assert dates.format_datetime(dt, 'O', locale='en') == 'GMT+5:30' assert dates.format_datetime(dt, 'OOOO', locale='en') == 'GMT+05:30' assert dates.format_datetime(dt, 'VV', locale='en') == 'Asia/Kolkata' assert dates.format_datetime(dt, 'VVV', locale='en') == 'Kolkata' @@ -134,6 +137,16 @@ def test_format_datetime_timezone_formats_kolkata(timezone_getter): assert dates.format_datetime(dt, 'xxxxx', locale='en') == '+05:30' +def test_format_datetime_timezone_short_gmt_localized(timezone_getter): + # The short localized GMT format (O) wraps the offset in the locale's + # own GMT format, e.g. UTC−8 in French. + tz = timezone_getter('America/Los_Angeles') + dt = _localize(tz, datetime(2016, 1, 13, 7, 8, 35)) + assert dates.format_datetime(dt, 'h:mm a O', locale='en') == '7:08 AM GMT-8' + assert dates.format_time(dt, 'HH:mm O', locale='en') == '07:08 GMT-8' + assert dates.format_datetime(dt, 'O', locale='fr_FR') == 'UTC-8' + + def test_format_time_with_naive_datetime_and_tzinfo(timezone_getter): d = datetime(2007, 4, 1, 15, 30) formatted = dates.format_time(d, 'long', tzinfo=timezone_getter('US/Eastern'), locale='en') From 13a42d6b743448bfe410f307cf69b303e824f9c0 Mon Sep 17 00:00:00 2001 From: Goldloli <39011904+Goldloli@users.noreply.github.com> Date: Sun, 22 Nov 2026 00:41:29 +0800 Subject: [PATCH 2/2] Keep the seconds in the short localized GMT offset ('O') --- babel/dates.py | 24 ++++++++++++++---------- tests/test_dates.py | 21 ++++++++++++++++++++- 2 files changed, 34 insertions(+), 11 deletions(-) diff --git a/babel/dates.py b/babel/dates.py index 05d2787cc..61710d214 100644 --- a/babel/dates.py +++ b/babel/dates.py @@ -548,11 +548,14 @@ def _get_timezone_gmt_short( """Return the offset from GMT for the given `datetime` in the short localized GMT format, e.g. ``GMT-8`` or ``GMT+5:30``. - The hours are given without a leading zero and the minutes are only - included when nonzero. The offset is wrapped in the locale's GMT format, - so e.g. French locales render as ``UTC-8``. - - This is the format used by the ``O`` date pattern field. (UTS #35) + The hours are given without a leading zero and the minutes and seconds + are only included when nonzero, e.g. ``GMT+0:09:21`` for an offset of + 9 minutes and 21 seconds (historical timezones may carry seconds in + their UTC offset). The offset is wrapped in the locale's GMT format, so + e.g. French locales render as ``UTC-8``. + + This is the format used by the ``O`` date pattern field. (UTS #35, + short localized GMT format) """ datetime = _ensure_datetime_tzinfo(_get_datetime(datetime)) locale = Locale.parse(locale or LC_TIME) @@ -561,11 +564,12 @@ def _get_timezone_gmt_short( seconds = offset.days * 24 * 60 * 60 + offset.seconds sign = '-' if seconds < 0 else '+' hours, seconds = divmod(abs(seconds), 3600) - minutes = seconds // 60 - if minutes: - offset_string = f"{sign}{hours}:{minutes:02d}" - else: - offset_string = f"{sign}{hours}" + minutes, seconds = divmod(seconds, 60) + offset_string = f"{sign}{hours}" + if minutes or seconds: + offset_string += f":{minutes:02d}" + if seconds: + offset_string += f":{seconds:02d}" return locale.zone_formats['gmt'] % offset_string diff --git a/tests/test_dates.py b/tests/test_dates.py index 6287a2aad..97e2c28fe 100644 --- a/tests/test_dates.py +++ b/tests/test_dates.py @@ -11,7 +11,7 @@ # history and logs, available at https://github.com/python-babel/babel/commits/master/. import calendar -from datetime import date, datetime, time, timedelta +from datetime import date, datetime, time, timedelta, timezone import freezegun import pytest @@ -147,6 +147,25 @@ def test_format_datetime_timezone_short_gmt_localized(timezone_getter): assert dates.format_datetime(dt, 'O', locale='fr_FR') == 'UTC-8' +def test_format_datetime_timezone_short_gmt_localized_with_seconds(): + # Historical timezones can have a UTC offset with a seconds part, e.g. + # Europe/Paris in 1890 was UTC+00:09:21 (the "Paris mean time" offset). + # The short localized GMT format keeps the seconds, like ICU's + # `shortOffset` does (UTS #35, localized GMT format). + tz = timezone(timedelta(minutes=9, seconds=21)) + dt = datetime(1890, 6, 1, 12, 0, tzinfo=tz) + assert dates.format_datetime(dt, 'O', locale='en_US') == 'GMT+0:09:21' + assert dates.format_datetime(dt, 'h:mm a O', locale='en_US') == '12:00 PM GMT+0:09:21' + # The locale's own GMT format is still applied. + assert dates.format_datetime(dt, 'O', locale='fr_FR') == 'UTC+0:09:21' + # The long localized GMT format (OOOO) is unaffected and keeps its + # existing hours:minutes output. + assert dates.format_datetime(dt, 'OOOO', locale='en_US') == 'GMT+00:09' + # Negative offsets with seconds keep the sign and both parts. + dt = datetime(1890, 6, 1, 12, 0, tzinfo=timezone(-timedelta(minutes=9, seconds=21))) + assert dates.format_datetime(dt, 'O', locale='en_US') == 'GMT-0:09:21' + + def test_format_time_with_naive_datetime_and_tzinfo(timezone_getter): d = datetime(2007, 4, 1, 15, 30) formatted = dates.format_time(d, 'long', tzinfo=timezone_getter('US/Eastern'), locale='en')