diff --git a/django/utils/datetime_safe.py b/django/utils/datetime_safe.py index ed8420eb8e..7eaa5c21ce 100644 --- a/django/utils/datetime_safe.py +++ b/django/utils/datetime_safe.py @@ -1,11 +1,11 @@ -# Python's datetime strftime doesn't handle dates before 1900. -# These classes override date and datetime to support the formatting of a date -# through its full "proleptic Gregorian" date range. +# These classes override date and datetime to ensure that strftime('%Y') +# returns four digits (with leading zeros) on years < 1000. +# https://bugs.python.org/issue13305 # # Based on code submitted to comp.lang.python by Andrew Dalke # -# >>> datetime_safe.date(1850, 8, 2).strftime("%Y/%m/%d was a %A") -# '1850/08/02 was a Friday' +# >>> datetime_safe.date(10, 8, 2).strftime("%Y/%m/%d was a %A") +# '0010/08/02 was a Monday' import re import time as ttime @@ -71,11 +71,11 @@ def _findall(text, substr): def strftime(dt, fmt): - if dt.year >= 1900: + if dt.year >= 1000: return super(type(dt), dt).strftime(fmt) illegal_formatting = _illegal_formatting.search(fmt) if illegal_formatting: - raise TypeError("strftime of dates before 1900 does not handle" + illegal_formatting.group(0)) + raise TypeError("strftime of dates before 1000 does not handle " + illegal_formatting.group(0)) year = dt.year # For every non-leap year century, advance by diff --git a/tests/utils_tests/test_datetime_safe.py b/tests/utils_tests/test_datetime_safe.py index 5fa08bc4f4..56eec838fa 100644 --- a/tests/utils_tests/test_datetime_safe.py +++ b/tests/utils_tests/test_datetime_safe.py @@ -1,17 +1,18 @@ -import unittest from datetime import ( date as original_date, datetime as original_datetime, time as original_time, ) +from django.test import SimpleTestCase from django.utils.datetime_safe import date, datetime, time -class DatetimeTests(unittest.TestCase): +class DatetimeTests(SimpleTestCase): def setUp(self): - self.just_safe = (1900, 1, 1) - self.just_unsafe = (1899, 12, 31, 23, 59, 59) + self.percent_y_safe = (1900, 1, 1) # >= 1900 required on Windows. + self.just_safe = (1000, 1, 1) + self.just_unsafe = (999, 12, 31, 23, 59, 59) self.just_time = (11, 30, 59) self.really_old = (20, 1, 1) self.more_recent = (2006, 1, 1) @@ -34,21 +35,23 @@ class DatetimeTests(unittest.TestCase): ) def test_safe_strftime(self): - self.assertEqual(date(*self.just_unsafe[:3]).strftime('%Y-%m-%d (weekday %w)'), '1899-12-31 (weekday 0)') - self.assertEqual(date(*self.just_safe).strftime('%Y-%m-%d (weekday %w)'), '1900-01-01 (weekday 1)') + self.assertEqual(date(*self.just_unsafe[:3]).strftime('%Y-%m-%d (weekday %w)'), '0999-12-31 (weekday 2)') + self.assertEqual(date(*self.just_safe).strftime('%Y-%m-%d (weekday %w)'), '1000-01-01 (weekday 3)') self.assertEqual( - datetime(*self.just_unsafe).strftime('%Y-%m-%d %H:%M:%S (weekday %w)'), '1899-12-31 23:59:59 (weekday 0)' + datetime(*self.just_unsafe).strftime('%Y-%m-%d %H:%M:%S (weekday %w)'), '0999-12-31 23:59:59 (weekday 2)' ) self.assertEqual( - datetime(*self.just_safe).strftime('%Y-%m-%d %H:%M:%S (weekday %w)'), '1900-01-01 00:00:00 (weekday 1)' + datetime(*self.just_safe).strftime('%Y-%m-%d %H:%M:%S (weekday %w)'), '1000-01-01 00:00:00 (weekday 3)' ) self.assertEqual(time(*self.just_time).strftime('%H:%M:%S AM'), '11:30:59 AM') # %y will error before this date - self.assertEqual(date(*self.just_safe).strftime('%y'), '00') - self.assertEqual(datetime(*self.just_safe).strftime('%y'), '00') + self.assertEqual(date(*self.percent_y_safe).strftime('%y'), '00') + self.assertEqual(datetime(*self.percent_y_safe).strftime('%y'), '00') + with self.assertRaisesMessage(TypeError, 'strftime of dates before 1000 does not handle %y'): + datetime(*self.just_unsafe).strftime('%y') self.assertEqual(date(1850, 8, 2).strftime("%Y/%m/%d was a %A"), '1850/08/02 was a Friday')