diff --git a/src/utils/time_manager.rs b/src/utils/time_manager.rs index a6b2c5b..2943627 100644 --- a/src/utils/time_manager.rs +++ b/src/utils/time_manager.rs @@ -1,6 +1,9 @@ use std::time::SystemTime; use std::convert::TryInto; +use chrono::{DateTime, NaiveDateTime, Utc}; +use serde::{de::Error, Deserialize, Deserializer, Serialize, Serializer}; + pub struct TimeManager { } @@ -12,4 +15,14 @@ impl TimeManager { pub fn timestamp() -> u64 { return Self::duration_since(SystemTime::UNIX_EPOCH); } + + pub fn deserialize<'de, D: Deserializer<'de>>(deserializer: D) -> std::result::Result, D::Error> { + let time: String = Deserialize::deserialize(deserializer)?; + + if time.is_empty() { + Ok(None) + } else { + Ok(Some(NaiveDateTime::parse_from_str(&time, "%Y-%m-%d %H:%M:%S").map_err(D::Error::custom)?)) + } + } }