Code

Ticket #2675: timesince.diff

File timesince.diff, 2.4 KB (added by nickefford, 7 years ago)

Patch for django.utils.timesince and template docs

Line 
1Index: django/utils/timesince.py
2===================================================================
3--- django/utils/timesince.py   (revision 6182)
4+++ django/utils/timesince.py   (working copy)
5@@ -32,17 +32,20 @@
6     # ignore microsecond part of 'd' since we removed it from 'now'
7     delta = now - (d - datetime.timedelta(0, 0, d.microsecond))
8     since = delta.days * 24 * 60 * 60 + delta.seconds
9-    for i, (seconds, name) in enumerate(chunks):
10-        count = since // seconds
11-        if count != 0:
12-            break
13-    s = ugettext('%(number)d %(type)s') % {'number': count, 'type': name(count)}
14-    if i + 1 < len(chunks):
15-        # Now get the second item
16-        seconds2, name2 = chunks[i + 1]
17-        count2 = (since - (seconds * count)) // seconds2
18-        if count2 != 0:
19-            s += ugettext(', %(number)d %(type)s') % {'number': count2, 'type': name2(count2)}
20+    if since < 0:
21+        s = u'0 ' + ugettext('minutes')
22+    else:
23+        for i, (seconds, name) in enumerate(chunks):
24+            count = since // seconds
25+            if count != 0:
26+                break
27+        s = ugettext('%(number)d %(type)s') % {'number': count, 'type': name(count)}
28+        if i + 1 < len(chunks):
29+            # Now get the second item
30+            seconds2, name2 = chunks[i + 1]
31+            count2 = (since - (seconds * count)) // seconds2
32+            if count2 != 0:
33+                s += ugettext(', %(number)d %(type)s') % {'number': count2, 'type': name2(count2)}
34     return s
35 
36 def timeuntil(d, now=None):
37Index: docs/templates.txt
38===================================================================
39--- docs/templates.txt  (revision 6182)
40+++ docs/templates.txt  (working copy)
41@@ -1272,6 +1272,10 @@
42 June 2006, and ``comment_date`` is a date instance for 08:00 on 1 June 2006,
43 then ``{{ comment_date|timesince:blog_date }}`` would return "8 hours".
44 
45+If the time interval is small enough, it will be expressed in minutes.
46+"0 minutes" will be returned for any date that is in the future relative
47+to the comparison point.
48+
49 timeuntil
50 ~~~~~~~~~
51 
52@@ -1284,6 +1288,10 @@
53 the comparison point (instead of *now*). If ``from_date`` contains 22 June
54 2006, then ``{{ conference_date|timeuntil:from_date }}`` will return "7 days".
55 
56+If the time interval is small enough, it will be expressed in minutes.
57+"0 minutes" will be returned for any date that is in the past relative to
58+the comparison point.
59+
60 title
61 ~~~~~
62