Skip to content

Latest commit

History

History
41 lines (28 loc) 路 1.8 KB

File metadata and controls

41 lines (28 loc) 路 1.8 KB

windows-time

WinRT TimeSpan and DateTime with idiomatic Rust conversions.

windows-time provides the two WinRT time primitives as plain #[repr(C)] Rust types: TimeSpan (a duration, stored as 100-nanosecond ticks) and DateTime (an instant on a 1601-based UTC clock). Both are Copy, support the usual arithmetic and comparison operators, convert to and from std::time types, and Display as ISO-8601.


Internal documentation

The remainder of this page covers how the crate is built and maintained. It is for contributors and is not needed to use windows-time.

How it's built

src/bindings.rs is generated by tool_bindings from crates/tools/bindings/src/time.txt; the conversion logic is hand-written.

Why not substitute the type at the projection layer

C++/WinRT replaces TimeSpan with std::chrono::duration<int64_t, std::ratio<1, 10'000'000>> and DateTime with the matching time_point. That works only because those chrono types are a single int64_t and therefore bit-for-bit ABI-compatible with the WinRT struct.

Rust has no equivalent. core::time::Duration is { u64 seconds, u32 nanos } (96 bits, unsigned) - it can't represent negative TimeSpan values and isn't ABI-compatible with i64, and std::time::SystemTime is opaque and platform-defined. So the ABI struct stays a single i64, and windows-time provides explicit conversions to and from the std types instead of aliasing them.

Testing

Run cargo test -p windows-time; see also the workspace test crates.