Permalink
Browse files

Fixed #1385 -- Allowed timesince filter to accept datetime.date obje…

…cts. Thanks, Matt

git-svn-id: http://code.djangoproject.com/svn/django/trunk@2375 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
adrianholovaty committed Feb 23, 2006
1 parent a5fad0e commit 9a74e89ef06433e53e6926a07d42c30d22840682
Showing with 8 additions and 1 deletion.
  1. +4 −1 django/utils/timesince.py
  2. +4 −0 tests/othertests/defaultfilters.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
@@ -237,6 +237,10 @@
>>> 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'

0 comments on commit 9a74e89

Please sign in to comment.