Fix OverflowError in parse_time for huge values (#187650)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Raman Gupta
2026-10-20 22:47:08 -04:00
committed by GitHub
co-authored by Claude Opus 5.5
parent 0448f49059
commit be85474fb9
3 changed files with 26 additions and 4 deletions
+3 -2
View File
@@ -306,8 +306,9 @@ def parse_time(time_str: str) -> dt.time | None:
minute = int(parts[1])
second = int(parts[2]) if len(parts) > 2 else 0
return dt.time(hour, minute, second)
except ValueError:
# ValueError if value cannot be converted to an int or not in range
except ValueError, OverflowError:
# ValueError if value cannot be converted to an int or not in range,
# OverflowError if it does not even fit in a C int
return None
+6 -1
View File
@@ -848,7 +848,12 @@ def test_time() -> None:
"""Test date validation."""
schema = probatio.Schema(cv.time)
for value in ("Not a time", "2016-11-23", "2016-11-23T18:59:08"):
for value in (
"Not a time",
"2016-11-23",
"2016-11-23T18:59:08",
"0:2147483648",
):
with pytest.raises(probatio.Invalid):
schema(value)
+17 -1
View File
@@ -1,7 +1,7 @@
"""Test Home Assistant date util methods."""
# pylint: disable=home-assistant-enforce-utcnow
from datetime import UTC, datetime, timedelta
from datetime import UTC, datetime, time, timedelta
import pytest
@@ -185,6 +185,22 @@ def test_parse_duration(
assert dt_util.parse_duration(duration_string) == expected_result
@pytest.mark.parametrize(
("time_string", "expected_result"),
[
pytest.param("12:34", time(12, 34), id="hours_minutes"),
pytest.param("12:34:56", time(12, 34, 56), id="hours_minutes_seconds"),
pytest.param("24:00", None, id="hour_out_of_range"),
pytest.param("0:2147483648", None, id="minute_overflows_c_int"),
pytest.param("2147483648:00", None, id="hour_overflows_c_int"),
pytest.param("0:0:99999999999999999999", None, id="second_overflows_c_long"),
],
)
def test_parse_time(time_string: str, expected_result: time | None) -> None:
"""Test that parse_time returns the expected result."""
assert dt_util.parse_time(time_string) == expected_result
def test_get_age() -> None:
"""Test get_age."""
diff = dt_util.now() - timedelta(seconds=0)