Skip to content

Support the short localized GMT offset ('O') date pattern - #1479

Open
Goldloli wants to merge 1 commit into
python-babel:masterfrom
Goldloli:support-short-gmt-offset-O-field
Open

Goldloli wants to merge 1 commit into
python-babel:masterfrom
Goldloli:support-short-gmt-offset-O-field

Conversation

@Goldloli

Copy link
Copy Markdown

Fixes #1478.

Problem

A date pattern with a single O (short localized GMT format, e.g. GMT-8) printed the string None, because DateTimeFormat.format_timezone only implemented OOOO and fell through for O:1 (there was a # TODO: To add support for O:1 marker). PATTERN_CHARS accepts O with count 1, so the pattern validated fine and the None silently ended up in the output.

Solution

  • Add a _get_timezone_gmt_short() helper that formats the UTC offset per UTS python-format flag incorrectly forced on all extractors #35 for O: hours without a leading zero, minutes only when nonzero (GMT-8, GMT+5:30, GMT+0 for UTC), wrapped in the locale's GMT format (UTC-8 in French).
  • format_timezone now uses it for O:1; OOOO behavior is unchanged.

Note: this adds the short-GMT helper independently of #1284 (open since June, which adds a helper with the same name and semantics for the z fallback). The two should be easy to unify if that PR moves forward.

Verification

  • make import-cldr (CLDR 49)
  • python -X utf8 -m pytest tests/test_dates.py -q → 2575 passed, 2 xfailed
  • python -X utf8 -m pytest -q (full suite) → 8222 passed, 3 skipped, 2 xfailed
  • ruff check babel/dates.py tests/test_dates.py → clean

New/extended tests cover O for Los Angeles (GMT-8), UTC (GMT+0), Kolkata (GMT+5:30), and the localized French GMT format (UTC-8), exercised through both the pytz and zoneinfo fixtures.

@Muguett-DBY Muguett-DBY left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One historical offset case for the new O formatter.

Comment thread babel/dates.py
seconds = offset.days * 24 * 60 * 60 + offset.seconds
sign = '-' if seconds < 0 else '+'
hours, seconds = divmod(abs(seconds), 3600)
minutes = seconds // 60

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this keep the seconds when the offset has them? A datetime with timezone(timedelta(minutes=9, seconds=21)) gives GMT+0:09 with O and en_US on this branch. That's the historical Paris offset, and ICU's shortOffset gives GMT+0:09:21 for Paris in 1890.

minutes, seconds = divmod(seconds, 60) would let this keep the seconds while leaving the whole-minute output alone. UTS #35, localized GMT format

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

format_datetime prints "None" for the 'O' time zone field

2 participants