-
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
Add something like Duration::as_fractional_secs
#38475
Comments
👍 I've found myself wanting something like this too quite a bit in the past. |
I'd recommend rewording to this as |
There are a bunch of convenience accessors/constructors that would be great to add to |
A somewhat orthogonal use case to keep in mind: I often add ad-hoc time measurements to my code as a difference between two |
I looked around, and but it's just re-exporting It outputs in ISO 8601 format, which looks like There's a chrono-humanize crate, which has a I couldn't find any other relevant existing crates, if someone knows about something I'd be interested to hear! Python's I'm not familiar enough with datetime handling in other languages to come up with other examples without a lot of digging, and I've already spent longer writing this comment than I intended. :) |
Related rust-lang/rfcs#1545 and #35868 |
If you are interested, I just released floating-duration (very basic). |
Also: shouldn't this thread be moved to the RFC repo? |
The |
I've found myself wanting to format a
Duration
as a human-readable number of seconds with a fractional portion. Of course I screwed up the conversion when I wrote that code, thankfully someone sent me a PR to fix it:mozilla/sccache#52
It would be nice if this was a built-in API. Ideally we'd have
pub fn as_fractional_secs(&self) -> f64
, but maybe that'd lose too much precision? I suppose we could alternately do something likepub fn as_fractional_secs(&self) -> (u64, f32)
, where it returns(self.as_secs(), self.subsec_nanos() as f32 / 1000_000f32)
. That's a bit less ergonomic, but not awful.Alternately, since it's already easy to do arithmetic on
Duration
s, another take on this would be to implementDisplay
forDuration
, which could write the value as seconds, and support the floating-point precision format field for fractional seconds.The text was updated successfully, but these errors were encountered: