exchange-hours 1.2.2

Exchange trading hours, session boundaries, and calendar-aware bar boundaries. UTC in, UTC out.
Documentation
// SPDX-License-Identifier: MIT-0

//! Korea Exchange cash equities.

use chrono_tz::Asia;

use super::super::StaticHoursProfile;
use crate::calendar::SessionRule;
use crate::calendar::rule::MON_FRI;

static KRX_REGULAR_CURRENT: &[SessionRule] = &[SessionRule {
    days: MON_FRI,
    open_ssm: 9 * 3600,
    close_ssm: 15 * 3600 + 20 * 60,
}];
static KRX_REGULAR_PRE_2016: &[SessionRule] = &[SessionRule {
    days: MON_FRI,
    open_ssm: 9 * 3600,
    close_ssm: 14 * 3600 + 50 * 60,
}];
static KRX_EXTENDED_CURRENT: &[SessionRule] = &[
    SessionRule {
        days: MON_FRI,
        open_ssm: 8 * 3600,
        close_ssm: 9 * 3600,
    },
    SessionRule {
        days: MON_FRI,
        open_ssm: 15 * 3600 + 20 * 60,
        close_ssm: 18 * 3600,
    },
];
static KRX_EXTENDED_POST_2016: &[SessionRule] = &[
    SessionRule {
        days: MON_FRI,
        open_ssm: 7 * 3600 + 30 * 60,
        close_ssm: 9 * 3600,
    },
    KRX_EXTENDED_CURRENT[1],
];
static KRX_EXTENDED_PRE_2016: &[SessionRule] = &[
    KRX_EXTENDED_POST_2016[0],
    SessionRule {
        days: MON_FRI,
        open_ssm: 14 * 3600 + 50 * 60,
        close_ssm: 18 * 3600,
    },
];

// KRX continuous trading is 09:00–15:20, followed by a ten-minute closing
// call and order/trading phases through 18:00. Opening-call orders begin 08:30,
// while executable off-hours block and basket trading begins at 08:00.
// https://global.krx.co.kr/contents/GLB/06/0602/0602020204/GLB0602020204T1.jsp
pub(crate) static KRX_PROFILE_CURRENT: StaticHoursProfile = StaticHoursProfile {
    tz: Asia::Seoul,
    regular: KRX_REGULAR_CURRENT,
    extended: KRX_EXTENDED_CURRENT,
    order_entry: &[],
    has_daily_close: true,
    has_weekend_close: true,
};

// The regular close extended by 30 minutes on 2016-08-01. Narrative:
// docs/evidence/krx.md.
pub(crate) static KRX_PROFILE_POST_2016_08_01: StaticHoursProfile = StaticHoursProfile {
    tz: Asia::Seoul,
    regular: KRX_REGULAR_CURRENT,
    extended: KRX_EXTENDED_POST_2016,
    order_entry: &[],
    has_daily_close: true,
    has_weekend_close: true,
};
pub(crate) static KRX_PROFILE_PRE_2016_08_01: StaticHoursProfile = StaticHoursProfile {
    tz: Asia::Seoul,
    regular: KRX_REGULAR_PRE_2016,
    extended: KRX_EXTENDED_PRE_2016,
    order_entry: &[],
    has_daily_close: true,
    has_weekend_close: true,
};

use crate::calendar::schedules::timeline::{Revision, local_date, revisions, select_revision};

pub(crate) const CURRENT: &StaticHoursProfile = &KRX_PROFILE_CURRENT;

// Evidence: docs/evidence/krx.md
static REVISIONS: &[Revision] = revisions![
    // The 2016-08-01 label names no document on purpose: FSC notice 73613 was
    // read on 2026-09-12 and dates the 2019-04-29 pre-market reduction, not
    // this row. No artifact in this source set dates the 2016 extension; the
    // defect is recorded in docs/evidence/krx.md. The day and the rules are
    // unchanged pending a re-sourcing change.
    (
        2016,
        8,
        1,
        &KRX_PROFILE_POST_2016_08_01,
        "2016-08-01 KRX regular-close extension, artifact unrecovered"
    ),
    (2019, 4, 29, &KRX_PROFILE_CURRENT, "KRX rulebook law 000111"),
];

pub(crate) fn profile_at(as_of: chrono::DateTime<chrono::Utc>) -> &'static StaticHoursProfile {
    select_revision(
        local_date(as_of, CURRENT.tz),
        &KRX_PROFILE_PRE_2016_08_01,
        REVISIONS,
    )
}