From 3e7f5131a57f1606afb59ea4d86cac7a6005b969 Mon Sep 17 00:00:00 2001 From: Gary Wilson Jr Date: Mon, 30 Mar 2009 21:32:34 +0000 Subject: [PATCH] Fixed #9065 -- Fixed the `timesince` and `timeuntil` template tags to work when both values involved are date objects, thanks to morty and mboersma for the patch. git-svn-id: http://code.djangoproject.com/svn/django/trunk@10215 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/utils/timesince.py | 6 ++++-- tests/regressiontests/templates/filters.py | 12 +++++++++++- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/django/utils/timesince.py b/django/utils/timesince.py index 1142e00d29..c584ce6981 100644 --- a/django/utils/timesince.py +++ b/django/utils/timesince.py @@ -25,9 +25,11 @@ def timesince(d, now=None): (60 * 60, lambda n: ungettext('hour', 'hours', n)), (60, lambda n: ungettext('minute', 'minutes', n)) ) - # Convert datetime.date to datetime.datetime for comparison - if d.__class__ is not datetime.datetime: + # Convert datetime.date to datetime.datetime for comparison. + if not isinstance(d, datetime.datetime): d = datetime.datetime(d.year, d.month, d.day) + if now and not isinstance(now, datetime.datetime): + now = datetime.datetime(now.year, now.month, now.day) if not now: if d.tzinfo: diff --git a/tests/regressiontests/templates/filters.py b/tests/regressiontests/templates/filters.py index 5e0c1a9860..91afdc74ba 100644 --- a/tests/regressiontests/templates/filters.py +++ b/tests/regressiontests/templates/filters.py @@ -7,7 +7,7 @@ timezones are only evaluated at the moment of execution and will therefore be consistent. """ -from datetime import datetime, timedelta +from datetime import date, datetime, timedelta from django.utils.tzinfo import LocalTimezone, FixedOffset from django.utils.safestring import mark_safe @@ -28,6 +28,8 @@ def get_filter_tests(): now = datetime.now() now_tz = datetime.now(LocalTimezone(now)) now_tz_i = datetime.now(FixedOffset((3 * 60) + 15)) # imaginary time zone + today = date.today() + return { # Default compare with datetime.now() 'filter-timesince01' : ('{{ a|timesince }}', {'a': datetime.now() + timedelta(minutes=-1, seconds = -10)}, '1 minute'), @@ -55,6 +57,10 @@ def get_filter_tests(): 'filter-timesince15' : ('{{ a|timesince:b }}', {'a': now, 'b': now_tz_i}, ''), 'filter-timesince16' : ('{{ a|timesince:b }}', {'a': now_tz_i, 'b': now}, ''), + # Regression for #9065 (two date objects). + 'filter-timesince17' : ('{{ a|timesince:b }}', {'a': today, 'b': today}, '0 minutes'), + 'filter-timesince18' : ('{{ a|timesince:b }}', {'a': today, 'b': today + timedelta(hours=24)}, '1 day'), + # Default compare with datetime.now() 'filter-timeuntil01' : ('{{ a|timeuntil }}', {'a':datetime.now() + timedelta(minutes=2, seconds = 10)}, '2 minutes'), 'filter-timeuntil02' : ('{{ a|timeuntil }}', {'a':(datetime.now() + timedelta(days=1, seconds = 10))}, '1 day'), @@ -74,6 +80,10 @@ def get_filter_tests(): 'filter-timeuntil10' : ('{{ a|timeuntil }}', {'a': now_tz_i}, '0 minutes'), 'filter-timeuntil11' : ('{{ a|timeuntil:b }}', {'a': now_tz_i, 'b': now_tz}, '0 minutes'), + # Regression for #9065 (two date objects). + 'filter-timeuntil12' : ('{{ a|timeuntil:b }}', {'a': today, 'b': today}, '0 minutes'), + 'filter-timeuntil13' : ('{{ a|timeuntil:b }}', {'a': today, 'b': today - timedelta(hours=24)}, '1 day'), + 'filter-addslash01': ("{% autoescape off %}{{ a|addslashes }} {{ b|addslashes }}{% endautoescape %}", {"a": "'", "b": mark_safe("'")}, ur"\' \'"), 'filter-addslash02': ("{{ a|addslashes }} {{ b|addslashes }}", {"a": "'", "b": mark_safe("'")}, ur"<a>\' \'"),