Skip to content

Commit 2be7a31

Browse files
author
Clar Charr
committed
Add more Duration methods for consistency.
1 parent 33245fe commit 2be7a31

File tree

1 file changed

+62
-1
lines changed

1 file changed

+62
-1
lines changed

src/libstd/time/duration.rs

+62-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ impl Duration {
108108
/// let duration = Duration::from_millis(2569);
109109
///
110110
/// assert_eq!(2, duration.as_secs());
111-
/// assert_eq!(569000000, duration.subsec_nanos());
111+
/// assert_eq!(569_000_000, duration.subsec_nanos());
112112
/// ```
113113
#[stable(feature = "duration", since = "1.3.0")]
114114
#[inline]
@@ -139,6 +139,27 @@ impl Duration {
139139
Duration { secs: secs, nanos: nanos }
140140
}
141141

142+
/// Creates a new `Duration` from the specified number of nanoseconds.
143+
///
144+
/// # Examples
145+
///
146+
/// ```
147+
/// #![feature(duration_extras)]
148+
/// use std::time::Duration;
149+
///
150+
/// let duration = Duration::from_nanos(1_000_000_123);
151+
///
152+
/// assert_eq!(1, duration.as_secs());
153+
/// assert_eq!(123, duration.subsec_nanos());
154+
/// ```
155+
#[unstable(feature = "duration_extras", issue = "46507")]
156+
#[inline]
157+
pub fn from_nanos(nanos: u64) -> Duration {
158+
let secs = nanos / (NANOS_PER_SEC as u64);
159+
let nanos = (nanos % (NANOS_PER_SEC as u64)) as u32;
160+
Duration { secs: secs, nanos: nanos }
161+
}
162+
142163
/// Returns the number of _whole_ seconds contained by this `Duration`.
143164
///
144165
/// The returned value does not include the fractional (nanosecond) part of the
@@ -171,6 +192,46 @@ impl Duration {
171192
#[inline]
172193
pub fn as_secs(&self) -> u64 { self.secs }
173194

195+
/// Returns the fractional part of this `Duration`, in milliseconds.
196+
///
197+
/// This method does **not** return the length of the duration when
198+
/// represented by milliseconds. The returned number always represents a
199+
/// fractional portion of a second (i.e. it is less than one thousand).
200+
///
201+
/// # Examples
202+
///
203+
/// ```
204+
/// #![feature(duration_extras)]
205+
/// use std::time::Duration;
206+
///
207+
/// let duration = Duration::from_millis(5432);
208+
/// assert_eq!(duration.as_secs(), 5);
209+
/// assert_eq!(duration.subsec_nanos(), 432_000_000);
210+
/// ```
211+
#[unstable(feature = "duration_extras", issue = "46507")]
212+
#[inline]
213+
pub fn subsec_millis(&self) -> u32 { self.nanos / NANOS_PER_MILLI }
214+
215+
/// Returns the fractional part of this `Duration`, in microseconds.
216+
///
217+
/// This method does **not** return the length of the duration when
218+
/// represented by microseconds. The returned number always represents a
219+
/// fractional portion of a second (i.e. it is less than one million).
220+
///
221+
/// # Examples
222+
///
223+
/// ```
224+
/// #![feature(duration_extras, duration_from_micros)]
225+
/// use std::time::Duration;
226+
///
227+
/// let duration = Duration::from_micros(1_234_567);
228+
/// assert_eq!(duration.as_secs(), 1);
229+
/// assert_eq!(duration.subsec_nanos(), 234_567_000);
230+
/// ```
231+
#[unstable(feature = "duration_extras", issue = "46507")]
232+
#[inline]
233+
pub fn subsec_micros(&self) -> u32 { self.nanos / NANOS_PER_MICRO }
234+
174235
/// Returns the fractional part of this `Duration`, in nanoseconds.
175236
///
176237
/// This method does **not** return the length of the duration when

0 commit comments

Comments
 (0)