diff --git a/django/utils/dateparse.py b/django/utils/dateparse.py index 0945c0c761..e069f5df39 100644 --- a/django/utils/dateparse.py +++ b/django/utils/dateparse.py @@ -144,4 +144,6 @@ def parse_duration(value): kw['microseconds'] = '-' + kw['microseconds'] 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) + if match.re == iso8601_duration_re: + days *= sign return days + sign * datetime.timedelta(**kw) diff --git a/tests/utils_tests/test_dateparse.py b/tests/utils_tests/test_dateparse.py index 59dcab6b1c..0ecbe24847 100644 --- a/tests/utils_tests/test_dateparse.py +++ b/tests/utils_tests/test_dateparse.py @@ -135,8 +135,11 @@ class DurationParseTests(unittest.TestCase): ('P4M', None), ('P4W', None), ('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)), ('PT5H', timedelta(hours=5)), ('-PT5H', timedelta(hours=-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)), + ('-P4DT1H', timedelta(days=-4, hours=-1)), ) for source, expected in test_values: with self.subTest(source=source):