From b77ad2a7c3af0a6728a23da7c4c85e9a7e44a3c2 Mon Sep 17 00:00:00 2001 From: prabhleen2317 Date: Mon, 16 Nov 2026 11:41:10 +0530 Subject: [PATCH 1/2] Test: Add unit tests for human-readable date/time formatters Cover the previously untested to_human_* formatters in pyiceberg/utils/datetime.py, plus micros_to_days and days_to_date. --- tests/utils/test_datetime.py | 57 ++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/tests/utils/test_datetime.py b/tests/utils/test_datetime.py index 1c8b7476d8..1db006715f 100644 --- a/tests/utils/test_datetime.py +++ b/tests/utils/test_datetime.py @@ -23,6 +23,8 @@ from pyiceberg.utils.datetime import ( datetime_to_millis, datetime_to_nanos, + days_to_date, + micros_to_days, millis_to_datetime, nanos_to_hours, nanos_to_micros, @@ -32,6 +34,13 @@ timestamp_to_nanos, timestamptz_to_micros, timestamptz_to_nanos, + to_human_day, + to_human_hour, + to_human_month, + to_human_time, + to_human_timestamp, + to_human_timestamptz, + to_human_year, ) timezones = [ @@ -220,3 +229,51 @@ def test_nanos_to_micros(nanos: int, micros: int) -> None: ) def test_nanos_to_hours(nanos: int, hours: int) -> None: assert hours == nanos_to_hours(nanos) + + +def test_micros_to_days() -> None: + for micros, days in [(0, 0), (86_400_000_000, 1), (-1, -1)]: + assert micros_to_days(micros) == days + + +def test_days_to_date() -> None: + for days, expected in [(0, "1970-01-01"), (31, "1970-02-01"), (-1, "1969-12-31")]: + assert days_to_date(days).isoformat() == expected + + +def test_to_human_year() -> None: + for ordinal, expected in [(0, "1970"), (1, "1971"), (-1, "1969"), (30, "2000")]: + assert to_human_year(ordinal) == expected + + +def test_to_human_month() -> None: + for ordinal, expected in [(0, "1970-01"), (11, "1970-12"), (12, "1971-01"), (13, "1971-02"), (-1, "1969-12")]: + assert to_human_month(ordinal) == expected + + +def test_to_human_day() -> None: + for ordinal, expected in [(0, "1970-01-01"), (31, "1970-02-01"), (-1, "1969-12-31")]: + assert to_human_day(ordinal) == expected + + +def test_to_human_hour() -> None: + for ordinal, expected in [(0, "1970-01-01-00"), (1, "1970-01-01-01"), (24, "1970-01-02-00"), (-1, "1969-12-31-23")]: + assert to_human_hour(ordinal) == expected + + +def test_to_human_time() -> None: + for micros, expected in [(0, "00:00:00"), (3_661_000_000, "01:01:01"), (73_304_375_612, "20:21:44.375612")]: + assert to_human_time(micros) == expected + + +def test_to_human_timestamp() -> None: + for micros, expected in [(0, "1970-01-01T00:00:00"), (1_740_342_104_375_612, "2025-02-23T20:21:44.375612")]: + assert to_human_timestamp(micros) == expected + + +def test_to_human_timestamptz() -> None: + for micros, expected in [ + (0, "1970-01-01T00:00:00+00:00"), + (1_740_342_104_375_612, "2025-02-23T20:21:44.375612+00:00"), + ]: + assert to_human_timestamptz(micros) == expected From 4047bb6d01f23e0b563e68bfce089cf3b10016b3 Mon Sep 17 00:00:00 2001 From: prabhleen2317 Date: Mon, 16 Nov 2026 12:59:13 +0530 Subject: [PATCH 2/2] Add edge cases for pre-epoch and 1582 calendar switch Cover the Julian/Gregorian boundary (proleptic Gregorian, per spec) in days_to_date, and negative pre-1970 inputs in the human-readable timestamp formatters, per review feedback. --- tests/utils/test_datetime.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/tests/utils/test_datetime.py b/tests/utils/test_datetime.py index 1db006715f..6cbbf9c1c4 100644 --- a/tests/utils/test_datetime.py +++ b/tests/utils/test_datetime.py @@ -237,7 +237,15 @@ def test_micros_to_days() -> None: def test_days_to_date() -> None: - for days, expected in [(0, "1970-01-01"), (31, "1970-02-01"), (-1, "1969-12-31")]: + for days, expected in [ + (0, "1970-01-01"), + (31, "1970-02-01"), + (-1, "1969-12-31"), + # Proleptic Gregorian (per the Iceberg spec) has no gap at the 1582 Julian/Gregorian switch. + (-141438, "1582-10-04"), + (-141437, "1582-10-05"), + (-141427, "1582-10-15"), + ]: assert days_to_date(days).isoformat() == expected @@ -267,13 +275,18 @@ def test_to_human_time() -> None: def test_to_human_timestamp() -> None: - for micros, expected in [(0, "1970-01-01T00:00:00"), (1_740_342_104_375_612, "2025-02-23T20:21:44.375612")]: + for micros, expected in [ + (0, "1970-01-01T00:00:00"), + (-1, "1969-12-31T23:59:59.999999"), + (1_740_342_104_375_612, "2025-02-23T20:21:44.375612"), + ]: assert to_human_timestamp(micros) == expected def test_to_human_timestamptz() -> None: for micros, expected in [ (0, "1970-01-01T00:00:00+00:00"), + (-1, "1969-12-31T23:59:59.999999+00:00"), (1_740_342_104_375_612, "2025-02-23T20:21:44.375612+00:00"), ]: assert to_human_timestamptz(micros) == expected