Function chrono::naive::serde::ts_nanoseconds::serialize[][src]

pub fn serialize<S>(
    dt: &NaiveDateTime,
    serializer: S
) -> Result<S::Ok, S::Error> where
    S: Serializer

Serialize a UTC datetime into an integer number of nanoseconds since the epoch

Intended for use with serdes serialize_with attribute.

Example:

use chrono::naive::serde::ts_nanoseconds::serialize as to_nano_ts;
#[derive(Serialize)]
struct S {
    #[serde(serialize_with = "to_nano_ts")]
    time: NaiveDateTime
}

let my_s = S {
    time: NaiveDate::from_ymd(2018, 5, 17).and_hms_nano(02, 04, 59, 918355733),
};
let as_string = serde_json::to_string(&my_s)?;
assert_eq!(as_string, r#"{"time":1526522699918355733}"#);