tempoch-core 0.4.5

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
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright (C) 2026 Vallés Puig, Ramon

//! Typed encoded representations of [`crate::Time`].

use core::fmt;
use core::marker::PhantomData;

use crate::context::TimeContext;
use crate::encoding::{
    j2000_seconds_to_jd, j2000_seconds_to_mjd, jd_to_j2000_seconds, mjd_to_j2000_seconds,
};
use crate::error::ConversionError;
use crate::scale::conversion::InfallibleScaleConvert;
use crate::scale::{CoordinateScale, Scale, TAI, TDB, TT, UTC};
use crate::sealed::Sealed;
use crate::target::{ContextConversionTarget, ConversionTarget, InfallibleConversionTarget};
use crate::time::Time;
use qtty::{Day, Quantity, Second, Unit};

/// Marker trait for external time encodings such as JD or Unix time.
#[allow(private_bounds)]
pub trait TimeRepresentation: Sealed + Copy + Clone + fmt::Debug + 'static {
    /// Quantity unit used by this representation.
    type Unit: Unit;

    /// Human-readable representation name.
    const NAME: &'static str;
}

/// Representation witness for scale `S`.
#[allow(private_bounds)]
pub trait RepresentationForScale<S: Scale>: TimeRepresentation + Sealed {
    fn try_from_time(
        time: Time<S>,
        ctx: &TimeContext,
    ) -> Result<Quantity<Self::Unit>, ConversionError>;
    fn try_into_time(
        raw: Quantity<Self::Unit>,
        ctx: &TimeContext,
    ) -> Result<Time<S>, ConversionError>;
}

/// Representation witness for scale `S` with context-free round-trips.
#[allow(private_bounds)]
pub trait InfallibleRepresentationForScale<S: Scale>: RepresentationForScale<S> + Sealed {
    fn from_time(time: Time<S>) -> Quantity<Self::Unit>;
    fn into_time(raw: Quantity<Self::Unit>) -> Time<S>;
}

/// J2000 seconds on the source scale's coordinate axis.
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct J2000s;

/// Julian Day on the source scale's coordinate axis.
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct JD;

/// Modified Julian Day on the source scale's coordinate axis.
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct MJD;

/// POSIX seconds on the UTC civil axis.
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Unix;

/// GPS seconds since the GPS epoch on the TAI/GPS continuous axis.
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct GPS;

impl Sealed for J2000s {}
impl Sealed for JD {}
impl Sealed for MJD {}
impl Sealed for Unix {}
impl Sealed for GPS {}

impl TimeRepresentation for J2000s {
    type Unit = qtty::unit::Second;
    const NAME: &'static str = "J2000s";
}

impl TimeRepresentation for JD {
    type Unit = qtty::unit::Day;
    const NAME: &'static str = "Julian Day";
}

impl TimeRepresentation for MJD {
    type Unit = qtty::unit::Day;
    const NAME: &'static str = "Modified Julian Day";
}

impl TimeRepresentation for Unix {
    type Unit = qtty::unit::Second;
    const NAME: &'static str = "Unix";
}

impl TimeRepresentation for GPS {
    type Unit = qtty::unit::Second;
    const NAME: &'static str = "GPS";
}

/// A typed external representation of a [`Time<S>`] instant.
pub struct EncodedTime<S: Scale, R: TimeRepresentation> {
    raw: Quantity<R::Unit>,
    _marker: PhantomData<fn() -> S>,
}

impl<S: Scale, R: TimeRepresentation> Copy for EncodedTime<S, R> {}

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

impl<S: Scale, R: TimeRepresentation> fmt::Debug for EncodedTime<S, R> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("EncodedTime")
            .field("scale", &S::NAME)
            .field("representation", &R::NAME)
            .field("raw", &self.raw)
            .finish()
    }
}

impl<S: Scale, R: TimeRepresentation> fmt::Display for EncodedTime<S, R>
where
    qtty::Quantity<R::Unit>: fmt::Display,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{} ", R::NAME)?;
        fmt::Display::fmt(&self.raw, f)
    }
}

impl<S: Scale, R: TimeRepresentation> fmt::LowerExp for EncodedTime<S, R>
where
    qtty::Quantity<R::Unit>: fmt::LowerExp,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::LowerExp::fmt(&self.raw, f)
    }
}

impl<S: Scale, R: TimeRepresentation> fmt::UpperExp for EncodedTime<S, R>
where
    qtty::Quantity<R::Unit>: fmt::UpperExp,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::UpperExp::fmt(&self.raw, f)
    }
}

impl<S: Scale, R: TimeRepresentation> PartialEq for EncodedTime<S, R> {
    #[inline]
    fn eq(&self, other: &Self) -> bool {
        self.raw == other.raw
    }
}

impl<S: Scale, R: TimeRepresentation> PartialOrd for EncodedTime<S, R> {
    #[inline]
    fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
        self.raw.partial_cmp(&other.raw)
    }
}

impl<S: Scale, R: TimeRepresentation> EncodedTime<S, R> {
    /// Construct from a raw quantity, bypassing the finite check.
    ///
    /// Passing a non-finite value yields an instant whose behaviour is
    /// unspecified. Prefer [`Self::try_new`] for user-supplied data; use this
    /// only when the value is known to be finite (e.g. compile-time constants).
    #[inline]
    pub const fn new_unchecked(raw: Quantity<R::Unit>) -> Self {
        Self {
            raw,
            _marker: PhantomData,
        }
    }

    /// Return the underlying typed quantity.
    #[inline]
    pub const fn raw(self) -> Quantity<R::Unit> {
        self.raw
    }

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

impl<S: Scale, R> EncodedTime<S, R>
where
    R: RepresentationForScale<S>,
{
    /// Construct a typed encoded instant from its raw quantity.
    #[inline]
    pub fn try_new(raw: Quantity<R::Unit>) -> Result<Self, ConversionError> {
        if raw.is_finite() {
            Ok(Self::new_unchecked(raw))
        } else {
            Err(ConversionError::NonFinite)
        }
    }

    /// Convert this encoded instant to the canonical [`Time<S>`] model.
    ///
    /// Snapshots the active time-data bundle at call time via
    /// [`TimeContext::new`]. For reproducible pipelines, prefer
    /// [`to_time_with`](Self::to_time_with) with an explicit context.
    #[inline]
    pub fn try_to_time(self) -> Result<Time<S>, ConversionError> {
        R::try_into_time(self.raw, &TimeContext::new())
    }

    /// Convert this encoded instant to the canonical [`Time<S>`] model using an explicit context.
    #[inline]
    pub fn to_time_with(self, ctx: &TimeContext) -> Result<Time<S>, ConversionError> {
        R::try_into_time(self.raw, ctx)
    }
}

impl<S: Scale, R> EncodedTime<S, R>
where
    R: InfallibleRepresentationForScale<S>,
{
    #[inline]
    pub(crate) fn from_time_infallible(time: Time<S>) -> Self {
        Self::new_unchecked(R::from_time(time))
    }

    /// Infallible conversion to the canonical [`Time<S>`] model.
    #[inline]
    pub fn to_time(self) -> Time<S> {
        R::into_time(self.raw)
    }

    /// Unified infallible conversion to a target scale or encoded format.
    #[allow(private_bounds)]
    #[inline]
    pub fn to<T>(self) -> T::Output
    where
        T: InfallibleConversionTarget<S>,
    {
        T::convert(self.to_time())
    }

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

impl<S: Scale, R> EncodedTime<S, R>
where
    R: RepresentationForScale<S>,
{
    /// Unified context-backed conversion to a target scale or encoded format.
    #[allow(private_bounds)]
    #[inline]
    pub fn to_with<T>(self, ctx: &TimeContext) -> Result<T::Output, ConversionError>
    where
        T: ContextConversionTarget<S>,
    {
        T::convert_with(self.to_time_with(ctx)?, ctx)
    }
}

/// `EncodedTime<S, JD>` convenience alias.
pub type JulianDate<S> = EncodedTime<S, JD>;

/// `EncodedTime<S, MJD>` convenience alias.
pub type ModifiedJulianDate<S> = EncodedTime<S, MJD>;

/// `EncodedTime<S, J2000s>` convenience alias.
pub type J2000Seconds<S> = EncodedTime<S, J2000s>;

/// `EncodedTime<UTC, Unix>` convenience alias.
pub type UnixTime = EncodedTime<UTC, Unix>;

/// `EncodedTime<TAI, GPS>` convenience alias.
pub type GpsTime = EncodedTime<TAI, GPS>;

/// J2000.0 epoch as a TT-scale Julian Date (JD(TT) = 2 451 545.0).
pub const J2000_TT: JulianDate<TT> = EncodedTime::<TT, JD>::new_unchecked(Day::new(2_451_545.0));

// ── Inherent helpers for Day-based encoded times (JD and MJD) ────────────────

impl<S: Scale, R> EncodedTime<S, R>
where
    R: TimeRepresentation<Unit = qtty::unit::Day>,
{
    /// Earlier of `self` and `other`.
    ///
    /// Equivalent to `if self <= other { self } else { other }`.
    #[inline]
    pub fn min(self, other: Self) -> Self {
        if self.raw.value() <= other.raw.value() {
            self
        } else {
            other
        }
    }

    /// Later of `self` and `other`.
    #[inline]
    pub fn max(self, other: Self) -> Self {
        if self.raw.value() >= other.raw.value() {
            self
        } else {
            other
        }
    }

    /// Midpoint between `self` and `other`.
    #[inline]
    pub fn mean(self, other: Self) -> Self {
        Self::new_unchecked(Day::new((self.raw.value() + other.raw.value()) * 0.5))
    }
}

// ── JulianDate<S> inherent helpers ───────────────────────────────────────────

/// Length of a Julian year in days (exactly 365.25 d).
pub const JULIAN_YEAR_DAYS: Day = Day::new(365.25);

impl<S: CoordinateScale> JulianDate<S> {
    /// Construct from a raw Julian Day value without validation.
    ///
    /// Prefer [`Self::try_new`] for untrusted input.
    #[inline]
    pub fn new(jd: f64) -> Self {
        Self::new_unchecked(Day::new(jd))
    }

    /// Raw Julian Day value as `f64` (days since noon 1 January 4713 BC JD).
    #[inline]
    pub fn jd_value(self) -> f64 {
        self.raw().value()
    }

    /// Julian centuries since J2000.0: `T = (JD − 2 451 545.0) / 36 525`.
    #[inline]
    pub fn julian_centuries(self) -> f64 {
        (self.raw().value() - crate::constats::J2000_JD_TT.value())
            / crate::constats::DAYS_PER_JC.value()
    }

    /// Julian millennia since J2000.0: `T = (JD − 2 451 545.0) / 365 250`.
    #[inline]
    pub fn julian_millennias(self) -> f64 {
        (self.raw().value() - crate::constats::J2000_JD_TT.value())
            / (crate::constats::DAYS_PER_JC.value() * 10.0)
    }

    /// Length of a Julian year in days (365.25 d).
    pub const JULIAN_YEAR: Day = Day::new(365.25);

    /// Length of a Julian century in days (36 525 d).
    pub const JULIAN_CENTURY: Day = Day::new(36_525.0);
}

impl JulianDate<TT> {
    /// J2000.0 epoch as a TT-scale Julian Date (`JD(TT) = 2 451 545.0`).
    pub const J2000: Self = J2000_TT;

    /// Convert this TT Julian Date to the TDB scale.
    ///
    /// Uses the Fairhead-Bretagnon periodic correction stored in the scale
    /// conversion layer. The result is on the TDB coordinate time axis, still
    /// expressed as a Julian Date.
    #[inline]
    pub fn tt_to_tdb(self) -> JulianDate<TDB> {
        JulianDate::<TDB>::from_time_infallible(self.to_time().to_scale::<TDB>())
    }

    /// Build a TT Julian Date from a `chrono::DateTime<Utc>`.
    ///
    /// Converts UTC → TAI → TT internally.  Panics if the UTC time data is
    /// unavailable for the supplied instant; use
    /// [`Time::<UTC>::try_from_chrono`] for a fallible path.
    #[inline]
    pub fn from_chrono(dt: chrono::DateTime<chrono::Utc>) -> Self {
        let utc = Time::<UTC>::from_chrono(dt);
        Self::from_time_infallible(utc.to_scale::<TT>())
    }

    /// Convert this TT Julian Date to a `chrono::DateTime<Utc>`.
    ///
    /// Returns `None` if the value is outside the supported UTC range.
    #[inline]
    pub fn to_chrono(self) -> Option<chrono::DateTime<chrono::Utc>> {
        self.to_time().to_scale::<UTC>().to_chrono()
    }
}

// ── Cross-representation From conversions (JD ↔ MJD for CoordinateScales) ───

impl<S: CoordinateScale> From<ModifiedJulianDate<S>> for JulianDate<S> {
    /// Convert a Modified Julian Date to a Julian Date on the same scale.
    #[inline]
    fn from(mjd: ModifiedJulianDate<S>) -> Self {
        mjd.to::<JD>()
    }
}

impl<S: CoordinateScale> From<JulianDate<S>> for ModifiedJulianDate<S> {
    /// Convert a Julian Date to a Modified Julian Date on the same scale.
    #[inline]
    fn from(jd: JulianDate<S>) -> Self {
        jd.to::<MJD>()
    }
}

// ── ModifiedJulianDate<S> inherent helpers ────────────────────────────────────

impl<S: CoordinateScale> ModifiedJulianDate<S> {
    /// Construct from a raw Modified Julian Day value without validation.
    ///
    /// Prefer [`Self::try_new`] for untrusted input.
    #[inline]
    pub fn new(mjd: f64) -> Self {
        Self::new_unchecked(Day::new(mjd))
    }

    /// Raw Modified Julian Day value as `f64` (days since midnight 17 November 1858).
    #[inline]
    pub fn mjd_value(self) -> f64 {
        self.raw().value()
    }
}

impl ModifiedJulianDate<TT> {
    /// Build a TT Modified Julian Date from a `chrono::DateTime<Utc>`.
    ///
    /// Converts UTC → TAI → TT internally.  Panics if UTC time data is
    /// unavailable; use [`Time::<UTC>::try_from_chrono`] for a fallible path.
    #[inline]
    pub fn from_chrono(dt: chrono::DateTime<chrono::Utc>) -> Self {
        let utc = Time::<UTC>::from_chrono(dt);
        Self::from_time_infallible(utc.to_scale::<TT>())
    }

    /// Convert this TT Modified Julian Date to a `chrono::DateTime<Utc>`.
    ///
    /// Returns `None` if the value is outside the supported UTC range.
    #[inline]
    pub fn to_chrono(self) -> Option<chrono::DateTime<chrono::Utc>> {
        self.to_time().to_scale::<UTC>().to_chrono()
    }
}

macro_rules! coordinate_representation {
    ($repr:ty, $quantity:ty, $from_time:expr, $to_time:expr) => {
        impl<S: CoordinateScale> RepresentationForScale<S> for $repr {
            #[inline]
            fn try_from_time(
                time: Time<S>,
                _ctx: &TimeContext,
            ) -> Result<$quantity, ConversionError> {
                Ok(<Self as InfallibleRepresentationForScale<S>>::from_time(
                    time,
                ))
            }

            #[inline]
            fn try_into_time(
                raw: $quantity,
                _ctx: &TimeContext,
            ) -> Result<Time<S>, ConversionError> {
                Ok(<Self as InfallibleRepresentationForScale<S>>::into_time(
                    raw,
                ))
            }
        }

        impl<S: CoordinateScale> InfallibleRepresentationForScale<S> for $repr {
            #[inline]
            fn from_time(time: Time<S>) -> $quantity {
                $from_time(time)
            }

            #[inline]
            fn into_time(raw: $quantity) -> Time<S> {
                $to_time(raw)
            }
        }
    };
}

coordinate_representation!(
    J2000s,
    Second,
    |time: Time<_>| time.raw_j2000_seconds(),
    |raw: Second| Time::from_raw_j2000_seconds(raw).expect("finite J2000 seconds must decode")
);
coordinate_representation!(
    JD,
    Day,
    |time: Time<_>| j2000_seconds_to_jd(time.raw_j2000_seconds()),
    |raw: Day| Time::from_raw_j2000_seconds(jd_to_j2000_seconds(raw))
        .expect("finite Julian date must decode")
);
coordinate_representation!(
    MJD,
    Day,
    |time: Time<_>| j2000_seconds_to_mjd(time.raw_j2000_seconds()),
    |raw: Day| Time::from_raw_j2000_seconds(mjd_to_j2000_seconds(raw))
        .expect("finite Modified Julian date must decode")
);

impl RepresentationForScale<UTC> for Unix {
    #[inline]
    fn try_from_time(time: Time<UTC>, ctx: &TimeContext) -> Result<Second, ConversionError> {
        time.raw_unix_seconds_with(ctx)
    }

    #[inline]
    fn try_into_time(raw: Second, ctx: &TimeContext) -> Result<Time<UTC>, ConversionError> {
        Time::from_raw_unix_seconds_with(raw, ctx)
    }
}

impl RepresentationForScale<TAI> for GPS {
    #[inline]
    fn try_from_time(time: Time<TAI>, _ctx: &TimeContext) -> Result<Second, ConversionError> {
        Ok(<Self as InfallibleRepresentationForScale<TAI>>::from_time(
            time,
        ))
    }

    #[inline]
    fn try_into_time(raw: Second, _ctx: &TimeContext) -> Result<Time<TAI>, ConversionError> {
        Ok(<Self as InfallibleRepresentationForScale<TAI>>::into_time(
            raw,
        ))
    }
}

impl InfallibleRepresentationForScale<TAI> for GPS {
    #[inline]
    fn from_time(time: Time<TAI>) -> Second {
        time.raw_gps_seconds()
    }

    #[inline]
    fn into_time(raw: Second) -> Time<TAI> {
        Time::from_raw_gps_seconds(raw).expect("finite GPS seconds must decode")
    }
}

// ── Arithmetic on EncodedTime ────────────────────────────────────────────────
//
// For JD- and MJD-based representations (both use `qtty::unit::Day`), it is
// natural to shift an instant by a number of days and to compute the signed
// duration between two instants.

impl<S: Scale, R> core::ops::Add<Day> for EncodedTime<S, R>
where
    R: TimeRepresentation<Unit = qtty::unit::Day>,
{
    type Output = Self;

    #[inline]
    fn add(self, rhs: Day) -> Self {
        Self::new_unchecked(Day::new(self.raw.value() + rhs.value()))
    }
}

impl<S: Scale, R> core::ops::Sub<Day> for EncodedTime<S, R>
where
    R: TimeRepresentation<Unit = qtty::unit::Day>,
{
    type Output = Self;

    #[inline]
    fn sub(self, rhs: Day) -> Self {
        Self::new_unchecked(Day::new(self.raw.value() - rhs.value()))
    }
}

impl<S: Scale, R> core::ops::AddAssign<Day> for EncodedTime<S, R>
where
    R: TimeRepresentation<Unit = qtty::unit::Day>,
{
    #[inline]
    fn add_assign(&mut self, rhs: Day) {
        *self = *self + rhs;
    }
}

impl<S: Scale, R> core::ops::SubAssign<Day> for EncodedTime<S, R>
where
    R: TimeRepresentation<Unit = qtty::unit::Day>,
{
    #[inline]
    fn sub_assign(&mut self, rhs: Day) {
        *self = *self - rhs;
    }
}

/// `b - a` returns the signed offset in days: positive when `b` is later.
impl<S: Scale, R> core::ops::Sub for EncodedTime<S, R>
where
    R: TimeRepresentation<Unit = qtty::unit::Day>,
{
    type Output = Day;

    #[inline]
    fn sub(self, rhs: Self) -> Day {
        Day::new(self.raw.value() - rhs.raw.value())
    }
}

impl<S: Scale, R> From<EncodedTime<S, R>> for Time<S>
where
    R: InfallibleRepresentationForScale<S>,
{
    #[inline]
    fn from(value: EncodedTime<S, R>) -> Self {
        value.to_time()
    }
}

impl<S: Scale, R> From<Time<S>> for EncodedTime<S, R>
where
    R: InfallibleRepresentationForScale<S>,
{
    #[inline]
    fn from(value: Time<S>) -> Self {
        Self::from_time_infallible(value)
    }
}

// ── ConversionTarget impls for format markers ────────────────────────────────

impl<S: CoordinateScale> ConversionTarget<S> for J2000s {
    type Output = EncodedTime<S, J2000s>;

    #[inline]
    fn try_convert(src: Time<S>) -> Result<Self::Output, ConversionError> {
        Ok(EncodedTime::from_time_infallible(src))
    }
}

impl<S: CoordinateScale> InfallibleConversionTarget<S> for J2000s {
    #[inline]
    fn convert(src: Time<S>) -> Self::Output {
        EncodedTime::from_time_infallible(src)
    }
}

impl<S: CoordinateScale> ConversionTarget<S> for JD {
    type Output = EncodedTime<S, JD>;

    #[inline]
    fn try_convert(src: Time<S>) -> Result<Self::Output, ConversionError> {
        Ok(EncodedTime::from_time_infallible(src))
    }
}

impl<S: CoordinateScale> InfallibleConversionTarget<S> for JD {
    #[inline]
    fn convert(src: Time<S>) -> Self::Output {
        EncodedTime::from_time_infallible(src)
    }
}

impl<S: CoordinateScale> ConversionTarget<S> for MJD {
    type Output = EncodedTime<S, MJD>;

    #[inline]
    fn try_convert(src: Time<S>) -> Result<Self::Output, ConversionError> {
        Ok(EncodedTime::from_time_infallible(src))
    }
}

impl<S: CoordinateScale> InfallibleConversionTarget<S> for MJD {
    #[inline]
    fn convert(src: Time<S>) -> Self::Output {
        EncodedTime::from_time_infallible(src)
    }
}

impl<S> ConversionTarget<S> for Unix
where
    S: crate::scale::Scale + InfallibleScaleConvert<UTC>,
{
    type Output = EncodedTime<UTC, Unix>;

    /// Snapshots the active time-data bundle at call time via
    /// [`TimeContext::new`]. For reproducible pipelines, prefer
    /// [`to_with::<Unix>(&ctx)`](crate::time::Time::to_with).
    #[inline]
    fn try_convert(src: Time<S>) -> Result<Self::Output, ConversionError> {
        let utc = src.to_scale::<UTC>();
        let raw = Unix::try_from_time(utc, &TimeContext::new())?;
        Ok(EncodedTime::new_unchecked(raw))
    }
}

impl ContextConversionTarget<UTC> for Unix {
    type Output = EncodedTime<UTC, Unix>;

    #[inline]
    fn convert_with(src: Time<UTC>, ctx: &TimeContext) -> Result<Self::Output, ConversionError> {
        let raw = Unix::try_from_time(src, ctx)?;
        Ok(EncodedTime::new_unchecked(raw))
    }
}

impl<S> ContextConversionTarget<S> for Unix
where
    S: crate::scale::Scale + crate::scale::conversion::ContextScaleConvert<UTC>,
{
    type Output = EncodedTime<UTC, Unix>;

    #[inline]
    fn convert_with(src: Time<S>, ctx: &TimeContext) -> Result<Self::Output, ConversionError> {
        let utc = src.to_scale_with::<UTC>(ctx)?;
        let raw = Unix::try_from_time(utc, ctx)?;
        Ok(EncodedTime::new_unchecked(raw))
    }
}

impl<S> ConversionTarget<S> for GPS
where
    S: crate::scale::Scale + InfallibleScaleConvert<TAI>,
{
    type Output = EncodedTime<TAI, GPS>;

    #[inline]
    fn try_convert(src: Time<S>) -> Result<Self::Output, ConversionError> {
        Ok(Self::convert(src))
    }
}

impl<S> InfallibleConversionTarget<S> for GPS
where
    S: crate::scale::Scale + InfallibleScaleConvert<TAI>,
{
    #[inline]
    fn convert(src: Time<S>) -> Self::Output {
        EncodedTime::from_time_infallible(src.to_scale::<TAI>())
    }
}

impl<S> ContextConversionTarget<S> for GPS
where
    S: crate::scale::Scale + crate::scale::conversion::ContextScaleConvert<TAI>,
{
    type Output = EncodedTime<TAI, GPS>;

    #[inline]
    fn convert_with(src: Time<S>, ctx: &TimeContext) -> Result<Self::Output, ConversionError> {
        let tai = src.to_scale_with::<TAI>(ctx)?;
        Ok(EncodedTime::from_time_infallible(tai))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::data::active::{active_time_data, with_test_time_data};
    use crate::scale::{TAI, TT, UT1, UTC};

    #[test]
    fn encoded_time_display_delegates_to_quantity() {
        let jd = JulianDate::<TT>::try_new(Day::new(2_451_545.123_456_789)).unwrap();

        assert_eq!(format!("{jd:.9}"), "Julian Day 2451545.123456789 d");
    }

    #[test]
    fn encoded_time_lower_exp_delegates_to_quantity() {
        let seconds = J2000Seconds::<TT>::try_new(Second::new(1_234.5)).unwrap();
        let formatted = format!("{seconds:.2e}");

        assert!(formatted.contains("e"));
        assert!(formatted.ends_with(" s"));
    }

    #[test]
    #[allow(clippy::clone_on_copy)]
    fn encoded_time_core_helpers_and_day_arithmetic() {
        let base = JulianDate::<TT>::try_new(Day::new(2_451_545.0)).unwrap();
        let later = JulianDate::<TT>::try_new(Day::new(2_451_547.0)).unwrap();

        assert_eq!(base.clone(), base);
        assert!(format!("{base:?}").contains("Julian Day"));
        assert!(format!("{base:.2E}").ends_with(" d"));
        assert_eq!(base.raw(), Day::new(2_451_545.0));
        assert_eq!(base.quantity(), base.raw());
        assert_eq!(base.jd_value(), 2_451_545.0);
        assert_eq!(base.julian_centuries(), 0.0);
        assert_eq!(base.julian_millennias(), 0.0);

        assert_eq!(base.min(later), base);
        assert_eq!(later.min(base), base);
        assert_eq!(base.max(later), later);
        assert_eq!(later.max(base), later);
        assert_eq!(base.mean(later).raw(), Day::new(2_451_546.0));

        assert_eq!((base + Day::new(2.0)).raw(), later.raw());
        assert_eq!((later - Day::new(2.0)).raw(), base.raw());
        assert_eq!(later - base, Day::new(2.0));

        let mut shifted = base;
        shifted += Day::new(3.0);
        shifted -= Day::new(1.0);
        assert_eq!(shifted, later);

        let mjd = ModifiedJulianDate::<TT>::new(51_544.5);
        assert_eq!(mjd.mjd_value(), 51_544.5);
        assert_eq!(JulianDate::<TT>::from(mjd).raw(), base.raw());
        assert_eq!(
            ModifiedJulianDate::<TT>::from(base).raw(),
            Day::new(51_544.5)
        );
    }

    #[test]
    fn encoded_time_conversion_helpers_cover_targets() {
        let ctx = TimeContext::new();
        let seconds = J2000Seconds::<TT>::try_new(Second::new(86_400.0)).unwrap();

        let time = seconds.try_to_time().unwrap();
        assert_eq!(seconds.to_time_with(&ctx).unwrap(), time);
        assert_eq!(seconds.to_time(), time);

        let jd = seconds.to::<JD>();
        let mjd = seconds.try_to::<MJD>().unwrap();
        let ut1 = seconds.to_with::<UT1>(&ctx).unwrap();
        assert!(ut1.raw_seconds_pair().0.is_finite());

        assert_eq!(jd.raw(), Day::new(2_451_546.0));
        assert_eq!(mjd.raw(), Day::new(51_545.5));

        let time_from_encoded: Time<TT> = seconds.into();
        let encoded_from_time: J2000Seconds<TT> = time_from_encoded.into();
        assert_eq!(
            encoded_from_time,
            J2000Seconds::<TT>::try_new(Second::new(86_400.0)).unwrap()
        );

        assert!(J2000Seconds::<TT>::try_new(Second::new(f64::NAN)).is_err());
    }

    #[test]
    fn gps_and_unix_encoded_representations_roundtrip() {
        let ctx = TimeContext::new();
        let utc =
            Time::<UTC>::from_raw_unix_seconds_with(Second::new(946_728_000.0), &ctx).unwrap();

        let unix = utc.to_with::<Unix>(&ctx).unwrap();
        let utc_from_unix = unix.to_time_with(&ctx).unwrap();
        assert!((utc_from_unix - utc).abs() < Second::new(1e-4));

        let ut1_from_unix = unix.to_with::<UT1>(&ctx).unwrap();
        assert!(ut1_from_unix.raw_seconds_pair().0.is_finite());

        let tai = utc.to::<TAI>();
        let gps = tai.try_to::<GPS>().unwrap();
        assert_eq!(gps.try_to_time().unwrap(), tai);
        assert_eq!(gps.to_time_with(&ctx).unwrap(), tai);

        let gps_as_jd = gps.try_to::<JD>().unwrap();
        assert!(gps_as_jd.raw().is_finite());
    }

    #[test]
    fn tt_jd_and_mjd_chrono_helpers_roundtrip() {
        let bundle = active_time_data().as_ref().clone();
        with_test_time_data(bundle, || {
            let dt = chrono::DateTime::from_timestamp(946_728_000, 250_000_000).unwrap();

            let jd = JulianDate::<TT>::from_chrono(dt);
            let jd_back = jd.to_chrono().unwrap();
            let jd_delta_ns =
                jd_back.timestamp_nanos_opt().unwrap() - dt.timestamp_nanos_opt().unwrap();
            assert!(jd_delta_ns.abs() < 50_000);

            let mjd = ModifiedJulianDate::<TT>::from_chrono(dt);
            let mjd_back = mjd.to_chrono().unwrap();
            let mjd_delta_ns =
                mjd_back.timestamp_nanos_opt().unwrap() - dt.timestamp_nanos_opt().unwrap();
            assert!(mjd_delta_ns.abs() < 50_000);

            assert!(JulianDate::<TT>::J2000.tt_to_tdb().raw().is_finite());
        });
    }
}