HOPS
HOPS class reference
hops_leap_seconds.hh
Go to the documentation of this file.
1 #ifndef HOPS_LEAP_SECONDS_HH__
2 #define HOPS_LEAP_SECONDS_HH__
3 
4 #include <algorithm>
5 #include <chrono>
6 #include <cstdint>
7 #include <vector>
8 
9 //header-only portion of the H. Hinnant date library (civil calendar only, no
10 //linking required). We deliberately do NOT include "date/tz.h" here: that is the
11 //compiled part that loads the IANA timezone/leap-second database from the host
12 //at runtime and forces a link against libdate-tz.
13 #include "date/date.h"
14 
41 namespace hops
42 {
43 
50 {
51  std::chrono::seconds elapsed;
53 };
54 
55 namespace leap_detail
56 {
57 
58 //fixed continuous-scale offsets (see file header for the derivation/verification):
59 // to_utc(tai) = utc_time(tai_count - kTaiEpochOffset) [1958-01-01 gap + 10 s base]
60 // to_utc(gps) = utc_time(gps_count + kGpsEpochOffset) [1970->1980-01-06 + 9 leaps]
61 static const std::int64_t kTaiEpochOffset = 378691210;
62 static const std::int64_t kGpsEpochOffset = 315964809;
63 
64 //One leap-second event: the sys-time (unix seconds) of 00:00:00 UTC on the day
65 //AFTER the event, and the SIGNED cumulative offset (utc_count - sys_count) that
66 //applies at/after it. Storing the cumulative value rather than deriving it from
67 //the row index lets the table represent negative (removed) leap seconds too: a
68 //+1 event steps the offset up (inserted 23:59:60), a -1 event steps it down
69 //(23:59:59 skipped). Entries are sorted ascending by transition_sys.
70 struct leap_entry
71 {
72  std::int64_t transition_sys;
73  std::int64_t cumulative;
74 };
75 
76 inline const std::vector< leap_entry >& leap_table()
77 {
78  static const std::vector< leap_entry > table = [] {
79  //(year, month, day of the day-after, signed delta) for each leap event.
80  //All leaps to date are +1; any future negative leap would use -1.
81  static const struct
82  {
83  int y, m, d, delta;
84  } events[] = {
85  {1972, 7, 1, +1},
86  {1973, 1, 1, +1},
87  {1974, 1, 1, +1},
88  {1975, 1, 1, +1},
89  {1976, 1, 1, +1},
90  {1977, 1, 1, +1},
91  {1978, 1, 1, +1},
92  {1979, 1, 1, +1},
93  {1980, 1, 1, +1},
94  {1981, 7, 1, +1},
95  {1982, 7, 1, +1},
96  {1983, 7, 1, +1},
97  {1985, 7, 1, +1},
98  {1988, 1, 1, +1},
99  {1990, 1, 1, +1},
100  {1991, 1, 1, +1},
101  {1992, 7, 1, +1},
102  {1993, 7, 1, +1},
103  {1994, 7, 1, +1},
104  {1996, 1, 1, +1},
105  {1997, 7, 1, +1},
106  {1999, 1, 1, +1},
107  {2006, 1, 1, +1},
108  {2009, 1, 1, +1},
109  {2012, 7, 1, +1},
110  {2015, 7, 1, +1},
111  {2017, 1, 1, +1}
112  };
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)
117  {
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;
123  t.push_back(leap_entry{transition, cumulative});
124  }
125  return t;
126  }();
127  return table;
128 }
129 
130 //signed cumulative offset (utc_count - sys_count) in effect at a given unix-second
131 //value on the SYS timescale.
132 inline std::int64_t offset_at_sys(std::int64_t sys_seconds)
133 {
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)
137  {
138  if(sys_seconds >= T[k].transition_sys)
139  {
140  offset = T[k].cumulative;
141  }
142  else
143  {
144  break;
145  }
146  }
147  return offset;
148 }
149 
150 } // namespace leap_detail
151 
152 //clock tag types. We only ever instantiate time_points with an explicit Duration,
153 //so the nested typedefs exist purely to model a Clock (and to give utc_clock a now()).
154 struct utc_clock;
155 struct tai_clock;
156 struct gps_clock;
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 >;
160 
164 template< class Duration > inline leap_second_info get_leap_second_info(const utc_time< Duration >& ut)
165 {
166  using namespace leap_detail;
167  const std::vector< leap_entry >& T = leap_table();
168  //floor to whole seconds (all HOPS instants are positive, so truncation == floor)
169  std::int64_t u = std::chrono::duration_cast< std::chrono::seconds >(ut.time_since_epoch()).count();
170 
171  //In the utc timescale the offset steps from C[k-1] to C[k] at the transition,
172  //and the step lands at utc threshold U_k = transition_sys + min(C[k-1], C[k]):
173  // +1 (inserted) leap: U_k = transition + C[k-1], and utc == U_k is the
174  // inserted 23:59:60 second;
175  // -1 (removed) leap: U_k = transition + C[k], with no inserted second (the
176  // skip is on the sys side, and surfaces via to_sys).
177  //U_k is strictly increasing, so scan for the last transition at/below u.
178  std::int64_t elapsed = 0;
179  long selected = -1;
180  for(std::size_t k = 0; k < T.size(); ++k)
181  {
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);
184  if(u >= threshold)
185  {
186  elapsed = T[k].cumulative;
187  selected = static_cast< long >(k);
188  }
189  else
190  {
191  break;
192  }
193  }
194 
195  bool is_ls = false;
196  if(selected >= 0)
197  {
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);
202  //only a positive (inserted) leap has a real 23:59:60 second, one wide
203  is_ls = (delta > 0) && (u == threshold);
204  }
205  return leap_second_info{std::chrono::seconds{elapsed}, is_ls};
206 }
207 
211 struct utc_clock
212 {
213  using duration = std::chrono::system_clock::duration;
214  using rep = duration::rep;
215  using period = duration::period;
216  using time_point = std::chrono::time_point< utc_clock >;
217  static CONSTDATA bool is_steady = false;
218 
219  template< class Duration >
221  from_sys(const date::sys_time< Duration >& st)
222  {
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();
225  std::chrono::seconds leaps{leap_detail::offset_at_sys(s)};
226  return utc_time< CD >{st.time_since_epoch() + leaps};
227  }
228 
229  template< class Duration >
230  static date::sys_time< typename std::common_type< Duration, std::chrono::seconds >::type >
232  {
233  using CD = typename std::common_type< Duration, std::chrono::seconds >::type;
235  return date::sys_time< CD >{ut.time_since_epoch() - info.elapsed};
236  }
237 
238  template< class Duration >
239  static date::local_time< typename std::common_type< Duration, std::chrono::seconds >::type >
241  {
242  using CD = typename std::common_type< Duration, std::chrono::seconds >::type;
243  return date::local_time< CD >{to_sys(ut).time_since_epoch()};
244  }
245 
246  template< class Duration >
248  from_local(const date::local_time< Duration >& lt)
249  {
250  return from_sys(date::sys_time< Duration >{lt.time_since_epoch()});
251  }
252 
254  {
255  return from_sys(std::chrono::time_point_cast< std::chrono::nanoseconds >(std::chrono::system_clock::now()));
256  }
257 };
258 
262 struct tai_clock
263 {
264  using duration = std::chrono::system_clock::duration;
265  using rep = duration::rep;
266  using period = duration::period;
267  using time_point = std::chrono::time_point< tai_clock >;
268  static CONSTDATA bool is_steady = false;
269 
270  template< class Duration > static utc_time< Duration > to_utc(const tai_time< Duration >& t)
271  {
272  return utc_time< Duration >{t.time_since_epoch() - std::chrono::seconds{leap_detail::kTaiEpochOffset}};
273  }
274 
275  template< class Duration > static tai_time< Duration > from_utc(const utc_time< Duration >& t)
276  {
277  return tai_time< Duration >{t.time_since_epoch() + std::chrono::seconds{leap_detail::kTaiEpochOffset}};
278  }
279 };
280 
284 struct gps_clock
285 {
286  using duration = std::chrono::system_clock::duration;
287  using rep = duration::rep;
288  using period = duration::period;
289  using time_point = std::chrono::time_point< gps_clock >;
290  static CONSTDATA bool is_steady = false;
291 
292  template< class Duration > static utc_time< Duration > to_utc(const gps_time< Duration >& t)
293  {
294  return utc_time< Duration >{t.time_since_epoch() + std::chrono::seconds{leap_detail::kGpsEpochOffset}};
295  }
296 
297  template< class Duration > static gps_time< Duration > from_utc(const utc_time< Duration >& t)
298  {
299  return gps_time< Duration >{t.time_since_epoch() - std::chrono::seconds{leap_detail::kGpsEpochOffset}};
300  }
301 };
302 
303 } // namespace hops
304 
305 #endif
#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 > &lt)
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