Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion babel/dates.py
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,38 @@ 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 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)

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 = 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


def get_timezone_location(
dt_or_tzinfo: _DtOrTzinfo = None,
locale: Locale | str | None = None,
Expand Down Expand Up @@ -1851,7 +1883,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':
Expand Down
34 changes: 33 additions & 1 deletion tests/test_dates.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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'
Expand All @@ -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'
Expand All @@ -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'
Expand All @@ -134,6 +137,35 @@ 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_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')
Expand Down