WinRT
TimeSpanandDateTimewith idiomatic Rust conversions.
- π¦ crates.io
- π docs.rs
- π Getting started
- π Source
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.
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.
src/bindings.rs is generated by tool_bindings from
crates/tools/bindings/src/time.txt; the conversion logic is hand-written.
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.
Run cargo test -p windows-time; see also the workspace test crates.