use alloc::vec::Vec;
use crate::calendar::J2000_JDN;
use crate::error::TimeError;
pub const DELTA_T_A: f64 = 32.184;
pub const K: f64 = 1.657e-3;
pub const EB: f64 = 1.671e-2;
pub const M0: f64 = 6.239996;
pub const M1: f64 = 1.99096871e-7;
pub const JDN_1960: i64 = 2_436_935;
pub const JDN_1972: i64 = 2_441_318;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct LeapEntry {
pub jdn: i64,
pub delta_at: i32,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum LeapSecondSource {
IersOnly,
Sofa,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum LeapSecondPolicy {
TableDriven,
FrozenAfter { jdn: i64 },
}
impl LeapSecondPolicy {
pub fn frozen_after(year: i32, month: u8, day: u8) -> Self {
Self::FrozenAfter {
jdn: crate::calendar::gregorian_to_jdn(year, month, day),
}
}
}
#[derive(Clone, Debug)]
pub struct LeapSecondTable {
pub delta_t_a: f64,
pub k: f64,
pub eb: f64,
pub m0: f64,
pub m1: f64,
entries: Vec<LeapEntry>,
nominal: Vec<i64>,
tai_start: Vec<i64>,
source: LeapSecondSource,
policy: LeapSecondPolicy,
}
impl LeapSecondTable {
pub fn new(mut entries: Vec<LeapEntry>) -> Result<Self, TimeError> {
if entries.is_empty() {
return Err(TimeError::LskMissing("DELTET/DELTA_AT"));
}
entries.sort_by_key(|e| e.jdn);
for pair in entries.windows(2) {
if pair[0].jdn == pair[1].jdn {
return Err(TimeError::LskParse);
}
}
let nominal: Vec<i64> = entries
.iter()
.map(|e| (e.jdn - J2000_JDN) * 86_400)
.collect();
let tai_start: Vec<i64> = entries
.iter()
.zip(nominal.iter())
.map(|(e, n)| *n + e.delta_at as i64)
.collect();
for pair in tai_start.windows(2) {
if pair[0] >= pair[1] {
return Err(TimeError::LskParse);
}
}
Ok(Self {
delta_t_a: DELTA_T_A,
k: K,
eb: EB,
m0: M0,
m1: M1,
entries,
nominal,
tai_start,
source: LeapSecondSource::IersOnly,
policy: LeapSecondPolicy::TableDriven,
})
}
pub fn with_constants(mut self, delta_t_a: f64, k: f64, eb: f64, m0: f64, m1: f64) -> Self {
self.delta_t_a = delta_t_a;
self.k = k;
self.eb = eb;
self.m0 = m0;
self.m1 = m1;
self
}
pub fn builtin() -> Result<Self, TimeError> {
crate::lsk::builtin()
}
pub fn from_lsk(text: &str) -> Result<Self, TimeError> {
crate::lsk::parse_lsk(text)
}
pub fn with_source(mut self, source: LeapSecondSource) -> Self {
self.source = source;
self
}
pub fn source(&self) -> LeapSecondSource {
self.source
}
pub fn with_policy(mut self, policy: LeapSecondPolicy) -> Self {
self.policy = policy;
self
}
pub fn policy(&self) -> LeapSecondPolicy {
self.policy
}
pub fn delta_at_f64(&self, jdn: i64, day_fraction: f64) -> Option<f64> {
if jdn >= JDN_1972 {
self.delta_at_for_jdn(jdn).map(|value| value as f64)
} else if self.source == LeapSecondSource::Sofa && jdn >= JDN_1960 {
let mjd = (jdn - 2_400_001) as f64 + day_fraction;
sofa_delta_at(mjd)
} else {
None
}
}
pub(crate) fn first_tai_start(&self) -> i64 {
self.tai_start[0]
}
pub fn entries(&self) -> &[LeapEntry] {
&self.entries
}
pub fn coverage(&self) -> (i64, i64) {
let first = self.entries.first().map(|entry| entry.jdn).unwrap_or(0);
let last = self.entries.last().map(|entry| entry.jdn).unwrap_or(0);
(first, last)
}
pub(crate) fn nominal_sec(&self, index: usize) -> i64 {
self.nominal[index]
}
pub(crate) fn interval_index(&self, tai_sec: i64) -> Option<usize> {
let max = self.max_index()?;
let idx = self.tai_start.partition_point(|&t| t <= tai_sec);
if idx == 0 {
None
} else {
Some((idx - 1).min(max))
}
}
pub(crate) fn is_active_index(&self, index: usize) -> bool {
match self.max_index() {
Some(max) => index <= max,
None => false,
}
}
fn max_index(&self) -> Option<usize> {
match self.policy {
LeapSecondPolicy::TableDriven => Some(self.entries.len() - 1),
LeapSecondPolicy::FrozenAfter { jdn } => {
let count = self.entries.partition_point(|e| e.jdn <= jdn);
count.checked_sub(1)
}
}
}
pub fn delta_at_for_jdn(&self, jdn: i64) -> Option<i32> {
let lookup = match self.policy {
LeapSecondPolicy::TableDriven => jdn,
LeapSecondPolicy::FrozenAfter { jdn: freeze } => jdn.min(freeze),
};
let idx = self.entries.partition_point(|e| e.jdn <= lookup);
if idx == 0 {
None
} else {
Some(self.entries[idx - 1].delta_at)
}
}
}
const SOFA_ROWS: &[(i64, f64, f64, f64)] = &[
(2_436_935, 1.417_818_0, 37_300.0, 0.001_296_0), (2_437_301, 1.422_818_0, 37_300.0, 0.001_296_0), (2_437_513, 1.372_818_0, 37_300.0, 0.001_296_0), (2_437_666, 1.845_858_0, 37_665.0, 0.001_123_2), (2_438_335, 1.945_858_0, 37_665.0, 0.001_123_2), (2_438_396, 3.240_130_0, 38_761.0, 0.001_296_0), (2_438_487, 3.340_130_0, 38_761.0, 0.001_296_0), (2_438_640, 3.440_130_0, 38_761.0, 0.001_296_0), (2_438_762, 3.540_130_0, 38_761.0, 0.001_296_0), (2_438_821, 3.640_130_0, 38_761.0, 0.001_296_0), (2_438_943, 3.740_130_0, 38_761.0, 0.001_296_0), (2_439_005, 3.840_130_0, 38_761.0, 0.001_296_0), (2_439_127, 4.313_170_0, 39_126.0, 0.002_592_0), (2_439_888, 4.213_170_0, 39_126.0, 0.002_592_0), ];
pub fn sofa_delta_at(mjd: f64) -> Option<f64> {
let mut value = None;
for &(start_jdn, base, reference_mjd, drift) in SOFA_ROWS {
let start_mjd = (start_jdn - 2_400_001) as f64;
if mjd < start_mjd {
break;
}
value = Some(base + (mjd - reference_mjd) * drift);
}
value
}