Changeset 6366
- Timestamp:
- 09/16/07 23:50:12 (1 year ago)
- Files:
-
- django/trunk/django/utils/timesince.py (modified) (2 diffs)
- django/trunk/docs/templates.txt (modified) (2 diffs)
- django/trunk/tests/regressiontests/templates/tests.py (modified) (2 diffs)
- django/trunk/tests/regressiontests/utils/tests.py (modified) (2 diffs)
- django/trunk/tests/regressiontests/utils/timesince.py (added)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/trunk/django/utils/timesince.py
r5671 r6366 5 5 def timesince(d, now=None): 6 6 """ 7 Takes two datetime objects and returns the time between then and now 8 as a nicely formatted string, e.g "10 minutes" 7 Takes two datetime objects and returns the time between d and now 8 as a nicely formatted string, e.g. "10 minutes". If d occurs after now, 9 then "0 minutes" is returned. 10 11 Units used are years, months, weeks, days, hours, and minutes. 12 Seconds and microseconds are ignored. Up to two adjacent units will be 13 displayed. For example, "2 weeks, 3 days" and "1 year, 3 months" are 14 possible outputs, but "2 weeks, 3 hours" and "1 year, 5 days" are not. 15 9 16 Adapted from http://blog.natbat.co.uk/archive/2003/Jun/14/time_since 10 17 """ … … 33 40 delta = now - (d - datetime.timedelta(0, 0, d.microsecond)) 34 41 since = delta.days * 24 * 60 * 60 + delta.seconds 42 if since <= 0: 43 # d is in the future compared to now, stop processing. 44 return u'0 ' + ugettext('minutes') 35 45 for i, (seconds, name) in enumerate(chunks): 36 46 count = since // seconds django/trunk/docs/templates.txt
r6271 r6366 1276 1276 then ``{{ comment_date|timesince:blog_date }}`` would return "8 hours". 1277 1277 1278 Minutes is the smallest unit used, and "0 minutes" will be returned for any 1279 date that is in the future relative to the comparison point. 1280 1278 1281 timeuntil 1279 1282 ~~~~~~~~~ … … 1282 1285 given date or datetime. For example, if today is 1 June 2006 and 1283 1286 ``conference_date`` is a date instance holding 29 June 2006, then 1284 ``{{ conference_date|timeuntil }}`` will return " 28 days".1287 ``{{ conference_date|timeuntil }}`` will return "4 weeks". 1285 1288 1286 1289 Takes an optional argument that is a variable containing the date to use as 1287 1290 the comparison point (instead of *now*). If ``from_date`` contains 22 June 1288 2006, then ``{{ conference_date|timeuntil:from_date }}`` will return "7 days". 1291 2006, then ``{{ conference_date|timeuntil:from_date }}`` will return "1 week". 1292 1293 Minutes is the smallest unit used, and "0 minutes" will be returned for any 1294 date that is in the past relative to the comparison point. 1289 1295 1290 1296 title django/trunk/tests/regressiontests/templates/tests.py
r6153 r6366 772 772 'timesince06' : ('{{ a|timesince:b }}', {'a':NOW_tz + timedelta(hours=8), 'b':NOW_tz}, '8 hours'), 773 773 774 # Check times in the future. 775 'timesince07' : ('{{ a|timesince }}', {'a':datetime.now() + timedelta(minutes=1, seconds=10)}, '0 minutes'), 776 'timesince08' : ('{{ a|timesince }}', {'a':datetime.now() + timedelta(days=1, minutes=1)}, '0 minutes'), 777 774 778 ### TIMEUNTIL TAG ################################################## 775 779 # Default compare with datetime.now() … … 781 785 'timeuntil04' : ('{{ a|timeuntil:b }}', {'a':NOW - timedelta(days=1), 'b':NOW - timedelta(days=2)}, '1 day'), 782 786 'timeuntil05' : ('{{ a|timeuntil:b }}', {'a':NOW - timedelta(days=2), 'b':NOW - timedelta(days=2, minutes=1)}, '1 minute'), 787 788 # Check times in the past. 789 'timeuntil07' : ('{{ a|timeuntil }}', {'a':datetime.now() - timedelta(minutes=1, seconds=10)}, '0 minutes'), 790 'timeuntil08' : ('{{ a|timeuntil }}', {'a':datetime.now() - timedelta(days=1, minutes=1)}, '0 minutes'), 783 791 784 792 ### URL TAG ######################################################## django/trunk/tests/regressiontests/utils/tests.py
r5876 r6366 6 6 7 7 from django.utils import html 8 9 from timesince import timesince_tests 8 10 9 11 class TestUtilsHtml(TestCase): … … 114 116 for value, output in items: 115 117 self.check_output(f, value, output) 118 119 __test__ = { 120 'timesince_tests': timesince_tests, 121 } 122 123 if __name__ == "__main__": 124 import doctest 125 doctest.testmod()
