600 lines
25 KiB
Rust
600 lines
25 KiB
Rust
//! Pure `std`-only North American Eastern Time (`16:00:00 ET`) session-close timestamp converter
|
||
//! covering `1976-01-01..=2030-12-31`.
|
||
//!
|
||
//! # DST Schedule (`America/New_York` == `America/Toronto` over `1976..=2030`)
|
||
//! Across the entire `1976..=2030` window, `America/New_York` and `America/Toronto` observed
|
||
//! identical Daylight Saving Time rules across three eras:
|
||
//! - **`1976..=1986`**: last Sunday of April → last Sunday of October.
|
||
//! - **`1987..=2006`**: first Sunday of April → last Sunday of October.
|
||
//! - **`2007..=2030`**: second Sunday of March → first Sunday of November (Energy Policy Act of
|
||
//! 2005 / matching Canadian provincial adoption).
|
||
//!
|
||
//! During EDT (`UTC-4`), `16:00:00 ET` is `20:00:00 UTC`. During EST (`UTC-5`), `16:00:00 ET` is
|
||
//! `21:00:00 UTC`. Because `16:00` is strictly after the `02:00` wall-clock transition, the spring
|
||
//! transition Sunday is already EDT (`20:00 UTC`) and the fall transition Sunday is already EST
|
||
//! (`21:00 UTC`).
|
||
//!
|
||
//! # Conservative Early-Close & Lower-Bound Policy
|
||
//! - **Early-close half days** (e.g., `13:00 ET` on Christmas Eve or the day after US
|
||
//! Thanksgiving) still map to `16:00:00 ET`, which is strictly later than the actual close and
|
||
//! therefore never introduces look-ahead bias for daily bar `known_at`.
|
||
//! - **Why the lower bound is `1976-01-01`**: in `1974–1975` the US Emergency Daylight Saving Time
|
||
//! Energy Conservation Act moved US clocks onto winter DST while Ontario/Toronto stayed on
|
||
//! standard rules, so `America/New_York` and `America/Toronto` diverged. Earlier dates return
|
||
//! [`SessionCloseError::DateOutOfRange`] rather than guessing exchange-specific historical rules.
|
||
|
||
use std::error::Error;
|
||
use std::fmt;
|
||
use std::sync::OnceLock;
|
||
|
||
use crate::shorts::epoch_days_to_ymd;
|
||
use crate::split::ymd_to_epoch_days;
|
||
|
||
/// First Gregorian year covered by [`et_session_close_utc_micros`] (`1976-01-01`).
|
||
pub const SESSION_CLOSE_MIN_SUPPORTED_YEAR: i32 = 1976;
|
||
|
||
/// Last Gregorian year covered by [`et_session_close_utc_micros`] (`2030-12-31`).
|
||
pub const SESSION_CLOSE_MAX_SUPPORTED_YEAR: i32 = 2030;
|
||
|
||
/// Epoch day for `1976-01-01` (inclusive lower bound of [`et_session_close_utc_micros`]).
|
||
pub const SESSION_CLOSE_MIN_SUPPORTED_DATE: i32 = 2_191;
|
||
|
||
/// Epoch day for `2030-12-31` (inclusive upper bound of [`et_session_close_utc_micros`]).
|
||
pub const SESSION_CLOSE_MAX_SUPPORTED_DATE: i32 = 22_279;
|
||
|
||
const MICROS_PER_SECOND: i64 = 1_000_000;
|
||
const MICROS_PER_MINUTE: i64 = 60 * MICROS_PER_SECOND;
|
||
const MICROS_PER_HOUR: i64 = 3_600 * MICROS_PER_SECOND;
|
||
const MICROS_PER_DAY: i64 = 86_400 * MICROS_PER_SECOND;
|
||
|
||
/// Inclusive lower bound in UTC microseconds for [`utc_micros_to_et_local`]
|
||
/// (`1976-01-01T00:00:00.000000 EST` = `1976-01-01T05:00:00.000000Z`).
|
||
pub const SESSION_MIN_SUPPORTED_UTC_MICROS: i64 =
|
||
SESSION_CLOSE_MIN_SUPPORTED_DATE as i64 * MICROS_PER_DAY + 5 * MICROS_PER_HOUR;
|
||
|
||
/// Inclusive upper bound in UTC microseconds for [`utc_micros_to_et_local`]
|
||
/// (`2030-12-31T23:59:59.999999 EST` = `2031-01-01T04:59:59.999999Z`).
|
||
pub const SESSION_MAX_SUPPORTED_UTC_MICROS: i64 =
|
||
(SESSION_CLOSE_MAX_SUPPORTED_DATE as i64 + 1) * MICROS_PER_DAY + 5 * MICROS_PER_HOUR - 1;
|
||
|
||
const SUPPORTED_YEAR_COUNT: usize =
|
||
(SESSION_CLOSE_MAX_SUPPORTED_YEAR - SESSION_CLOSE_MIN_SUPPORTED_YEAR + 1) as usize;
|
||
|
||
/// Eastern Time (`America/New_York` / `America/Toronto`) calendar date, minute-of-day, and DST
|
||
/// state returned by [`utc_micros_to_et_local`].
|
||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||
pub struct EtLocalDateTime {
|
||
/// Local Eastern calendar date as days since `1970-01-01`.
|
||
pub session_date: i32,
|
||
/// Local Eastern minute of day (`0..=1439`, i.e., `hour * 60 + minute`).
|
||
pub minute_of_day: u16,
|
||
/// `true` during EDT (`UTC-4`), `false` during EST (`UTC-5`).
|
||
pub is_edt: bool,
|
||
}
|
||
|
||
/// Typed error returned by [`et_session_close_utc_micros`] and [`utc_micros_to_et_local`].
|
||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||
pub enum SessionCloseError {
|
||
/// The requested epoch day fell outside `[SESSION_CLOSE_MIN_SUPPORTED_DATE, SESSION_CLOSE_MAX_SUPPORTED_DATE]`.
|
||
DateOutOfRange {
|
||
epoch_day: i32,
|
||
min_supported: i32,
|
||
max_supported: i32,
|
||
},
|
||
/// The requested UTC timestamp fell outside `[SESSION_MIN_SUPPORTED_UTC_MICROS, SESSION_MAX_SUPPORTED_UTC_MICROS]`.
|
||
TimestampOutOfRange {
|
||
utc_micros: i64,
|
||
min_supported: i64,
|
||
max_supported: i64,
|
||
},
|
||
}
|
||
|
||
impl fmt::Display for SessionCloseError {
|
||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||
match *self {
|
||
Self::DateOutOfRange {
|
||
epoch_day,
|
||
min_supported,
|
||
max_supported,
|
||
} => write!(
|
||
f,
|
||
"epoch day {epoch_day} is outside supported session-close range [{min_supported}, {max_supported}] ({SESSION_CLOSE_MIN_SUPPORTED_YEAR}-01-01..={SESSION_CLOSE_MAX_SUPPORTED_YEAR}-12-31)"
|
||
),
|
||
Self::TimestampOutOfRange {
|
||
utc_micros,
|
||
min_supported,
|
||
max_supported,
|
||
} => write!(
|
||
f,
|
||
"UTC timestamp {utc_micros} us is outside supported Eastern Time range [{min_supported}, {max_supported}] ({SESSION_CLOSE_MIN_SUPPORTED_YEAR}-01-01T05:00:00Z..=2031-01-01T04:59:59.999999Z)"
|
||
),
|
||
}
|
||
}
|
||
}
|
||
|
||
impl Error for SessionCloseError {}
|
||
|
||
/// ISO-8601 weekday for an epoch day: `1 = Monday`, ..., `6 = Saturday`, `7 = Sunday`.
|
||
#[inline]
|
||
fn iso_weekday(epoch_day: i32) -> u8 {
|
||
// 1970-01-01 (epoch_day == 0) was Thursday (ISO 4).
|
||
((i64::from(epoch_day) + 3).rem_euclid(7) + 1) as u8
|
||
}
|
||
|
||
/// Returns the epoch day of the `nth` Sunday (`1`-indexed) in `(year, month)`.
|
||
fn nth_sunday_of_month(year: i32, month: u32, nth: u32) -> i32 {
|
||
let first = ymd_to_epoch_days(year, month, 1).expect("valid first of month");
|
||
let first_wd = iso_weekday(first);
|
||
let delta = (7 - i32::from(first_wd)).rem_euclid(7);
|
||
first + delta + (nth as i32 - 1) * 7
|
||
}
|
||
|
||
/// Returns the epoch day of the last Sunday in `(year, month)`.
|
||
fn last_sunday_of_month(year: i32, month: u32) -> i32 {
|
||
let (next_year, next_month) = if month == 12 {
|
||
(year + 1, 1)
|
||
} else {
|
||
(year, month + 1)
|
||
};
|
||
let last = ymd_to_epoch_days(next_year, next_month, 1).expect("valid first of next month") - 1;
|
||
let last_wd = iso_weekday(last);
|
||
last - (i32::from(last_wd) % 7)
|
||
}
|
||
|
||
fn build_dst_bounds() -> [(i32, i32); SUPPORTED_YEAR_COUNT] {
|
||
let mut table = [(0i32, 0i32); SUPPORTED_YEAR_COUNT];
|
||
for year in SESSION_CLOSE_MIN_SUPPORTED_YEAR..=SESSION_CLOSE_MAX_SUPPORTED_YEAR {
|
||
let (dst_start, dst_end) = match year {
|
||
1976..=1986 => (
|
||
last_sunday_of_month(year, 4),
|
||
last_sunday_of_month(year, 10),
|
||
),
|
||
1987..=2006 => (
|
||
nth_sunday_of_month(year, 4, 1),
|
||
last_sunday_of_month(year, 10),
|
||
),
|
||
2007..=2030 => (
|
||
nth_sunday_of_month(year, 3, 2),
|
||
nth_sunday_of_month(year, 11, 1),
|
||
),
|
||
_ => unreachable!("year is bounded by supported range"),
|
||
};
|
||
table[(year - SESSION_CLOSE_MIN_SUPPORTED_YEAR) as usize] = (dst_start, dst_end);
|
||
}
|
||
table
|
||
}
|
||
|
||
#[inline]
|
||
fn dst_bounds() -> &'static [(i32, i32); SUPPORTED_YEAR_COUNT] {
|
||
static TABLE: OnceLock<[(i32, i32); SUPPORTED_YEAR_COUNT]> = OnceLock::new();
|
||
TABLE.get_or_init(build_dst_bounds)
|
||
}
|
||
|
||
/// Converts `epoch_day` at `16:00:00` Eastern wall-clock (`America/New_York` / `America/Toronto`)
|
||
/// to UTC microseconds since the Unix epoch.
|
||
///
|
||
/// Covers `1976-01-01..=2030-12-31` across all three North American DST eras (`1976–1986`,
|
||
/// `1987–2006`, `2007–2030`). Returns [`SessionCloseError::DateOutOfRange`] for any date outside
|
||
/// `[SESSION_CLOSE_MIN_SUPPORTED_DATE, SESSION_CLOSE_MAX_SUPPORTED_DATE]`.
|
||
#[inline]
|
||
pub fn et_session_close_utc_micros(epoch_day: i32) -> Result<i64, SessionCloseError> {
|
||
if !(SESSION_CLOSE_MIN_SUPPORTED_DATE..=SESSION_CLOSE_MAX_SUPPORTED_DATE).contains(&epoch_day) {
|
||
return Err(SessionCloseError::DateOutOfRange {
|
||
epoch_day,
|
||
min_supported: SESSION_CLOSE_MIN_SUPPORTED_DATE,
|
||
max_supported: SESSION_CLOSE_MAX_SUPPORTED_DATE,
|
||
});
|
||
}
|
||
|
||
let (year, _, _) = epoch_days_to_ymd(epoch_day);
|
||
let (dst_start, dst_end) = dst_bounds()[(year - SESSION_CLOSE_MIN_SUPPORTED_YEAR) as usize];
|
||
let is_edt = epoch_day >= dst_start && epoch_day < dst_end;
|
||
let utc_hour_secs: i64 = if is_edt { 20 * 3_600 } else { 21 * 3_600 };
|
||
Ok((i64::from(epoch_day) * 86_400 + utc_hour_secs) * 1_000_000)
|
||
}
|
||
|
||
/// Converts a UTC timestamp (microseconds since the Unix epoch) into North American Eastern Time
|
||
/// (`America/New_York` / `America/Toronto`) calendar date, minute-of-day (`0..=1439`), and DST
|
||
/// state.
|
||
///
|
||
/// Reuses the crate's `1976..=2030` DST table across all three historical eras (`1976–1986`,
|
||
/// `1987–2006`, `2007–2030`) with exact `02:00 EST` (`07:00 UTC`) spring-forward and `02:00 EDT`
|
||
/// (`06:00 UTC`) fall-back transitions. Returns [`SessionCloseError::TimestampOutOfRange`] for any
|
||
/// timestamp outside `[SESSION_MIN_SUPPORTED_UTC_MICROS, SESSION_MAX_SUPPORTED_UTC_MICROS]`.
|
||
#[inline]
|
||
pub fn utc_micros_to_et_local(utc_micros: i64) -> Result<EtLocalDateTime, SessionCloseError> {
|
||
if !(SESSION_MIN_SUPPORTED_UTC_MICROS..=SESSION_MAX_SUPPORTED_UTC_MICROS).contains(&utc_micros) {
|
||
return Err(SessionCloseError::TimestampOutOfRange {
|
||
utc_micros,
|
||
min_supported: SESSION_MIN_SUPPORTED_UTC_MICROS,
|
||
max_supported: SESSION_MAX_SUPPORTED_UTC_MICROS,
|
||
});
|
||
}
|
||
|
||
// Bounds check above guarantees `est_micros >= 2191 * MICROS_PER_DAY > 0` without overflow.
|
||
let est_micros = utc_micros - 5 * MICROS_PER_HOUR;
|
||
let est_day = (est_micros / MICROS_PER_DAY) as i32;
|
||
let (year, _, _) = epoch_days_to_ymd(est_day);
|
||
let (dst_start, dst_end) = dst_bounds()[(year - SESSION_CLOSE_MIN_SUPPORTED_YEAR) as usize];
|
||
|
||
// Spring forward: 02:00:00 EST = 07:00:00 UTC on `dst_start`.
|
||
// Fall back: 02:00:00 EDT = 06:00:00 UTC on `dst_end`.
|
||
let dst_start_utc = i64::from(dst_start) * MICROS_PER_DAY + 7 * MICROS_PER_HOUR;
|
||
let dst_end_utc = i64::from(dst_end) * MICROS_PER_DAY + 6 * MICROS_PER_HOUR;
|
||
let is_edt = utc_micros >= dst_start_utc && utc_micros < dst_end_utc;
|
||
|
||
let local_micros = if is_edt {
|
||
est_micros + MICROS_PER_HOUR
|
||
} else {
|
||
est_micros
|
||
};
|
||
let session_date = (local_micros / MICROS_PER_DAY) as i32;
|
||
let rem_micros = local_micros - i64::from(session_date) * MICROS_PER_DAY;
|
||
let minute_of_day = (rem_micros / MICROS_PER_MINUTE) as u16;
|
||
|
||
Ok(EtLocalDateTime {
|
||
session_date,
|
||
minute_of_day,
|
||
is_edt,
|
||
})
|
||
}
|
||
|
||
#[cfg(test)]
|
||
mod tests {
|
||
use super::*;
|
||
use crate::shorts::{et_to_utc_micros, MAX_SUPPORTED_DATE, MIN_SUPPORTED_DATE};
|
||
|
||
fn day(y: i32, m: u32, d: u32) -> i32 {
|
||
ymd_to_epoch_days(y, m, d).expect("valid date")
|
||
}
|
||
|
||
fn expected_utc_micros(epoch_day: i32, utc_hour: i64) -> i64 {
|
||
(i64::from(epoch_day) * 86_400 + utc_hour * 3_600) * 1_000_000
|
||
}
|
||
|
||
#[test]
|
||
fn supported_date_constants_match_ymd_to_epoch_days() {
|
||
assert_eq!(SESSION_CLOSE_MIN_SUPPORTED_DATE, day(1976, 1, 1));
|
||
assert_eq!(SESSION_CLOSE_MAX_SUPPORTED_DATE, day(2030, 12, 31));
|
||
assert_eq!(SUPPORTED_YEAR_COUNT, 55);
|
||
}
|
||
|
||
#[test]
|
||
fn known_vectors_cover_all_three_dst_eras_and_transitions() {
|
||
let cases = [
|
||
// Era 1 (1976..=1986): last Sunday of April -> last Sunday of October
|
||
(1976, 4, 24, 21), // Saturday before spring-forward (EST)
|
||
(1976, 4, 25, 20), // Last Sunday of April (EDT)
|
||
(1976, 10, 30, 20), // Saturday before fall-back (EDT)
|
||
(1976, 10, 31, 21), // Last Sunday of October (EST)
|
||
(1980, 1, 15, 21), // Era 1 mid-winter (EST)
|
||
(1980, 7, 15, 20), // Era 1 mid-summer (EDT)
|
||
(1986, 4, 26, 21), // Saturday before spring-forward (EST)
|
||
(1986, 4, 27, 20), // Last Sunday of April (EDT)
|
||
// Era 2 (1987..=2006): first Sunday of April -> last Sunday of October
|
||
(1987, 4, 4, 21), // Saturday before spring-forward (EST)
|
||
(1987, 4, 5, 20), // First Sunday of April (EDT)
|
||
(1987, 10, 24, 20), // Saturday before fall-back (EDT)
|
||
(1987, 10, 25, 21), // Last Sunday of October (EST)
|
||
(1999, 1, 15, 21), // Era 2 mid-winter (EST)
|
||
(1999, 7, 15, 20), // Era 2 mid-summer (EDT)
|
||
(2006, 4, 1, 21), // Saturday before spring-forward (EST)
|
||
(2006, 4, 2, 20), // First Sunday of April (EDT)
|
||
(2006, 10, 28, 20), // Saturday before fall-back (EDT)
|
||
(2006, 10, 29, 21), // Last Sunday of October (EST)
|
||
// Era 3 (2007..=2030): second Sunday of March -> first Sunday of November
|
||
(2007, 3, 10, 21), // Saturday before spring-forward (EST)
|
||
(2007, 3, 11, 20), // Second Sunday of March (EDT)
|
||
(2007, 11, 3, 20), // Saturday before fall-back (EDT)
|
||
(2007, 11, 4, 21), // First Sunday of November (EST)
|
||
(2024, 1, 15, 21), // Era 3 mid-winter (EST)
|
||
(2024, 7, 15, 20), // Era 3 mid-summer (EDT)
|
||
];
|
||
|
||
for (y, m, d, expected_hour) in cases {
|
||
let epoch_day = day(y, m, d);
|
||
let actual = et_session_close_utc_micros(epoch_day).expect("in-range date");
|
||
let expected = expected_utc_micros(epoch_day, expected_hour);
|
||
assert_eq!(
|
||
actual, expected,
|
||
"micros mismatch on {y:04}-{m:02}-{d:02} (expected {expected_hour}:00 UTC)"
|
||
);
|
||
let hour_of_day = (actual / 1_000_000 - i64::from(epoch_day) * 86_400) / 3_600;
|
||
assert_eq!(
|
||
hour_of_day, expected_hour,
|
||
"UTC hour mismatch on {y:04}-{m:02}-{d:02}"
|
||
);
|
||
}
|
||
}
|
||
|
||
#[test]
|
||
fn bit_exact_parity_with_shorts_et_to_utc_micros_for_2008_to_2030() {
|
||
for d in MIN_SUPPORTED_DATE..=MAX_SUPPORTED_DATE {
|
||
let expected = et_to_utc_micros(d, 16, 0, 0).expect("shorts 2008..=2030 date");
|
||
let actual = et_session_close_utc_micros(d).expect("session_close 2008..=2030 date");
|
||
assert_eq!(
|
||
actual, expected,
|
||
"parity mismatch against shorts::et_to_utc_micros on epoch_day={d}"
|
||
);
|
||
}
|
||
}
|
||
|
||
#[test]
|
||
fn full_window_sweep_has_only_20_or_21_utc_and_two_sunday_flips_per_year() {
|
||
let mut spring_flips_by_year = [0u32; SUPPORTED_YEAR_COUNT];
|
||
let mut fall_flips_by_year = [0u32; SUPPORTED_YEAR_COUNT];
|
||
|
||
let mut prev_hour: Option<i64> = None;
|
||
for d in SESSION_CLOSE_MIN_SUPPORTED_DATE..=SESSION_CLOSE_MAX_SUPPORTED_DATE {
|
||
let micros = et_session_close_utc_micros(d).expect("in-range date");
|
||
let offset_micros = micros - i64::from(d) * 86_400 * 1_000_000;
|
||
let utc_hour = match offset_micros {
|
||
72_000_000_000 => 20, // 20 * 3600 * 1e6
|
||
75_600_000_000 => 21, // 21 * 3600 * 1e6
|
||
other => panic!(
|
||
"unexpected UTC offset micros {other} on epoch_day={d} (expected 20h or 21h)"
|
||
),
|
||
};
|
||
|
||
if let Some(prev) = prev_hour {
|
||
if prev != utc_hour {
|
||
assert_eq!(
|
||
iso_weekday(d),
|
||
7,
|
||
"DST flip on epoch_day={d} must occur on a Sunday"
|
||
);
|
||
let (year, _, _) = epoch_days_to_ymd(d);
|
||
let idx = (year - SESSION_CLOSE_MIN_SUPPORTED_YEAR) as usize;
|
||
match (prev, utc_hour) {
|
||
(21, 20) => spring_flips_by_year[idx] += 1,
|
||
(20, 21) => fall_flips_by_year[idx] += 1,
|
||
_ => unreachable!(),
|
||
}
|
||
}
|
||
}
|
||
prev_hour = Some(utc_hour);
|
||
}
|
||
|
||
for year in SESSION_CLOSE_MIN_SUPPORTED_YEAR..=SESSION_CLOSE_MAX_SUPPORTED_YEAR {
|
||
let idx = (year - SESSION_CLOSE_MIN_SUPPORTED_YEAR) as usize;
|
||
assert_eq!(
|
||
spring_flips_by_year[idx], 1,
|
||
"year {year} must have exactly one EST->EDT flip"
|
||
);
|
||
assert_eq!(
|
||
fall_flips_by_year[idx], 1,
|
||
"year {year} must have exactly one EDT->EST flip"
|
||
);
|
||
}
|
||
}
|
||
|
||
#[test]
|
||
fn out_of_range_dates_return_typed_error_without_panicking() {
|
||
let bad_days = [
|
||
day(1975, 12, 31),
|
||
day(2031, 1, 1),
|
||
i32::MIN,
|
||
i32::MAX,
|
||
];
|
||
for bad_day in bad_days {
|
||
let err = et_session_close_utc_micros(bad_day).unwrap_err();
|
||
assert_eq!(
|
||
err,
|
||
SessionCloseError::DateOutOfRange {
|
||
epoch_day: bad_day,
|
||
min_supported: SESSION_CLOSE_MIN_SUPPORTED_DATE,
|
||
max_supported: SESSION_CLOSE_MAX_SUPPORTED_DATE,
|
||
}
|
||
);
|
||
assert!(err.source().is_none());
|
||
let msg = err.to_string();
|
||
assert!(
|
||
msg.contains(&bad_day.to_string()) && msg.contains("1976-01-01..=2030-12-31"),
|
||
"unexpected Display message: {msg}"
|
||
);
|
||
}
|
||
}
|
||
|
||
fn utc_micros(y: i32, m: u32, d: u32, h: i64, min: i64, sec: i64, us: i64) -> i64 {
|
||
let ep = day(y, m, d);
|
||
((i64::from(ep) * 86_400 + h * 3_600 + min * 60 + sec) * 1_000_000) + us
|
||
}
|
||
|
||
#[test]
|
||
fn utc_micros_to_et_local_round_trips_et_session_close_across_all_supported_days() {
|
||
for d in SESSION_CLOSE_MIN_SUPPORTED_DATE..=SESSION_CLOSE_MAX_SUPPORTED_DATE {
|
||
let close_utc = et_session_close_utc_micros(d).expect("in-range date");
|
||
let local = utc_micros_to_et_local(close_utc).expect("in-range timestamp");
|
||
assert_eq!(local.session_date, d, "session_date mismatch on day={d}");
|
||
assert_eq!(
|
||
local.minute_of_day,
|
||
16 * 60,
|
||
"16:00 ET minute mismatch on day={d}"
|
||
);
|
||
let expected_edt =
|
||
(close_utc - i64::from(d) * MICROS_PER_DAY) == 20 * MICROS_PER_HOUR;
|
||
assert_eq!(local.is_edt, expected_edt, "DST flag mismatch on day={d}");
|
||
}
|
||
}
|
||
|
||
#[test]
|
||
fn utc_micros_to_et_local_handles_dst_transitions_across_all_three_eras() {
|
||
// (year, spring_month, spring_day, fall_month, fall_day)
|
||
let eras = [
|
||
// Era 1 (1976..=1986): last Sun Apr -> last Sun Oct
|
||
(1976, 4, 25, 10, 31),
|
||
(1986, 4, 27, 10, 26),
|
||
// Era 2 (1987..=2006): 1st Sun Apr -> last Sun Oct (including 2003)
|
||
(1987, 4, 5, 10, 25),
|
||
(2003, 4, 6, 10, 26),
|
||
(2006, 4, 2, 10, 29),
|
||
// Era 3 (2007..=2030): 2nd Sun Mar -> 1st Sun Nov
|
||
(2007, 3, 11, 11, 4),
|
||
(2024, 3, 10, 11, 3),
|
||
(2030, 3, 10, 11, 3),
|
||
];
|
||
|
||
for (y, sm, sd, fm, fd) in eras {
|
||
let spring_day = day(y, sm, sd);
|
||
// 06:59:59.999999 UTC -> 01:59:59.999999 EST
|
||
let before_spring = utc_micros(y, sm, sd, 6, 59, 59, 999_999);
|
||
assert_eq!(
|
||
utc_micros_to_et_local(before_spring).unwrap(),
|
||
EtLocalDateTime {
|
||
session_date: spring_day,
|
||
minute_of_day: 60 + 59,
|
||
is_edt: false,
|
||
},
|
||
"before spring-forward in {y}"
|
||
);
|
||
// 07:00:00.000000 UTC -> 03:00:00.000000 EDT (skips 02:xx)
|
||
let at_spring = utc_micros(y, sm, sd, 7, 0, 0, 0);
|
||
assert_eq!(
|
||
utc_micros_to_et_local(at_spring).unwrap(),
|
||
EtLocalDateTime {
|
||
session_date: spring_day,
|
||
minute_of_day: 3 * 60,
|
||
is_edt: true,
|
||
},
|
||
"at spring-forward in {y}"
|
||
);
|
||
|
||
let fall_day = day(y, fm, fd);
|
||
// 05:59:59.999999 UTC -> 01:59:59.999999 EDT (first 01:59)
|
||
let before_fall = utc_micros(y, fm, fd, 5, 59, 59, 999_999);
|
||
assert_eq!(
|
||
utc_micros_to_et_local(before_fall).unwrap(),
|
||
EtLocalDateTime {
|
||
session_date: fall_day,
|
||
minute_of_day: 60 + 59,
|
||
is_edt: true,
|
||
},
|
||
"before fall-back in {y}"
|
||
);
|
||
// 06:00:00.000000 UTC -> 01:00:00.000000 EST (repeated 01:00)
|
||
let at_fall = utc_micros(y, fm, fd, 6, 0, 0, 0);
|
||
assert_eq!(
|
||
utc_micros_to_et_local(at_fall).unwrap(),
|
||
EtLocalDateTime {
|
||
session_date: fall_day,
|
||
minute_of_day: 60,
|
||
is_edt: false,
|
||
},
|
||
"at fall-back in {y}"
|
||
);
|
||
}
|
||
}
|
||
|
||
#[test]
|
||
fn utc_micros_to_et_local_handles_utc_vs_et_midnight_crossovers() {
|
||
// Summer (EDT, UTC-4): 2024-06-10 00:30 UTC == 2024-06-09 20:30 EDT (previous calendar day)
|
||
assert_eq!(
|
||
utc_micros_to_et_local(utc_micros(2024, 6, 10, 0, 30, 0, 0)).unwrap(),
|
||
EtLocalDateTime {
|
||
session_date: day(2024, 6, 9),
|
||
minute_of_day: 20 * 60 + 30,
|
||
is_edt: true,
|
||
}
|
||
);
|
||
// 2024-06-10 03:59:59.999999 UTC == 2024-06-09 23:59 EDT
|
||
assert_eq!(
|
||
utc_micros_to_et_local(utc_micros(2024, 6, 10, 3, 59, 59, 999_999)).unwrap(),
|
||
EtLocalDateTime {
|
||
session_date: day(2024, 6, 9),
|
||
minute_of_day: 23 * 60 + 59,
|
||
is_edt: true,
|
||
}
|
||
);
|
||
// 2024-06-10 04:00:00.000000 UTC == 2024-06-10 00:00 EDT
|
||
assert_eq!(
|
||
utc_micros_to_et_local(utc_micros(2024, 6, 10, 4, 0, 0, 0)).unwrap(),
|
||
EtLocalDateTime {
|
||
session_date: day(2024, 6, 10),
|
||
minute_of_day: 0,
|
||
is_edt: true,
|
||
}
|
||
);
|
||
|
||
// Winter (EST, UTC-5): 2025-01-16 00:30 UTC == 2025-01-15 19:30 EST
|
||
assert_eq!(
|
||
utc_micros_to_et_local(utc_micros(2025, 1, 16, 0, 30, 0, 0)).unwrap(),
|
||
EtLocalDateTime {
|
||
session_date: day(2025, 1, 15),
|
||
minute_of_day: 19 * 60 + 30,
|
||
is_edt: false,
|
||
}
|
||
);
|
||
// 2025-01-16 04:59:59.999999 UTC == 2025-01-15 23:59 EST
|
||
assert_eq!(
|
||
utc_micros_to_et_local(utc_micros(2025, 1, 16, 4, 59, 59, 999_999)).unwrap(),
|
||
EtLocalDateTime {
|
||
session_date: day(2025, 1, 15),
|
||
minute_of_day: 23 * 60 + 59,
|
||
is_edt: false,
|
||
}
|
||
);
|
||
// 2025-01-16 05:00:00.000000 UTC == 2025-01-16 00:00 EST
|
||
assert_eq!(
|
||
utc_micros_to_et_local(utc_micros(2025, 1, 16, 5, 0, 0, 0)).unwrap(),
|
||
EtLocalDateTime {
|
||
session_date: day(2025, 1, 16),
|
||
minute_of_day: 0,
|
||
is_edt: false,
|
||
}
|
||
);
|
||
}
|
||
|
||
#[test]
|
||
fn utc_micros_to_et_local_bounds_and_out_of_range_errors() {
|
||
// Exact inclusive bounds
|
||
assert_eq!(
|
||
SESSION_MIN_SUPPORTED_UTC_MICROS,
|
||
utc_micros(1976, 1, 1, 5, 0, 0, 0)
|
||
);
|
||
assert_eq!(
|
||
SESSION_MAX_SUPPORTED_UTC_MICROS,
|
||
utc_micros(2031, 1, 1, 4, 59, 59, 999_999)
|
||
);
|
||
assert_eq!(
|
||
utc_micros_to_et_local(SESSION_MIN_SUPPORTED_UTC_MICROS).unwrap(),
|
||
EtLocalDateTime {
|
||
session_date: SESSION_CLOSE_MIN_SUPPORTED_DATE,
|
||
minute_of_day: 0,
|
||
is_edt: false,
|
||
}
|
||
);
|
||
assert_eq!(
|
||
utc_micros_to_et_local(SESSION_MAX_SUPPORTED_UTC_MICROS).unwrap(),
|
||
EtLocalDateTime {
|
||
session_date: SESSION_CLOSE_MAX_SUPPORTED_DATE,
|
||
minute_of_day: 23 * 60 + 59,
|
||
is_edt: false,
|
||
}
|
||
);
|
||
|
||
let bad_ts = [
|
||
SESSION_MIN_SUPPORTED_UTC_MICROS - 1,
|
||
SESSION_MAX_SUPPORTED_UTC_MICROS + 1,
|
||
0,
|
||
i64::MIN,
|
||
i64::MAX,
|
||
];
|
||
for ts in bad_ts {
|
||
let err = utc_micros_to_et_local(ts).unwrap_err();
|
||
assert_eq!(
|
||
err,
|
||
SessionCloseError::TimestampOutOfRange {
|
||
utc_micros: ts,
|
||
min_supported: SESSION_MIN_SUPPORTED_UTC_MICROS,
|
||
max_supported: SESSION_MAX_SUPPORTED_UTC_MICROS,
|
||
}
|
||
);
|
||
assert!(err.source().is_none());
|
||
let msg = err.to_string();
|
||
assert!(
|
||
msg.contains(&ts.to_string()) && msg.contains("1976-01-01T05:00:00Z"),
|
||
"unexpected Display message: {msg}"
|
||
);
|
||
}
|
||
}
|
||
}
|