@@ -34,7 +34,7 @@ pub use self::duration::Duration;
34
34
mod duration;
35
35
36
36
/// A measurement of a monotonically increasing clock.
37
- /// Opaque and useful only with `Duration`.
37
+ /// Opaque and useful only with `Duration`.
38
38
///
39
39
/// Instants are always guaranteed to be greater than any previously measured
40
40
/// instant when created, and are often useful for tasks such as measuring
@@ -73,23 +73,28 @@ pub struct Instant(time::Instant);
73
73
/// A measurement of the system clock, useful for talking to
74
74
/// external entities like the file system or other processes.
75
75
///
76
- /// Distinct from the `Instant` type, this time measurement **is not
76
+ /// Distinct from the [ `Instant`] type, this time measurement **is not
77
77
/// monotonic**. This means that you can save a file to the file system, then
78
78
/// save another file to the file system, **and the second file has a
79
79
/// `SystemTime` measurement earlier than the first**. In other words, an
80
80
/// operation that happens after another operation in real time may have an
81
81
/// earlier `SystemTime`!
82
82
///
83
83
/// Consequently, comparing two `SystemTime` instances to learn about the
84
- /// duration between them returns a `Result` instead of an infallible `Duration`
84
+ /// duration between them returns a [ `Result`] instead of an infallible [ `Duration`]
85
85
/// to indicate that this sort of time drift may happen and needs to be handled.
86
86
///
87
- /// Although a `SystemTime` cannot be directly inspected, the `UNIX_EPOCH`
87
+ /// Although a `SystemTime` cannot be directly inspected, the [ `UNIX_EPOCH`]
88
88
/// constant is provided in this module as an anchor in time to learn
89
89
/// information about a `SystemTime`. By calculating the duration from this
90
90
/// fixed point in time, a `SystemTime` can be converted to a human-readable time,
91
91
/// or perhaps some other string representation.
92
92
///
93
+ /// [`Instant`]: ../../std/time/struct.Instant.html
94
+ /// [`Result`]: ../../std/result/enum.Result.html
95
+ /// [`Duration`]: ../../std/time/struct.Duration.html
96
+ /// [`UNIX_EPOCH`]: ../../std/time/constant.UNIX_EPOCH.html
97
+ ///
93
98
/// Example:
94
99
///
95
100
/// ```no_run
@@ -117,14 +122,38 @@ pub struct Instant(time::Instant);
117
122
#[ stable( feature = "time2" , since = "1.8.0" ) ]
118
123
pub struct SystemTime ( time:: SystemTime ) ;
119
124
120
- /// An error returned from the `duration_since` method on `SystemTime`,
121
- /// used to learn how far in the opposite direction a system time lies.
125
+ /// An error returned from the `duration_since` and `elapsed` methods on
126
+ /// `SystemTime`, used to learn how far in the opposite direction a system time
127
+ /// lies.
128
+ ///
129
+ /// # Examples
130
+ ///
131
+ /// ```no_run
132
+ /// use std::thread::sleep;
133
+ /// use std::time::{Duration, SystemTime};
134
+ ///
135
+ /// let sys_time = SystemTime::now();
136
+ /// sleep(Duration::from_secs(1));
137
+ /// let new_sys_time = SystemTime::now();
138
+ /// match sys_time.duration_since(new_sys_time) {
139
+ /// Ok(_) => {}
140
+ /// Err(e) => println!("SystemTimeError difference: {:?}", e.duration()),
141
+ /// }
142
+ /// ```
122
143
#[ derive( Clone , Debug ) ]
123
144
#[ stable( feature = "time2" , since = "1.8.0" ) ]
124
145
pub struct SystemTimeError ( Duration ) ;
125
146
126
147
impl Instant {
127
148
/// Returns an instant corresponding to "now".
149
+ ///
150
+ /// # Examples
151
+ ///
152
+ /// ```
153
+ /// use std::time::Instant;
154
+ ///
155
+ /// let now = Instant::now();
156
+ /// ```
128
157
#[ stable( feature = "time2" , since = "1.8.0" ) ]
129
158
pub fn now ( ) -> Instant {
130
159
Instant ( time:: Instant :: now ( ) )
@@ -138,6 +167,18 @@ impl Instant {
138
167
/// only be possible if `earlier` was created after `self`. Because
139
168
/// `Instant` is monotonic, the only time that this should happen should be
140
169
/// a bug.
170
+ ///
171
+ /// # Examples
172
+ ///
173
+ /// ```no_run
174
+ /// use std::time::{Duration, Instant};
175
+ /// use std::thread::sleep;
176
+ ///
177
+ /// let now = Instant::now();
178
+ /// sleep(Duration::new(1, 0));
179
+ /// let new_now = Instant::now();
180
+ /// println!("{:?}", new_now.duration_since(now));
181
+ /// ```
141
182
#[ stable( feature = "time2" , since = "1.8.0" ) ]
142
183
pub fn duration_since ( & self , earlier : Instant ) -> Duration {
143
184
self . 0 . sub_instant ( & earlier. 0 )
@@ -218,6 +259,14 @@ impl fmt::Debug for Instant {
218
259
219
260
impl SystemTime {
220
261
/// Returns the system time corresponding to "now".
262
+ ///
263
+ /// # Examples
264
+ ///
265
+ /// ```
266
+ /// use std::time::SystemTime;
267
+ ///
268
+ /// let sys_time = SystemTime::now();
269
+ /// ```
221
270
#[ stable( feature = "time2" , since = "1.8.0" ) ]
222
271
pub fn now ( ) -> SystemTime {
223
272
SystemTime ( time:: SystemTime :: now ( ) )
@@ -229,11 +278,26 @@ impl SystemTime {
229
278
/// guaranteed to always be before later measurements (due to anomalies such
230
279
/// as the system clock being adjusted either forwards or backwards).
231
280
///
232
- /// If successful, `Ok( Duration)` is returned where the duration represents
281
+ /// If successful, [ `Ok`]`(`[` Duration`]` )` is returned where the duration represents
233
282
/// the amount of time elapsed from the specified measurement to this one.
234
283
///
235
- /// Returns an `Err` if `earlier` is later than `self`, and the error
284
+ /// Returns an [ `Err`] if `earlier` is later than `self`, and the error
236
285
/// contains how far from `self` the time is.
286
+ ///
287
+ /// [`Ok`]: ../../std/result/enum.Result.html#variant.Ok
288
+ /// [`Duration`]: ../../std/time/struct.Duration.html
289
+ /// [`Err`]: ../../std/result/enum.Result.html#variant.Err
290
+ ///
291
+ /// # Examples
292
+ ///
293
+ /// ```
294
+ /// use std::time::SystemTime;
295
+ ///
296
+ /// let sys_time = SystemTime::now();
297
+ /// let difference = sys_time.duration_since(sys_time)
298
+ /// .expect("SystemTime::duration_since failed");
299
+ /// println!("{:?}", difference);
300
+ /// ```
237
301
#[ stable( feature = "time2" , since = "1.8.0" ) ]
238
302
pub fn duration_since ( & self , earlier : SystemTime )
239
303
-> Result < Duration , SystemTimeError > {
@@ -244,12 +308,28 @@ impl SystemTime {
244
308
///
245
309
/// This function may fail as the underlying system clock is susceptible to
246
310
/// drift and updates (e.g. the system clock could go backwards), so this
247
- /// function may not always succeed. If successful, `Ok(duration )` is
311
+ /// function may not always succeed. If successful, [ `Ok`]`(`[`Duration`]` )` is
248
312
/// returned where the duration represents the amount of time elapsed from
249
313
/// this time measurement to the current time.
250
314
///
251
- /// Returns an `Err` if `self` is later than the current system time, and
315
+ /// Returns an [ `Err`] if `self` is later than the current system time, and
252
316
/// the error contains how far from the current system time `self` is.
317
+ ///
318
+ /// [`Ok`]: ../../std/result/enum.Result.html#variant.Ok
319
+ /// [`Duration`]: ../../std/time/struct.Duration.html
320
+ /// [`Err`]: ../../std/result/enum.Result.html#variant.Err
321
+ ///
322
+ /// # Examples
323
+ ///
324
+ /// ```no_run
325
+ /// use std::thread::sleep;
326
+ /// use std::time::{Duration, SystemTime};
327
+ ///
328
+ /// let sys_time = SystemTime::now();
329
+ /// let one_sec = Duration::from_secs(1);
330
+ /// sleep(one_sec);
331
+ /// assert!(sys_time.elapsed().unwrap() >= one_sec);
332
+ /// ```
253
333
#[ stable( feature = "time2" , since = "1.8.0" ) ]
254
334
pub fn elapsed ( & self ) -> Result < Duration , SystemTimeError > {
255
335
SystemTime :: now ( ) . duration_since ( * self )
@@ -300,19 +380,40 @@ impl fmt::Debug for SystemTime {
300
380
///
301
381
/// This constant is defined to be "1970-01-01 00:00:00 UTC" on all systems with
302
382
/// respect to the system clock. Using `duration_since` on an existing
303
- /// `SystemTime` instance can tell how far away from this point in time a
383
+ /// [ `SystemTime`] instance can tell how far away from this point in time a
304
384
/// measurement lies, and using `UNIX_EPOCH + duration` can be used to create a
305
- /// `SystemTime` instance to represent another fixed point in time.
385
+ /// [`SystemTime`] instance to represent another fixed point in time.
386
+ ///
387
+ /// [`SystemTime`]: ../../std/time/struct.SystemTime.html
306
388
#[ stable( feature = "time2" , since = "1.8.0" ) ]
307
389
pub const UNIX_EPOCH : SystemTime = SystemTime ( time:: UNIX_EPOCH ) ;
308
390
309
391
impl SystemTimeError {
310
392
/// Returns the positive duration which represents how far forward the
311
393
/// second system time was from the first.
312
394
///
313
- /// A `SystemTimeError` is returned from the `duration_since`
314
- /// operation whenever the second system time represents a point later
395
+ /// A `SystemTimeError` is returned from the [ `duration_since`] and [`elapsed`]
396
+ /// methods of [`SystemTime`] whenever the second system time represents a point later
315
397
/// in time than the `self` of the method call.
398
+ ///
399
+ /// [`duration_since`]: ../../std/time/struct.SystemTime.html#method.duration_since
400
+ /// [`elapsed`]: ../../std/time/struct.SystemTime.html#method.elapsed
401
+ /// [`SystemTime`]: ../../std/time/struct.SystemTime.html
402
+ ///
403
+ /// # Examples
404
+ ///
405
+ /// ```no_run
406
+ /// use std::thread::sleep;
407
+ /// use std::time::{Duration, SystemTime};
408
+ ///
409
+ /// let sys_time = SystemTime::now();
410
+ /// sleep(Duration::from_secs(1));
411
+ /// let new_sys_time = SystemTime::now();
412
+ /// match sys_time.duration_since(new_sys_time) {
413
+ /// Ok(_) => {}
414
+ /// Err(e) => println!("SystemTimeError difference: {:?}", e.duration()),
415
+ /// }
416
+ /// ```
316
417
#[ stable( feature = "time2" , since = "1.8.0" ) ]
317
418
pub fn duration ( & self ) -> Duration {
318
419
self . 0
0 commit comments