chrono::serde::ts_seconds_option

Function serialize

source
pub fn serialize<S>(
    opt: &Option<DateTime<Utc>>,
    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 or none

Intended for use with serdes serialize_with attribute.

ยงExample:

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

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