Opened 7 years ago

Closed 7 years ago

Last modified 7 years ago

#16924 closed Bug (fixed)

'date' template filter displays certain timezones incorrectly

Reported by: Drew Roos Owned by: nobody
Component: Core (Other) Version: master
Severity: Normal Keywords: template, date, filter, timezone
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: yes Patch needs improvement: yes
Easy pickings: yes UI/UX: no


the date template filter will display the wrong timezone offset, if that timezone is a partial-hour offset from UTC, and is west of UTC (negative offset).

>> from django.utils.dateformat import format
>> import pytz
>> from datetime import datetime

>> x = pytz.UTC.localize(datetime(2011, 1, 1)).astimezone(pytz.timezone('America/St_Johns'))
>> format(x, 'O')

>> y = pytz.UTC.localize(datetime(1970, 1, 1)).astimezone(pytz.timezone('Africa/Monrovia'))
>> format(y, 'O')

the correct offsets are -0330 and -0044, respectively.

Attachments (1)

patch.diff (1.0 KB) - added by Drew Roos 7 years ago.
patch to fix bug

Download all attachments as: .zip

Change History (5)

comment:1 Changed 7 years ago by Aymeric Augustin

Component: Template systemCore (Other)
Easy pickings: set
Has patch: unset
Triage Stage: UnreviewedAccepted

Indeed, the arithmetic at
is incorrect for negative values:

>>> seconds = 600
>>> u"%+03d%02d" % (seconds // 3600, (seconds // 60) % 60)
>>> seconds = -600
>>> u"%+03d%02d" % (seconds // 3600, (seconds // 60) % 60)

Changed 7 years ago by Drew Roos

Attachment: patch.diff added

patch to fix bug

comment:2 Changed 7 years ago by Drew Roos

Has patch: set

comment:3 Changed 7 years ago by Aymeric Augustin

Needs tests: set
Patch needs improvement: set

math.copysign makes the code a bit hard to read.

Wouldn't this be more explicit?

sign = '-' if seconds < 0 else '+'
seconds = abs(seconds)
return '%s%02d%02d" % (sign, seconds // 3600, (seconds // 60) % 60)

This will need a regression test, too.

comment:4 Changed 7 years ago by Ramiro Morales

Resolution: fixed
Status: newclosed

In [16903]:

Fixed #16924 -- Corrected date template filter handling of negative (West of UTC) timezone offsets.

The 'O' format specifier output was incorrect. Thanks mrgriscom and Aymeric Augustin.

Note: See TracTickets for help on using tickets.
Back to Top