Opened 13 years ago

Closed 13 years ago

Last modified 13 years ago

#16924 closed Bug (fixed)

'date' template filter displays certain timezones incorrectly

Reported by: Drew Roos Owned by: nobody
Component: Core (Other) Version: dev
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

Description

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')
u'-0430'

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

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

Attachments (1)

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

Download all attachments as: .zip

Change History (5)

comment:1 by Aymeric Augustin, 13 years ago

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

Indeed, the arithmetic at https://code.djangoproject.com/browser/django/trunk/django/utils/dateformat.py#L184
is incorrect for negative values:

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

by Drew Roos, 13 years ago

Attachment: patch.diff added

patch to fix bug

comment:2 by Drew Roos, 13 years ago

Has patch: set

comment:3 by Aymeric Augustin, 13 years ago

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 by Ramiro Morales, 13 years ago

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