@@ -108,7 +108,7 @@ impl Duration {
108
108
/// let duration = Duration::from_millis(2569);
109
109
///
110
110
/// assert_eq!(2, duration.as_secs());
111
- /// assert_eq!(569000000 , duration.subsec_nanos());
111
+ /// assert_eq!(569_000_000 , duration.subsec_nanos());
112
112
/// ```
113
113
#[ stable( feature = "duration" , since = "1.3.0" ) ]
114
114
#[ inline]
@@ -139,6 +139,27 @@ impl Duration {
139
139
Duration { secs : secs, nanos : nanos }
140
140
}
141
141
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
+
142
163
/// Returns the number of _whole_ seconds contained by this `Duration`.
143
164
///
144
165
/// The returned value does not include the fractional (nanosecond) part of the
@@ -171,6 +192,46 @@ impl Duration {
171
192
#[ inline]
172
193
pub fn as_secs ( & self ) -> u64 { self . secs }
173
194
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
+
174
235
/// Returns the fractional part of this `Duration`, in nanoseconds.
175
236
///
176
237
/// This method does **not** return the length of the duration when
0 commit comments