summaryrefslogtreecommitdiffstats
path: root/tests/datetime.rs
blob: 2b0c22d870e06ef49513d3e003390fa019ccc28e (plain)
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#![cfg(all(
    feature = "toml",
    feature = "json",
    feature = "hjson",
    feature = "yaml",
    feature = "ini",
    feature = "ron",
))]

extern crate chrono;
extern crate config;

use chrono::{DateTime, TimeZone, Utc};
use config::*;

fn make() -> Config {
    Config::builder()
        .add_source(File::from_str(
            r#"
            {
                "json_datetime": "2017-05-10T02:14:53Z"
            }
            "#,
            FileFormat::Json,
        ))
        .add_source(File::from_str(
            r#"
            yaml_datetime: 2017-06-12T10:58:30Z
            "#,
            FileFormat::Yaml,
        ))
        .add_source(File::from_str(
            r#"
            toml_datetime = 2017-05-11T14:55:15Z
            "#,
            FileFormat::Toml,
        ))
        .add_source(File::from_str(
            r#"
            {
                "hjson_datetime": "2017-05-10T02:14:53Z"
            }
            "#,
            FileFormat::Hjson,
        ))
        .add_source(File::from_str(
            r#"
                ini_datetime = 2017-05-10T02:14:53Z
            "#,
            FileFormat::Ini,
        ))
        .add_source(File::from_str(
            r#"
            (
                ron_datetime: "2021-04-19T11:33:02Z"
            )
            "#,
            FileFormat::Ron,
        ))
        .build()
        .unwrap()
}

#[test]
fn test_datetime_string() {
    let s = make();

    // JSON
    let date: String = s.get("json_datetime").unwrap();

    assert_eq!(&date, "2017-05-10T02:14:53Z");

    // TOML
    let date: String = s.get("toml_datetime").unwrap();

    assert_eq!(&date, "2017-05-11T14:55:15Z");

    // YAML
    let date: String = s.get("yaml_datetime").unwrap();

    assert_eq!(&date, "2017-06-12T10:58:30Z");

    // HJSON
    let date: String = s.get("hjson_datetime").unwrap();

    assert_eq!(&date, "2017-05-10T02:14:53Z");

    // INI
    let date: String = s.get("ini_datetime").unwrap();

    assert_eq!(&date, "2017-05-10T02:14:53Z");

    // RON
    let date: String = s.get("ron_datetime").unwrap();

    assert_eq!(&date, "2021-04-19T11:33:02Z");
}

#[test]
fn test_datetime() {
    let s = make();

    // JSON
    let date: DateTime<Utc> = s.get("json_datetime").unwrap();

    assert_eq!(date, Utc.ymd(2017, 5, 10).and_hms(2, 14, 53));

    // TOML
    let date: DateTime<Utc> = s.get("toml_datetime").unwrap();

    assert_eq!(date, Utc.ymd(2017, 5, 11).and_hms(14, 55, 15));

    // YAML
    let date: DateTime<Utc> = s.get("yaml_datetime").unwrap();

    assert_eq!(date, Utc.ymd(2017, 6, 12).and_hms(10, 58, 30));

    // HJSON
    let date: DateTime<Utc> = s.get("hjson_datetime").unwrap();

    assert_eq!(date, Utc.ymd(2017, 5, 10).and_hms(2, 14, 53));

    // INI
    let date: DateTime<Utc> = s.get("ini_datetime").unwrap();

    assert_eq!(date, Utc.ymd(2017, 5, 10).and_hms(2, 14, 53));

    // RON
    let date: DateTime<Utc> = s.get("ron_datetime").unwrap();

    assert_eq!(date, Utc.ymd(2021, 4, 19).and_hms(11, 33, 2));
}