Skip to content

std: make Instant a signed Duration on all platforms - #158368

Open
joboet wants to merge 2 commits into
rust-lang:mainfrom
joboet:instant_shared_repr
Open

std: make Instant a signed Duration on all platforms#158368
joboet wants to merge 2 commits into
rust-lang:mainfrom
joboet:instant_shared_repr

Conversation

@joboet

@joboet joboet commented Jun 24, 2026

Copy link
Copy Markdown
Member

This fixes #156142 for all targets using a very principled solution: representing Instant the same way on all platforms.

Currently, not all platforms have the same range and precision of Instant: while all UNIX platforms (and Hermit) represent Instant using a pair consisting of an 64-bit signed second field and a non-negative nanosecond offset and thus are able to represent timepoints well before the system epoch, most other platforms use a Duration with the result that operations like Instant::now() - Duration::from(/* 100 years */) do not succeed. SOLID even represents Instant using a microsecond counter, which means that nanosecond arithmetic is lossy.

To improve the portability of time arithmetic, I propose using the same representation for Instant on all platforms: a 64-bit signed second field and a non-negative nanosecond offset (i.e. a signed Duration). This means that:

  • Since all platform timestamp types can either represent the same time range or a smaller one, Instant::now can never fail due to an out-of-bounds timestamp from the platform.
  • Time arithmetic works identically on all platforms, operations like adding or subtracting large but not unreasonable durations do not fail (as long as the platform chooses a reasonable epoch).

Note that this does not add any significant conversion cost since all platforms currently convert to some seconds-based unit anyway.

I've split this PR into two parts: the first makes the actual changes to the implementation, the second adjusts the documentation to remove any mentions of platform-specific representations.

@rustbot rustbot added O-SGX Target: SGX S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Jun 24, 2026
@rustbot

rustbot commented Jun 24, 2026

Copy link
Copy Markdown
Collaborator

r? @Darksonn

rustbot has assigned @Darksonn.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: @ChrisDenton, libs
  • @ChrisDenton, libs expanded to 13 candidates
  • Random selection from 6 candidates

@joboet joboet added I-libs-nominated Nominated for discussion during a libs team meeting. I-libs-api-nominated [DEPRECATED; DO NOT USE] labels Jun 24, 2026

@joboet joboet Jun 24, 2026

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This does more than required for the conversion – but the existing implementation was both incorrect (the TSC-based time passed 1000 times slower than real time due to a faulty conversion) and lossy. Since our UEFI code isn't free from issues anyway I took the liberty of adjusting this to what I see as a better algorithm. I can split this out if you'd like.

View changes since the review

@rust-log-analyzer

This comment has been minimized.

@joboet
joboet force-pushed the instant_shared_repr branch from d8f7cbd to c8d3117 Compare June 24, 2026 17:11
@rust-log-analyzer

This comment has been minimized.

Comment thread library/std/src/sys/time/uefi.rs Outdated
@joboet
joboet force-pushed the instant_shared_repr branch from c8d3117 to a35d18f Compare June 25, 2026 12:59
@ChrisDenton

Copy link
Copy Markdown
Member

So to state this explicitly, this makes Instant a signed Duration?

@joboet
joboet force-pushed the instant_shared_repr branch from a35d18f to a399bb4 Compare June 25, 2026 13:57
@joboet

joboet commented Jun 25, 2026

Copy link
Copy Markdown
Member Author

So to state this explicitly, this makes Instant a signed Duration?

Yes, exactly.

@joboet joboet changed the title std: represent Instant in the same way on all platforms std: make Instant a signed Duration on all platforms Jun 25, 2026
@Amanieu

Amanieu commented Jun 30, 2026

Copy link
Copy Markdown
Member

@rfcbot merge libs

@rust-rfcbot

rust-rfcbot commented Jun 30, 2026

Copy link
Copy Markdown
Collaborator

@Amanieu has proposed to merge this. The next step is review by the rest of the tagged team members:

Concerns:

Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up!

See this document for info about what commands tagged team members can give me.

@rust-rfcbot rust-rfcbot added proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. labels Jun 30, 2026
@the8472

the8472 commented Jun 30, 2026

Copy link
Copy Markdown
Member

Since this changes the windows implementation to do more work it would be good to have a benchmark how this affects throughput/latency of Instant::now()

@joboet

joboet commented Jun 30, 2026

Copy link
Copy Markdown
Member Author

Since this changes the windows implementation to do more work it would be good to have a benchmark how this affects throughput/latency of Instant::now()

It doesn't, the Windows implementation already converts to seconds/nanoseconds on every Instant::now. The difference is that between unsigned division and remainder and signed div_euclid/rem_euclid, which I think is safe to assume negligible.

@Darksonn

Darksonn commented Jul 1, 2026

Copy link
Copy Markdown
Member

@rustbot label -S-waiting-on-review +S-waiting-on-fcp

@rustbot rustbot added S-waiting-on-fcp Status: PR is in FCP and is awaiting for FCP to complete. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 1, 2026
@Amanieu Amanieu removed I-libs-api-nominated [DEPRECATED; DO NOT USE] I-libs-nominated Nominated for discussion during a libs team meeting. labels Jul 1, 2026
@rust-bors

rust-bors Bot commented Jul 10, 2026

Copy link
Copy Markdown
Contributor

☔ The latest upstream changes made this pull request unmergeable. Please resolve the merge conflicts by rebasing.

@joboet

joboet commented Aug 15, 2026

Copy link
Copy Markdown
Member Author

@rustbot label +I-libs-nominated

Just to make sure this doesn't get forgotten... or are there any concerns?

@rustbot rustbot added the I-libs-nominated Nominated for discussion during a libs team meeting. label Aug 15, 2026
@joshtriplett

Copy link
Copy Markdown
Member

This seems generally reasonable to me.

@rfcbot reviewed

Two thoughts, though:

First: I'd like to hear from @BurntSushi, as the author of jiff, before we merge this.

@rfcbot concern input-from-andrew-gallant

Second, and this is not a blocker for this proposal: I'm wondering if we should provide SignedDuration as a library type. It seems useful in other contexts.

(I briefly entertained the idea of SignedDuration stealing a sign bit from the nanoseconds, in order to have the same maximum as Duration and let conversions to it and signed-difference subtractions be infallible, but I don't think it's worth the complexity in practice, and conversions from it back to Duration would still be fallible.)

@nia-e nia-e removed the I-libs-nominated Nominated for discussion during a libs team meeting. label Aug 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. O-SGX Target: SGX proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. S-waiting-on-fcp Status: PR is in FCP and is awaiting for FCP to complete. 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.

[Windows] computing an Instant from before program startup might panic

10 participants