Opened 15 years ago

Closed 8 years ago

#11321 closed Bug (fixed)

Bug with humanize and timezones

Reported by: aarond10 Owned by: nobody
Component: contrib.humanize Version: dev
Severity: Normal Keywords: humanize, naturalday, timezone
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: yes
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: yes

Description

Humanize's naturalday filter uses "date.today()" to determine relative dates. This is not valid if you pass it a datetime with the tzinfo field set. The following patch uses a datetime object as an intermediary to correct this behavior.

Attachments (5)

humanize_patch.txt (1.5 KB ) - added by aarond10 15 years ago.
Patch to humanize.py
11321-r11173.diff (1.2 KB ) - added by Sergio Oliveira 15 years ago.
Patch including some fixes (using svn diff)
11321-r11173-test.diff (2.2 KB ) - added by Sergio Oliveira 15 years ago.
Unit tests
11321-r11173.2.diff (3.4 KB ) - added by Sergio Oliveira 15 years ago.
11321-r11173.3.diff (3.4 KB ) - added by Sergio Oliveira 15 years ago.
Merging tests and patch

Download all attachments as: .zip

Change History (13)

by aarond10, 15 years ago

Attachment: humanize_patch.txt added

Patch to humanize.py

comment:1 by Russell Keith-Magee, 15 years ago

milestone: 1.1
Needs tests: set
Triage Stage: UnreviewedAccepted

This is annoying, but it isn't critical for v1.1 at this late stage (it doesn't cause data loss, and there are viable workarounds).

Also, the patch needs tests in order to prove that it works.

comment:2 by aarond10ster@…, 15 years ago

Very basic test cases:

from django.contrib.humanize.templatetags.humanize import naturalday
from datetime import datetime
from pytz import timezone

# Test without timezone (basically the same as existing code)

naturalday(datetime.now())

u'today'

# GMT+9

naturalday(datetime.now(timezone('Asia/Tokyo')))

u'today'

# GMT+2

naturalday(datetime.now(timezone('Europe/Rome')))

u'today'

# GMT+0

naturalday(datetime.now(timezone('UTC')))

u'today'

# GMT-4

naturalday(datetime.now(timezone('America/New_York')))

u'today'

by Sergio Oliveira, 15 years ago

Attachment: 11321-r11173.diff added

Patch including some fixes (using svn diff)

by Sergio Oliveira, 15 years ago

Attachment: 11321-r11173-test.diff added

Unit tests

by Sergio Oliveira, 15 years ago

Attachment: 11321-r11173.2.diff added

by Sergio Oliveira, 15 years ago

Attachment: 11321-r11173.3.diff added

Merging tests and patch

comment:3 by Brandon M Height, 13 years ago

Needs documentation: set
Needs tests: unset

comment:4 by Gabriel Hurley, 13 years ago

Component: Contrib appscontrib.humanize

comment:5 by Julien Phalip, 13 years ago

Severity: Normal
Type: Bug

comment:6 by Jannis Leidel, 13 years ago

Resolution: fixed
Status: newclosed

In [16072]:

Fixed #11321 -- Handle timezones correctly in conjunction with naturalday filter. Thanks, aarond10 and seocam.

comment:7 by Gerben Morsink, 8 years ago

Easy pickings: set
Resolution: fixed
Status: closednew
UI/UX: set

To be honoust I still don't like the solution.

It means you have to create a custom Date object (that inherits from datetime.date), where you manually set the tzinfo attribute.

I think using timezone.get_current_timezone() would be much more convenient!

comment:8 by Aymeric Augustin, 8 years ago

Resolution: fixed
Status: newclosed

This ticket was closed 5 years ago, before Django gained support for timezones in version 1.4. The naturalday filter was refactored in that version.

Given that the landscape has changed significantly since this ticket was discussed, it would be best to open a new ticket and explain the problem you see in the current implementation.

That will make it more likely that someone will understand what you want to do and make it happen. Thanks!

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