Opened 11 months ago

Closed 11 months ago

Last modified 11 months ago

#23340 closed Bug (fixed)

naturaltime documentation doesn't match behavior

Reported by: zachborboa Owned by: blackguest
Component: contrib.humanize Version: master
Severity: Normal Keywords: afraid-to-commit
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description

The documentation for django.contrib.humanize.templatetags.humanize.naturaltime doesn't match it's behavior.

Test:

from collections import OrderedDict
from datetime import datetime

from django.contrib.humanize.templatetags.humanize import naturaltime


# From the documentation.
times = OrderedDict()
times['17 Feb 2007 16:30:00'] = 'now'
times['17 Feb 2007 16:29:31'] = '29 seconds ago'
times['17 Feb 2007 16:29:00'] = 'a minute ago'
times['17 Feb 2007 16:25:35'] = '4 minutes ago'
times['17 Feb 2007 15:30:29'] = 'an hour ago'
times['17 Feb 2007 13:31:29'] = '2 hours ago'
times['16 Feb 2007 13:31:29'] = '1 day, 3 hours ago'
times['17 Feb 2007 16:30:30'] = '29 seconds from now'
times['17 Feb 2007 16:31:00'] = 'a minute from now'
times['17 Feb 2007 16:34:35'] = '4 minutes from now'
times['17 Feb 2007 16:30:29'] = 'an hour from now'
times['17 Feb 2007 18:31:29'] = '2 hours from now'
times['18 Feb 2007 16:31:29'] = '1 day from now'
times['26 Feb 2007 18:31:29'] = '1 week, 2 days from now'

time_format = '%d %b %Y %H:%M:%S'
now = datetime.strptime('17 Feb 2007 16:30:00', time_format)
for time, expected_time in times.iteritems():
    time = datetime.strptime(time, time_format)
    got = naturaltime(time).replace(u'\xa0', u' ')
    print '   {0}'.format(now)
    print '   {0}'.format(time)
    if got != expected_time:
        print '-- {0}'.format(got)
        print '++ {0}'.format(expected_time)
    else:
        print 'OK'
    print '-' * 80

Modified "now".

diff --git a/django/contrib/humanize/templatetags/humanize.py b/django/contrib/humanize/templatetags/humanize.py
index 993b728..7e28b04 100644
--- a/django/contrib/humanize/templatetags/humanize.py
+++ b/django/contrib/humanize/templatetags/humanize.py
@@ -193,7 +193,7 @@ def naturaltime(value):
     if not isinstance(value, date):  # datetime is a subclass of date
         return value
 
-    now = datetime.now(utc if is_aware(value) else None)
+    now = datetime.strptime('17 Feb 2007 16:30:00', '%d %b %Y %H:%M:%S') # TESTING
     if value < now:
         delta = now - value
         if delta.days != 0:

Output:

   2007-02-17 16:30:00
   2007-02-17 16:30:00
OK
--------------------------------------------------------------------------------
   2007-02-17 16:30:00
   2007-02-17 16:29:31
OK
--------------------------------------------------------------------------------
   2007-02-17 16:30:00
   2007-02-17 16:29:00
OK
--------------------------------------------------------------------------------
   2007-02-17 16:30:00
   2007-02-17 16:25:35
OK
--------------------------------------------------------------------------------
   2007-02-17 16:30:00
   2007-02-17 15:30:29
-- 59 minutes ago
++ an hour ago
--------------------------------------------------------------------------------
   2007-02-17 16:30:00
   2007-02-17 13:31:29
OK
--------------------------------------------------------------------------------
   2007-02-17 16:30:00
   2007-02-16 13:31:29
-- 1 day, 2 hours ago
++ 1 day, 3 hours ago
--------------------------------------------------------------------------------
   2007-02-17 16:30:00
   2007-02-17 16:30:30
-- 30 seconds from now
++ 29 seconds from now
--------------------------------------------------------------------------------
   2007-02-17 16:30:00
   2007-02-17 16:31:00
OK
--------------------------------------------------------------------------------
   2007-02-17 16:30:00
   2007-02-17 16:34:35
OK
--------------------------------------------------------------------------------
   2007-02-17 16:30:00
   2007-02-17 16:30:29
-- 29 seconds from now
++ an hour from now
--------------------------------------------------------------------------------
   2007-02-17 16:30:00
   2007-02-17 18:31:29
OK
--------------------------------------------------------------------------------
   2007-02-17 16:30:00
   2007-02-18 16:31:29
OK
--------------------------------------------------------------------------------
   2007-02-17 16:30:00
   2007-02-26 18:31:29
OK
--------------------------------------------------------------------------------

Attachments (1)

doc_fix.diff (5.6 KB) - added by blackguest 11 months ago.
patched diff of document fix and tests.

Download all attachments as: .zip

Change History (16)

comment:1 Changed 11 months ago by bmispelon

  • Component changed from Uncategorized to contrib.humanize
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted
  • Type changed from Uncategorized to Bug

Hi,

Indeed, there seem to be an inconsistency here.
It'd be interesting to see if the (documentation) bug has always been there or if something was broken inadvertently at some point.

Thanks.

comment:3 Changed 11 months ago by jaap3

Seems related to #16941

comment:4 Changed 11 months ago by evildmp

  • Easy pickings set
  • Keywords afraid-to-commit added

I've marked this ticket as especially suitable for people following the ​Don't be afraid to commit tutorial at the DjangoCon US 2014 sprints. If you're tackling this ticket, please don't hesitate to ask me for guidance if you'd like any, either at the sprints themselves, or here or on the Django IRC channels, where I can be found as EvilDMP.

comment:5 Changed 11 months ago by blackguest

  • Owner changed from nobody to blackguest
  • Status changed from new to assigned

Changed 11 months ago by blackguest

patched diff of document fix and tests.

comment:6 Changed 11 months ago by blackguest

  • Has patch set

comment:7 Changed 11 months ago by collinanderson

converted patch into a pull request https://github.com/django/django/pull/3175

comment:8 Changed 11 months ago by Tim Graham <timograham@…>

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

In 7353e26d5d41563ca51520fb38b5950530cb6b9c:

Fixed #23340 -- Corrected naturaltime docs to match behavior.

Thanks zachborboa for the report and blackguest for the patch.

comment:9 Changed 11 months ago by Tim Graham <timograham@…>

In 0a2bc6e6bd29ccfbed86c58835c68c349c5a6757:

Additional edits for refs #23340; thanks Loic.

comment:10 Changed 11 months ago by Tim Graham <timograham@…>

In c9e0afe2d3fa51b1b76ff5cbd53f95cf19f984f1:

[1.6.x] Fixed #23340 -- Corrected naturaltime docs to match behavior.

Thanks zachborboa for the report and blackguest for the patch.

Backport of 7353e26d5d from master

comment:11 Changed 11 months ago by Tim Graham <timograham@…>

In 8635fa0b118fcf7e7a0234fb2edb57715d6f6a61:

[1.6.x] Additional edits for refs #23340; thanks Loic.

Backport of 0a2bc6e6bd from master

comment:12 Changed 11 months ago by Tim Graham <timograham@…>

In d19291e9ad58107825581bd2297398cf8d824fba:

[1.7.x] Fixed #23340 -- Corrected naturaltime docs to match behavior.

Thanks zachborboa for the report and blackguest for the patch.

Backport of 7353e26d5d and 0a2bc6e6bd from master

comment:13 Changed 11 months ago by Tim Graham <timograham@…>

In 96010ae15bc2ce6b6a73a224da04fde94242a10d:

Corrected indentation in a contrib.humanize test; refs #23340.

Thanks Zach Borboa for the report.

comment:14 Changed 11 months ago by Tim Graham <timograham@…>

In ac54e6af95a2d38734aa17daad5eaf0ee453acf9:

[1.7.x] Corrected indentation in a contrib.humanize test; refs #23340.

Thanks Zach Borboa for the report.

Backport of 96010ae15b from master

comment:15 Changed 11 months ago by Tim Graham <timograham@…>

In cc8486b6fe7247ce1bf06f661248191483f7296c:

[1.6.x] Corrected indentation in a contrib.humanize test; refs #23340.

Thanks Zach Borboa for the report.

Backport of 96010ae15b from master

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