Skip to content

Time units #52556

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

Closed
wants to merge 12 commits into from
32 changes: 32 additions & 0 deletions src/libcore/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,29 @@ const NANOS_PER_MICRO: u32 = 1_000;
const MILLIS_PER_SEC: u64 = 1_000;
const MICROS_PER_SEC: u64 = 1_000_000;

/// 1 nanosecond `Duration`
#[unstable(feature = "time_units", issue = "0")]
const NS: Duration = Duration::from_nanos(1);
/// 1 microsecond `Duration`
#[unstable(feature = "time_units", issue = "0")]
const US: Duration = Duration::from_micros(1);
#[unstable(feature = "time_units", issue = "0")]
/// 1 millisecond `Duration`
#[unstable(feature = "time_units", issue = "0")]
const MS: Duration = Duration::from_millis(1);
/// 1 second `Duration`
#[unstable(feature = "time_units", issue = "0")]
const S: Duration = Duration::from_secs(1);
/// 1 minute `Duration`
#[unstable(feature = "time_units", issue = "0")]
const M: Duration = Duration::from_secs(60);
/// 1 hour `Duration`
#[unstable(feature = "time_units", issue = "0")]
const H: Duration = Duration::from_secs(60*60);
/// 1 day `Duration`
#[unstable(feature = "time_units", issue = "0")]
const D: Duration = Duration::from_secs(24*60*60);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to #47097, I doubt we want to support hours and days.

Abbreviating minutes to M is also confusing (months? meters?). The SI standard short form MIN conflicts in meaning with "minimum". I would like to leave this out as well, keeping only S, MS, US and NS.


/// A `Duration` type to represent a span of time, typically used for system
/// timeouts.
///
Expand Down Expand Up @@ -501,6 +524,15 @@ impl Mul<u32> for Duration {
}
}

#[unstable(feature = "time_units", issue = "0")]
impl Mul<Duration> for u32 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This impl is going to be insta-stable unfortunately.

Copy link
Contributor Author

@newpavlov newpavlov Jul 20, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should I keep the attribute or use instead something like #[stable(feature = "time_units", since = "1.29.0")]?

Copy link
Member

@kennytm kennytm Jul 20, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@newpavlov The latter (with a different feature name, otherwise tidy will reject it).

Copy link
Contributor Author

@newpavlov newpavlov Jul 20, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, what should I use for feature then? With time_units it can not be built.

UPD: Ah, haven't noticed your update. :)

type Output = Duration;

fn mul(self, rhs: Duration) -> Duration {
rhs.checked_mul(self).expect("overflow when multiplying scalar by duration")
}
}

#[stable(feature = "time_augmented_assignment", since = "1.9.0")]
impl MulAssign<u32> for Duration {
fn mul_assign(&mut self, rhs: u32) {
Expand Down
1 change: 1 addition & 0 deletions src/libstd/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ use sys_common::FromInner;

#[stable(feature = "time", since = "1.3.0")]
pub use core::time::Duration;
pub use core::time::{NS, US, MS, S, M, H, D};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line needs an #[unstable] attribute.


/// A measurement of a monotonically nondecreasing clock.
/// Opaque and useful only with `Duration`.
Expand Down