EpixZone fork. This is a fork of
saturating-timewith one fix:find_limitno longer hangs forever on platforms whose clock is coarser than one nanosecond. WindowsSystemTimeis aFILETIMEwith 100ns granularity, so the upstream 0.4.0 limit search spins forever there, which made everysaturating_add/saturating_subon aSystemTimehang on Windows. See the commit for details. Used by EpixNet until the fix lands upstream.
A trait for limits and saturations on types inside [std::time].
- Easy:
saturating-timeonly adds a single trait, [SaturatingTime] that is implemented for various types from the standard library. - Future-Proof: In the case that [
SaturatingTime::saturating_add()] and [SaturatingTime::saturating_sub()] become a part of the standard library, developers would only have to remove theuse saturating_time::SaturatingTimeline from their code.1 2. - Portable: The algorithm for determining the limits is portable across operating systems: enjoy this crate from Windows, Darwin, and Linux, across the BSD systems, up to exotic ones such as Hermit OS and Redox.
- Secure: This crate does not make any use of
unsafeRust code.
saturating-time is a very minimal crate that only exposes a minimal trait:
[SaturatingTime].
The trait itself offers the following methods:
- [
SaturatingTime::max_value()] – Returns the maximum value for this type. - [
SaturatingTime::min_value()] – Returns the minimum value for this type. - [
SaturatingTime::saturating_add()] – Saturating addition for this type. - [
SaturatingTime::saturating_sub()] – Saturating subtraction for this type. - [
SaturatingTime::saturating_duration_since()] - Saturating time deltas for this type.
This trait is sealed, meaning applications may not implement it themselves. However, this crate implements this trait for two structures:
- [
std::time::Instant] - [
std::time::SystemTime]
Add the following to your Cargo.toml:
[dependencies]
saturating-time = "0.4.0"If you use Rust nightly, you may want to do:
[dependencies]
saturating-time = { version = "0.4.0", features = ["nightly"] }Now, you can use saturating-time in your code:
use std::time::{Duration, SystemTime};
use saturating_time::SaturatingTime;
// Get the maximum and minimum.
let max = SystemTime::max_value();
let min = SystemTime::min_value();
assert_eq!(max.saturating_add(Duration::new(1, 0)), max);
assert_eq!(min.saturating_sub(Duration::new(1, 0)), min);
assert!(max.saturating_duration_since(SystemTime::UNIX_EPOCH) >= Duration::ZERO);The eventual goal is to get this functionality into the Rust standard library.
In December 2025, SystemTime::MIN and SystemTime::MAX got merged
into nightly.3
This feature is guarded behind time_systemtime_limits and a tracking
issue regarding the stabilization of it exists.4
In January 2025, SystemTime::saturating_add(), SystemTime::saturating_sub(),
and SystemTime::saturating_duration_since() got merged into nightly.5
This feature is guarded behind time_saturating_systemtime and a tracking
issue regarding the stabilization of it exists.6
None yet.
This crate is licensed under MIT OR Apache-2.0.
See the respective LICENSE-* files in the repository for more information.
Footnotes
-
This is an effort the maintainers are actively working upon. ↩
-
Assuming the name, signature, and behavior does not change. Unfortunately, we likely have to change the signature for the parameters because we currently use
selfwhereas the standard library uses&self. It should not be a big problem though, because bothInstantandSystemTimeimplementCopy. ↩