Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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
Jacob Kaplan-Moss authored August 05, 2008
1  AUTHORS
@@ -83,6 +83,7 @@ answer newbie questions, and generally made Django that much better:
83 83
     btoll@bestweb.net
84 84
     Jonathan Buchanan <jonathan.buchanan@gmail.com>
85 85
     Keith Bussell <kbussell@gmail.com>
  86
+    Chris Cahoo <chris.cahoo@gmail.com>
86 87
     Juan Manuel Caicedo <juan.manuel.caicedo@gmail.com>
87 88
     Trevor Caira <trevor@caira.com>
88 89
     Ricardo Javier Cárdenes Medina <ricardo.cardenes@gmail.com>
23  django/contrib/syndication/feeds.py
... ...
@@ -1,7 +1,10 @@
  1
+from datetime import datetime, timedelta
  2
+
1 3
 from django.core.exceptions import ImproperlyConfigured, ObjectDoesNotExist
2 4
 from django.template import loader, Template, TemplateDoesNotExist
3 5
 from django.contrib.sites.models import Site, RequestSite
4 6
 from django.utils import feedgenerator
  7
+from django.utils.tzinfo import FixedOffset
5 8
 from django.utils.encoding import smart_unicode, iri_to_uri
6 9
 from django.conf import settings         
7 10
 from django.template import RequestContext
@@ -124,13 +127,31 @@ def get_feed(self, url=None):
124 127
                 author_link = self.__get_dynamic_attr('item_author_link', item)
125 128
             else:
126 129
                 author_email = author_link = None
  130
+
  131
+            pubdate = self.__get_dynamic_attr('item_pubdate', item)
  132
+            now = datetime.now()
  133
+            utcnow = datetime.utcnow()
  134
+
  135
+            # Must always subtract smaller time from larger time here.
  136
+            if utcnow > now:
  137
+                sign = -1
  138
+                tzDifference = (utcnow - now)
  139
+            else:
  140
+                sign = 1
  141
+                tzDifference = (now - utcnow)
  142
+
  143
+            # Round the timezone offset to the nearest half hour.
  144
+            tzOffsetMinutes = sign * ((tzDifference.seconds / 60 + 15) / 30) * 30
  145
+            tzOffset = timedelta(minutes=tzOffsetMinutes)
  146
+            pubdate = pubdate.replace(tzinfo=FixedOffset(tzOffset))
  147
+
127 148
             feed.add_item(
128 149
                 title = title_tmp.render(RequestContext(self.request, {'obj': item, 'site': current_site})),
129 150
                 link = link,
130 151
                 description = description_tmp.render(RequestContext(self.request, {'obj': item, 'site': current_site})),
131 152
                 unique_id = self.__get_dynamic_attr('item_guid', item, link),
132 153
                 enclosure = enc,
133  
-                pubdate = self.__get_dynamic_attr('item_pubdate', item),
  154
+                pubdate = pubdate,
134 155
                 author_name = author_name,
135 156
                 author_email = author_email,
136 157
                 author_link = author_link,
17  django/utils/feedgenerator.py
@@ -22,14 +22,25 @@
22 22
 from django.utils.xmlutils import SimplerXMLGenerator
23 23
 from django.utils.encoding import force_unicode, iri_to_uri
24 24
 import datetime, re, time
25  
-import email.Utils
26 25
 
27 26
 def rfc2822_date(date):
28  
-    return email.Utils.formatdate(time.mktime(date.timetuple()))
  27
+    # We do this ourselves to be timezone aware, email.Utils is not tz aware.
  28
+    if date.tzinfo:
  29
+        time_str = date.strftime('%a, %d %b %Y %H:%M:%S ')
  30
+        offset = date.tzinfo.utcoffset(date)
  31
+        timezone = (offset.days * 24 * 60) + (offset.seconds / 60)
  32
+        hour, minute = divmod(timezone, 60)
  33
+        return time_str + "%+03d%02d" % (hour, minute)
  34
+    else:
  35
+        return date.strftime('%a, %d %b %Y %H:%M:%S -0000')
29 36
 
30 37
 def rfc3339_date(date):
31 38
     if date.tzinfo:
32  
-        return date.strftime('%Y-%m-%dT%H:%M:%S%z')
  39
+        time_str = date.strftime('%Y-%m-%dT%H:%M:%S')
  40
+        offset = date.tzinfo.utcoffset(date)
  41
+        timezone = (offset.days * 24 * 60) + (offset.seconds / 60)
  42
+        hour, minute = divmod(timezone, 60)
  43
+        return time_str + "%+03d:%02d" % (hour, minute)
33 44
     else:
34 45
         return date.strftime('%Y-%m-%dT%H:%M:%SZ')
35 46
 
9  django/utils/tzinfo.py
@@ -14,8 +14,13 @@
14 14
 class FixedOffset(tzinfo):
15 15
     "Fixed offset in minutes east from UTC."
16 16
     def __init__(self, offset):
17  
-        self.__offset = timedelta(minutes=offset)
18  
-        self.__name = u"%+03d%02d" % (offset // 60, offset % 60)
  17
+        if isinstance(offset, timedelta):
  18
+            self.__offset = offset
  19
+            offset = self.__offset.seconds // 60
  20
+        else:
  21
+            self.__offset = timedelta(minutes=offset)
  22
+
  23
+        self.__name = u"%+03d%02d" % (offset / 60, offset % 60)
19 24
 
20 25
     def __repr__(self):
21 26
         return self.__name

0 notes on commit 34008aa

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