diff --git a/src/libcore/lib.rs b/src/libcore/lib.rs index bbe6ae8619fec..5dcfd2c9f773b 100644 --- a/src/libcore/lib.rs +++ b/src/libcore/lib.rs @@ -122,6 +122,7 @@ #![feature(const_slice_len)] #![feature(const_str_as_bytes)] #![feature(const_str_len)] +#![feature(time_units)] #[prelude_import] #[allow(unused)] diff --git a/src/libcore/time.rs b/src/libcore/time.rs index 54973b7b7783a..c26a3225ad4ba 100644 --- a/src/libcore/time.rs +++ b/src/libcore/time.rs @@ -31,6 +31,35 @@ 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")] +pub const NANOSECOND: Duration = Duration { secs: 0, nanos: 1 }; +/// 1 microsecond `Duration`. +#[unstable(feature = "time_units", issue = "0")] +pub const MICROSECOND: Duration = Duration { secs: 0, nanos: NANOS_PER_MICRO }; +/// 1 millisecond `Duration`. +#[unstable(feature = "time_units", issue = "0")] +pub const MILLISECOND: Duration = Duration { secs: 0, nanos: NANOS_PER_MILLI }; +/// 1 second `Duration`. +#[unstable(feature = "time_units", issue = "0")] +pub const SECOND: Duration = Duration { secs: 1, nanos: 0 }; +/// 1 minute `Duration` (60 seconds). +/// +/// Note that it's different from UTC minute, which can be 59-61 seconds long. +#[unstable(feature = "time_units", issue = "0")] +pub const MINUTE: Duration = Duration { secs: 60, nanos: 0 }; +/// 1 hour `Duration` (60*60 = 3 600 seconds). +/// +/// Note that it's different from UTC hour, which can be 3559-3661 seconds long. +#[unstable(feature = "time_units", issue = "0")] +pub const HOUR: Duration = Duration { secs: 3_600, nanos: 0 }; +/// 1 day `Duration` (24*60*60 = 86 400 seconds). +/// +/// Note that it's different from UTC day, length of which can vary from plus +/// minus second (leap seconds) and up to plus minus an hour (Daylight Save Time). +#[unstable(feature = "time_units", issue = "0")] +pub const DAY: Duration = Duration { secs: 86_400, nanos: 0 }; + /// A `Duration` type to represent a span of time, typically used for system /// timeouts. /// @@ -501,6 +530,15 @@ impl Mul for Duration { } } +#[stable(feature = "u32_duration_mul", since = "1.29.0")] +impl Mul for u32 { + 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 for Duration { fn mul_assign(&mut self, rhs: u32) { diff --git a/src/libstd/lib.rs b/src/libstd/lib.rs index fec14b8d67d35..ddc10d43c5471 100644 --- a/src/libstd/lib.rs +++ b/src/libstd/lib.rs @@ -324,6 +324,7 @@ #![feature(float_internals)] #![feature(panic_info_message)] #![feature(panic_implementation)] +#![feature(time_units)] #![default_lib_allocator] diff --git a/src/libstd/time.rs b/src/libstd/time.rs index 90ab349159915..c9299f5c83191 100644 --- a/src/libstd/time.rs +++ b/src/libstd/time.rs @@ -30,6 +30,10 @@ use sys_common::FromInner; #[stable(feature = "time", since = "1.3.0")] pub use core::time::Duration; +#[unstable(feature = "time_units", issue = "0")] +pub use core::time::{ + NANOSECOND, MICROSECOND, MILLISECOND, SECOND, MINUTE, HOUR, DAY, +}; /// A measurement of a monotonically nondecreasing clock. /// Opaque and useful only with `Duration`.