Skip to main content

deep_time/dt/
ops.rs

1use crate::{Dt, Real};
2use core::cmp::Ordering;
3use core::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign};
4
5impl Add<Dt> for Dt {
6    type Output = Self;
7
8    #[inline]
9    fn add(self, rhs: Dt) -> Self {
10        self.add(rhs)
11    }
12}
13
14impl AddAssign<Dt> for Dt {
15    #[inline]
16    fn add_assign(&mut self, rhs: Dt) {
17        *self = self.add(rhs);
18    }
19}
20
21impl Sub<Dt> for Dt {
22    type Output = Self;
23
24    #[inline]
25    fn sub(self, rhs: Dt) -> Self {
26        self.sub(rhs)
27    }
28}
29
30impl SubAssign<Dt> for Dt {
31    #[inline]
32    fn sub_assign(&mut self, rhs: Dt) {
33        *self = self.sub(rhs);
34    }
35}
36
37impl Neg for Dt {
38    type Output = Self;
39
40    /// Negates this `Dt` (returns the additive inverse).
41    #[inline]
42    fn neg(self) -> Self {
43        self.neg()
44    }
45}
46
47impl Mul<i64> for Dt {
48    type Output = Self;
49
50    /// Multiplies this `Dt` by an integer scalar.
51    #[inline]
52    fn mul(self, rhs: i64) -> Self {
53        self.mul(rhs)
54    }
55}
56
57impl MulAssign<i64> for Dt {
58    /// Multiplies this `Dt` by an integer scalar in place.
59    #[inline]
60    fn mul_assign(&mut self, rhs: i64) {
61        *self = self.mul(rhs);
62    }
63}
64
65impl Div<i64> for Dt {
66    type Output = Self;
67
68    /// Divides this `Dt` by an integer scalar.
69    #[inline]
70    fn div(self, rhs: i64) -> Self {
71        self.div(rhs)
72    }
73}
74
75impl DivAssign<i64> for Dt {
76    /// Divides this `Dt` by an integer scalar in place.
77    #[inline]
78    fn div_assign(&mut self, rhs: i64) {
79        *self = self.div(rhs);
80    }
81}
82
83impl Mul<f64> for Dt {
84    type Output = Self;
85
86    #[inline]
87    fn mul(self, rhs: f64) -> Self {
88        self.mul_by_f(rhs)
89    }
90}
91
92impl MulAssign<f64> for Dt {
93    #[inline]
94    fn mul_assign(&mut self, rhs: f64) {
95        *self = self.mul_by_f(rhs);
96    }
97}
98
99impl Div<f64> for Dt {
100    type Output = Self;
101
102    #[inline]
103    fn div(self, rhs: f64) -> Self {
104        self.div_by_f(rhs)
105    }
106}
107
108impl DivAssign<f64> for Dt {
109    #[inline]
110    fn div_assign(&mut self, rhs: f64) {
111        *self = self.div_by_f(rhs);
112    }
113}
114
115impl Mul<Dt> for i64 {
116    type Output = Dt;
117
118    #[inline]
119    fn mul(self, rhs: Dt) -> Dt {
120        rhs.mul(self)
121    }
122}
123
124impl Mul<Dt> for f64 {
125    type Output = Dt;
126
127    #[inline]
128    fn mul(self, rhs: Dt) -> Dt {
129        rhs.mul_by_f(self)
130    }
131}
132
133impl Div<Dt> for Dt {
134    type Output = Real;
135
136    #[inline]
137    fn div(self, rhs: Dt) -> Real {
138        self.div_dt(rhs)
139    }
140}
141
142impl Dt {
143    /// Compares the time values represented by two `Dt`s.
144    ///
145    /// - This comparison is based on the raw `(sec, attos)` representation
146    ///   after normalizing (without mutating self or other) any un-carried
147    ///   attoseconds.
148    /// - Does **not** perform scale conversion.
149    pub const fn cmp(&self, other: &Self) -> Ordering {
150        let a = self.carry_attos();
151        let b = other.carry_attos();
152
153        if a.sec < b.sec {
154            Ordering::Less
155        } else if a.sec > b.sec {
156            Ordering::Greater
157        } else if a.attos < b.attos {
158            Ordering::Less
159        } else if a.attos > b.attos {
160            Ordering::Greater
161        } else {
162            Ordering::Equal
163        }
164    }
165
166    /// Returns the smaller of two `Dt`s according to the total physical-time order
167    /// defined by [`Self::cmp`].
168    ///
169    /// This is a `const fn` and can be used in const contexts.
170    #[inline]
171    pub const fn min(self, other: Self) -> Self {
172        match self.cmp(&other) {
173            Ordering::Greater => other,
174            _ => self,
175        }
176    }
177
178    /// Returns the larger of two `Dt`s according to the total physical-time order
179    /// defined by [`Self::cmp`].
180    ///
181    /// See [`Self::min`] for more details.
182    #[inline]
183    pub const fn max(self, other: Self) -> Self {
184        match self.cmp(&other) {
185            Ordering::Less => other,
186            _ => self,
187        }
188    }
189
190    /// True if both sides have matching `sec` and `attos` fields.
191    ///
192    /// This is a `const fn` so it can be used in const contexts.
193    #[inline]
194    pub const fn eq(&self, other: &Self) -> bool {
195        matches!(Dt::cmp(self, other), Ordering::Equal)
196    }
197
198    /// Returns `true` if this `Dt` is less than the other.
199    ///
200    /// This is a `const fn` so it can be used in const contexts.
201    pub const fn lt(&self, other: &Self) -> bool {
202        matches!(self.cmp(other), Ordering::Less)
203    }
204
205    /// Returns `true` if this `Dt` is greater than the other.
206    ///
207    /// This is a `const fn` so it can be used in const contexts.
208    pub const fn gt(&self, other: &Self) -> bool {
209        matches!(self.cmp(other), Ordering::Greater)
210    }
211
212    /// Returns `true` if this `Dt` is less than or equal to the other.
213    ///
214    /// This is a `const fn` so it can be used in const contexts.
215    pub const fn le(&self, other: &Self) -> bool {
216        !matches!(self.cmp(other), Ordering::Greater)
217    }
218
219    /// Returns `true` if this `Dt` is greater than or equal to the other.
220    ///
221    /// This is a `const fn` so it can be used in const contexts.
222    pub const fn ge(&self, other: &Self) -> bool {
223        !matches!(self.cmp(other), Ordering::Less)
224    }
225}
226
227impl PartialEq for Dt {
228    #[inline]
229    fn eq(&self, other: &Self) -> bool {
230        Dt::eq(self, other)
231    }
232}
233
234impl Eq for Dt {}
235
236impl PartialOrd for Dt {
237    #[inline]
238    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
239        Some(Dt::cmp(self, other))
240    }
241}
242
243impl Ord for Dt {
244    #[inline]
245    fn cmp(&self, other: &Self) -> Ordering {
246        Dt::cmp(self, other)
247    }
248}
249
250impl core::hash::Hash for Dt {
251    /// Hashes the canonical TAI representation so that two `Dt`s that are
252    /// physically equal (after conversion) produce the same hash, regardless of
253    /// the original [`Scale`].
254    fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
255        self.sec.hash(state);
256        self.attos.hash(state);
257    }
258}