summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAymeric Augustin <aymeric.augustin@m4x.org>2012-03-22 07:29:39 +0000
committerAymeric Augustin <aymeric.augustin@m4x.org>2012-03-22 07:29:39 +0000
commit1e28567e0d2be1f09a301131b1a29b939e797d6c (patch)
treeae87a125e8f3778cc2b4a54765a5e8f343719f9b
parent4219e2b7f833171bd24c6dd9802deb9eb7d0bafe (diff)
Fixed #17937 -- Avoided an error in the timeuntil filter when it receives a date object. Thanks Dmitry Guyvoronsky for the report.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@17774 bcc190cf-cafb-0310-a4f2-bffc1f526a37
-rw-r--r--django/utils/timesince.py8
-rw-r--r--tests/regressiontests/utils/timesince.py12
2 files changed, 12 insertions, 8 deletions
diff --git a/django/utils/timesince.py b/django/utils/timesince.py
index 511acb518a..276108b55a 100644
--- a/django/utils/timesince.py
+++ b/django/utils/timesince.py
@@ -3,7 +3,7 @@ import datetime
from django.utils.timezone import is_aware, utc
from django.utils.translation import ungettext, ugettext
-def timesince(d, now=None):
+def timesince(d, now=None, reversed=False):
"""
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,
@@ -33,7 +33,7 @@ def timesince(d, now=None):
if not now:
now = datetime.datetime.now(utc if is_aware(d) else None)
- delta = now - d
+ delta = (d - now) if reversed else (now - d)
# ignore microseconds
since = delta.days * 24 * 60 * 60 + delta.seconds
if since <= 0:
@@ -57,6 +57,4 @@ def timeuntil(d, now=None):
Like timesince, but returns a string measuring the time until
the given time.
"""
- if not now:
- now = datetime.datetime.now(utc if is_aware(d) else None)
- return timesince(now, d)
+ return timesince(d, now, reversed=True)
diff --git a/tests/regressiontests/utils/timesince.py b/tests/regressiontests/utils/timesince.py
index b0f94d182b..a4d479ad3e 100644
--- a/tests/regressiontests/utils/timesince.py
+++ b/tests/regressiontests/utils/timesince.py
@@ -99,12 +99,18 @@ class TimesinceTests(unittest.TestCase):
self.assertEqual(timesince(now_tz), u'0 minutes')
self.assertEqual(timeuntil(now_tz, now_tz_i), u'0 minutes')
+ def test_date_objects(self):
+ """ Both timesince and timeuntil should work on date objects (#17937). """
+ today = datetime.date.today()
+ self.assertEqual(timesince(today + self.oneday), u'0 minutes')
+ self.assertEqual(timeuntil(today - self.oneday), u'0 minutes')
+
def test_both_date_objects(self):
""" Timesince should work with both date objects (#9672) """
today = datetime.date.today()
- self.assertEqual(timeuntil(today+self.oneday, today), u'1 day')
- self.assertEqual(timeuntil(today-self.oneday, today), u'0 minutes')
- self.assertEqual(timeuntil(today+self.oneweek, today), u'1 week')
+ self.assertEqual(timeuntil(today + self.oneday, today), u'1 day')
+ self.assertEqual(timeuntil(today - self.oneday, today), u'0 minutes')
+ self.assertEqual(timeuntil(today + self.oneweek, today), u'1 week')
def test_naive_datetime_with_tzinfo_attribute(self):
class naive(datetime.tzinfo):