2007-09-17 12:54:53 +08:00
|
|
|
import datetime
|
|
|
|
import time
|
|
|
|
|
2005-10-23 05:37:59 +08:00
|
|
|
from django.utils.tzinfo import LocalTimezone
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
from django.utils.translation import ungettext, ugettext
|
2005-07-13 09:25:57 +08:00
|
|
|
|
|
|
|
def timesince(d, now=None):
|
|
|
|
"""
|
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.
|
|
|
|
|
2005-07-13 09:25:57 +08:00
|
|
|
Adapted from http://blog.natbat.co.uk/archive/2003/Jun/14/time_since
|
|
|
|
"""
|
|
|
|
chunks = (
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
(60 * 60 * 24 * 365, lambda n: ungettext('year', 'years', n)),
|
|
|
|
(60 * 60 * 24 * 30, lambda n: ungettext('month', 'months', n)),
|
|
|
|
(60 * 60 * 24 * 7, lambda n : ungettext('week', 'weeks', n)),
|
|
|
|
(60 * 60 * 24, lambda n : ungettext('day', 'days', n)),
|
|
|
|
(60 * 60, lambda n: ungettext('hour', 'hours', n)),
|
|
|
|
(60, lambda n: ungettext('minute', 'minutes', n))
|
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:
|
|
|
|
if d.tzinfo:
|
|
|
|
now = datetime.datetime.now(LocalTimezone(d))
|
|
|
|
else:
|
|
|
|
now = datetime.datetime.now()
|
2006-02-24 04:27:03 +08:00
|
|
|
|
2006-01-02 02:37:33 +08:00
|
|
|
# ignore microsecond part of 'd' since we removed it from 'now'
|
|
|
|
delta = now - (d - datetime.timedelta(0, 0, d.microsecond))
|
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.
|
|
|
|
return u'0 ' + ugettext('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
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
s = ugettext('%(number)d %(type)s') % {'number': count, 'type': 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:
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
s += ugettext(', %(number)d %(type)s') % {'number': count2, 'type': name2(count2)}
|
2005-07-13 09:25:57 +08:00
|
|
|
return s
|
|
|
|
|
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.
|
|
|
|
"""
|
2008-08-26 16:08:55 +08:00
|
|
|
if not now:
|
2008-08-29 00:31:50 +08:00
|
|
|
if getattr(d, 'tzinfo', None):
|
2008-08-26 16:08:55 +08:00
|
|
|
now = datetime.datetime.now(LocalTimezone(d))
|
|
|
|
else:
|
|
|
|
now = datetime.datetime.now()
|
2005-12-09 10:52:27 +08:00
|
|
|
return timesince(now, d)
|