Changeset 3185
- Timestamp:
- 06/21/06 01:56:08 (2 years ago)
- Files:
-
- django/trunk/django/template/defaultfilters.py (modified) (2 diffs)
- django/trunk/django/utils/timesince.py (modified) (1 diff)
- django/trunk/docs/templates.txt (modified) (1 diff)
- django/trunk/tests/othertests/templates.py (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/trunk/django/template/defaultfilters.py
r3117 r3185 346 346 return time_format(value, arg) 347 347 348 def timesince(value ):348 def timesince(value, arg=None): 349 349 'Formats a date as the time since that date (i.e. "4 days, 6 hours")' 350 350 from django.utils.timesince import timesince 351 351 if not value: 352 352 return '' 353 if arg: 354 return timesince(arg, value) 353 355 return timesince(value) 356 357 def timeuntil(value, arg=None): 358 'Formats a date as the time until that date (i.e. "4 days, 6 hours")' 359 from django.utils.timesince import timesince 360 from datetime import datetime 361 if not value: 362 return '' 363 if arg: 364 return timesince(arg, value) 365 return timesince(datetime.now(), value) 354 366 355 367 ################### … … 486 498 register.filter(time) 487 499 register.filter(timesince) 500 register.filter(timeuntil) 488 501 register.filter(title) 489 502 register.filter(truncatewords) django/trunk/django/utils/timesince.py
r2809 r3185 48 48 return s 49 49 50 def timeuntil(d ):50 def timeuntil(d, now=None): 51 51 """ 52 52 Like timesince, but returns a string measuring the time until 53 53 the given time. 54 54 """ 55 now = datetime.datetime.now() 55 if now == None: 56 now = datetime.datetime.now() 56 57 return timesince(now, d) django/trunk/docs/templates.txt
r3138 r3185 1023 1023 Formats a date as the time since that date (i.e. "4 days, 6 hours"). 1024 1024 1025 Takes an optional argument that is a variable containing the date to use as 1026 the comparison point (without the argument, the comparison point is *now*). 1027 For example, if ``blog_date`` is a date instance representing midnight on 1 1028 June 2006, and ``comment_date`` is a date instanace for 08:00 on 1 June 2006, 1029 then ``{{ comment_date|timesince:blog_date }}`` would return "8 hours". 1030 1031 timeuntil 1032 ~~~~~~~~~ 1033 1034 Similar to ``timesince``, except that it measures the time from now until the 1035 given date or datetime. For example, if today is 1 June 2006 and 1036 ``conference_date`` is a date instance holding 29 June 2006, then 1037 ``{{ conference_date|timeuntil }}`` will return "28 days". 1038 1039 Takes an optional argument that is a variable containing the date to use as 1040 the comparison point (instead of *now*). If ``from_date`` contains 22 June 1041 2006, then ``{{ conference_date|timeuntil:from_date }}`` will return "7 days". 1042 1025 1043 title 1026 1044 ~~~~~ django/trunk/tests/othertests/templates.py
r3138 r3185 5 5 from django.template import loader 6 6 from django.utils.translation import activate, deactivate, install 7 from datetime import datetime 7 from datetime import datetime, timedelta 8 8 import traceback 9 9 … … 57 57 def method(self): 58 58 return "OtherClass.method" 59 60 # NOW used by timesince tag tests. 61 NOW = datetime.now() 59 62 60 63 # SYNTAX -- … … 531 534 # 'now03' : ('{% now "j \"n\" Y"%}', {}, str(datetime.now().day) + '"' + str(datetime.now().month) + '"' + str(datetime.now().year)), 532 535 # 'now04' : ('{% now "j \nn\n Y"%}', {}, str(datetime.now().day) + '\n' + str(datetime.now().month) + '\n' + str(datetime.now().year)) 536 537 ### TIMESINCE TAG ################################################## 538 # Default compare with datetime.now() 539 'timesince01' : ('{{ a|timesince }}', {'a':datetime.now()}, '0 minutes'), 540 'timesince02' : ('{{ a|timesince }}', {'a':(datetime.now() - timedelta(days=1))}, '1 day'), 541 'timesince03' : ('{{ a|timesince }}', {'a':(datetime.now() - 542 timedelta(hours=1, minutes=25))}, '1 hour, 25 minutes'), 543 544 # Compare to a given parameter 545 'timesince04' : ('{{ a|timesince:b }}', {'a':NOW + timedelta(days=2), 'b':NOW + timedelta(days=1)}, '1 day'), 546 'timesince05' : ('{{ a|timesince:b }}', {'a':NOW + timedelta(days=2), 'b':NOW + timedelta(days=2)}, '0 minutes'), 547 548 ### TIMEUNTIL TAG ################################################## 549 # Default compare with datetime.now() 550 'timeuntil01' : ('{{ a|timeuntil }}', {'a':datetime.now()}, '0 minutes'), 551 'timeuntil02' : ('{{ a|timeuntil }}', {'a':(datetime.now() + timedelta(days=1))}, '1 day'), 552 'timeuntil03' : ('{{ a|timeuntil }}', {'a':(datetime.now() + timedelta(hours=8, minutes=10))}, '8 hours, 10 minutes'), 553 554 # Compare to a given parameter 555 'timeuntil04' : ('{{ a|timeuntil:b }}', {'a':NOW - timedelta(days=1), 'b':NOW - timedelta(days=2)}, '1 day'), 556 'timeuntil05' : ('{{ a|timeuntil:b }}', {'a':NOW - timedelta(days=2), 'b':NOW - timedelta(days=2)}, '0 minutes'), 533 557 } 534 558
