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

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

Intended for use with serdes serialize_with attribute.


use chrono::naive::serde::ts_seconds::serialize as to_ts;
struct S {
    #[serde(serialize_with = "to_ts")]
    time: NaiveDateTime

let my_s = S {
    time: NaiveDate::from_ymd(2015, 5, 15).and_hms(10, 0, 0),
let as_string = serde_json::to_string(&my_s)?;
assert_eq!(as_string, r#"{"time":1431684000}"#);