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