Skip to main content

waft_test/
time_zone.rs

1//! [`TestTimeZone`]: the local time zone a [`TestPlatform`](crate::TestPlatform) answers, so a
2//! test of local time reads the zone it chose rather than the machine's.
3
4use std::fmt;
5use std::rc::Rc;
6
7use waft_embedder::SignedDuration;
8
9/// The offset from UTC and the zone's name at an instant in seconds since the epoch, as a host
10/// answers `Platform::time_zone_offset` and `Platform::time_zone_name`.
11type Answer = dyn Fn(i64) -> (SignedDuration, String);
12
13/// A local time zone for a test: UTC, a fixed offset and name, or any rule of the instant, such
14/// as a zone with summer time.
15#[derive(Clone)]
16pub struct TestTimeZone {
17    answer: Rc<Answer>,
18}
19
20impl TestTimeZone {
21    /// UTC, the zone a `TestPlatform` starts in.
22    pub fn utc() -> TestTimeZone {
23        TestTimeZone::fixed(SignedDuration::ZERO, "UTC")
24    }
25
26    /// A zone that is `offset` from UTC all year, named `name`.
27    pub fn fixed(offset: SignedDuration, name: &str) -> TestTimeZone {
28        let name = name.to_string();
29        TestTimeZone::new(move |_| (offset, name.clone()))
30    }
31
32    /// A zone whose offset and name at an instant in seconds since the epoch `rule` answers.
33    pub fn new(rule: impl Fn(i64) -> (SignedDuration, String) + 'static) -> TestTimeZone {
34        TestTimeZone {
35            answer: Rc::new(rule),
36        }
37    }
38
39    /// The offset from UTC at `seconds_since_epoch`.
40    pub fn offset(&self, seconds_since_epoch: i64) -> SignedDuration {
41        (self.answer)(seconds_since_epoch).0
42    }
43
44    /// The zone's name at `seconds_since_epoch`.
45    pub fn name(&self, seconds_since_epoch: i64) -> String {
46        (self.answer)(seconds_since_epoch).1
47    }
48}
49
50impl Default for TestTimeZone {
51    fn default() -> TestTimeZone {
52        TestTimeZone::utc()
53    }
54}
55
56impl fmt::Debug for TestTimeZone {
57    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
58        f.debug_struct("TestTimeZone").finish_non_exhaustive()
59    }
60}
61
62#[cfg(test)]
63mod tests {
64    use std::rc::Rc;
65
66    use waft_embedder::SignedDuration;
67    use waft_foundation::{AppCell, DateTime};
68
69    use crate::{TestPlatform, TestTimeZone};
70
71    #[test]
72    fn a_test_platform_is_in_utc_until_a_case_sets_its_zone() {
73        let platform = Rc::new(TestPlatform::new());
74        let cell = AppCell::with_platform(Rc::clone(&platform) as waft_embedder::PlatformRef);
75        let app = cell.borrow();
76        let new_year = DateTime::new(&app, 2026, 1, 1);
77        assert_eq!(new_year.time_zone_offset(&app), SignedDuration::ZERO);
78        assert_eq!(new_year.time_zone_name(&app), "UTC");
79
80        platform.set_time_zone(TestTimeZone::fixed(SignedDuration::from_hours(1), "CET"));
81        let in_paris = DateTime::new(&app, 2026, 1, 1);
82        assert_eq!(
83            in_paris.time_zone_offset(&app),
84            SignedDuration::from_hours(1)
85        );
86        assert_eq!(in_paris.to_utc().hour(&app), 23);
87        assert_eq!(
88            new_year.hour(&app),
89            1,
90            "the fields read the zone the platform answers now"
91        );
92        assert_eq!(
93            new_year.time_zone_offset(&app),
94            SignedDuration::from_hours(1),
95            "asked again"
96        );
97        assert_eq!(new_year.time_zone_name(&app), "CET", "asked again");
98    }
99}