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,
Exchange,
Metals,
}
pub struct UnitedKingdom;
impl UnitedKingdom {
pub fn new(market: Market) -> Calendar {
let imp: crate::shared::Shared<dyn CalendarImpl> = match market {
Market::Settlement => shared(SettlementImpl),
Market::Exchange => shared(ExchangeImpl),
Market::Metals => shared(MetalsImpl),
};
Calendar::from_impl(imp)
}
}
fn is_bank_holiday(d: i32, w: Weekday, m: Month, y: i32) -> bool {
(d <= 7 && w == Weekday::Monday && m == Month::May && y != 1995 && y != 2020)
|| (d == 8 && m == Month::May && (y == 1995 || y == 2020))
|| (d >= 25 && w == Weekday::Monday && m == Month::May && y != 2002 && y != 2012 && y != 2022)
|| ((d == 3 || d == 4) && m == Month::June && y == 2002)
|| ((d == 4 || d == 5) && m == Month::June && y == 2012)
|| ((d == 2 || d == 3) && m == Month::June && y == 2022)
|| (d >= 25 && w == Weekday::Monday && m == Month::August)
|| (d == 29 && m == Month::April && y == 2011)
|| (d == 19 && m == Month::September && y == 2022)
|| (d == 8 && m == Month::May && y == 2023)
}
fn uk_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)
|| ((d == 1 || ((d == 2 || d == 3) && w == Weekday::Monday)) && m == Month::January)
|| (dd == em - 3)
|| (dd == em)
|| is_bank_holiday(d, w, m, y)
|| ((d == 25 || (d == 27 && (w == Weekday::Monday || w == Weekday::Tuesday)))
&& m == Month::December)
|| ((d == 26 || (d == 28 && (w == Weekday::Monday || w == Weekday::Tuesday)))
&& m == Month::December)
|| (d == 31 && m == Month::December && y == 1999))
}
struct SettlementImpl;
struct ExchangeImpl;
struct MetalsImpl;
impl CalendarImpl for SettlementImpl {
fn name(&self) -> String {
"UK settlement".to_string()
}
fn is_weekend(&self, w: Weekday) -> bool {
is_weekend_sat_sun(w)
}
fn is_business_day(&self, date: Date) -> bool {
uk_is_business_day(date)
}
}
impl CalendarImpl for ExchangeImpl {
fn name(&self) -> String {
"London stock exchange".to_string()
}
fn is_weekend(&self, w: Weekday) -> bool {
is_weekend_sat_sun(w)
}
fn is_business_day(&self, date: Date) -> bool {
uk_is_business_day(date)
}
}
impl CalendarImpl for MetalsImpl {
fn name(&self) -> String {
"London metals exchange".to_string()
}
fn is_weekend(&self, w: Weekday) -> bool {
is_weekend_sat_sun(w)
}
fn is_business_day(&self, date: Date) -> bool {
uk_is_business_day(date)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn names_match_quantlib() {
assert_eq!(
UnitedKingdom::new(Market::Settlement).name(),
"UK settlement"
);
assert_eq!(
UnitedKingdom::new(Market::Exchange).name(),
"London stock exchange"
);
assert_eq!(
UnitedKingdom::new(Market::Metals).name(),
"London metals exchange"
);
}
#[test]
fn fixed_holidays() {
let c = UnitedKingdom::new(Market::Settlement);
assert!(c.is_holiday(Date::new(1, Month::January, 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 = UnitedKingdom::new(Market::Settlement);
assert!(c.is_weekend(Weekday::Saturday));
assert!(c.is_weekend(Weekday::Sunday));
}
}