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
8 changes: 7 additions & 1 deletion sqlmesh/utils/date.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,13 @@ def to_datetime(
try:
dt = datetime.strptime(str(value), DATE_INT_FMT)
except ValueError:
dt = datetime.fromtimestamp(epoch / 1000.0, tz=UTC)
try:
dt = datetime.fromtimestamp(epoch / 1000.0, tz=UTC)
except (OverflowError, OSError, ValueError):
# A non-finite or out-of-range epoch (e.g. "inf", 1e30, or a
# huge millis value) overflows fromtimestamp. Fall through to
# the ValueError below rather than leaking OverflowError/OSError.
dt = None

if dt is None:
raise ValueError(f"Could not convert `{value}` to datetime.")
Expand Down
11 changes: 11 additions & 0 deletions tests/utils/test_date.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,17 @@ def test_to_datetime() -> None:
assert to_datetime("31536000000") == target


@pytest.mark.parametrize(
"value",
["inf", "-inf", "1e30", "99999999999999999999", float("inf")],
)
def test_to_datetime_out_of_range_raises(value: t.Any) -> None:
# A non-finite or out-of-range epoch overflows fromtimestamp; it must raise
# the documented ValueError rather than leaking OverflowError/OSError.
with pytest.raises(ValueError):
to_datetime(value)


@pytest.mark.parametrize(
"expression, result",
[
Expand Down