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':
84  break;
85  case 'I':
87  break;
88  case 'M':
90  break;
91  case 'S':
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':
136  break;
137  case 'x':
139  break;
140  case 'X':
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':
161  break;
162  case 'I':
164  break;
165  case 'M':
167  break;
168  case 'S':
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) {
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;
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:
377  typedef std::chrono::duration<Rep, Period> duration;
378 
379  struct spec_handler {
382 
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 
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};
415  if (begin == end) return begin;
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_
FMT_CONSTEXPR bool check_arg_id(unsigned)
Definition: core.h:908
void on_datetime(numeric_system)
Definition: chrono.h:280
std::tm time() const
Definition: chrono.h:242
FMT_CONSTEXPR const Char * parse_width(const Char *begin, const Char *end, Handler &&handler)
Definition: format.h:1884
void msg(const char *fmt,...)
Definition: message.cpp:107
FMT_CONSTEXPR auto begin(const C &c) -> decltype(c.begin())
Definition: format.h:251
FMT_CONSTEXPR const char * get_units()
Definition: chrono.h:344
void on_minute(numeric_system ns)
Definition: chrono.h:304
numeric_system
Definition: chrono.h:23
STL namespace.
void write(std::basic_ostream< Char > &os, basic_buffer< Char > &buf)
Definition: ostream.h:76
FormatContext::char_type char_type
Definition: chrono.h:227
void on_dec0_weekday(numeric_system)
Definition: chrono.h:192
void on_dec1_weekday(numeric_system)
Definition: chrono.h:193
void on_second(numeric_system)
Definition: chrono.h:199
#define FMT_END_NAMESPACE
Definition: core.h:153
int hour12() const
Definition: chrono.h:234
const double width
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
int second() const
Definition: chrono.h:240
T * data() FMT_NOEXCEPT
Definition: core.h:256
chrono_formatter(FormatContext &ctx, OutputIt o)
Definition: chrono.h:229
FMT_CONSTEXPR auto end(const C &c) -> decltype(c.end())
Definition: format.h:257
void on_second(numeric_system ns)
Definition: chrono.h:312
void on_text(const char_type *begin, const char_type *end)
Definition: chrono.h:269
void on_minute(numeric_system)
Definition: chrono.h:198
second seconds
Alias for common language habits.
Definition: spacetime.h:83
std::size_t size() const FMT_NOEXCEPT
Definition: core.h:250
void on_dec0_weekday(numeric_system)
Definition: chrono.h:276
int minute() const
Definition: chrono.h:239
std::string str(const std::pair< Type, Type > &tt)
Definition: test_pimpos.cxx:12
void on_datetime(numeric_system)
Definition: chrono.h:200
void on_12_hour(numeric_system)
Definition: chrono.h:197
static int max(int a, int b)
void on_dec1_weekday(numeric_system)
Definition: chrono.h:277
void on_24_hour(numeric_system)
Definition: chrono.h:196
std::chrono::seconds s
Definition: chrono.h:224
void format_localized(const tm &time, const char *format)
Definition: chrono.h:259
alignment
Definition: format.h:1079
millisecond milliseconds
Alias for common language habits.
Definition: spacetime.h:100
#define FMT_NULL
Definition: core.h:107
void on_24_hour(numeric_system ns)
Definition: chrono.h:288
FMT_CONSTEXPR std::make_unsigned< Int >::type to_unsigned(Int value)
Definition: core.h:208
#define FMT_CONSTEXPR
Definition: core.h:69
T min(sqlite3 *const db, std::string const &table_name, std::string const &column_name)
Definition: statistics.h:55
const void * ptr(const T *p)
Definition: format.h:3138
def fill(s)
Definition: translator.py:93
const GenericPointer< typename T::ValueType > T2 value
Definition: pointer.h:1225
FMT_CONSTEXPR const Char * parse_chrono_format(const Char *begin, const Char *end, Handler &&handler)
Definition: chrono.h:31
void on_loc_time(numeric_system)
Definition: chrono.h:282
int to_int(Int value)
Definition: chrono.h:214
FormatContext & context
Definition: chrono.h:222
T copy(T const &v)
std::chrono::milliseconds ms
Definition: chrono.h:225
void on_loc_date(numeric_system)
Definition: chrono.h:201
FMT_CONSTEXPR iterator end() const
Definition: core.h:392
static msg_handler handler
Definition: qglobal.cpp:234
FMT_CONSTEXPR const Char * parse_align(const Char *begin, const Char *end, Handler &&handler)
Definition: format.h:1847
#define FMT_ASSERT(condition, message)
Definition: core.h:170
static QCString align(DocHtmlCell *cell)
FMT_CONSTEXPR iterator begin() const
Definition: core.h:391
unsigned width_
Definition: format.h:1088
void on_12_hour(numeric_system ns)
Definition: chrono.h:296
void on_loc_date(numeric_system)
Definition: chrono.h:281
FMT_CONSTEXPR arg_ref_type make_arg_ref(internal::auto_id)
Definition: chrono.h:391
std::chrono::duration< Rep, Period > duration
Definition: chrono.h:377
second_as<> second
Type of time stored in seconds, in double precision.
Definition: spacetime.h:80
void write(int value, int width)
Definition: chrono.h:250
auto format(const duration &d, FormatContext &ctx) -> decltype(ctx.out())
Definition: chrono.h:423
void on_loc_time(numeric_system)
Definition: chrono.h:202
std::size_t n
Definition: format.h:3399
QAsciiDict< Entry > ns
FMT_CONSTEXPR unsigned next_arg_id()
Definition: format.h:1118
FMT_CONSTEXPR auto parse(basic_parse_context< Char > &ctx) -> decltype(ctx.begin())
Definition: chrono.h:409
int count_digits(uint64_t n)
Definition: format.h:777
void on_text(const Char *, const Char *)
Definition: chrono.h:189