use std::borrow::Cow;
use chrono::{DateTime, NaiveDate, Utc};
use chrono_tz::{America, Europe, Tz, US};
use crate::calendar::profiles::{
BLUE_OCEAN_PROFILE, C1_PROFILE_POST_2024_08_26, C1_PROFILE_PRE_2024_08_26,
CBOT_PROFILE_POST2013, CBOT_PROFILE_PRE2013, CFE_PROFILE, CFE_PROFILE_PRE_2021_12_06,
CFE_PROFILE_PRE2014, CME_PROFILE_POST2016, CME_PROFILE_PRE2016, EUREX_PROFILE_NO_ASIAN,
EUREX_PROFILE_WITH_ASIAN, IEX_PROFILE_POST2015, IEX_PROFILE_PRE2015, NYSE_TEXAS_PROFILE,
US_EQUITIES_PROFILE, US_EQUITY_EARLY_0700_PROFILE, from_profile,
};
use crate::calendar::{Exchange, MarketHours};
use super::hours_for_exchange;
#[expect(
clippy::panic,
reason = "const-eval only: this arm is how an invalid date literal becomes a \
build failure, and a `const` item cannot reach it at runtime"
)]
const fn cutover(year: i32, month: u32, day: u32) -> NaiveDate {
match NaiveDate::from_ymd_opt(year, month, day) {
Some(date) => date,
None => panic!("invalid hard-coded cutover date"),
}
}
fn is_before(as_of: DateTime<Utc>, tz: Tz, cutover: NaiveDate) -> bool {
as_of.with_timezone(&tz).date_naive() < cutover
}
fn no_sessions(exchange: Exchange, tz: Tz) -> MarketHours {
MarketHours {
exchange,
tz,
regular: Cow::Borrowed(&[]),
extended: Cow::Borrowed(&[]),
has_daily_close: true,
has_weekend_close: true,
}
}
const C1_GTH_CUTOVER: NaiveDate = cutover(2024, 8, 26);
const CME_CUTOVER: NaiveDate = cutover(2016, 3, 4);
const EUREX_ASIAN_CUTOVER: NaiveDate = cutover(2018, 12, 10);
const IEX_EXTENDED_CUTOVER: NaiveDate = cutover(2015, 8, 21);
const CFE_ETH_CUTOVER: NaiveDate = cutover(2014, 6, 1);
const CFE_QUEUING_CUTOVER: NaiveDate = cutover(2021, 12, 6);
const NYSE_TEXAS_GO_LIVE: NaiveDate = cutover(2025, 3, 31);
const BLUE_OCEAN_GO_LIVE: NaiveDate = cutover(2021, 10, 5);
const CBOT_CUTOVER: NaiveDate = cutover(2013, 4, 8);
const EDGX_EARLY_CUTOVER: NaiveDate = cutover(2021, 3, 8);
const BZX_EARLY_CUTOVER: NaiveDate = cutover(2025, 5, 1);
#[must_use]
pub fn hours_for_exchange_as_of(exch: Exchange, as_of: DateTime<Utc>) -> MarketHours {
match exch {
Exchange::CboeEdgx => {
if is_before(as_of, America::New_York, EDGX_EARLY_CUTOVER) {
from_profile(Exchange::CboeEdgx, &US_EQUITY_EARLY_0700_PROFILE)
} else {
from_profile(Exchange::CboeEdgx, &US_EQUITIES_PROFILE)
}
}
Exchange::CboeBzx => {
if is_before(as_of, America::New_York, BZX_EARLY_CUTOVER) {
from_profile(Exchange::CboeBzx, &US_EQUITY_EARLY_0700_PROFILE)
} else {
from_profile(Exchange::CboeBzx, &US_EQUITIES_PROFILE)
}
}
Exchange::CboeOptionsC1 => {
if is_before(as_of, America::New_York, C1_GTH_CUTOVER) {
from_profile(Exchange::CboeOptionsC1, &C1_PROFILE_PRE_2024_08_26)
} else {
from_profile(Exchange::CboeOptionsC1, &C1_PROFILE_POST_2024_08_26)
}
}
Exchange::Cme => {
if is_before(as_of, US::Central, CME_CUTOVER) {
from_profile(Exchange::Cme, &CME_PROFILE_PRE2016)
} else {
from_profile(Exchange::Cme, &CME_PROFILE_POST2016)
}
}
Exchange::Eurex => {
if is_before(as_of, Europe::Berlin, EUREX_ASIAN_CUTOVER) {
from_profile(Exchange::Eurex, &EUREX_PROFILE_NO_ASIAN)
} else {
from_profile(Exchange::Eurex, &EUREX_PROFILE_WITH_ASIAN)
}
}
Exchange::Iex => {
if is_before(as_of, America::New_York, IEX_EXTENDED_CUTOVER) {
from_profile(Exchange::Iex, &IEX_PROFILE_PRE2015)
} else {
from_profile(Exchange::Iex, &IEX_PROFILE_POST2015)
}
}
Exchange::Cfe => {
if is_before(as_of, US::Central, CFE_ETH_CUTOVER) {
from_profile(Exchange::Cfe, &CFE_PROFILE_PRE2014)
} else if is_before(as_of, US::Central, CFE_QUEUING_CUTOVER) {
from_profile(Exchange::Cfe, &CFE_PROFILE_PRE_2021_12_06)
} else {
from_profile(Exchange::Cfe, &CFE_PROFILE)
}
}
Exchange::NyseTexas => {
if is_before(as_of, America::New_York, NYSE_TEXAS_GO_LIVE) {
no_sessions(Exchange::NyseTexas, America::New_York)
} else {
from_profile(Exchange::NyseTexas, &NYSE_TEXAS_PROFILE)
}
}
Exchange::BlueOceanAts => {
if is_before(as_of, America::New_York, BLUE_OCEAN_GO_LIVE) {
no_sessions(Exchange::BlueOceanAts, America::New_York)
} else {
from_profile(Exchange::BlueOceanAts, &BLUE_OCEAN_PROFILE)
}
}
Exchange::Cbot => {
if is_before(as_of, US::Central, CBOT_CUTOVER) {
from_profile(Exchange::Cbot, &CBOT_PROFILE_PRE2013)
} else {
from_profile(Exchange::Cbot, &CBOT_PROFILE_POST2013)
}
}
#[expect(
clippy::match_same_arms,
reason = "identical to the default arm today, but kept separate as the \
documented marker for where the sourced cutovers land"
)]
Exchange::MemxEq | Exchange::MiaxPearlEq => hours_for_exchange(exch),
_ => hours_for_exchange(exch),
}
}