Refs #30803 -- Allowed comma separators for decimal fractions in parse_duration().

This commit is contained in:
Farhaan Bukhsh 2019-11-27 08:14:00 +01:00 committed by Mariusz Felisiak
parent cab3661832
commit 42b23d1e79
4 changed files with 12 additions and 2 deletions

View File

@ -137,11 +137,11 @@ def parse_duration(value):
) )
if match: if match:
kw = match.groupdict() kw = match.groupdict()
days = datetime.timedelta(float(kw.pop('days', 0) or 0))
sign = -1 if kw.pop('sign', '+') == '-' else 1 sign = -1 if kw.pop('sign', '+') == '-' else 1
if kw.get('microseconds'): if kw.get('microseconds'):
kw['microseconds'] = kw['microseconds'].ljust(6, '0') kw['microseconds'] = kw['microseconds'].ljust(6, '0')
if kw.get('seconds') and kw.get('microseconds') and kw['seconds'].startswith('-'): if kw.get('seconds') and kw.get('microseconds') and kw['seconds'].startswith('-'):
kw['microseconds'] = '-' + kw['microseconds'] 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) return days + sign * datetime.timedelta(**kw)

View File

@ -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 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``). 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`` ``django.utils.decorators``
=========================== ===========================

View File

@ -256,6 +256,9 @@ Utilities
* :func:`~django.utils.encoding.filepath_to_uri` now supports * :func:`~django.utils.encoding.filepath_to_uri` now supports
:class:`pathlib.Path`. :class:`pathlib.Path`.
* :func:`~django.utils.dateparse.parse_duration` now supports comma separators
for decimal fractions in the ISO 8601 format.
Validators Validators
~~~~~~~~~~ ~~~~~~~~~~

View File

@ -131,10 +131,12 @@ class DurationParseTests(unittest.TestCase):
('P4W', None), ('P4W', None),
('P4D', timedelta(days=4)), ('P4D', timedelta(days=4)),
('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)), ('PT5M', timedelta(minutes=5)),
('PT5S', timedelta(seconds=5)), ('PT5S', timedelta(seconds=5)),
('PT0.000005S', timedelta(microseconds=5)), ('PT0.000005S', timedelta(microseconds=5)),
('PT0,000005S', timedelta(microseconds=5)),
) )
for source, expected in test_values: for source, expected in test_values:
with self.subTest(source=source): with self.subTest(source=source):