Function chrono::serde::ts_milliseconds::serialize

source ·
pub fn serialize<S>(
    dt: &DateTime<Utc>,
    serializer: S,
) -> Result<S::Ok, S::Error>
where S: Serializer,
Expand description

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

Intended for use with serdes serialize_with attribute.

§Example:

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

let my_s = S {
    time: NaiveDate::from_ymd_opt(2018, 5, 17)
        .unwrap()
        .and_hms_milli_opt(02, 04, 59, 918)
        .unwrap()
        .and_utc(),
};
let as_string = serde_json::to_string(&my_s)?;
assert_eq!(as_string, r#"{"time":1526522699918}"#);