From 612c526f9a04402d6946b703205af13f9acfc4be Mon Sep 17 00:00:00 2001 From: Nobody Date: Mon, 21 Feb 2022 00:07:51 +0500 Subject: [PATCH] Add deserializer for NaiveDateTime --- src/utils/time_manager.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) 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)?)) + } + } }