Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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
Jannis Leidel authored April 22, 2011
4  django/contrib/humanize/templatetags/humanize.py
@@ -84,6 +84,7 @@ def naturalday(value, arg=None):
84 84
     formatted according to settings.DATE_FORMAT.
85 85
     """
86 86
     try:
  87
+        tzinfo = getattr(value, 'tzinfo', None)
87 88
         value = date(value.year, value.month, value.day)
88 89
     except AttributeError:
89 90
         # Passed value wasn't a date object
@@ -91,7 +92,8 @@ def naturalday(value, arg=None):
91 92
     except ValueError:
92 93
         # Date arguments out of range
93 94
         return value
94  
-    delta = value - date.today()
  95
+    today = datetime.now(tzinfo).replace(microsecond=0, second=0, minute=0, hour=0)
  96
+    delta = value - today.date()
95 97
     if delta.days == 0:
96 98
         return _(u'today')
97 99
     elif delta.days == 1:
38  tests/regressiontests/humanize/tests.py
... ...
@@ -1,4 +1,4 @@
1  
-from datetime import timedelta, date, datetime
  1
+from datetime import timedelta, date, datetime, tzinfo, timedelta
2 2
 
3 3
 from django.template import Template, Context, add_to_builtins
4 4
 from django.utils import unittest
@@ -8,6 +8,24 @@
8 8
 
9 9
 add_to_builtins('django.contrib.humanize.templatetags.humanize')
10 10
 
  11
+
  12
+class FixedOffset(tzinfo):
  13
+    """Fixed offset in hours east from UTC."""
  14
+
  15
+    def __init__(self, offset, name):
  16
+        self.__offset = timedelta(hours=offset)
  17
+        self.__name = name
  18
+
  19
+    def utcoffset(self, dt):
  20
+        return self.__offset
  21
+
  22
+    def tzname(self, dt):
  23
+        return self.__name
  24
+
  25
+    def dst(self, dt):
  26
+        return timedelta(0)
  27
+
  28
+
11 29
 class HumanizeTests(unittest.TestCase):
12 30
 
13 31
     def humanize_tester(self, test_list, result_list, method):
@@ -97,7 +115,19 @@ def test_naturaltime(self):
97 115
         rendered = t.render(Context(locals())).strip()
98 116
         self.assertTrue(u' hours ago' in rendered)
99 117
 
  118
+    def test_naturalday_tz(self):
  119
+        from django.contrib.humanize.templatetags.humanize import naturalday
100 120
 
101  
-if __name__ == '__main__':
102  
-    unittest.main()
103  
-
  121
+        today = date.today()
  122
+        tz_one = FixedOffset(-12, 'TzOne')
  123
+        tz_two = FixedOffset(12, 'TzTwo')
  124
+
  125
+        # Can be today or yesterday
  126
+        date_one = datetime(today.year, today.month, today.day, tzinfo=tz_one)
  127
+        naturalday_one = naturalday(date_one)
  128
+        # Can be today or tomorrow
  129
+        date_two = datetime(today.year, today.month, today.day, tzinfo=tz_two)
  130
+        naturalday_two = naturalday(date_two)
  131
+
  132
+        # As 24h of difference they will never be the same
  133
+        self.assertNotEqual(naturalday_one, naturalday_two)

0 notes on commit e744fec

Please sign in to comment.
Something went wrong with that request. Please try again.