Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Duration div mul extras #52813

Merged
merged 18 commits into from
Sep 21, 2018
Merged
122 changes: 121 additions & 1 deletion src/libcore/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
//! assert_eq!(Duration::new(5, 0), Duration::from_secs(5));
//! ```

use fmt;
use {fmt, u64};
use iter::Sum;
use ops::{Add, Sub, Mul, Div, AddAssign, SubAssign, MulAssign, DivAssign};

Expand All @@ -30,6 +30,7 @@ const NANOS_PER_MILLI: u32 = 1_000_000;
const NANOS_PER_MICRO: u32 = 1_000;
const MILLIS_PER_SEC: u64 = 1_000;
const MICROS_PER_SEC: u64 = 1_000_000;
const MAX_NANOS_F64: f64 = ((u64::MAX as u128)*(NANOS_PER_SEC as u128)) as f64;

/// A `Duration` type to represent a span of time, typically used for system
/// timeouts.
Expand Down Expand Up @@ -458,6 +459,116 @@ impl Duration {
None
}
}

/// Returns the number of seconds contained by this `Duration` as `f64`.
///
/// The returned value does include the fractional (nanosecond) part of the duration.
///
/// # Examples
/// ```
/// #![feature(duration_float)]
/// use std::time::Duration;
///
/// let dur = Duration::new(2, 700_000_000);
/// assert_eq!(dur.as_float_secs(), 2.7);
/// ```
#[unstable(feature = "duration_float", issue = "0")]
#[inline]
pub fn as_float_secs(&self) -> f64 {
(self.secs as f64) + (self.nanos as f64) / (NANOS_PER_SEC as f64)
}

/// Creates a new `Duration` from the specified number of seconds.
///
/// # Examples
/// ```
/// #![feature(duration_float)]
/// use std::time::Duration;
///
/// let dur = Duration::from_float_secs(2.7);
/// assert_eq!(dur, Duration::new(2, 700_000_000));
/// ```
#[unstable(feature = "duration_float", issue = "0")]
#[inline]
pub fn from_float_secs(secs: f64) -> Duration {
let nanos = (secs * (NANOS_PER_SEC as f64)) as u128;
Duration {
secs: (nanos / (NANOS_PER_SEC as u128)) as u64,
nanos: (nanos % (NANOS_PER_SEC as u128)) as u32,
}
}

/// Multiply `Duration` by `f64`.
///
/// # Examples
/// ```
/// #![feature(duration_float)]
/// use std::time::Duration;
///
/// let dur = Duration::new(2, 700_000_000);
/// assert_eq!(dur.mul_f64(3.14), Duration::new(8, 478_000_000));
/// assert_eq!(dur.mul_f64(3.14e5), Duration::new(847_800, 0));
/// ```
#[unstable(feature = "duration_float", issue = "0")]
#[inline]
pub fn mul_f64(self, rhs: f64) -> Duration {
let secs = rhs * self.as_float_secs();
if !secs.is_finite() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should these assertions be moved into float_from_secs perhaps?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, yes, you are right.

panic!("got non-finite value when multiplying duration by float");
}
if secs > MAX_NANOS_F64 {
panic!("overflow when multiplying duration by float");
}
if secs < 0.0 {
panic!("underflow when multiplying duration by float");
}
Duration::from_float_secs(secs)
}

/// Divide `Duration` by `f64`.
///
/// # Examples
/// ```
/// #![feature(duration_float)]
/// use std::time::Duration;
///
/// let dur = Duration::new(2, 700_000_000);
/// assert_eq!(dur.div_f64(3.14), Duration::new(0, 859_872_611));
/// // note that truncation is used, not rounding
/// assert_eq!(dur.div_f64(3.14e5), Duration::new(0, 8_598));
/// ```
#[unstable(feature = "duration_float", issue = "0")]
#[inline]
pub fn div_f64(self, rhs: f64) -> Duration {
let secs = self.as_float_secs() / rhs;
if !secs.is_finite() {
panic!("got non-finite value when dividing duration by float");
}
if secs > MAX_NANOS_F64 {
panic!("overflow when dividing duration by float");
}
if secs < 0.0 {
panic!("underflow when multiplying duration by float");
}
Duration::from_float_secs(secs)
}

/// Divide `Duration` by `Duration` and return `f64`.
///
/// # Examples
/// ```
/// #![feature(duration_float)]
/// use std::time::Duration;
///
/// let dur1 = Duration::new(2, 700_000_000);
/// let dur2 = Duration::new(5, 400_000_000);
/// assert_eq!(dur1.div_duration(dur2), 0.5);
/// ```
#[unstable(feature = "duration_float", issue = "0")]
#[inline]
pub fn div_duration(self, rhs: Duration) -> f64 {
self.as_float_secs()/rhs.as_float_secs()
}
}

#[stable(feature = "duration", since = "1.3.0")]
Expand Down Expand Up @@ -501,6 +612,15 @@ impl Mul<u32> for Duration {
}
}

#[stable(feature = "symmetric_u32_duration_mul", since = "1.30.0")]
impl Mul<Duration> for u32 {
type Output = Duration;

fn mul(self, rhs: Duration) -> Duration {
rhs * self
}
}

#[stable(feature = "time_augmented_assignment", since = "1.9.0")]
impl MulAssign<u32> for Duration {
fn mul_assign(&mut self, rhs: u32) {
Expand Down