1
0
mirror of https://github.com/django/django.git synced 2024-12-23 01:25:58 +00:00

Fixed #32269 -- Fixed parse_duration() for negative days in ISO 8601 format.

This commit is contained in:
starryrbs 2020-12-19 17:53:35 +08:00 committed by Mariusz Felisiak
parent 57d05f94c3
commit 2a76f43134
2 changed files with 6 additions and 0 deletions

View File

@ -144,4 +144,6 @@ def parse_duration(value):
kw['microseconds'] = '-' + kw['microseconds'] kw['microseconds'] = '-' + kw['microseconds']
kw = {k: float(v.replace(',', '.')) for k, v in kw.items() if v is not None} kw = {k: float(v.replace(',', '.')) for k, v in kw.items() if v is not None}
days = datetime.timedelta(kw.pop('days', .0) or .0) days = datetime.timedelta(kw.pop('days', .0) or .0)
if match.re == iso8601_duration_re:
days *= sign
return days + sign * datetime.timedelta(**kw) return days + sign * datetime.timedelta(**kw)

View File

@ -135,8 +135,11 @@ class DurationParseTests(unittest.TestCase):
('P4M', None), ('P4M', None),
('P4W', None), ('P4W', None),
('P4D', timedelta(days=4)), ('P4D', timedelta(days=4)),
('-P1D', timedelta(days=-1)),
('P0.5D', timedelta(hours=12)), ('P0.5D', timedelta(hours=12)),
('P0,5D', timedelta(hours=12)), ('P0,5D', timedelta(hours=12)),
('-P0.5D', timedelta(hours=-12)),
('-P0,5D', timedelta(hours=-12)),
('PT5H', timedelta(hours=5)), ('PT5H', timedelta(hours=5)),
('-PT5H', timedelta(hours=-5)), ('-PT5H', timedelta(hours=-5)),
('PT5M', timedelta(minutes=5)), ('PT5M', timedelta(minutes=5)),
@ -147,6 +150,7 @@ class DurationParseTests(unittest.TestCase):
('PT0,000005S', timedelta(microseconds=5)), ('PT0,000005S', timedelta(microseconds=5)),
('-PT0.000005S', timedelta(microseconds=-5)), ('-PT0.000005S', timedelta(microseconds=-5)),
('-PT0,000005S', timedelta(microseconds=-5)), ('-PT0,000005S', timedelta(microseconds=-5)),
('-P4DT1H', timedelta(days=-4, hours=-1)),
) )
for source, expected in test_values: for source, expected in test_values:
with self.subTest(source=source): with self.subTest(source=source):