Ticket #28690: parse-http-date-year.patch

File parse-http-date-year.patch, 2.1 KB (added by Mads Jensen, 7 years ago)
  • django/utils/http.py

    diff --git a/django/utils/http.py b/django/utils/http.py
    index c13f446..8008714 100644
    a b def parse_http_date(date):  
    132132
    133133    Return an integer expressed in seconds since the epoch, in UTC.
    134134    """
    135     # emails.Util.parsedate does the job for RFC1123 dates; unfortunately
     135    # email.utils.parsedate does the job for RFC1123 dates; unfortunately
    136136    # RFC7231 makes it mandatory to support RFC850 dates too. So we roll
    137137    # our own RFC-compliant parsing.
    138138    for regex in RFC1123_DATE, RFC850_DATE, ASCTIME_DATE:
    def parse_http_date(date):  
    144144    try:
    145145        year = int(m.group('year'))
    146146        if year < 100:
    147             if year < 70:
     147            if year < 50:
    148148                year += 2000
    149149            else:
    150150                year += 1900
  • tests/utils_tests/test_http.py

    diff --git a/tests/utils_tests/test_http.py b/tests/utils_tests/test_http.py
    index 04c2d55..0df0967 100644
    a b class HttpDateProcessingTests(unittest.TestCase):  
    223223        parsed = http.parse_http_date('Sunday, 06-Nov-94 08:49:37 GMT')
    224224        self.assertEqual(datetime.utcfromtimestamp(parsed), datetime(1994, 11, 6, 8, 49, 37))
    225225
     226    def test_parsing_rfc850_year(self):
     227        dates = (
     228            ('Monday, 15-Aug-05 08:49:37 GMT', datetime(2005, 8, 15, 8, 49, 37)),
     229            ('Monday, 15-Aug-55 08:49:37 GMT', datetime(1955, 8, 15, 8, 49, 37)),
     230            ('Monday, 15-Aug-49 08:49:37 GMT', datetime(2049, 8, 15, 8, 49, 37)),
     231            ('Monday, 15-Aug-95 08:49:37 GMT', datetime(1995, 8, 15, 8, 49, 37)),
     232        )
     233        for rfc850str, rfc850date in dates:
     234            with self.subTest(string=rfc850str, date=rfc850date):
     235                parsed = http.parse_http_date(rfc850str)
     236                self.assertEqual(
     237                    datetime.utcfromtimestamp(parsed),
     238                    rfc850date
     239                )
     240
    226241    def test_parsing_asctime(self):
    227242        parsed = http.parse_http_date('Sun Nov  6 08:49:37 1994')
    228243        self.assertEqual(datetime.utcfromtimestamp(parsed), datetime(1994, 11, 6, 8, 49, 37))
Back to Top