2015-06-05 06:46:52 +08:00
|
|
|
import calendar
|
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
|
2017-01-27 03:58:33 +08:00
|
|
|
from django.utils.translation import gettext, ngettext_lazy
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2018-03-13 23:28:39 +08:00
|
|
|
TIME_STRINGS = {
|
|
|
|
'year': ngettext_lazy('%d year', '%d years'),
|
|
|
|
'month': ngettext_lazy('%d month', '%d months'),
|
|
|
|
'week': ngettext_lazy('%d week', '%d weeks'),
|
|
|
|
'day': ngettext_lazy('%d day', '%d days'),
|
|
|
|
'hour': ngettext_lazy('%d hour', '%d hours'),
|
|
|
|
'minute': ngettext_lazy('%d minute', '%d minutes'),
|
|
|
|
}
|
|
|
|
|
2015-03-05 03:24:30 +08:00
|
|
|
TIMESINCE_CHUNKS = (
|
2018-03-13 23:28:39 +08:00
|
|
|
(60 * 60 * 24 * 365, 'year'),
|
|
|
|
(60 * 60 * 24 * 30, 'month'),
|
|
|
|
(60 * 60 * 24 * 7, 'week'),
|
|
|
|
(60 * 60 * 24, 'day'),
|
|
|
|
(60 * 60, 'hour'),
|
|
|
|
(60, 'minute'),
|
2015-03-05 03:24:30 +08:00
|
|
|
)
|
|
|
|
|
2013-11-03 07:53:29 +08:00
|
|
|
|
2020-07-03 14:01:45 +08:00
|
|
|
def timesince(d, now=None, reversed=False, time_strings=None, depth=2):
|
2005-07-13 09:25:57 +08:00
|
|
|
"""
|
2017-01-25 04:32:33 +08:00
|
|
|
Take two datetime objects and return the time between d and now as a nicely
|
|
|
|
formatted string, e.g. "10 minutes". If d occurs after now, return
|
|
|
|
"0 minutes".
|
2007-09-17 12:50:12 +08:00
|
|
|
|
|
|
|
Units used are years, months, weeks, days, hours, and minutes.
|
2020-07-03 14:01:45 +08:00
|
|
|
Seconds and microseconds are ignored. Up to `depth` adjacent units will be
|
2007-09-17 12:50:12 +08:00
|
|
|
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.
|
|
|
|
|
2018-03-13 23:28:39 +08:00
|
|
|
`time_strings` is an optional dict of strings to replace the default
|
|
|
|
TIME_STRINGS dict.
|
|
|
|
|
2020-07-03 14:01:45 +08:00
|
|
|
`depth` is an optional integer to control the number of adjacent time
|
|
|
|
units returned.
|
|
|
|
|
2013-07-07 09:24:38 +08:00
|
|
|
Adapted from
|
2018-09-26 14:48:47 +08:00
|
|
|
https://web.archive.org/web/20060617175230/http://blog.natbat.co.uk/archive/2003/Jun/14/time_since
|
2005-07-13 09:25:57 +08:00
|
|
|
"""
|
2018-03-13 23:28:39 +08:00
|
|
|
if time_strings is None:
|
|
|
|
time_strings = TIME_STRINGS
|
2020-07-03 14:01:45 +08:00
|
|
|
if depth <= 0:
|
|
|
|
raise ValueError('depth must be greater than 0.')
|
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
|
|
|
|
2018-01-04 07:52:12 +08:00
|
|
|
now = now or datetime.datetime.now(utc if is_aware(d) else None)
|
2006-02-24 04:27:03 +08:00
|
|
|
|
2016-12-27 22:29:11 +08:00
|
|
|
if reversed:
|
|
|
|
d, now = now, d
|
|
|
|
delta = now - d
|
2015-06-05 06:46:52 +08:00
|
|
|
|
|
|
|
# Deal with leapyears by subtracing the number of leapdays
|
2016-12-27 22:29:11 +08:00
|
|
|
leapdays = calendar.leapdays(d.year, now.year)
|
2017-01-02 21:40:44 +08:00
|
|
|
if leapdays != 0:
|
|
|
|
if calendar.isleap(d.year):
|
|
|
|
leapdays -= 1
|
|
|
|
elif calendar.isleap(now.year):
|
|
|
|
leapdays += 1
|
2016-12-27 22:29:11 +08:00
|
|
|
delta -= datetime.timedelta(leapdays)
|
2015-06-05 06:46:52 +08:00
|
|
|
|
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.
|
2019-10-28 18:17:29 +08:00
|
|
|
return avoid_wrapping(time_strings['minute'] % 0)
|
2015-03-05 03:24:30 +08:00
|
|
|
for i, (seconds, name) in enumerate(TIMESINCE_CHUNKS):
|
2007-07-12 21:55:19 +08:00
|
|
|
count = since // seconds
|
2005-07-13 09:25:57 +08:00
|
|
|
if count != 0:
|
|
|
|
break
|
2020-07-03 14:01:45 +08:00
|
|
|
else:
|
|
|
|
return avoid_wrapping(time_strings['minute'] % 0)
|
|
|
|
result = []
|
|
|
|
current_depth = 0
|
|
|
|
while i < len(TIMESINCE_CHUNKS) and current_depth < depth:
|
|
|
|
seconds, name = TIMESINCE_CHUNKS[i]
|
|
|
|
count = since // seconds
|
|
|
|
if count == 0:
|
|
|
|
break
|
|
|
|
result.append(avoid_wrapping(time_strings[name] % count))
|
|
|
|
since -= seconds * count
|
|
|
|
current_depth += 1
|
|
|
|
i += 1
|
|
|
|
return gettext(', ').join(result)
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2013-11-03 07:53:29 +08:00
|
|
|
|
2020-07-03 14:01:45 +08:00
|
|
|
def timeuntil(d, now=None, time_strings=None, depth=2):
|
2005-07-13 09:25:57 +08:00
|
|
|
"""
|
2017-01-25 04:32:33 +08:00
|
|
|
Like timesince, but return a string measuring the time until the given time.
|
2005-07-13 09:25:57 +08:00
|
|
|
"""
|
2020-07-03 14:01:45 +08:00
|
|
|
return timesince(d, now, reversed=True, time_strings=time_strings, depth=depth)
|