Module chrono::serde::ts_microseconds

source ·
Expand description

Ser/de to/from timestamps in microseconds

Intended for use with serde’s with attribute.

§Example:

use chrono::serde::ts_microseconds;
#[derive(Deserialize, Serialize)]
struct S {
    #[serde(with = "ts_microseconds")]
    time: DateTime<Utc>,
}

let time = NaiveDate::from_ymd_opt(2018, 5, 17)
    .unwrap()
    .and_hms_micro_opt(02, 04, 59, 918355)
    .unwrap()
    .and_local_timezone(Utc)
    .unwrap();
let my_s = S { time: time.clone() };

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

Functions§

  • Deserialize a DateTime from a microsecond timestamp
  • Serialize a UTC datetime into an integer number of microseconds since the epoch