Skip to content

Tracking Issue for Duration::try_from_secs_{f32, f64} #83400

Closed

Description

@mbartlett21

Feature gate: #![feature(duration_checked_float)]

This is a tracking issue for checked methods to construct a duration from a floating-point value of seconds without panicking.

Public API

// core::time

impl Duration {
    pub const fn try_from_secs_f32(secs: f32) -> Result<Duration, FromFloatSecsError>;
    pub const fn try_from_secs_f64(secs: f64) -> Result<Duration, FromFloatSecsError>;
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FromFloatSecsError { ... }

impl core::fmt::Display for FromFloatSecsError { ... }
// std::error

impl std::error::Error for FromFloatSecsError { ... }

Steps / History

Unresolved Questions

Activity

  1. added
    C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFC
    T-libs-api[DEPRECATED; DO NOT USE]
    on Mar 23, 2021
  2. changed the title [-]Tracking Issue for `Duration::checked_from_secs_{f32, f64}`[/-] [+]Tracking Issue for `Duration::try_from_secs_{f32, f64}`[/+] on Mar 23, 2021
  3. workingjubilee commented on Apr 10, 2021

    @workingjubilee
    Member

    This needs more testing to vet its behavior in the face of various interesting floating point values.
    In particular, subnormal values and values close to the limit of f32 and f64's upper range.

  4. mbartlett21 commented on Apr 15, 2021

    @mbartlett21
    ContributorAuthor

    This needs more testing to vet its behavior in the face of various interesting floating point values.
    In particular, subnormal values and values close to the limit of f32 and f64's upper range.

    This just currently checks what the othee panicking methods for the same thing do.

  5. added a commit that references this issue on Jun 15, 2021
  6. newpavlov commented on Oct 27, 2021

    @newpavlov
    Contributor

    The new code in #90247 should handle all interesting values perfectly.

  7. nvzqz commented on May 13, 2022

    @nvzqz
    Contributor

    What is left to stabilize this? It is important to me that time conversions I'm using don't panic.

  8. mbartlett21 commented on May 16, 2022

    @mbartlett21
    ContributorAuthor

    There is still an unresolved question:

    What should the error type be called?

    Once that is resolved, we'll need a stabilisation PR and then, once that gets approved, it will be a month or two until it appears in stable

    EDIT: Note: #96051 changes the rounding mode to round-to-nearest instead of truncate. (note on the note: That pr is also changing the stabilised methods from_secs_*, but it would be nice to have it correct when it is first stabilised)

  9. arniu commented on Aug 15, 2022

    @arniu

    Why not just impl TryFrom<f32 | f64> for Duration?

  10. mbartlett21 commented on Aug 15, 2022

    @mbartlett21
    ContributorAuthor

    Because it isn't clear from TryFrom that it is meant to be seconds. It could be milliseconds or hours or days or some other reasonable measurement.
    This is the same reason why Duration doesn't impl From<u128> or something. It could likewise be a lot of other units.

  11. lopopolo commented on Sep 25, 2022

    @lopopolo
    Contributor

    I came across this unstable API when debugging a panic in a time crate I maintain:

    I will prepare a stabilization PR shortly and hope we can resolve the open question on error type naming in the context of an FCP. I hope that's an ok way to approach this.

  12. lopopolo commented on Sep 25, 2022

    @lopopolo
    Contributor

    I have pushed a stabilization PR and stabilization report here: #102271.

    The stabilization PR renames the error type to TryFromFloatSecs to indicate the fallibility of the method like Vec::try_reserve and TryReserveError.

  13. added a commit that references this issue on Oct 14, 2022
  14. added a commit that references this issue on Oct 24, 2022
  15. added a commit that references this issue on Oct 24, 2022
  16. Dylan-DPC commented on Feb 29, 2024

    @Dylan-DPC
    Member

    This is stabilised already in #102271

  17. added a commit that references this issue on Mar 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCT-libs-api[DEPRECATED; DO NOT USE]

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions