From 9a74e89ef06433e53e6926a07d42c30d22840682 Mon Sep 17 00:00:00 2001 From: Adrian Holovaty Date: Thu, 23 Feb 2006 20:27:03 +0000 Subject: [PATCH] Fixed #1385 -- Allowed timesince filter to accept datetime.date objects. Thanks, Matt git-svn-id: http://code.djangoproject.com/svn/django/trunk@2375 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/utils/timesince.py | 5 ++++- tests/othertests/defaultfilters.py | 4 ++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/django/utils/timesince.py b/django/utils/timesince.py index 0d1ab470e5..a16616584b 100644 --- a/django/utils/timesince.py +++ b/django/utils/timesince.py @@ -15,6 +15,9 @@ def timesince(d, now=None): (60 * 60, lambda n: ngettext('hour', 'hours', n)), (60, lambda n: ngettext('minute', 'minutes', n)) ) + # Convert datetime.date to datetime.datetime for comparison + if d.__class__ is not datetime.datetime: + d = datetime.datetime(d.year, d.month, d.day) if now: t = now.timetuple() else: @@ -24,7 +27,7 @@ def timesince(d, now=None): else: tz = None now = datetime.datetime(t[0], t[1], t[2], t[3], t[4], t[5], tzinfo=tz) - + # 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 diff --git a/tests/othertests/defaultfilters.py b/tests/othertests/defaultfilters.py index d0d5d21e58..fa018a2328 100644 --- a/tests/othertests/defaultfilters.py +++ b/tests/othertests/defaultfilters.py @@ -237,6 +237,10 @@ False >>> timesince(datetime.datetime.now() - datetime.timedelta(1)) '1 day' +# datetime.date compataibility with timesince +>>> timesince(datetime.date.today() - datetime.timedelta(1)) +'1 day, 23 hours' + >>> default("val", "default") 'val'