Ticket #11321: 11321-r11173.2.diff

File 11321-r11173.2.diff, 3.4 KB (added by Sergio Oliveira, 15 years ago)
  • django/contrib/humanize/templatetags/humanize.py

     
    22from django.utils.encoding import force_unicode
    33from django import template
    44from django.template import defaultfilters
    5 from datetime import date
     5from datetime import date, datetime
    66import re
    77
    88register = template.Library()
     
    8080    present day returns representing string. Otherwise, returns a string
    8181    formatted according to settings.DATE_FORMAT.
    8282    """
    83     try:
     83    try:
     84        tzinfo = getattr(value, 'tzinfo', None)
    8485        value = date(value.year, value.month, value.day)
    8586    except AttributeError:
    8687        # Passed value wasn't a date object
     
    8889    except ValueError:
    8990        # Date arguments out of range
    9091        return value
    91     delta = value - date.today()
     92    delta = value - datetime.now(tzinfo).\
     93                    replace(microsecond=0, second=0, minute=0, hour=0).date()
    9294    if delta.days == 0:
    9395        return _(u'today')
    9496    elif delta.days == 1:
  • tests/regressiontests/humanize/timezone.py

     
     1from datetime import tzinfo, timedelta
     2
     3class FixedOffset(tzinfo):
     4    """Fixed offset in hours east from UTC."""
     5
     6    def __init__(self, offset, name):
     7        self.__offset = timedelta(hours=offset)
     8        self.__name = name
     9
     10    def utcoffset(self, dt):
     11        return self.__offset
     12
     13    def tzname(self, dt):
     14        return self.__name
     15
     16    def dst(self, dt):
     17        return timedelta(0)
  • tests/regressiontests/humanize/tests.py

     
    5959        tomorrow = today + timedelta(days=1)
    6060        someday = today - timedelta(days=10)
    6161        notdate = u"I'm not a date value"
    62 
     62       
    6363        test_list = (today, yesterday, tomorrow, someday, notdate)
    6464        someday_result = defaultfilters.date(someday)
    6565        result_list = (_(u'today'), _(u'yesterday'), _(u'tomorrow'),
    6666                       someday_result, u"I'm not a date value")
    6767        self.humanize_tester(test_list, result_list, 'naturalday')
     68   
     69    def test_naturalday_tz(self):
     70        from datetime import datetime
     71        from django.contrib.humanize.templatetags.humanize import naturalday
     72        from timezone import FixedOffset
     73       
     74        today = date.today()
     75       
     76        tz_one = FixedOffset(-12, 'TzOne')
     77        tz_two = FixedOffset(12, 'TzTwo')
    6878
     79        # Can be today or yesterday
     80        date_one = datetime(today.year, today.month, today.day, tzinfo=tz_one)
     81        naturalday_one = naturalday(date_one)
     82        # Can be today or tomorrow
     83        date_two = datetime(today.year, today.month, today.day, tzinfo=tz_two)
     84        naturalday_two = naturalday(date_two)
     85       
     86        # As 24h of difference they will never be the same   
     87        self.assertNotEqual(naturalday_one, naturalday_two)
     88
    6989if __name__ == '__main__':
    7090    unittest.main()
    7191
Back to Top