From 55a10fb6f446ff29ae5cd134315fc3987da87c6f Mon Sep 17 00:00:00 2001 From: Faris Basha TM <72191505+farisbasha@users.noreply.github.com> Date: Sat, 5 Dec 2026 05:16:35 +0530 Subject: [PATCH] Fix multi-digit decimal fractions in the pure Python duration parser The pure Python ISO 8601 duration parser divided the digits after the decimal mark by 10 whatever their count, so PT1.25H became 3h30m instead of 1h15m and long fractions raised OverflowError. Fractional weeks also dropped everything below whole hours. Parse the fraction as 0., which matches the Rust parser. Fixes #534 --- src/pendulum/parsing/iso8601.py | 9 ++++----- tests/parsing/test_parse_iso8601.py | 6 ++++++ 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/pendulum/parsing/iso8601.py b/src/pendulum/parsing/iso8601.py index c65d249e5..d1255ce66 100644 --- a/src/pendulum/parsing/iso8601.py +++ b/src/pendulum/parsing/iso8601.py @@ -296,8 +296,7 @@ def _parse_iso8601_duration(text: str, **options: str) -> Duration | None: if "." in _weeks: _weeks, portion = _weeks.split(".") weeks = int(_weeks) - _days = int(portion) / 10 * 7 - days, hours = int(_days // 1), int(_days % 1 * HOURS_PER_DAY) + days = float(f"0.{portion}") * 7 else: weeks = int(_weeks) @@ -344,7 +343,7 @@ def _parse_iso8601_duration(text: str, **options: str) -> Duration | None: _days, _hours = _days.split(".") days = int(_days) - hours = int(_hours) / 10 * HOURS_PER_DAY + hours = float(f"0.{_hours}") * HOURS_PER_DAY else: days = int(_days) @@ -374,7 +373,7 @@ def _parse_iso8601_duration(text: str, **options: str) -> Duration | None: _hours, _mins = _hours.split(".") hours += int(_hours) - minutes += int(_mins) / 10 * MINUTES_PER_HOUR + minutes += float(f"0.{_mins}") * MINUTES_PER_HOUR else: hours += int(_hours) @@ -389,7 +388,7 @@ def _parse_iso8601_duration(text: str, **options: str) -> Duration | None: _minutes, _secs = _minutes.split(".") minutes += int(_minutes) - seconds += int(_secs) / 10 * SECONDS_PER_MINUTE + seconds += float(f"0.{_secs}") * SECONDS_PER_MINUTE else: minutes += int(_minutes) diff --git a/tests/parsing/test_parse_iso8601.py b/tests/parsing/test_parse_iso8601.py index ed2d39887..934bba9d0 100644 --- a/tests/parsing/test_parse_iso8601.py +++ b/tests/parsing/test_parse_iso8601.py @@ -190,6 +190,12 @@ def test_parse_iso8601_invalid(): ("PT1H", (0, 0, 0, 0, 1, 0, 0, 0)), ("PT1.5H", (0, 0, 0, 0, 1, 30, 0, 0)), ("PT1,5H", (0, 0, 0, 0, 1, 30, 0, 0)), + ("P1.1W", (0, 0, 1, 0, 16, 48, 0, 0)), + ("P1.25W", (0, 0, 1, 1, 18, 0, 0, 0)), + ("P1.25D", (0, 0, 0, 1, 6, 0, 0, 0)), + ("P1.05D", (0, 0, 0, 1, 1, 12, 0, 0)), + ("PT1.25H", (0, 0, 0, 0, 1, 15, 0, 0)), + ("PT16.48M", (0, 0, 0, 0, 0, 16, 28, 800_000)), ("P2Y30M4DT5H6M7S", (2, 30, 0, 4, 5, 6, 7, 0)), ], )