Django

Code

Changeset 6366

Show
Ignore:
Timestamp:
09/16/07 23:50:12 (1 year ago)
Author:
gwilson
Message:

Fixed #2675 -- Changed the timeuntil and timesince template filters to display "0 minutes" when passed a past or future date respectively instead of "-1 years, 12 months". Thanks to nickefford for the patch.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/utils/timesince.py

    r5671 r6366  
    55def timesince(d, now=None): 
    66    """ 
    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 
    916    Adapted from http://blog.natbat.co.uk/archive/2003/Jun/14/time_since 
    1017    """ 
     
    3340    delta = now - (d - datetime.timedelta(0, 0, d.microsecond)) 
    3441    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') 
    3545    for i, (seconds, name) in enumerate(chunks): 
    3646        count = since // seconds 
  • django/trunk/docs/templates.txt

    r6271 r6366  
    12761276then ``{{ comment_date|timesince:blog_date }}`` would return "8 hours". 
    12771277 
     1278Minutes is the smallest unit used, and "0 minutes" will be returned for any 
     1279date that is in the future relative to the comparison point. 
     1280 
    12781281timeuntil 
    12791282~~~~~~~~~ 
     
    12821285given date or datetime. For example, if today is 1 June 2006 and 
    12831286``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". 
    12851288 
    12861289Takes an optional argument that is a variable containing the date to use as 
    12871290the comparison point (instead of *now*). If ``from_date`` contains 22 June 
    1288 2006, then ``{{ conference_date|timeuntil:from_date }}`` will return "7 days". 
     12912006, then ``{{ conference_date|timeuntil:from_date }}`` will return "1 week". 
     1292 
     1293Minutes is the smallest unit used, and "0 minutes" will be returned for any 
     1294date that is in the past relative to the comparison point. 
    12891295 
    12901296title 
  • django/trunk/tests/regressiontests/templates/tests.py

    r6153 r6366  
    772772            'timesince06' : ('{{ a|timesince:b }}', {'a':NOW_tz + timedelta(hours=8), 'b':NOW_tz}, '8 hours'), 
    773773 
     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 
    774778            ### TIMEUNTIL TAG ################################################## 
    775779            # Default compare with datetime.now() 
     
    781785            'timeuntil04' : ('{{ a|timeuntil:b }}', {'a':NOW - timedelta(days=1), 'b':NOW - timedelta(days=2)}, '1 day'), 
    782786            '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'), 
    783791 
    784792            ### URL TAG ######################################################## 
  • django/trunk/tests/regressiontests/utils/tests.py

    r5876 r6366  
    66 
    77from django.utils import html 
     8 
     9from timesince import timesince_tests 
    810 
    911class TestUtilsHtml(TestCase): 
     
    114116        for value, output in items: 
    115117            self.check_output(f, value, output) 
     118 
     119__test__ = { 
     120    'timesince_tests': timesince_tests, 
     121} 
     122 
     123if __name__ == "__main__": 
     124    import doctest 
     125    doctest.testmod()