use crate::data::active::{active_time_data, time_data_eop_at};
use crate::eop::EopValues;
use qtty::{Day, Second};
use std::sync::Arc;
use tempoch_time_data::TimeDataBundle;
#[derive(Debug, Clone)]
pub struct TimeContext {
data: Arc<TimeDataBundle>,
eop: EopSource,
utc_pre_definition: bool,
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
enum EopSource {
#[default]
None,
Builtin,
}
impl Default for TimeContext {
#[inline]
fn default() -> Self {
Self::new()
}
}
impl TimeContext {
#[inline]
fn snapshot(eop: EopSource) -> Self {
Self {
data: active_time_data(),
eop,
utc_pre_definition: false,
}
}
#[inline]
pub fn new() -> Self {
Self::snapshot(EopSource::None)
}
#[inline]
pub fn with_builtin_eop() -> Self {
Self::snapshot(EopSource::Builtin)
}
#[inline]
pub(crate) fn time_data(&self) -> &TimeDataBundle {
self.data.as_ref()
}
#[inline]
pub fn allow_pre_definition_utc(mut self) -> Self {
self.utc_pre_definition = true;
self
}
#[inline]
pub(crate) fn allows_pre_definition_utc(&self) -> bool {
self.utc_pre_definition
}
#[inline]
pub fn eop_at(&self, mjd_utc: Day) -> Option<EopValues> {
match self.eop {
EopSource::None => None,
EopSource::Builtin => time_data_eop_at(self.time_data(), mjd_utc),
}
}
#[inline]
pub fn ut1_minus_utc(&self, mjd_utc: Day) -> Option<Second> {
self.eop_at(mjd_utc).map(|v| v.ut1_minus_utc)
}
}