| 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 | |
|---|
| 19 | FMT_BEGIN_NAMESPACE |
|---|
| 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. |
|---|
| 26 | alternative |
|---|
| 27 | }; |
|---|
| 28 | |
|---|
| 29 | // Parses a put_time-like format string and invokes handler actions. |
|---|
| 30 | template <typename Char, typename Handler> |
|---|
| 31 | FMT_CONSTEXPR const Char *parse_chrono_format( |
|---|
| 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 | |
|---|
| 185 | struct chrono_format_checker { |
|---|
| 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(); } |
|---|
| 196 | void on_24_hour(numeric_system) {} |
|---|
| 197 | void on_12_hour(numeric_system) {} |
|---|
| 198 | void on_minute(numeric_system) {} |
|---|
| 199 | void on_second(numeric_system) {} |
|---|
| 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> |
|---|
| 221 | struct chrono_formatter { |
|---|
| 222 | FormatContext &context; |
|---|
| 223 | OutputIt out; |
|---|
| 224 | std::chrono::seconds s; |
|---|
| 225 | std::chrono::milliseconds ms; |
|---|
| 226 | |
|---|
| 227 | typedef typename FormatContext::char_type char_type; |
|---|
| 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() {} |
|---|
| 276 | void on_dec0_weekday(numeric_system) {} |
|---|
| 277 | void on_dec1_weekday(numeric_system) {} |
|---|
| 278 | void on_abbr_month() {} |
|---|
| 279 | void on_full_month() {} |
|---|
| 280 | void on_datetime(numeric_system) {} |
|---|
| 281 | void on_loc_date(numeric_system) {} |
|---|
| 282 | void on_loc_time(numeric_system) {} |
|---|
| 283 | void on_us_date() {} |
|---|
| 284 | void on_iso_date() {} |
|---|
| 285 | void on_utc_offset() {} |
|---|
| 286 | void on_tz_name() {} |
|---|
| 287 | |
|---|
| 288 | void on_24_hour(numeric_system ns) { |
|---|
| 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 | |
|---|
| 296 | void on_12_hour(numeric_system ns) { |
|---|
| 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 | |
|---|
| 304 | void on_minute(numeric_system ns) { |
|---|
| 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 | |
|---|
| 312 | void on_second(numeric_system ns) { |
|---|
| 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 | |
|---|
| 328 | void on_24_hour_time() { |
|---|
| 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 | |
|---|
| 409 | FMT_CONSTEXPR auto parse(basic_parse_context<Char> &ctx) |
|---|
| 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); |
|---|
| 417 | end = parse_chrono_format(begin, end, internal::chrono_format_checker()); |
|---|
| 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); |
|---|
| 440 | internal::chrono_formatter<FormatContext, decltype(out)> f(ctx, out); |
|---|
| 441 | f.s = std::chrono::duration_cast<std::chrono::seconds>(d); |
|---|
| 442 | f.ms = std::chrono::duration_cast<std::chrono::milliseconds>(d - f.s); |
|---|
| 443 | parse_chrono_format(begin, end, f); |
|---|
| 444 | } |
|---|
| 445 | w.write(buf.data(), buf.size(), spec); |
|---|
| 446 | return w.out(); |
|---|
| 447 | } |
|---|
| 448 | }; |
|---|
| 449 | |
|---|
| 450 | FMT_END_NAMESPACE |
|---|
| 451 | |
|---|
| 452 | #endif // FMT_CHRONO_H_ |
|---|