Skip to content

[RFC] add Duration::as_millis #41328

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/doc/unstable-book/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions src/doc/unstable-book/src/duration-as-millis.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# `duration_as_millis`

The tracking issue for this feature is: FIXME

------------------------
19 changes: 19 additions & 0 deletions src/libstd/time/duration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down