use chrono::{DateTime, NaiveDate, Offset, Utc};
use chrono_tz::Tz;
use super::StaticHoursProfile;
use crate::calendar::local_time::{bounded_utc, mk_local_open};
#[derive(Clone, Copy)]
pub(crate) struct SourceRef(&'static str);
impl SourceRef {
pub(crate) const fn new(citation: &'static str) -> Self {
Self(citation)
}
pub(crate) const fn as_str(self) -> &'static str {
self.0
}
}
#[derive(Clone, Copy)]
pub(crate) struct Revision {
pub(crate) effective: NaiveDate,
pub(crate) profile: &'static StaticHoursProfile,
pub(crate) source: SourceRef,
}
macro_rules! revisions {
(
$( ($year:expr, $month:expr, $day:expr, $profile:expr, $citation:literal $(,)?) ),+ $(,)?
) => {{
const TIMELINE: &[$crate::calendar::schedules::timeline::Revision] = &[
$(
$crate::calendar::schedules::timeline::Revision {
effective: $crate::calendar::schedules::timeline::effective_date(
$year, $month, $day,
),
profile: $profile,
source: $crate::calendar::schedules::timeline::SourceRef::new($citation),
}
),+
];
const _: () = {
const DATES: &[(i32, u32, u32)] = &[$(($year, $month, $day)),+];
$crate::calendar::schedules::timeline::assert_ascending(DATES);
$crate::calendar::schedules::timeline::assert_cited(TIMELINE);
};
TIMELINE
}};
}
pub(crate) use revisions;
macro_rules! horizon {
(none) => {
None
};
($year:expr, $month:expr, $day:expr $(,)?) => {
Some($crate::calendar::schedules::timeline::effective_date(
$year, $month, $day,
))
};
}
pub(crate) use horizon;
pub(crate) const fn assert_ascending(rows: &[(i32, u32, u32)]) {
let mut index = 1;
while index < rows.len() {
assert!(
day_key(rows[index].0, rows[index].1, rows[index].2)
> day_key(rows[index - 1].0, rows[index - 1].1, rows[index - 1].2),
"revision timeline is not strictly ascending; a row is out of order or shadowed"
);
index += 1;
}
}
pub(crate) const fn assert_cited(rows: &[Revision]) {
let mut index = 0;
while index < rows.len() {
assert!(
!rows[index].source.as_str().is_empty(),
"revision row carries no primary-source citation"
);
index += 1;
}
}
const fn day_key(year: i32, month: u32, day: u32) -> i64 {
year as i64 * 10_000 + month as i64 * 100 + day as i64
}
#[expect(
clippy::panic,
reason = "const-eval only: an invalid sourced date must fail the build"
)]
pub(crate) const fn effective_date(year: i32, month: u32, day: u32) -> NaiveDate {
match NaiveDate::from_ymd_opt(year, month, day) {
Some(date) => date,
None => panic!("invalid hard-coded effective date"),
}
}
#[inline]
pub(crate) fn local_date(as_of: DateTime<Utc>, tz: Tz) -> NaiveDate {
bounded_utc(as_of, tz).with_timezone(&tz).date_naive()
}
pub(crate) fn select_revision(
day: NaiveDate,
baseline: &'static StaticHoursProfile,
revisions: &[Revision],
) -> &'static StaticHoursProfile {
let insertion = revisions.partition_point(|revision| revision.effective <= day);
insertion
.checked_sub(1)
.and_then(|index| revisions.get(index))
.map_or(baseline, |revision| revision.profile)
}
pub(crate) fn reference_delta_seconds(as_of: DateTime<Utc>, venue_tz: Tz, reference_tz: Tz) -> i32 {
let venue_noon = mk_local_open(venue_tz, local_date(as_of, venue_tz), 12 * 3600);
let reference_noon = venue_noon.with_timezone(&reference_tz);
reference_noon.offset().fix().local_minus_utc() - venue_noon.offset().fix().local_minus_utc()
}