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