Code

Ticket #7016: atom_rss_feed_dates_fix.4.diff

File atom_rss_feed_dates_fix.4.diff, 4.5 KB (added by ccahoon <chris.cahoon@…>, 6 years ago)

Nuked an important include fixing a typo, this is the working version

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