Skip to content

Commit 37972ae

Browse files
authored
add as_float_secs and from_float_secs methods, refactor float methods
1 parent c11281f commit 37972ae

File tree

1 file changed

+55
-36
lines changed

1 file changed

+55
-36
lines changed

src/libcore/time.rs

Lines changed: 55 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -459,96 +459,115 @@ impl Duration {
459459
None
460460
}
461461
}
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+
}
462480

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+
463501
/// Multiply `Duration` by `f64`.
464502
///
465503
/// # Examples
466504
/// ```
467-
/// #![feature(duration_float_ops)]
505+
/// #![feature(duration_float)]
468506
/// use std::time::Duration;
469507
///
470508
/// let dur = Duration::new(2, 700_000_000);
471509
/// assert_eq!(dur.mul_f64(3.14), Duration::new(8, 478_000_000));
472510
/// assert_eq!(dur.mul_f64(3.14e5), Duration::new(847_800, 0));
473511
/// ```
474-
#[unstable(feature = "duration_float_ops",
475-
reason = "duration/floats operations are unstabe",
476-
issue = "0")]
512+
#[unstable(feature = "duration_float", issue = "0")]
477513
#[inline]
478514
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() {
482517
panic!("got non-finite value when multiplying duration by float");
483518
}
484-
if nanos_f64 > MAX_NANOS_F64 {
519+
if secs > MAX_NANOS_F64 {
485520
panic!("overflow when multiplying duration by float");
486521
}
487-
if nanos_f64 < 0.0 {
522+
if secs < 0.0 {
488523
panic!("underflow when multiplying duration by float");
489524
}
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)
495526
}
496527

497528
/// Divide `Duration` by `f64`.
498529
///
499530
/// # Examples
500531
/// ```
501-
/// #![feature(duration_float_ops)]
532+
/// #![feature(duration_float)]
502533
/// use std::time::Duration;
503534
///
504535
/// let dur = Duration::new(2, 700_000_000);
505536
/// assert_eq!(dur.div_f64(3.14), Duration::new(0, 859_872_611));
506537
/// // note that truncation is used, not rounding
507538
/// assert_eq!(dur.div_f64(3.14e5), Duration::new(0, 8_598));
508539
/// ```
509-
#[unstable(feature = "duration_float_ops",
510-
reason = "duration/floats operations are unstabe",
511-
issue = "0")]
540+
#[unstable(feature = "duration_float", issue = "0")]
512541
#[inline]
513542
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() {
517545
panic!("got non-finite value when dividing duration by float");
518546
}
519-
if nanos_f64 > MAX_NANOS_F64 {
547+
if secs > MAX_NANOS_F64 {
520548
panic!("overflow when dividing duration by float");
521549
}
522-
if nanos_f64 < 0.0 {
550+
if secs < 0.0 {
523551
panic!("underflow when multiplying duration by float");
524552
}
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)
530554
}
531555

532556
/// Divide `Duration` by `Duration` and return `f64`.
533557
///
534558
/// # Examples
535559
/// ```
536-
/// #![feature(duration_float_ops)]
560+
/// #![feature(duration_float)]
537561
/// use std::time::Duration;
538562
///
539563
/// let dur1 = Duration::new(2, 700_000_000);
540564
/// let dur2 = Duration::new(5, 400_000_000);
541565
/// assert_eq!(dur1.div_duration(dur2), 0.5);
542566
/// ```
543-
#[unstable(feature = "duration_float_ops",
544-
reason = "duration/floats operations are unstabe",
545-
issue = "0")]
567+
#[unstable(feature = "duration_float", issue = "0")]
546568
#[inline]
547569
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()
552571
}
553572
}
554573

0 commit comments

Comments
 (0)