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