diff --git a/src/doc/unstable-book/src/SUMMARY.md b/src/doc/unstable-book/src/SUMMARY.md index a9796fdf01e0d..f9bf78f58c7cb 100644 --- a/src/doc/unstable-book/src/SUMMARY.md +++ b/src/doc/unstable-book/src/SUMMARY.md @@ -65,6 +65,7 @@ - [drop_types_in_const](drop-types-in-const.md) - [dropck_eyepatch](dropck-eyepatch.md) - [dropck_parametricity](dropck-parametricity.md) +- [duration_as_millis](duration-as-millis.md) - [enumset](enumset.md) - [error_type_id](error-type-id.md) - [exact_size_is_empty](exact-size-is-empty.md) diff --git a/src/doc/unstable-book/src/duration-as-millis.md b/src/doc/unstable-book/src/duration-as-millis.md new file mode 100644 index 0000000000000..f1cbea0e89426 --- /dev/null +++ b/src/doc/unstable-book/src/duration-as-millis.md @@ -0,0 +1,5 @@ +# `duration_as_millis` + +The tracking issue for this feature is: FIXME + +------------------------ diff --git a/src/libstd/time/duration.rs b/src/libstd/time/duration.rs index af7eaeb3106b2..1b3de28f17cc5 100644 --- a/src/libstd/time/duration.rs +++ b/src/libstd/time/duration.rs @@ -126,6 +126,25 @@ impl Duration { #[inline] pub fn as_secs(&self) -> u64 { self.secs } + /// Returns the number of whole milliseconds represented by this `Duration`. + /// + /// The extra precision represented by this duration is ignored (i.e. extra + /// nanoseconds are not represented in the returned value). + /// + /// # Examples + /// + /// ``` + /// use std::time::Duration; + /// + /// let half_second = Duration::from_millis(500); + /// assert_eq!(half_second.as_millis(), 500); + /// ``` + #[unstable(feature = "duration_as_millis", issue = "0")] + #[inline] + pub fn as_millis(&self) -> u64 { + self.secs * MILLIS_PER_SEC + (self.nanos / NANOS_PER_MILLI) as u64 + } + /// Returns the nanosecond precision represented by this `Duration`. /// /// This method does **not** return the length of the duration when