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'