Skip to content
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

Time units #52556

Closed
wants to merge 12 commits into from
1 change: 1 addition & 0 deletions src/libcore/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@
#![feature(const_slice_len)]
#![feature(const_str_as_bytes)]
#![feature(const_str_len)]
#![feature(time_units)]

#[prelude_import]
#[allow(unused)]
Expand Down
38 changes: 38 additions & 0 deletions src/libcore/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand Down Expand Up @@ -501,6 +530,15 @@ impl Mul<u32> for Duration {
}
}

#[stable(feature = "u32_duration_mul", since = "1.29.0")]
impl Mul<Duration> 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<u32> for Duration {
fn mul_assign(&mut self, rhs: u32) {
Expand Down
1 change: 1 addition & 0 deletions src/libstd/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,7 @@
#![feature(float_internals)]
#![feature(panic_info_message)]
#![feature(panic_implementation)]
#![feature(time_units)]

#![default_lib_allocator]

Expand Down
4 changes: 4 additions & 0 deletions src/libstd/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down