Opened 7 years ago

Closed 5 months ago

#11321 closed Bug (fixed)

Bug with humanize and timezones

Reported by: aarond10 Owned by: nobody
Component: contrib.humanize Version: master
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 7 years ago.
Patch to humanize.py
11321-r11173.diff (1.2 KB) - added by Sergio Oliveira 7 years ago.
Patch including some fixes (using svn diff)
11321-r11173-test.diff (2.2 KB) - added by Sergio Oliveira 7 years ago.
Unit tests
11321-r11173.2.diff (3.4 KB) - added by Sergio Oliveira 7 years ago.
11321-r11173.3.diff (3.4 KB) - added by Sergio Oliveira 7 years ago.
Merging tests and patch

Download all attachments as: .zip

Change History (13)

Changed 7 years ago by aarond10

Attachment: humanize_patch.txt added

Patch to humanize.py

comment:1 Changed 7 years ago by Russell Keith-Magee

milestone: 1.1
Needs documentation: unset
Needs tests: set
Patch needs improvement: unset
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 Changed 7 years ago by aarond10ster@…

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'

Changed 7 years ago by Sergio Oliveira

Attachment: 11321-r11173.diff added

Patch including some fixes (using svn diff)

Changed 7 years ago by Sergio Oliveira

Attachment: 11321-r11173-test.diff added

Unit tests

Changed 7 years ago by Sergio Oliveira

Attachment: 11321-r11173.2.diff added

Changed 7 years ago by Sergio Oliveira

Attachment: 11321-r11173.3.diff added

Merging tests and patch

comment:3 Changed 6 years ago by Brandon M Height

Needs documentation: set
Needs tests: unset

comment:4 Changed 6 years ago by Gabriel Hurley

Component: Contrib appscontrib.humanize

comment:5 Changed 5 years ago by Julien Phalip

Severity: Normal
Type: Bug

comment:6 Changed 5 years ago by Jannis Leidel

Resolution: fixed
Status: newclosed

In [16072]:

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

comment:7 Changed 5 months ago by gabn88

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 Changed 5 months ago by Aymeric Augustin

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