From 42b23d1e79b21ddd8abd7216a6771f105dd7062c Mon Sep 17 00:00:00 2001 From: Farhaan Bukhsh Date: Wed, 27 Nov 2019 08:14:00 +0100 Subject: [PATCH] Refs #30803 -- Allowed comma separators for decimal fractions in parse_duration(). --- django/utils/dateparse.py | 4 ++-- docs/ref/utils.txt | 5 +++++ docs/releases/3.1.txt | 3 +++ tests/utils_tests/test_dateparse.py | 2 ++ 4 files changed, 12 insertions(+), 2 deletions(-) diff --git a/django/utils/dateparse.py b/django/utils/dateparse.py index d142e161b6..535655561c 100644 --- a/django/utils/dateparse.py +++ b/django/utils/dateparse.py @@ -137,11 +137,11 @@ def parse_duration(value): ) if match: kw = match.groupdict() - days = datetime.timedelta(float(kw.pop('days', 0) or 0)) sign = -1 if kw.pop('sign', '+') == '-' else 1 if kw.get('microseconds'): kw['microseconds'] = kw['microseconds'].ljust(6, '0') if kw.get('seconds') and kw.get('microseconds') and kw['seconds'].startswith('-'): kw['microseconds'] = '-' + kw['microseconds'] - kw = {k: float(v) 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) return days + sign * datetime.timedelta(**kw) diff --git a/docs/ref/utils.txt b/docs/ref/utils.txt index 09b174c1ce..8aa8c3403d 100644 --- a/docs/ref/utils.txt +++ b/docs/ref/utils.txt @@ -154,6 +154,11 @@ The functions defined in this module share the following properties: 8601 (e.g. ``P4DT1H15M20S`` which is equivalent to ``4 1:15:20``) or PostgreSQL's day-time interval format (e.g. ``3 days 04:05:06``). + .. versionchanged:: 3.1 + + Support for comma separators for decimal fractions in the ISO 8601 + format was added. + ``django.utils.decorators`` =========================== diff --git a/docs/releases/3.1.txt b/docs/releases/3.1.txt index b7c9806692..ed841cc962 100644 --- a/docs/releases/3.1.txt +++ b/docs/releases/3.1.txt @@ -256,6 +256,9 @@ Utilities * :func:`~django.utils.encoding.filepath_to_uri` now supports :class:`pathlib.Path`. +* :func:`~django.utils.dateparse.parse_duration` now supports comma separators + for decimal fractions in the ISO 8601 format. + Validators ~~~~~~~~~~ diff --git a/tests/utils_tests/test_dateparse.py b/tests/utils_tests/test_dateparse.py index 535815c7e2..b1b591a7df 100644 --- a/tests/utils_tests/test_dateparse.py +++ b/tests/utils_tests/test_dateparse.py @@ -131,10 +131,12 @@ class DurationParseTests(unittest.TestCase): ('P4W', None), ('P4D', timedelta(days=4)), ('P0.5D', timedelta(hours=12)), + ('P0,5D', timedelta(hours=12)), ('PT5H', timedelta(hours=5)), ('PT5M', timedelta(minutes=5)), ('PT5S', timedelta(seconds=5)), ('PT0.000005S', timedelta(microseconds=5)), + ('PT0,000005S', timedelta(microseconds=5)), ) for source, expected in test_values: with self.subTest(source=source):