Fixed #11321 -- Handle timezones correctly in conjunction with naturalday filter. Thanks, aarond10 and seocam.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@16072 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Jannis Leidel 2011-04-22 12:02:55 +00:00
parent ea248f0107
commit e744fec8f9
2 changed files with 36 additions and 4 deletions

View File

@ -84,6 +84,7 @@ def naturalday(value, arg=None):
formatted according to settings.DATE_FORMAT. formatted according to settings.DATE_FORMAT.
""" """
try: try:
tzinfo = getattr(value, 'tzinfo', None)
value = date(value.year, value.month, value.day) value = date(value.year, value.month, value.day)
except AttributeError: except AttributeError:
# Passed value wasn't a date object # Passed value wasn't a date object
@ -91,7 +92,8 @@ def naturalday(value, arg=None):
except ValueError: except ValueError:
# Date arguments out of range # Date arguments out of range
return value 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: if delta.days == 0:
return _(u'today') return _(u'today')
elif delta.days == 1: elif delta.days == 1:

View File

@ -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.template import Template, Context, add_to_builtins
from django.utils import unittest from django.utils import unittest
@ -8,6 +8,24 @@ from django.utils.html import escape
add_to_builtins('django.contrib.humanize.templatetags.humanize') 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): class HumanizeTests(unittest.TestCase):
def humanize_tester(self, test_list, result_list, method): def humanize_tester(self, test_list, result_list, method):
@ -97,7 +115,19 @@ class HumanizeTests(unittest.TestCase):
rendered = t.render(Context(locals())).strip() rendered = t.render(Context(locals())).strip()
self.assertTrue(u' hours ago' in rendered) self.assertTrue(u' hours ago' in rendered)
def test_naturalday_tz(self):
from django.contrib.humanize.templatetags.humanize import naturalday
if __name__ == '__main__': today = date.today()
unittest.main() 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)