use chrono::{DateTime, Utc}; use serde::de::Error as DeError; use serde::{Deserialize, Serialize}; /// Wrapping struct for `DateTime`. #[derive(Copy, Clone)] pub struct DateTimeUTC(DateTime); impl From> for DateTimeUTC { fn from(value: DateTime) -> Self { Self(value) } } impl From for DateTime { fn from(value: DateTimeUTC) -> Self { value.0 } } impl Serialize for DateTimeUTC { fn serialize(&self, serializer: S) -> Result where S: serde::Serializer, { let (total_seconds, subsec_nano) = (self.0.timestamp(), self.0.timestamp_subsec_nanos()); (total_seconds, subsec_nano).serialize(serializer) } } impl<'de> Deserialize<'de> for DateTimeUTC { fn deserialize(deserializer: D) -> Result where D: serde::Deserializer<'de>, { let (sec, subsec_nano): (i64, u32) = Deserialize::deserialize(deserializer)?; if let Some(dt) = chrono::DateTime::from_timestamp(sec, subsec_nano) { Ok(dt.into()) } else { Err(DeError::custom("DateTimeUTC deserialization failed")) } } }