Ticket #12771: patch_with_docs.diff
File patch_with_docs.diff, 5.3 KB (added by , 14 years ago) |
---|
-
django/contrib/humanize/templatetags/humanize.py
diff --git a/django/contrib/humanize/templatetags/humanize.py b/django/contrib/humanize/templatetags/humanize.py index cfa93fe..6a3ecd5 100644
a b from django.utils.translation import ungettext, ugettext as _ 2 2 from django.utils.encoding import force_unicode 3 3 from django import template 4 4 from django.template import defaultfilters 5 from datetime import date 5 from datetime import date, datetime 6 6 import re 7 7 8 8 register = template.Library() … … def naturalday(value, arg=None): 100 100 return _(u'yesterday') 101 101 return defaultfilters.date(value, arg) 102 102 register.filter(naturalday) 103 104 def naturaltime(value, arg=None): 105 """ 106 For date and time values shows how many seconds, minutes or hours ago compared to 107 current timestamp returns representing string. Otherwise, returns a string 108 formatted according to settings.DATE_FORMAT 109 """ 110 try: 111 value = datetime(value.year, value.month, value.day, value.hour, value.minute, value.second) 112 except AttributeError: 113 return value 114 except ValueError: 115 return value 116 117 delta = datetime.now() - value 118 if delta.days != 0: 119 value = date(value.year, value.month, value.day) 120 return naturalday(value, arg) 121 elif delta.seconds == 0: 122 return _(u'now') 123 elif delta.seconds < 60: 124 return _(u"%s seconds ago" % (delta.seconds)) 125 elif delta.seconds / 60 < 2: 126 return _(r'a minute ago') 127 elif delta.seconds / 60 < 60: 128 return _(u"%s minutes ago" % (delta.seconds/60)) 129 elif delta.seconds / 60 / 60 < 2: 130 return _(u'an hour ago') 131 elif delta.seconds / 60 / 60 < 24: 132 return _(u"%s hours ago" % (delta.seconds/60/60)) 133 return naturalday(value, arg) 134 register.filter(naturaltime) -
docs/ref/contrib/humanize.txt
diff --git a/docs/ref/contrib/humanize.txt b/docs/ref/contrib/humanize.txt index ef48f48..a9a2c17 100644
a b Examples (when 'today' is 17 Feb 2007): 82 82 * Any other day is formatted according to given argument or the 83 83 :setting:`DATE_FORMAT` setting if no argument is given. 84 84 85 .. templatefilter:: naturaltime 86 87 naturaltime 88 ---------- 89 90 For date and time values shows how many seconds, minutes or hours ago compared 91 to current timestamp returns representing string. Otherwise, it behaves like 92 :tfilter:`naturaldate`, so it can also take string argument for date formating. 93 94 **Argument:** Date formatting string as described in the :tfilter:`date` tag. 95 96 Examples (when 'now' is 17 Feb 2007 16:30:00): 97 98 * ``17 Feb 2007 16:30:00`` becomes ``now``. 99 * ``17 Feb 2007 16:29:31`` becomes ``29 seconds ago``. 100 * ``17 Feb 2007 16:29:00`` becomes ``a minute ago``. 101 * ``17 Feb 2007 16:25:35`` becomes ``4 minutes ago``. 102 * ``17 Feb 2007 15:30:29`` becomes ``an hours ago``. 103 * ``17 Feb 2007 13:31:29`` becomes ``2 hours ago``. 104 * ``16 Feb 2007 13:31:29`` becomes ``yesterday``. 105 * Any other day is formatted according to given argument or the 106 :setting:`DATE_FORMAT` setting if no argument is given. 107 85 108 .. templatefilter:: ordinal 86 109 87 110 ordinal -
tests/regressiontests/humanize/tests.py
diff --git a/tests/regressiontests/humanize/tests.py b/tests/regressiontests/humanize/tests.py index 7eeaaee..c53060b 100644
a b 1 from datetime import timedelta, date 1 from datetime import timedelta, date, datetime 2 2 3 3 from django.template import Template, Context, add_to_builtins 4 4 from django.utils import unittest … … class HumanizeTests(unittest.TestCase): 60 60 61 61 def test_naturalday(self): 62 62 from django.template import defaultfilters 63 today = date .today()63 today = datetime.now() 64 64 yesterday = today - timedelta(days=1) 65 65 tomorrow = today + timedelta(days=1) 66 66 someday = today - timedelta(days=10) … … class HumanizeTests(unittest.TestCase): 72 72 someday_result, u"I'm not a date value", None) 73 73 self.humanize_tester(test_list, result_list, 'naturalday') 74 74 75 def test_naturaltime(self): 76 from django.template import defaultfilters 77 now = datetime.now() 78 seconds_ago = now - timedelta(seconds=30) 79 a_minute_ago = now - timedelta(minutes=1, seconds=30) 80 minutes_ago = now - timedelta(minutes=2) 81 an_hour_ago = now - timedelta(hours=1, minutes=30, seconds=30) 82 hours_ago = now - timedelta(hours=23, minutes=50, seconds=50) 83 84 test_list = (now, a_minute_ago, an_hour_ago) 85 result_list = (_(u'now'), _(u'a minute ago'), _(u'an hour ago')) 86 self.humanize_tester(test_list, result_list, 'naturaltime') 87 88 t = Template('{{ seconds_ago|%s }}' % 'naturaltime') 89 rendered = t.render(Context(locals())).strip() 90 self.assertTrue(u' seconds ago' in rendered) 91 92 t = Template('{{ minutes_ago|%s }}' % 'naturaltime') 93 rendered = t.render(Context(locals())).strip() 94 self.assertTrue(u' minutes ago' in rendered) 95 96 t = Template('{{ hours_ago|%s }}' % 'naturaltime') 97 rendered = t.render(Context(locals())).strip() 98 self.assertTrue(u' hours ago' in rendered) 99 100 75 101 if __name__ == '__main__': 76 102 unittest.main() 77 103