-
Notifications
You must be signed in to change notification settings - Fork 13.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Docs for Duration are lacking #39949
Comments
This seems like just as much a question for @rust-lang/libs as it does @rust-lang/docs |
How about this non-lossy function? fn as_nanos(&self) -> u128 {
self.secs as u128 * 1_000_000_000 + self.nanos as u128
} Printing: println!("{:.3}", duration.as_nanos() as f64 / 1e9); |
The It looks like several |
They were not blocked on i128, they're blocked on someone writing up a proposal for what specifically to do. i128 returns are on option. |
Docs team triage: adding p-medium for option 2. |
What's wrong with the first proposal anyway? Why not just use an
( Alternatively there could be something like
|
@stjepang mentioned that There are just under 32*10^6 seconds in a year. That's less than 2^25. Put another way, an Is it possible that some application will require more accuracy than that? Perhaps, but it's going to be some pretty special application and I'd be surprised if |
@dhardy You raise a fair point about accuracy requirements in practice. Could you elaborate why you dislike Also, what do you think about this API? fn secs(&self) -> u64; // maybe secs_part, as in "the seconds part of the duration"
fn nanos(&self) -> u32; // maybe nanos_part, as in "the nanos part of the duration"
fn total_secs(&self) -> f64;
fn total_nanos(&self) -> f64; |
@stjepang I have nothing against the existence of
The latter is something I implemented using a trait as a local solution, but could also be a good solution here. |
Opened a PR that adds a doc example alongside |
…teveklabnik Improvements to `std::time::Duration` doc examples. Opened primarily for the last commit, in response to comments in rust-lang#39949.
…teveklabnik Improvements to `std::time::Duration` doc examples. Opened primarily for the last commit, in response to comments in rust-lang#39949.
Is there anything actionable left to do here from a docs standpoint? Seems like the remaining discussion in the GitHub Issue is related to new functionality on |
From #39949 (comment), looks like there's other issues for the other libs parts of this. Let's close. |
The most common use of
Duration
for me is benchmarking, and it's always a very unpleasant experience. This is what I typically write:Output:
Duration { secs: 1, nanos: 59219906 }
Wait, how much time has elapsed? That's terribly unreadable... :(
The docs for
Duration
are not very helpful either. When you first see it,Duration
is pretty confusing, mostly because of theas_secs
/subsec_nanos
split.Then I take a look at the docs for
Instant
, but the examples show how to print the seconds part only.Anyways, all I want to do is just print how much time has elapsed without squinting to connect
secs
andnanos
together. Finally, I arrive at this horrible solution:Three ways of fixing this come to my mind:
Duration
that converts it to a (lossy)f64
format.Instant
andDuration
that demonstrate how to print the duration time in a human readable form (e.g. total seconds with precision to 3 decimals).Debug
formatting forDuration
and print it like this:Duration(secs.nanos: 1.059219906)
(note the leading zero). Or maybe evenDuration { secs: 1, nanos: 059219906 }
(again, the leading zero).What do you think?
The text was updated successfully, but these errors were encountered: