Commit f31fbbae authored by Ketan Bhatt's avatar Ketan Bhatt Committed by Tim Graham
Browse files

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

parent 92107522
Loading
Loading
Loading
Loading
+4 −2
Original line number Diff line number Diff line
@@ -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):
+6 −1
Original line number Diff line number Diff line
@@ -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``
-------------
+5 −1
Original line number Diff line number Diff line
@@ -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()
  <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:

+9 −3
Original line number Diff line number Diff line
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('<atom:link', feed_content)
        self.assertIn('href="/feed/"', 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)