Livox SDK API  V2.2.0
chrono.h
Go to the documentation of this file.
1 // Formatting library for C++ - chrono support
2 //
3 // Copyright (c) 2012 - present, Victor Zverovich
4 // All rights reserved.
5 //
6 // For the license information refer to format.h.
7 
8 #ifndef FMT_CHRONO_H_
9 #define FMT_CHRONO_H_
10 
11 #include "format.h"
12 #include "locale.h"
13 
14 #include <chrono>
15 #include <ctime>
16 #include <locale>
17 #include <sstream>
18 
20 
21 namespace internal{
22 
23 enum class numeric_system {
24  standard,
25  // Alternative numeric system, e.g. 十二 instead of 12 in ja_JP locale.
27 };
28 
29 // Parses a put_time-like format string and invokes handler actions.
30 template <typename Char, typename Handler>
32  const Char *begin, const Char *end, Handler &&handler) {
33  auto ptr = begin;
34  while (ptr != end) {
35  auto c = *ptr;
36  if (c == '}') break;
37  if (c != '%') {
38  ++ptr;
39  continue;
40  }
41  if (begin != ptr)
42  handler.on_text(begin, ptr);
43  ++ptr; // consume '%'
44  if (ptr == end)
45  throw format_error("invalid format");
46  c = *ptr++;
47  switch (c) {
48  case '%':
49  handler.on_text(ptr - 1, ptr);
50  break;
51  case 'n': {
52  const char newline[] = "\n";
53  handler.on_text(newline, newline + 1);
54  break;
55  }
56  case 't': {
57  const char tab[] = "\t";
58  handler.on_text(tab, tab + 1);
59  break;
60  }
61  // Day of the week:
62  case 'a':
63  handler.on_abbr_weekday();
64  break;
65  case 'A':
66  handler.on_full_weekday();
67  break;
68  case 'w':
69  handler.on_dec0_weekday(numeric_system::standard);
70  break;
71  case 'u':
72  handler.on_dec1_weekday(numeric_system::standard);
73  break;
74  // Month:
75  case 'b':
76  handler.on_abbr_month();
77  break;
78  case 'B':
79  handler.on_full_month();
80  break;
81  // Hour, minute, second:
82  case 'H':
83  handler.on_24_hour(numeric_system::standard);
84  break;
85  case 'I':
86  handler.on_12_hour(numeric_system::standard);
87  break;
88  case 'M':
89  handler.on_minute(numeric_system::standard);
90  break;
91  case 'S':
92  handler.on_second(numeric_system::standard);
93  break;
94  // Other:
95  case 'c':
96  handler.on_datetime(numeric_system::standard);
97  break;
98  case 'x':
99  handler.on_loc_date(numeric_system::standard);
100  break;
101  case 'X':
102  handler.on_loc_time(numeric_system::standard);
103  break;
104  case 'D':
105  handler.on_us_date();
106  break;
107  case 'F':
108  handler.on_iso_date();
109  break;
110  case 'r':
111  handler.on_12_hour_time();
112  break;
113  case 'R':
114  handler.on_24_hour_time();
115  break;
116  case 'T':
117  handler.on_iso_time();
118  break;
119  case 'p':
120  handler.on_am_pm();
121  break;
122  case 'z':
123  handler.on_utc_offset();
124  break;
125  case 'Z':
126  handler.on_tz_name();
127  break;
128  // Alternative representation:
129  case 'E': {
130  if (ptr == end)
131  throw format_error("invalid format");
132  c = *ptr++;
133  switch (c) {
134  case 'c':
135  handler.on_datetime(numeric_system::alternative);
136  break;
137  case 'x':
138  handler.on_loc_date(numeric_system::alternative);
139  break;
140  case 'X':
141  handler.on_loc_time(numeric_system::alternative);
142  break;
143  default:
144  throw format_error("invalid format");
145  }
146  break;
147  }
148  case 'O':
149  if (ptr == end)
150  throw format_error("invalid format");
151  c = *ptr++;
152  switch (c) {
153  case 'w':
154  handler.on_dec0_weekday(numeric_system::alternative);
155  break;
156  case 'u':
157  handler.on_dec1_weekday(numeric_system::alternative);
158  break;
159  case 'H':
160  handler.on_24_hour(numeric_system::alternative);
161  break;
162  case 'I':
163  handler.on_12_hour(numeric_system::alternative);
164  break;
165  case 'M':
166  handler.on_minute(numeric_system::alternative);
167  break;
168  case 'S':
169  handler.on_second(numeric_system::alternative);
170  break;
171  default:
172  throw format_error("invalid format");
173  }
174  break;
175  default:
176  throw format_error("invalid format");
177  }
178  begin = ptr;
179  }
180  if (begin != ptr)
181  handler.on_text(begin, ptr);
182  return ptr;
183 }
184 
186  void report_no_date() { throw format_error("no date"); }
187 
188  template <typename Char>
189  void on_text(const Char *, const Char *) {}
190  void on_abbr_weekday() { report_no_date(); }
191  void on_full_weekday() { report_no_date(); }
192  void on_dec0_weekday(numeric_system) { report_no_date(); }
193  void on_dec1_weekday(numeric_system) { report_no_date(); }
194  void on_abbr_month() { report_no_date(); }
195  void on_full_month() { report_no_date(); }
200  void on_datetime(numeric_system) { report_no_date(); }
201  void on_loc_date(numeric_system) { report_no_date(); }
202  void on_loc_time(numeric_system) { report_no_date(); }
203  void on_us_date() { report_no_date(); }
204  void on_iso_date() { report_no_date(); }
205  void on_12_hour_time() {}
206  void on_24_hour_time() {}
207  void on_iso_time() {}
208  void on_am_pm() {}
209  void on_utc_offset() { report_no_date(); }
210  void on_tz_name() { report_no_date(); }
211 };
212 
213 template <typename Int>
214 inline int to_int(Int value) {
215  FMT_ASSERT(value >= (std::numeric_limits<int>::min)() &&
216  value <= (std::numeric_limits<int>::max)(), "invalid value");
217  return static_cast<int>(value);
218 }
219 
220 template <typename FormatContext, typename OutputIt>
222  FormatContext &context;
223  OutputIt out;
224  std::chrono::seconds s;
225  std::chrono::milliseconds ms;
226 
228 
229  explicit chrono_formatter(FormatContext &ctx, OutputIt o)
230  : context(ctx), out(o) {}
231 
232  int hour() const { return to_int((s.count() / 3600) % 24); }
233 
234  int hour12() const {
235  auto hour = to_int((s.count() / 3600) % 12);
236  return hour > 0 ? hour : 12;
237  }
238 
239  int minute() const { return to_int((s.count() / 60) % 60); }
240  int second() const { return to_int(s.count() % 60); }
241 
242  std::tm time() const {
243  auto time = std::tm();
244  time.tm_hour = hour();
245  time.tm_min = minute();
246  time.tm_sec = second();
247  return time;
248  }
249 
250  void write(int value, int width) {
251  typedef typename int_traits<int>::main_type main_type;
252  main_type n = to_unsigned(value);
253  int num_digits = internal::count_digits(n);
254  if (width > num_digits)
255  out = std::fill_n(out, width - num_digits, '0');
256  out = format_decimal<char_type>(out, n, num_digits);
257  }
258 
259  void format_localized(const tm &time, const char *format) {
260  auto locale = context.locale().template get<std::locale>();
261  auto &facet = std::use_facet<std::time_put<char_type>>(locale);
262  std::basic_ostringstream<char_type> os;
263  os.imbue(locale);
264  facet.put(os, os, ' ', &time, format, format + std::strlen(format));
265  auto str = os.str();
266  std::copy(str.begin(), str.end(), out);
267  }
268 
269  void on_text(const char_type *begin, const char_type *end) {
270  std::copy(begin, end, out);
271  }
272 
273  // These are not implemented because durations don't have date information.
274  void on_abbr_weekday() {}
275  void on_full_weekday() {}
278  void on_abbr_month() {}
279  void on_full_month() {}
283  void on_us_date() {}
284  void on_iso_date() {}
285  void on_utc_offset() {}
286  void on_tz_name() {}
287 
289  if (ns == numeric_system::standard)
290  return write(hour(), 2);
291  auto time = tm();
292  time.tm_hour = hour();
293  format_localized(time, "%OH");
294  }
295 
297  if (ns == numeric_system::standard)
298  return write(hour12(), 2);
299  auto time = tm();
300  time.tm_hour = hour();
301  format_localized(time, "%OI");
302  }
303 
305  if (ns == numeric_system::standard)
306  return write(minute(), 2);
307  auto time = tm();
308  time.tm_min = minute();
309  format_localized(time, "%OM");
310  }
311 
313  if (ns == numeric_system::standard) {
314  write(second(), 2);
315  if (ms != std::chrono::milliseconds(0)) {
316  *out++ = '.';
317  write(to_int(ms.count()), 3);
318  }
319  return;
320  }
321  auto time = tm();
322  time.tm_sec = second();
323  format_localized(time, "%OS");
324  }
325 
326  void on_12_hour_time() { format_localized(time(), "%r"); }
327 
329  write(hour(), 2);
330  *out++ = ':';
331  write(minute(), 2);
332  }
333 
334  void on_iso_time() {
335  on_24_hour_time();
336  *out++ = ':';
337  write(second(), 2);
338  }
339 
340  void on_am_pm() { format_localized(time(), "%p"); }
341 };
342 } // namespace internal
343 
344 template <typename Period> FMT_CONSTEXPR const char *get_units() {
345  return FMT_NULL;
346 }
347 template <> FMT_CONSTEXPR const char *get_units<std::atto>() { return "as"; }
348 template <> FMT_CONSTEXPR const char *get_units<std::femto>() { return "fs"; }
349 template <> FMT_CONSTEXPR const char *get_units<std::pico>() { return "ps"; }
350 template <> FMT_CONSTEXPR const char *get_units<std::nano>() { return "ns"; }
351 template <> FMT_CONSTEXPR const char *get_units<std::micro>() { return "µs"; }
352 template <> FMT_CONSTEXPR const char *get_units<std::milli>() { return "ms"; }
353 template <> FMT_CONSTEXPR const char *get_units<std::centi>() { return "cs"; }
354 template <> FMT_CONSTEXPR const char *get_units<std::deci>() { return "ds"; }
355 template <> FMT_CONSTEXPR const char *get_units<std::ratio<1>>() { return "s"; }
356 template <> FMT_CONSTEXPR const char *get_units<std::deca>() { return "das"; }
357 template <> FMT_CONSTEXPR const char *get_units<std::hecto>() { return "hs"; }
358 template <> FMT_CONSTEXPR const char *get_units<std::kilo>() { return "ks"; }
359 template <> FMT_CONSTEXPR const char *get_units<std::mega>() { return "Ms"; }
360 template <> FMT_CONSTEXPR const char *get_units<std::giga>() { return "Gs"; }
361 template <> FMT_CONSTEXPR const char *get_units<std::tera>() { return "Ts"; }
362 template <> FMT_CONSTEXPR const char *get_units<std::peta>() { return "Ps"; }
363 template <> FMT_CONSTEXPR const char *get_units<std::exa>() { return "Es"; }
364 template <> FMT_CONSTEXPR const char *get_units<std::ratio<60>>() {
365  return "m";
366 }
367 template <> FMT_CONSTEXPR const char *get_units<std::ratio<3600>>() {
368  return "h";
369 }
370 
371 template <typename Rep, typename Period, typename Char>
372 struct formatter<std::chrono::duration<Rep, Period>, Char> {
373  private:
374  align_spec spec;
375  internal::arg_ref<Char> width_ref;
376  mutable basic_string_view<Char> format_str;
377  typedef std::chrono::duration<Rep, Period> duration;
378 
379  struct spec_handler {
380  formatter &f;
381  basic_parse_context<Char> &context;
382 
383  typedef internal::arg_ref<Char> arg_ref_type;
384 
385  template <typename Id>
386  FMT_CONSTEXPR arg_ref_type make_arg_ref(Id arg_id) {
387  context.check_arg_id(arg_id);
388  return arg_ref_type(arg_id);
389  }
390 
391  FMT_CONSTEXPR arg_ref_type make_arg_ref(internal::auto_id) {
392  return arg_ref_type(context.next_arg_id());
393  }
394 
395  void on_error(const char *msg) { throw format_error(msg); }
396  void on_fill(Char fill) { f.spec.fill_ = fill; }
397  void on_align(alignment align) { f.spec.align_ = align; }
398  void on_width(unsigned width) { f.spec.width_ = width; }
399 
400  template <typename Id>
401  void on_dynamic_width(Id arg_id) {
402  f.width_ref = make_arg_ref(arg_id);
403  }
404  };
405 
406  public:
407  formatter() : spec() {}
408 
410  -> decltype(ctx.begin()) {
411  auto begin = ctx.begin(), end = ctx.end();
412  if (begin == end) return begin;
413  spec_handler handler{*this, ctx};
414  begin = internal::parse_align(begin, end, handler);
415  if (begin == end) return begin;
416  begin = internal::parse_width(begin, end, handler);
418  format_str = basic_string_view<Char>(&*begin, internal::to_unsigned(end - begin));
419  return end;
420  }
421 
422  template <typename FormatContext>
423  auto format(const duration &d, FormatContext &ctx)
424  -> decltype(ctx.out()) {
425  auto begin = format_str.begin(), end = format_str.end();
426  memory_buffer buf;
427  typedef output_range<decltype(ctx.out()), Char> range;
428  basic_writer<range> w(range(ctx.out()));
429  if (begin == end || *begin == '}') {
430  if (const char *unit = get_units<Period>())
431  format_to(buf, "{}{}", d.count(), unit);
432  else if (Period::den == 1)
433  format_to(buf, "{}[{}]s", d.count(), Period::num);
434  else
435  format_to(buf, "{}[{}/{}]s", d.count(), Period::num, Period::den);
436  internal::handle_dynamic_spec<internal::width_checker>(
437  spec.width_, width_ref, ctx);
438  } else {
439  auto out = std::back_inserter(buf);
441  f.s = std::chrono::duration_cast<std::chrono::seconds>(d);
442  f.ms = std::chrono::duration_cast<std::chrono::milliseconds>(d - f.s);
444  }
445  w.write(buf.data(), buf.size(), spec);
446  return w.out();
447  }
448 };
449 
451 
452 #endif // FMT_CHRONO_H_
void on_dec1_weekday(numeric_system)
Definition: chrono.h:193
auto begin(const C &c) -> decltype(c.begin())
Definition: format.h:251
void on_24_hour(numeric_system)
Definition: chrono.h:196
void on_second(numeric_system)
Definition: chrono.h:199
void write(std::basic_ostream< Char > &os, basic_buffer< Char > &buf)
Definition: ostream.h:76
FormatContext::char_type char_type
Definition: chrono.h:227
auto format(const duration &d, FormatContext &ctx) -> decltype(ctx.out())
Definition: chrono.h:423
void on_datetime(numeric_system)
Definition: chrono.h:280
void on_loc_time(numeric_system)
Definition: chrono.h:202
void on_loc_date(numeric_system)
Definition: chrono.h:281
const char * get_units()
Definition: chrono.h:344
Definition: format.h:297
void on_loc_time(numeric_system)
Definition: chrono.h:282
std::chrono::seconds s
Definition: chrono.h:224
#define FMT_END_NAMESPACE
Definition: core.h:153
auto parse(basic_parse_context< Char > &ctx) -> decltype(ctx.begin())
Definition: chrono.h:409
const Char * parse_width(const Char *begin, const Char *end, Handler &&handler)
Definition: format.h:1884
void on_loc_date(numeric_system)
Definition: chrono.h:201
std::size_t size() const
Definition: core.h:250
chrono_formatter(FormatContext &ctx, OutputIt o)
Definition: chrono.h:229
void on_12_hour(numeric_system)
Definition: chrono.h:197
void on_dec0_weekday(numeric_system)
Definition: chrono.h:192
bool check_arg_id(unsigned)
Definition: core.h:908
void on_second(numeric_system ns)
Definition: chrono.h:312
void on_24_hour(numeric_system ns)
Definition: chrono.h:288
std::make_unsigned< Int >::type to_unsigned(Int value)
Definition: core.h:208
std::basic_string< typename char_t< S >::type > format(const S &format_str, const Args &...args)
Definition: core.h:1454
void on_datetime(numeric_system)
Definition: chrono.h:200
void on_text(const char_type *begin, const char_type *end)
Definition: chrono.h:269
#define FMT_NULL
Definition: core.h:107
void on_dec1_weekday(numeric_system)
Definition: chrono.h:277
void on_text(const Char *, const Char *)
Definition: chrono.h:189
void on_12_hour(numeric_system ns)
Definition: chrono.h:296
#define FMT_CONSTEXPR
Definition: core.h:69
unsigned width_
Definition: format.h:1088
const void * ptr(const T *p)
Definition: format.h:3138
const Char * parse_chrono_format(const Char *begin, const Char *end, Handler &&handler)
Definition: chrono.h:31
void on_minute(numeric_system)
Definition: chrono.h:198
std::chrono::milliseconds ms
Definition: chrono.h:225
void copy(const RangeT &range, OutputIterator out)
Definition: ranges.h:58
std::enable_if< is_contiguous< Container >::value &&internal::is_string< S >::value, std::back_insert_iterator< Container > >::type format_to(std::back_insert_iterator< Container > out, const S &format_str, const Args &...args)
Definition: core.h:1430
void on_minute(numeric_system ns)
Definition: chrono.h:304
#define FMT_ASSERT(condition, message)
Definition: core.h:170
const Char * parse_align(const Char *begin, const Char *end, Handler &&handler)
Definition: format.h:1847
alignment
Definition: format.h:1079
#define FMT_BEGIN_NAMESPACE
Definition: core.h:155
unsigned count_digits(T n)
Definition: fmt_helper.h:47
iterator begin() const
Definition: core.h:391
void write(int value, int width)
Definition: chrono.h:250
void on_dec0_weekday(numeric_system)
Definition: chrono.h:276
int to_int(Int value)
Definition: chrono.h:214
auto end(const C &c) -> decltype(c.end())
Definition: format.h:257
void format_localized(const tm &time, const char *format)
Definition: chrono.h:259
iterator end() const
Definition: core.h:392