From 80b38326316b2fb4f7ca772f8066ad8ce5614da7 Mon Sep 17 00:00:00 2001 From: Kevin Flores <101906409+kevincommits@users.noreply.github.com> Date: Thu, 3 Dec 2026 14:52:02 -0500 Subject: [PATCH] Reject a decimal fraction of the hour or minute in parse() --- src/pendulum/parsing/__init__.py | 4 ++++ src/pendulum/parsing/iso8601.py | 4 ++++ tests/parsing/test_parsing.py | 20 ++++++++++++++++++++ 3 files changed, 28 insertions(+) diff --git a/src/pendulum/parsing/__init__.py b/src/pendulum/parsing/__init__.py index d88c089a6..c3a4380c2 100644 --- a/src/pendulum/parsing/__init__.py +++ b/src/pendulum/parsing/__init__.py @@ -179,6 +179,10 @@ def _parse_common(text: str, **options: Any) -> datetime | date | time: # Grabbing subseconds, if any microsecond = 0 if m.group("subsecondsection"): + if not m.group("second"): + # Decimal fractions of hours or minutes are not supported + raise ParserError("Invalid time string") + # Limiting to 6 chars subsecond = m.group("subsecond")[:6] diff --git a/src/pendulum/parsing/iso8601.py b/src/pendulum/parsing/iso8601.py index c65d249e5..32c18a479 100644 --- a/src/pendulum/parsing/iso8601.py +++ b/src/pendulum/parsing/iso8601.py @@ -226,6 +226,10 @@ def parse_iso8601( # Grabbing subseconds, if any if m.group("subsecondsection"): + if not m.group("second"): + # Decimal fractions of hours or minutes are not supported + raise ParserError("Invalid ISO 8601 time part") + # Limiting to 6 chars subsecond = m.group("subsecond")[:6] diff --git a/tests/parsing/test_parsing.py b/tests/parsing/test_parsing.py index 8151ce12f..d1c1e738e 100644 --- a/tests/parsing/test_parsing.py +++ b/tests/parsing/test_parsing.py @@ -684,6 +684,26 @@ def test_invalid(): parse(text) +@pytest.mark.parametrize( + "text", + [ + "12:30.5", + "12:30,5", + "2016:12:26 15:45.5", + "T12,5", + "1230,5", + "2016-10-06T12,5", + "2016-10-06T12:30.5", + "20161006T1230,5", + ], +) +def test_fraction_without_seconds_is_invalid(text): + # A decimal fraction after the hour or minute must not be read + # as a fraction of a second + with pytest.raises(ParserError): + parse(text) + + def test_parse_empty_string(): with pytest.raises(ParserError): parse("")