Skip to content

Conversation

@cvengler
Copy link
Owner

@cvengler cvengler commented Nov 11, 2025

This merge request introduces two new constants to SystemTime: MIN and MAX, whose values represent the maximum values for the respective data type, depending upon the platform.

Technically, this value is already obtainable during runtime with the following algorithm:
Use SystemTime::UNIX_EPOCH and call checked_add (or checked_sub) repeatedly with Duration::new(0, 1) on it, until it returns None.
Mathematically speaking, this algorithm will terminate after a finite amount of steps, yet it is impractical to run it, as it takes practically forever.

Besides, this commit also adds a unit test to verify those values represent the respective minimum and maximum, by letting a checked_add and checked_sub on it fail.

In the future, the hope of the authors lies within the creation of a SystemTime::saturating_add and SystemTime::saturating_sub, similar to the functions already present in std::time::Duration.
However, for those, these constants are crucially required, thereby this should be seen as the initial step towards this direction.
With this change, implementing these functions oneself outside the standard library becomes feasible in a portable manner for the first time.

This feature (and a related saturating version of checked_{add, sub} has been requested multiple times over the course of the past few years, most notably:


This is a draft before it gets upstreamed, cc @ijackson.

@cvengler cvengler force-pushed the time_systemtime_limits branch from f508099 to 10b48fb Compare November 11, 2025 15:04
Comment on lines 516 to 517
/// This value differs a lot between platforms, but it is ensured that any
/// positive addition to [`SystemTime::MAX`] will fail.

Choose a reason for hiding this comment

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

Suggested change
/// This value differs a lot between platforms, but it is ensured that any
/// positive addition to [`SystemTime::MAX`] will fail.
/// This value differs a lot between platforms, but it is always the case that any
/// positive addition to [`SystemTime::MAX`] will fail.


/// Represents the minimum value representable by [`SystemTime`] on this platform.
///
/// This value differs a lot between platforms, but it is ensured that any

Choose a reason for hiding this comment

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

Suggested change
/// This value differs a lot between platforms, but it is ensured that any
/// This value differs a lot between platforms, but it is always the case that any

This commit introduces two new constants to SystemTime: `MIN` and `MAX`,
whose value represent the maximum values for the respective data type,
depending upon the platform.

Technically, this value is already obtainable during runtime with the
following algorithm: Use `SystemTime::UNIX_EPOCH` and call `checked_add`
(or `checked_sub`) repeatedly with `Duration::new(0, 1)` on it, until it
returns None.  Mathematically speaking, this algorithm will terminate
after a finite amount of steps, yet it is impractical to run it, as it
takes practically forever.

Besides, this commit also adds a unit test.  Concrete implementation
depending upon the platform is done in later commits.

In the future, the hope of the authors lies within the creation of a
`SystemTime::saturating_add` and `SystemTime::saturating_sub`, similar
to the functions already present in `std::time::Duration`.  However, for
those, these constants are crucially required, thereby this should be
seen as the initial step towards this direction.

This feature (and a related saturating version of `checked_{add, sub}`
has been requested multiple times over the course of the past few years,
most notably:
* rust-lang#100141
* rust-lang#133525
* rust-lang#105762
* rust-lang#71224
* rust-lang#45448
* rust-lang#52555
This commit implements `SystemTime::MIN` and `SystemTime::MAX` for for
HermitOS, which itself is more or less identical to the Unix
implementation.
This commit implements `SystemTime::MIN` and `SystemTime::MAX` for sgx.

The implementation uses a `Duration` to store the Unix time, thereby
implying `Duration::ZERO` and `Duration::MAX` as the limits.
This commit implements `SystemTime::MIN` and `SystemTime::MAX` for
solid.

The implementation uses a `time_t` to store the system time within a
single value (i.e. no dual secs/nanosecs handling), thereby implying its
`::MIN` and `::MAX` values as the respective boundaries.
This commit implements `SystemTime::MIN` and `SystemTime::MAX` for the
UEFI platform.

UEFI has a weird way to store times, i.e. a very complicated struct.
The standard proclaims "1900-01-01T00:00:00+0000" to be the lowest
possible value and `MAX_UEFI_TIME` is already present for the upper
limit.
This commit implements `SystemTime::MIN` and `SystemTime::MAX` for
wasip1.

Similar to sgx, a `Duration` is used to store the time, thereby
depending on those limits.
See the wasip1 implementation, which is functionally equivalent.
This commit implements `SystemTime::MIN` and `SystemTime::MAX` for the
Windows platform.

Windows is weird.  The Win32 documentation makes no statement on a
maximum value here.  Next to this, there are two conflicting types:
`SYSTEMTIME` and `FILETIME`.  Rust's Standard Library uses `FILETIME`,
whose limit will (probably) be `i64::MAX` packed into two integers.
However, `SYSTEMTIME` has a lower-limit.
This commit implements `SystemTime::MIN` and `SystemTime::MAX` for xous.

It is similar to wasip1, wasip2, and sgx in the sense of once again
using a `Duration` to store the value.
This commit implements `SystemTime::MIN` and `SystemTime::MAX` for all
unsupported platforms.

Unsupported platforms store a `SystemTime` in a `Duration`, just like
wasip1, sgx, and a few others, thereby implying `Duration::ZERO` and
`Duration::MAX` as the respective limits.
@cvengler cvengler force-pushed the time_systemtime_limits branch from 10b48fb to 143f4ce Compare November 11, 2025 16:11
@cvengler cvengler closed this Nov 11, 2025
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