Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixed #11321 -- Handle timezones correctly in conjunction with natura…

…lday filter. Thanks, aarond10 and seocam.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@16072 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit e744fec8f9c47c8d45045d36df202deb4b6c3b9f 1 parent ea248f0
@jezdez jezdez authored
View
4 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:
View
38 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 @@
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 @@ def test_naturaltime(self):
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)
Please sign in to comment.
Something went wrong with that request. Please try again.