Add deserializer for NaiveDateTime

This commit is contained in:
Nobody 2022-02-21 00:07:51 +05:00
parent b128782ac2
commit 612c526f9a

View File

@ -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<Option<NaiveDateTime>, 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)?))
}
}
}