Opened 4 years ago

Closed 4 years ago

Last modified 4 years ago

#16924 closed Bug (fixed)

'date' template filter displays certain timezones incorrectly

Reported by: mrgriscom 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

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 mrgriscom 4 years ago.
patch to fix bug

Download all attachments as: .zip

Change History (5)

comment:1 Changed 4 years ago by aaugustin

  • Component changed from Template system to Core (Other)
  • Easy pickings set
  • Has patch unset
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted

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'

Changed 4 years ago by mrgriscom

patch to fix bug

comment:2 Changed 4 years ago by mrgriscom

  • Has patch set

comment:3 Changed 4 years ago by aaugustin

  • 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 4 years ago by ramiro

  • Resolution set to fixed
  • Status changed from new to closed

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