use crate::shared::shared;
use crate::time::calendar::{Calendar, CalendarImpl, is_weekend_sat_sun};
use crate::time::calendars::islamicholidays::moon_sighting::{is_eid_al_adha, is_eid_al_fitr};
use crate::time::date::{Date, Month, Year};
use crate::time::weekday::Weekday;
const HOLIDAY_HORIZON: Year = 2040;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Market {
Uzse,
}
pub struct Uzbekistan;
impl Uzbekistan {
pub fn new(market: Market) -> Calendar {
let imp: crate::shared::Shared<dyn CalendarImpl> = match market {
Market::Uzse => shared(Impl),
};
Calendar::from_impl(imp)
}
}
struct Impl;
impl CalendarImpl for Impl {
fn name(&self) -> String {
"Uzbekistan 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 w = date.weekday();
let d = date.day_of_month();
let m = date.month();
let y = date.year();
assert!(
y <= HOLIDAY_HORIZON,
"Uzbekistan Islamic (Eid) holidays are tabulated only through {HOLIDAY_HORIZON} \
(matching QuantLib); year {y} is beyond the supported horizon"
);
!(is_weekend_sat_sun(w)
|| is_eid_al_fitr(date)
|| is_eid_al_adha(date)
|| (d == 1 && m == Month::January)
|| (d == 8 && m == Month::March)
|| (d == 21 && m == Month::March)
|| (d == 9 && m == Month::May)
|| (d == 1 && m == Month::September)
|| (d == 1 && m == Month::October)
|| (d == 8 && m == Month::December))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn name_matches_quantlib() {
assert_eq!(
Uzbekistan::new(Market::Uzse).name(),
"Uzbekistan Stock Exchange"
);
}
#[test]
fn fixed_holidays() {
let c = Uzbekistan::new(Market::Uzse);
for (d, m) in [
(1, Month::January), (8, Month::March), (21, Month::March), (9, Month::May), (1, Month::September), (1, Month::October), (8, Month::December), ] {
assert!(c.is_holiday(Date::new(d, m, 2020)), "{d} {m} 2020");
}
}
#[test]
fn weekend_rule() {
let c = Uzbekistan::new(Market::Uzse);
assert!(c.is_weekend(Weekday::Saturday));
assert!(c.is_weekend(Weekday::Sunday));
assert!(!c.is_weekend(Weekday::Friday));
}
#[test]
fn in_horizon_query_works() {
let c = Uzbekistan::new(Market::Uzse);
assert!(c.is_holiday(Date::new(1, Month::January, HOLIDAY_HORIZON)));
}
#[test]
#[should_panic(expected = "beyond the supported horizon")]
fn beyond_horizon_panics() {
let c = Uzbekistan::new(Market::Uzse);
let _ = c.is_business_day(Date::new(1, Month::January, HOLIDAY_HORIZON + 1));
}
}