Skip to content

Add lossless DateTime reconstruction from nanosecond timestamps - #37

Merged
egorsmkv merged 1 commit into
masterfrom
fix/36-unix-timestamp-nanos
Oct 18, 2026
Merged

egorsmkv merged 1 commit into
masterfrom
fix/36-unix-timestamp-nanos

Conversation

@egorsmkv

Copy link
Copy Markdown
Contributor

Closes #36.

Adds DateTime::from_unix_timestamp_nanos(i128) and Python DateTime.from_unix_timestamp_nanos(nanos: int), allowing from_unix_timestamp_nanos(dt.unix_timestamp_nanos()) == dt for every valid DateTime. For example, -1 reconstructs 1969-12-31T23:59:59.999999999Z exactly.

The Rust constructor uses Euclidean division and checked integer conversions, then delegates to the existing seconds/nanoseconds constructor. Out-of-date-range values return DateError::OutOfRange / Python ValueError; Python integers outside i128 raise OverflowError. Existing APIs, pickle reconstruction, dependencies and free-threaded support are unchanged. Includes type stubs, API documentation and executable examples.

Regression coverage includes exact epoch and pre-epoch instants, subsecond boundaries, BCE/leap dates, both i32 year endpoints, one nanosecond outside each date limit, i128 extremes and Python input validation. Deterministic samples span the entire supported timestamp interval, with independent differential checks against the time crate in its supported range. Before implementation, the existing 200 Python tests passed and the new regression failed because the constructor was absent.

Validation on Linux x86_64 (Rust 1.99.1):

  • cargo test --locked and cargo test --locked --no-default-features: 24 integration tests + 1 doctest pass each.
  • cargo check --locked --no-default-features and cargo fmt --check: pass.
  • cargo clippy --locked --all-targets -- -D warnings and the same with --features python: pass.
  • CPython 3.12.14 and free-threaded CPython 3.14.8: all 347 Python tests pass on each; importing fasttime leaves the GIL disabled on 3.14t.
  • All Python examples execute successfully on CPython 3.12.
  • Ruff 0.16.10 lint/format checks and Pyright 1.1.414 (--pythonpath .venv/bin/python): pass.

@egorsmkv
egorsmkv merged commit 3fb4175 into master Oct 18, 2026
24 checks passed
@egorsmkv
egorsmkv deleted the fix/36-unix-timestamp-nanos branch October 18, 2026 10:46
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.

Missing round-trip conversion to/from timestamp

1 participant