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#[cfg(feature = "std")]
22#[allow(clippy::exhaustive_structs)]
23#[derive(Debug)]
24pub struct DefaultTimeProvider;
25
26#[cfg(feature = "std")]
27impl TimeProvider for DefaultTimeProvider {
28 fn current_time(&self) -> Option<UnixTime> {
29 Some(UnixTime::now())
30 }
31}