Skip to content

Add something like Duration::as_fractional_secs #38475

Closed
@luser

Description

@luser

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 Durations, 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-feature-requestCategory: A feature request, i.e: not implemented / a PR.T-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions