#23340 closed Bug (fixed)
naturaltime documentation doesn't match behavior
Reported by: | Zach Borboa | Owned by: | blackguest |
---|---|---|---|
Component: | contrib.humanize | Version: | dev |
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)
Change History (16)
comment:1 by , 10 years ago
Component: | Uncategorized → contrib.humanize |
---|---|
Triage Stage: | Unreviewed → Accepted |
Type: | Uncategorized → Bug |
comment:2 by , 10 years ago
The documentation bug has been introduced here:
https://github.com/django/django/commit/578a31fea3cb439f6bfeeb7825ac835820a9f58c#diff-18a5dbaf4a414f662e7b768de91c9e30R118
when solving #15921
comment:4 by , 10 years ago
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 by , 10 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
comment:6 by , 10 years ago
Has patch: | set |
---|
comment:7 by , 10 years ago
converted patch into a pull request https://github.com/django/django/pull/3175
comment:8 by , 10 years ago
Resolution: | → fixed |
---|---|
Status: | assigned → closed |
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.