Skip to main content

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/// Default `TimeProvider` implementation that uses `std`
21#[expect(clippy::exhaustive_structs)]
22#[derive(Debug)]
23pub struct DefaultTimeProvider;
24
25impl TimeProvider for DefaultTimeProvider {
26    fn current_time(&self) -> Option<UnixTime> {
27        Some(UnixTime::now())
28    }
29}