Ticket #3311: naturalday_unicode.patch
File naturalday_unicode.patch, 4.1 KB (added by , 17 years ago) |
---|
-
django/contrib/humanize/templatetags/humanize.py
1 1 from django.utils.translation import ungettext, ugettext as _ 2 2 from django.utils.encoding import force_unicode 3 3 from django import template 4 from django.template import defaultfilters 5 from django.conf import settings 6 from datetime import date, timedelta 4 7 import re 5 8 6 9 register = template.Library() … … 67 70 return value 68 71 return (_('one'), _('two'), _('three'), _('four'), _('five'), _('six'), _('seven'), _('eight'), _('nine'))[value-1] 69 72 register.filter(apnumber) 73 74 def naturalday(value, arg=None): 75 """ 76 For date values that are tomorrow, today or yesterday compared to 77 present day returns representing string. Otherwise, returns a string 78 formatted according to settings.DATE_FORMAT. 79 """ 80 try: 81 value = date(value.year, value.month, value.day) 82 except AttributeError: 83 # Passed value wasn't a date object 84 return value 85 except ValueError: 86 # Date arguments out of range 87 return value 88 delta = value - date.today() 89 if delta.days == 0: 90 return _(u'today') 91 elif delta.days == 1: 92 return _(u'tomorrow') 93 elif delta.days == -1: 94 return _(u'yesterday') 95 return defaultfilters.date(value, arg) 96 register.filter(naturalday) -
tests/regressiontests/humanize/tests.py
1 1 import unittest 2 from datetime import timedelta, date 2 3 from django.template import Template, Context, add_to_builtins 4 from django.utils.dateformat import DateFormat 5 from django.utils.translation import ugettext as _ 3 6 4 7 add_to_builtins('django.contrib.humanize.templatetags.humanize') 5 8 … … 8 11 def humanize_tester(self, test_list, result_list, method): 9 12 # Using max below ensures we go through both lists 10 13 # However, if the lists are not equal length, this raises an exception 11 for index in xrange( len(max(test_list,result_list))):14 for index in xrange(max(len(test_list), len(result_list))): 12 15 test_content = test_list[index] 13 16 t = Template('{{ test_content|%s }}' % method) 14 17 rendered = t.render(Context(locals())).strip() … … 49 52 50 53 self.humanize_tester(test_list, result_list, 'apnumber') 51 54 55 def test_naturalday(self): 56 from django.template import defaultfilters 57 today = date.today() 58 yesterday = today - timedelta(days=1) 59 tomorrow = today + timedelta(days=1) 60 someday = today - timedelta(days=10) 61 notdate = u"I'm not a date value" 62 63 test_list = (today, yesterday, tomorrow, someday, notdate) 64 someday_result = defaultfilters.date(someday) 65 result_list = (_(u'today'), _(u'yesterday'), _(u'tomorrow'), 66 someday_result, u"I'm not a date value") 67 self.humanize_tester(test_list, result_list, 'naturalday') 68 52 69 if __name__ == '__main__': 53 70 unittest.main() 54 71 -
docs/add_ons.txt
138 138 139 139 You can pass in either an integer or a string representation of an integer. 140 140 141 naturalday 142 ---------- 143 144 Converts a date to natural string, if it's today, tomorrow or yesterday 145 146 **Argument:** Date formatting string as described in default tag now_. 147 148 .. _now: ../templates/#now 149 150 Examples (when 'today' is 17 Feb 2007): 151 152 * ``16 Feb 2007`` becomes ``yesterday``. 153 * ``17 Feb 2007`` becomes ``today``. 154 * ``18 Feb 2007`` becomes ``tomorrow``. 155 * Any other day is formatted according to given argument or `DATE_FORMAT`_ if 156 argument is not given. 157 158 .. _DATE_FORMAT: ../settings/#date_format 159 141 160 flatpages 142 161 ========= 143 162