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#[macro_export]
28macro_rules! f {
29    ($x:expr) => {
30        $x as $crate::Real
31    };
32}
33
34/// Safe Euclidean division.
35/// Returns `default` if `rhs == 0` or if `lhs == i128::MIN && rhs == -1`.
36macro_rules! safe_div_euc {
37    ($lhs:expr, $rhs:expr, $default:expr) => {{
38        match ($lhs).checked_div_euclid($rhs) {
39            Some(q) => q,
40            None => $default,
41        }
42    }};
43}
44
45/// Safe Euclidean remainder.
46/// Returns `$default` if `rhs == 0` or if `lhs == Self::MIN && rhs == -1`.
47macro_rules! safe_rem_euc {
48    ($lhs:expr, $rhs:expr, $default:expr) => {{
49        match ($lhs).checked_rem_euclid($rhs) {
50            Some(r) => r,
51            None => $default,
52        }
53    }};
54}
55
56#[inline(always)]
57pub const fn to_sec_f(attos: u128) -> Real {
58    f!(attos) / ATTOS_PER_SECF
59}
60
61/// Converts attoseconds → seconds (s)
62#[inline(always)]
63pub fn to_sec(attos: i128) -> i128 {
64    attos / ATTOS_PER_SEC_I128
65}
66
67/// Converts attoseconds → milliseconds (ms)
68#[inline(always)]
69pub fn to_ms(attos: i128) -> i128 {
70    attos / ATTOS_PER_MS_I128
71}
72
73/// Converts attoseconds → microseconds (us)
74#[inline(always)]
75pub fn to_us(attos: i128) -> i128 {
76    attos / ATTOS_PER_US_I128
77}
78
79/// Converts attoseconds → nanoseconds (ns)
80#[inline(always)]
81pub fn to_ns(attos: i128) -> i128 {
82    attos / ATTOS_PER_NS_I128
83}
84
85/// Converts attoseconds → picoseconds (ps)
86#[inline(always)]
87pub fn to_ps(attos: i128) -> i128 {
88    attos / ATTOS_PER_PS_I128
89}
90
91/// Converts attoseconds → femtoseconds (fs)
92#[inline(always)]
93pub fn to_fs(attos: i128) -> i128 {
94    attos / ATTOS_PER_FS_I128
95}
96
97// _________________________________________
98// FEATURE MOD
99// _________________________________________
100#[cfg(feature = "parse")]
101mod alloc_parse;
102
103#[cfg(feature = "sidereal")]
104pub mod sidereal;
105
106// _________________________________________
107// MOD
108// _________________________________________
109mod an_err;
110mod ascii_str;
111mod clock_model;
112mod drift;
113mod dt;
114mod gregorian_time;
115mod light_time;
116mod parser;
117mod position;
118mod scale;
119mod time_parts;
120mod time_range;
121
122// _________________________________________
123// PUB MOD
124// _________________________________________
125pub mod constants;
126pub mod error;
127pub mod historical_sofa;
128pub mod leap_seconds;
129pub mod math;
130pub mod tzdb;
131
132// _________________________________________
133// FEATURE PUB MOD
134// _________________________________________
135#[cfg(feature = "bop")]
136pub mod bop;
137
138// _________________________________________
139// FEATURE CRATE USE
140// _________________________________________
141#[cfg(feature = "parse")]
142pub(crate) use alloc_parse::{
143    alloc_constants::*, date::*, date_classification::*, duration::*, lang_data::*, lang_map::*,
144    languages::en::*, parse_date::*, types::*,
145};
146
147// _________________________________________
148// CRATE USE
149// _________________________________________
150pub(crate) use constants::*;
151
152// _________________________________________
153// FEATURE PUB USE
154// _________________________________________
155#[cfg(feature = "parse")]
156pub use alloc_parse::types::{DateOrder, DateParseMode, Lang, ParseCfg};
157
158#[cfg(feature = "wire")]
159pub use an_err::{WireErr, WireLocation};
160
161#[cfg(feature = "sidereal")]
162pub use sidereal::Sidereal;
163
164// _________________________________________
165// PUB USE
166// _________________________________________
167pub use an_err::AnErr;
168pub use ascii_str::{AsciiStr, AsciiStrError};
169pub use clock_model::ClockModel;
170pub use drift::{Drift, Spacetime};
171pub use dt::Dt;
172pub use dt::numbers_traits::{AttosTraits, TimeTraits};
173pub use error::{DtErr, DtErrKind};
174pub use gregorian_time::{GregorianTime, YmdHms};
175pub use light_time::ObserverState;
176pub use math::{
177    atan::atan,
178    atan2::atan2,
179    cos::cos,
180    floor::floor_f,
181    log::log,
182    sin::sin,
183    sqrt::{hypot, sqrt},
184    tan::tan,
185};
186pub use position::{Position, Velocity};
187pub use scale::Scale;
188pub use time_parts::{Meridiem, Offset, TimeParts, Weekday};
189pub use time_range::{Every, TimeRange};