1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
pub mod date_format {
use chrono::{DateTime, Utc};
use serde::Serializer;
// Stripped out nanoseconds from ISO 8601.
const FORMAT: &'static str = "%Y-%m-%dT%H:%M:%SZ";
// The signature of a serialize_with function must follow the pattern:
//
// fn serialize<S>(&T, S) -> Result<S::Ok, S::Error> where S: Serializer
//
// although it may also be generic over the input types T.
pub fn serialize<S>(date: &DateTime<Utc>, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer
{
let s = format!("{}", date.format(FORMAT));
serializer.serialize_str(&s)
}
}
pub mod optional_date_format {
use chrono::{DateTime, Utc};
use serde::Serializer;
// Stripped out nanoseconds from ISO 8601.
const FORMAT: &'static str = "%Y-%m-%dT%H:%M:%SZ";
// The signature of a serialize_with function must follow the pattern:
//
// fn serialize<S>(&T, S) -> Result<S::Ok, S::Error> where S: Serializer
//
// although it may also be generic over the input types T.
pub fn serialize<S>(date: &Option<DateTime<Utc>>, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer
{
if let Some(ref d) = *date {
let s = format!("{}", &d.format(FORMAT));
return serializer.serialize_str(&s)
};
serializer.serialize_none()
}
}