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
This commit is contained in:
Adrian Holovaty 2006-02-23 20:27:03 +00:00
parent a5fad0eb37
commit 9a74e89ef0
2 changed files with 8 additions and 1 deletions

View File

@ -15,6 +15,9 @@ def timesince(d, now=None):
(60 * 60, lambda n: ngettext('hour', 'hours', n)), (60 * 60, lambda n: ngettext('hour', 'hours', n)),
(60, lambda n: ngettext('minute', 'minutes', 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: if now:
t = now.timetuple() t = now.timetuple()
else: else:
@ -24,7 +27,7 @@ def timesince(d, now=None):
else: else:
tz = None tz = None
now = datetime.datetime(t[0], t[1], t[2], t[3], t[4], t[5], tzinfo=tz) 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' # ignore microsecond part of 'd' since we removed it from 'now'
delta = now - (d - datetime.timedelta(0, 0, d.microsecond)) delta = now - (d - datetime.timedelta(0, 0, d.microsecond))
since = delta.days * 24 * 60 * 60 + delta.seconds since = delta.days * 24 * 60 * 60 + delta.seconds

View File

@ -237,6 +237,10 @@ False
>>> timesince(datetime.datetime.now() - datetime.timedelta(1)) >>> timesince(datetime.datetime.now() - datetime.timedelta(1))
'1 day' '1 day'
# datetime.date compataibility with timesince
>>> timesince(datetime.date.today() - datetime.timedelta(1))
'1 day, 23 hours'
>>> default("val", "default") >>> default("val", "default")
'val' 'val'