Skip to content

Fix get_timezone_gmt hour for negative fractional offsets#1291

Open
vineethsaivs wants to merge 1 commit into
python-babel:masterfrom
vineethsaivs:fix/timezone-gmt-negative-offset
Open

Fix get_timezone_gmt hour for negative fractional offsets#1291
vineethsaivs wants to merge 1 commit into
python-babel:masterfrom
vineethsaivs:fix/timezone-gmt-negative-offset

Conversation

@vineethsaivs

Copy link
Copy Markdown

Summary

get_timezone_gmt renders the wrong hour for any timezone whose UTC offset has a non-zero minute part and is negative. For example, Newfoundland Standard Time (America/St_Johns, UTC-03:30):

>>> from datetime import datetime
>>> from babel.dates import get_timezone, get_timezone_gmt
>>> tz = get_timezone('America/St_Johns')
>>> dt = datetime(2007, 1, 1, 12, 0, tzinfo=tz)
>>> get_timezone_gmt(dt, locale='en')
'GMT-04:30'          # should be 'GMT-03:30'
>>> get_timezone_gmt(dt, 'short', locale='en')
'-0430'              # should be '-0330'

Pacific/Marquesas (UTC-09:30) is likewise rendered as GMT-10:30. Positive fractional offsets (Asia/Kolkata, +05:30) and whole-hour negatives (America/Los_Angeles, -08:00) are correct, so this is specific to negative offsets with minutes. Newfoundland Time covers a real, populated region, so this is user-facing incorrect output.

Root cause

divmod() on a negative value floors the quotient and returns a non-negative remainder:

>>> divmod(-12600, 3600)   # -3:30 in seconds
(-4, 1800)

The code printed the floored hour (-4) via %+03d while taking the minutes from the positive remainder (1800 // 60 == 30), producing -04:30 for a -03:30 offset.

Fix

Decompose the offset from its absolute value and track the sign separately (the same idiom the code already uses elsewhere):

sign = '-' if seconds < 0 else '+'
hours, seconds = divmod(abs(seconds), 3600)

and emit the sign explicitly ('%s%02d...') instead of relying on '%+03d'.

For every offset that already formatted correctly the output is unchanged: '%s%02d' with the explicit sign is byte-identical to '%+03d' whenever 0 <= hours < 100. I verified this exhaustively across all offsets from -12:00 to +14:00 in 15-minute steps and all four widths (plus return_z): the new code differs from the old in exactly the negative-fractional cases and nowhere else.

Tests

Added three assertions to the existing test_get_timezone_gmt (already parametrized over both the pytz and zoneinfo backends). They fail before the change (AssertionError: assert 'GMT-04:30' == 'GMT-03:30') and pass after, on both backends.

Happy to add a CHANGES.rst bugfix entry if you would like one included in this PR rather than compiled at release time.

For a timezone whose UTC offset has a non-zero minute part and is
negative (for example America/St_Johns at UTC-03:30), get_timezone_gmt
rendered the hour one step too far from zero, producing 'GMT-04:30'
instead of 'GMT-03:30'.

divmod() on a negative number floors the quotient and returns a
non-negative remainder, so divmod(-12600, 3600) is (-4, 1800): the code
printed the floored hour -4 while taking the minutes from the positive
remainder. Decompose the offset from its absolute value and track the
sign separately. For every offset that already formatted correctly the
output is unchanged, because '%s%02d' with the explicit sign matches
'%+03d' whenever 0 <= hours < 100.
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.

1 participant