|
| 1 | +#pragma once |
| 2 | + |
| 3 | +/** |
| 4 | + * @file scales.hpp |
| 5 | + * @brief Time-scale tag types and traits for the tempoch Time<S> template. |
| 6 | + * |
| 7 | + * Mirrors the Rust `tempoch_core::scales` module. Each tag is an empty struct |
| 8 | + * that selects the FFI functions used by `Time<S>`. |
| 9 | + * |
| 10 | + * Adding a new scale requires: |
| 11 | + * 1. Define a tag struct (e.g. `struct TTScale {};`). |
| 12 | + * 2. Specialise `TimeScaleTraits<TTScale>` with the FFI calls. |
| 13 | + * 3. Specialise `TimeConvertTraits<A,B>` for each supported conversion pair. |
| 14 | + */ |
| 15 | + |
| 16 | +#include "ffi_core.hpp" // tempoch_ffi.h + check_status |
| 17 | + |
| 18 | +namespace tempoch { |
| 19 | + |
| 20 | +// Forward declaration — defined in time_base.hpp. |
| 21 | +struct CivilTime; |
| 22 | + |
| 23 | +// ============================================================================ |
| 24 | +// Scale Tags |
| 25 | +// ============================================================================ |
| 26 | + |
| 27 | +/// Julian Date (days since −4712‑01‑01T12:00 TT). |
| 28 | +struct JDScale {}; |
| 29 | + |
| 30 | +/// Modified Julian Date (JD − 2 400 000.5). |
| 31 | +struct MJDScale {}; |
| 32 | + |
| 33 | +/// UTC, internally stored as MJD days for arithmetic. |
| 34 | +struct UTCScale {}; |
| 35 | + |
| 36 | +// Stubs for future FFI-backed scales — uncomment and specialise traits once |
| 37 | +// the FFI exposes the conversion functions. |
| 38 | +// struct TTScale {}; // Terrestrial Time |
| 39 | +// struct TAIScale {}; // International Atomic Time |
| 40 | +// struct TDBScale {}; // Barycentric Dynamical Time |
| 41 | +// struct TCGScale {}; // Geocentric Coordinate Time |
| 42 | +// struct TCBScale {}; // Barycentric Coordinate Time |
| 43 | +// struct GPSScale {}; // GPS Time |
| 44 | +// struct UTScale {}; // Universal Time (UT1) |
| 45 | + |
| 46 | +// ============================================================================ |
| 47 | +// TimeScaleTraits — per-scale FFI dispatch |
| 48 | +// ============================================================================ |
| 49 | + |
| 50 | +/** |
| 51 | + * @brief Primary template — must be specialised for every supported scale. |
| 52 | + * |
| 53 | + * Required static members: |
| 54 | + * - `const char* label()` |
| 55 | + * - `double from_utc(const CivilTime&)` — civil time → raw days |
| 56 | + * - `CivilTime to_utc(double days)` — raw days → civil time |
| 57 | + * - `double add_days(double days, double delta)` |
| 58 | + * - `double difference(double a, double b)` — a − b in days |
| 59 | + */ |
| 60 | +template <typename S> struct TimeScaleTraits { |
| 61 | + static_assert(sizeof(S) == 0, |
| 62 | + "TimeScaleTraits<S> must be specialised for this scale."); |
| 63 | +}; |
| 64 | + |
| 65 | +// ── JDScale ───────────────────────────────────────────────────────────────── |
| 66 | + |
| 67 | +template <> struct TimeScaleTraits<JDScale> { |
| 68 | + static constexpr const char *label() { return "JD"; } |
| 69 | + |
| 70 | + static double from_civil(const CivilTime &ct); |
| 71 | + static CivilTime to_civil(double jd); |
| 72 | + |
| 73 | + static double add_days(double jd, double delta) { |
| 74 | + return tempoch_jd_add_days(jd, delta); |
| 75 | + } |
| 76 | + |
| 77 | + static double difference(double a, double b) { |
| 78 | + return tempoch_jd_difference(a, b); |
| 79 | + } |
| 80 | + |
| 81 | + /// Difference as a typed `qtty_quantity_t` (Day unit). |
| 82 | + static qtty_quantity_t difference_qty(double a, double b) { |
| 83 | + return tempoch_jd_difference_qty(a, b); |
| 84 | + } |
| 85 | + |
| 86 | + /// Add a typed duration quantity and write result to @p out. |
| 87 | + static void add_qty(double jd, qtty_quantity_t duration, double &out) { |
| 88 | + check_status(tempoch_jd_add_qty(jd, duration, &out), "Time<JD>::add_qty"); |
| 89 | + } |
| 90 | + |
| 91 | + /// J2000.0 epoch constant (JD 2 451 545.0). |
| 92 | + static double j2000() { return tempoch_jd_j2000(); } |
| 93 | + |
| 94 | + /// Julian centuries elapsed since J2000. |
| 95 | + static double julian_centuries(double jd) { |
| 96 | + return tempoch_jd_julian_centuries(jd); |
| 97 | + } |
| 98 | + |
| 99 | + /// Julian centuries since J2000 as a typed `qtty_quantity_t` |
| 100 | + /// (JulianCentury unit). |
| 101 | + static qtty_quantity_t julian_centuries_qty(double jd) { |
| 102 | + return tempoch_jd_julian_centuries_qty(jd); |
| 103 | + } |
| 104 | +}; |
| 105 | + |
| 106 | +// ── MJDScale ──────────────────────────────────────────────────────────────── |
| 107 | + |
| 108 | +template <> struct TimeScaleTraits<MJDScale> { |
| 109 | + static constexpr const char *label() { return "MJD"; } |
| 110 | + |
| 111 | + static double from_civil(const CivilTime &ct); |
| 112 | + static CivilTime to_civil(double mjd); |
| 113 | + |
| 114 | + static double add_days(double mjd, double delta) { |
| 115 | + return tempoch_mjd_add_days(mjd, delta); |
| 116 | + } |
| 117 | + |
| 118 | + static double difference(double a, double b) { |
| 119 | + return tempoch_mjd_difference(a, b); |
| 120 | + } |
| 121 | + |
| 122 | + /// Difference as a typed `qtty_quantity_t` (Day unit). |
| 123 | + static qtty_quantity_t difference_qty(double a, double b) { |
| 124 | + return tempoch_mjd_difference_qty(a, b); |
| 125 | + } |
| 126 | + |
| 127 | + /// Add a typed duration quantity and write result to @p out. |
| 128 | + static void add_qty(double mjd, qtty_quantity_t duration, double &out) { |
| 129 | + check_status(tempoch_mjd_add_qty(mjd, duration, &out), |
| 130 | + "Time<MJD>::add_qty"); |
| 131 | + } |
| 132 | +}; |
| 133 | + |
| 134 | +// ── UTCScale (internally stored as MJD) ───────────────────────────────────── |
| 135 | + |
| 136 | +template <> struct TimeScaleTraits<UTCScale> { |
| 137 | + static constexpr const char *label() { return "UTC"; } |
| 138 | + |
| 139 | + static double from_civil(const CivilTime &ct); |
| 140 | + static CivilTime to_civil(double mjd); |
| 141 | + |
| 142 | + static double add_days(double mjd, double delta) { |
| 143 | + return tempoch_mjd_add_days(mjd, delta); |
| 144 | + } |
| 145 | + |
| 146 | + static double difference(double a, double b) { |
| 147 | + return tempoch_mjd_difference(a, b); |
| 148 | + } |
| 149 | + |
| 150 | + /// Difference as a typed `qtty_quantity_t` (Day unit). |
| 151 | + static qtty_quantity_t difference_qty(double a, double b) { |
| 152 | + return TimeScaleTraits<MJDScale>::difference_qty(a, b); |
| 153 | + } |
| 154 | + |
| 155 | + /// Add a typed duration quantity and write result to @p out. |
| 156 | + static void add_qty(double mjd, qtty_quantity_t duration, double &out) { |
| 157 | + TimeScaleTraits<MJDScale>::add_qty(mjd, duration, out); |
| 158 | + } |
| 159 | +}; |
| 160 | + |
| 161 | +// ============================================================================ |
| 162 | +// TimeConvertTraits — cross-scale conversion |
| 163 | +// ============================================================================ |
| 164 | + |
| 165 | +/** |
| 166 | + * @brief Primary template — specialise for each supported A→B pair. |
| 167 | + * |
| 168 | + * Required: `static double convert(double src_days)`. |
| 169 | + */ |
| 170 | +template <typename From, typename To> struct TimeConvertTraits { |
| 171 | + static_assert(sizeof(From) == 0, |
| 172 | + "TimeConvertTraits<From,To> is not specialised for this pair."); |
| 173 | +}; |
| 174 | + |
| 175 | +// ── JD ↔ MJD ──────────────────────────────────────────────────────────────── |
| 176 | + |
| 177 | +template <> struct TimeConvertTraits<JDScale, MJDScale> { |
| 178 | + static double convert(double jd) { return tempoch_jd_to_mjd(jd); } |
| 179 | +}; |
| 180 | + |
| 181 | +template <> struct TimeConvertTraits<MJDScale, JDScale> { |
| 182 | + static double convert(double mjd) { return tempoch_mjd_to_jd(mjd); } |
| 183 | +}; |
| 184 | + |
| 185 | +// ── JD ↔ UTC (UTC stored as MJD internally) ──────────────────────────────── |
| 186 | + |
| 187 | +template <> struct TimeConvertTraits<JDScale, UTCScale> { |
| 188 | + static double convert(double jd) { return tempoch_jd_to_mjd(jd); } |
| 189 | +}; |
| 190 | + |
| 191 | +template <> struct TimeConvertTraits<UTCScale, JDScale> { |
| 192 | + static double convert(double mjd) { return tempoch_mjd_to_jd(mjd); } |
| 193 | +}; |
| 194 | + |
| 195 | +// ── MJD ↔ UTC (identity — both stored as MJD) ────────────────────────────── |
| 196 | + |
| 197 | +template <> struct TimeConvertTraits<MJDScale, UTCScale> { |
| 198 | + static double convert(double mjd) { return mjd; } |
| 199 | +}; |
| 200 | + |
| 201 | +template <> struct TimeConvertTraits<UTCScale, MJDScale> { |
| 202 | + static double convert(double mjd) { return mjd; } |
| 203 | +}; |
| 204 | + |
| 205 | +} // namespace tempoch |
0 commit comments