Opened 6 years ago

Closed 4 years 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: UI/UX:

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

Download all attachments as: .zip

Change History (11)

Changed 6 years ago by aarond10

Patch to humanize.py

comment:1 Changed 6 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 6 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 6 years ago by seocam

Patch including some fixes (using svn diff)

Changed 6 years ago by seocam

Unit tests

Changed 6 years ago by seocam

Changed 6 years ago by seocam

Merging tests and patch

comment:3 Changed 4 years ago by lasko

  • Needs documentation set
  • Needs tests unset

comment:4 Changed 4 years ago by gabrielhurley

  • Component changed from Contrib apps to contrib.humanize

comment:5 Changed 4 years ago by julien

  • Severity set to Normal
  • Type set to Bug

comment:6 Changed 4 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.

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