Refs #26281 -- Added a helpful error message for an invalid "r" specifier to dateformat.format().

This commit is contained in:
Baptiste Mispelon 2019-11-22 09:40:49 +01:00 committed by Mariusz Felisiak
parent 379bf1a2d4
commit 76ec032712
2 changed files with 6 additions and 1 deletions

View File

@ -278,6 +278,11 @@ class DateFormat(TimeFormat):
def r(self): def r(self):
"RFC 5322 formatted date; e.g. 'Thu, 21 Dec 2000 16:01:07 +0200'" "RFC 5322 formatted date; e.g. 'Thu, 21 Dec 2000 16:01:07 +0200'"
if type(self.data) is datetime.date:
raise TypeError(
"The format for date objects may not contain time-related "
"format specifiers (found 'r')."
)
return self.format('D, j M Y H:i:s O') return self.format('D, j M Y H:i:s O')
def S(self): def S(self):

View File

@ -149,7 +149,7 @@ class DateFormatTests(SimpleTestCase):
def test_invalid_time_format_specifiers(self): def test_invalid_time_format_specifiers(self):
my_birthday = date(1984, 8, 7) my_birthday = date(1984, 8, 7)
for specifier in ['a', 'A', 'f', 'g', 'G', 'h', 'H', 'i', 'P', 's', 'u']: for specifier in ['a', 'A', 'f', 'g', 'G', 'h', 'H', 'i', 'P', 'r', 's', 'u']:
msg = ( msg = (
"The format for date objects may not contain time-related " "The format for date objects may not contain time-related "
"format specifiers (found '%s')." % specifier "format specifiers (found '%s')." % specifier