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

core: add Duration constructors #120307

Merged
merged 1 commit into from
Feb 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 120 additions & 0 deletions library/core/src/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ const NANOS_PER_MILLI: u32 = 1_000_000;
const NANOS_PER_MICRO: u32 = 1_000;
const MILLIS_PER_SEC: u64 = 1_000;
const MICROS_PER_SEC: u64 = 1_000_000;
#[unstable(feature = "duration_units", issue = "120301")]
const SECS_PER_MINUTE: u64 = 60;
#[unstable(feature = "duration_units", issue = "120301")]
const MINS_PER_HOUR: u64 = 60;
#[unstable(feature = "duration_units", issue = "120301")]
const HOURS_PER_DAY: u64 = 24;
#[unstable(feature = "duration_units", issue = "120301")]
const DAYS_PER_WEEK: u64 = 7;

#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(transparent)]
Expand Down Expand Up @@ -286,6 +294,118 @@ impl Duration {
Duration::new(nanos / (NANOS_PER_SEC as u64), (nanos % (NANOS_PER_SEC as u64)) as u32)
}

/// Creates a new `Duration` from the specified number of weeks.
///
/// # Panics
///
/// Panics if the given number of weeks overflows the `Duration` size.
///
/// # Examples
///
/// ```
/// #![feature(duration_constructors)]
/// use std::time::Duration;
///
/// let duration = Duration::from_weeks(4);
///
/// assert_eq!(4 * 7 * 24 * 60 * 60, duration.as_secs());
/// assert_eq!(0, duration.subsec_nanos());
/// ```
#[unstable(feature = "duration_constructors", issue = "120301")]
#[must_use]
#[inline]
pub const fn from_weeks(weeks: u64) -> Duration {
if weeks > u64::MAX / (SECS_PER_MINUTE * MINS_PER_HOUR * HOURS_PER_DAY * DAYS_PER_WEEK) {
panic!("overflow in Duration::from_days");
}

Duration::from_secs(weeks * MINS_PER_HOUR * SECS_PER_MINUTE * HOURS_PER_DAY * DAYS_PER_WEEK)
}

/// Creates a new `Duration` from the specified number of days.
///
/// # Panics
///
/// Panics if the given number of days overflows the `Duration` size.
///
/// # Examples
///
/// ```
/// #![feature(duration_constructors)]
/// use std::time::Duration;
///
/// let duration = Duration::from_days(7);
///
/// assert_eq!(7 * 24 * 60 * 60, duration.as_secs());
/// assert_eq!(0, duration.subsec_nanos());
/// ```
#[unstable(feature = "duration_constructors", issue = "120301")]
#[must_use]
#[inline]
pub const fn from_days(days: u64) -> Duration {
if days > u64::MAX / (SECS_PER_MINUTE * MINS_PER_HOUR * HOURS_PER_DAY) {
panic!("overflow in Duration::from_days");
}

Duration::from_secs(days * MINS_PER_HOUR * SECS_PER_MINUTE * HOURS_PER_DAY)
}

/// Creates a new `Duration` from the specified number of hours.
///
/// # Panics
///
/// Panics if the given number of hours overflows the `Duration` size.
///
/// # Examples
///
/// ```
/// #![feature(duration_constructors)]
/// use std::time::Duration;
///
/// let duration = Duration::from_hours(6);
///
/// assert_eq!(6 * 60 * 60, duration.as_secs());
/// assert_eq!(0, duration.subsec_nanos());
/// ```
#[unstable(feature = "duration_constructors", issue = "120301")]
#[must_use]
#[inline]
pub const fn from_hours(hours: u64) -> Duration {
if hours > u64::MAX / (SECS_PER_MINUTE * MINS_PER_HOUR) {
panic!("overflow in Duration::from_hours");
}

Duration::from_secs(hours * MINS_PER_HOUR * SECS_PER_MINUTE)
}

/// Creates a new `Duration` from the specified number of minutes.
///
/// # Panics
///
/// Panics if the given number of minutes overflows the `Duration` size.
///
/// # Examples
///
/// ```
/// #![feature(duration_constructors)]
/// use std::time::Duration;
///
/// let duration = Duration::from_mins(10);
///
/// assert_eq!(10 * 60, duration.as_secs());
/// assert_eq!(0, duration.subsec_nanos());
/// ```
#[unstable(feature = "duration_constructors", issue = "120301")]
#[must_use]
#[inline]
pub const fn from_mins(mins: u64) -> Duration {
if mins > u64::MAX / SECS_PER_MINUTE {
panic!("overflow in Duration::from_mins");
}

Duration::from_secs(mins * SECS_PER_MINUTE)
}

/// Returns true if this `Duration` spans no time.
///
/// # Examples
Expand Down
1 change: 1 addition & 0 deletions library/core/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#![feature(duration_abs_diff)]
#![feature(duration_consts_float)]
#![feature(duration_constants)]
#![feature(duration_constructors)]
#![feature(exact_size_is_empty)]
#![feature(extern_types)]
#![feature(flt2dec)]
Expand Down
43 changes: 43 additions & 0 deletions library/core/tests/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,49 @@ fn new_overflow() {
let _ = Duration::new(u64::MAX, 1_000_000_000);
}

#[test]
#[should_panic]
fn from_mins_overflow() {
let overflow = u64::MAX / 60 + 1;
let _ = Duration::from_mins(overflow);
}

#[test]
#[should_panic]
fn from_hours_overflow() {
let overflow = u64::MAX / (60 * 60) + 1;
let _ = Duration::from_hours(overflow);
}

#[test]
#[should_panic]
fn from_days_overflow() {
let overflow = u64::MAX / (24 * 60 * 60) + 1;
let _ = Duration::from_days(overflow);
}

#[test]
#[should_panic]
fn from_weeks_overflow() {
let overflow = u64::MAX / (7 * 24 * 60 * 60) + 1;
let _ = Duration::from_weeks(overflow);
}

#[test]
fn constructors() {
assert_eq!(Duration::from_weeks(1), Duration::from_secs(7 * 24 * 60 * 60));
assert_eq!(Duration::from_weeks(0), Duration::ZERO);

assert_eq!(Duration::from_days(1), Duration::from_secs(86_400));
assert_eq!(Duration::from_days(0), Duration::ZERO);

assert_eq!(Duration::from_hours(1), Duration::from_secs(3_600));
assert_eq!(Duration::from_hours(0), Duration::ZERO);

assert_eq!(Duration::from_mins(1), Duration::from_secs(60));
assert_eq!(Duration::from_mins(0), Duration::ZERO);
}

#[test]
fn secs() {
assert_eq!(Duration::new(0, 0).as_secs(), 0);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# `duration_constructors`

The tracking issue for this feature is: [#120301]

[#120301]: https://github.com/rust-lang/rust/issues/120301

------------------------

Add the methods `from_mins`, `from_hours` and `from_days` to `Duration`.
Loading