Ticket #2053: timesince.diff
File timesince.diff, 4.0 KB (added by , 18 years ago) |
---|
-
django/utils/timesince.py
47 47 s += ', %d %s' % (count2, name2(count2)) 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/template/defaultfilters.py
341 341 arg = settings.TIME_FORMAT 342 342 return time_format(value, arg) 343 343 344 def timesince(value ):344 def timesince(value, arg=None): 345 345 'Formats a date as the time since that date (i.e. "4 days, 6 hours")' 346 346 from django.utils.timesince import timesince 347 return timesince(value) 347 if arg: 348 return timesince(arg, value) 349 else: 350 return timesince(value) 348 351 352 def timeuntil(value, arg=None): 353 'Formats a date as the time until that date (i.e. "4 days, 6 hours")' 354 from django.utils.timesince import timesince 355 from datetime import datetime 356 if arg: 357 return timesince(value, arg) 358 else: 359 return timesince(value, datetime.now()) 360 361 349 362 ################### 350 363 # LOGIC # 351 364 ################### … … 479 492 register.filter(striptags) 480 493 register.filter(time) 481 494 register.filter(timesince) 495 register.filter(timeuntil) 482 496 register.filter(title) 483 497 register.filter(truncatewords) 484 498 register.filter(unordered_list) -
tests/othertests/templates.py
4 4 from django import template 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 10 10 ################################# … … 57 57 def method(self): 58 58 return "OtherClass.method" 59 59 60 # NOW used by timesince tag tests. 61 NOW = datetime.now() 62 60 63 # SYNTAX -- 61 64 # 'template_name': ('template contents', 'context dict', 'expected string output' or Exception class) 62 65 TEMPLATE_TESTS = { … … 449 452 'now02' : ('{% now "j "n" Y"%}', {}, template.TemplateSyntaxError), 450 453 # 'now03' : ('{% now "j \"n\" Y"%}', {}, str(datetime.now().day) + '"' + str(datetime.now().month) + '"' + str(datetime.now().year)), 451 454 # 'now04' : ('{% now "j \nn\n Y"%}', {}, str(datetime.now().day) + '\n' + str(datetime.now().month) + '\n' + str(datetime.now().year)) 455 456 ### TIMESINCE TAG ################################################## 457 # Default compare with datetime.now() 458 'timesince01' : ('{{ a|timesince }}', {'a':datetime.now()}, '0 minutes'), 459 'timesince02' : ('{{ a|timesince }}', {'a':(datetime.now() - timedelta(days=1))}, '1 day'), 460 461 # Compare to a given parameter 462 'timesince03' : ('{{ a|timesince:b }}', {'a':NOW + timedelta(days=2), 'b':NOW + timedelta(days=1)}, '1 day'), 463 'timesince04' : ('{{ a|timesince:b }}', {'a':NOW + timedelta(days=2), 'b':NOW + timedelta(days=2)}, '0 minutes'), 464 465 ### TIMEUNTIL TAG ################################################## 466 # Default compare with datetime.now() 467 'timeuntil01' : ('{{ a|timeuntil }}', {'a':datetime.now()}, '0 minutes'), 468 'timeuntil02' : ('{{ a|timeuntil }}', {'a':(datetime.now() - timedelta(days=1))}, '1 day'), 469 470 # Compare to a given parameter 471 'timeuntil03' : ('{{ a|timeuntil:b }}', {'a':NOW - timedelta(days=2), 'b':NOW - timedelta(days=1)}, '1 day'), 472 'timeuntil04' : ('{{ a|timeuntil:b }}', {'a':NOW - timedelta(days=2), 'b':NOW - timedelta(days=2)}, '0 minutes'), 452 473 } 453 474 454 475 def test_template_loader(template_name, template_dirs=None):