@@ -459,96 +459,115 @@ impl Duration {
459
459
None
460
460
}
461
461
}
462
+
463
+ /// Returns the number of seconds contained by this `Duration` as `f64`.
464
+ ///
465
+ /// The returned value does include the fractional (nanosecond) part of the duration.
466
+ ///
467
+ /// # Examples
468
+ /// ```
469
+ /// #![feature(duration_float)]
470
+ /// use std::time::Duration;
471
+ ///
472
+ /// let dur = Duration::new(2, 700_000_000);
473
+ /// assert_eq!(dur.as_float_secs(), 2.7);
474
+ /// ```
475
+ #[ unstable( feature = "duration_float" , issue = "0" ) ]
476
+ #[ inline]
477
+ pub fn as_float_secs ( & self ) -> f64 {
478
+ ( self . secs as f64 ) + ( self . nanos as f64 ) / ( NANOS_PER_SEC as f64 )
479
+ }
462
480
481
+ /// Creates a new `Duration` from the specified number of seconds.
482
+ ///
483
+ /// # Examples
484
+ /// ```
485
+ /// #![feature(duration_float)]
486
+ /// use std::time::Duration;
487
+ ///
488
+ /// let dur = Duration::from_float_secs(2.7);
489
+ /// assert_eq!(dur, Duration::new(2, 700_000_000));
490
+ /// ```
491
+ #[ unstable( feature = "duration_float" , issue = "0" ) ]
492
+ #[ inline]
493
+ pub fn from_float_secs ( secs : f64 ) -> Duration {
494
+ let nanos = ( secs * ( NANOS_PER_SEC as f64 ) ) as u128 ;
495
+ Duration {
496
+ secs : ( nanos / ( NANOS_PER_SEC as u128 ) ) as u64 ,
497
+ nanos : ( nanos % ( NANOS_PER_SEC as u128 ) ) as u32 ,
498
+ }
499
+ }
500
+
463
501
/// Multiply `Duration` by `f64`.
464
502
///
465
503
/// # Examples
466
504
/// ```
467
- /// #![feature(duration_float_ops )]
505
+ /// #![feature(duration_float )]
468
506
/// use std::time::Duration;
469
507
///
470
508
/// let dur = Duration::new(2, 700_000_000);
471
509
/// assert_eq!(dur.mul_f64(3.14), Duration::new(8, 478_000_000));
472
510
/// assert_eq!(dur.mul_f64(3.14e5), Duration::new(847_800, 0));
473
511
/// ```
474
- #[ unstable( feature = "duration_float_ops" ,
475
- reason = "duration/floats operations are unstabe" ,
476
- issue = "0" ) ]
512
+ #[ unstable( feature = "duration_float" , issue = "0" ) ]
477
513
#[ inline]
478
514
pub fn mul_f64 ( self , rhs : f64 ) -> Duration {
479
- const NPS : f64 = NANOS_PER_SEC as f64 ;
480
- let nanos_f64 = rhs * ( NPS * ( self . secs as f64 ) + ( self . nanos as f64 ) ) ;
481
- if !nanos_f64. is_finite ( ) {
515
+ let secs = rhs * self . as_float_secs ( ) ;
516
+ if !secs. is_finite ( ) {
482
517
panic ! ( "got non-finite value when multiplying duration by float" ) ;
483
518
}
484
- if nanos_f64 > MAX_NANOS_F64 {
519
+ if secs > MAX_NANOS_F64 {
485
520
panic ! ( "overflow when multiplying duration by float" ) ;
486
521
}
487
- if nanos_f64 < 0.0 {
522
+ if secs < 0.0 {
488
523
panic ! ( "underflow when multiplying duration by float" ) ;
489
524
}
490
- let nanos_u128 = nanos_f64 as u128 ;
491
- Duration {
492
- secs : ( nanos_u128 / ( NANOS_PER_SEC as u128 ) ) as u64 ,
493
- nanos : ( nanos_u128 % ( NANOS_PER_SEC as u128 ) ) as u32 ,
494
- }
525
+ Duration :: from_float_secs ( secs)
495
526
}
496
527
497
528
/// Divide `Duration` by `f64`.
498
529
///
499
530
/// # Examples
500
531
/// ```
501
- /// #![feature(duration_float_ops )]
532
+ /// #![feature(duration_float )]
502
533
/// use std::time::Duration;
503
534
///
504
535
/// let dur = Duration::new(2, 700_000_000);
505
536
/// assert_eq!(dur.div_f64(3.14), Duration::new(0, 859_872_611));
506
537
/// // note that truncation is used, not rounding
507
538
/// assert_eq!(dur.div_f64(3.14e5), Duration::new(0, 8_598));
508
539
/// ```
509
- #[ unstable( feature = "duration_float_ops" ,
510
- reason = "duration/floats operations are unstabe" ,
511
- issue = "0" ) ]
540
+ #[ unstable( feature = "duration_float" , issue = "0" ) ]
512
541
#[ inline]
513
542
pub fn div_f64 ( self , rhs : f64 ) -> Duration {
514
- const NPS : f64 = NANOS_PER_SEC as f64 ;
515
- let nanos_f64 = ( NPS * ( self . secs as f64 ) + ( self . nanos as f64 ) ) / rhs;
516
- if !nanos_f64. is_finite ( ) {
543
+ let secs = self . as_float_secs ( ) / rhs;
544
+ if !secs. is_finite ( ) {
517
545
panic ! ( "got non-finite value when dividing duration by float" ) ;
518
546
}
519
- if nanos_f64 > MAX_NANOS_F64 {
547
+ if secs > MAX_NANOS_F64 {
520
548
panic ! ( "overflow when dividing duration by float" ) ;
521
549
}
522
- if nanos_f64 < 0.0 {
550
+ if secs < 0.0 {
523
551
panic ! ( "underflow when multiplying duration by float" ) ;
524
552
}
525
- let nanos_u128 = nanos_f64 as u128 ;
526
- Duration {
527
- secs : ( nanos_u128 / ( NANOS_PER_SEC as u128 ) ) as u64 ,
528
- nanos : ( nanos_u128 % ( NANOS_PER_SEC as u128 ) ) as u32 ,
529
- }
553
+ Duration :: from_float_secs ( secs)
530
554
}
531
555
532
556
/// Divide `Duration` by `Duration` and return `f64`.
533
557
///
534
558
/// # Examples
535
559
/// ```
536
- /// #![feature(duration_float_ops )]
560
+ /// #![feature(duration_float )]
537
561
/// use std::time::Duration;
538
562
///
539
563
/// let dur1 = Duration::new(2, 700_000_000);
540
564
/// let dur2 = Duration::new(5, 400_000_000);
541
565
/// assert_eq!(dur1.div_duration(dur2), 0.5);
542
566
/// ```
543
- #[ unstable( feature = "duration_float_ops" ,
544
- reason = "duration/floats operations are unstabe" ,
545
- issue = "0" ) ]
567
+ #[ unstable( feature = "duration_float" , issue = "0" ) ]
546
568
#[ inline]
547
569
pub fn div_duration ( self , rhs : Duration ) -> f64 {
548
- const NPS : f64 = NANOS_PER_SEC as f64 ;
549
- let nanos1 = NPS * ( self . secs as f64 ) + ( self . nanos as f64 ) ;
550
- let nanos2 = NPS * ( rhs. secs as f64 ) + ( rhs. nanos as f64 ) ;
551
- nanos1/nanos2
570
+ self . as_float_secs ( ) /rhs. as_float_secs ( )
552
571
}
553
572
}
554
573
0 commit comments