Opened 7 years ago

Closed 4 weeks 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 seocam 7 years ago.
Patch including some fixes (using svn diff)
11321-r11173-test.diff (2.2 KB) - added by seocam 7 years ago.
Unit tests
11321-r11173.2.diff (3.4 KB) - added by seocam 7 years ago.
11321-r11173.3.diff (3.4 KB) - added by seocam 7 years ago.
Merging tests and patch

Download all attachments as: .zip

Change History (13)

Changed 7 years ago by aarond10

Patch to humanize.py

comment:1 Changed 7 years ago by russellm

  • milestone 1.1 deleted
  • Needs documentation unset
  • Needs tests set
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted

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 seocam

Patch including some fixes (using svn diff)

Changed 7 years ago by seocam

Unit tests

Changed 7 years ago by seocam

Changed 7 years ago by seocam

Merging tests and patch

comment:3 Changed 6 years ago by lasko

  • Needs documentation set
  • Needs tests unset

comment:4 Changed 5 years ago by gabrielhurley

  • Component changed from Contrib apps to contrib.humanize

comment:5 Changed 5 years ago by julien

  • Severity set to Normal
  • Type set to Bug

comment:6 Changed 5 years ago by jezdez

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

In [16072]:

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

comment:7 Changed 4 weeks ago by gabn88

  • Easy pickings set
  • Resolution fixed deleted
  • Status changed from closed to new
  • 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 4 weeks ago by aaugustin

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

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