Ticket #479: django-timesince.2.patch

File django-timesince.2.patch, 1.1 KB (added by sune.kirkeby@…, 19 years ago)
  • timesince.py

     
    11import time, math, datetime
     2from tzinfo import LocalTimezone
    23
    34def timesince(d, now=None):
    45    """
     
    67    as a nicely formatted string, e.g "10 minutes"
    78    Adapted from http://blog.natbat.co.uk/archive/2003/Jun/14/time_since
    89    """
    9     original = time.mktime(d.timetuple())
    1010    chunks = (
    1111      (60 * 60 * 24 * 365, 'year'),
    1212      (60 * 60 * 24 * 30, 'month'),
     
    1414      (60 * 60, 'hour'),
    1515      (60, 'minute')
    1616    )
    17     if not now:
    18         now = time.time()
    19     since = now - original
     17    if now:
     18        t = time.mktime(now)
     19    else:
     20        t = time.localtime()
     21    if d.tzinfo:
     22        tz = LocalTimezone()
     23    else:
     24        tz = None
     25    now = datetime.datetime(t[0], t[1], t[2], t[3], t[4], t[5], tzinfo=tz)
     26    since = (now - d).seconds
    2027    # Crazy iteration syntax because we need i to be current index
    2128    for i, (seconds, name) in zip(range(len(chunks)), chunks):
    2229        count = math.floor(since / seconds)
Back to Top