Ticket #7201: 7201.3.diff
File 7201.3.diff, 7.8 KB (added by , 16 years ago) |
---|
-
django/utils/timesince.py
27 27 ) 28 28 # Convert datetime.date to datetime.datetime for comparison 29 29 if d.__class__ is not datetime.datetime: 30 d = datetime.datetime(d.year, d.month, d.day) 31 if now: 32 t = now.timetuple() 33 else: 34 t = time.localtime() 35 if d.tzinfo: 36 tz = LocalTimezone(d) 37 else: 38 tz = None 39 now = datetime.datetime(t[0], t[1], t[2], t[3], t[4], t[5], tzinfo=tz) 30 d = datetime.datetime(d.year, d.month, d.day) 40 31 32 if not now: 33 if d.tzinfo: 34 now = datetime.datetime.now(LocalTimezone(d)) 35 else: 36 now = datetime.datetime.now() 37 41 38 # ignore microsecond part of 'd' since we removed it from 'now' 42 39 delta = now - (d - datetime.timedelta(0, 0, d.microsecond)) 43 40 since = delta.days * 24 * 60 * 60 + delta.seconds … … 62 59 Like timesince, but returns a string measuring the time until 63 60 the given time. 64 61 """ 65 if now == None: 66 now = datetime.datetime.now() 62 if not now: 63 if d.tzinfo: 64 now = datetime.datetime.now(LocalTimezone(d)) 65 else: 66 now = datetime.datetime.now() 67 67 return timesince(now, d) -
django/template/defaultfilters.py
642 642 from django.utils.timesince import timesince 643 643 if not value: 644 644 return u'' 645 if arg: 646 return timesince(arg, value) 647 return timesince(value) 645 try: 646 if arg: 647 return timesince(arg, value) 648 return timesince(value) 649 except (ValueError, TypeError): 650 return u'' 648 651 timesince.is_safe = False 649 652 650 653 def timeuntil(value, arg=None): 651 654 """Formats a date as the time until that date (i.e. "4 days, 6 hours").""" 652 from django.utils.timesince import time since655 from django.utils.timesince import timeuntil 653 656 from datetime import datetime 654 657 if not value: 655 658 return u'' 656 if arg: 657 return timesince(arg, value) 658 return timesince(datetime.now(), value) 659 try: 660 return timeuntil(value, arg) 661 except (ValueError, TypeError): 662 return u'' 659 663 timeuntil.is_safe = False 660 664 661 665 ################### -
tests/regressiontests/templates/filters.py
9 9 10 10 from datetime import datetime, timedelta 11 11 12 from django.utils.tzinfo import LocalTimezone 12 from django.utils.tzinfo import LocalTimezone, FixedOffset 13 13 from django.utils.safestring import mark_safe 14 14 15 15 # These two classes are used to test auto-escaping of __unicode__ output. … … 20 20 class SafeClass: 21 21 def __unicode__(self): 22 22 return mark_safe(u'you > me') 23 23 24 24 # RESULT SYNTAX -- 25 25 # 'template_name': ('template contents', 'context dict', 26 26 # 'expected string output' or Exception class) 27 27 def get_filter_tests(): 28 28 now = datetime.now() 29 29 now_tz = datetime.now(LocalTimezone(now)) 30 now_tz_i = datetime.now(FixedOffset((3 * 60) + 15)) # imaginary time zone 30 31 return { 31 32 # Default compare with datetime.now() 32 33 'filter-timesince01' : ('{{ a|timesince }}', {'a': datetime.now() + timedelta(minutes=-1, seconds = -10)}, '1 minute'), … … 40 41 # Check that timezone is respected 41 42 'filter-timesince06' : ('{{ a|timesince:b }}', {'a':now_tz + timedelta(hours=8), 'b':now_tz}, '8 hours'), 42 43 44 # Ensures that differing timezones are calculated correctly 45 'filter-timesince07' : ('{{ a|timesince }}', {'a': now}, '0 minutes'), 46 'filter-timesince08' : ('{{ a|timesince }}', {'a': now_tz}, '0 minutes'), 47 'filter-timesince09' : ('{{ a|timesince }}', {'a': now_tz_i}, '0 minutes'), 48 'filter-timesince10' : ('{{ a|timesince:b }}', {'a': now_tz, 'b': now_tz_i}, '0 minutes'), 49 'filter-timesince11' : ('{{ a|timesince:b }}', {'a': now, 'b': now_tz_i}, ''), 50 'filter-timesince12' : ('{{ a|timesince:b }}', {'a': now_tz_i, 'b': now}, ''), 51 43 52 # Default compare with datetime.now() 44 53 'filter-timeuntil01' : ('{{ a|timeuntil }}', {'a':datetime.now() + timedelta(minutes=2, seconds = 10)}, '2 minutes'), 45 54 'filter-timeuntil02' : ('{{ a|timeuntil }}', {'a':(datetime.now() + timedelta(days=1, seconds = 10))}, '1 day'), … … 49 58 'filter-timeuntil04' : ('{{ a|timeuntil:b }}', {'a':now - timedelta(days=1), 'b':now - timedelta(days=2)}, '1 day'), 50 59 'filter-timeuntil05' : ('{{ a|timeuntil:b }}', {'a':now - timedelta(days=2), 'b':now - timedelta(days=2, minutes=1)}, '1 minute'), 51 60 61 # Ensures that differing timezones are calculated correctly 62 'filter-timeuntil06' : ('{{ a|timeuntil }}', {'a': now_tz_i}, '0 minutes'), 63 'filter-timeuntil07' : ('{{ a|timeuntil:b }}', {'a': now_tz_i, 'b': now_tz}, '0 minutes'), 64 52 65 'filter-addslash01': ("{% autoescape off %}{{ a|addslashes }} {{ b|addslashes }}{% endautoescape %}", {"a": "<a>'", "b": mark_safe("<a>'")}, ur"<a>\' <a>\'"), 53 66 'filter-addslash02': ("{{ a|addslashes }} {{ b|addslashes }}", {"a": "<a>'", "b": mark_safe("<a>'")}, ur"<a>\' <a>\'"), 54 67 -
tests/regressiontests/utils/timesince.py
1 1 """ 2 2 >>> from datetime import datetime, timedelta 3 >>> from django.utils.timesince import timesince 3 >>> from django.utils.timesince import timesince, timeuntil 4 >>> from django.utils.tzinfo import LocalTimezone, FixedOffset 4 5 5 6 >>> t = datetime(2007, 8, 14, 13, 46, 0) 6 7 … … 74 75 u'0 minutes' 75 76 >>> timesince(t, t-4*oneday-5*oneminute) 76 77 u'0 minutes' 78 79 # When using two different timezones. 80 >>> now = datetime.now() 81 >>> now_tz = datetime.now(LocalTimezone(now)) 82 >>> now_tz_i = datetime.now(FixedOffset((3 * 60) + 15)) 83 >>> timesince(now_tz, now_tz_i) 84 u'0 minutes' 85 >>> timeuntil(now_tz, now_tz_i) 86 u'0 minutes' 77 87 """ -
AUTHORS
87 87 Juan Manuel Caicedo <juan.manuel.caicedo@gmail.com> 88 88 Trevor Caira <trevor@caira.com> 89 89 Ricardo Javier Cárdenes Medina <ricardo.cardenes@gmail.com> 90 Jeremy Carbaugh <jcarbaugh@gmail.com> 90 91 Graham Carlyle <graham.carlyle@maplecroft.net> 91 92 Antonio Cavedoni <http://cavedoni.com/> 92 93 C8E -
docs/templates.txt
1763 1763 June 2006, and ``comment_date`` is a date instance for 08:00 on 1 June 2006, 1764 1764 then ``{{ comment_date|timesince:blog_date }}`` would return "8 hours". 1765 1765 1766 Comparing offset-naive and offset-aware datetimes will return an empty string. 1767 1766 1768 Minutes is the smallest unit used, and "0 minutes" will be returned for any 1767 1769 date that is in the future relative to the comparison point. 1768 1770 … … 1778 1780 the comparison point (instead of *now*). If ``from_date`` contains 22 June 1779 1781 2006, then ``{{ conference_date|timeuntil:from_date }}`` will return "1 week". 1780 1782 1783 Comparing offset-naive and offset-aware datetimes will return an empty string. 1784 1781 1785 Minutes is the smallest unit used, and "0 minutes" will be returned for any 1782 1786 date that is in the past relative to the comparison point. 1783 1787