chrono::naive::serde::ts_nanoseconds

Function deserialize

Source
pub fn deserialize<'de, D>(d: D) -> Result<NaiveDateTime, D::Error>
where D: Deserializer<'de>,
Expand description

Deserialize a NaiveDateTime from a nanoseconds timestamp

Intended for use with serdes deserialize_with attribute.

ยงExample:

use chrono::naive::serde::ts_nanoseconds::deserialize as from_nano_ts;
#[derive(Debug, PartialEq, Deserialize)]
struct S {
    #[serde(deserialize_with = "from_nano_ts")]
    time: NaiveDateTime,
}

let my_s: S = serde_json::from_str(r#"{ "time": 1526522699918355733 }"#)?;
let expected = DateTime::from_timestamp(1526522699, 918355733).unwrap().naive_utc();
assert_eq!(my_s, S { time: expected });

let my_s: S = serde_json::from_str(r#"{ "time": -1 }"#)?;
let expected = DateTime::from_timestamp(-1, 999_999_999).unwrap().naive_utc();
assert_eq!(my_s, S { time: expected });