1 #ifndef HOPS_LEAP_SECONDS_HH__
2 #define HOPS_LEAP_SECONDS_HH__
13 #include "date/date.h"
68 static const std::int64_t kTaiEpochOffset = 378691210;
69 static const std::int64_t kGpsEpochOffset = 315964809;
85 static const std::vector< leap_entry > table = [] {
120 std::vector< leap_entry >
t;
121 t.reserve(
sizeof(events) /
sizeof(events[0]));
122 std::int64_t cumulative = 0;
123 for(std::size_t i = 0; i <
sizeof(events) /
sizeof(events[0]); ++i)
125 date::year_month_day d{
date::year{events[i].y}, date::month{
static_cast< unsigned >(events[i].m)},
126 date::day{
static_cast< unsigned >(events[i].d)}};
127 std::int64_t transition =
128 std::chrono::duration_cast< std::chrono::seconds >(date::sys_days{d}.time_since_epoch()).count();
129 cumulative += events[i].delta;
141 const std::vector< leap_entry >& T =
leap_table();
142 std::int64_t offset = 0;
143 for(std::size_t k = 0; k < T.size(); ++k)
145 if(sys_seconds >= T[k].transition_sys)
147 offset = T[k].cumulative;
164 template<
class Duration >
using utc_time = std::chrono::time_point< utc_clock, Duration >;
165 template<
class Duration >
using tai_time = std::chrono::time_point< tai_clock, Duration >;
166 template<
class Duration >
using gps_time = std::chrono::time_point< gps_clock, Duration >;
173 using namespace leap_detail;
174 const std::vector< leap_entry >& T =
leap_table();
176 std::int64_t u = std::chrono::duration_cast< std::chrono::seconds >(ut.time_since_epoch()).count();
185 std::int64_t elapsed = 0;
187 for(std::size_t k = 0; k < T.size(); ++k)
189 std::int64_t prev = (k == 0) ? 0 : T[k - 1].cumulative;
190 std::int64_t threshold = T[k].transition_sys +
std::min(prev, T[k].cumulative);
193 elapsed = T[k].cumulative;
194 selected =
static_cast< long >(k);
205 std::size_t k =
static_cast< std::size_t
>(selected);
206 std::int64_t prev = (k == 0) ? 0 : T[k - 1].cumulative;
207 std::int64_t delta = T[k].cumulative - prev;
208 std::int64_t threshold = T[k].transition_sys +
std::min(prev, T[k].cumulative);
210 is_ls = (delta > 0) && (u == threshold);
220 using duration = std::chrono::system_clock::duration;
221 using rep = duration::rep;
226 template<
class Duration >
230 using CD =
typename std::common_type< Duration, std::chrono::seconds >::type;
231 std::int64_t s = std::chrono::duration_cast< std::chrono::seconds >(st.time_since_epoch()).count();
236 template<
class Duration >
237 static date::sys_time< typename std::common_type< Duration, std::chrono::seconds >::type >
240 using CD =
typename std::common_type< Duration, std::chrono::seconds >::type;
242 return date::sys_time< CD >{ut.time_since_epoch() - info.
elapsed};
245 template<
class Duration >
246 static date::local_time< typename std::common_type< Duration, std::chrono::seconds >::type >
249 using CD =
typename std::common_type< Duration, std::chrono::seconds >::type;
250 return date::local_time< CD >{
to_sys(ut).time_since_epoch()};
253 template<
class Duration >
257 using CD =
typename std::common_type< Duration, std::chrono::seconds >::type;
258 return from_sys(date::sys_time< Duration >{lt.time_since_epoch()});
263 return from_sys(std::chrono::time_point_cast< std::chrono::nanoseconds >(std::chrono::system_clock::now()));
272 using duration = std::chrono::system_clock::duration;
273 using rep = duration::rep;
280 return utc_time< Duration >{
t.time_since_epoch() - std::chrono::seconds{leap_detail::kTaiEpochOffset}};
285 return tai_time< Duration >{
t.time_since_epoch() + std::chrono::seconds{leap_detail::kTaiEpochOffset}};
294 using duration = std::chrono::system_clock::duration;
295 using rep = duration::rep;
302 return utc_time< Duration >{
t.time_since_epoch() + std::chrono::seconds{leap_detail::kGpsEpochOffset}};
307 return gps_time< Duration >{
t.time_since_epoch() - std::chrono::seconds{leap_detail::kGpsEpochOffset}};
#define min(a, b)
Definition: max555.c:9
short day
Definition: mk4_typedefs.h:17
short year
Definition: mk4_typedefs.h:16
std::int64_t cumulative
Definition: hops_leap_seconds.hh:80
const std::vector< leap_entry > & leap_table()
Definition: hops_leap_seconds.hh:83
std::int64_t offset_at_sys(std::int64_t sys_seconds)
Definition: hops_leap_seconds.hh:139
std::int64_t transition_sys
Definition: hops_leap_seconds.hh:79
Definition: hops_leap_seconds.hh:78
t
Definition: picking_aedit.py:14
Definition: MHO_AdhocFlagging.hh:18
std::chrono::seconds elapsed
Definition: hops_leap_seconds.hh:58
leap_second_info get_leap_second_info(const utc_time< Duration > &ut)
Leap-second lookup for a utc_time, matching date::get_leap_second_info.
Definition: hops_leap_seconds.hh:171
bool is_leap_second
Definition: hops_leap_seconds.hh:59
std::chrono::time_point< gps_clock, Duration > gps_time
Definition: hops_leap_seconds.hh:166
std::chrono::time_point< utc_clock, Duration > utc_time
Definition: hops_leap_seconds.hh:164
std::chrono::time_point< tai_clock, Duration > tai_time
Definition: hops_leap_seconds.hh:165
Result of a leap-second lookup, mirroring date::leap_second_info. elapsed = number of leap seconds in...
Definition: hops_leap_seconds.hh:57
GPS clock: SI seconds since 1980-01-06; constant offset from UTC.
Definition: hops_leap_seconds.hh:293
static utc_time< Duration > to_utc(const gps_time< Duration > &t)
Definition: hops_leap_seconds.hh:300
duration::period period
Definition: hops_leap_seconds.hh:296
static CONSTDATA bool is_steady
Definition: hops_leap_seconds.hh:298
std::chrono::time_point< gps_clock > time_point
Definition: hops_leap_seconds.hh:297
duration::rep rep
Definition: hops_leap_seconds.hh:295
std::chrono::system_clock::duration duration
Definition: hops_leap_seconds.hh:294
static gps_time< Duration > from_utc(const utc_time< Duration > &t)
Definition: hops_leap_seconds.hh:305
TAI clock: SI seconds since 1958-01-01; constant offset from UTC.
Definition: hops_leap_seconds.hh:271
static utc_time< Duration > to_utc(const tai_time< Duration > &t)
Definition: hops_leap_seconds.hh:278
duration::period period
Definition: hops_leap_seconds.hh:274
static CONSTDATA bool is_steady
Definition: hops_leap_seconds.hh:276
std::chrono::system_clock::duration duration
Definition: hops_leap_seconds.hh:272
duration::rep rep
Definition: hops_leap_seconds.hh:273
static tai_time< Duration > from_utc(const utc_time< Duration > &t)
Definition: hops_leap_seconds.hh:283
std::chrono::time_point< tai_clock > time_point
Definition: hops_leap_seconds.hh:275
UTC clock: real SI seconds since 1970-01-01 including leap seconds.
Definition: hops_leap_seconds.hh:219
std::chrono::system_clock::duration duration
Definition: hops_leap_seconds.hh:220
static date::sys_time< typename std::common_type< Duration, std::chrono::seconds >::type > to_sys(const utc_time< Duration > &ut)
Definition: hops_leap_seconds.hh:238
std::chrono::time_point< utc_clock > time_point
Definition: hops_leap_seconds.hh:223
static CONSTDATA bool is_steady
Definition: hops_leap_seconds.hh:224
duration::rep rep
Definition: hops_leap_seconds.hh:221
static utc_time< typename std::common_type< Duration, std::chrono::seconds >::type > from_local(const date::local_time< Duration > <)
Definition: hops_leap_seconds.hh:255
duration::period period
Definition: hops_leap_seconds.hh:222
static date::local_time< typename std::common_type< Duration, std::chrono::seconds >::type > to_local(const utc_time< Duration > &ut)
Definition: hops_leap_seconds.hh:247
static utc_time< std::chrono::nanoseconds > now()
Definition: hops_leap_seconds.hh:261
static utc_time< typename std::common_type< Duration, std::chrono::seconds >::type > from_sys(const date::sys_time< Duration > &st)
Definition: hops_leap_seconds.hh:228