exchange-hours 1.2.2

Exchange trading hours, session boundaries, and calendar-aware bar boundaries. UTC in, UTC out.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
// SPDX-License-Identifier: MIT-0

//! Allocation-free exception providers backed by caller-owned static records.

use chrono::NaiveDate;

use super::{
    BlockViolation, DateException, ExceptionBlock, ExceptionCoverage, SessionExceptionSource,
    first_block_violation,
};
use crate::calendar::exchange_calendar::CalendarSource;

/// One caller-supplied record for a venue-local trade date.
///
/// The three constructors are mutually exclusive by construction: a record
/// either states that the date is normal, that it carries no session at all, or
/// that a complete ordered block set replaces it. Nothing can be closed and
/// replaced at once, and no record carries scalar boundary edits — those belong
/// to [`DayPolicy`](crate::DayPolicy), which overlays this layer.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct SessionExceptionRecord<'a> {
    trade_date: NaiveDate,
    state: RecordState<'a>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum RecordState<'a> {
    KnownNormal,
    Closed,
    ReplaceSessions(&'a [ExceptionBlock]),
}

impl<'a> SessionExceptionRecord<'a> {
    /// Records that `trade_date` was audited and follows the normal week.
    ///
    /// Explicit normal records are optional: every uncovered-by-a-record date
    /// inside a table's coverage window already reads as
    /// [`DateException::KnownNormal`]. They document an audit that found
    /// nothing.
    #[must_use]
    pub const fn known_normal(trade_date: NaiveDate) -> Self {
        Self {
            trade_date,
            state: RecordState::KnownNormal,
        }
    }

    /// Records that no session belongs to `trade_date`.
    #[must_use]
    pub const fn closed(trade_date: NaiveDate) -> Self {
        Self {
            trade_date,
            state: RecordState::Closed,
        }
    }

    /// Records the complete ordered block set that replaces `trade_date`.
    ///
    /// The slice is validated by [`StaticSessionExceptions::new`]: it must be
    /// non-empty, ordered by opening day then open time, and every block must
    /// hold the [`ExceptionBlock`] domain invariants.
    #[must_use]
    pub const fn replace_sessions(trade_date: NaiveDate, blocks: &'a [ExceptionBlock]) -> Self {
        Self {
            trade_date,
            state: RecordState::ReplaceSessions(blocks),
        }
    }

    /// Returns the venue-local trade date this record describes.
    #[must_use]
    pub const fn trade_date(self) -> NaiveDate {
        self.trade_date
    }

    /// Returns this record's state as a [`DateException`].
    ///
    /// A record never reports [`DateException::OutOfCoverage`]; that state
    /// belongs to dates a table has no record *and* no coverage for.
    #[must_use]
    pub const fn exception(self) -> DateException<'a> {
        match self.state {
            RecordState::KnownNormal => DateException::KnownNormal,
            RecordState::Closed => DateException::Closed,
            RecordState::ReplaceSessions(blocks) => DateException::ReplaceSessions(blocks),
        }
    }
}

/// An allocation-free [`SessionExceptionSource`] over caller-owned records.
///
/// Records are keyed by venue-local **trade date** and are exact dates, never
/// inferred weekday or holiday rules. Construction validates the whole slice
/// once; queries then use exact-date binary search without allocation, I/O, or
/// clock reads.
///
/// The table stores no operator data of its own. It carries the caller's
/// identity scope and audited window so the engine can refuse a provider built
/// for another schedule and so an unaudited date stays distinguishable from an
/// ordinary weekday.
///
/// # Example
///
/// ```
/// use chrono::NaiveDate;
/// use exchange_hours::{
///     CalendarSource, DateException, Exchange, ExceptionBlock, SessionExceptionRecord,
///     SessionExceptionSource, StaticSessionExceptions,
/// };
///
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let day = |d| NaiveDate::from_ymd_opt(2011, 11, d).ok_or("invalid date");
///
/// // A regular-only early close: extended trading continues past 13:00 local.
/// static BLOCKS: [ExceptionBlock; 2] = [
///     ExceptionBlock::regular(0, 9 * 3_600 + 30 * 60, 13 * 3_600),
///     ExceptionBlock::extended(0, 13 * 3_600, 17 * 3_600),
/// ];
/// let records = [SessionExceptionRecord::replace_sessions(day(25)?, &BLOCKS)];
/// let table = StaticSessionExceptions::new(
///     CalendarSource::Exchange(Exchange::Nasdaq),
///     day(21)?,
///     day(28)?,
///     &records,
/// )?;
///
/// assert_eq!(table.exception_on(day(25)?), DateException::ReplaceSessions(&BLOCKS));
/// assert_eq!(table.exception_on(day(24)?), DateException::KnownNormal);
/// assert_eq!(table.exception_on(day(29)?), DateException::OutOfCoverage);
/// # Ok(())
/// # }
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct StaticSessionExceptions<'a> {
    source: CalendarSource,
    coverage: ExceptionCoverage,
    records: &'a [SessionExceptionRecord<'a>],
}

impl<'a> StaticSessionExceptions<'a> {
    /// Validates and borrows a slice of trade-date records for one identity.
    ///
    /// `first_covered` and `last_covered` publish the audited window: dates
    /// inside it with no record are [`DateException::KnownNormal`], and dates
    /// outside it are [`DateException::OutOfCoverage`]. An empty record slice
    /// is valid and asserts that the whole window was audited and found normal.
    /// This is a `const fn`, so a caller can validate while constructing a
    /// constant or static table.
    ///
    /// Validation scans left to right, checking the coverage bounds, then each
    /// record's ordering and window membership, then each replacement block's
    /// ordering and domain, and returns the first violation.
    ///
    /// # Errors
    ///
    /// Returns [`StaticSessionExceptionsError`] for inverted coverage bounds,
    /// the first duplicate or out-of-order record date, the first record
    /// outside the coverage window, an empty replacement set, or the first
    /// replacement block that is out of order, carries an offset outside
    /// [`ExceptionBlock::MIN_DAY_OFFSET`]`..=`[`ExceptionBlock::MAX_DAY_OFFSET`],
    /// opens outside `0..86_400`, or closes outside `0..=86_400`.
    pub const fn new(
        source: CalendarSource,
        first_covered: NaiveDate,
        last_covered: NaiveDate,
        records: &'a [SessionExceptionRecord<'a>],
    ) -> Result<Self, StaticSessionExceptionsError> {
        if last_covered.signed_duration_since(first_covered).num_days() < 0 {
            return Err(StaticSessionExceptionsError::CoverageBoundsInverted);
        }
        let mut index = 0;
        while index < records.len() {
            let current = records[index];
            if index > 0
                && current
                    .trade_date
                    .signed_duration_since(records[index - 1].trade_date)
                    .num_days()
                    <= 0
            {
                return Err(StaticSessionExceptionsError::DatesNotStrictlyIncreasing { index });
            }
            if current
                .trade_date
                .signed_duration_since(first_covered)
                .num_days()
                < 0
                || last_covered
                    .signed_duration_since(current.trade_date)
                    .num_days()
                    < 0
            {
                return Err(StaticSessionExceptionsError::RecordOutsideCoverage { index });
            }
            if let RecordState::ReplaceSessions(blocks) = current.state
                && let Err(error) = validate_blocks(index, blocks)
            {
                return Err(error);
            }
            index += 1;
        }
        Ok(Self {
            source,
            coverage: ExceptionCoverage {
                first: first_covered,
                last: last_covered,
            },
            records,
        })
    }

    /// Returns the identity these records are scoped to.
    #[must_use]
    pub const fn source(self) -> CalendarSource {
        self.source
    }

    /// Returns the audited trade-date window.
    #[must_use]
    pub const fn coverage(self) -> ExceptionCoverage {
        self.coverage
    }

    /// Returns the exact record for `trade_date`, if one exists.
    ///
    /// Lookup is allocation-free and logarithmic in the number of records. A
    /// date outside the coverage window still returns its record if one was
    /// supplied, which validation makes impossible.
    #[must_use]
    pub fn record_on(self, trade_date: NaiveDate) -> Option<SessionExceptionRecord<'a>> {
        self.records
            .binary_search_by_key(&trade_date, |record| record.trade_date)
            .ok()
            .and_then(|index| self.records.get(index).copied())
    }

    /// Returns what this table knows about `trade_date`.
    #[must_use]
    pub fn exception_on(self, trade_date: NaiveDate) -> DateException<'a> {
        if !self.coverage.contains(trade_date) {
            return DateException::OutOfCoverage;
        }
        self.record_on(trade_date).map_or(
            DateException::KnownNormal,
            SessionExceptionRecord::exception,
        )
    }

    /// Returns the validated records in their strictly increasing order.
    #[must_use]
    pub const fn records(self) -> &'a [SessionExceptionRecord<'a>] {
        self.records
    }
}

const fn validate_blocks(
    index: usize,
    blocks: &[ExceptionBlock],
) -> Result<(), StaticSessionExceptionsError> {
    match first_block_violation(blocks) {
        None => Ok(()),
        Some(BlockViolation::Empty) => {
            Err(StaticSessionExceptionsError::EmptyReplacement { index })
        }
        Some(BlockViolation::OffsetOutOfRange {
            block,
            open_day_offset,
        }) => Err(StaticSessionExceptionsError::BlockOffsetOutOfRange {
            index,
            block,
            open_day_offset,
        }),
        Some(BlockViolation::OpenOutOfRange { block, open_ssm }) => {
            Err(StaticSessionExceptionsError::BlockOpenOutOfRange {
                index,
                block,
                open_ssm,
            })
        }
        Some(BlockViolation::CloseOutOfRange { block, close_ssm }) => {
            Err(StaticSessionExceptionsError::BlockCloseOutOfRange {
                index,
                block,
                close_ssm,
            })
        }
        Some(BlockViolation::ClosesAfterTradeDate { block }) => {
            Err(StaticSessionExceptionsError::BlockClosesAfterTradeDate { index, block })
        }
        Some(BlockViolation::NotOrdered { block }) => {
            Err(StaticSessionExceptionsError::BlocksNotOrdered { index, block })
        }
    }
}

impl SessionExceptionSource for StaticSessionExceptions<'_> {
    fn source(&self) -> CalendarSource {
        StaticSessionExceptions::source(*self)
    }

    fn coverage(&self) -> Option<ExceptionCoverage> {
        Some(StaticSessionExceptions::coverage(*self))
    }

    fn exception_on(&self, trade_date: NaiveDate) -> DateException<'_> {
        StaticSessionExceptions::exception_on(*self, trade_date)
    }

    /// One partition point over the validated, sorted records — the same shape
    /// the built-in table's gate answers from. A provider that audited a
    /// window and recorded nothing (an empty slice) answers `false` for every
    /// window, which is what makes it a true no-op on the engine's hot path
    /// (issue #127): the engine must not derive a trading day — and refuse on
    /// a day that derivation reaches — for a provider that can move nothing.
    fn may_affect(&self, first: NaiveDate, last: NaiveDate) -> bool {
        let start = self
            .records
            .partition_point(|record| record.trade_date < first);
        self.records
            .get(start)
            .is_some_and(|record| record.trade_date <= last)
    }
}

/// A [`StaticSessionExceptions`] record-set invariant violation.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum StaticSessionExceptionsError {
    /// The last covered trade date precedes the first.
    CoverageBoundsInverted,
    /// The record at `index` is not later than its predecessor.
    DatesNotStrictlyIncreasing {
        /// Index of the duplicate or out-of-order record.
        index: usize,
    },
    /// The record at `index` falls outside the published coverage window.
    RecordOutsideCoverage {
        /// Index of the out-of-window record.
        index: usize,
    },
    /// The replacement at `index` carries no blocks; use a closed record.
    EmptyReplacement {
        /// Index of the empty replacement record.
        index: usize,
    },
    /// A block's opening-day offset is outside the permitted range.
    BlockOffsetOutOfRange {
        /// Index of the owning record.
        index: usize,
        /// Position of the invalid block inside that record.
        block: usize,
        /// Rejected opening-day offset.
        open_day_offset: i8,
    },
    /// A block's open is outside `0..86_400`.
    BlockOpenOutOfRange {
        /// Index of the owning record.
        index: usize,
        /// Position of the invalid block inside that record.
        block: usize,
        /// Rejected open value.
        open_ssm: u32,
    },
    /// A block's close is outside `0..=86_400`.
    BlockCloseOutOfRange {
        /// Index of the owning record.
        index: usize,
        /// Position of the invalid block inside that record.
        block: usize,
        /// Rejected close value.
        close_ssm: u32,
    },
    /// A block opening on its trade date closes on the following local date.
    ///
    /// A trade date is named by the local date of its final close, so a block
    /// at offset `0` may not wrap. A span covering one whole local day is
    /// stated as `open_ssm = 0`, `close_ssm = 86_400`, which does not wrap.
    BlockClosesAfterTradeDate {
        /// Index of the owning record.
        index: usize,
        /// Position of the wrapping block inside that record.
        block: usize,
    },
    /// A block does not start at or after its predecessor.
    BlocksNotOrdered {
        /// Index of the owning record.
        index: usize,
        /// Position of the out-of-order block inside that record.
        block: usize,
    },
}

impl core::fmt::Display for StaticSessionExceptionsError {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self {
            Self::CoverageBoundsInverted => {
                write!(f, "last covered trade date precedes the first")
            }
            Self::DatesNotStrictlyIncreasing { index } => write!(
                f,
                "record at index {index} is not later than its predecessor"
            ),
            Self::RecordOutsideCoverage { index } => write!(
                f,
                "record at index {index} falls outside the published coverage window"
            ),
            Self::EmptyReplacement { index } => write!(
                f,
                "replacement record at index {index} carries no blocks; record it as closed"
            ),
            Self::BlockOffsetOutOfRange {
                index,
                block,
                open_day_offset,
            } => write!(
                f,
                "block {block} of record {index} has open_day_offset {open_day_offset} outside {}..={}",
                ExceptionBlock::MIN_DAY_OFFSET,
                ExceptionBlock::MAX_DAY_OFFSET
            ),
            Self::BlockOpenOutOfRange {
                index,
                block,
                open_ssm,
            } => write!(
                f,
                "block {block} of record {index} has open_ssm {open_ssm} outside 0..86400"
            ),
            Self::BlockCloseOutOfRange {
                index,
                block,
                close_ssm,
            } => write!(
                f,
                "block {block} of record {index} has close_ssm {close_ssm} outside 0..=86400"
            ),
            Self::BlockClosesAfterTradeDate { index, block } => write!(
                f,
                "block {block} of record {index} opens on its trade date and closes on the next local date"
            ),
            Self::BlocksNotOrdered { index, block } => write!(
                f,
                "block {block} of record {index} starts before its predecessor"
            ),
        }
    }
}

impl std::error::Error for StaticSessionExceptionsError {}