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;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Market {
Settlement,
Wse,
}
pub struct Poland;
impl Poland {
pub fn new(market: Market) -> Calendar {
let imp: crate::shared::Shared<dyn CalendarImpl> = match market {
Market::Settlement => shared(SettlementImpl),
Market::Wse => shared(WseImpl),
};
Calendar::from_impl(imp)
}
}
fn settlement_is_business_day(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)
|| (dd == em)
|| (dd == em + 59)
|| (d == 1 && m == Month::January)
|| (d == 6 && m == Month::January && y >= 2011)
|| (d == 1 && m == Month::May)
|| (d == 3 && m == Month::May)
|| (d == 15 && m == Month::August)
|| (d == 1 && m == Month::November)
|| (d == 11 && m == Month::November)
|| (d == 25 && m == Month::December)
|| (d == 26 && m == Month::December))
}
struct SettlementImpl;
impl CalendarImpl for SettlementImpl {
fn name(&self) -> String {
"Poland Settlement".to_string()
}
fn is_weekend(&self, w: Weekday) -> bool {
is_weekend_sat_sun(w)
}
fn is_business_day(&self, date: Date) -> bool {
settlement_is_business_day(date)
}
}
struct WseImpl;
impl CalendarImpl for WseImpl {
fn name(&self) -> String {
"Warsaw stock exchange".to_string()
}
fn is_weekend(&self, w: Weekday) -> bool {
is_weekend_sat_sun(w)
}
fn is_business_day(&self, date: Date) -> bool {
let d = date.day_of_month();
let m = date.month();
if m == Month::December && (d == 24 || d == 31) {
return false;
}
settlement_is_business_day(date)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn names_match_quantlib() {
assert_eq!(Poland::new(Market::Settlement).name(), "Poland Settlement");
assert_eq!(Poland::new(Market::Wse).name(), "Warsaw stock exchange");
}
#[test]
fn fixed_holidays() {
let c = Poland::new(Market::Settlement);
assert!(c.is_holiday(Date::new(1, Month::January, 2019)));
assert!(c.is_holiday(Date::new(1, Month::May, 2019)));
assert!(c.is_holiday(Date::new(3, Month::May, 2019)));
assert!(c.is_holiday(Date::new(15, Month::August, 2019)));
assert!(c.is_holiday(Date::new(1, Month::November, 2019)));
assert!(c.is_holiday(Date::new(11, Month::November, 2019)));
assert!(c.is_holiday(Date::new(25, Month::December, 2019)));
assert!(c.is_holiday(Date::new(26, Month::December, 2019)));
}
#[test]
fn wse_extra_holidays() {
let c = Poland::new(Market::Wse);
assert!(c.is_holiday(Date::new(24, Month::December, 2019)));
assert!(c.is_holiday(Date::new(31, Month::December, 2019)));
}
#[test]
fn weekend_rule() {
let c = Poland::new(Market::Settlement);
assert!(c.is_weekend(Weekday::Saturday));
assert!(c.is_weekend(Weekday::Sunday));
}
}