diff --git a/AUTHORS b/AUTHORS index a9f55836e6..8431e55634 100644 --- a/AUTHORS +++ b/AUTHORS @@ -83,6 +83,7 @@ answer newbie questions, and generally made Django that much better: btoll@bestweb.net Jonathan Buchanan Keith Bussell + Chris Cahoo Juan Manuel Caicedo Trevor Caira Ricardo Javier Cárdenes Medina diff --git a/django/contrib/syndication/feeds.py b/django/contrib/syndication/feeds.py index 63d2431883..a802171800 100644 --- a/django/contrib/syndication/feeds.py +++ b/django/contrib/syndication/feeds.py @@ -1,7 +1,10 @@ +from datetime import datetime, timedelta + from django.core.exceptions import ImproperlyConfigured, ObjectDoesNotExist from django.template import loader, Template, TemplateDoesNotExist from django.contrib.sites.models import Site, RequestSite from django.utils import feedgenerator +from django.utils.tzinfo import FixedOffset from django.utils.encoding import smart_unicode, iri_to_uri from django.conf import settings from django.template import RequestContext @@ -124,13 +127,31 @@ class Feed(object): author_link = self.__get_dynamic_attr('item_author_link', item) else: author_email = author_link = None + + pubdate = self.__get_dynamic_attr('item_pubdate', item) + now = datetime.now() + utcnow = 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 = timedelta(minutes=tzOffsetMinutes) + pubdate = pubdate.replace(tzinfo=FixedOffset(tzOffset)) + feed.add_item( title = title_tmp.render(RequestContext(self.request, {'obj': item, 'site': current_site})), link = link, description = description_tmp.render(RequestContext(self.request, {'obj': item, 'site': current_site})), unique_id = self.__get_dynamic_attr('item_guid', item, link), enclosure = enc, - pubdate = self.__get_dynamic_attr('item_pubdate', item), + pubdate = pubdate, author_name = author_name, author_email = author_email, author_link = author_link, diff --git a/django/utils/feedgenerator.py b/django/utils/feedgenerator.py index 23400bb8e7..ce09c4fc27 100644 --- a/django/utils/feedgenerator.py +++ b/django/utils/feedgenerator.py @@ -22,14 +22,25 @@ http://diveintomark.org/archives/2004/02/04/incompatible-rss from django.utils.xmlutils import SimplerXMLGenerator from django.utils.encoding import force_unicode, iri_to_uri import datetime, re, time -import email.Utils def rfc2822_date(date): - return email.Utils.formatdate(time.mktime(date.timetuple())) + # We do this ourselves to be timezone aware, email.Utils is not tz aware. + if date.tzinfo: + time_str = date.strftime('%a, %d %b %Y %H:%M:%S ') + offset = date.tzinfo.utcoffset(date) + timezone = (offset.days * 24 * 60) + (offset.seconds / 60) + hour, minute = divmod(timezone, 60) + return time_str + "%+03d%02d" % (hour, minute) + else: + return date.strftime('%a, %d %b %Y %H:%M:%S -0000') def rfc3339_date(date): if date.tzinfo: - return date.strftime('%Y-%m-%dT%H:%M:%S%z') + time_str = date.strftime('%Y-%m-%dT%H:%M:%S') + offset = date.tzinfo.utcoffset(date) + timezone = (offset.days * 24 * 60) + (offset.seconds / 60) + hour, minute = divmod(timezone, 60) + return time_str + "%+03d:%02d" % (hour, minute) else: return date.strftime('%Y-%m-%dT%H:%M:%SZ') diff --git a/django/utils/tzinfo.py b/django/utils/tzinfo.py index 9352acd52a..383c09427e 100644 --- a/django/utils/tzinfo.py +++ b/django/utils/tzinfo.py @@ -14,8 +14,13 @@ except: class FixedOffset(tzinfo): "Fixed offset in minutes east from UTC." def __init__(self, offset): - self.__offset = timedelta(minutes=offset) - self.__name = u"%+03d%02d" % (offset // 60, offset % 60) + if isinstance(offset, timedelta): + self.__offset = offset + offset = self.__offset.seconds // 60 + else: + self.__offset = timedelta(minutes=offset) + + self.__name = u"%+03d%02d" % (offset / 60, offset % 60) def __repr__(self): return self.__name