@@ -511,6 +511,69 @@ impl SystemTime {
511511 #[ stable( feature = "assoc_unix_epoch" , since = "1.28.0" ) ]
512512 pub const UNIX_EPOCH : SystemTime = UNIX_EPOCH ;
513513
514+ /// Represents the maximum value representable by [`SystemTime`] on this platform.
515+ ///
516+ /// This value differs a lot between platforms, but it is always the case
517+ /// that any positive addition to [`SystemTime::MAX`] will fail.
518+ ///
519+ /// # Examples
520+ ///
521+ /// ```no_run
522+ /// #![feature(time_systemtime_limits)]
523+ /// use std::time::{Duration, SystemTime};
524+ ///
525+ /// // Adding zero will change nothing.
526+ /// assert_eq!(SystemTime::MAX.checked_add(Duration::ZERO), Some(SystemTime::MAX));
527+ ///
528+ /// // But adding just 1ns will already fail.
529+ /// assert_eq!(SystemTime::MAX.checked_add(Duration::new(0, 1)), None);
530+ ///
531+ /// // Utilize this for saturating arithmetic 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);
539+ /// ```
540+ #[ unstable( feature = "time_systemtime_limits" , issue = "149067" ) ]
541+ pub const MAX : SystemTime = SystemTime ( time:: SystemTime :: MAX ) ;
542+
543+ /// Represents the minimum value representable by [`SystemTime`] on this platform.
544+ ///
545+ /// This value differs a lot between platforms, but it is always the case
546+ /// that any positive subtraction from [`SystemTime::MIN`] will fail.
547+ ///
548+ /// Depending on the platform, this may be either less than or equal to
549+ /// [`SystemTime::UNIX_EPOCH`], depending on whether the operating system
550+ /// supports the representation of timestamps before the Unix epoch or not.
551+ /// However, it is always guaranteed that a [`SystemTime::UNIX_EPOCH`] fits
552+ /// between a [`SystemTime::MIN`] and [`SystemTime::MAX`].
553+ ///
554+ /// # Examples
555+ ///
556+ /// ```
557+ /// #![feature(time_systemtime_limits)]
558+ /// use std::time::{Duration, SystemTime};
559+ ///
560+ /// // Subtracting zero will change nothing.
561+ /// assert_eq!(SystemTime::MIN.checked_sub(Duration::ZERO), Some(SystemTime::MIN));
562+ ///
563+ /// // But subtracting just 1ns will already fail.
564+ /// assert_eq!(SystemTime::MIN.checked_sub(Duration::new(0, 1)), None);
565+ ///
566+ /// // Utilize this for saturating arithmetic 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);
573+ /// ```
574+ #[ unstable( feature = "time_systemtime_limits" , issue = "149067" ) ]
575+ pub const MIN : SystemTime = SystemTime ( time:: SystemTime :: MIN ) ;
576+
514577 /// Returns the system time corresponding to "now".
515578 ///
516579 /// # Examples
0 commit comments