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: 6 additions & 2 deletions pyiceberg/utils/datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,9 @@ def timestamp_to_nanos(timestamp_str: str) -> int:
if match := ISO_TIMESTAMP_NANO.fullmatch(timestamp_str):
# Python datetime does not have native nanoseconds support
# Hence we need to extract nanoseconds timestamp manually
ns_str = match.group(3) or "0"
# group(3) holds the sub-microsecond digits (fraction positions 7-9), so
# right-pad to 3 digits before reading them as nanoseconds (e.g. "7" -> 700).
ns_str = (match.group(3) or "0").ljust(3, "0")
ms_str = match.group(2) if match.group(2) else ""
timestamp_str_without_ns_str = match.group(1) + ms_str
return datetime_to_nanos(datetime.fromisoformat(timestamp_str_without_ns_str)) + int(ns_str)
Expand All @@ -136,7 +138,9 @@ def timestamptz_to_nanos(timestamptz_str: str) -> int:
if match := ISO_TIMESTAMPTZ_NANO.fullmatch(timestamptz_str):
# Python datetime does not have native nanoseconds support
# Hence we need to extract nanoseconds timestamp manually
ns_str = match.group(3) or "0"
# group(3) holds the sub-microsecond digits (fraction positions 7-9), so
# right-pad to 3 digits before reading them as nanoseconds (e.g. "7" -> 700).
ns_str = (match.group(3) or "0").ljust(3, "0")
ms_str = match.group(2) if match.group(2) else ""
timestamptz_str_without_ns_str = match.group(1) + ms_str + match.group(4)
return datetime_to_nanos(datetime.fromisoformat(timestamptz_str_without_ns_str)) + int(ns_str)
Expand Down
4 changes: 4 additions & 0 deletions tests/utils/test_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ def test_datetime_to_nanos(datetime_: datetime, nanos: int) -> None:
[
("1970-01-01T00:00:00", 0),
("2025-02-23T20:21:44.375612", 1740342104375612000),
("2025-02-23T20:21:44.3756127", 1740342104375612700),
("2025-02-23T20:21:44.37561278", 1740342104375612780),
Comment on lines +114 to +115

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could modify the value to something like 2025-02-23T20:21:44.12345678 to easily understand the precision.

Additionally, we should cover all precisions (1~5 look missing). However, follow-up is acceptable.

("2025-02-23T20:21:44.375612001", 1740342104375612001),
],
)
Expand All @@ -128,6 +130,8 @@ def test_timestamp_to_nanos_unexpected_zone_offset() -> None:
[
("1970-01-01T00:00:00+00:00", 0),
("2025-02-23T16:21:44.375612-04:00", 1740342104375612000),
("2025-02-23T16:21:44.3756127-04:00", 1740342104375612700),
("2025-02-23T16:21:44.37561278-04:00", 1740342104375612780),
("2025-02-23T16:21:44.375612001-04:00", 1740342104375612001),
],
)
Expand Down