1use std::time::SystemTime;
5
6use chrono::offset::Utc;
7use chrono::DateTime;
8use chrono::SecondsFormat;
9
10
11pub fn print_system_time_to_rfc3339(time: &SystemTime) -> String {
13 DateTime::<Utc>::from(*time).to_rfc3339_opts(SecondsFormat::Millis, true)
14}
15
16
17pub fn print_system_time_to_rfc3339_with_nanos(time: &SystemTime) -> String {
19 DateTime::<Utc>::from(*time).to_rfc3339_opts(SecondsFormat::Nanos, true)
22}
23
24
25pub fn print_system_time_to_iso8601_date(time: &SystemTime) -> String {
27 DateTime::<Utc>::from(*time).format("%Y-%m-%d").to_string()
28}
29
30
31#[cfg(test)]
32mod tests {
33 use super::*;
34
35 use crate::parse::parse_system_time_from_str;
36
37
38 #[test]
40 fn print_rfc3339() {
41 let string = "2018-04-01T12:04:17.050Z";
42 let time = parse_system_time_from_str(string).unwrap();
43 let result = print_system_time_to_rfc3339(&time);
44 assert_eq!(result, string)
45 }
46
47
48 #[test]
51 fn print_rfc3339_with_nanos() {
52 let string = "2018-04-01T12:04:17.050937231Z";
53 let time = parse_system_time_from_str(string).unwrap();
54 let result = print_system_time_to_rfc3339_with_nanos(&time);
55 assert_eq!(result, string)
56 }
57
58
59 #[test]
61 fn print_iso8601_date() {
62 let string = "2018-05-07T23:59:59Z";
63 let time = parse_system_time_from_str(string).unwrap();
64 let result = print_system_time_to_iso8601_date(&time);
65 assert_eq!(result, "2018-05-07");
66
67 let string = "2018-05-07T00:00:00Z";
68 let time = parse_system_time_from_str(string).unwrap();
69 let result = print_system_time_to_iso8601_date(&time);
70 assert_eq!(result, "2018-05-07");
71 }
72}