Skip to content

Read a naive datetime in older_than as local time - #4220

Merged
Fokko merged 2 commits into
apache:mainfrom
Rahul-cs50:fix/4182-older-than-naive-datetime
Dec 2, 2026
Merged

Fokko merged 2 commits into
apache:mainfrom
Rahul-cs50:fix/4182-older-than-naive-datetime

Conversation

@Rahul-cs50

Copy link
Copy Markdown
Contributor

Closes #4182

Rationale for this change

expire_snapshots().older_than(dt) converts the cutoff with datetime_to_millis, which reads a naive datetime as UTC. The docs call it as older_than(datetime.now() - timedelta(days=3)), and datetime.now() is a naive local time, so east of UTC the cutoff lands later than asked (in Tokyo, datetime.now() - timedelta(hours=1) expires snapshots that are seconds old) and west of UTC it lands earlier.

older_than() now reads a naive datetime as local time, as datetime.timestamp() does, by calling astimezone() on it before the conversion. An aware datetime is handled as before. datetime_to_millis itself is unchanged, since a naive value there means UTC for timestamp columns.

Are these changes tested?

Yes. test_expire_snapshots_older_than_reads_naive_datetime_as_local_time in tests/table/test_expire_snapshots.py sets TZ to Asia/Tokyo and checks that a naive cutoff one minute before a snapshot keeps it and one minute after expires it. It fails on main and passes with this change (it is skipped where time.tzset is unavailable). The issue's script now keeps all three snapshots under Asia/Tokyo, America/Los_Angeles and UTC. tests/table and tests/utils: 768 passed. ruff check and ruff format --check are clean.

Are there any user-facing changes?

older_than() with a naive datetime now uses the local time zone instead of UTC. Callers who passed naive UTC datetimes on purpose should pass an aware one (datetime.now(timezone.utc)).

expire_snapshots().older_than() converted its cutoff with
datetime_to_millis, which reads a naive datetime as UTC. The docs call
it with datetime.now() - timedelta(...), a naive local time, so east of
UTC the cutoff landed later and expired snapshots that should be kept,
and west of UTC it landed earlier. A naive datetime is now read as local
time, as datetime.timestamp() does; an aware one is unchanged.

Closes apache#4182
Copilot AI balanced review requested due to automatic review settings October 31, 2026 08:44

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@github-actions

github-actions Bot commented Dec 1, 2026

Copy link
Copy Markdown

This pull request has been marked as stale due to 30 days of inactivity. It will be closed in 1 week if no further activity occurs. If you think that's incorrect or this pull request requires a review, please simply write any comment. If closed, you can revive the PR at any time and @mention a reviewer or discuss it on the dev@iceberg.apache.org list. Thank you for your contributions.

@github-actions github-actions Bot added the stale label Dec 1, 2026

@Fokko Fokko left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nice catch, and thanks @Rahul-cs50 for fixing this, and @RyanLin5967 for the clear repro in the issue 👍 Reading a naive datetime as local time matches datetime.timestamp() and how we already use datetime.now().astimezone() elsewhere, and it makes the older_than(datetime.now() - timedelta(days=3)) example in the docs correct. Verified locally: the new test fails on main and passes with the fix.

Comment on lines 178 to +181

def test_expire_snapshots_older_than_reads_naive_datetime_as_local_time(table_v2: Table, monkeypatch: pytest.MonkeyPatch) -> None:
"""Test that older_than reads a naive datetime, as datetime.now() returns it, in the local time zone."""
if not hasattr(time, "tzset"):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nit: How about using skipif, so the condition is visible on the test itself?

Suggested change
def test_expire_snapshots_older_than_reads_naive_datetime_as_local_time(table_v2: Table, monkeypatch: pytest.MonkeyPatch) -> None:
"""Test that older_than reads a naive datetime, as datetime.now() returns it, in the local time zone."""
if not hasattr(time, "tzset"):
@pytest.mark.skipif(not hasattr(time, "tzset"), reason="time.tzset is not available on Windows")
def test_expire_snapshots_older_than_reads_naive_datetime_as_local_time(table_v2: Table, monkeypatch: pytest.MonkeyPatch) -> None:
"""Test that older_than reads a naive datetime, as datetime.now() returns it, in the local time zone."""

@Fokko
Fokko enabled auto-merge December 2, 2026 12:30
@Fokko
Fokko added this pull request to the merge queue Dec 2, 2026
Merged via the queue into apache:main with commit a926207 Dec 2, 2026
20 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

older_than reads a naive datetime as UTC and expires snapshots it should keep

3 participants