Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #9957: feeds now respect time zone information provided by the …

…pub date.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@10435 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 2c3c1f3ec4d87d35a8c466471b64cb39f1b097e7 1 parent a6356ca
Jacob Kaplan-Moss authored April 07, 2009
2  django/contrib/syndication/feeds.py
@@ -145,7 +145,7 @@ def get_feed(self, url=None):
145 145
                 author_email = author_link = None
146 146
 
147 147
             pubdate = self.__get_dynamic_attr('item_pubdate', item)
148  
-            if pubdate:
  148
+            if pubdate and not pubdate.tzinfo:
149 149
                 now = datetime.now()
150 150
                 utcnow = datetime.utcnow()
151 151
 
18  tests/regressiontests/syndication/feeds.py
... ...
@@ -1,6 +1,7 @@
1 1
 from django.core.exceptions import ObjectDoesNotExist
2 2
 from django.contrib.syndication import feeds
3 3
 from django.utils.feedgenerator import Atom1Feed
  4
+from django.utils import tzinfo
4 5
 
5 6
 class ComplexFeed(feeds.Feed):
6 7
     def get_object(self, bits):
@@ -46,3 +47,20 @@ def add_item_elements(self, handler, item):
46 47
     
47 48
 class TestCustomFeed(TestAtomFeed):
48 49
     feed_type = MyCustomAtom1Feed
  50
+    
  51
+class NaiveDatesFeed(TestAtomFeed):
  52
+    """
  53
+    A feed with naive (non-timezone-aware) dates.
  54
+    """
  55
+    def item_pubdate(self, item):
  56
+        return item.date
  57
+        
  58
+class TZAwareDatesFeed(TestAtomFeed):
  59
+    """
  60
+    A feed with timezone-aware dates.
  61
+    """
  62
+    def item_pubdate(self, item):
  63
+        # Provide a weird offset so that the test can know it's getting this
  64
+        # specific offset and not accidentally getting on from 
  65
+        # settings.TIME_ZONE.
  66
+        return item.date.replace(tzinfo=tzinfo.FixedOffset(42))
27  tests/regressiontests/syndication/tests.py
... ...
@@ -1,8 +1,10 @@
1 1
 # -*- coding: utf-8 -*-
2 2
 
  3
+import datetime
3 4
 from xml.dom import minidom
4 5
 from django.test import TestCase
5 6
 from django.test.client import Client
  7
+from django.utils import tzinfo
6 8
 from models import Entry
7 9
 try:
8 10
     set
@@ -91,4 +93,27 @@ def test_title_escaping(self):
91 93
             link = item.getElementsByTagName('link')[0]
92 94
             if link.firstChild.wholeText == 'http://example.com/blog/4/':
93 95
                 title = item.getElementsByTagName('title')[0]
94  
-                self.assertEquals(title.firstChild.wholeText, u'A & B < C > D')
  96
+                self.assertEquals(title.firstChild.wholeText, u'A & B < C > D')
  97
+                
  98
+    def test_naive_datetime_conversion(self):
  99
+        """
  100
+        Test that datetimes are correctly converted to the local time zone.
  101
+        """
  102
+        # Naive date times passed in get converted to the local time zone, so
  103
+        # check the recived zone offset against the local offset.
  104
+        response = self.client.get('/syndication/feeds/naive-dates/')
  105
+        doc = minidom.parseString(response.content)
  106
+        updated = doc.getElementsByTagName('updated')[0].firstChild.wholeText        
  107
+        tz = tzinfo.LocalTimezone(datetime.datetime.now())
  108
+        now = datetime.datetime.now(tz)
  109
+        self.assertEqual(updated[-6:], str(now)[-6:])
  110
+        
  111
+    def test_aware_datetime_conversion(self):
  112
+        """
  113
+        Test that datetimes with timezones don't get trodden on.
  114
+        """
  115
+        response = self.client.get('/syndication/feeds/aware-dates/')
  116
+        doc = minidom.parseString(response.content)
  117
+        updated = doc.getElementsByTagName('updated')[0].firstChild.wholeText
  118
+        self.assertEqual(updated[-6:], '+00:42')
  119
+        
13  tests/regressiontests/syndication/urls.py
... ...
@@ -1,12 +1,13 @@
1  
-from feeds import TestRssFeed, TestAtomFeed, TestCustomFeed, ComplexFeed
  1
+import feeds
2 2
 from django.conf.urls.defaults import patterns
3 3
 
4 4
 feed_dict = {
5  
-    'complex': ComplexFeed,
6  
-    'rss': TestRssFeed,
7  
-    'atom': TestAtomFeed,
8  
-    'custom': TestCustomFeed,
9  
-    
  5
+    'complex': feeds.ComplexFeed,
  6
+    'rss': feeds.TestRssFeed,
  7
+    'atom': feeds.TestAtomFeed,
  8
+    'custom': feeds.TestCustomFeed,
  9
+    'naive-dates': feeds.NaiveDatesFeed,
  10
+    'aware-dates': feeds.TZAwareDatesFeed,    
10 11
 }
11 12
 urlpatterns = patterns('',
12 13
     (r'^feeds/(?P<url>.*)/$', 'django.contrib.syndication.views.feed', {'feed_dict': feed_dict})

0 notes on commit 2c3c1f3

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