@@ -28,6 +28,14 @@ const NANOS_PER_MILLI: u32 = 1_000_000;
28
28
const NANOS_PER_MICRO : u32 = 1_000 ;
29
29
const MILLIS_PER_SEC : u64 = 1_000 ;
30
30
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 ;
31
39
32
40
#[ derive( Clone , Copy , PartialEq , Eq , PartialOrd , Ord , Hash ) ]
33
41
#[ repr( transparent) ]
@@ -291,6 +299,118 @@ impl Duration {
291
299
Duration :: new ( nanos / ( NANOS_PER_SEC as u64 ) , ( nanos % ( NANOS_PER_SEC as u64 ) ) as u32 )
292
300
}
293
301
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
+
294
414
/// Returns true if this `Duration` spans no time.
295
415
///
296
416
/// # Examples
0 commit comments