From f31fbbae1ab1572e1637fc5ca9ae4bae66679294 Mon Sep 17 00:00:00 2001 From: Ketan Bhatt Date: Tue, 24 May 2016 22:20:20 +0530 Subject: [PATCH] Fixed #26653 -- Made SyndicationFeed.latest_post_date() return time in UTC. --- django/utils/feedgenerator.py | 6 ++++-- docs/ref/utils.txt | 7 ++++++- docs/releases/1.11.txt | 6 +++++- tests/utils_tests/test_feedgenerator.py | 12 +++++++++--- 4 files changed, 24 insertions(+), 7 deletions(-) diff --git a/django/utils/feedgenerator.py b/django/utils/feedgenerator.py index 3a91af3815..aeb054f4e9 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 d97a0becb1..2fc868e143 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 6a1f1e0241..969316785c 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 21c88f93ce..59800b8bcd 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('