Description
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 like pub 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 implement Display
for Duration
, which could write the value as seconds, and support the floating-point precision format field for fractional seconds.