1 #![cfg(all(
2     feature = "toml",
3     feature = "json",
4     feature = "yaml",
5     feature = "ini",
6     feature = "ron",
7 ))]
8 
9 use chrono::{DateTime, TimeZone, Utc};
10 use config::{Config, File, FileFormat};
11 
make() -> Config12 fn make() -> Config {
13     Config::builder()
14         .add_source(File::from_str(
15             r#"
16             {
17                 "json_datetime": "2017-05-10T02:14:53Z"
18             }
19             "#,
20             FileFormat::Json,
21         ))
22         .add_source(File::from_str(
23             r#"
24             yaml_datetime: 2017-06-12T10:58:30Z
25             "#,
26             FileFormat::Yaml,
27         ))
28         .add_source(File::from_str(
29             r#"
30             toml_datetime = 2017-05-11T14:55:15Z
31             "#,
32             FileFormat::Toml,
33         ))
34         .add_source(File::from_str(
35             r#"
36                 ini_datetime = 2017-05-10T02:14:53Z
37             "#,
38             FileFormat::Ini,
39         ))
40         .add_source(File::from_str(
41             r#"
42             (
43                 ron_datetime: "2021-04-19T11:33:02Z"
44             )
45             "#,
46             FileFormat::Ron,
47         ))
48         .build()
49         .unwrap()
50 }
51 
52 #[test]
test_datetime_string()53 fn test_datetime_string() {
54     let s = make();
55 
56     // JSON
57     let date: String = s.get("json_datetime").unwrap();
58 
59     assert_eq!(&date, "2017-05-10T02:14:53Z");
60 
61     // TOML
62     let date: String = s.get("toml_datetime").unwrap();
63 
64     assert_eq!(&date, "2017-05-11T14:55:15Z");
65 
66     // YAML
67     let date: String = s.get("yaml_datetime").unwrap();
68 
69     assert_eq!(&date, "2017-06-12T10:58:30Z");
70 
71     // INI
72     let date: String = s.get("ini_datetime").unwrap();
73 
74     assert_eq!(&date, "2017-05-10T02:14:53Z");
75 
76     // RON
77     let date: String = s.get("ron_datetime").unwrap();
78 
79     assert_eq!(&date, "2021-04-19T11:33:02Z");
80 }
81 
82 #[test]
test_datetime()83 fn test_datetime() {
84     let s = make();
85 
86     // JSON
87     let date: DateTime<Utc> = s.get("json_datetime").unwrap();
88 
89     assert_eq!(date, Utc.with_ymd_and_hms(2017, 5, 10, 2, 14, 53).unwrap());
90 
91     // TOML
92     let date: DateTime<Utc> = s.get("toml_datetime").unwrap();
93 
94     assert_eq!(date, Utc.with_ymd_and_hms(2017, 5, 11, 14, 55, 15).unwrap());
95 
96     // YAML
97     let date: DateTime<Utc> = s.get("yaml_datetime").unwrap();
98 
99     assert_eq!(date, Utc.with_ymd_and_hms(2017, 6, 12, 10, 58, 30).unwrap());
100 
101     // INI
102     let date: DateTime<Utc> = s.get("ini_datetime").unwrap();
103 
104     assert_eq!(date, Utc.with_ymd_and_hms(2017, 5, 10, 2, 14, 53).unwrap());
105 
106     // RON
107     let date: DateTime<Utc> = s.get("ron_datetime").unwrap();
108 
109     assert_eq!(date, Utc.with_ymd_and_hms(2021, 4, 19, 11, 33, 2).unwrap());
110 }
111