Refs #29600 -- Updated django.utils.datetime_safe now that Python 2 is unsupported.
This commit is contained in:
parent
fa54ebc722
commit
2092206bee
|
@ -1,11 +1,11 @@
|
||||||
# Python's datetime strftime doesn't handle dates before 1900.
|
# These classes override date and datetime to ensure that strftime('%Y')
|
||||||
# These classes override date and datetime to support the formatting of a date
|
# returns four digits (with leading zeros) on years < 1000.
|
||||||
# through its full "proleptic Gregorian" date range.
|
# https://bugs.python.org/issue13305
|
||||||
#
|
#
|
||||||
# Based on code submitted to comp.lang.python by Andrew Dalke
|
# Based on code submitted to comp.lang.python by Andrew Dalke
|
||||||
#
|
#
|
||||||
# >>> datetime_safe.date(1850, 8, 2).strftime("%Y/%m/%d was a %A")
|
# >>> datetime_safe.date(10, 8, 2).strftime("%Y/%m/%d was a %A")
|
||||||
# '1850/08/02 was a Friday'
|
# '0010/08/02 was a Monday'
|
||||||
|
|
||||||
import re
|
import re
|
||||||
import time as ttime
|
import time as ttime
|
||||||
|
@ -71,11 +71,11 @@ def _findall(text, substr):
|
||||||
|
|
||||||
|
|
||||||
def strftime(dt, fmt):
|
def strftime(dt, fmt):
|
||||||
if dt.year >= 1900:
|
if dt.year >= 1000:
|
||||||
return super(type(dt), dt).strftime(fmt)
|
return super(type(dt), dt).strftime(fmt)
|
||||||
illegal_formatting = _illegal_formatting.search(fmt)
|
illegal_formatting = _illegal_formatting.search(fmt)
|
||||||
if illegal_formatting:
|
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
|
year = dt.year
|
||||||
# For every non-leap year century, advance by
|
# For every non-leap year century, advance by
|
||||||
|
|
|
@ -1,17 +1,18 @@
|
||||||
import unittest
|
|
||||||
from datetime import (
|
from datetime import (
|
||||||
date as original_date, datetime as original_datetime,
|
date as original_date, datetime as original_datetime,
|
||||||
time as original_time,
|
time as original_time,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
from django.test import SimpleTestCase
|
||||||
from django.utils.datetime_safe import date, datetime, time
|
from django.utils.datetime_safe import date, datetime, time
|
||||||
|
|
||||||
|
|
||||||
class DatetimeTests(unittest.TestCase):
|
class DatetimeTests(SimpleTestCase):
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
self.just_safe = (1900, 1, 1)
|
self.percent_y_safe = (1900, 1, 1) # >= 1900 required on Windows.
|
||||||
self.just_unsafe = (1899, 12, 31, 23, 59, 59)
|
self.just_safe = (1000, 1, 1)
|
||||||
|
self.just_unsafe = (999, 12, 31, 23, 59, 59)
|
||||||
self.just_time = (11, 30, 59)
|
self.just_time = (11, 30, 59)
|
||||||
self.really_old = (20, 1, 1)
|
self.really_old = (20, 1, 1)
|
||||||
self.more_recent = (2006, 1, 1)
|
self.more_recent = (2006, 1, 1)
|
||||||
|
@ -34,21 +35,23 @@ class DatetimeTests(unittest.TestCase):
|
||||||
)
|
)
|
||||||
|
|
||||||
def test_safe_strftime(self):
|
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_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)'), '1900-01-01 (weekday 1)')
|
self.assertEqual(date(*self.just_safe).strftime('%Y-%m-%d (weekday %w)'), '1000-01-01 (weekday 3)')
|
||||||
|
|
||||||
self.assertEqual(
|
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(
|
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')
|
self.assertEqual(time(*self.just_time).strftime('%H:%M:%S AM'), '11:30:59 AM')
|
||||||
|
|
||||||
# %y will error before this date
|
# %y will error before this date
|
||||||
self.assertEqual(date(*self.just_safe).strftime('%y'), '00')
|
self.assertEqual(date(*self.percent_y_safe).strftime('%y'), '00')
|
||||||
self.assertEqual(datetime(*self.just_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')
|
self.assertEqual(date(1850, 8, 2).strftime("%Y/%m/%d was a %A"), '1850/08/02 was a Friday')
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue