2012-06-08 00:08:47 +08:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2007-09-17 12:54:53 +08:00
|
|
|
import datetime
|
|
|
|
|
2013-05-18 19:58:45 +08:00
|
|
|
from django.utils.html import avoid_wrapping
|
2011-11-18 21:01:06 +08:00
|
|
|
from django.utils.timezone import is_aware, utc
|
2013-02-02 04:22:20 +08:00
|
|
|
from django.utils.translation import ugettext, ungettext_lazy
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2012-03-22 15:29:39 +08:00
|
|
|
def timesince(d, now=None, reversed=False):
|
2005-07-13 09:25:57 +08:00
|
|
|
"""
|
2007-09-17 12:50:12 +08:00
|
|
|
Takes two datetime objects and returns the time between d and now
|
|
|
|
as a nicely formatted string, e.g. "10 minutes". If d occurs after now,
|
|
|
|
then "0 minutes" is returned.
|
|
|
|
|
|
|
|
Units used are years, months, weeks, days, hours, and minutes.
|
|
|
|
Seconds and microseconds are ignored. Up to two adjacent units will be
|
|
|
|
displayed. For example, "2 weeks, 3 days" and "1 year, 3 months" are
|
|
|
|
possible outputs, but "2 weeks, 3 hours" and "1 year, 5 days" are not.
|
|
|
|
|
2013-07-07 09:24:38 +08:00
|
|
|
Adapted from
|
|
|
|
http://web.archive.org/web/20060617175230/http://blog.natbat.co.uk/archive/2003/Jun/14/time_since
|
2005-07-13 09:25:57 +08:00
|
|
|
"""
|
|
|
|
chunks = (
|
2013-02-02 04:22:20 +08:00
|
|
|
(60 * 60 * 24 * 365, ungettext_lazy('%d year', '%d years')),
|
|
|
|
(60 * 60 * 24 * 30, ungettext_lazy('%d month', '%d months')),
|
|
|
|
(60 * 60 * 24 * 7, ungettext_lazy('%d week', '%d weeks')),
|
|
|
|
(60 * 60 * 24, ungettext_lazy('%d day', '%d days')),
|
|
|
|
(60 * 60, ungettext_lazy('%d hour', '%d hours')),
|
|
|
|
(60, ungettext_lazy('%d minute', '%d minutes'))
|
2005-07-13 09:25:57 +08:00
|
|
|
)
|
2009-03-31 05:32:34 +08:00
|
|
|
# Convert datetime.date to datetime.datetime for comparison.
|
|
|
|
if not isinstance(d, datetime.datetime):
|
2006-02-24 04:27:03 +08:00
|
|
|
d = datetime.datetime(d.year, d.month, d.day)
|
2009-03-31 05:32:34 +08:00
|
|
|
if now and not isinstance(now, datetime.datetime):
|
|
|
|
now = datetime.datetime(now.year, now.month, now.day)
|
2008-08-26 16:08:55 +08:00
|
|
|
|
|
|
|
if not now:
|
2011-11-18 21:01:06 +08:00
|
|
|
now = datetime.datetime.now(utc if is_aware(d) else None)
|
2006-02-24 04:27:03 +08:00
|
|
|
|
2012-03-22 15:29:39 +08:00
|
|
|
delta = (d - now) if reversed else (now - d)
|
2011-11-18 21:01:06 +08:00
|
|
|
# ignore microseconds
|
2005-10-23 05:37:59 +08:00
|
|
|
since = delta.days * 24 * 60 * 60 + delta.seconds
|
2007-09-17 12:50:12 +08:00
|
|
|
if since <= 0:
|
|
|
|
# d is in the future compared to now, stop processing.
|
2013-05-18 19:58:45 +08:00
|
|
|
return avoid_wrapping(ugettext('0 minutes'))
|
2005-11-23 05:45:54 +08:00
|
|
|
for i, (seconds, name) in enumerate(chunks):
|
2007-07-12 21:55:19 +08:00
|
|
|
count = since // seconds
|
2005-07-13 09:25:57 +08:00
|
|
|
if count != 0:
|
|
|
|
break
|
2013-05-18 19:58:45 +08:00
|
|
|
result = avoid_wrapping(name % count)
|
2005-07-13 09:25:57 +08:00
|
|
|
if i + 1 < len(chunks):
|
|
|
|
# Now get the second item
|
|
|
|
seconds2, name2 = chunks[i + 1]
|
2007-07-12 21:55:19 +08:00
|
|
|
count2 = (since - (seconds * count)) // seconds2
|
2005-07-13 09:25:57 +08:00
|
|
|
if count2 != 0:
|
2013-05-18 19:58:45 +08:00
|
|
|
result += ugettext(', ') + avoid_wrapping(name2 % count2)
|
2013-02-02 04:22:20 +08:00
|
|
|
return result
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2006-06-21 14:56:08 +08:00
|
|
|
def timeuntil(d, now=None):
|
2005-07-13 09:25:57 +08:00
|
|
|
"""
|
|
|
|
Like timesince, but returns a string measuring the time until
|
|
|
|
the given time.
|
|
|
|
"""
|
2012-03-22 15:29:39 +08:00
|
|
|
return timesince(d, now, reversed=True)
|