pub fn serialize<S>(
    opt: &Option<DateTime<Utc>>,
    serializer: S
) -> Result<S::Ok, S::Error>where
    S: Serializer,
Available on crate feature serde only.
Expand description

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

Intended for use with serdes serialize_with attribute.

Example:

use chrono::serde::ts_nanoseconds_option::serialize as to_nano_tsopt;
#[derive(Serialize)]
struct S {
    #[serde(serialize_with = "to_nano_tsopt")]
    time: Option<DateTime<Utc>>
}

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