cometbft_light_client/components/
clock.rs

1//! Provides an interface and a default implementation of the `Clock` component
2
3use std::convert::TryInto;
4
5use time::OffsetDateTime;
6
7use crate::verifier::types::Time;
8
9/// Abstracts over the current time.
10pub trait Clock: Send + Sync {
11    /// Get the current time.
12    fn now(&self) -> Time;
13}
14
15/// Provides the current wall clock time.
16#[derive(Copy, Clone, Debug)]
17pub struct SystemClock;
18impl Clock for SystemClock {
19    fn now(&self) -> Time {
20        OffsetDateTime::now_utc()
21            .try_into()
22            .expect("system clock produces invalid time")
23    }
24}
25
26#[derive(Copy, Clone, Debug)]
27pub struct FixedClock {
28    now: Time,
29}
30
31impl FixedClock {
32    pub fn new(now: Time) -> Self {
33        Self { now }
34    }
35}
36
37impl Clock for FixedClock {
38    fn now(&self) -> Time {
39        self.now
40    }
41}