Ticket #479: django-timesince.patch

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

Fix timesince to take timezones into account

  • 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    now = datetime.datetime(t[0], t[1], t[2], t[3], t[4], t[5],
     22                            tzinfo=LocalTimezone())
     23    since = (now - d).seconds
    2024    # Crazy iteration syntax because we need i to be current index
    2125    for i, (seconds, name) in zip(range(len(chunks)), chunks):
    2226        count = math.floor(since / seconds)
Back to Top