diff --git a/django/contrib/humanize/templatetags/humanize.py b/django/contrib/humanize/templatetags/humanize.py index 0c6d389e344..5f3e2a7aed4 100644 --- a/django/contrib/humanize/templatetags/humanize.py +++ b/django/contrib/humanize/templatetags/humanize.py @@ -84,6 +84,7 @@ def naturalday(value, arg=None): formatted according to settings.DATE_FORMAT. """ try: + tzinfo = getattr(value, 'tzinfo', None) value = date(value.year, value.month, value.day) except AttributeError: # Passed value wasn't a date object @@ -91,7 +92,8 @@ def naturalday(value, arg=None): except ValueError: # Date arguments out of range return value - delta = value - date.today() + today = datetime.now(tzinfo).replace(microsecond=0, second=0, minute=0, hour=0) + delta = value - today.date() if delta.days == 0: return _(u'today') elif delta.days == 1: diff --git a/tests/regressiontests/humanize/tests.py b/tests/regressiontests/humanize/tests.py index 476de786ce6..ade0d1a4d4a 100644 --- a/tests/regressiontests/humanize/tests.py +++ b/tests/regressiontests/humanize/tests.py @@ -1,4 +1,4 @@ -from datetime import timedelta, date, datetime +from datetime import timedelta, date, datetime, tzinfo, timedelta from django.template import Template, Context, add_to_builtins from django.utils import unittest @@ -8,6 +8,24 @@ from django.utils.html import escape add_to_builtins('django.contrib.humanize.templatetags.humanize') + +class FixedOffset(tzinfo): + """Fixed offset in hours east from UTC.""" + + def __init__(self, offset, name): + self.__offset = timedelta(hours=offset) + self.__name = name + + def utcoffset(self, dt): + return self.__offset + + def tzname(self, dt): + return self.__name + + def dst(self, dt): + return timedelta(0) + + class HumanizeTests(unittest.TestCase): def humanize_tester(self, test_list, result_list, method): @@ -97,7 +115,19 @@ class HumanizeTests(unittest.TestCase): rendered = t.render(Context(locals())).strip() self.assertTrue(u' hours ago' in rendered) + def test_naturalday_tz(self): + from django.contrib.humanize.templatetags.humanize import naturalday -if __name__ == '__main__': - unittest.main() + today = date.today() + tz_one = FixedOffset(-12, 'TzOne') + tz_two = FixedOffset(12, 'TzTwo') + # Can be today or yesterday + date_one = datetime(today.year, today.month, today.day, tzinfo=tz_one) + naturalday_one = naturalday(date_one) + # Can be today or tomorrow + date_two = datetime(today.year, today.month, today.day, tzinfo=tz_two) + naturalday_two = naturalday(date_two) + + # As 24h of difference they will never be the same + self.assertNotEqual(naturalday_one, naturalday_two)