Ticket #2053: timesince.diff

File timesince.diff, 4.0 KB (added by john@…, 9 years ago)

[patch] for this ticket.

  • django/utils/timesince.py

     
    4747            s += ', %d %s' % (count2, name2(count2))
    4848    return s
    4949
    50 def timeuntil(d):
     50def timeuntil(d, now=None):
    5151    """
    5252    Like timesince, but returns a string measuring the time until
    5353    the given time.
    5454    """
    55     now = datetime.datetime.now()
     55    if now == None:
     56        now = datetime.datetime.now()
    5657    return timesince(now, d)
  • django/template/defaultfilters.py

     
    341341        arg = settings.TIME_FORMAT
    342342    return time_format(value, arg)
    343343
    344 def timesince(value):
     344def timesince(value, arg=None):
    345345    'Formats a date as the time since that date (i.e. "4 days, 6 hours")'
    346346    from django.utils.timesince import timesince
    347     return timesince(value)
     347    if arg:
     348        return timesince(arg, value)
     349    else:
     350        return timesince(value)
    348351
     352def 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
    349362###################
    350363# LOGIC           #
    351364###################
     
    479492register.filter(striptags)
    480493register.filter(time)
    481494register.filter(timesince)
     495register.filter(timeuntil)
    482496register.filter(title)
    483497register.filter(truncatewords)
    484498register.filter(unordered_list)
  • tests/othertests/templates.py

     
    44from django import template
    55from django.template import loader
    66from django.utils.translation import activate, deactivate, install
    7 from datetime import datetime
     7from datetime import datetime, timedelta
    88import traceback
    99
    1010#################################
     
    5757    def method(self):
    5858        return "OtherClass.method"
    5959
     60# NOW used by timesince tag tests.
     61NOW = datetime.now()
     62
    6063# SYNTAX --
    6164# 'template_name': ('template contents', 'context dict', 'expected string output' or Exception class)
    6265TEMPLATE_TESTS = {
     
    449452    'now02' : ('{% now "j "n" Y"%}', {}, template.TemplateSyntaxError),
    450453#    'now03' : ('{% now "j \"n\" Y"%}', {}, str(datetime.now().day) + '"' + str(datetime.now().month) + '"' + str(datetime.now().year)),
    451454#    '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'),
    452473}
    453474
    454475def test_template_loader(template_name, template_dirs=None):
Back to Top