tempoch-cpp
Header-only C++ wrapper for tempoch
Loading...
Searching...
No Matches
time.hpp
Go to the documentation of this file.
1#pragma once
2
10#include "ffi_core.hpp"
11
12namespace tempoch {
13
14// ============================================================================
15// UTC
16// ============================================================================
17
28struct UTC {
30 int32_t year;
32 uint8_t month;
34 uint8_t day;
36 uint8_t hour;
38 uint8_t minute;
40 uint8_t second;
42 uint32_t nanosecond;
43
45 UTC() : year(2000), month(1), day(1), hour(12), minute(0), second(0), nanosecond(0) {}
46
57 UTC(int32_t y, uint8_t mo, uint8_t d,
58 uint8_t h = 0, uint8_t mi = 0, uint8_t s = 0, uint32_t ns = 0)
59 : year(y), month(mo), day(d), hour(h), minute(mi), second(s), nanosecond(ns) {}
60
62 tempoch_utc_t to_c() const {
63 return {year, month, day, hour, minute, second, nanosecond};
64 }
65
67 static UTC from_c(const tempoch_utc_t& c) {
68 return UTC(c.year, c.month, c.day, c.hour, c.minute, c.second, c.nanosecond);
69 }
70};
71
72// ============================================================================
73// JulianDate
74// ============================================================================
75
90 double m_value;
91
92public:
93 constexpr explicit JulianDate(double v) : m_value(v) {}
94
96 static JulianDate J2000() { return JulianDate(tempoch_jd_j2000()); }
97
99 static JulianDate from_utc(const UTC& utc) {
100 double jd;
101 auto c = utc.to_c();
102 check_status(tempoch_jd_from_utc(c, &jd), "JulianDate::from_utc");
103 return JulianDate(jd);
104 }
105
107 constexpr double value() const { return m_value; }
108
110 double to_mjd() const { return tempoch_jd_to_mjd(m_value); }
111
113 UTC to_utc() const {
114 tempoch_utc_t out;
115 check_status(tempoch_jd_to_utc(m_value, &out), "JulianDate::to_utc");
116 return UTC::from_c(out);
117 }
118
120 double operator-(const JulianDate& other) const {
121 return tempoch_jd_difference(m_value, other.m_value);
122 }
123
125 JulianDate operator+(double days) const {
126 return JulianDate(tempoch_jd_add_days(m_value, days));
127 }
128
130 double julian_centuries() const {
131 return tempoch_jd_julian_centuries(m_value);
132 }
133
134 bool operator==(const JulianDate& o) const { return m_value == o.m_value; }
135 bool operator!=(const JulianDate& o) const { return m_value != o.m_value; }
136 bool operator< (const JulianDate& o) const { return m_value < o.m_value; }
137 bool operator<=(const JulianDate& o) const { return m_value <= o.m_value; }
138 bool operator> (const JulianDate& o) const { return m_value > o.m_value; }
139 bool operator>=(const JulianDate& o) const { return m_value >= o.m_value; }
140};
141
142// ============================================================================
143// MJD (Modified Julian Date)
144// ============================================================================
145
158class MJD {
159 double m_value;
160
161public:
162 constexpr MJD() : m_value(0.0) {}
163 constexpr explicit MJD(double v) : m_value(v) {}
164
166 static MJD from_utc(const UTC& utc) {
167 double mjd;
168 auto c = utc.to_c();
169 check_status(tempoch_mjd_from_utc(c, &mjd), "MJD::from_utc");
170 return MJD(mjd);
171 }
172
174 static MJD from_jd(const JulianDate& jd) {
175 return MJD(tempoch_jd_to_mjd(jd.value()));
176 }
177
179 constexpr double value() const { return m_value; }
180
182 JulianDate to_jd() const { return JulianDate(tempoch_mjd_to_jd(m_value)); }
183
185 UTC to_utc() const {
186 tempoch_utc_t out;
187 check_status(tempoch_mjd_to_utc(m_value, &out), "MJD::to_utc");
188 return UTC::from_c(out);
189 }
190
192 double operator-(const MJD& other) const {
193 return tempoch_mjd_difference(m_value, other.m_value);
194 }
195
197 MJD operator+(double days) const {
198 return MJD(tempoch_mjd_add_days(m_value, days));
199 }
200
201 bool operator==(const MJD& o) const { return m_value == o.m_value; }
202 bool operator!=(const MJD& o) const { return m_value != o.m_value; }
203 bool operator< (const MJD& o) const { return m_value < o.m_value; }
204 bool operator<=(const MJD& o) const { return m_value <= o.m_value; }
205 bool operator> (const MJD& o) const { return m_value > o.m_value; }
206 bool operator>=(const MJD& o) const { return m_value >= o.m_value; }
207};
208
209} // namespace tempoch
Julian Date wrapper (value type).
Definition time.hpp:89
static JulianDate from_utc(const UTC &utc)
Create from a UTC date-time.
Definition time.hpp:99
bool operator!=(const JulianDate &o) const
Definition time.hpp:135
static JulianDate J2000()
J2000.0 epoch (2451545.0).
Definition time.hpp:96
double to_mjd() const
Convert to MJD.
Definition time.hpp:110
bool operator>(const JulianDate &o) const
Definition time.hpp:138
UTC to_utc() const
Convert to UTC.
Definition time.hpp:113
double julian_centuries() const
Julian centuries since J2000.
Definition time.hpp:130
bool operator>=(const JulianDate &o) const
Definition time.hpp:139
constexpr JulianDate(double v)
Definition time.hpp:93
double operator-(const JulianDate &other) const
Difference in days (this – other).
Definition time.hpp:120
constexpr double value() const
Raw value.
Definition time.hpp:107
JulianDate operator+(double days) const
Add days.
Definition time.hpp:125
bool operator<=(const JulianDate &o) const
Definition time.hpp:137
bool operator==(const JulianDate &o) const
Definition time.hpp:134
bool operator<(const JulianDate &o) const
Definition time.hpp:136
Modified Julian Date wrapper (value type).
Definition time.hpp:158
constexpr MJD()
Definition time.hpp:162
MJD operator+(double days) const
Add days.
Definition time.hpp:197
constexpr double value() const
Raw value.
Definition time.hpp:179
double operator-(const MJD &other) const
Difference in days (this – other).
Definition time.hpp:192
bool operator<(const MJD &o) const
Definition time.hpp:203
static MJD from_jd(const JulianDate &jd)
Create from a Julian Date.
Definition time.hpp:174
JulianDate to_jd() const
Convert to JD.
Definition time.hpp:182
bool operator>(const MJD &o) const
Definition time.hpp:205
bool operator==(const MJD &o) const
Definition time.hpp:201
bool operator<=(const MJD &o) const
Definition time.hpp:204
constexpr MJD(double v)
Definition time.hpp:163
bool operator>=(const MJD &o) const
Definition time.hpp:206
UTC to_utc() const
Convert to UTC.
Definition time.hpp:185
static MJD from_utc(const UTC &utc)
Create from a UTC date-time.
Definition time.hpp:166
bool operator!=(const MJD &o) const
Definition time.hpp:202
Error handling for the tempoch C++ wrapper.
void check_status(tempoch_status_t status, const char *operation)
Check a tempoch_status_t and throw the appropriate exception on error.
Definition ffi_core.hpp:71
UTC date-time breakdown.
Definition time.hpp:28
static UTC from_c(const tempoch_utc_t &c)
Create from the C FFI struct.
Definition time.hpp:67
uint8_t hour
Hour in range [0, 23].
Definition time.hpp:36
uint8_t second
Second in range [0, 60], leap second aware.
Definition time.hpp:40
uint8_t day
Day of month in range [1, 31].
Definition time.hpp:34
uint32_t nanosecond
Nanosecond component in range [0, 999,999,999].
Definition time.hpp:42
tempoch_utc_t to_c() const
Convert to the C FFI struct.
Definition time.hpp:62
int32_t year
Gregorian year (astronomical year numbering).
Definition time.hpp:30
uint8_t month
Month in range [1, 12].
Definition time.hpp:32
UTC()
Default constructor: J2000 epoch noon-like civil representation.
Definition time.hpp:45
UTC(int32_t y, uint8_t mo, uint8_t d, uint8_t h=0, uint8_t mi=0, uint8_t s=0, uint32_t ns=0)
Construct from civil UTC components.
Definition time.hpp:57
uint8_t minute
Minute in range [0, 59].
Definition time.hpp:38