1 #ifndef HOPS_LEAP_SECONDS_HH__
2 #define HOPS_LEAP_SECONDS_HH__
13 #include "date/date.h"
61 static const std::int64_t kTaiEpochOffset = 378691210;
62 static const std::int64_t kGpsEpochOffset = 315964809;
78 static const std::vector< leap_entry > table = [] {
113 std::vector< leap_entry >
t;
114 t.reserve(
sizeof(events) /
sizeof(events[0]));
115 std::int64_t cumulative = 0;
116 for(std::size_t i = 0; i <
sizeof(events) /
sizeof(events[0]); ++i)
118 date::year_month_day d{
date::year{events[i].y}, date::month{
static_cast< unsigned >(events[i].m)},
119 date::day{
static_cast< unsigned >(events[i].d)}};
120 std::int64_t transition =
121 std::chrono::duration_cast< std::chrono::seconds >(date::sys_days{d}.time_since_epoch()).count();
122 cumulative += events[i].delta;
134 const std::vector< leap_entry >& T =
leap_table();
135 std::int64_t offset = 0;
136 for(std::size_t k = 0; k < T.size(); ++k)
138 if(sys_seconds >= T[k].transition_sys)
140 offset = T[k].cumulative;
157 template<
class Duration >
using utc_time = std::chrono::time_point< utc_clock, Duration >;
158 template<
class Duration >
using tai_time = std::chrono::time_point< tai_clock, Duration >;
159 template<
class Duration >
using gps_time = std::chrono::time_point< gps_clock, Duration >;
166 using namespace leap_detail;
167 const std::vector< leap_entry >& T =
leap_table();
169 std::int64_t u = std::chrono::duration_cast< std::chrono::seconds >(ut.time_since_epoch()).count();
178 std::int64_t elapsed = 0;
180 for(std::size_t k = 0; k < T.size(); ++k)
182 std::int64_t prev = (k == 0) ? 0 : T[k - 1].cumulative;
183 std::int64_t threshold = T[k].transition_sys +
std::min(prev, T[k].cumulative);
186 elapsed = T[k].cumulative;
187 selected =
static_cast< long >(k);
198 std::size_t k =
static_cast< std::size_t
>(selected);
199 std::int64_t prev = (k == 0) ? 0 : T[k - 1].cumulative;
200 std::int64_t delta = T[k].cumulative - prev;
201 std::int64_t threshold = T[k].transition_sys +
std::min(prev, T[k].cumulative);
203 is_ls = (delta > 0) && (u == threshold);
213 using duration = std::chrono::system_clock::duration;
214 using rep = duration::rep;
219 template<
class Duration >
223 using CD =
typename std::common_type< Duration, std::chrono::seconds >::type;
224 std::int64_t s = std::chrono::duration_cast< std::chrono::seconds >(st.time_since_epoch()).count();
229 template<
class Duration >
230 static date::sys_time< typename std::common_type< Duration, std::chrono::seconds >::type >
233 using CD =
typename std::common_type< Duration, std::chrono::seconds >::type;
235 return date::sys_time< CD >{ut.time_since_epoch() - info.
elapsed};
238 template<
class Duration >
239 static date::local_time< typename std::common_type< Duration, std::chrono::seconds >::type >
242 using CD =
typename std::common_type< Duration, std::chrono::seconds >::type;
243 return date::local_time< CD >{
to_sys(ut).time_since_epoch()};
246 template<
class Duration >
250 return from_sys(date::sys_time< Duration >{lt.time_since_epoch()});
255 return from_sys(std::chrono::time_point_cast< std::chrono::nanoseconds >(std::chrono::system_clock::now()));
264 using duration = std::chrono::system_clock::duration;
265 using rep = duration::rep;
272 return utc_time< Duration >{
t.time_since_epoch() - std::chrono::seconds{leap_detail::kTaiEpochOffset}};
277 return tai_time< Duration >{
t.time_since_epoch() + std::chrono::seconds{leap_detail::kTaiEpochOffset}};
286 using duration = std::chrono::system_clock::duration;
287 using rep = duration::rep;
294 return utc_time< Duration >{
t.time_since_epoch() + std::chrono::seconds{leap_detail::kGpsEpochOffset}};
299 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:73
const std::vector< leap_entry > & leap_table()
Definition: hops_leap_seconds.hh:76
std::int64_t offset_at_sys(std::int64_t sys_seconds)
Definition: hops_leap_seconds.hh:132
std::int64_t transition_sys
Definition: hops_leap_seconds.hh:72
Definition: hops_leap_seconds.hh:71
t
Definition: picking_aedit.py:14
Definition: MHO_AdhocFlagging.hh:18
std::chrono::seconds elapsed
Definition: hops_leap_seconds.hh:51
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:164
bool is_leap_second
Definition: hops_leap_seconds.hh:52
std::chrono::time_point< gps_clock, Duration > gps_time
Definition: hops_leap_seconds.hh:159
std::chrono::time_point< utc_clock, Duration > utc_time
Definition: hops_leap_seconds.hh:157
std::chrono::time_point< tai_clock, Duration > tai_time
Definition: hops_leap_seconds.hh:158
Result of a leap-second lookup, mirroring date::leap_second_info. elapsed = number of leap seconds in...
Definition: hops_leap_seconds.hh:50
GPS clock: SI seconds since 1980-01-06; constant offset from UTC.
Definition: hops_leap_seconds.hh:285
static utc_time< Duration > to_utc(const gps_time< Duration > &t)
Definition: hops_leap_seconds.hh:292
duration::period period
Definition: hops_leap_seconds.hh:288
static CONSTDATA bool is_steady
Definition: hops_leap_seconds.hh:290
std::chrono::time_point< gps_clock > time_point
Definition: hops_leap_seconds.hh:289
duration::rep rep
Definition: hops_leap_seconds.hh:287
std::chrono::system_clock::duration duration
Definition: hops_leap_seconds.hh:286
static gps_time< Duration > from_utc(const utc_time< Duration > &t)
Definition: hops_leap_seconds.hh:297
TAI clock: SI seconds since 1958-01-01; constant offset from UTC.
Definition: hops_leap_seconds.hh:263
static utc_time< Duration > to_utc(const tai_time< Duration > &t)
Definition: hops_leap_seconds.hh:270
duration::period period
Definition: hops_leap_seconds.hh:266
static CONSTDATA bool is_steady
Definition: hops_leap_seconds.hh:268
std::chrono::system_clock::duration duration
Definition: hops_leap_seconds.hh:264
duration::rep rep
Definition: hops_leap_seconds.hh:265
static tai_time< Duration > from_utc(const utc_time< Duration > &t)
Definition: hops_leap_seconds.hh:275
std::chrono::time_point< tai_clock > time_point
Definition: hops_leap_seconds.hh:267
UTC clock: real SI seconds since 1970-01-01 including leap seconds.
Definition: hops_leap_seconds.hh:212
std::chrono::system_clock::duration duration
Definition: hops_leap_seconds.hh:213
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:231
std::chrono::time_point< utc_clock > time_point
Definition: hops_leap_seconds.hh:216
static CONSTDATA bool is_steady
Definition: hops_leap_seconds.hh:217
duration::rep rep
Definition: hops_leap_seconds.hh:214
static utc_time< typename std::common_type< Duration, std::chrono::seconds >::type > from_local(const date::local_time< Duration > <)
Definition: hops_leap_seconds.hh:248
duration::period period
Definition: hops_leap_seconds.hh:215
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:240
static utc_time< std::chrono::nanoseconds > now()
Definition: hops_leap_seconds.hh:253
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:221