Django

Code

Changeset 3185

Show
Ignore:
Timestamp:
06/21/06 01:56:08 (2 years ago)
Author:
mtredinnick
Message:

Fixed #2053 -- added an optional comparison argument to the "timesince" filter.
Added a "timeuntil" filter that works analogously. Thanks, john@sneeu.com.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/template/defaultfilters.py

    r3117 r3185  
    346346    return time_format(value, arg) 
    347347 
    348 def timesince(value): 
     348def timesince(value, arg=None): 
    349349    'Formats a date as the time since that date (i.e. "4 days, 6 hours")' 
    350350    from django.utils.timesince import timesince 
    351351    if not value: 
    352352        return '' 
     353    if arg: 
     354        return timesince(arg, value) 
    353355    return timesince(value) 
     356 
     357def 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) 
    354366 
    355367################### 
     
    486498register.filter(time) 
    487499register.filter(timesince) 
     500register.filter(timeuntil) 
    488501register.filter(title) 
    489502register.filter(truncatewords) 
  • django/trunk/django/utils/timesince.py

    r2809 r3185  
    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/trunk/docs/templates.txt

    r3138 r3185  
    10231023Formats a date as the time since that date (i.e. "4 days, 6 hours"). 
    10241024 
     1025Takes an optional argument that is a variable containing the date to use as 
     1026the comparison point (without the argument, the comparison point is *now*). 
     1027For example, if ``blog_date`` is a date instance representing midnight on 1 
     1028June 2006, and ``comment_date`` is a date instanace for 08:00 on 1 June 2006, 
     1029then ``{{ comment_date|timesince:blog_date }}`` would return "8 hours". 
     1030 
     1031timeuntil 
     1032~~~~~~~~~ 
     1033 
     1034Similar to ``timesince``, except that it measures the time from now until the 
     1035given 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 
     1039Takes an optional argument that is a variable containing the date to use as 
     1040the comparison point (instead of *now*). If ``from_date`` contains 22 June 
     10412006, then ``{{ conference_date|timeuntil:from_date }}`` will return "7 days". 
     1042 
    10251043title 
    10261044~~~~~ 
  • django/trunk/tests/othertests/templates.py

    r3138 r3185  
    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 
     
    5757    def method(self): 
    5858        return "OtherClass.method" 
     59 
     60# NOW used by timesince tag tests. 
     61NOW = datetime.now() 
    5962 
    6063# SYNTAX -- 
     
    531534#    'now03' : ('{% now "j \"n\" Y"%}', {}, str(datetime.now().day) + '"' + str(datetime.now().month) + '"' + str(datetime.now().year)), 
    532535#    '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'), 
    533557} 
    534558