mirror of https://github.com/django/django.git
Fixed #32149 -- Added support for years < 1000 to DateFormat.y().
This commit is contained in:
parent
c448e614c6
commit
895f6e4992
|
@ -325,8 +325,8 @@ class DateFormat(TimeFormat):
|
||||||
return self.data.isocalendar()[1]
|
return self.data.isocalendar()[1]
|
||||||
|
|
||||||
def y(self):
|
def y(self):
|
||||||
"Year, 2 digits; e.g. '99'"
|
"""Year, 2 digits with leading zeros; e.g. '99'."""
|
||||||
return str(self.data.year)[2:]
|
return '%02d' % (self.data.year % 100)
|
||||||
|
|
||||||
def Y(self):
|
def Y(self):
|
||||||
"Year, 4 digits; e.g. '1999'"
|
"Year, 4 digits; e.g. '1999'"
|
||||||
|
|
|
@ -1379,7 +1379,7 @@ Format character Description Example output
|
||||||
style. Proprietary extension.
|
style. Proprietary extension.
|
||||||
``t`` Number of days in the given month. ``28`` to ``31``
|
``t`` Number of days in the given month. ``28`` to ``31``
|
||||||
**Year**
|
**Year**
|
||||||
``y`` Year, 2 digits. ``'99'``
|
``y`` Year, 2 digits with leading zeros. ``'00'`` to ``'99'``
|
||||||
``Y`` Year, 4 digits. ``'1999'``
|
``Y`` Year, 4 digits. ``'1999'``
|
||||||
``L`` Boolean for whether it's a leap year. ``True`` or ``False``
|
``L`` Boolean for whether it's a leap year. ``True`` or ``False``
|
||||||
``o`` ISO-8601 week-numbering year, ``'1999'``
|
``o`` ISO-8601 week-numbering year, ``'1999'``
|
||||||
|
|
|
@ -165,3 +165,16 @@ class DateFormatTests(SimpleTestCase):
|
||||||
dateformat.format(dt, 'r'),
|
dateformat.format(dt, 'r'),
|
||||||
'Sun, 08 Jul 1979 22:00:00 +0100',
|
'Sun, 08 Jul 1979 22:00:00 +0100',
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def test_year_before_1000(self):
|
||||||
|
tests = [
|
||||||
|
(476, '76'),
|
||||||
|
(42, '42'),
|
||||||
|
(4, '04'),
|
||||||
|
]
|
||||||
|
for year, expected_date in tests:
|
||||||
|
with self.subTest(year=year):
|
||||||
|
self.assertEqual(
|
||||||
|
dateformat.format(datetime(year, 9, 8, 5, 0), 'y'),
|
||||||
|
expected_date,
|
||||||
|
)
|
||||||
|
|
Loading…
Reference in New Issue