]> git.lizzy.rs Git - rust.git/blobdiff - src/libcore/time.rs
move checks to from_float_secs
[rust.git] / src / libcore / time.rs
index 56e45146f5911961b9947b83a83eae9f3a8969ed..f0e4b29700b4651b50e48eb29d3ff38c86e1c2a8 100644 (file)
@@ -30,6 +30,7 @@
 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 + 1)*(NANOS_PER_SEC as u128) - 1) as f64;
 
 /// A `Duration` type to represent a span of time, typically used for system
 /// timeouts.
@@ -458,6 +459,106 @@ pub fn checked_div(self, rhs: u32) -> Option<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);
+        if !nanos.is_finite() {
+            panic!("got non-finite value when converting float to duration");
+        }
+        if nanos > MAX_NANOS_F64 {
+            panic!("overflow when converting float to duration");
+        }
+        if nanos < 0.0 {
+            panic!("underflow when converting float to duration");
+        }
+        let nanos =  nanos 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 {
+        Duration::from_float_secs(rhs * self.as_float_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 {
+        Duration::from_float_secs(self.as_float_secs() / rhs)
+    }
+
+    /// 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")]
@@ -501,60 +602,12 @@ fn mul(self, rhs: u32) -> Duration {
     }
 }
 
-#[stable(feature = "duration_mul_div_extras", since = "1.29.0")]
+#[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.checked_mul(self).expect("overflow when multiplying scalar by duration")
-    }
-}
-
-#[stable(feature = "duration_mul_div_extras", since = "1.29.0")]
-impl Mul<f64> for Duration {
-    type Output = Duration;
-
-    fn mul(self, rhs: f64) -> Duration {
-        const NPS: f64 = NANOS_PER_SEC as f64;
-        if rhs.is_sign_negative() {
-            panic!("duration can not be multiplied by negative float");
-        }
-        let nanos_f64 = rhs * (NPS * (self.secs as f64) + (self.nanos as f64));
-        if !nanos_f64.is_finite() {
-            panic!("got non-finite value when multiplying duration by float");
-        }
-        if nanos_f64 > ((u64::MAX as u128)*(NANOS_PER_SEC as u128)) as f64 {
-            panic!("overflow when multiplying duration by float");
-        };
-        let nanos_u128 = nanos_f64 as u128;
-        Duration {
-            secs: (nanos_u128 / (NANOS_PER_SEC as u128)) as u64,
-            nanos: (nanos_u128 % (NANOS_PER_SEC as u128)) as u32,
-        }
-    }
-}
-
-#[stable(feature = "duration_mul_div_extras", since = "1.29.0")]
-impl Mul<Duration> for f64 {
-    type Output = Duration;
-
-    fn mul(self, rhs: Duration) -> Duration {
-        const NPS: f64 = NANOS_PER_SEC as f64;
-        if self.is_sign_negative() {
-            panic!("duration can not be multiplied by negative float");
-        }
-        let nanos_f64 = self * (NPS * (rhs.secs as f64) + (rhs.nanos as f64));
-        if !nanos_f64.is_finite() {
-            panic!("got non-finite value when multiplying float by duration");
-        }
-        if nanos_f64 > ((u64::MAX as u128)*(NANOS_PER_SEC as u128)) as f64 {
-            panic!("overflow when multiplying float by duration");
-        };
-        let nanos_u128 = nanos_f64 as u128;
-        Duration {
-            secs: (nanos_u128 / (NANOS_PER_SEC as u128)) as u64,
-            nanos: (nanos_u128 % (NANOS_PER_SEC as u128)) as u32,
-        }
+        rhs * self
     }
 }
 
@@ -565,13 +618,6 @@ fn mul_assign(&mut self, rhs: u32) {
     }
 }
 
-#[stable(feature = "duration_mul_div_extras", since = "1.29.0")]
-impl MulAssign<f64> for Duration {
-    fn mul_assign(&mut self, rhs: f64) {
-        *self = *self * rhs;
-    }
-}
-
 #[stable(feature = "duration", since = "1.3.0")]
 impl Div<u32> for Duration {
     type Output = Duration;
@@ -581,42 +627,6 @@ fn div(self, rhs: u32) -> Duration {
     }
 }
 
-#[stable(feature = "duration_mul_div_extras", since = "1.29.0")]
-impl Div<f64> for Duration {
-    type Output = Duration;
-
-    fn div(self, rhs: f64) -> Duration {
-        const NPS: f64 = NANOS_PER_SEC as f64;
-        if rhs.is_sign_negative() {
-            panic!("duration can not be divided by negative float");
-        }
-        let nanos_f64 = (NPS * (self.secs as f64) + (self.nanos as f64)) / rhs;
-        if !nanos_f64.is_finite() {
-            panic!("got non-finite value when dividing duration by float");
-        }
-        if nanos_f64 > ((u64::MAX as u128)*(NANOS_PER_SEC as u128)) as f64 {
-            panic!("overflow when dividing duration by float");
-        };
-        let nanos_u128 = nanos_f64 as u128;
-        Duration {
-            secs: (nanos_u128 / (NANOS_PER_SEC as u128)) as u64,
-            nanos: (nanos_u128 % (NANOS_PER_SEC as u128)) as u32,
-        }
-    }
-}
-
-#[stable(feature = "duration_mul_div_extras", since = "1.29.0")]
-impl Div<Duration> for Duration {
-    type Output = f64;
-
-    fn div(self, rhs: Duration) -> f64 {
-        const NPS: f64 = NANOS_PER_SEC as f64;
-        let nanos1 = NPS * (self.secs as f64) + (self.nanos as f64);
-        let nanos2 = NPS * (rhs.secs as f64) + (rhs.nanos as f64);
-        nanos1/nanos2
-    }
-}
-
 #[stable(feature = "time_augmented_assignment", since = "1.9.0")]
 impl DivAssign<u32> for Duration {
     fn div_assign(&mut self, rhs: u32) {
@@ -624,14 +634,6 @@ fn div_assign(&mut self, rhs: u32) {
     }
 }
 
-#[stable(feature = "duration_mul_div_extras", since = "1.29.0")]
-impl DivAssign<f64> for Duration {
-    fn div_assign(&mut self, rhs: f64) {
-        *self = *self / rhs;
-    }
-}
-
-
 macro_rules! sum_durations {
     ($iter:expr) => {{
         let mut total_secs: u64 = 0;