Refactor date serde
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
use crate::bodystream::response;
|
||||
use crate::err::Error;
|
||||
use crate::ReqCtx;
|
||||
use err::anyhow::Context;
|
||||
use futures_util::StreamExt;
|
||||
use http::Method;
|
||||
use http::Request;
|
||||
|
||||
@@ -642,6 +642,7 @@ pub struct StatusBoardEntry {
|
||||
|
||||
mod instant_serde {
|
||||
use super::*;
|
||||
use netpod::DATETIME_FMT_3MS;
|
||||
use serde::Serializer;
|
||||
pub fn ser<S: Serializer>(x: &SystemTime, ser: S) -> Result<S::Ok, S::Error> {
|
||||
use chrono::LocalResult;
|
||||
@@ -650,11 +651,11 @@ mod instant_serde {
|
||||
match res {
|
||||
LocalResult::None => Err(serde::ser::Error::custom(format!("Bad local instant conversion"))),
|
||||
LocalResult::Single(dt) => {
|
||||
let s = dt.format("%Y-%m-%dT%H:%M:%S%.3fZ").to_string();
|
||||
let s = dt.format(DATETIME_FMT_3MS).to_string();
|
||||
ser.serialize_str(&s)
|
||||
}
|
||||
LocalResult::Ambiguous(dt, _dt2) => {
|
||||
let s = dt.format("%Y-%m-%dT%H:%M:%S%.3fZ").to_string();
|
||||
let s = dt.format(DATETIME_FMT_3MS).to_string();
|
||||
ser.serialize_str(&s)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,6 +23,7 @@ use netpod::FromUrl;
|
||||
use netpod::HasBackend;
|
||||
use netpod::HasTimeout;
|
||||
use netpod::NodeConfigCached;
|
||||
use netpod::DATETIME_FMT_9MS;
|
||||
use serde::Deserialize;
|
||||
use serde::Serialize;
|
||||
use std::collections::BTreeMap;
|
||||
@@ -1429,12 +1430,11 @@ impl Api4MapPulse2HttpFunction {
|
||||
Ok(Some(val)) => {
|
||||
let sec = val / SEC;
|
||||
let ns = val % SEC;
|
||||
let date_fmt = "%Y-%m-%dT%H:%M:%S.%9fZ";
|
||||
let datetime = Utc
|
||||
.timestamp_opt(sec as i64, ns as u32)
|
||||
.earliest()
|
||||
.ok_or_else(|| Error::with_msg_no_trace("DateTime earliest fail"))?
|
||||
.format(date_fmt)
|
||||
.format(DATETIME_FMT_9MS)
|
||||
.to_string();
|
||||
let res = Api4MapPulse2Response { sec, ns, datetime };
|
||||
Ok(response(StatusCode::OK).body(Body::from(serde_json::to_vec(&res)?))?)
|
||||
|
||||
Reference in New Issue
Block a user