#23340: naturaltime documentation doesn't match behavior
-------------------------------+--------------------
Reporter: zachborboa | Owner: nobody
Type: Uncategorized | Status: new
Component: Uncategorized | Version: master
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------
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
--------------------------------------------------------------------------------
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/23340>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/053.cd05f44c337cf48329cb717cb384a770%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.