rustls/
time_provider.rs

1//! The library's source of time.
2
3use core::fmt::Debug;
4
5use pki_types::UnixTime;
6
7/// An object that provides the current time.
8///
9/// This is used to, for example, check if a certificate has expired during
10/// certificate validation, or to check the age of a ticket.
11pub trait TimeProvider: Debug + Send + Sync {
12    /// Returns the current wall time.
13    ///
14    /// This is not required to be monotonic.
15    ///
16    /// Return `None` if unable to retrieve the time.
17    fn current_time(&self) -> Option<UnixTime>;
18}
19
20#[derive(Debug)]
21#[cfg(feature = "std")]
22/// Default `TimeProvider` implementation that uses `std`
23pub struct DefaultTimeProvider;
24
25#[cfg(feature = "std")]
26impl TimeProvider for DefaultTimeProvider {
27    fn current_time(&self) -> Option<UnixTime> {
28        Some(UnixTime::now())
29    }
30}