pub fn serialize<S>(
dt: &NaiveDateTime,
serializer: S,
) -> Result<S::Ok, S::Error>where
S: Serializer,
Expand description
Serialize a datetime into an integer number of nanoseconds since the epoch
Intended for use with serde
s serialize_with
attribute.
§Errors
An i64
with nanosecond precision can span a range of ~584 years. This function returns an
error on an out of range DateTime
.
The dates that can be represented as nanoseconds are between 1677-09-21T00:12:44.0 and 2262-04-11T23:47:16.854775804.
§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_opt(2018, 5, 17).unwrap().and_hms_nano_opt(02, 04, 59, 918355733).unwrap(),
};
let as_string = serde_json::to_string(&my_s)?;
assert_eq!(as_string, r#"{"time":1526522699918355733}"#);