Skip to content

Commit 117fa1d

Browse files
authored
Rollup merge of rust-lang#63053 - kornelski:clockdrift, r=shepmaster
SystemTime docs: recommend Instant for elapsed time Introduction to `SystemTime` mentions problems with non-monotonic clocks, but individual methods don't. For benefit of users who jump directly to method's documentation, also recommend `Instant` in `elapsed` and `duration_since`. `SystemTime::elapsed()` docs overpromised the elapsed time. It's not elapsed time, but a difference between two clocks.
2 parents a3cae57 + 55c07b3 commit 117fa1d

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

src/libstd/time.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,7 @@ impl SystemTime {
396396
/// This function may fail because measurements taken earlier are not
397397
/// guaranteed to always be before later measurements (due to anomalies such
398398
/// as the system clock being adjusted either forwards or backwards).
399+
/// [`Instant`] can be used to measure elapsed time without this risk of failure.
399400
///
400401
/// If successful, [`Ok`]`(`[`Duration`]`)` is returned where the duration represents
401402
/// the amount of time elapsed from the specified measurement to this one.
@@ -406,6 +407,7 @@ impl SystemTime {
406407
/// [`Ok`]: ../../std/result/enum.Result.html#variant.Ok
407408
/// [`Duration`]: ../../std/time/struct.Duration.html
408409
/// [`Err`]: ../../std/result/enum.Result.html#variant.Err
410+
/// [`Instant`]: ../../std/time/struct.Instant.html
409411
///
410412
/// # Examples
411413
///
@@ -414,7 +416,7 @@ impl SystemTime {
414416
///
415417
/// let sys_time = SystemTime::now();
416418
/// let difference = sys_time.duration_since(sys_time)
417-
/// .expect("SystemTime::duration_since failed");
419+
/// .expect("Clock may have gone backwards");
418420
/// println!("{:?}", difference);
419421
/// ```
420422
#[stable(feature = "time2", since = "1.8.0")]
@@ -423,20 +425,24 @@ impl SystemTime {
423425
self.0.sub_time(&earlier.0).map_err(SystemTimeError)
424426
}
425427

426-
/// Returns the amount of time elapsed since this system time was created.
428+
/// Returns the difference between the clock time when this
429+
/// system time was created, and the current clock time.
427430
///
428431
/// This function may fail as the underlying system clock is susceptible to
429432
/// drift and updates (e.g., the system clock could go backwards), so this
430433
/// function may not always succeed. If successful, [`Ok`]`(`[`Duration`]`)` is
431434
/// returned where the duration represents the amount of time elapsed from
432435
/// this time measurement to the current time.
433436
///
437+
/// To measure elapsed time reliably, use [`Instant`] instead.
438+
///
434439
/// Returns an [`Err`] if `self` is later than the current system time, and
435440
/// the error contains how far from the current system time `self` is.
436441
///
437442
/// [`Ok`]: ../../std/result/enum.Result.html#variant.Ok
438443
/// [`Duration`]: ../../std/time/struct.Duration.html
439444
/// [`Err`]: ../../std/result/enum.Result.html#variant.Err
445+
/// [`Instant`]: ../../std/time/struct.Instant.html
440446
///
441447
/// # Examples
442448
///

0 commit comments

Comments
 (0)