Permalink
Browse files

Fixed #12998 -- Corrected handling of time zones in syndication frame…

…work.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@12791 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
freakboy3742 committed Mar 15, 2010
1 parent 2d57300 commit e89a5e06cfb627c61af449f9e58f24ce561f840b
Showing with 22 additions and 20 deletions.
  1. +2 −15 django/contrib/syndication/views.py
  2. +20 −5 tests/regressiontests/syndication/tests.py
@@ -158,21 +158,8 @@ def get_feed(self, obj, request):
pubdate = self.__get_dynamic_attr('item_pubdate', item)
if pubdate and not pubdate.tzinfo:
- now = datetime.datetime.now()
- utcnow = datetime.datetime.utcnow()
-
- # Must always subtract smaller time from larger time here.
- if utcnow > now:
- sign = -1
- tzDifference = (utcnow - now)
- else:
- sign = 1
- tzDifference = (now - utcnow)
-
- # Round the timezone offset to the nearest half hour.
- tzOffsetMinutes = sign * ((tzDifference.seconds / 60 + 15) / 30) * 30
- tzOffset = datetime.timedelta(minutes=tzOffsetMinutes)
- pubdate = pubdate.replace(tzinfo=tzinfo.FixedOffset(tzOffset))
+ ltz = tzinfo.LocalTimezone(pubdate)
+ pubdate = pubdate.replace(tzinfo=ltz)
feed.add_item(
title = title,
@@ -3,6 +3,7 @@
from django.core.exceptions import ImproperlyConfigured
from django.test import TestCase
from django.utils import tzinfo
+from django.utils.feedgenerator import rfc2822_date, rfc3339_date
from models import Entry
from xml.dom import minidom
@@ -55,13 +56,19 @@ def test_rss2_feed(self):
chan_elem = feed.getElementsByTagName('channel')
self.assertEqual(len(chan_elem), 1)
chan = chan_elem[0]
+
+ # Find the last build date
+ d = Entry.objects.latest('date').date
+ ltz = tzinfo.LocalTimezone(d)
+ last_build_date = rfc2822_date(d.replace(tzinfo=ltz))
+
self.assertChildNodes(chan, ['title', 'link', 'description', 'language', 'lastBuildDate', 'item', 'atom:link', 'ttl', 'copyright', 'category'])
self.assertChildNodeContent(chan, {
'title': 'My blog',
'description': 'A more thorough description of my blog.',
'link': 'http://example.com/blog/',
'language': 'en',
- 'lastBuildDate': 'Thu, 03 Jan 2008 13:30:00 -0600',
+ 'lastBuildDate': last_build_date,
#'atom:link': '',
'ttl': '600',
'copyright': 'Copyright (c) 2007, Sally Smith',
@@ -80,14 +87,19 @@ def test_rss2_feed(self):
'http://example.com/syndication/rss2/'
)
+ # Find the pubdate of the first feed item
+ d = Entry.objects.get(pk=1).date
+ ltz = tzinfo.LocalTimezone(d)
+ pub_date = rfc2822_date(d.replace(tzinfo=ltz))
+
items = chan.getElementsByTagName('item')
self.assertEqual(len(items), Entry.objects.count())
self.assertChildNodeContent(items[0], {
'title': 'My first entry',
'description': 'Overridden description: My first entry',
'link': 'http://example.com/blog/1/',
'guid': 'http://example.com/blog/1/',
- 'pubDate': 'Tue, 01 Jan 2008 12:30:00 -0600',
+ 'pubDate': pub_date,
'author': 'test@example.com (Sally Smith)',
})
self.assertCategories(items[0], ['python', 'testing']);
@@ -198,9 +210,12 @@ def test_naive_datetime_conversion(self):
response = self.client.get('/syndication/naive-dates/')
doc = minidom.parseString(response.content)
updated = doc.getElementsByTagName('updated')[0].firstChild.wholeText
- tz = tzinfo.LocalTimezone(datetime.datetime.now())
- now = datetime.datetime.now(tz)
- self.assertEqual(updated[-6:], str(now)[-6:])
+
+ d = Entry.objects.latest('date').date
+ ltz = tzinfo.LocalTimezone(d)
+ latest = rfc3339_date(d.replace(tzinfo=ltz))
+
+ self.assertEqual(updated, latest)
def test_aware_datetime_conversion(self):
"""

0 comments on commit e89a5e0

Please sign in to comment.