Skip to main content

deep_time/
lib.rs

1#![cfg_attr(test, allow(clippy::all))]
2#![cfg_attr(not(feature = "std"), no_std)]
3
4#[cfg(feature = "alloc")]
5extern crate alloc;
6#[cfg(feature = "std")]
7extern crate std;
8
9// ──────────────────────────────────────────────────────────────
10// Optional panic handler (opt-in via feature)
11// ──────────────────────────────────────────────────────────────
12#[cfg(all(feature = "panic-handler", not(feature = "std"), not(test)))]
13use core::panic::PanicInfo;
14
15#[cfg(all(feature = "panic-handler", not(feature = "std"), not(test)))]
16#[panic_handler]
17fn panic(_info: &PanicInfo) -> ! {
18    // Uses spin_loop() for better power characteristics than plain loop{}
19    loop {
20        core::hint::spin_loop();
21    }
22}
23
24// maybe one day upgrade to f128 ¯\_(ツ)_/¯
25pub type Real = f64;
26
27/// Convert a number to the crates [`Real`] type (f64).
28///
29/// Equivalent to `n as f64`.
30#[macro_export]
31macro_rules! f {
32    ($x:expr) => {
33        $x as $crate::Real
34    };
35}
36
37/// Safe Euclidean division.
38/// Returns `default` if `rhs == 0` or if `lhs == i128::MIN && rhs == -1`.
39macro_rules! safe_div_euc {
40    ($lhs:expr, $rhs:expr, $default:expr) => {{
41        match ($lhs).checked_div_euclid($rhs) {
42            Some(q) => q,
43            None => $default,
44        }
45    }};
46}
47
48/// Safe Euclidean remainder.
49/// Returns `$default` if `rhs == 0` or if `lhs == Self::MIN && rhs == -1`.
50macro_rules! safe_rem_euc {
51    ($lhs:expr, $rhs:expr, $default:expr) => {{
52        match ($lhs).checked_rem_euclid($rhs) {
53            Some(r) => r,
54            None => $default,
55        }
56    }};
57}
58
59/// **Lossy** conversion of attoseconds to → float seconds (s).
60#[inline(always)]
61pub const fn to_sec_f(attos: u128) -> Real {
62    f!(attos) / ATTOS_PER_SECF
63}
64
65/// Converts attoseconds → seconds (s)
66#[inline(always)]
67pub fn to_sec(attos: i128) -> i128 {
68    attos / ATTOS_PER_SEC_I128
69}
70
71/// Converts attoseconds → milliseconds (ms)
72#[inline(always)]
73pub fn to_ms(attos: i128) -> i128 {
74    attos / ATTOS_PER_MS_I128
75}
76
77/// Converts attoseconds → microseconds (us)
78#[inline(always)]
79pub fn to_us(attos: i128) -> i128 {
80    attos / ATTOS_PER_US_I128
81}
82
83/// Converts attoseconds → nanoseconds (ns)
84#[inline(always)]
85pub fn to_ns(attos: i128) -> i128 {
86    attos / ATTOS_PER_NS_I128
87}
88
89/// Converts attoseconds → picoseconds (ps)
90#[inline(always)]
91pub fn to_ps(attos: i128) -> i128 {
92    attos / ATTOS_PER_PS_I128
93}
94
95/// Converts attoseconds → femtoseconds (fs)
96#[inline(always)]
97pub fn to_fs(attos: i128) -> i128 {
98    attos / ATTOS_PER_FS_I128
99}
100
101// _________________________________________
102// FEATURE MOD
103// _________________________________________
104#[cfg(feature = "parse")]
105mod alloc_parse;
106
107#[cfg(feature = "wire")]
108mod wire;
109
110// _________________________________________
111// MOD
112// _________________________________________
113mod an_err;
114mod drift;
115mod dt;
116mod light_time;
117mod lite_str;
118mod parser;
119mod position;
120mod scale;
121mod time_parts;
122mod time_range;
123mod ymdhms;
124
125// _________________________________________
126// PUB MOD
127// _________________________________________
128pub mod constants;
129pub mod error;
130pub mod historical_sofa;
131pub mod leap_seconds;
132pub mod math;
133pub mod tzdb;
134
135// _________________________________________
136// FEATURE PUB MOD
137// _________________________________________
138#[cfg(feature = "eop")]
139pub mod eop;
140
141#[cfg(feature = "sidereal")]
142pub mod sidereal;
143
144// _________________________________________
145// FEATURE CRATE USE
146// _________________________________________
147#[cfg(feature = "parse")]
148pub(crate) use alloc_parse::{
149    alloc_constants::*, date::*, date_classification::*, duration::*, lang_data::*, lang_map::*,
150    languages::en::*, parse_date::*, types::*,
151};
152
153// _________________________________________
154// CRATE USE
155// _________________________________________
156pub(crate) use constants::*;
157
158// _________________________________________
159// FEATURE PUB USE
160// _________________________________________
161#[cfg(feature = "parse")]
162pub use alloc_parse::types::{Lang, Mode, Order, ParseCfg};
163
164#[cfg(feature = "wire")]
165pub use an_err::{WireErr, WireLocation};
166
167#[cfg(feature = "sidereal")]
168pub use sidereal::Sidereal;
169
170#[cfg(feature = "mars")]
171pub use dt::mars;
172
173// _________________________________________
174// PUB USE
175// _________________________________________
176pub use an_err::AnErr;
177pub use drift::{Drift, Spacetime};
178pub use dt::numbers_traits::{AttosTraits, TimeTraits};
179pub use dt::{Dt, lunar};
180pub use error::{DtErr, DtErrKind};
181pub use light_time::ObserverState;
182pub use lite_str::{LiteStr, LiteStrErr};
183pub use math::{
184    atan::atan,
185    atan2::atan2,
186    cos::cos,
187    div::rem_euclid_f,
188    floor::floor_f,
189    log::log,
190    sin::sin,
191    sqrt::{hypot, sqrt},
192    tan::tan,
193};
194pub use parser::StrPTimeFmt;
195pub use position::{Position, Velocity};
196pub use scale::Scale;
197pub use time_parts::{Meridiem, Offset, TimeParts, Weekday};
198pub use time_range::{Every, TimeRange};
199pub use ymdhms::{YmdHms, YmdHmsRich};