Skip to content

Commit 881fa8a

Browse files
committed
time: More code examples for SystemTime limits
1 parent c0baa6f commit 881fa8a

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

library/std/src/time.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,15 @@ impl SystemTime {
527527
///
528528
/// // But adding just 1ns will already fail.
529529
/// assert_eq!(SystemTime::MAX.checked_add(Duration::new(0, 1)), None);
530+
///
531+
/// // Utilize this for saturating artihmetic to improve error handling.
532+
/// // In this case, we will use a certificate with a timestamp in the
533+
/// // future as a practical example.
534+
/// let configured_offset = Duration::from_secs(60 * 60 * 24);
535+
/// let valid_after =
536+
/// SystemTime::now()
537+
/// .checked_add(configured_offset)
538+
/// .unwrap_or(SystemTime::MAX);
530539
/// ```
531540
#[unstable(feature = "time_systemtime_limits", issue = "149067")]
532541
pub const MAX: SystemTime = SystemTime(time::SystemTime::MAX);
@@ -553,6 +562,14 @@ impl SystemTime {
553562
///
554563
/// // But subtracting just 1ns will already fail.
555564
/// assert_eq!(SystemTime::MIN.checked_sub(Duration::new(0, 1)), None);
565+
///
566+
/// // Utilize this for saturating artihmetic to improve error handling.
567+
/// // In this case, we will use a cache expiry as a practical example.
568+
/// let configured_expiry = Duration::from_secs(60 * 3);
569+
/// let expiry_threshold =
570+
/// SystemTime::now()
571+
/// .checked_sub(configured_expiry)
572+
/// .unwrap_or(SystemTime::MIN);
556573
/// ```
557574
#[unstable(feature = "time_systemtime_limits", issue = "149067")]
558575
pub const MIN: SystemTime = SystemTime(time::SystemTime::MIN);

0 commit comments

Comments
 (0)