Code

Ticket #7016: atom_rss_feed_dates_fix.2.diff

File atom_rss_feed_dates_fix.2.diff, 4.8 KB (added by ccahoon <chris.cahoon@…>, 6 years ago)
Line 
1Index: django/contrib/syndication/feeds.py
2===================================================================
3--- django/contrib/syndication/feeds.py (revision 8168)
4+++ django/contrib/syndication/feeds.py (working copy)
5@@ -2,6 +2,7 @@
6 from django.template import loader, Template, TemplateDoesNotExist
7 from django.contrib.sites.models import Site, RequestSite
8 from django.utils import feedgenerator
9+from django.utils.tzinfo import FixedOffset
10 from django.utils.encoding import smart_unicode, iri_to_uri
11 from django.conf import settings         
12 from django.template import RequestContext
13@@ -109,7 +110,7 @@
14             description_tmp = Template('{{ obj }}')
15 
16         for item in self.__get_dynamic_attr('items', obj):
17-            link = add_domain(current_site.domain, self.__get_dynamic_attr('item_link', item))
18+            link = add_domain(atom/current_site.domain, self.__get_dynamic_attr('item_link', item))
19             enc = None
20             enc_url = self.__get_dynamic_attr('item_enclosure_url', item)
21             if enc_url:
22@@ -124,13 +125,31 @@
23                 author_link = self.__get_dynamic_attr('item_author_link', item)
24             else:
25                 author_email = author_link = None
26+                   
27+            pubdate = self.__get_dynamic_attr('item_pubdate', item)
28+            now = datetime.now()
29+            utcnow = datetime.utcnow()   
30+
31+            # Must always subtract smaller time from larger time here.
32+            if utcnow > now:
33+                sign = -1
34+                tzDifference = (utcnow - now)
35+            else:
36+                sign = 1
37+                tzDifference = (now - utcnow)
38+           
39+            # Round the timezone offset to the nearest half hour.   
40+            tzOffsetMinutes = sign * ((tzDifference.seconds / 60 + 15) / 30) * 30
41+            tzOffset = timedelta(minutes=tzOffsetMinutes)
42+            pubdate = pubdate.replace(tzinfo=FixedOffset(tzOffset))
43+             
44             feed.add_item(
45                 title = title_tmp.render(RequestContext(self.request, {'obj': item, 'site': current_site})),
46                 link = link,
47                 description = description_tmp.render(RequestContext(self.request, {'obj': item, 'site': current_site})),
48                 unique_id = self.__get_dynamic_attr('item_guid', item, link),
49                 enclosure = enc,
50-                pubdate = self.__get_dynamic_attr('item_pubdate', item),
51+                pubdate = pubdate,
52                 author_name = author_name,
53                 author_email = author_email,
54                 author_link = author_link,
55Index: django/utils/tzinfo.py
56===================================================================
57--- django/utils/tzinfo.py      (revision 8168)
58+++ django/utils/tzinfo.py      (working copy)
59@@ -14,8 +14,13 @@
60 class FixedOffset(tzinfo):
61     "Fixed offset in minutes east from UTC."
62     def __init__(self, offset):
63-        self.__offset = timedelta(minutes=offset)
64-        self.__name = u"%+03d%02d" % (offset // 60, offset % 60)
65+        if isinstance(offset, timedelta):
66+            self.__offset = offset
67+            offset = self.__offset.seconds // 60
68+        else:
69+            self.__offset = timedelta(minutes=offset)
70+           
71+        self.__name = u"%+03d%02d" % (offset / 60, offset % 60)
72 
73     def __repr__(self):
74         return self.__name
75@@ -28,7 +33,7 @@
76 
77     def dst(self, dt):
78         return timedelta(0)
79-
80+       
81 class LocalTimezone(tzinfo):
82     "Proxy timezone information from time module."
83     def __init__(self, dt):
84Index: django/utils/feedgenerator.py
85===================================================================
86--- django/utils/feedgenerator.py       (revision 8168)
87+++ django/utils/feedgenerator.py       (working copy)
88@@ -22,14 +22,25 @@
89 from django.utils.xmlutils import SimplerXMLGenerator
90 from django.utils.encoding import force_unicode, iri_to_uri
91 import datetime, re, time
92-import email.Utils
93 
94 def rfc2822_date(date):
95-    return email.Utils.formatdate(time.mktime(date.timetuple()))
96+    # We do this ourselves to be timezone aware, email.Utils is not tz aware.
97+    if date.tzinfo:
98+        time_str = date.strftime('%a, %d %b %Y %H:%M:%S ') 
99+        offset = date.tzinfo.utcoffset(date)       
100+        timezone = (offset.days * 24 * 60) + (offset.seconds / 60)
101+        hour, minute = divmod(timezone, 60)
102+        return time_str + "%+03d%02d" % (hour, minute)
103+    else:
104+        return date.strftime('%a, %d %b %Y %H:%M:%S -0000')
105 
106 def rfc3339_date(date):
107     if date.tzinfo:
108-        return date.strftime('%Y-%m-%dT%H:%M:%S%z')
109+        time_str = date.strftime('%Y-%m-%dT%H:%M:%S') 
110+        offset = date.tzinfo.utcoffset(date)       
111+        timezone = (offset.days * 24 * 60) + (offset.seconds / 60)
112+        hour, minute = divmod(timezone, 60)
113+        return time_str + "%+03d:%02d" % (hour, minute)
114     else:
115         return date.strftime('%Y-%m-%dT%H:%M:%SZ')
116