chrono::naive::serde

Module ts_nanoseconds_option

Source
Expand description

Ser/de to/from optional timestamps in nanoseconds

Intended for use with serde’s with attribute.

§Example:

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

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

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

Functions§

  • Deserialize a NaiveDateTime from a nanosecond timestamp or none
  • Serialize a datetime into an integer number of nanoseconds since the epoch or none