Skip to main content

tempoch_core/
lib.rs

1// SPDX-License-Identifier: AGPL-3.0-only
2// Copyright (C) 2026 Vallés Puig, Ramon
3
4//! Typed astronomical time primitives.
5//!
6//! The central type is [`Time<S>`], where `S` is a [`Scale`] marker
7//! (`TT`, `TAI`, `UTC`, `UT1`, `TDB`, `TCG`, `TCB`).
8//!
9//! `tempoch` makes a few explicit modeling decisions:
10//!
11//! - [`Time<S>`] is an instant on a scale-specific axis, not a bare scalar.
12//! - Time arithmetic follows affine rules: instant minus instant yields a
13//!   duration; shifting an instant by a duration yields another instant.
14//! - Internal storage is a compensated `(hi, lo)` pair of J2000-based seconds
15//!   so large epoch values can retain small corrections and sub-second detail.
16//! - `JD`, `MJD`, `J2000s`, `Unix`, and `GPS` are conversion targets,
17//!   not independent storage models.
18//! - `UTC` keeps special civil semantics: it is stored as a continuous instant
19//!   and interpreted through the active UTC-TAI table when civil labels are
20//!   needed.
21//!
22//! The old format-generic storage model has been replaced with explicit
23//! constructors and accessors:
24//!
25//! - built-in coordinate scales expose J2000-second, JD, and MJD
26//!   constructors/accessors
27//! - `UTC` exposes both raw instant-axis helpers and civil/transport APIs
28//!   (`chrono`, POSIX)
29//! - `TAI` exposes GPS transport helpers
30//! - unified conversion targets are available through `time.to::<Target>()`,
31//!   `time.try_to::<Target>()`, and `time.to_with::<Target>(&ctx)`
32//!
33//! See [`constats`] for typed epoch and offset constants.
34
35mod civil;
36pub mod constats;
37mod context;
38mod data;
39mod delta_t;
40pub(crate) mod encoding;
41pub mod eop;
42pub mod error;
43pub(crate) mod generated;
44mod interval;
45pub mod representation;
46pub mod scalar;
47mod scale;
48mod sealed;
49mod target;
50mod time;
51
52#[cfg(feature = "serde")]
53#[path = "serde.rs"]
54mod serde_impl;
55#[cfg(feature = "serde")]
56pub mod tagged;
57
58pub use constats::{
59    GPS_EPOCH_JD_TAI, GPS_EPOCH_JD_UTC, GPS_EPOCH_TAI_MINUS_UTC, UTC_DEFINED_FROM_MJD,
60};
61pub use context::TimeContext;
62#[cfg(feature = "runtime-data-fetch")]
63pub use data::active::{
64    fetch_latest_time_data, refresh_runtime_time_data, update_runtime_time_data,
65};
66pub use delta_t::{delta_t_seconds, delta_t_seconds_extrapolated, DELTA_T_PREDICTION_HORIZON_MJD};
67pub use error::{ConversionError, TimeDataError};
68pub use generated::{
69    EOP_END_MJD, EOP_OBSERVED_END_MJD, EOP_START_MJD, MODERN_DELTA_T_OBSERVED_END_MJD,
70};
71pub use interval::{Interval, InvalidIntervalError, InvalidPeriodError, Period, PeriodListError};
72pub use representation::{
73    EncodedTime, GpsTime, InfallibleRepresentationForScale, J2000Seconds, J2000s, JulianDate,
74    ModifiedJulianDate, RepresentationForScale, TimeRepresentation, Unix, UnixTime, GPS, JD, MJD,
75};
76pub use scalar::{
77    scalar_add_days, scalar_difference_in_days, time_tt_from_scalar, time_tt_to_scalar, ScaleKind,
78};
79pub use scale::{ContinuousScale, CoordinateScale, Scale, TAI, TCB, TCG, TDB, TT, UT1, UTC};
80pub use target::{ContextConversionTarget, ConversionTarget, InfallibleConversionTarget};
81pub use time::Time;
82
83#[cfg(test)]
84mod size_tests {
85    use super::*;
86    #[test]
87    fn time_uses_compensated_pair_storage() {
88        assert_eq!(core::mem::size_of::<Time<TT>>(), 16);
89        assert_eq!(core::mem::size_of::<Time<TAI>>(), 16);
90        assert_eq!(core::mem::size_of::<Time<TDB>>(), 16);
91        assert_eq!(core::mem::size_of::<Time<TCG>>(), 16);
92        assert_eq!(core::mem::size_of::<Time<TCB>>(), 16);
93        assert_eq!(core::mem::size_of::<Time<UT1>>(), 16);
94        assert_eq!(core::mem::size_of::<Time<UTC>>(), 16);
95    }
96}