chrono::naive::serde

Module ts_milliseconds

Source
Expand description

Used to serialize/deserialize from millisecond-precision timestamps

§Example:

use chrono::naive::serde::ts_milliseconds;
#[derive(Deserialize, Serialize)]
struct S {
    #[serde(with = "ts_milliseconds")]
    time: NaiveDateTime,
}

let time =
    NaiveDate::from_ymd_opt(2018, 5, 17).unwrap().and_hms_milli_opt(02, 04, 59, 918).unwrap();
let my_s = S { time: time.clone() };

let as_string = serde_json::to_string(&my_s)?;
assert_eq!(as_string, r#"{"time":1526522699918}"#);
let my_s: S = serde_json::from_str(&as_string)?;
assert_eq!(my_s.time, time);

Functions§

  • Deserialize a NaiveDateTime from a milliseconds timestamp
  • Serialize a datetime into an integer number of milliseconds since the epoch