Ticket #479: django-timesince.2.patch
File django-timesince.2.patch, 1.1 KB (added by , 19 years ago) |
---|
-
timesince.py
1 1 import time, math, datetime 2 from tzinfo import LocalTimezone 2 3 3 4 def timesince(d, now=None): 4 5 """ … … 6 7 as a nicely formatted string, e.g "10 minutes" 7 8 Adapted from http://blog.natbat.co.uk/archive/2003/Jun/14/time_since 8 9 """ 9 original = time.mktime(d.timetuple())10 10 chunks = ( 11 11 (60 * 60 * 24 * 365, 'year'), 12 12 (60 * 60 * 24 * 30, 'month'), … … 14 14 (60 * 60, 'hour'), 15 15 (60, 'minute') 16 16 ) 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 20 27 # Crazy iteration syntax because we need i to be current index 21 28 for i, (seconds, name) in zip(range(len(chunks)), chunks): 22 29 count = math.floor(since / seconds)