Fixed #26653 -- Made SyndicationFeed.latest_post_date() return time in UTC.

This commit is contained in:
Ketan Bhatt 2016-05-24 22:20:20 +05:30 committed by Tim Graham
parent 92107522ed
commit f31fbbae1a
4 changed files with 24 additions and 7 deletions

View File

@ -31,6 +31,7 @@ from django.utils.deprecation import RemovedInDjango20Warning
from django.utils.encoding import force_text, iri_to_uri from django.utils.encoding import force_text, iri_to_uri
from django.utils.six import StringIO from django.utils.six import StringIO
from django.utils.six.moves.urllib.parse import urlparse from django.utils.six.moves.urllib.parse import urlparse
from django.utils.timezone import utc
from django.utils.xmlutils import SimplerXMLGenerator from django.utils.xmlutils import SimplerXMLGenerator
@ -211,7 +212,7 @@ class SyndicationFeed(object):
def latest_post_date(self): def latest_post_date(self):
""" """
Returns the latest item's pubdate or updateddate. If no items Returns the latest item's pubdate or updateddate. If no items
have either of these attributes this returns the current date/time. have either of these attributes this returns the current UTC date/time.
""" """
latest_date = None latest_date = None
date_keys = ('updateddate', 'pubdate') date_keys = ('updateddate', 'pubdate')
@ -223,7 +224,8 @@ class SyndicationFeed(object):
if latest_date is None or item_date > latest_date: if latest_date is None or item_date > latest_date:
latest_date = item_date latest_date = item_date
return latest_date or datetime.datetime.now() # datetime.now(tz=utc) is slower, as documented in django.utils.timezone.now
return latest_date or datetime.datetime.utcnow().replace(tzinfo=utc)
class Enclosure(object): class Enclosure(object):

View File

@ -397,7 +397,12 @@ https://web.archive.org/web/20110718035220/http://diveintomark.org/archives/2004
Returns the latest ``pubdate`` or ``updateddate`` for all items in the Returns the latest ``pubdate`` or ``updateddate`` for all items in the
feed. If no items have either of these attributes this returns the feed. If no items have either of these attributes this returns the
current date/time. current UTC date/time.
.. versionchanged:: 1.11
In older versions, it returned the current date/time without any
timezone information.
``Enclosure`` ``Enclosure``
------------- -------------

View File

@ -207,7 +207,11 @@ Database backend API
Miscellaneous Miscellaneous
------------- -------------
* ... * If no items in the feed have a ``pubdate`` or ``updateddate`` attribute,
:meth:`SyndicationFeed.latest_post_date()
<django.utils.feedgenerator.SyndicationFeed.latest_post_date>` now returns
the current UTC date/time, instead of a datetime without any timezone
information.
.. _deprecated-features-1.11: .. _deprecated-features-1.11:

View File

@ -1,13 +1,13 @@
from __future__ import unicode_literals from __future__ import unicode_literals
import datetime import datetime
import unittest
from django.test import SimpleTestCase
from django.utils import feedgenerator from django.utils import feedgenerator
from django.utils.timezone import get_fixed_timezone from django.utils.timezone import get_fixed_timezone, utc
class FeedgeneratorTest(unittest.TestCase): class FeedgeneratorTest(SimpleTestCase):
""" """
Tests for the low-level syndication feed framework. Tests for the low-level syndication feed framework.
""" """
@ -121,3 +121,9 @@ class FeedgeneratorTest(unittest.TestCase):
self.assertIn('<atom:link', feed_content) self.assertIn('<atom:link', feed_content)
self.assertIn('href="/feed/"', feed_content) self.assertIn('href="/feed/"', feed_content)
self.assertIn('rel="self"', feed_content) self.assertIn('rel="self"', feed_content)
def test_latest_post_date_returns_utc_time(self):
for use_tz in (True, False):
with self.settings(USE_TZ=use_tz):
rss_feed = feedgenerator.Rss201rev2Feed('title', 'link', 'description')
self.assertEqual(rss_feed.latest_post_date().tzinfo, utc)