Vulcan
Aerospace Engineering Utilities Built on Janus
Loading...
Searching...
No Matches
TimeScales.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <cmath>
4#include <janus/janus.hpp>
8
9namespace vulcan::time {
10
11// =============================================================================
12// Enumeration of Time Scales
13// =============================================================================
14
23
24// =============================================================================
25// TAI ↔ TT Conversions (Templated - Fixed Offset)
26// =============================================================================
27
35template <typename Scalar>
36[[nodiscard]] constexpr Scalar tai_to_tt(const Scalar &tai_jd) {
37 return tai_jd +
39}
40
44template <typename Scalar>
45[[nodiscard]] constexpr Scalar tt_to_tai(const Scalar &tt_jd) {
46 return tt_jd -
48}
49
50// =============================================================================
51// TAI ↔ GPS Conversions (Templated - Fixed Offset)
52// =============================================================================
53
59template <typename Scalar>
60[[nodiscard]] constexpr Scalar tai_to_gps(const Scalar &tai_jd) {
61 return tai_jd +
63}
64
68template <typename Scalar>
69[[nodiscard]] constexpr Scalar gps_to_tai(const Scalar &gps_jd) {
70 return gps_jd +
72}
73
74// =============================================================================
75// TAI ↔ UTC Conversions (Templated with Leap Second Parameter)
76// =============================================================================
77
88template <typename Scalar>
89[[nodiscard]] constexpr Scalar utc_to_tai(const Scalar &utc_jd, int delta_at) {
90 return utc_jd +
91 static_cast<double>(delta_at) / constants::time::SECONDS_PER_DAY;
92}
93
102template <typename Scalar>
103[[nodiscard]] constexpr Scalar tai_to_utc(const Scalar &tai_jd, int delta_at) {
104 return tai_jd -
105 static_cast<double>(delta_at) / constants::time::SECONDS_PER_DAY;
106}
107
108// =============================================================================
109// TAI ↔ UTC Conversions (Numeric with Automatic Lookup)
110// =============================================================================
111
117[[nodiscard]] inline double utc_to_tai(double utc_jd) {
118 int delta_at = leap_seconds_at_utc(utc_jd);
119 return utc_to_tai(utc_jd, delta_at);
120}
121
125[[nodiscard]] inline double tai_to_utc(double tai_jd) {
126 int delta_at = leap_seconds_at_tai(tai_jd);
127 return tai_to_utc(tai_jd, delta_at);
128}
129
130// =============================================================================
131// UTC ↔ GPS Conversions (Convenience)
132// =============================================================================
133
139template <typename Scalar>
140[[nodiscard]] constexpr Scalar utc_to_gps(const Scalar &utc_jd, int delta_at) {
141 Scalar tai_jd = utc_to_tai(utc_jd, delta_at);
142 return tai_to_gps(tai_jd);
143}
144
148template <typename Scalar>
149[[nodiscard]] constexpr Scalar gps_to_utc(const Scalar &gps_jd, int delta_at) {
150 Scalar tai_jd = gps_to_tai(gps_jd);
151 return tai_to_utc(tai_jd, delta_at);
152}
153
157[[nodiscard]] inline double utc_to_gps(double utc_jd) {
158 int delta_at = leap_seconds_at_utc(utc_jd);
159 return utc_to_gps(utc_jd, delta_at);
160}
161
165[[nodiscard]] inline double gps_to_utc(double gps_jd) {
166 // GPS JD to TAI JD, then lookup
167 double tai_jd = gps_to_tai(gps_jd);
168 int delta_at = leap_seconds_at_tai(tai_jd);
169 return gps_to_utc(gps_jd, delta_at);
170}
171
172// =============================================================================
173// TT ↔ TDB Conversions (Templated - Periodic Approximation)
174// =============================================================================
175
188template <typename Scalar> [[nodiscard]] Scalar tt_to_tdb(const Scalar &tt_jd) {
189 // Julian centuries since J2000.0 TT
190 Scalar T = jd_to_j2000_centuries(tt_jd);
191
192 // Mean anomaly of Earth around Sun (radians)
193 // g = 357.5277233° + 35999.05034° * T
194 Scalar g = (357.5277233 + 35999.05034 * T) * M_PI / 180.0;
195
196 // TDB - TT in seconds (simplified Fairhead & Bretagnon)
197 Scalar dt = 0.001657 * janus::sin(g);
198
199 return tt_jd + dt / constants::time::SECONDS_PER_DAY;
200}
201
208template <typename Scalar>
209[[nodiscard]] Scalar tdb_to_tt(const Scalar &tdb_jd) {
210 // First approximation: TT ≈ TDB
211 Scalar T = jd_to_j2000_centuries(tdb_jd);
212
213 // Mean anomaly
214 Scalar g = (357.5277233 + 35999.05034 * T) * M_PI / 180.0;
215
216 // Correction
217 Scalar dt = 0.001657 * janus::sin(g);
218
219 return tdb_jd - dt / constants::time::SECONDS_PER_DAY;
220}
221
222// =============================================================================
223// Symbolic UTC ↔ TAI with Interpolated Leap Seconds
224// =============================================================================
225
236template <typename Scalar>
237[[nodiscard]] Scalar utc_to_tai_symbolic(const Scalar &utc_jd) {
238 Scalar delta_at = leap_seconds_symbolic(utc_jd);
239 return utc_jd + delta_at / constants::time::SECONDS_PER_DAY;
240}
241
249template <typename Scalar>
250[[nodiscard]] Scalar tai_to_utc_symbolic(const Scalar &tai_jd) {
251 // Approximate: use TAI JD in interpolator (error < 37 seconds offset)
252 // This is acceptable for smooth optimization purposes
253 Scalar delta_at = leap_seconds_symbolic(tai_jd);
254 return tai_jd - delta_at / constants::time::SECONDS_PER_DAY;
255}
256
257} // namespace vulcan::time
constexpr double SECONDS_PER_DAY
Seconds per day.
Definition TimeConstants.hpp:45
constexpr double TT_TAI_OFFSET
TT - TAI offset [s] (exact, by definition).
Definition TimeConstants.hpp:32
constexpr double GPS_TAI_OFFSET
GPS - TAI offset [s] (exact: GPS = TAI - 19s at GPS epoch).
Definition TimeConstants.hpp:35
constexpr double TAI_GPS_OFFSET
TAI - GPS offset [s].
Definition TimeConstants.hpp:38
Definition Epoch.hpp:12
constexpr Scalar gps_to_tai(const Scalar &gps_jd)
Convert GPS Julian Date to TAI Julian Date.
Definition TimeScales.hpp:69
constexpr Scalar utc_to_gps(const Scalar &utc_jd, int delta_at)
Convert UTC Julian Date to GPS Julian Date (templated).
Definition TimeScales.hpp:140
int leap_seconds_at_utc(double utc_jd)
Get TAI - UTC offset for a given UTC Julian Date.
Definition LeapSeconds.hpp:93
constexpr Scalar utc_to_tai(const Scalar &utc_jd, int delta_at)
Convert UTC Julian Date to TAI Julian Date (templated).
Definition TimeScales.hpp:89
constexpr Scalar tt_to_tai(const Scalar &tt_jd)
Convert TT Julian Date to TAI Julian Date.
Definition TimeScales.hpp:45
Scalar tdb_to_tt(const Scalar &tdb_jd)
Convert TDB Julian Date to TT Julian Date.
Definition TimeScales.hpp:209
constexpr Scalar tai_to_utc(const Scalar &tai_jd, int delta_at)
Convert TAI Julian Date to UTC Julian Date (templated).
Definition TimeScales.hpp:103
Scalar tt_to_tdb(const Scalar &tt_jd)
Convert TT Julian Date to TDB Julian Date.
Definition TimeScales.hpp:188
TimeScale
Definition TimeScales.hpp:15
@ UT1
Universal Time 1 (Earth rotation, requires IERS data).
Definition TimeScales.hpp:21
@ GPS
GPS Time (GPS = TAI - 19s, continuous since 1980).
Definition TimeScales.hpp:20
@ UTC
Coordinated Universal Time (civil time, has leap seconds).
Definition TimeScales.hpp:16
@ TAI
International Atomic Time (continuous SI seconds).
Definition TimeScales.hpp:17
@ TDB
Barycentric Dynamical Time (for solar system dynamics).
Definition TimeScales.hpp:19
@ TT
Terrestrial Time (TT = TAI + 32.184s).
Definition TimeScales.hpp:18
int leap_seconds_at_tai(double tai_jd)
Get TAI - UTC offset (leap seconds) for a given TAI Julian Date.
Definition LeapSeconds.hpp:58
Scalar leap_seconds_symbolic(const Scalar &utc_jd)
Symbolic leap second lookup (smooth approximation).
Definition LeapSeconds.hpp:213
constexpr Scalar jd_to_j2000_centuries(const Scalar &jd)
Convert Julian Date to Julian centuries since J2000.0.
Definition JulianDate.hpp:140
Scalar tai_to_utc_symbolic(const Scalar &tai_jd)
Convert TAI Julian Date to UTC Julian Date (fully symbolic).
Definition TimeScales.hpp:250
constexpr Scalar gps_to_utc(const Scalar &gps_jd, int delta_at)
Convert GPS Julian Date to UTC Julian Date (templated).
Definition TimeScales.hpp:149
Scalar utc_to_tai_symbolic(const Scalar &utc_jd)
Convert UTC Julian Date to TAI Julian Date (fully symbolic).
Definition TimeScales.hpp:237
constexpr Scalar tai_to_tt(const Scalar &tai_jd)
Convert TAI Julian Date to TT Julian Date.
Definition TimeScales.hpp:36
constexpr Scalar tai_to_gps(const Scalar &tai_jd)
Convert TAI Julian Date to GPS Julian Date.
Definition TimeScales.hpp:60