tempoch-core 0.6.4

Core astronomical time primitives for tempoch.
Documentation
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright (C) 2026 Vallés Puig, Ramon

//! `Time<S, F>` — canonical instant with compensated precision and a format tag.

use core::fmt;
use core::marker::PhantomData;
use core::ops::{Add, AddAssign, Sub, SubAssign};

use crate::earth::context::TimeContext;
use crate::encoding::jd_to_julian_centuries;
use crate::format::{J2000s, TimeFormat};
use crate::foundation::error::ConversionError;
use crate::model::scale::conversion::{ContextScaleConvert, InfallibleScaleConvert};
use crate::model::scale::{CoordinateScale, Scale, TT, UTC};
use crate::model::target::{ContextConversionTarget, ConversionTarget, InfallibleConversionTarget};
use crate::{FormatForScale, InfallibleFormatForScale};
use affn::algebra::{Space, SplitPoint1, SplitQuantity};
use qtty::time::TimeUnit;
use qtty::unit::Second as SecondUnit;
use qtty::{Quantity, Second};

/// Split-axis scalars must not be NaN; ±∞ may be stored but many conversions still reject them.
#[inline]
fn coordinate_pair_ok(hi: f64, lo: f64) -> bool {
    !hi.is_nan() && !lo.is_nan()
}

#[derive(Copy, Clone)]
pub(crate) struct ScaleAxis<S: Scale>(PhantomData<fn() -> S>);

impl<S: Scale> fmt::Debug for ScaleAxis<S> {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_tuple("ScaleAxis").field(&S::NAME).finish()
    }
}

impl<S: Scale> Space for ScaleAxis<S> {}

/// A point in time on scale `S`, tagged with external format phantom `F`.
///
/// The default `F` is [`J2000s`], so `Time<S>` in code is `Time<S, J2000s>`:
/// SI seconds since J2000.0 TT on the scale's coordinate axis.
///
/// Storage is always a compensated `(hi, lo)` pair of seconds. The format tag
/// does not duplicate storage; it only types the API (`raw()`, conversions, …).
///
/// # Preconditions
///
/// **NaN must never appear** in encoded scalars or storage components — behavior is undefined if it does.
/// **±∞** may be carried when callers use instants as sentinels; operations that require finite coordinates
/// (ΔT loops, UTC civil decoding, POSIX Unix mapping, …) may still return [`ConversionError::NonFinite`].
pub struct Time<S: Scale, F: TimeFormat = J2000s> {
    instant: SplitPoint1<ScaleAxis<S>, SecondUnit>,
    _fmt: PhantomData<fn() -> F>,
}

impl<S: Scale, F: TimeFormat> Copy for Time<S, F> {}

impl<S: Scale, F: TimeFormat> Clone for Time<S, F> {
    #[inline]
    fn clone(&self) -> Self {
        *self
    }
}

impl<S: Scale, F: TimeFormat> PartialEq for Time<S, F> {
    #[inline]
    fn eq(&self, other: &Self) -> bool {
        self.split_seconds() == other.split_seconds()
    }
}

impl<S: Scale, F: TimeFormat> PartialOrd for Time<S, F> {
    #[inline]
    fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
        let (self_hi, self_lo) = self.split_seconds();
        let (other_hi, other_lo) = other.split_seconds();
        match self_hi.partial_cmp(&other_hi) {
            Some(core::cmp::Ordering::Equal) => self_lo.partial_cmp(&other_lo),
            ordering => ordering,
        }
    }
}

impl<S: Scale, F: TimeFormat> fmt::Debug for Time<S, F> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let (hi, lo) = self.split_seconds();
        f.debug_struct("Time")
            .field("scale", &S::NAME)
            .field("format", &F::NAME)
            .field("hi_s", &hi)
            .field("lo_s", &lo)
            .finish()
    }
}

impl<S: CoordinateScale, F> fmt::Display for Time<S, F>
where
    F: InfallibleFormatForScale<S>,
    qtty::Quantity<F::Unit>: fmt::Display,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if F::NAME == J2000s::NAME {
            write!(f, "{} {:.9}", S::NAME, self.total_seconds().value())
        } else {
            fmt::Display::fmt(&F::from_time(*self), f)
        }
    }
}

impl fmt::Display for Time<UTC, crate::format::Unix> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self.try_raw_with(&TimeContext::new()) {
            Ok(q) => fmt::Display::fmt(&q, f),
            Err(_) => f.write_str("Unix(<invalid for display>)"),
        }
    }
}

impl<S: CoordinateScale, F> fmt::LowerExp for Time<S, F>
where
    F: InfallibleFormatForScale<S>,
    qtty::Quantity<F::Unit>: fmt::LowerExp,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::LowerExp::fmt(&F::from_time(*self), f)
    }
}

impl<S: CoordinateScale, F> fmt::UpperExp for Time<S, F>
where
    F: InfallibleFormatForScale<S>,
    qtty::Quantity<F::Unit>: fmt::UpperExp,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::UpperExp::fmt(&F::from_time(*self), f)
    }
}

impl<S: Scale, F: TimeFormat> Time<S, F> {
    #[inline]
    pub(crate) fn from_split(hi: Second, lo: Second) -> Self {
        debug_assert!(
            coordinate_pair_ok(hi.value(), lo.value()),
            "time split pair must not contain NaN"
        );
        let instant = SplitPoint1::new(hi, lo);
        let (hi, lo) = instant.coordinate().pair();
        debug_assert!(
            coordinate_pair_ok(hi.value(), lo.value()),
            "time split pair must not contain NaN"
        );
        Self {
            instant,
            _fmt: PhantomData,
        }
    }

    #[inline]
    pub(crate) fn try_from_split(hi: Second, lo: Second) -> Result<Self, ConversionError> {
        if coordinate_pair_ok(hi.value(), lo.value()) {
            Ok(Self::from_split(hi, lo))
        } else {
            Err(ConversionError::NonFinite)
        }
    }

    /// Same instant, different format tag (zero cost).
    #[inline]
    pub fn reinterpret<G: TimeFormat>(self) -> Time<S, G> {
        Time {
            instant: self.instant,
            _fmt: PhantomData,
        }
    }

    /// SI J2000-second tagged view of the same instant.
    #[inline]
    pub fn to_j2000s(self) -> Time<S, J2000s> {
        self.reinterpret()
    }

    #[inline]
    pub(crate) fn split_seconds(self) -> (Second, Second) {
        self.instant.coordinate().pair()
    }

    #[inline]
    pub(crate) fn total_seconds(self) -> Second {
        self.instant.coordinate().total()
    }

    /// Raw internal storage pair in J2000-TT seconds on the instance scale.
    #[inline]
    pub fn raw_seconds_pair(self) -> (Second, Second) {
        self.split_seconds()
    }
}

impl<S: CoordinateScale> Time<S, J2000s> {
    /// Build from J2000 TT seconds on the scale's coordinate axis.
    #[inline]
    pub fn from_raw_j2000_seconds(seconds: Second) -> Result<Self, ConversionError> {
        Self::try_from_split(seconds, Second::new(0.0))
    }

    /// Build from a split J2000-second pair on the scale's coordinate axis.
    #[inline]
    pub fn try_from_raw_j2000_seconds_split(
        hi: Second,
        lo: Second,
    ) -> Result<Self, ConversionError> {
        Self::try_from_split(hi, lo)
    }

    #[inline]
    pub(crate) fn raw_j2000_seconds(self) -> Second {
        self.total_seconds()
    }

    /// Shift this instant forward by a typed duration.
    #[inline]
    pub fn shifted_by<U>(self, delta: qtty::Quantity<U>) -> Self
    where
        U: TimeUnit,
    {
        self + delta
    }

    /// Shift this instant backward by a typed duration.
    #[inline]
    pub fn shifted_back_by<U>(self, delta: qtty::Quantity<U>) -> Self
    where
        U: TimeUnit,
    {
        self - delta
    }

    /// Duration from `other` to `self`.
    #[inline]
    pub fn duration_since(self, other: Self) -> Second {
        self - other
    }

    /// Duration from `self` to `other`.
    #[inline]
    pub fn duration_until(self, other: Self) -> Second {
        other - self
    }
}

impl<S: CoordinateScale, F: InfallibleFormatForScale<S>> Time<S, F> {
    /// Encoded scalar for this format (derived from split storage).
    #[inline]
    pub fn raw(self) -> Quantity<F::Unit> {
        F::from_time(self)
    }

    /// Alias for [`Self::raw`].
    #[inline]
    pub fn quantity(self) -> Quantity<F::Unit> {
        F::from_time(self)
    }
}

impl<S: CoordinateScale, F: TimeFormat> Time<S, F> {
    /// Exact-precision duration from `other` to `self`.
    ///
    /// Unlike the [`Sub`] implementation that returns a `Quantity<F::Unit>`
    /// (and therefore goes through `f64`), this method projects the difference
    /// into [`crate::ExactDuration`], which has 1 ns resolution.
    ///
    /// **Precision note:** `Time<S>` stores instants as a compensated split-f64
    /// pair. Near typical astronomy epochs (e.g. J2000 ± 50 years) the ULP of
    /// the high word is roughly 120–150 ns, so differences smaller than that
    /// may not round-trip exactly. For sub-microsecond precision on two instants
    /// that were originally constructed from the same `ExactDuration` arithmetic,
    /// the compensation pair reduces the error significantly, but this is not a
    /// guarantee of nanosecond parity for arbitrary instants.
    ///
    /// Returns [`crate::DurationError::Overflow`] only if the difference is
    /// outside the i128-nanosecond range (≈ ±5.4 × 10²¹ yr), which is unreachable
    /// for any physical astronomy use case.
    #[inline]
    pub fn diff_exact(self, other: Self) -> Result<crate::ExactDuration, crate::DurationError> {
        let delta: Second = self.instant - other.instant;
        crate::ExactDuration::try_from_quantity(delta)
    }

    /// Shift this instant by an [`crate::ExactDuration`], returning `Err` if the
    /// duration's seconds component exceeds the `i64` range (≈ ±292 billion years).
    ///
    /// **Precision note:** The duration is split into a whole-second component and
    /// a sub-second nanosecond remainder, each added to the compensated split-f64
    /// storage separately. The whole-second part is an integer `f64` (exact for
    /// `|seconds| < 2^53`). The nanosecond remainder crosses the split-f64 storage
    /// boundary and is therefore bounded by the documented split-f64 precision
    /// limits (ULP ≈ 120–150 ns near J2000 ± 50 years), so shifts smaller than
    /// that threshold may not alter the stored instant.
    #[inline]
    pub fn try_add_exact(
        self,
        delta: crate::ExactDuration,
    ) -> Result<Self, crate::foundation::duration::DurationError> {
        let (whole_secs, sub_nanos) = delta.as_seconds_i64_nanos_checked()?;
        let t = self.instant + Second::new(whole_secs as f64);
        Ok(Self {
            instant: t + Second::new(sub_nanos as f64 * 1e-9),
            _fmt: PhantomData,
        })
    }

    /// Shift this instant backward by an [`crate::ExactDuration`], returning `Err`
    /// if the duration's seconds component exceeds the `i64` range.
    ///
    /// See [`Self::try_add_exact`] for precision notes.
    #[inline]
    pub fn try_sub_exact(
        self,
        delta: crate::ExactDuration,
    ) -> Result<Self, crate::foundation::duration::DurationError> {
        let (whole_secs, sub_nanos) = delta.as_seconds_i64_nanos_checked()?;
        let t = self.instant - Second::new(whole_secs as f64);
        Ok(Self {
            instant: t - Second::new(sub_nanos as f64 * 1e-9),
            _fmt: PhantomData,
        })
    }

    /// Shift this instant by an [`crate::ExactDuration`].
    ///
    /// **Panics** if the duration's seconds component exceeds the `i64` range
    /// (≈ ±292 billion years). Use [`try_add_exact`](Self::try_add_exact) for
    /// the fallible variant that returns `Err` instead.
    ///
    /// See [`try_add_exact`](Self::try_add_exact) for precision notes.
    #[inline]
    pub fn add_exact(self, delta: crate::ExactDuration) -> Self {
        self.try_add_exact(delta)
            .expect("ExactDuration::add_exact: duration exceeds i64 seconds range")
    }

    /// Shift this instant backward by an [`crate::ExactDuration`].
    ///
    /// **Panics** if the duration's seconds component exceeds the `i64` range.
    /// Use [`try_sub_exact`](Self::try_sub_exact) for the fallible variant.
    ///
    /// See [`try_add_exact`](Self::try_add_exact) for precision notes.
    #[inline]
    pub fn sub_exact(self, delta: crate::ExactDuration) -> Self {
        self.try_sub_exact(delta)
            .expect("ExactDuration::sub_exact: duration exceeds i64 seconds range")
    }

    /// Round this instant to the nearest multiple of `quantum` measured from
    /// `epoch`. Banker's rounding (half-to-even) at the quantum boundary.
    /// Returns `self` unchanged on overflow.
    pub fn round_to_epoch(self, epoch: Self, quantum: crate::ExactDuration) -> Self {
        match self.diff_exact(epoch) {
            Ok(d) => epoch.add_exact(d.round_to(quantum)),
            Err(_) => self,
        }
    }

    /// Floor this instant toward `epoch − ∞` at `quantum`.
    pub fn floor_to_epoch(self, epoch: Self, quantum: crate::ExactDuration) -> Self {
        match self.diff_exact(epoch) {
            Ok(d) => epoch.add_exact(d.floor_to(quantum)),
            Err(_) => self,
        }
    }

    /// Ceil this instant toward `epoch + ∞` at `quantum`.
    pub fn ceil_to_epoch(self, epoch: Self, quantum: crate::ExactDuration) -> Self {
        match self.diff_exact(epoch) {
            Ok(d) => epoch.add_exact(d.ceil_to(quantum)),
            Err(_) => self,
        }
    }
}

impl<S: CoordinateScale, F> Time<S, F>
where
    F: FormatForScale<S>,
{
    #[inline]
    pub fn try_raw_with(self, ctx: &TimeContext) -> Result<Quantity<F::Unit>, ConversionError> {
        F::try_from_time(self, ctx)
    }
}

impl<S: Scale, F: TimeFormat> Time<S, F> {
    /// Unified infallible conversion to a scale/view target.
    #[allow(private_bounds)]
    #[inline]
    pub fn to<T>(self) -> T::Output
    where
        T: InfallibleConversionTarget<S, F>,
    {
        T::convert(self)
    }

    /// Unified fallible conversion to a scale/view target.
    #[allow(private_bounds)]
    #[inline]
    pub fn try_to<T>(self) -> Result<T::Output, ConversionError>
    where
        T: ConversionTarget<S, F>,
    {
        T::try_convert(self)
    }

    /// Unified context-backed conversion to a scale/view target.
    #[allow(private_bounds)]
    #[inline]
    pub fn to_with<T>(self, ctx: &TimeContext) -> Result<T::Output, ConversionError>
    where
        T: ContextConversionTarget<S, F>,
    {
        T::convert_with(self, ctx)
    }

    /// Infallible scale conversion; preserves format tag `F`.
    #[allow(private_bounds)]
    #[inline]
    pub fn to_scale<S2: Scale>(self) -> Time<S2, F>
    where
        S: InfallibleScaleConvert<S2>,
    {
        let (hi, lo) = self.split_seconds();
        let (new_hi, new_lo) = <S as InfallibleScaleConvert<S2>>::convert(hi, lo);
        Time::from_split(new_hi, new_lo)
    }

    /// Context-required scale conversion (UT1 routes); preserves `F`.
    #[allow(private_bounds)]
    #[inline]
    pub fn to_scale_with<S2: Scale>(self, ctx: &TimeContext) -> Result<Time<S2, F>, ConversionError>
    where
        S: ContextScaleConvert<S2>,
    {
        let (hi, lo) = self.split_seconds();
        let (new_hi, new_lo) = <S as ContextScaleConvert<S2>>::convert_with(hi, lo, ctx)?;
        Ok(Time::from_split(new_hi, new_lo))
    }
}

impl<S: Scale, F: FormatForScale<S>> Time<S, F> {
    /// Fallible constructor from an encoded scalar.
    ///
    /// Only surfaces **domain** failures from format decoding (UTC policy, leap seconds, ranges, …).
    /// Scalar hygiene is a caller precondition: **NaN must not be passed**; ±∞ is accepted only where the format decoder tolerates it.
    #[inline]
    pub fn try_new(raw: Quantity<F::Unit>) -> Result<Self, ConversionError> {
        F::try_into_time(raw, &TimeContext::new())
    }

    /// Like [`Self::try_new`], but uses `ctx` for UTC / POSIX decoding policy.
    #[inline]
    pub fn try_new_with(
        raw: Quantity<F::Unit>,
        ctx: &TimeContext,
    ) -> Result<Self, ConversionError> {
        F::try_into_time(raw, ctx)
    }
}

impl<S: Scale, F: InfallibleFormatForScale<S>> Time<S, F> {
    /// Infallible constructor from the raw scalar value for format `F`.
    ///
    /// # Panics
    ///
    /// If `value` is **NaN**. ±∞ is allowed as storage when callers use sentinel instants.
    #[track_caller]
    #[inline]
    pub fn new(value: f64) -> Self {
        assert!(
            !value.is_nan(),
            "time scalar must not be NaN (±∞ is allowed)"
        );
        F::into_time(Quantity::<F::Unit>::new(value))
    }
}

impl<S: CoordinateScale, F: InfallibleFormatForScale<S>> Time<S, F> {
    #[inline]
    pub fn min(self, other: Self) -> Self {
        if self <= other {
            self
        } else {
            other
        }
    }

    #[inline]
    pub fn max(self, other: Self) -> Self {
        if self >= other {
            self
        } else {
            other
        }
    }

    #[inline]
    pub fn mean(self, other: Self) -> Self {
        let t = self.to_j2000s() + ((other.to_j2000s() - self.to_j2000s()) * 0.5);
        t.reinterpret()
    }
}

/// TT Julian date at J2000.0 (`JD 2 451 545.0`); matches [`Self::jd_epoch_tt`], usable in `const`.
impl Time<TT, crate::format::JD> {
    pub const JD_EPOCH_J2000_0: Self = Self {
        instant: SplitPoint1::from_split(SplitQuantity::from_normalized_parts(
            Second::new(0.0),
            Second::new(0.0),
        )),
        _fmt: PhantomData,
    };
}

impl<S: Scale> Time<S, crate::format::JD> {
    /// TT J2000.0 as a Julian Date on scale `S` (JD 2 451 545.0).
    #[inline]
    pub fn jd_epoch_tt() -> Self
    where
        S: CoordinateScale,
    {
        Time::<S, J2000s>::from_raw_j2000_seconds(Second::new(0.0))
            .expect("J2000 origin")
            .reinterpret()
    }

    #[inline]
    pub fn value(self) -> f64
    where
        S: CoordinateScale,
    {
        self.raw().value()
    }

    #[inline]
    pub fn julian_centuries(self) -> f64
    where
        S: CoordinateScale,
    {
        jd_to_julian_centuries(self.raw())
    }
}

impl<S: Scale> Time<S, crate::format::MJD> {
    #[inline]
    pub fn value(self) -> f64
    where
        S: CoordinateScale,
    {
        self.raw().value()
    }
}

impl<S: CoordinateScale, F, U> Add<Quantity<U>> for Time<S, F>
where
    F: InfallibleFormatForScale<S>,
    U: TimeUnit,
{
    type Output = Self;

    #[inline]
    fn add(self, rhs: Quantity<U>) -> Self::Output {
        Self {
            instant: self.instant + rhs.to::<SecondUnit>(),
            _fmt: PhantomData,
        }
    }
}

impl<S: CoordinateScale, F, U> Sub<Quantity<U>> for Time<S, F>
where
    F: InfallibleFormatForScale<S>,
    U: TimeUnit,
{
    type Output = Self;

    #[inline]
    fn sub(self, rhs: Quantity<U>) -> Self::Output {
        Self {
            instant: self.instant - rhs.to::<SecondUnit>(),
            _fmt: PhantomData,
        }
    }
}

impl<S: CoordinateScale, F> Sub for Time<S, F>
where
    F: InfallibleFormatForScale<S>,
    F::Unit: TimeUnit,
{
    type Output = Quantity<F::Unit>;

    #[inline]
    fn sub(self, rhs: Self) -> Self::Output {
        let delta: Second = self.instant - rhs.instant;
        delta.to::<F::Unit>()
    }
}

impl<S: CoordinateScale, F, U> AddAssign<Quantity<U>> for Time<S, F>
where
    F: InfallibleFormatForScale<S>,
    U: TimeUnit,
{
    #[inline]
    fn add_assign(&mut self, rhs: Quantity<U>) {
        *self = *self + rhs;
    }
}

impl<S: CoordinateScale, F, U> SubAssign<Quantity<U>> for Time<S, F>
where
    F: InfallibleFormatForScale<S>,
    U: TimeUnit,
{
    #[inline]
    fn sub_assign(&mut self, rhs: Quantity<U>) {
        *self = *self - rhs;
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::format::J2000s;
    use crate::foundation::duration::ExactDuration;
    use crate::model::scale::TAI;

    type TaiJ2000 = Time<TAI, J2000s>;

    fn j2000_tai() -> TaiJ2000 {
        TaiJ2000::from_raw_j2000_seconds(Second::new(0.0)).unwrap()
    }

    fn j2000_tai_plus_50yr() -> TaiJ2000 {
        // 50 Julian years = 50 * 365.25 * 86400 = 1_577_836_800 s
        TaiJ2000::from_raw_j2000_seconds(Second::new(1_577_836_800.0)).unwrap()
    }

    #[test]
    fn add_exact_1ns_at_j2000() {
        let t = j2000_tai();
        let d = ExactDuration::from_nanos(1);
        let shifted = t.add_exact(d);
        let diff = shifted.diff_exact(t).unwrap();
        // Near J2000 hi ≈ 0; lo stores the 1 ns shift exactly.
        assert_eq!(diff.as_nanos_i128(), 1, "1 ns shift at J2000 must be exact");
    }

    #[test]
    fn add_sub_round_trip_1ns_at_j2000_plus_50yr() {
        let t = j2000_tai_plus_50yr();
        // 1 ns: ULP of hi at 1.57e9 s is ~240 ns, so the lo word carries it.
        for ns in [1_i128, 123, 999] {
            let d = ExactDuration::from_nanos(ns);
            let shifted = t.add_exact(d).sub_exact(d);
            let back = shifted.diff_exact(t).unwrap();
            assert!(
                back.as_nanos_i128().abs() < 100,
                "add/sub round-trip drift at J2000+50yr for {ns} ns: {} ns",
                back.as_nanos_i128()
            );
        }
    }

    #[test]
    fn add_exact_1yr_plus_1ns_preserves_1ns() {
        let t = j2000_tai();
        // 1 Julian year = 31_557_600 s
        let one_year = ExactDuration::from_nanos(31_557_600 * 1_000_000_000);
        let one_ns = ExactDuration::from_nanos(1);
        let combined = (one_year + one_ns)
            .checked_add(ExactDuration::ZERO)
            .unwrap();
        let d_year = t.add_exact(one_year);
        let d_combined = t.add_exact(combined);
        let diff = d_combined.diff_exact(d_year).unwrap();
        // The difference should be 1 ns; allow up to 2 ns for sub-nanosecond f64 rounding.
        assert!(
            diff.as_nanos_i128().abs() <= 2,
            "1 yr + 1 ns shift must preserve 1 ns component; diff = {} ns",
            diff.as_nanos_i128()
        );
    }

    #[test]
    fn try_add_exact_overflow_returns_err() {
        let t = j2000_tai();
        // ExactDuration::MAX has > i64::MAX seconds → try_add_exact must return Err.
        let result = t.try_add_exact(ExactDuration::MAX);
        assert!(
            result.is_err(),
            "expected Err for try_add_exact(MAX), got Ok"
        );
        let result2 = t.try_sub_exact(ExactDuration::MAX);
        assert!(
            result2.is_err(),
            "expected Err for try_sub_exact(MAX), got Ok"
        );
    }

    #[test]
    #[should_panic(expected = "ExactDuration::add_exact")]
    fn add_exact_panics_on_overflow() {
        let t = j2000_tai();
        let _ = t.add_exact(ExactDuration::MAX);
    }
}