@@ -28,6 +28,14 @@ const NANOS_PER_MILLI: u32 = 1_000_000;
2828const NANOS_PER_MICRO : u32 = 1_000 ;
2929const MILLIS_PER_SEC : u64 = 1_000 ;
3030const 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
0 commit comments