use chrono::NaiveDate;
use super::DayPolicy;
const SECONDS_PER_DAY: u32 = 86_400;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct DayOverride {
trade_date: NaiveDate,
kind: DayOverrideKind,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum DayOverrideKind {
Closed,
Modified {
late_open_ssm: Option<u32>,
early_close_ssm: Option<u32>,
},
}
impl DayOverride {
#[must_use]
pub const fn closed(trade_date: NaiveDate) -> Self {
Self {
trade_date,
kind: DayOverrideKind::Closed,
}
}
#[must_use]
pub const fn early_close(trade_date: NaiveDate, early_close_ssm: u32) -> Self {
Self {
trade_date,
kind: DayOverrideKind::Modified {
late_open_ssm: None,
early_close_ssm: Some(early_close_ssm),
},
}
}
#[must_use]
pub const fn late_open(trade_date: NaiveDate, late_open_ssm: u32) -> Self {
Self {
trade_date,
kind: DayOverrideKind::Modified {
late_open_ssm: Some(late_open_ssm),
early_close_ssm: None,
},
}
}
#[must_use]
pub const fn late_open_and_early_close(
trade_date: NaiveDate,
late_open_ssm: u32,
early_close_ssm: u32,
) -> Self {
Self {
trade_date,
kind: DayOverrideKind::Modified {
late_open_ssm: Some(late_open_ssm),
early_close_ssm: Some(early_close_ssm),
},
}
}
#[must_use]
pub const fn trade_date(self) -> NaiveDate {
self.trade_date
}
#[must_use]
pub const fn is_closed(self) -> bool {
matches!(self.kind, DayOverrideKind::Closed)
}
#[must_use]
pub const fn early_close_ssm(self) -> Option<u32> {
match self.kind {
DayOverrideKind::Closed => None,
DayOverrideKind::Modified {
early_close_ssm, ..
} => early_close_ssm,
}
}
#[must_use]
pub const fn late_open_ssm(self) -> Option<u32> {
match self.kind {
DayOverrideKind::Closed => None,
DayOverrideKind::Modified { late_open_ssm, .. } => late_open_ssm,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct StaticDayPolicy<'a> {
overrides: &'a [DayOverride],
}
impl<'a> StaticDayPolicy<'a> {
pub const fn new(overrides: &'a [DayOverride]) -> Result<Self, StaticDayPolicyError> {
let mut index = 0;
while index < overrides.len() {
let current = overrides[index];
if index > 0
&& current
.trade_date
.signed_duration_since(overrides[index - 1].trade_date)
.num_days()
<= 0
{
return Err(StaticDayPolicyError::DatesNotStrictlyIncreasing { index });
}
if let Some(early_close_ssm) = current.early_close_ssm()
&& early_close_ssm > SECONDS_PER_DAY
{
return Err(StaticDayPolicyError::EarlyCloseOutOfRange {
index,
early_close_ssm,
});
}
if let Some(late_open_ssm) = current.late_open_ssm()
&& late_open_ssm >= SECONDS_PER_DAY
{
return Err(StaticDayPolicyError::LateOpenOutOfRange {
index,
late_open_ssm,
});
}
index += 1;
}
Ok(Self { overrides })
}
#[must_use]
pub fn override_on(self, trade_date: NaiveDate) -> Option<DayOverride> {
self.overrides
.binary_search_by_key(&trade_date, |record| record.trade_date)
.ok()
.and_then(|index| self.overrides.get(index).copied())
}
#[must_use]
pub const fn overrides(self) -> &'a [DayOverride] {
self.overrides
}
}
impl DayPolicy for StaticDayPolicy<'_> {
fn is_closed(&self, trade_date: NaiveDate) -> bool {
self.override_on(trade_date)
.is_some_and(DayOverride::is_closed)
}
fn early_close_ssm(&self, trade_date: NaiveDate) -> Option<u32> {
self.override_on(trade_date)
.and_then(DayOverride::early_close_ssm)
}
fn late_open_ssm(&self, trade_date: NaiveDate) -> Option<u32> {
self.override_on(trade_date)
.and_then(DayOverride::late_open_ssm)
}
fn may_affect(&self, first: NaiveDate, last: NaiveDate) -> bool {
let start = self
.overrides
.partition_point(|record| record.trade_date < first);
self.overrides
.get(start)
.is_some_and(|record| record.trade_date <= last)
}
}
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum StaticDayPolicyError {
DatesNotStrictlyIncreasing {
index: usize,
},
EarlyCloseOutOfRange {
index: usize,
early_close_ssm: u32,
},
LateOpenOutOfRange {
index: usize,
late_open_ssm: u32,
},
}
impl core::fmt::Display for StaticDayPolicyError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Self::DatesNotStrictlyIncreasing { index } => {
write!(
f,
"override at index {index} is not later than its predecessor"
)
}
Self::EarlyCloseOutOfRange {
index,
early_close_ssm,
} => write!(
f,
"early_close_ssm {early_close_ssm} at index {index} is outside 0..=86400"
),
Self::LateOpenOutOfRange {
index,
late_open_ssm,
} => write!(
f,
"late_open_ssm {late_open_ssm} at index {index} is outside 0..86400"
),
}
}
}
impl std::error::Error for StaticDayPolicyError {}