Code

Ticket #9672: timeuntil.date.diff

File timeuntil.date.diff, 1.6 KB (added by rossp, 5 years ago)

Moved fix into timesince block so any use of timesince() will apply it, and added unit tests. Against Django r9739.

Line 
1Index: django/utils/timesince.py
2===================================================================
3--- django/utils/timesince.py   (revision 9739)
4+++ django/utils/timesince.py   (working copy)
5@@ -34,6 +34,10 @@
6             now = datetime.datetime.now(LocalTimezone(d))
7         else:
8             now = datetime.datetime.now()
9+
10+    # Convert datetime.date to datetime.datetime for comparison
11+    if now.__class__ is not datetime.datetime:
12+        now = datetime.datetime(now.year, now.month, now.day)
13
14     # ignore microsecond part of 'd' since we removed it from 'now'
15     delta = now - (d - datetime.timedelta(0, 0, d.microsecond))
16@@ -64,4 +68,5 @@
17             now = datetime.datetime.now(LocalTimezone(d))
18         else:
19             now = datetime.datetime.now()
20+
21     return timesince(now, d)
22Index: tests/regressiontests/utils/timesince.py
23===================================================================
24--- tests/regressiontests/utils/timesince.py    (revision 9739)
25+++ tests/regressiontests/utils/timesince.py    (working copy)
26@@ -1,5 +1,5 @@
27 """
28->>> from datetime import datetime, timedelta
29+>>> from datetime import datetime, timedelta, date
30 >>> from django.utils.timesince import timesince, timeuntil
31 >>> from django.utils.tzinfo import LocalTimezone, FixedOffset
32
33@@ -86,4 +86,13 @@
34 u'0 minutes'
35 >>> timeuntil(now_tz, now_tz_i)
36 u'0 minutes'
37+
38+# Timeuntil should work with both date and datetime objects
39+>>> today = date.today()
40+>>> timeuntil(today+oneday, today)
41+u'1 day'
42+>>> timeuntil(today-oneday, today)
43+u'0 minutes'
44+>>> timeuntil(today+oneweek, today)
45+u'1 week'
46 """