summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorvarunkasyap <varunkasyap@hotmail.com>2025-11-22 17:54:05 +0530
committerJacob Walls <jacobtylerwalls@gmail.com>2025-12-17 10:19:05 -0500
commit0d8548e5831bc610102d5e4b8a2366f26818a28a (patch)
tree66afb67472c108cc8301716f2b1bd7037bc63af5
parent6cc1231285a20b11058143f8cb0a6b4b3999b23a (diff)
Fixed #36747 -- Parsed weeks from ISO 8601 format in parse_duration().
-rw-r--r--django/utils/dateparse.py5
-rw-r--r--docs/ref/utils.txt9
-rw-r--r--docs/releases/6.1.txt3
-rw-r--r--tests/utils_tests/test_dateparse.py6
4 files changed, 19 insertions, 4 deletions
diff --git a/django/utils/dateparse.py b/django/utils/dateparse.py
index 42c2684d7c..b95fb21102 100644
--- a/django/utils/dateparse.py
+++ b/django/utils/dateparse.py
@@ -40,6 +40,7 @@ standard_duration_re = _lazy_re_compile(
iso8601_duration_re = _lazy_re_compile(
r"^(?P<sign>[-+]?)"
r"P"
+ r"(?:(?P<weeks>\d+([.,]\d+)?)W)?"
r"(?:(?P<days>\d+([.,]\d+)?)D)?"
r"(?:T"
r"(?:(?P<hours>\d+([.,]\d+)?)H)?"
@@ -134,8 +135,8 @@ def parse_duration(value):
The preferred format for durations in Django is '%d %H:%M:%S.%f'.
- Also supports ISO 8601 representation and PostgreSQL's day-time interval
- format.
+ Also supports ISO 8601 representation (excluding years and months) and
+ PostgreSQL's day-time interval format.
"""
match = (
standard_duration_re.match(value)
diff --git a/docs/ref/utils.txt b/docs/ref/utils.txt
index 69af23ad83..1c42784d13 100644
--- a/docs/ref/utils.txt
+++ b/docs/ref/utils.txt
@@ -147,6 +147,15 @@ The functions defined in this module share the following properties:
``P4DT1H15M20S`` which is equivalent to ``4 1:15:20``) or PostgreSQL's
day-time interval format (e.g. ``3 days 04:05:06``).
+ .. admonition:: Years and months not supported
+
+ ISO 8601 years (``Y``) and months (``M``) are unsupported as they
+ cannot be represented by :class:`~datetime.timedelta`.
+
+ .. versionchanged:: 6.1
+
+ ISO 8601 duration parsing now supports weeks (``PnW``).
+
``django.utils.decorators``
===========================
diff --git a/docs/releases/6.1.txt b/docs/releases/6.1.txt
index a392b3d755..4036514984 100644
--- a/docs/releases/6.1.txt
+++ b/docs/releases/6.1.txt
@@ -321,7 +321,8 @@ URLs
Utilities
~~~~~~~~~
-* ...
+* :func:`~django.utils.dateparse.parse_duration` now supports ISO 8601
+ durations expressed in weeks (``PnW``).
Validators
~~~~~~~~~~
diff --git a/tests/utils_tests/test_dateparse.py b/tests/utils_tests/test_dateparse.py
index a01942bfd0..b37cc9544a 100644
--- a/tests/utils_tests/test_dateparse.py
+++ b/tests/utils_tests/test_dateparse.py
@@ -194,7 +194,11 @@ class DurationParseTests(unittest.TestCase):
test_values = (
("P4Y", None),
("P4M", None),
- ("P4W", None),
+ ("P4W", timedelta(weeks=4)),
+ ("P0.5W", timedelta(weeks=0.5)),
+ ("P0,5W", timedelta(weeks=0.5)),
+ ("-P0.5W", timedelta(weeks=-0.5)),
+ ("P1W1D", timedelta(weeks=1, days=1)),
("P4D", timedelta(days=4)),
("-P1D", timedelta(days=-1)),
("P0.5D", timedelta(hours=12)),