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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
use crate::{
ATTOS_PER_FS, ATTOS_PER_MS, ATTOS_PER_NS, ATTOS_PER_PS, ATTOS_PER_SEC_I128, ATTOS_PER_SECF,
ATTOS_PER_US, ClockDrift, Dt, LocalSpacetime, Real, TSpan,
};
impl Dt {
#[inline]
pub const fn add(self, span: TSpan) -> Self {
let (sec, attos) = TSpan::add_time(self.sec, self.attos, span.sec, span.attos);
Self { sec, attos }
}
#[inline]
pub const fn sub(self, span: TSpan) -> Self {
let (sec, attos) = TSpan::sub_time(self.sec, self.attos, span.sec, span.attos);
Self { sec, attos }
}
/// Converts this `Dt` to a floating-point number of seconds since the reference epoch of its associated scale.
///
/// The conversion is lossy by design, as `f64` (`Real`) provides approximately 15.95 decimal digits of precision.
/// For full exactness, use the integer components `sec` and `attos` directly or higher-precision arithmetic when available.
#[inline]
pub const fn to_sec_f(self) -> Real {
f!(self.sec) + f!(self.attos) / ATTOS_PER_SECF
}
/// Advances this `Dt` by the given elapsed duration while applying the relativistic proper-time correction
/// derived from the supplied `LocalSpacetime` model.
///
/// This method is intended for simulation of remote clocks (e.g., Earth time as observed from a spacecraft).
/// For the spacecraft's own hardware proper-time clock, use the plain `add` method instead.
#[inline]
pub const fn adjusted_advance(&mut self, elapsed: &TSpan, local_spacetime: &LocalSpacetime) {
let dtau =
elapsed.add(ClockDrift::from_local_spacetime(local_spacetime).time_diff_after(elapsed));
*self = self.add(dtau);
}
/// Advances this `Dt` by the given elapsed duration while applying the relativistic proper-time correction
/// from a pre-computed `ClockDrift` value.
///
/// This is an optimized variant of `adjusted_advance` for callers that already hold a `ClockDrift` instance.
/// It is intended for simulation of remote clocks; the spacecraft's own hardware clock should use the plain `add` method.
#[inline]
pub const fn adjusted_advance_using_drift(&mut self, elapsed: &TSpan, drift: &ClockDrift) {
let dtau = elapsed.add(drift.time_diff_after(elapsed));
*self = self.add(dtau);
}
/// Computes the TAI signed duration between this `Dt` and an earlier instant.
#[inline]
pub const fn to_tai_since(&self, earlier: Self) -> TSpan {
TSpan::diff_raw(self.sec, self.attos, earlier.sec, earlier.attos)
}
/// This method is lossy by design and is provided for testing and debugging purposes only.
/// For the exact duration, use `duration_since` or `duration_since_ref`.
#[inline]
pub const fn to_tai_since_f(&self, other: Self) -> Real {
self.to_sec_f() - other.to_sec_f()
}
/// Adds exactly 1 second to this time value using saturating arithmetic.
#[inline]
pub const fn add_1sec(&mut self) {
self.sec = self.sec.saturating_add(1);
}
/// Adds exactly 1 minute (60 seconds) to this time value using saturating arithmetic.
#[inline]
pub const fn add_1min(&mut self) {
self.sec = self.sec.saturating_add(60);
}
/// Adds exactly 1 hour (3600 seconds) to this time value using saturating arithmetic.
#[inline]
pub const fn add_1hr(&mut self) {
self.sec = self.sec.saturating_add(3600);
}
/// Adds exactly 1 millisecond to this time value.
///
/// This affects the subsecond component and may cause a carry into the seconds field.
#[inline]
pub const fn add_1ms(&mut self) {
TSpan::add_attos_to(&mut self.sec, &mut self.attos, ATTOS_PER_MS);
}
/// Adds exactly 1 microsecond to this time value.
///
/// This affects the subsecond component and may cause a carry into the seconds field.
#[inline]
pub const fn add_1us(&mut self) {
TSpan::add_attos_to(&mut self.sec, &mut self.attos, ATTOS_PER_US);
}
/// Adds exactly 1 nanosecond to this time value.
///
/// This affects the subsecond component and may cause a carry into the seconds field.
#[inline]
pub const fn add_1ns(&mut self) {
TSpan::add_attos_to(&mut self.sec, &mut self.attos, ATTOS_PER_NS);
}
/// Adds the specified number of seconds to this time value using saturating arithmetic.
#[inline]
pub const fn add_sec(&mut self, n: i64) {
self.sec = self.sec.saturating_add(n);
}
/// Adds the specified number of minutes to this time value using saturating arithmetic.
#[inline]
pub const fn add_min(&mut self, n: i64) {
self.sec = self.sec.saturating_add(n.saturating_mul(60));
}
/// Adds the specified number of hours to this time value using saturating arithmetic.
#[inline]
pub const fn add_hr(&mut self, n: i64) {
self.sec = self.sec.saturating_add(n.saturating_mul(3600));
}
/// Adds the specified number of milliseconds to this time value.
///
/// Handles carry into the seconds field using saturating logic.
#[inline]
pub const fn add_ms(&mut self, n: i64) {
TSpan::add_attos_span(&mut self.sec, &mut self.attos, n, ATTOS_PER_MS);
}
/// Adds the specified number of microseconds to this time value.
///
/// Handles carry into the seconds field using saturating logic.
#[inline]
pub const fn add_us(&mut self, n: i64) {
TSpan::add_attos_span(&mut self.sec, &mut self.attos, n, ATTOS_PER_US);
}
/// Adds the specified number of nanoseconds to this time value.
///
/// Handles carry into the seconds field using saturating logic.
#[inline]
pub const fn add_ns(&mut self, n: i64) {
TSpan::add_attos_span(&mut self.sec, &mut self.attos, n, ATTOS_PER_NS);
}
/// Adds the specified number of picoseconds to this time value.
///
/// Handles carry into the seconds field using saturating logic.
#[inline]
pub const fn add_ps(&mut self, n: i64) {
TSpan::add_attos_span(&mut self.sec, &mut self.attos, n, ATTOS_PER_PS);
}
/// Adds the specified number of femtoseconds to this time value.
///
/// Handles carry into the seconds field using saturating logic.
#[inline]
pub const fn add_fs(&mut self, n: i64) {
TSpan::add_attos_span(&mut self.sec, &mut self.attos, n, ATTOS_PER_FS);
}
/// Adds the specified number of attoseconds to this time value.
///
/// Handles carry into the seconds field using saturating logic.
#[inline]
pub const fn add_attos(&mut self, n: i64) {
TSpan::add_attos_span(&mut self.sec, &mut self.attos, n, 1);
}
// =====================================================================
// Single-unit subtraction methods (convenience methods for -1)
// =====================================================================
/// Subtracts exactly 1 hour (3600 seconds) from this time value using saturating arithmetic.
#[inline]
pub const fn sub_1hr(&mut self) {
self.sec = self.sec.saturating_sub(3600);
}
/// Subtracts exactly 1 minute (60 seconds) from this time value using saturating arithmetic.
#[inline]
pub const fn sub_1min(&mut self) {
self.sec = self.sec.saturating_sub(60);
}
/// Subtracts exactly 1 second from this time value using saturating arithmetic.
#[inline]
pub const fn sub_1sec(&mut self) {
self.sec = self.sec.saturating_sub(1);
}
/// Subtracts exactly 1 millisecond from this time value.
///
/// This affects the subsecond component and may cause a borrow from the seconds field.
#[inline]
pub const fn sub_1ms(&mut self) {
TSpan::add_attos_span(&mut self.sec, &mut self.attos, -1, ATTOS_PER_MS);
}
/// Subtracts exactly 1 microsecond from this time value.
///
/// This affects the subsecond component and may cause a borrow from the seconds field.
#[inline]
pub const fn sub_1us(&mut self) {
TSpan::add_attos_span(&mut self.sec, &mut self.attos, -1, ATTOS_PER_US);
}
/// Subtracts exactly 1 nanosecond from this time value.
///
/// This affects the subsecond component and may cause a borrow from the seconds field.
#[inline]
pub const fn sub_1ns(&mut self) {
TSpan::add_attos_span(&mut self.sec, &mut self.attos, -1, ATTOS_PER_NS);
}
/// Subtracts the specified number of seconds from this time value using saturating arithmetic.
#[inline]
pub const fn sub_sec(&mut self, n: i64) {
self.sec = self.sec.saturating_sub(n);
}
/// Subtracts the specified number of minutes from this time value using saturating arithmetic.
#[inline]
pub const fn sub_min(&mut self, n: i64) {
self.sec = self.sec.saturating_sub(n.saturating_mul(60));
}
/// Subtracts the specified number of hours from this time value using saturating arithmetic.
#[inline]
pub const fn sub_hr(&mut self, n: i64) {
self.sec = self.sec.saturating_sub(n.saturating_mul(3600));
}
/// Subtracts the specified number of milliseconds from this time value.
///
/// Handles borrow from the seconds field using saturating logic.
#[inline]
pub const fn sub_ms(&mut self, n: i64) {
TSpan::add_attos_span(
&mut self.sec,
&mut self.attos,
n.saturating_neg(),
ATTOS_PER_MS,
);
}
/// Subtracts the specified number of microseconds from this time value.
///
/// Handles borrow from the seconds field using saturating logic.
#[inline]
pub const fn sub_us(&mut self, n: i64) {
TSpan::add_attos_span(
&mut self.sec,
&mut self.attos,
n.saturating_neg(),
ATTOS_PER_US,
);
}
/// Subtracts the specified number of nanoseconds from this time value.
///
/// Handles borrow from the seconds field using saturating logic.
#[inline]
pub const fn sub_ns(&mut self, n: i64) {
TSpan::add_attos_span(
&mut self.sec,
&mut self.attos,
n.saturating_neg(),
ATTOS_PER_NS,
);
}
/// Subtracts the specified number of picoseconds from this time value.
///
/// Handles borrow from the seconds field using saturating logic.
#[inline]
pub const fn sub_ps(&mut self, n: i64) {
TSpan::add_attos_span(
&mut self.sec,
&mut self.attos,
n.saturating_neg(),
ATTOS_PER_PS,
);
}
/// Subtracts the specified number of femtoseconds from this time value.
///
/// Handles borrow from the seconds field using saturating logic.
#[inline]
pub const fn sub_fs(&mut self, n: i64) {
TSpan::add_attos_span(
&mut self.sec,
&mut self.attos,
n.saturating_neg(),
ATTOS_PER_FS,
);
}
/// Subtracts the specified number of attoseconds from this time value.
///
/// Handles borrow from the seconds field using saturating logic.
#[inline]
pub const fn sub_attos(&mut self, n: i64) {
TSpan::add_attos_span(&mut self.sec, &mut self.attos, n.saturating_neg(), 1);
}
/// Total attoseconds (exact i128 representation within the representable range).
#[inline]
pub const fn to_attos(self) -> i128 {
(self.sec as i128) * ATTOS_PER_SEC_I128 + (self.attos as i128)
}
/// Returns the total duration in milliseconds.
#[inline]
pub const fn to_ms(self) -> i128 {
self.to_attos() / (ATTOS_PER_MS as i128)
}
/// Returns the total duration in microseconds.
#[inline]
pub const fn to_us(self) -> i128 {
self.to_attos() / (ATTOS_PER_US as i128)
}
/// Returns the total duration in nanoseconds.
#[inline]
pub const fn to_ns(self) -> i128 {
self.to_attos() / (ATTOS_PER_NS as i128)
}
/// Returns the total duration in picoseconds.
#[inline]
pub const fn to_ps(self) -> i128 {
self.to_attos() / (ATTOS_PER_PS as i128)
}
/// Returns the total duration in femtoseconds.
#[inline]
pub const fn to_fs(self) -> i128 {
self.to_attos() / (ATTOS_PER_FS as i128)
}
}