Skip to content

Conversation

@cvengler
Copy link

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 to figure out some CI problems, before making this ready for review.

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.
@rustbot rustbot added O-hermit Operating System: Hermit O-SGX Target: SGX O-solid Operating System: SOLID O-unix Operating system: Unix-like O-wasi Operating system: Wasi, Webassembly System Interface O-windows Operating system: Windows S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Nov 11, 2025
@cvengler
Copy link
Author

For what it is worth: Right now, the Rust ecosystem does weird stuff in order to figure out a limit.

chrono for example picks arbitrary value that is far in the future but not too far in the future (as I have been told).

arti, the project I work upon and from which the motivation of this PR stems from, uses a custom SaturatingSystemTime, that is highly unportable and very Unix specific: https://gitlab.torproject.org/tpo/core/arti/-/blob/main/crates/tor-dirserver/src/mirror/operation.rs?ref_type=heads#L93

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.
@rust-log-analyzer
Copy link
Collaborator

The job pr-check-2 failed! Check out the build log: (web) (plain enhanced) (plain)

Click to see the possible cause of the failure (guessed by this bot)

@Kivooeo
Copy link
Member

Kivooeo commented Nov 11, 2025

I'm pretty sure, though I could be wrong that this will need an ACP first before any work

@cvengler
Copy link
Author

I'm pretty sure, though I could be wrong that this will need an ACP first before any work

I am fine in opening such a thing.
We can always defer such an issue if not required. 🙂

cc @ijackson

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

O-hermit Operating System: Hermit O-SGX Target: SGX O-solid Operating System: SOLID O-unix Operating system: Unix-like O-wasi Operating system: Wasi, Webassembly System Interface O-windows Operating system: Windows S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants