diff --git a/django/utils/feedgenerator.py b/django/utils/feedgenerator.py index 3a91af38154..aeb054f4e9b 100644 --- a/django/utils/feedgenerator.py +++ b/django/utils/feedgenerator.py @@ -31,6 +31,7 @@ from django.utils.deprecation import RemovedInDjango20Warning from django.utils.encoding import force_text, iri_to_uri from django.utils.six import StringIO from django.utils.six.moves.urllib.parse import urlparse +from django.utils.timezone import utc from django.utils.xmlutils import SimplerXMLGenerator @@ -211,7 +212,7 @@ class SyndicationFeed(object): def latest_post_date(self): """ 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 date_keys = ('updateddate', 'pubdate') @@ -223,7 +224,8 @@ class SyndicationFeed(object): if latest_date is None or item_date > latest_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): diff --git a/docs/ref/utils.txt b/docs/ref/utils.txt index d97a0becb18..2fc868e143a 100644 --- a/docs/ref/utils.txt +++ b/docs/ref/utils.txt @@ -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 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`` ------------- diff --git a/docs/releases/1.11.txt b/docs/releases/1.11.txt index 6a1f1e02419..969316785c1 100644 --- a/docs/releases/1.11.txt +++ b/docs/releases/1.11.txt @@ -207,7 +207,11 @@ Database backend API Miscellaneous ------------- -* ... +* If no items in the feed have a ``pubdate`` or ``updateddate`` attribute, + :meth:`SyndicationFeed.latest_post_date() + ` now returns + the current UTC date/time, instead of a datetime without any timezone + information. .. _deprecated-features-1.11: diff --git a/tests/utils_tests/test_feedgenerator.py b/tests/utils_tests/test_feedgenerator.py index 21c88f93ce6..59800b8bcd5 100644 --- a/tests/utils_tests/test_feedgenerator.py +++ b/tests/utils_tests/test_feedgenerator.py @@ -1,13 +1,13 @@ from __future__ import unicode_literals import datetime -import unittest +from django.test import SimpleTestCase 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. """ @@ -121,3 +121,9 @@ class FeedgeneratorTest(unittest.TestCase): self.assertIn('