Skip to content

Commit df3ba96

Browse files
committed
fix(datetime): raise specific 'Missing zone
offset' error in timestamptz_to_nanos
1 parent d101879 commit df3ba96

2 files changed

Lines changed: 11 additions & 1 deletion

File tree

pyiceberg/utils/datetime.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ def timestamptz_to_nanos(timestamptz_str: str) -> int:
140140
ms_str = match.group(2) if match.group(2) else ""
141141
timestamptz_str_without_ns_str = match.group(1) + ms_str + match.group(4)
142142
return datetime_to_nanos(datetime.fromisoformat(timestamptz_str_without_ns_str)) + int(ns_str)
143-
if ISO_TIMESTAMPTZ_NANO.fullmatch(timestamptz_str):
143+
if ISO_TIMESTAMP_NANO.fullmatch(timestamptz_str):
144144
# When we can match a timestamp without a zone, we can give a more specific error
145145
raise ValueError(f"Missing zone offset: {timestamptz_str} (must be ISO-8601)")
146146
raise ValueError(f"Invalid timestamp with zone: {timestamptz_str} (must be ISO-8601)")

tests/utils/test_datetime.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,16 @@ def test_timestamptz_to_nanos(timestamp: str, nanos: int) -> None:
130130
assert nanos == timestamptz_to_nanos(timestamp)
131131

132132

133+
def test_timestamptz_to_nanos_missing_zone_offset() -> None:
134+
with pytest.raises(ValueError, match="Missing zone offset: 2025-02-23T20:21:44.375612001"):
135+
timestamptz_to_nanos("2025-02-23T20:21:44.375612001")
136+
137+
138+
def test_timestamp_to_nanos_unexpected_zone_offset() -> None:
139+
with pytest.raises(ValueError, match="Zone offset provided, but not expected: 2025-02-23T16:21:44.375612001-04:00"):
140+
timestamp_to_nanos("2025-02-23T16:21:44.375612001-04:00")
141+
142+
133143
@pytest.mark.parametrize("nanos, micros", [(1510871468000001001, 1510871468000001), (-1510871468000001001, -1510871468000002)])
134144
def test_nanos_to_micros(nanos: int, micros: int) -> None:
135145
assert micros == nanos_to_micros(nanos)

0 commit comments

Comments
 (0)