1
use chrono::{TimeZone, Utc};
2
use chrono_tz::Tz;
3
use forms::{now_instant, parse_date, parse_date_in, parse_day, parse_post_date, step_date};
4

            
5
#[test]
6
1
fn rfc3339_keeps_full_precision() {
7
1
    let dt = parse_date("2026-10-27T09:30:15.123456+02:00", false).expect("rfc3339");
8
1
    assert_eq!(dt.to_rfc3339(), "2026-10-27T07:30:15.123456+00:00");
9
1
}
10

            
11
#[test]
12
1
fn org_read_date_forms_are_midnight_free() {
13
1
    let expected = Utc.with_ymd_and_hms(2026, 10, 27, 9, 30, 0).unwrap();
14
1
    assert_eq!(parse_date("2026-10-27T09:30", false), Ok(expected));
15
1
    assert_eq!(parse_date("2026-10-27 09:30", true), Ok(expected));
16
1
}
17

            
18
#[test]
19
1
fn date_only_is_midnight_or_end_of_day() {
20
1
    let start = parse_date("2026-10-27", false).expect("date");
21
1
    let end = parse_date("2026-10-27", true).expect("date");
22
1
    assert_eq!(start.to_rfc3339(), "2026-10-27T00:00:00+00:00");
23
1
    assert_eq!(end.to_rfc3339(), "2026-10-27T23:59:59.999999+00:00");
24
1
}
25

            
26
#[test]
27
1
fn garbage_is_rejected() {
28
1
    assert!(parse_date("not-a-date", false).is_err());
29
1
    assert!(parse_date("2026-13-01", false).is_err());
30
1
    assert!(parse_date("", false).is_err());
31
1
}
32

            
33
#[test]
34
1
fn now_instant_is_a_full_utc_instant() {
35
1
    let now = now_instant();
36
1
    assert!(chrono::DateTime::parse_from_rfc3339(&now).is_ok(), "{now}");
37
1
    assert!(now.ends_with('Z'));
38
1
    assert!(parse_date(&now, false).is_ok());
39
1
}
40

            
41
#[test]
42
1
fn step_preserves_minute_forms() {
43
1
    assert_eq!(
44
1
        step_date("2024-01-31T10:30", 1).as_deref(),
45
1
        Some("2024-02-01T10:30")
46
    );
47
1
    assert_eq!(
48
1
        step_date("2024-03-01 08:15", -1).as_deref(),
49
1
        Some("2024-02-29T08:15")
50
    );
51
1
    assert_eq!(step_date("2024-01-31", 1).as_deref(), Some("2024-02-01"));
52
1
    assert_eq!(step_date("2024-12-31", 1).as_deref(), Some("2025-01-01"));
53
1
}
54

            
55
#[test]
56
1
fn step_keeps_the_seconds_of_an_offset_free_value() {
57
1
    assert_eq!(
58
1
        step_date("2026-10-27T09:30:15", 1).as_deref(),
59
1
        Some("2026-10-28T09:30:15")
60
    );
61
1
    assert_eq!(
62
1
        step_date(" 2026-10-27T09:30:15 ", -1).as_deref(),
63
1
        Some("2026-10-26T09:30:15")
64
    );
65
1
}
66

            
67
#[test]
68
1
fn step_keeps_seconds_fractions_and_offset_of_rfc3339() {
69
1
    assert_eq!(
70
1
        step_date("2026-10-27T09:30:15.123456+00:00", 1).as_deref(),
71
1
        Some("2026-10-28T09:30:15.123456Z")
72
    );
73
1
    assert_eq!(
74
1
        step_date("2026-10-27T09:30:15+02:00", -1).as_deref(),
75
1
        Some("2026-10-26T09:30:15+02:00")
76
    );
77
1
}
78

            
79
#[test]
80
1
fn step_of_garbage_is_none() {
81
1
    assert_eq!(step_date("not-a-date", 1), None);
82
1
    assert_eq!(step_date("", -1), None);
83
1
    assert_eq!(step_date("2026-10-27", i64::MAX), None);
84
1
}
85

            
86
#[test]
87
1
fn range_end_keeps_a_sub_second_instant_of_that_day_inside() {
88
1
    let end = parse_date("2026-10-27", true).expect("date");
89
1
    let late = parse_date("2026-10-27T23:59:59.5Z", false).expect("rfc3339");
90
1
    let next = parse_date("2026-10-28", false).expect("date");
91
1
    assert!(late <= end);
92
1
    assert!(end < next);
93
1
}
94

            
95
#[test]
96
1
fn seconds_without_offset_are_utc() {
97
1
    let expected = Utc.with_ymd_and_hms(2026, 10, 27, 9, 30, 15).unwrap();
98
1
    assert_eq!(parse_date("2026-10-27T09:30:15", false), Ok(expected));
99
1
}
100

            
101
11
fn utc(y: i32, mo: u32, d: u32, h: u32, mi: u32, s: u32) -> chrono::DateTime<Utc> {
102
11
    Utc.with_ymd_and_hms(y, mo, d, h, mi, s).unwrap()
103
11
}
104

            
105
#[test]
106
1
fn a_date_only_range_is_the_local_day_in_tokyo() {
107
1
    let from = parse_date_in("2026-12-01", false, &Tz::Asia__Tokyo).expect("from");
108
1
    let to = parse_date_in("2026-12-01", true, &Tz::Asia__Tokyo).expect("to");
109
1
    assert_eq!(from, utc(2026, 11, 30, 15, 0, 0));
110
1
    assert_eq!(to.to_rfc3339(), "2026-12-01T14:59:59.999999+00:00");
111
1
}
112

            
113
#[test]
114
1
fn a_date_only_range_is_the_local_day_in_new_york() {
115
1
    let from = parse_date_in("2026-12-01", false, &Tz::America__New_York).expect("from");
116
1
    let to = parse_date_in("2026-12-01", true, &Tz::America__New_York).expect("to");
117
1
    assert_eq!(from, utc(2026, 12, 1, 5, 0, 0));
118
1
    assert_eq!(to.to_rfc3339(), "2026-12-02T04:59:59.999999+00:00");
119
1
}
120

            
121
#[test]
122
1
fn a_zone_does_not_move_an_explicit_instant_or_an_offset_free_time() {
123
1
    let zone = Tz::Asia__Tokyo;
124
1
    let explicit = parse_date_in("2026-12-01T00:00:00+02:00", false, &zone).expect("rfc3339");
125
1
    assert_eq!(explicit, utc(2026, 11, 30, 22, 0, 0));
126
1
    let bare = parse_date_in("2026-12-01T09:30", false, &zone).expect("minutes");
127
1
    assert_eq!(bare, utc(2026, 12, 1, 9, 30, 0));
128
1
}
129

            
130
#[test]
131
1
fn a_date_only_post_date_is_noon_utc_and_other_forms_are_unchanged() {
132
1
    assert_eq!(
133
1
        parse_post_date("2026-12-01"),
134
1
        Ok(utc(2026, 12, 1, 12, 0, 0))
135
    );
136
1
    assert_eq!(
137
1
        parse_post_date(" 2026-12-01 "),
138
1
        Ok(utc(2026, 12, 1, 12, 0, 0))
139
    );
140
1
    assert_eq!(
141
1
        parse_post_date("2026-12-01T00:00"),
142
1
        Ok(utc(2026, 12, 1, 0, 0, 0))
143
    );
144
1
    assert_eq!(
145
1
        parse_post_date("2026-12-01T08:00:00+02:00"),
146
1
        Ok(utc(2026, 12, 1, 6, 0, 0))
147
    );
148
1
    assert!(parse_post_date("nope").is_err());
149
1
}
150

            
151
81
fn day(date: &str, range_end: bool, zone: Tz) -> chrono::DateTime<Utc> {
152
81
    parse_date_in(date, range_end, &zone).expect("date")
153
81
}
154

            
155
4
fn rfc(instant: chrono::DateTime<Utc>) -> String {
156
4
    instant.to_rfc3339_opts(chrono::SecondsFormat::Micros, true)
157
4
}
158

            
159
#[test]
160
1
fn a_midnight_skipped_by_a_dst_gap_starts_when_the_gap_ends() {
161
1
    let from = day("2018-11-04", false, Tz::America__Sao_Paulo);
162
1
    assert_eq!(from, utc(2018, 11, 4, 3, 0, 0));
163
1
}
164

            
165
#[test]
166
1
fn a_day_ending_in_a_repeated_hour_keeps_the_whole_hour() {
167
1
    let santiago = day("2026-04-04", true, Tz::America__Santiago);
168
1
    assert_eq!(rfc(santiago), "2026-04-05T03:59:59.999999Z");
169
1
    let beirut = day("2026-10-24", true, Tz::Asia__Beirut);
170
1
    assert_eq!(rfc(beirut), "2026-10-24T21:59:59.999999Z");
171
1
    let cairo_next = day("2026-10-30", false, Tz::Africa__Cairo);
172
1
    let cairo = day("2026-10-29", true, Tz::Africa__Cairo);
173
1
    assert_eq!(cairo + chrono::Duration::microseconds(1), cairo_next);
174
1
}
175

            
176
#[test]
177
1
fn a_skipped_whole_day_resolves_to_the_next_existing_instant() {
178
1
    let apia = Tz::Pacific__Apia;
179
1
    let start = utc(2011, 12, 30, 10, 0, 0);
180
1
    assert_eq!(day("2011-12-30", false, apia), start);
181
1
    assert_eq!(day("2011-12-31", false, apia), start);
182
1
    assert_eq!(
183
1
        rfc(day("2011-12-29", true, apia)),
184
1
        "2011-12-30T09:59:59.999999Z"
185
    );
186
1
    assert_eq!(
187
1
        rfc(day("2011-12-30", true, apia)),
188
1
        "2011-12-30T09:59:59.999999Z"
189
    );
190
1
    let kiritimati = Tz::Pacific__Kiritimati;
191
1
    let start = utc(1994, 12, 31, 10, 0, 0);
192
1
    assert_eq!(day("1994-12-31", false, kiritimati), start);
193
1
    assert_eq!(day("1995-01-01", false, kiritimati), start);
194
1
}
195

            
196
#[test]
197
1
fn back_to_back_days_are_contiguous_in_every_zone() {
198
1
    let days = [
199
1
        ("2026-03-07", "2026-03-08"),
200
1
        ("2026-04-04", "2026-04-05"),
201
1
        ("2018-11-03", "2018-11-04"),
202
1
        ("2026-10-24", "2026-10-25"),
203
1
        ("2026-12-01", "2026-12-02"),
204
1
    ];
205
1
    for zone in [
206
1
        Tz::UTC,
207
1
        Tz::Asia__Tokyo,
208
1
        Tz::America__New_York,
209
1
        Tz::America__Santiago,
210
1
        Tz::America__Sao_Paulo,
211
1
        Tz::Asia__Beirut,
212
1
        Tz::Australia__Lord_Howe,
213
1
    ] {
214
7
        for (first, second) in days {
215
35
            let end = day(first, true, zone);
216
35
            let next = day(second, false, zone);
217
35
            assert_eq!(
218
35
                end + chrono::Duration::microseconds(1),
219
35
                next,
220
                "{zone} {first}"
221
            );
222
        }
223
    }
224
1
}
225

            
226
#[test]
227
1
fn a_day_is_a_plain_date_and_a_time_is_refused() {
228
1
    assert_eq!(
229
1
        parse_day(" 2026-12-01 ").map(|d| d.to_string()),
230
1
        Ok("2026-12-01".to_string())
231
    );
232
1
    for text in [
233
1
        "2026-12-01T10:00",
234
1
        "2026-12-01 10:00",
235
1
        "2026-12-01T10:00:00Z",
236
1
    ] {
237
3
        let message = parse_day(text).expect_err(text);
238
3
        assert!(message.contains("is not a day"), "{message}");
239
    }
240
1
}