use std::borrow::Cow;
use chrono::{DateTime, NaiveDate, Utc};
use chrono_tz::Tz;
use super::exchange_calendar::CalendarSource;
use super::query::gate::answered;
use super::query::{QueryContext, status, week};
use super::{Exchange, SessionKind, SessionRule, SessionState};
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MarketHours {
pub source: CalendarSource,
pub tz: Tz,
pub regular: Cow<'static, [SessionRule]>,
pub extended: Cow<'static, [SessionRule]>,
pub order_entry: Cow<'static, [SessionRule]>,
pub has_daily_close: bool,
pub has_weekend_close: bool,
}
impl MarketHours {
#[must_use]
pub fn new(
source: impl Into<CalendarSource>,
tz: Tz,
regular: Cow<'static, [SessionRule]>,
extended: Cow<'static, [SessionRule]>,
order_entry: Cow<'static, [SessionRule]>,
has_daily_close: bool,
has_weekend_close: bool,
) -> Self {
Self {
source: source.into(),
tz,
regular,
extended,
order_entry,
has_daily_close,
has_weekend_close,
}
}
#[must_use]
pub fn is_accepting_orders(&self, t: DateTime<Utc>) -> bool {
self.is_open(t) || self.is_order_entry_only(t)
}
#[must_use]
pub fn is_order_entry_only(&self, t: DateTime<Utc>) -> bool {
!self.is_open(t)
&& self
.order_entry_view()
.is_open_with(t, SessionKind::Regular)
}
fn order_entry_view(&self) -> Self {
Self {
source: self.source,
tz: self.tz,
regular: self.order_entry.clone(),
extended: Cow::Borrowed(&[]),
order_entry: Cow::Borrowed(&[]),
has_daily_close: self.has_daily_close,
has_weekend_close: self.has_weekend_close,
}
}
#[must_use]
pub const fn source(&self) -> CalendarSource {
self.source
}
#[must_use]
pub const fn exchange(&self) -> Option<Exchange> {
match self.source {
CalendarSource::Exchange(exchange) => Some(exchange),
CalendarSource::MarketHoursKey(_) => None,
}
}
#[must_use]
pub const fn market_hours_key(&self) -> Option<crate::MarketHoursKey> {
match self.source {
CalendarSource::MarketHoursKey(key) => Some(key),
CalendarSource::Exchange(_) => None,
}
}
#[must_use]
pub fn normal_week_open_seconds(&self) -> u64 {
week::fixed_normal_week_open_seconds(self)
}
#[must_use]
pub fn is_open(&self, t: DateTime<Utc>) -> bool {
self.is_open_with(t, SessionKind::Both)
}
#[must_use]
pub fn is_open_with(&self, t: DateTime<Utc>, kind: SessionKind) -> bool {
answered(status::is_open_with(&QueryContext::fixed(self), t, kind))
}
#[must_use]
pub fn is_open_regular(&self, t: DateTime<Utc>) -> bool {
self.is_open_with(t, SessionKind::Regular)
}
#[must_use]
pub fn is_open_extended(&self, t: DateTime<Utc>) -> bool {
self.is_open_with(t, SessionKind::Extended)
}
#[must_use]
pub fn is_maintenance(&self, t: DateTime<Utc>) -> bool {
answered(status::is_maintenance(&QueryContext::fixed(self), t))
}
#[must_use]
pub fn session_state(&self, t: DateTime<Utc>) -> SessionState {
answered(status::session_state(&QueryContext::fixed(self), t))
}
#[must_use]
pub fn is_closed_all_day_in_calendar(
&self,
day: NaiveDate,
calendar_tz: Tz,
kind: SessionKind,
) -> bool {
answered(status::is_closed_all_day_in_calendar(
&QueryContext::fixed(self),
day,
calendar_tz,
kind,
))
}
#[inline]
#[must_use]
pub fn is_closed_all_day_on(&self, local_day: NaiveDate, kind: SessionKind) -> bool {
self.is_closed_all_day_in_calendar(local_day, self.tz, kind)
}
#[inline]
#[must_use]
pub fn is_closed_all_day_at(
&self,
ts_utc: DateTime<Utc>,
calendar_tz: Tz,
kind: SessionKind,
) -> bool {
answered(status::is_closed_all_day_at(
&QueryContext::fixed(self),
ts_utc,
calendar_tz,
kind,
))
}
}