chrono::serde::ts_milliseconds

Function deserialize

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

Deserialize a DateTime from a millisecond timestamp

Intended for use with serdes deserialize_with attribute.

ยงExample:

use chrono::serde::ts_milliseconds::deserialize as from_milli_ts;
#[derive(Debug, PartialEq, Deserialize)]
struct S {
    #[serde(deserialize_with = "from_milli_ts")]
    time: DateTime<Utc>,
}

let my_s: S = serde_json::from_str(r#"{ "time": 1526522699918 }"#)?;
assert_eq!(my_s, S { time: Utc.timestamp_opt(1526522699, 918000000).unwrap() });

let my_s: S = serde_json::from_str(r#"{ "time": -1 }"#)?;
assert_eq!(my_s, S { time: Utc.timestamp_opt(-1, 999_000_000).unwrap() });