summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJacob Kaplan-Moss <jacob@jacobian.org>2008-08-05 17:38:49 +0000
committerJacob Kaplan-Moss <jacob@jacobian.org>2008-08-05 17:38:49 +0000
commit34008aaeb0fc62d782735ff62c745a658888521e (patch)
treeae519b39a06040a0d04d903ad0100f783f02ae1d
parent8e24b3761084618fa259f6499b1a9110d19a28c2 (diff)
Fixed #7016: use correct time zones for Atom feeds. Thanks, Chris Cahoon.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@8216 bcc190cf-cafb-0310-a4f2-bffc1f526a37
-rw-r--r--AUTHORS1
-rw-r--r--django/contrib/syndication/feeds.py23
-rw-r--r--django/utils/feedgenerator.py17
-rw-r--r--django/utils/tzinfo.py9
4 files changed, 44 insertions, 6 deletions
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 <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>
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