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 = "sidereal")]
108pub mod sidereal;
109
110// _________________________________________
111// MOD
112// _________________________________________
113mod an_err;
114mod ascii_str;
115mod clock_model;
116mod drift;
117mod dt;
118mod gregorian_time;
119mod light_time;
120mod parser;
121mod position;
122mod scale;
123mod time_parts;
124mod time_range;
125
126// _________________________________________
127// PUB MOD
128// _________________________________________
129pub mod constants;
130pub mod error;
131pub mod historical_sofa;
132pub mod leap_seconds;
133pub mod math;
134pub mod tzdb;
135
136// _________________________________________
137// FEATURE PUB MOD
138// _________________________________________
139#[cfg(feature = "bop")]
140pub mod bop;
141
142// _________________________________________
143// FEATURE CRATE USE
144// _________________________________________
145#[cfg(feature = "parse")]
146pub(crate) use alloc_parse::{
147    alloc_constants::*, date::*, date_classification::*, duration::*, lang_data::*, lang_map::*,
148    languages::en::*, parse_date::*, types::*,
149};
150
151// _________________________________________
152// CRATE USE
153// _________________________________________
154pub(crate) use constants::*;
155
156// _________________________________________
157// FEATURE PUB USE
158// _________________________________________
159#[cfg(feature = "parse")]
160pub use alloc_parse::types::{DateOrder, DateParseMode, Lang, ParseCfg};
161
162#[cfg(feature = "wire")]
163pub use an_err::{WireErr, WireLocation};
164
165#[cfg(feature = "sidereal")]
166pub use sidereal::Sidereal;
167
168#[cfg(feature = "mars")]
169pub use dt::mars;
170
171// _________________________________________
172// PUB USE
173// _________________________________________
174pub use an_err::AnErr;
175pub use ascii_str::{AsciiStr, AsciiStrError};
176pub use clock_model::ClockModel;
177pub use drift::{Drift, Spacetime};
178pub use dt::numbers_traits::{AttosTraits, TimeTraits};
179pub use dt::{Dt, lunar};
180pub use error::{DtErr, DtErrKind};
181pub use gregorian_time::{GregorianTime, YmdHms};
182pub use light_time::ObserverState;
183pub use math::{
184    atan::atan,
185    atan2::atan2,
186    cos::cos,
187    floor::floor_f,
188    log::log,
189    sin::sin,
190    sqrt::{hypot, sqrt},
191    tan::tan,
192};
193pub use position::{Position, Velocity};
194pub use scale::Scale;
195pub use time_parts::{Meridiem, Offset, TimeParts, Weekday};
196pub use time_range::{Every, TimeRange};