Skip to content

Commit

Permalink
Fixed #7016: use correct time zones for Atom feeds. Thanks, Chris Cah…
Browse files Browse the repository at this point in the history
…oon.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@8216 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information
jacobian committed Aug 5, 2008
1 parent 8e24b37 commit 34008aa
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 6 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
23 changes: 22 additions & 1 deletion django/contrib/syndication/feeds.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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,
Expand Down
17 changes: 14 additions & 3 deletions django/utils/feedgenerator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down
9 changes: 7 additions & 2 deletions django/utils/tzinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 34008aa

Please sign in to comment.