mirror of
https://github.com/RustySamovar/RustySamovar.git
synced 2024-11-22 02:45:34 +00:00
Add deserializer for NaiveDateTime
This commit is contained in:
parent
b128782ac2
commit
612c526f9a
@ -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)?))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user