Ticket #7201: 7201.4.diff

File 7201.4.diff, 7.9 KB (added by Jeremy Carbaugh, 12 years ago)

Updated to work with changes introduced in [8535]

  • django/utils/timesince.py

     
    2727    )
    2828    # Convert datetime.date to datetime.datetime for comparison
    2929    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)   
    4031
     32    if not now:
     33        if d.tzinfo:
     34            now = datetime.datetime.now(LocalTimezone(d))
     35        else:
     36            now = datetime.datetime.now()
     37
    4138    # ignore microsecond part of 'd' since we removed it from 'now'
    4239    delta = now - (d - datetime.timedelta(0, 0, d.microsecond))
    4340    since = delta.days * 24 * 60 * 60 + delta.seconds
     
    6259    Like timesince, but returns a string measuring the time until
    6360    the given time.
    6461    """
    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()
    6767    return timesince(now, d)
  • django/template/defaultfilters.py

     
    642642    from django.utils.timesince import timesince
    643643    if not value:
    644644        return u''
    645     if arg:
    646         return timesince(value, arg)
    647     return timesince(value)
     645    try:
     646        if arg:
     647            return timesince(value, arg)       
     648        return timesince(value)
     649    except (ValueError, TypeError):
     650        return u''
    648651timesince.is_safe = False
    649652
    650653def timeuntil(value, arg=None):
    651654    """Formats a date as the time until that date (i.e. "4 days, 6 hours")."""
    652     from django.utils.timesince import timesince
     655    from django.utils.timesince import timeuntil
    653656    from datetime import datetime
    654657    if not value:
    655658        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''
    659663timeuntil.is_safe = False
    660664
    661665###################
  • tests/regressiontests/templates/filters.py

     
    99
    1010from datetime import datetime, timedelta
    1111
    12 from django.utils.tzinfo import LocalTimezone
     12from django.utils.tzinfo import LocalTimezone, FixedOffset
    1313from django.utils.safestring import mark_safe
    1414
    1515# These two classes are used to test auto-escaping of __unicode__ output.
     
    2020class SafeClass:
    2121    def __unicode__(self):
    2222        return mark_safe(u'you > me')
    23 
     23       
    2424# RESULT SYNTAX --
    2525# 'template_name': ('template contents', 'context dict',
    2626#                   'expected string output' or Exception class)
    2727def get_filter_tests():
    2828    now = datetime.now()
    2929    now_tz = datetime.now(LocalTimezone(now))
     30    now_tz_i = datetime.now(FixedOffset((3 * 60) + 15)) # imaginary time zone
    3031    return {
    3132        # Default compare with datetime.now()
    3233        'filter-timesince01' : ('{{ a|timesince }}', {'a': datetime.now() + timedelta(minutes=-1, seconds = -10)}, '1 minute'),
     
    4647        'filter-timesince09': ('{{ later|timesince }}', { 'later': now + timedelta(days=7) }, '0 minutes'),
    4748        'filter-timesince10': ('{{ later|timesince:now }}', { 'now': now, 'later': now + timedelta(days=7) }, '0 minutes'),
    4849
     50        # Ensures that differing timezones are calculated correctly
     51        'filter-timesince11' : ('{{ a|timesince }}', {'a': now}, '0 minutes'),
     52        'filter-timesince12' : ('{{ a|timesince }}', {'a': now_tz}, '0 minutes'),
     53        'filter-timesince13' : ('{{ a|timesince }}', {'a': now_tz_i}, '0 minutes'),
     54        'filter-timesince14' : ('{{ a|timesince:b }}', {'a': now_tz, 'b': now_tz_i}, '0 minutes'),
     55        'filter-timesince15' : ('{{ a|timesince:b }}', {'a': now, 'b': now_tz_i}, ''),
     56        'filter-timesince16' : ('{{ a|timesince:b }}', {'a': now_tz_i, 'b': now}, ''),
     57       
    4958        # Default compare with datetime.now()
    5059        'filter-timeuntil01' : ('{{ a|timeuntil }}', {'a':datetime.now() + timedelta(minutes=2, seconds = 10)}, '2 minutes'),
    5160        'filter-timeuntil02' : ('{{ a|timeuntil }}', {'a':(datetime.now() + timedelta(days=1, seconds = 10))}, '1 day'),
     
    6170        'filter-timeuntil08': ('{{ later|timeuntil }}', { 'later': now + timedelta(days=7, hours=1) }, '1 week'),
    6271        'filter-timeuntil09': ('{{ later|timeuntil:now }}', { 'now': now, 'later': now + timedelta(days=7) }, '1 week'),
    6372
     73        # Ensures that differing timezones are calculated correctly
     74        'filter-timeuntil10' : ('{{ a|timeuntil }}', {'a': now_tz_i}, '0 minutes'),
     75        'filter-timeuntil11' : ('{{ a|timeuntil:b }}', {'a': now_tz_i, 'b': now_tz}, '0 minutes'),
    6476
    6577        'filter-addslash01': ("{% autoescape off %}{{ a|addslashes }} {{ b|addslashes }}{% endautoescape %}", {"a": "<a>'", "b": mark_safe("<a>'")}, ur"<a>\' <a>\'"),
    6678        'filter-addslash02': ("{{ a|addslashes }} {{ b|addslashes }}", {"a": "<a>'", "b": mark_safe("<a>'")}, ur"&lt;a&gt;\&#39; <a>\'"),
  • tests/regressiontests/utils/timesince.py

     
    11"""
    22>>> 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
    45
    56>>> t = datetime(2007, 8, 14, 13, 46, 0)
    67
     
    7475u'0 minutes'
    7576>>> timesince(t, t-4*oneday-5*oneminute)
    7677u'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)
     84u'0 minutes'
     85>>> timesince(now_tz)
     86u'0 minutes'
     87>>> timeuntil(now_tz, now_tz_i)
     88u'0 minutes'
    7789"""
  • AUTHORS

     
    8888    Juan Manuel Caicedo <juan.manuel.caicedo@gmail.com>
    8989    Trevor Caira <trevor@caira.com>
    9090    Ricardo Javier Cárdenes Medina <ricardo.cardenes@gmail.com>
     91    Jeremy Carbaugh <jcarbaugh@gmail.com>
    9192    Graham Carlyle <graham.carlyle@maplecroft.net>
    9293    Antonio Cavedoni <http://cavedoni.com/>
    9394    C8E
  • docs/ref/templates/builtins.txt

     
    13321332June 2006, and ``comment_date`` is a date instance for 08:00 on 1 June 2006,
    13331333then ``{{ blog_date|timesince:comment_date }}`` would return "8 hours".
    13341334
     1335Comparing offset-naive and offset-aware datetimes will return an empty string.
     1336
    13351337Minutes is the smallest unit used, and "0 minutes" will be returned for any
    13361338date that is in the future relative to the comparison point.
    13371339
     
    13491351the comparison point (instead of *now*). If ``from_date`` contains 22 June
    135013522006, then ``{{ conference_date|timeuntil:from_date }}`` will return "1 week".
    13511353
     1354Comparing offset-naive and offset-aware datetimes will return an empty string.
     1355
    13521356Minutes is the smallest unit used, and "0 minutes" will be returned for any
    13531357date that is in the past relative to the comparison point.
    13541358
Back to Top