Django

Code

Changeset 8216

Show
Ignore:
Timestamp:
08/05/08 12:38:49 (4 months ago)
Author:
jacob
Message:

Fixed #7016: use correct time zones for Atom feeds. Thanks, Chris Cahoon.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/AUTHORS

    r8136 r8216  
    8484    Jonathan Buchanan <jonathan.buchanan@gmail.com> 
    8585    Keith Bussell <kbussell@gmail.com> 
     86    Chris Cahoo <chris.cahoo@gmail.com> 
    8687    Juan Manuel Caicedo <juan.manuel.caicedo@gmail.com> 
    8788    Trevor Caira <trevor@caira.com> 
  • django/trunk/django/contrib/syndication/feeds.py

    r8046 r8216  
     1from datetime import datetime, timedelta 
     2 
    13from django.core.exceptions import ImproperlyConfigured, ObjectDoesNotExist 
    24from django.template import loader, Template, TemplateDoesNotExist 
    35from django.contrib.sites.models import Site, RequestSite 
    46from django.utils import feedgenerator 
     7from django.utils.tzinfo import FixedOffset 
    58from django.utils.encoding import smart_unicode, iri_to_uri 
    69from django.conf import settings          
     
    125128            else: 
    126129                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 
    127148            feed.add_item( 
    128149                title = title_tmp.render(RequestContext(self.request, {'obj': item, 'site': current_site})), 
     
    131152                unique_id = self.__get_dynamic_attr('item_guid', item, link), 
    132153                enclosure = enc, 
    133                 pubdate = self.__get_dynamic_attr('item_pubdate', item)
     154                pubdate = pubdate
    134155                author_name = author_name, 
    135156                author_email = author_email, 
  • django/trunk/django/utils/feedgenerator.py

    r7066 r8216  
    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') 
  • django/trunk/django/utils/tzinfo.py

    r7945 r8216  
    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):