Ticket #9762: rfc2822.2.diff

File rfc2822.2.diff, 3.0 KB (added by kgrandis, 14 years ago)

updated tests to unit tests and fixed an issue with tzinfo

  • django/utils/dateformat.py

     
    1414import re
    1515import time
    1616import calendar
     17from email.utils import formatdate
    1718from django.utils.dates import MONTHS, MONTHS_3, MONTHS_AP, WEEKDAYS, WEEKDAYS_ABBR
    1819from django.utils.tzinfo import LocalTimezone
    1920from django.utils.translation import ugettext as _
     
    172173
    173174    def r(self):
    174175        "RFC 2822 formatted date; e.g. 'Thu, 21 Dec 2000 16:01:07 +0200'"
    175         return self.format('D, j M Y H:i:s O')
     176        # solution taken from http://bugs.python.org/issue665194 until
     177        # Python addresses this feature
     178        timestamp = time.mktime(self.data.timetuple())
     179        return formatdate(timestamp, True)
    176180
     181
    177182    def S(self):
    178183        "English ordinal suffix for the day of the month, 2 characters; i.e. 'st', 'nd', 'rd' or 'th'"
    179184        if self.data.day in (11, 12, 13): # Special case
  • tests/regressiontests/dateformat/tests.py

     
    1 
    2 from django.utils import dateformat, translation
    31from unittest import TestCase
    42import datetime, os, time
     3from django.conf import settings
     4from django.utils import dateformat, translation
    55
    66class DateFormatTests(TestCase):
    77    def setUp(self):
    88        self.old_TZ = os.environ.get('TZ')
    99        os.environ['TZ'] = 'Europe/Copenhagen'
    1010        translation.activate('en-us')
     11        settings.LANGUAGE_CODE = 'en'
    1112
    1213        try:
    1314            # Check if a timezone has been set
     
    8283
    8384        if self.tz_tests:
    8485            self.assertEquals(dateformat.format(my_birthday, 'O'), u'+0100')
    85             self.assertEquals(dateformat.format(my_birthday, 'r'), u'Sun, 8 Jul 1979 22:00:00 +0100')
     86            self.assertEquals(dateformat.format(my_birthday, 'r'), u'Sun, 08 Jul 1979 22:00:00 +0100')
    8687            self.assertEquals(dateformat.format(my_birthday, 'T'), u'CET')
    8788            self.assertEquals(dateformat.format(my_birthday, 'U'), u'300315600')
    8889            self.assertEquals(dateformat.format(my_birthday, 'Z'), u'3600')
     
    9091            self.assertEquals(dateformat.format(summertime, 'O'), u'+0200')
    9192            self.assertEquals(dateformat.format(wintertime, 'I'), u'0')
    9293            self.assertEquals(dateformat.format(wintertime, 'O'), u'+0100')
     94
     95    def test_rfc2822_trans(self):
     96        settings.LANGUAGE_CODE='es'
     97        translation.activate('es')
     98        # Even though the language is now Spanish the RFC 2822 date string
     99        #  should not be translated. Sun, not "dom"
     100        not_my_birthday = datetime.datetime(1979, 7, 8, 22, 00)
     101
     102        if self.tz_tests:
     103            self.assertEquals( dateformat.format(not_my_birthday, 'r'), u'Sun, 08 Jul 1979 22:00:00 +0100')
Back to Top