use crate::shared::shared;
use crate::time::calendar::{Calendar, CalendarImpl, is_weekend_sat_sun, western_easter_monday};
use crate::time::date::{Date, Month};
use crate::time::weekday::Weekday;
pub struct Switzerland;
impl Switzerland {
pub fn new() -> Calendar {
Calendar::from_impl(shared(Impl))
}
}
struct Impl;
impl CalendarImpl for Impl {
fn name(&self) -> String {
"Switzerland".to_string()
}
fn is_weekend(&self, w: Weekday) -> bool {
is_weekend_sat_sun(w)
}
fn is_business_day(&self, date: Date) -> bool {
let w = date.weekday();
let d = date.day_of_month();
let dd = date.day_of_year();
let m = date.month();
let y = date.year();
let em = western_easter_monday(y);
!(is_weekend_sat_sun(w)
|| (d == 1 && m == Month::January)
|| (d == 2 && m == Month::January)
|| (dd == em - 3)
|| (dd == em)
|| (dd == em + 38)
|| (dd == em + 49)
|| (d == 1 && m == Month::May)
|| (d == 1 && m == Month::August)
|| (d == 25 && m == Month::December)
|| (d == 26 && m == Month::December))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn name_matches_quantlib() {
assert_eq!(Switzerland::new().name(), "Switzerland");
}
#[test]
fn fixed_holidays() {
let c = Switzerland::new();
assert!(c.is_holiday(Date::new(1, Month::January, 2019)));
assert!(c.is_holiday(Date::new(2, Month::January, 2019)));
assert!(c.is_holiday(Date::new(1, Month::May, 2019)));
assert!(c.is_holiday(Date::new(1, Month::August, 2019)));
assert!(c.is_holiday(Date::new(25, Month::December, 2019)));
assert!(c.is_holiday(Date::new(26, Month::December, 2019)));
}
#[test]
fn weekend_rule() {
let c = Switzerland::new();
assert!(c.is_weekend(Weekday::Saturday));
assert!(c.is_weekend(Weekday::Sunday));
}
}