Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #2053 -- added an optional comparison argument to the "timesinc…

…e" filter.

Added a "timeuntil" filter that works analogously. Thanks, john@sneeu.com.


git-svn-id: http://code.djangoproject.com/svn/django/trunk@3185 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 239adf83d33c47ea6dd961904d79962cd7e5a65c 1 parent c4fa8a1
Malcolm Tredinnick authored June 21, 2006
15  django/template/defaultfilters.py
@@ -345,13 +345,25 @@ def time(value, arg=None):
345 345
         arg = settings.TIME_FORMAT
346 346
     return time_format(value, arg)
347 347
 
348  
-def timesince(value):
  348
+def timesince(value, arg=None):
349 349
     'Formats a date as the time since that date (i.e. "4 days, 6 hours")'
350 350
     from django.utils.timesince import timesince
351 351
     if not value:
352 352
         return ''
  353
+    if arg:
  354
+        return timesince(arg, value)
353 355
     return timesince(value)
354 356
 
  357
+def timeuntil(value, arg=None):
  358
+    'Formats a date as the time until that date (i.e. "4 days, 6 hours")'
  359
+    from django.utils.timesince import timesince
  360
+    from datetime import datetime
  361
+    if not value:
  362
+        return ''
  363
+    if arg:
  364
+        return timesince(arg, value)
  365
+    return timesince(datetime.now(), value)
  366
+
355 367
 ###################
356 368
 # LOGIC           #
357 369
 ###################
@@ -485,6 +497,7 @@ def pprint(value):
485 497
 register.filter(striptags)
486 498
 register.filter(time)
487 499
 register.filter(timesince)
  500
+register.filter(timeuntil)
488 501
 register.filter(title)
489 502
 register.filter(truncatewords)
490 503
 register.filter(unordered_list)
5  django/utils/timesince.py
@@ -47,10 +47,11 @@ def timesince(d, now=None):
47 47
             s += ', %d %s' % (count2, name2(count2))
48 48
     return s
49 49
 
50  
-def timeuntil(d):
  50
+def timeuntil(d, now=None):
51 51
     """
52 52
     Like timesince, but returns a string measuring the time until
53 53
     the given time.
54 54
     """
55  
-    now = datetime.datetime.now()
  55
+    if now == None:
  56
+        now = datetime.datetime.now()
56 57
     return timesince(now, d)
18  docs/templates.txt
@@ -1022,6 +1022,24 @@ timesince
1022 1022
 
1023 1023
 Formats a date as the time since that date (i.e. "4 days, 6 hours").
1024 1024
 
  1025
+Takes an optional argument that is a variable containing the date to use as
  1026
+the comparison point (without the argument, the comparison point is *now*).
  1027
+For example, if ``blog_date`` is a date instance representing midnight on 1
  1028
+June 2006, and ``comment_date`` is a date instanace for 08:00 on 1 June 2006,
  1029
+then ``{{ comment_date|timesince:blog_date }}`` would return "8 hours".
  1030
+
  1031
+timeuntil
  1032
+~~~~~~~~~
  1033
+
  1034
+Similar to ``timesince``, except that it measures the time from now until the
  1035
+given date or datetime. For example, if today is 1 June 2006 and
  1036
+``conference_date`` is a date instance holding 29 June 2006, then
  1037
+``{{ conference_date|timeuntil }}`` will return "28 days".
  1038
+
  1039
+Takes an optional argument that is a variable containing the date to use as
  1040
+the comparison point (instead of *now*). If ``from_date`` contains 22 June
  1041
+2006, then ``{{ conference_date|timeuntil:from_date }}`` will return "7 days".
  1042
+
1025 1043
 title
1026 1044
 ~~~~~
1027 1045
 
26  tests/othertests/templates.py
@@ -4,7 +4,7 @@
4 4
 from django import template
5 5
 from django.template import loader
6 6
 from django.utils.translation import activate, deactivate, install
7  
-from datetime import datetime
  7
+from datetime import datetime, timedelta
8 8
 import traceback
9 9
 
10 10
 #################################
@@ -57,6 +57,9 @@ class OtherClass:
57 57
     def method(self):
58 58
         return "OtherClass.method"
59 59
 
  60
+# NOW used by timesince tag tests.
  61
+NOW = datetime.now()
  62
+
60 63
 # SYNTAX --
61 64
 # 'template_name': ('template contents', 'context dict', 'expected string output' or Exception class)
62 65
 TEMPLATE_TESTS = {
@@ -530,6 +533,27 @@ def method(self):
530 533
     'now02' : ('{% now "j "n" Y"%}', {}, template.TemplateSyntaxError),
531 534
 #    'now03' : ('{% now "j \"n\" Y"%}', {}, str(datetime.now().day) + '"' + str(datetime.now().month) + '"' + str(datetime.now().year)),
532 535
 #    'now04' : ('{% now "j \nn\n Y"%}', {}, str(datetime.now().day) + '\n' + str(datetime.now().month) + '\n' + str(datetime.now().year))
  536
+
  537
+    ### TIMESINCE TAG ##################################################
  538
+    # Default compare with datetime.now()
  539
+    'timesince01' : ('{{ a|timesince }}', {'a':datetime.now()}, '0 minutes'),
  540
+    'timesince02' : ('{{ a|timesince }}', {'a':(datetime.now() - timedelta(days=1))}, '1 day'),
  541
+    'timesince03' : ('{{ a|timesince }}', {'a':(datetime.now() -
  542
+        timedelta(hours=1, minutes=25))}, '1 hour, 25 minutes'),
  543
+
  544
+    # Compare to a given parameter
  545
+    'timesince04' : ('{{ a|timesince:b }}', {'a':NOW + timedelta(days=2), 'b':NOW + timedelta(days=1)}, '1 day'),
  546
+    'timesince05' : ('{{ a|timesince:b }}', {'a':NOW + timedelta(days=2), 'b':NOW + timedelta(days=2)}, '0 minutes'),
  547
+
  548
+    ### TIMEUNTIL TAG ##################################################
  549
+    # Default compare with datetime.now()
  550
+    'timeuntil01' : ('{{ a|timeuntil }}', {'a':datetime.now()}, '0 minutes'),
  551
+    'timeuntil02' : ('{{ a|timeuntil }}', {'a':(datetime.now() + timedelta(days=1))}, '1 day'),
  552
+    'timeuntil03' : ('{{ a|timeuntil }}', {'a':(datetime.now() + timedelta(hours=8, minutes=10))}, '8 hours, 10 minutes'),
  553
+
  554
+    # Compare to a given parameter
  555
+    'timeuntil04' : ('{{ a|timeuntil:b }}', {'a':NOW - timedelta(days=1), 'b':NOW - timedelta(days=2)}, '1 day'),
  556
+    'timeuntil05' : ('{{ a|timeuntil:b }}', {'a':NOW - timedelta(days=2), 'b':NOW - timedelta(days=2)}, '0 minutes'),
533 557
 }
534 558
 
535 559
 def test_template_loader(template_name, template_dirs=None):

0 notes on commit 239adf8

Please sign in to comment.
Something went wrong with that request. Please try again.