Ticket #2675: timesince.diff

File timesince.diff, 2.4 KB (added by Nick Efford, 17 years ago)

Patch for django.utils.timesince and template docs

  • django/utils/timesince.py

     
    3232    # ignore microsecond part of 'd' since we removed it from 'now'
    3333    delta = now - (d - datetime.timedelta(0, 0, d.microsecond))
    3434    since = delta.days * 24 * 60 * 60 + delta.seconds
    35     for i, (seconds, name) in enumerate(chunks):
    36         count = since // seconds
    37         if count != 0:
    38             break
    39     s = ugettext('%(number)d %(type)s') % {'number': count, 'type': name(count)}
    40     if i + 1 < len(chunks):
    41         # Now get the second item
    42         seconds2, name2 = chunks[i + 1]
    43         count2 = (since - (seconds * count)) // seconds2
    44         if count2 != 0:
    45             s += ugettext(', %(number)d %(type)s') % {'number': count2, 'type': name2(count2)}
     35    if since < 0:
     36        s = u'0 ' + ugettext('minutes')
     37    else:
     38        for i, (seconds, name) in enumerate(chunks):
     39            count = since // seconds
     40            if count != 0:
     41                break
     42        s = ugettext('%(number)d %(type)s') % {'number': count, 'type': name(count)}
     43        if i + 1 < len(chunks):
     44            # Now get the second item
     45            seconds2, name2 = chunks[i + 1]
     46            count2 = (since - (seconds * count)) // seconds2
     47            if count2 != 0:
     48                s += ugettext(', %(number)d %(type)s') % {'number': count2, 'type': name2(count2)}
    4649    return s
    4750
    4851def timeuntil(d, now=None):
  • docs/templates.txt

     
    12721272June 2006, and ``comment_date`` is a date instance for 08:00 on 1 June 2006,
    12731273then ``{{ comment_date|timesince:blog_date }}`` would return "8 hours".
    12741274
     1275If the time interval is small enough, it will be expressed in minutes.
     1276"0 minutes" will be returned for any date that is in the future relative
     1277to the comparison point.
     1278
    12751279timeuntil
    12761280~~~~~~~~~
    12771281
     
    12841288the comparison point (instead of *now*). If ``from_date`` contains 22 June
    128512892006, then ``{{ conference_date|timeuntil:from_date }}`` will return "7 days".
    12861290
     1291If the time interval is small enough, it will be expressed in minutes.
     1292"0 minutes" will be returned for any date that is in the past relative to
     1293the comparison point.
     1294
    12871295title
    12881296~~~~~
    12891297
Back to Top