pub fn deserialize_datetime_utc_from_milliseconds<'de, D>(
deserializer: D,
) -> Result<DateTime<Utc>, D::Error>where
D: Deserializer<'de>,
Expand description
Deserializes a chrono::DateTime<Utc>
from a milliseconds time stamp. Useful when the data is coming from a number
which is not a seconds time stamp but milliseconds one. It also handles the string to number conversion if the
data was passed as a string with number inside like “1519927261900”.
§Example:
use chrono::prelude::*;
use serde_aux::prelude::*;
#[derive(serde::Deserialize, Debug)]
struct MyStruct {
#[serde(deserialize_with = "deserialize_datetime_utc_from_milliseconds")]
time: DateTime<Utc>,
}
let s = r#" { "time": "1519927261900" } "#;
let a: MyStruct = serde_json::from_str(s).unwrap();
assert_eq!(a.time.timestamp(), 1519927261);
assert_eq!(a.time.timestamp_subsec_millis(), 900);