Django

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@gmail.com>, 5 months ago)
  • django/contrib/syndication/feeds.py

    old new  
    22from django.template import loader, Template, TemplateDoesNotExist 
    33from django.contrib.sites.models import Site, RequestSite 
    44from django.utils import feedgenerator 
     5from django.utils.tzinfo import FixedOffset 
    56from django.utils.encoding import smart_unicode, iri_to_uri 
    67from django.conf import settings          
    78from django.template import RequestContext 
     
    109110            description_tmp = Template('{{ obj }}') 
    110111 
    111112        for item in self.__get_dynamic_attr('items', obj): 
    112             link = add_domain(current_site.domain, self.__get_dynamic_attr('item_link', item)) 
     113            link = add_domain(atom/current_site.domain, self.__get_dynamic_attr('item_link', item)) 
    113114            enc = None 
    114115            enc_url = self.__get_dynamic_attr('item_enclosure_url', item) 
    115116            if enc_url: 
     
    124125                author_link = self.__get_dynamic_attr('item_author_link', item) 
    125126            else: 
    126127                author_email = author_link = None 
     128                     
     129            pubdate = self.__get_dynamic_attr('item_pubdate', item) 
     130            now = datetime.now() 
     131            utcnow = datetime.utcnow()    
     132 
     133            # Must always subtract smaller time from larger time here.  
     134            if utcnow > now: 
     135                sign = -1 
     136                tzDifference = (utcnow - now)  
     137            else: 
     138                sign = 1 
     139                tzDifference = (now - utcnow)  
     140             
     141            # Round the timezone offset to the nearest half hour.     
     142            tzOffsetMinutes = sign * ((tzDifference.seconds / 60 + 15) / 30) * 30 
     143            tzOffset = timedelta(minutes=tzOffsetMinutes) 
     144            pubdate = pubdate.replace(tzinfo=FixedOffset(tzOffset)) 
     145              
    127146            feed.add_item( 
    128147                title = title_tmp.render(RequestContext(self.request, {'obj': item, 'site': current_site})), 
    129148                link = link, 
    130149                description = description_tmp.render(RequestContext(self.request, {'obj': item, 'site': current_site})), 
    131150                unique_id = self.__get_dynamic_attr('item_guid', item, link), 
    132151                enclosure = enc, 
    133                 pubdate = self.__get_dynamic_attr('item_pubdate', item)
     152                pubdate = pubdate
    134153                author_name = author_name, 
    135154                author_email = author_email, 
    136155                author_link = author_link, 
  • django/utils/tzinfo.py

    old new  
    1414class FixedOffset(tzinfo): 
    1515    "Fixed offset in minutes east from UTC." 
    1616    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) 
    1924 
    2025    def __repr__(self): 
    2126        return self.__name 
     
    2833 
    2934    def dst(self, dt): 
    3035        return timedelta(0) 
    31  
     36         
    3237class LocalTimezone(tzinfo): 
    3338    "Proxy timezone information from time module." 
    3439    def __init__(self, dt): 
  • django/utils/feedgenerator.py

    old new  
    2222from django.utils.xmlutils import SimplerXMLGenerator 
    2323from django.utils.encoding import force_unicode, iri_to_uri 
    2424import datetime, re, time 
    25 import email.Utils 
    2625 
    2726def 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') 
    2936 
    3037def rfc3339_date(date): 
    3138    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) 
    3344    else: 
    3445        return date.strftime('%Y-%m-%dT%H:%M:%SZ') 
    3546