Skip to content

Commit 93554a4

Browse files
authored
fix: nanos_to_hours returning incorrect hour values (#2920)
# Rationale for this change This PR removes an extra 0 from the nanos_to_hours conversion method which resulted in incorrect partitioning. Found this while testing out the V3 `TimestampNanoType` and partitioning. ## Are these changes tested? Yes, I added some tests from the java side to ensure we align. - https://github.com/apache/iceberg/blob/9b573c7b46950a41d614c4240752f255282d8c1f/api/src/test/java/org/apache/iceberg/util/TestDateTimeUtil.java#L70 ## Are there any user-facing changes? No
1 parent 2b84bf5 commit 93554a4

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

pyiceberg/utils/datetime.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ def nanos_to_time(nanos: int) -> time:
273273

274274
def nanos_to_hours(nanos: int) -> int:
275275
"""Convert a timestamp in nanoseconds to hours from 1970-01-01T00:00."""
276-
return nanos // 3_600_000_000_0000
276+
return nanos // 3_600_000_000_000
277277

278278

279279
def nanos_to_micros(nanos: int) -> int:

tests/utils/test_datetime.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
datetime_to_millis,
2424
datetime_to_nanos,
2525
millis_to_datetime,
26+
nanos_to_hours,
2627
nanos_to_micros,
2728
time_str_to_nanos,
2829
time_to_nanos,
@@ -132,3 +133,14 @@ def test_timestamptz_to_nanos(timestamp: str, nanos: int) -> None:
132133
@pytest.mark.parametrize("nanos, micros", [(1510871468000001001, 1510871468000001), (-1510871468000001001, -1510871468000002)])
133134
def test_nanos_to_micros(nanos: int, micros: int) -> None:
134135
assert micros == nanos_to_micros(nanos)
136+
137+
138+
@pytest.mark.parametrize(
139+
"nanos, hours",
140+
[
141+
(1510871468000001001, 419686),
142+
(-1510871468000001001, -419687),
143+
],
144+
)
145+
def test_nanos_to_hours(nanos: int, hours: int) -> None:
146+
assert hours == nanos_to_hours(nanos)

0 commit comments

Comments
 (0)