use crate::shared::shared;
use crate::time::calendar::{Calendar, CalendarImpl, is_weekend_sat_sun, orthodox_easter_monday};
use crate::time::date::{Date, Month};
use crate::time::weekday::Weekday;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Market {
Public,
Bvb,
}
pub struct Romania;
impl Romania {
pub fn new(market: Market) -> Calendar {
let imp: crate::shared::Shared<dyn CalendarImpl> = match market {
Market::Public => shared(PublicImpl),
Market::Bvb => shared(BvbImpl),
};
Calendar::from_impl(imp)
}
}
fn public_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 = orthodox_easter_monday(y);
!(is_weekend_sat_sun(w)
|| (d == 1 && m == Month::January)
|| (d == 2 && m == Month::January)
|| (d == 24 && m == Month::January)
|| (dd == em)
|| (d == 1 && m == Month::May)
|| (dd == em + 49)
|| (d == 1 && m == Month::June && y >= 2017)
|| (d == 15 && m == Month::August)
|| (d == 30 && m == Month::November)
|| (d == 1 && m == Month::December)
|| (d == 25 && m == Month::December)
|| (d == 26 && m == Month::December))
}
struct PublicImpl;
impl CalendarImpl for PublicImpl {
fn name(&self) -> String {
"Romania".to_string()
}
fn is_weekend(&self, w: Weekday) -> bool {
is_weekend_sat_sun(w)
}
fn is_business_day(&self, date: Date) -> bool {
public_is_business_day(date)
}
}
struct BvbImpl;
impl CalendarImpl for BvbImpl {
fn name(&self) -> String {
"Bucharest stock exchange".to_string()
}
fn is_weekend(&self, w: Weekday) -> bool {
is_weekend_sat_sun(w)
}
fn is_business_day(&self, date: Date) -> bool {
if !public_is_business_day(date) {
return false;
}
let d = date.day_of_month();
let m = date.month();
let y = date.year();
if m == Month::December && y == 2014 && (d == 24 || d == 31) {
return false;
}
true
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn names_match_quantlib() {
assert_eq!(Romania::new(Market::Public).name(), "Romania");
assert_eq!(Romania::new(Market::Bvb).name(), "Bucharest stock exchange");
}
#[test]
fn fixed_holidays() {
let c = Romania::new(Market::Public);
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(24, Month::January, 2019)));
assert!(c.is_holiday(Date::new(1, Month::May, 2019)));
assert!(c.is_holiday(Date::new(15, Month::August, 2019)));
assert!(c.is_holiday(Date::new(30, Month::November, 2019)));
assert!(c.is_holiday(Date::new(1, Month::December, 2019)));
assert!(c.is_holiday(Date::new(25, Month::December, 2019)));
assert!(c.is_holiday(Date::new(26, Month::December, 2019)));
}
#[test]
fn bvb_one_off_closings() {
let c = Romania::new(Market::Bvb);
assert!(c.is_holiday(Date::new(24, Month::December, 2014)));
assert!(c.is_holiday(Date::new(31, Month::December, 2014)));
}
#[test]
fn weekend_rule() {
let c = Romania::new(Market::Bvb);
assert!(c.is_weekend(Weekday::Saturday));
assert!(c.is_weekend(Weekday::Sunday));
}
}