Rfcs: More methods for std::time::Duration

Created on 22 Feb 2017  Â·  5Comments  Â·  Source: rust-lang/rfcs

Right now, we have:

  • from_secs
  • from_millis
  • as_secs
  • subsec_nanos

It'd be nice if we also had:

  • from_hours (note: potential for overflow)
  • from_mins (note: potential for overflow)
  • from_micros
  • from_nanos
  • as_hours
  • as_mins
  • subsec_millis
  • subsec_micros

And potentially also:

  • from_days (note: potential for overflow)
  • as_days
  • as_millis (note: potential for overflow)
  • as_micros (note: potential for overflow)
  • as_nanos (note: potential for overflow)

(Note that as_millis is already suggested by #1545).

time::Duration already has a lot of these, and it'd be nice if the libstd version did too. These don't seem that hard to implement or maintain, and they seem useful enough to have.

T-libs

Most helpful comment

The reason why these larger, more-confusing units should be included (IMHO) is precisely because they are hard to get right. I think C#'s TimeSpan is a good model for Duration; it provides an easy way to go from logical units to system time and abstracts away all the things that could go wrong. Conversation to and from "higher order" units via the from_xxx and as_xxx methods is a great boon to developers of "real" software, and if implemented properly, could save a whole class of problems for developers down the road.

If this should not be included in core, there should at least be a blessed crate for this; already there is a huge and unwieldy gap between std and the time crate, which is a source of enormous confusion to the entire community.

All 5 comments

Yes, time::Duration is lacking quite some functionality.

Go uses constants to work with durations. Maybe we could introduce this idea in Rust:

let four_seconds = time::SECOND * 4;

let five_hundred_millis = time::MILLISECOND * 500;

let one_and_a_half_second = time::SECOND + time::SECOND / 2;

let one_day = time::HOUR * 24;

Similarly, as_* could be realized by division and subsec_* by the remainder.

The duration reform rfc
explicitly avoids adding units such as hours or days.

On Mar 17, 2017 4:33 PM, "polyfloyd" notifications@github.com wrote:

Yes, time::Duration is lacking quite some functionality.

Go https://golang.org/pkg/time/#Duration uses constants to work with
durations. Maybe we could introduce this idea in Rust:

let four_seconds = time::SECOND * 4;
let five_hundred_millis = time::MILLISECOND * 500;
let one_and_a_half_second = time::SECOND + time::SECOND / 2;
let one_day = time::HOUR * 24;

Similarly, as_* could be realized by division and subsec_* by the
remainder.

—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/rust-lang/rfcs/issues/1917#issuecomment-287370092,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AApc0kQw6293PzpBZgrCO2PtdzOoT3Ffks5rmpmmgaJpZM4MIAni
.

The reason why these larger, more-confusing units should be included (IMHO) is precisely because they are hard to get right. I think C#'s TimeSpan is a good model for Duration; it provides an easy way to go from logical units to system time and abstracts away all the things that could go wrong. Conversation to and from "higher order" units via the from_xxx and as_xxx methods is a great boon to developers of "real" software, and if implemented properly, could save a whole class of problems for developers down the road.

If this should not be included in core, there should at least be a blessed crate for this; already there is a huge and unwieldy gap between std and the time crate, which is a source of enormous confusion to the entire community.

Some of these seem to be open as https://github.com/rust-lang/rust/pull/47097

Why is only as_hours() using the long form as compared to the others? What if others typed as_hrs()? Will the error redirect to as_hours()?

Was this page helpful?
0 / 5 - 0 ratings