summaryrefslogtreecommitdiffstats
path: root/crates/common/clock/src/lib.rs
blob: 7c87d4a0bc340653fea7af81e14b5814b02c5585 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
use mockall::automock;
use serde::{Deserialize, Deserializer};
use time::{format_description::well_known::Rfc3339, OffsetDateTime};

pub type Timestamp = OffsetDateTime;

#[automock]
pub trait Clock: Sync + Send + 'static {
    fn now(&self) -> Timestamp;
}

#[derive(Clone)]
pub struct WallClock;

impl Clock for WallClock {
    fn now(&self) -> Timestamp {
        OffsetDateTime::now_utc()
    }
}

pub fn deserialize_iso8601_timestamp<'de, D>(
    deserializer: D,
) -> Result<Option<OffsetDateTime>, D::Error>
where
    D: Deserializer<'de>,
{
    let timestamp = String::deserialize(deserializer)?;
    OffsetDateTime::parse(timestamp.as_str(), &Rfc3339)
        .map_err(serde::de::Error::custom)
        .map(Some)
}