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
13 changes: 7 additions & 6 deletions babel/dates.py
Original file line number Diff line number Diff line change
Expand Up @@ -461,18 +461,19 @@ def get_timezone_gmt(

offset = datetime.tzinfo.utcoffset(datetime)
seconds = offset.days * 24 * 60 * 60 + offset.seconds
hours, seconds = divmod(seconds, 3600)
sign = '-' if seconds < 0 else '+'
hours, seconds = divmod(abs(seconds), 3600)
if return_z and hours == 0 and seconds == 0:
return 'Z'
Comment on lines +464 to 467

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

While we're at it, let's move this check higher up, before we do the sign and divmod dance:

Suggested change
sign = '-' if seconds < 0 else '+'
hours, seconds = divmod(abs(seconds), 3600)
if return_z and hours == 0 and seconds == 0:
return 'Z'
if return_z and seconds == 0:
return 'Z'
sign = '-' if seconds < 0 else '+'
hours, seconds = divmod(abs(seconds), 3600)

elif seconds == 0 and width == 'iso8601_short':
return '%+03d' % hours
return '%s%02d' % (sign, hours)
elif width == 'short' or width == 'iso8601_short':
pattern = '%+03d%02d'
pattern = '%s%02d%02d'
elif width == 'iso8601':
pattern = '%+03d:%02d'
pattern = '%s%02d:%02d'
else:
pattern = locale.zone_formats['gmt'] % '%+03d:%02d'
return pattern % (hours, seconds // 60)
pattern = locale.zone_formats['gmt'] % '%s%02d:%02d'

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Just for my own record: over all of the locales we have now, zone_formats["gmt"]s are

Counter({'GMT%s': 937, 'UTC%s': 53, 'غرينتش%s': 29, 'GMT %s': 25, '𞤑𞤖𞤘%s': 13, 'जीएमटी%s': 4, '%s GMT': 4, 'MAG%s': 3, 'ᱡᱤᱮᱢᱴᱤ%s': 3, 'WAT%s': 3, '%s گرینویچ': 3, 'জি এম টি %s': 3, 'گرینیچ %s': 3, 'TMG%s': 2, 'जी.एम.टी. %s': 2, '𐐘𐐣𐐓 %s': 2, '%s Gk': 2, 'ម៉ោង\u200bសកល %s': 2, '[GMT]%s': 2, 'ग्री॰ मै॰ टै॰%s': 2, '·𐑜𐑥𐑑%s': 2, 'Гринуич%s': 2, 'जि.एम.ति %s': 2, 'ꋧꃅꎕꏦꄮꈉ%s': 2, 'ߜ߭ߕߖ%s': 2, 'जी एम टी %s': 2, 'tenpo UTC%s': 2, 'ཇི་ཨེམ་ཏི་%s': 2, 'ጂ ኤም ቲ%s': 2, 'GMT%s\u200e': 2, 'ග්\u200dරිමවේ%s': 2, 'ജിഎംടി %s': 2})

so this'll work as before.

return pattern % (sign, hours, seconds // 60)


def get_timezone_location(
Expand Down
7 changes: 7 additions & 0 deletions tests/test_dates.py
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,13 @@ def test_get_timezone_gmt(timezone_getter):
assert dates.get_timezone_gmt(dt, 'short', locale='en') == '-0700'
assert dates.get_timezone_gmt(dt, locale='en', width='iso8601_short') == '-07'
assert dates.get_timezone_gmt(dt, 'long', locale='fr_FR') == 'UTC-07:00'
# A negative offset with a non-zero minute part must keep the correct hour
# (Newfoundland Standard Time is UTC-03:30, not UTC-04:30).
tz = timezone_getter('America/St_Johns')
dt = _localize(tz, datetime(2007, 1, 1, 12, 0))
assert dates.get_timezone_gmt(dt, locale='en') == 'GMT-03:30'
assert dates.get_timezone_gmt(dt, 'short', locale='en') == '-0330'
assert dates.get_timezone_gmt(dt, locale='en', width='iso8601') == '-03:30'


def test_get_timezone_location(timezone_getter):
Expand Down
Loading