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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
use chrono::{DateTime, Local, NaiveTime, TimeZone, Utc};
use serde::Deserialize;
use std::fmt::{Display, Formatter};
use std::ops::Range;

#[derive(Deserialize, Debug, PartialEq, Clone)]
pub struct TimeSlot {
    /// The start of the slot.
    /// If this is after the end, the time slot wraps around midnight.
    start: NaiveTime,
    /// The end of the slot.
    end: NaiveTime,
}

impl TimeSlot {
    pub fn contains(&self, time: &NaiveTime) -> bool {
        return if self.start < self.end {
            self.start <= *time && *time <= self.end
        } else {
            *time > self.start || *time <= self.end
        };
    }

    #[cfg(test)]
    pub fn get_start(&self) -> NaiveTime {
        self.start
    }

    #[cfg(test)]
    pub fn get_end(&self) -> NaiveTime {
        self.end
    }
}

impl From<Range<NaiveTime>> for TimeSlot {
    fn from(range: Range<NaiveTime>) -> Self {
        Self {
            start: range.start,
            end: range.end,
        }
    }
}

impl Display for TimeSlot {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}-{}", self.start, self.end)
    }
}

#[derive(Deserialize, PartialEq, Debug, Clone)]
#[serde(tag = "type")]
pub enum ZonedSlot {
    Utc(TimeSlot),
    Local(TimeSlot),
}

impl ZonedSlot {
    pub fn contains(&self, now: &DateTime<Utc>) -> bool {
        return match self {
            ZonedSlot::Utc(slot) => slot.contains(&now.time()),
            ZonedSlot::Local(slot) => {
                slot.contains(&Local::from_utc_datetime(&Local, &now.naive_utc()).time())
            }
        };
    }
}

impl Display for ZonedSlot {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        return match self {
            ZonedSlot::Utc(slot) => write!(f, "{} UTC", slot),
            ZonedSlot::Local(slot) => write!(f, "{} Local Time", slot),
        };
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::time_util::test_utils::{date, time};
    use chrono::{Local, NaiveDate, NaiveDateTime, TimeZone, Utc};

    fn timeslot_of(start: (u32, u32, u32), end: (u32, u32, u32)) -> TimeSlot {
        (time(start.0, start.1, start.2)..time(end.0, end.1, end.2)).into()
    }

    fn timeslot_contains_time_fn(slot: &TimeSlot, h: u32, m: u32, s: u32) -> bool {
        let time = time(h, m, s);
        slot.contains(&time)
    }

    fn zoned_utc_timeslot_contains_time_fn(
        slot: &ZonedSlot,
        date: NaiveDate,
        h: u32,
        m: u32,
        s: u32,
    ) -> bool {
        let time = time(h, m, s);
        let datetime = Utc::from_utc_datetime(&Utc, &NaiveDateTime::new(date, time));
        slot.contains(&datetime)
    }

    fn zoned_local_timeslot_contains_time_fn(
        slot: &ZonedSlot,
        date: NaiveDate,
        h: u32,
        m: u32,
        s: u32,
    ) -> bool {
        let time = time(h, m, s);
        let local_datetime =
            Local::from_local_datetime(&Local, &NaiveDateTime::new(date, time)).unwrap();
        let utc_datetime = Utc::from_utc_datetime(&Utc, &local_datetime.naive_utc());
        slot.contains(&utc_datetime)
    }

    #[test]
    fn test_timeslot_during_day() {
        let slot: TimeSlot = timeslot_of((12, 13, 00), (15, 52, 12));
        let date = date(2021, 04, 16);

        let slot_contains_time = |h, m, s| timeslot_contains_time_fn(&slot, h, m, s);

        assert!(slot_contains_time(13, 20, 55), "Slot should contain time");
        assert!(
            !slot_contains_time(18, 00, 00),
            "Slot should not contain time"
        );
        assert!(
            !slot_contains_time(18, 00, 00),
            "Slot should not contain time"
        );

        // Same scenarios should apply to UTC zoned when supplied with utc.
        let zoned_time_slot = ZonedSlot::Utc(slot.clone());

        let zoned_slot_contains_time =
            |h, m, s| zoned_utc_timeslot_contains_time_fn(&zoned_time_slot, date, h, m, s);

        assert!(
            zoned_slot_contains_time(13, 20, 55),
            "Zoned Slot should contain time"
        );
        assert!(
            !zoned_slot_contains_time(18, 00, 00),
            "Zoned Slot should not contain time"
        );
        assert!(
            !zoned_slot_contains_time(18, 00, 00),
            "Zoned Slot should not contain time"
        );
    }

    #[test]
    fn test_timeslot_overnight() {
        let slot: TimeSlot = timeslot_of((22, 55, 32), (04, 26, 26));
        let date = date(2021, 04, 16);

        let slot_contains_time = |h, m, s| timeslot_contains_time_fn(&slot, h, m, s);

        assert!(slot_contains_time(02, 00, 00), "Slot should contain time");
        assert!(slot_contains_time(23, 20, 55), "Slot should contain time");
        assert!(
            !slot_contains_time(13, 00, 00),
            "Slot should not contain time"
        );

        // Same scenarios should apply to UTC zoned when supplied with utc.
        let zoned_time_slot = ZonedSlot::Utc(slot.clone());

        let zoned_slot_contains_time =
            |h, m, s| zoned_utc_timeslot_contains_time_fn(&zoned_time_slot, date, h, m, s);

        assert!(
            zoned_slot_contains_time(02, 00, 00),
            "Slot should contain time"
        );
        assert!(
            zoned_slot_contains_time(23, 20, 55),
            "Slot should contain time"
        );
        assert!(
            !zoned_slot_contains_time(13, 00, 00),
            "Slot should not contain time"
        );
    }

    #[test]
    fn test_zoned_local_timeslot_during_day() {
        let zoned_time_slot = ZonedSlot::Local(timeslot_of((12, 13, 00), (15, 52, 12)));
        let date = date(2021, 04, 16);

        let zoned_slot_contains_time =
            |h, m, s| zoned_local_timeslot_contains_time_fn(&zoned_time_slot, date, h, m, s);

        assert!(
            zoned_slot_contains_time(13, 20, 55),
            "Slot should contain time"
        );
        assert!(
            !zoned_slot_contains_time(18, 00, 00),
            "Slot should not contain time"
        );
        assert!(
            !zoned_slot_contains_time(18, 00, 00),
            "Slot should not contain time"
        );

        assert!(
            !zoned_slot_contains_time(11, 30, 00),
            "Slot should not contain time"
        );
        assert!(
            zoned_slot_contains_time(13, 30, 00),
            "Slot should not contain time"
        );
    }

    #[test]
    fn test_zoned_local_timeslot_overnight() {
        let zoned_time_slot = ZonedSlot::Local(timeslot_of((22, 55, 32), (04, 26, 26)));
        let date = date(2021, 04, 16);

        let zoned_slot_contains_time =
            |h, m, s| zoned_local_timeslot_contains_time_fn(&zoned_time_slot, date, h, m, s);

        assert!(
            zoned_slot_contains_time(02, 00, 00),
            "Slot should contain time"
        );
        assert!(
            zoned_slot_contains_time(23, 20, 55),
            "Slot should contain time"
        );
        assert!(
            !zoned_slot_contains_time(13, 00, 00),
            "Slot should not contain time"
        );

        assert!(
            !zoned_slot_contains_time(22, 30, 00),
            "Slot should not contain time"
        );
        assert!(
            zoned_slot_contains_time(23, 30, 00),
            "Slot should not contain time"
        );
    }

    #[test]
    fn manual_check() {
        std::env::set_var("TZ", "GB");
        let zoned_time_slot = ZonedSlot::Local(timeslot_of((22, 55, 32), (04, 26, 26)));
        let bst_date = date(2021, 04, 16); // BST
        let contained =
            Utc::from_utc_datetime(&Utc, &NaiveDateTime::new(bst_date, time(22, 30, 32)));
        assert!(zoned_time_slot.contains(&contained));

        let gmt_date = date(2021, 01, 16); // GMT
        let not_contained =
            Utc::from_utc_datetime(&Utc, &NaiveDateTime::new(gmt_date, time(22, 30, 32)));
        assert!(!zoned_time_slot.contains(&not_contained));
    }
}