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