Skip to content

encode whole-second datetime timestamps as integers across full range#317

Merged
agronholm merged 5 commits into
agronholm:masterfrom
sahvx655-wq:datetime-timestamp-i64
Jun 28, 2026
Merged

encode whole-second datetime timestamps as integers across full range#317
agronholm merged 5 commits into
agronholm:masterfrom
sahvx655-wq:datetime-timestamp-i64

Conversation

@sahvx655-wq

Copy link
Copy Markdown
Contributor

Changes

Under datetime_as_timestamp, encode_datetime decides whether a value can be written as an integer by narrowing the POSIX timestamp through a u32 and comparing it back against the float. That cast saturates: anything negative collapses to 0 and anything past 2106-02-07 collapses to u32::MAX, so the equality check never holds for a whole-second datetime before 1970 or after 2106. Those values still round-trip, but they go out as a tag 1 float64 rather than the compact integer the original pure-Python encoder always produced for whole-second timestamps. I spotted it while diffing the Rust output against the historical implementation, where datetime(1969, 12, 31, tzinfo=utc) came back as c1fb... instead of a negint.

Widening the cast to i64 restores the integer path across the whole representable datetime range, negative timestamps included, while the existing timestamp_int as f64 == timestamp_float guard still falls back to a float when there are sub-second microseconds. The decision belongs here in the encoder because it is what shapes the wire form. Left as is it emits non-canonical, larger output and silently disagrees with every other cbor2 release for any timestamp outside a 136-year window.

Checklist

  • You've added tests (in tests/) which would fail without your patch
  • You've updated the documentation (in docs/), in case of behavior changes or new features
  • You've added a new changelog entry (in docs/versionhistory.rst).

@coveralls

coveralls commented Jun 10, 2026

Copy link
Copy Markdown

Coverage Status

coverage: 94.659%. remained the same — sahvx655-wq:datetime-timestamp-i64 into agronholm:master

@agronholm agronholm left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Looks good, thanks!

@agronholm agronholm merged commit 1c827f1 into agronholm:master Jun 28, 2026
10 of 15 checks passed
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.

3 participants