Skip to content

Commit bdf983a

Browse files
authored
Unrolled build for rust-lang#120307
Rollup merge of rust-lang#120307 - djc:duration-constructors, r=Mark-Simulacrum core: add Duration constructors Add more `Duration` constructors. Tracking issue: rust-lang#120301. These match similar convenience constructors available on both `chrono::Duration` and `time::Duration`. What's the best ordering for these with respect to the existing constructors?
2 parents 899c895 + e077ff0 commit bdf983a

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)]
@@ -291,6 +299,118 @@ impl Duration {
291299
Duration::new(nanos / (NANOS_PER_SEC as u64), (nanos % (NANOS_PER_SEC as u64)) as u32)
292300
}
293301

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