Skip to content

Commit e077ff0

Browse files
committed
core: add Duration constructors
1 parent f6ee4bf commit e077ff0

File tree

4 files changed

+173
-0
lines changed

4 files changed

+173
-0
lines changed

library/core/src/time.rs

+120
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@ const NANOS_PER_MILLI: u32 = 1_000_000;
2828
const NANOS_PER_MICRO: u32 = 1_000;
2929
const MILLIS_PER_SEC: u64 = 1_000;
3030
const MICROS_PER_SEC: u64 = 1_000_000;
31+
#[unstable(feature = "duration_units", issue = "120301")]
32+
const SECS_PER_MINUTE: u64 = 60;
33+
#[unstable(feature = "duration_units", issue = "120301")]
34+
const MINS_PER_HOUR: u64 = 60;
35+
#[unstable(feature = "duration_units", issue = "120301")]
36+
const HOURS_PER_DAY: u64 = 24;
37+
#[unstable(feature = "duration_units", issue = "120301")]
38+
const DAYS_PER_WEEK: u64 = 7;
3139

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

297+
/// Creates a new `Duration` from the specified number of weeks.
298+
///
299+
/// # Panics
300+
///
301+
/// Panics if the given number of weeks overflows the `Duration` size.
302+
///
303+
/// # Examples
304+
///
305+
/// ```
306+
/// #![feature(duration_constructors)]
307+
/// use std::time::Duration;
308+
///
309+
/// let duration = Duration::from_weeks(4);
310+
///
311+
/// assert_eq!(4 * 7 * 24 * 60 * 60, duration.as_secs());
312+
/// assert_eq!(0, duration.subsec_nanos());
313+
/// ```
314+
#[unstable(feature = "duration_constructors", issue = "120301")]
315+
#[must_use]
316+
#[inline]
317+
pub const fn from_weeks(weeks: u64) -> Duration {
318+
if weeks > u64::MAX / (SECS_PER_MINUTE * MINS_PER_HOUR * HOURS_PER_DAY * DAYS_PER_WEEK) {
319+
panic!("overflow in Duration::from_days");
320+
}
321+
322+
Duration::from_secs(weeks * MINS_PER_HOUR * SECS_PER_MINUTE * HOURS_PER_DAY * DAYS_PER_WEEK)
323+
}
324+
325+
/// Creates a new `Duration` from the specified number of days.
326+
///
327+
/// # Panics
328+
///
329+
/// Panics if the given number of days overflows the `Duration` size.
330+
///
331+
/// # Examples
332+
///
333+
/// ```
334+
/// #![feature(duration_constructors)]
335+
/// use std::time::Duration;
336+
///
337+
/// let duration = Duration::from_days(7);
338+
///
339+
/// assert_eq!(7 * 24 * 60 * 60, duration.as_secs());
340+
/// assert_eq!(0, duration.subsec_nanos());
341+
/// ```
342+
#[unstable(feature = "duration_constructors", issue = "120301")]
343+
#[must_use]
344+
#[inline]
345+
pub const fn from_days(days: u64) -> Duration {
346+
if days > u64::MAX / (SECS_PER_MINUTE * MINS_PER_HOUR * HOURS_PER_DAY) {
347+
panic!("overflow in Duration::from_days");
348+
}
349+
350+
Duration::from_secs(days * MINS_PER_HOUR * SECS_PER_MINUTE * HOURS_PER_DAY)
351+
}
352+
353+
/// Creates a new `Duration` from the specified number of hours.
354+
///
355+
/// # Panics
356+
///
357+
/// Panics if the given number of hours overflows the `Duration` size.
358+
///
359+
/// # Examples
360+
///
361+
/// ```
362+
/// #![feature(duration_constructors)]
363+
/// use std::time::Duration;
364+
///
365+
/// let duration = Duration::from_hours(6);
366+
///
367+
/// assert_eq!(6 * 60 * 60, duration.as_secs());
368+
/// assert_eq!(0, duration.subsec_nanos());
369+
/// ```
370+
#[unstable(feature = "duration_constructors", issue = "120301")]
371+
#[must_use]
372+
#[inline]
373+
pub const fn from_hours(hours: u64) -> Duration {
374+
if hours > u64::MAX / (SECS_PER_MINUTE * MINS_PER_HOUR) {
375+
panic!("overflow in Duration::from_hours");
376+
}
377+
378+
Duration::from_secs(hours * MINS_PER_HOUR * SECS_PER_MINUTE)
379+
}
380+
381+
/// Creates a new `Duration` from the specified number of minutes.
382+
///
383+
/// # Panics
384+
///
385+
/// Panics if the given number of minutes overflows the `Duration` size.
386+
///
387+
/// # Examples
388+
///
389+
/// ```
390+
/// #![feature(duration_constructors)]
391+
/// use std::time::Duration;
392+
///
393+
/// let duration = Duration::from_mins(10);
394+
///
395+
/// assert_eq!(10 * 60, duration.as_secs());
396+
/// assert_eq!(0, duration.subsec_nanos());
397+
/// ```
398+
#[unstable(feature = "duration_constructors", issue = "120301")]
399+
#[must_use]
400+
#[inline]
401+
pub const fn from_mins(mins: u64) -> Duration {
402+
if mins > u64::MAX / SECS_PER_MINUTE {
403+
panic!("overflow in Duration::from_mins");
404+
}
405+
406+
Duration::from_secs(mins * SECS_PER_MINUTE)
407+
}
408+
289409
/// Returns true if this `Duration` spans no time.
290410
///
291411
/// # Examples

library/core/tests/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#![feature(duration_abs_diff)]
3333
#![feature(duration_consts_float)]
3434
#![feature(duration_constants)]
35+
#![feature(duration_constructors)]
3536
#![feature(exact_size_is_empty)]
3637
#![feature(extern_types)]
3738
#![feature(flt2dec)]

library/core/tests/time.rs

+43
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,49 @@ fn new_overflow() {
1717
let _ = Duration::new(u64::MAX, 1_000_000_000);
1818
}
1919

20+
#[test]
21+
#[should_panic]
22+
fn from_mins_overflow() {
23+
let overflow = u64::MAX / 60 + 1;
24+
let _ = Duration::from_mins(overflow);
25+
}
26+
27+
#[test]
28+
#[should_panic]
29+
fn from_hours_overflow() {
30+
let overflow = u64::MAX / (60 * 60) + 1;
31+
let _ = Duration::from_hours(overflow);
32+
}
33+
34+
#[test]
35+
#[should_panic]
36+
fn from_days_overflow() {
37+
let overflow = u64::MAX / (24 * 60 * 60) + 1;
38+
let _ = Duration::from_days(overflow);
39+
}
40+
41+
#[test]
42+
#[should_panic]
43+
fn from_weeks_overflow() {
44+
let overflow = u64::MAX / (7 * 24 * 60 * 60) + 1;
45+
let _ = Duration::from_weeks(overflow);
46+
}
47+
48+
#[test]
49+
fn constructors() {
50+
assert_eq!(Duration::from_weeks(1), Duration::from_secs(7 * 24 * 60 * 60));
51+
assert_eq!(Duration::from_weeks(0), Duration::ZERO);
52+
53+
assert_eq!(Duration::from_days(1), Duration::from_secs(86_400));
54+
assert_eq!(Duration::from_days(0), Duration::ZERO);
55+
56+
assert_eq!(Duration::from_hours(1), Duration::from_secs(3_600));
57+
assert_eq!(Duration::from_hours(0), Duration::ZERO);
58+
59+
assert_eq!(Duration::from_mins(1), Duration::from_secs(60));
60+
assert_eq!(Duration::from_mins(0), Duration::ZERO);
61+
}
62+
2063
#[test]
2164
fn secs() {
2265
assert_eq!(Duration::new(0, 0).as_secs(), 0);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# `duration_constructors`
2+
3+
The tracking issue for this feature is: [#120301]
4+
5+
[#120301]: https://github.com/rust-lang/rust/issues/120301
6+
7+
------------------------
8+
9+
Add the methods `from_mins`, `from_hours` and `from_days` to `Duration`.

0 commit comments

Comments
 (0)