Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixed #7016: use correct time zones for Atom feeds. Thanks, Chris Cah…

…oon.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@8216 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 34008aaeb0fc62d782735ff62c745a658888521e 1 parent 8e24b37
@jacobian jacobian authored
View
1  AUTHORS
@@ -83,6 +83,7 @@ answer newbie questions, and generally made Django that much better:
btoll@bestweb.net
Jonathan Buchanan <jonathan.buchanan@gmail.com>
Keith Bussell <kbussell@gmail.com>
+ Chris Cahoo <chris.cahoo@gmail.com>
Juan Manuel Caicedo <juan.manuel.caicedo@gmail.com>
Trevor Caira <trevor@caira.com>
Ricardo Javier Cárdenes Medina <ricardo.cardenes@gmail.com>
View
23 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 @@ def get_feed(self, url=None):
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,
View
17 django/utils/feedgenerator.py
@@ -22,14 +22,25 @@
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')
View
9 django/utils/tzinfo.py
@@ -14,8 +14,13 @@
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

0 comments on commit 34008aa

Please sign in to comment.
Something went wrong with that request. Please try again.