Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/pendulum/parsing/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down
4 changes: 4 additions & 0 deletions src/pendulum/parsing/iso8601.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down
20 changes: 20 additions & 0 deletions tests/parsing/test_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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("")
Expand Down