use std::fmt;
use std::rc::Rc;
use waft_embedder::SignedDuration;
type Answer = dyn Fn(i64) -> (SignedDuration, String);
#[derive(Clone)]
pub struct TestTimeZone {
answer: Rc<Answer>,
}
impl TestTimeZone {
pub fn utc() -> TestTimeZone {
TestTimeZone::fixed(SignedDuration::ZERO, "UTC")
}
pub fn fixed(offset: SignedDuration, name: &str) -> TestTimeZone {
let name = name.to_string();
TestTimeZone::new(move |_| (offset, name.clone()))
}
pub fn new(rule: impl Fn(i64) -> (SignedDuration, String) + 'static) -> TestTimeZone {
TestTimeZone {
answer: Rc::new(rule),
}
}
pub fn offset(&self, seconds_since_epoch: i64) -> SignedDuration {
(self.answer)(seconds_since_epoch).0
}
pub fn name(&self, seconds_since_epoch: i64) -> String {
(self.answer)(seconds_since_epoch).1
}
}
impl Default for TestTimeZone {
fn default() -> TestTimeZone {
TestTimeZone::utc()
}
}
impl fmt::Debug for TestTimeZone {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("TestTimeZone").finish_non_exhaustive()
}
}
#[cfg(test)]
mod tests {
use std::rc::Rc;
use waft_embedder::SignedDuration;
use waft_foundation::{AppCell, DateTime};
use crate::{TestPlatform, TestTimeZone};
#[test]
fn a_test_platform_is_in_utc_until_a_case_sets_its_zone() {
let platform = Rc::new(TestPlatform::new());
let cell = AppCell::with_platform(Rc::clone(&platform) as waft_embedder::PlatformRef);
let app = cell.borrow();
let new_year = DateTime::new(&app, 2026, 1, 1);
assert_eq!(new_year.time_zone_offset(&app), SignedDuration::ZERO);
assert_eq!(new_year.time_zone_name(&app), "UTC");
platform.set_time_zone(TestTimeZone::fixed(SignedDuration::from_hours(1), "CET"));
let in_paris = DateTime::new(&app, 2026, 1, 1);
assert_eq!(
in_paris.time_zone_offset(&app),
SignedDuration::from_hours(1)
);
assert_eq!(in_paris.to_utc().hour(&app), 23);
assert_eq!(
new_year.hour(&app),
1,
"the fields read the zone the platform answers now"
);
assert_eq!(
new_year.time_zone_offset(&app),
SignedDuration::from_hours(1),
"asked again"
);
assert_eq!(new_year.time_zone_name(&app), "CET", "asked again");
}
}