Skip to content

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

Open
Rahul-cs50 wants to merge 1 commit into
apache:mainfrom
Rahul-cs50:fix/4182-older-than-naive-datetime
Open

Rahul-cs50 wants to merge 1 commit into
apache:mainfrom
Rahul-cs50:fix/4182-older-than-naive-datetime

Conversation

@Rahul-cs50

Copy link
Copy Markdown

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.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

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

2 participants

⌂ Proxy