Vulcan
Aerospace Engineering Utilities Built on Janus
Loading...
Searching...
No Matches
GPSTime.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// GPS Week Number Utilities
13// =============================================================================
14
22inline constexpr int GPS_WEEK_ROLLOVER = 1024;
23
27inline constexpr int GPS_CURRENT_ROLLOVER = 2;
28
39[[nodiscard]] inline constexpr int
40full_gps_week(int week_mod, int rollover_count = GPS_CURRENT_ROLLOVER) {
41 return week_mod + rollover_count * GPS_WEEK_ROLLOVER;
42}
43
52template <typename Scalar>
53[[nodiscard]] Scalar gps_week_to_utc_jd(int week, const Scalar &seconds_of_week,
54 int delta_at = 37) {
55 // GPS time as JD
56 Scalar gps_jd = constants::time::JD_GPS_EPOCH +
57 static_cast<double>(week) * 7.0 +
58 seconds_of_week / constants::time::SECONDS_PER_DAY;
59
60 // Convert GPS JD to UTC JD
61 return gps_to_utc(gps_jd, delta_at);
62}
63
67template <typename Scalar>
68[[nodiscard]] Scalar gps_week_to_tai_jd(int week,
69 const Scalar &seconds_of_week) {
70 Scalar gps_jd = constants::time::JD_GPS_EPOCH +
71 static_cast<double>(week) * 7.0 +
72 seconds_of_week / constants::time::SECONDS_PER_DAY;
73
74 return gps_to_tai(gps_jd);
75}
76
84[[nodiscard]] inline std::pair<int, double> utc_jd_to_gps_week(double utc_jd,
85 int delta_at) {
86 double gps_jd = utc_to_gps(utc_jd, delta_at);
87 double days_since_epoch = gps_jd - constants::time::JD_GPS_EPOCH;
88
89 int week = static_cast<int>(std::floor(days_since_epoch / 7.0));
90 double days_into_week = days_since_epoch - week * 7.0;
91 double seconds_of_week = days_into_week * constants::time::SECONDS_PER_DAY;
92
93 return {week, seconds_of_week};
94}
95
100[[nodiscard]] inline std::pair<int, double>
102 int delta_at = leap_seconds_at_utc(utc_jd);
103 return utc_jd_to_gps_week(utc_jd, delta_at);
104}
105
106// =============================================================================
107// GPS-UTC Offset
108// =============================================================================
109
119[[nodiscard]] inline int gps_utc_offset(double utc_jd) {
120 int delta_at = leap_seconds_at_utc(utc_jd);
121 return delta_at - static_cast<int>(constants::time::TAI_GPS_OFFSET);
122}
123
127[[nodiscard]] inline constexpr int gps_utc_offset(int delta_at) {
128 return delta_at - static_cast<int>(constants::time::TAI_GPS_OFFSET);
129}
130
131// =============================================================================
132// Day of Week Utilities
133// =============================================================================
134
149
153[[nodiscard]] inline GpsDayOfWeek gps_day_of_week(double seconds_of_week) {
154 int day = static_cast<int>(
155 std::floor(seconds_of_week / constants::time::SECONDS_PER_DAY));
156 return static_cast<GpsDayOfWeek>(day % 7);
157}
158
165[[nodiscard]] inline double gps_time_of_day(double seconds_of_week) {
166 return std::fmod(seconds_of_week, constants::time::SECONDS_PER_DAY);
167}
168
169} // namespace vulcan::time
constexpr double JD_GPS_EPOCH
Julian Date of GPS epoch (1980-01-06 00:00:00 UTC).
Definition TimeConstants.hpp:19
constexpr double SECONDS_PER_DAY
Seconds per day.
Definition TimeConstants.hpp:45
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
double gps_time_of_day(double seconds_of_week)
Get time of day from GPS seconds of week.
Definition GPSTime.hpp:165
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
std::pair< int, double > utc_jd_to_gps_week(double utc_jd, int delta_at)
Convert UTC Julian Date to GPS week and seconds.
Definition GPSTime.hpp:84
GpsDayOfWeek gps_day_of_week(double seconds_of_week)
Get GPS day of week from seconds into the week.
Definition GPSTime.hpp:153
Scalar gps_week_to_tai_jd(int week, const Scalar &seconds_of_week)
Convert GPS week/seconds to TAI Julian Date.
Definition GPSTime.hpp:68
constexpr int GPS_WEEK_ROLLOVER
GPS week number rollover constant.
Definition GPSTime.hpp:22
constexpr int GPS_CURRENT_ROLLOVER
Current GPS week rollover count (post-April 2019).
Definition GPSTime.hpp:27
constexpr int full_gps_week(int week_mod, int rollover_count=GPS_CURRENT_ROLLOVER)
Calculate full GPS week number from a potentially rolled-over week.
Definition GPSTime.hpp:40
std::pair< int, double > utc_jd_to_gps_week_auto(double utc_jd)
Convert UTC Julian Date to GPS week and seconds (auto leap second lookup).
Definition GPSTime.hpp:101
Scalar gps_week_to_utc_jd(int week, const Scalar &seconds_of_week, int delta_at=37)
Convert GPS week/seconds to UTC Julian Date.
Definition GPSTime.hpp:53
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
int gps_utc_offset(double utc_jd)
Get GPS - UTC offset for a given UTC Julian Date.
Definition GPSTime.hpp:119
GpsDayOfWeek
GPS day of week enumeration.
Definition GPSTime.hpp:140
@ Tuesday
Definition GPSTime.hpp:143
@ Monday
Definition GPSTime.hpp:142
@ Thursday
Definition GPSTime.hpp:145
@ Wednesday
Definition GPSTime.hpp:144
@ Saturday
Definition GPSTime.hpp:147
@ Sunday
Definition GPSTime.hpp:141
@ Friday
Definition GPSTime.hpp:146