summaryrefslogtreecommitdiff
path: root/django
diff options
context:
space:
mode:
authorGary Wilson Jr <gary.wilson@gmail.com>2007-09-17 04:50:12 +0000
committerGary Wilson Jr <gary.wilson@gmail.com>2007-09-17 04:50:12 +0000
commit66203fc9ee7499bc331123678c7280873dd912f9 (patch)
treef405878af3192c0e1f701d9e4d4fadb94ad96261 /django
parent771481695f0e53556824bdd64662efea87d55f32 (diff)
Fixed #2675 -- Changed the `timeuntil` and `timesince` template filters to display "0 minutes" when passed a past or future date respectively instead of "-1 years, 12 months". Thanks to nickefford for the patch.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@6366 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django')
-rw-r--r--django/utils/timesince.py14
1 files changed, 12 insertions, 2 deletions
diff --git a/django/utils/timesince.py b/django/utils/timesince.py
index 455788e7d7..a69e55498a 100644
--- a/django/utils/timesince.py
+++ b/django/utils/timesince.py
@@ -4,8 +4,15 @@ from django.utils.translation import ungettext, ugettext
def timesince(d, now=None):
"""
- Takes two datetime objects and returns the time between then and now
- as a nicely formatted string, e.g "10 minutes"
+ 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.
+
Adapted from http://blog.natbat.co.uk/archive/2003/Jun/14/time_since
"""
chunks = (
@@ -32,6 +39,9 @@ def timesince(d, now=None):
# ignore microsecond part of 'd' since we removed it from 'now'
delta = now - (d - datetime.timedelta(0, 0, d.microsecond))
since = delta.days * 24 * 60 * 60 + delta.seconds
+ if since <= 0:
+ # d is in the future compared to now, stop processing.
+ return u'0 ' + ugettext('minutes')
for i, (seconds, name) in enumerate(chunks):
count = since // seconds
if count != 0: