STIR  6.3.0
chrono.h
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 #ifndef FMT_MODULE
12 # include <algorithm>
13 # include <chrono>
14 # include <cmath> // std::isfinite
15 # include <cstring> // std::memcpy
16 # include <ctime>
17 # include <iterator>
18 # include <locale>
19 # include <ostream>
20 # include <type_traits>
21 #endif
22 
23 #include "format.h"
24 
25 FMT_BEGIN_NAMESPACE
26 
27 // Enable safe chrono durations, unless explicitly disabled.
28 #ifndef FMT_SAFE_DURATION_CAST
29 # define FMT_SAFE_DURATION_CAST 1
30 #endif
31 #if FMT_SAFE_DURATION_CAST
32 
33 // For conversion between std::chrono::durations without undefined
34 // behaviour or erroneous results.
35 // This is a stripped down version of duration_cast, for inclusion in fmt.
36 // See https://github.com/pauldreik/safe_duration_cast
37 //
38 // Copyright Paul Dreik 2019
39 namespace safe_duration_cast {
40 
41 // DEPRECATED!
42 template <typename To, typename From,
43  FMT_ENABLE_IF(!std::is_same<From, To>::value &&
44  std::numeric_limits<From>::is_signed ==
45  std::numeric_limits<To>::is_signed)>
46 FMT_CONSTEXPR auto lossless_integral_conversion(const From from, int& ec)
47  -> To {
48  ec = 0;
49  using F = std::numeric_limits<From>;
50  using T = std::numeric_limits<To>;
51  static_assert(F::is_integer, "From must be integral");
52  static_assert(T::is_integer, "To must be integral");
53 
54  // A and B are both signed, or both unsigned.
55  if (detail::const_check(F::digits <= T::digits)) {
56  // From fits in To without any problem.
57  } else {
58  // From does not always fit in To, resort to a dynamic check.
59  if (from < (T::min)() || from > (T::max)()) {
60  // outside range.
61  ec = 1;
62  return {};
63  }
64  }
65  return static_cast<To>(from);
66 }
67 
70 template <typename To, typename From,
71  FMT_ENABLE_IF(!std::is_same<From, To>::value &&
72  std::numeric_limits<From>::is_signed !=
73  std::numeric_limits<To>::is_signed)>
74 FMT_CONSTEXPR auto lossless_integral_conversion(const From from, int& ec)
75  -> To {
76  ec = 0;
77  using F = std::numeric_limits<From>;
78  using T = std::numeric_limits<To>;
79  static_assert(F::is_integer, "From must be integral");
80  static_assert(T::is_integer, "To must be integral");
81 
82  if (detail::const_check(F::is_signed && !T::is_signed)) {
83  // From may be negative, not allowed!
84  if (fmt::detail::is_negative(from)) {
85  ec = 1;
86  return {};
87  }
88  // From is positive. Can it always fit in To?
89  if (detail::const_check(F::digits > T::digits) &&
90  from > static_cast<From>(detail::max_value<To>())) {
91  ec = 1;
92  return {};
93  }
94  }
95 
96  if (detail::const_check(!F::is_signed && T::is_signed &&
97  F::digits >= T::digits) &&
98  from > static_cast<From>(detail::max_value<To>())) {
99  ec = 1;
100  return {};
101  }
102  return static_cast<To>(from); // Lossless conversion.
103 }
104 
105 template <typename To, typename From,
106  FMT_ENABLE_IF(std::is_same<From, To>::value)>
107 FMT_CONSTEXPR auto lossless_integral_conversion(const From from, int& ec)
108  -> To {
109  ec = 0;
110  return from;
111 } // function
112 
113 // clang-format off
126 // clang-format on
127 template <typename To, typename From,
128  FMT_ENABLE_IF(!std::is_same<From, To>::value)>
129 FMT_CONSTEXPR auto safe_float_conversion(const From from, int& ec) -> To {
130  ec = 0;
131  using T = std::numeric_limits<To>;
132  static_assert(std::is_floating_point<From>::value, "From must be floating");
133  static_assert(std::is_floating_point<To>::value, "To must be floating");
134 
135  // catch the only happy case
136  if (std::isfinite(from)) {
137  if (from >= T::lowest() && from <= (T::max)()) {
138  return static_cast<To>(from);
139  }
140  // not within range.
141  ec = 1;
142  return {};
143  }
144 
145  // nan and inf will be preserved
146  return static_cast<To>(from);
147 } // function
148 
149 template <typename To, typename From,
150  FMT_ENABLE_IF(std::is_same<From, To>::value)>
151 FMT_CONSTEXPR auto safe_float_conversion(const From from, int& ec) -> To {
152  ec = 0;
153  static_assert(std::is_floating_point<From>::value, "From must be floating");
154  return from;
155 }
156 
158 template <typename To, typename FromRep, typename FromPeriod,
159  FMT_ENABLE_IF(std::is_floating_point<FromRep>::value),
160  FMT_ENABLE_IF(std::is_floating_point<typename To::rep>::value)>
161 auto safe_duration_cast(std::chrono::duration<FromRep, FromPeriod> from,
162  int& ec) -> To {
163  using From = std::chrono::duration<FromRep, FromPeriod>;
164  ec = 0;
165 
166  // the basic idea is that we need to convert from count() in the from type
167  // to count() in the To type, by multiplying it with this:
168  struct Factor
169  : std::ratio_divide<typename From::period, typename To::period> {};
170 
171  static_assert(Factor::num > 0, "num must be positive");
172  static_assert(Factor::den > 0, "den must be positive");
173 
174  // the conversion is like this: multiply from.count() with Factor::num
175  // /Factor::den and convert it to To::rep, all this without
176  // overflow/underflow. let's start by finding a suitable type that can hold
177  // both To, From and Factor::num
178  using IntermediateRep =
179  typename std::common_type<typename From::rep, typename To::rep,
180  decltype(Factor::num)>::type;
181 
182  // force conversion of From::rep -> IntermediateRep to be safe,
183  // even if it will never happen be narrowing in this context.
184  IntermediateRep count =
185  safe_float_conversion<IntermediateRep>(from.count(), ec);
186  if (ec) {
187  return {};
188  }
189 
190  // multiply with Factor::num without overflow or underflow
191  if (detail::const_check(Factor::num != 1)) {
192  constexpr auto max1 = detail::max_value<IntermediateRep>() /
193  static_cast<IntermediateRep>(Factor::num);
194  if (count > max1) {
195  ec = 1;
196  return {};
197  }
198  constexpr auto min1 = std::numeric_limits<IntermediateRep>::lowest() /
199  static_cast<IntermediateRep>(Factor::num);
200  if (count < min1) {
201  ec = 1;
202  return {};
203  }
204  count *= static_cast<IntermediateRep>(Factor::num);
205  }
206 
207  // this can't go wrong, right? den>0 is checked earlier.
208  if (detail::const_check(Factor::den != 1)) {
209  using common_t = typename std::common_type<IntermediateRep, intmax_t>::type;
210  count /= static_cast<common_t>(Factor::den);
211  }
212 
213  // convert to the to type, safely
214  using ToRep = typename To::rep;
215 
216  const ToRep tocount = safe_float_conversion<ToRep>(count, ec);
217  if (ec) {
218  return {};
219  }
220  return To{tocount};
221 }
222 } // namespace safe_duration_cast
223 #endif
224 
225 namespace detail {
226 
227 // Check if std::chrono::utc_time is available.
228 #ifdef FMT_USE_UTC_TIME
229 // Use the provided definition.
230 #elif defined(__cpp_lib_chrono)
231 # define FMT_USE_UTC_TIME (__cpp_lib_chrono >= 201907L)
232 #else
233 # define FMT_USE_UTC_TIME 0
234 #endif
235 #if FMT_USE_UTC_TIME
236 using utc_clock = std::chrono::utc_clock;
237 #else
238 struct utc_clock {
239  template <typename T> void to_sys(T);
240 };
241 #endif
242 
243 // Check if std::chrono::local_time is available.
244 #ifdef FMT_USE_LOCAL_TIME
245 // Use the provided definition.
246 #elif defined(__cpp_lib_chrono)
247 # define FMT_USE_LOCAL_TIME (__cpp_lib_chrono >= 201907L)
248 #else
249 # define FMT_USE_LOCAL_TIME 0
250 #endif
251 #if FMT_USE_LOCAL_TIME
252 using local_t = std::chrono::local_t;
253 #else
254 struct local_t {};
255 #endif
256 
257 } // namespace detail
258 
259 template <typename Duration>
260 using sys_time = std::chrono::time_point<std::chrono::system_clock, Duration>;
261 
262 template <typename Duration>
263 using utc_time = std::chrono::time_point<detail::utc_clock, Duration>;
264 
265 template <class Duration>
266 using local_time = std::chrono::time_point<detail::local_t, Duration>;
267 
268 namespace detail {
269 
270 // Prevents expansion of a preceding token as a function-style macro.
271 // Usage: f FMT_NOMACRO()
272 #define FMT_NOMACRO
273 
274 template <typename T = void> struct null {};
275 inline auto gmtime_r(...) -> null<> { return null<>(); }
276 inline auto gmtime_s(...) -> null<> { return null<>(); }
277 
278 // It is defined here and not in ostream.h because the latter has expensive
279 // includes.
280 template <typename StreamBuf> class formatbuf : public StreamBuf {
281  private:
282  using char_type = typename StreamBuf::char_type;
283  using streamsize = decltype(std::declval<StreamBuf>().sputn(nullptr, 0));
284  using int_type = typename StreamBuf::int_type;
285  using traits_type = typename StreamBuf::traits_type;
286 
287  buffer<char_type>& buffer_;
288 
289  public:
290  explicit formatbuf(buffer<char_type>& buf) : buffer_(buf) {}
291 
292  protected:
293  // The put area is always empty. This makes the implementation simpler and has
294  // the advantage that the streambuf and the buffer are always in sync and
295  // sputc never writes into uninitialized memory. A disadvantage is that each
296  // call to sputc always results in a (virtual) call to overflow. There is no
297  // disadvantage here for sputn since this always results in a call to xsputn.
298 
299  auto overflow(int_type ch) -> int_type override {
300  if (!traits_type::eq_int_type(ch, traits_type::eof()))
301  buffer_.push_back(static_cast<char_type>(ch));
302  return ch;
303  }
304 
305  auto xsputn(const char_type* s, streamsize count) -> streamsize override {
306  buffer_.append(s, s + count);
307  return count;
308  }
309 };
310 
311 inline auto get_classic_locale() -> const std::locale& {
312  static const auto& locale = std::locale::classic();
313  return locale;
314 }
315 
316 template <typename CodeUnit> struct codecvt_result {
317  static constexpr size_t max_size = 32;
318  CodeUnit buf[max_size];
319  CodeUnit* end;
320 };
321 
322 template <typename CodeUnit>
323 void write_codecvt(codecvt_result<CodeUnit>& out, string_view in,
324  const std::locale& loc) {
325  FMT_PRAGMA_CLANG(diagnostic push)
326  FMT_PRAGMA_CLANG(diagnostic ignored "-Wdeprecated")
327  auto& f = std::use_facet<std::codecvt<CodeUnit, char, std::mbstate_t>>(loc);
328  FMT_PRAGMA_CLANG(diagnostic pop)
329  auto mb = std::mbstate_t();
330  const char* from_next = nullptr;
331  auto result = f.in(mb, in.begin(), in.end(), from_next, std::begin(out.buf),
332  std::end(out.buf), out.end);
333  if (result != std::codecvt_base::ok)
334  FMT_THROW(format_error("failed to format time"));
335 }
336 
337 template <typename OutputIt>
338 auto write_encoded_tm_str(OutputIt out, string_view in, const std::locale& loc)
339  -> OutputIt {
340  if (const_check(detail::use_utf8) && loc != get_classic_locale()) {
341  // char16_t and char32_t codecvts are broken in MSVC (linkage errors) and
342  // gcc-4.
343 #if FMT_MSC_VERSION != 0 || \
344  (defined(__GLIBCXX__) && \
345  (!defined(_GLIBCXX_USE_DUAL_ABI) || _GLIBCXX_USE_DUAL_ABI == 0))
346  // The _GLIBCXX_USE_DUAL_ABI macro is always defined in libstdc++ from gcc-5
347  // and newer.
348  using code_unit = wchar_t;
349 #else
350  using code_unit = char32_t;
351 #endif
352 
353  using unit_t = codecvt_result<code_unit>;
354  unit_t unit;
355  write_codecvt(unit, in, loc);
356  // In UTF-8 is used one to four one-byte code units.
357  auto u =
358  to_utf8<code_unit, basic_memory_buffer<char, unit_t::max_size * 4>>();
359  if (!u.convert({unit.buf, to_unsigned(unit.end - unit.buf)}))
360  FMT_THROW(format_error("failed to format time"));
361  return copy<char>(u.c_str(), u.c_str() + u.size(), out);
362  }
363  return copy<char>(in.data(), in.data() + in.size(), out);
364 }
365 
366 template <typename Char, typename OutputIt,
367  FMT_ENABLE_IF(!std::is_same<Char, char>::value)>
368 auto write_tm_str(OutputIt out, string_view sv, const std::locale& loc)
369  -> OutputIt {
370  codecvt_result<Char> unit;
371  write_codecvt(unit, sv, loc);
372  return copy<Char>(unit.buf, unit.end, out);
373 }
374 
375 template <typename Char, typename OutputIt,
376  FMT_ENABLE_IF(std::is_same<Char, char>::value)>
377 auto write_tm_str(OutputIt out, string_view sv, const std::locale& loc)
378  -> OutputIt {
379  return write_encoded_tm_str(out, sv, loc);
380 }
381 
382 template <typename Char>
383 inline void do_write(buffer<Char>& buf, const std::tm& time,
384  const std::locale& loc, char format, char modifier) {
385  auto&& format_buf = formatbuf<std::basic_streambuf<Char>>(buf);
386  auto&& os = std::basic_ostream<Char>(&format_buf);
387  os.imbue(loc);
388  const auto& facet = std::use_facet<std::time_put<Char>>(loc);
389  auto end = facet.put(os, os, Char(' '), &time, format, modifier);
390  if (end.failed()) FMT_THROW(format_error("failed to format time"));
391 }
392 
393 template <typename Char, typename OutputIt,
394  FMT_ENABLE_IF(!std::is_same<Char, char>::value)>
395 auto write(OutputIt out, const std::tm& time, const std::locale& loc,
396  char format, char modifier = 0) -> OutputIt {
397  auto&& buf = get_buffer<Char>(out);
398  do_write<Char>(buf, time, loc, format, modifier);
399  return get_iterator(buf, out);
400 }
401 
402 template <typename Char, typename OutputIt,
403  FMT_ENABLE_IF(std::is_same<Char, char>::value)>
404 auto write(OutputIt out, const std::tm& time, const std::locale& loc,
405  char format, char modifier = 0) -> OutputIt {
406  auto&& buf = basic_memory_buffer<Char>();
407  do_write<char>(buf, time, loc, format, modifier);
408  return write_encoded_tm_str(out, string_view(buf.data(), buf.size()), loc);
409 }
410 
411 template <typename T, typename U>
412 using is_similar_arithmetic_type =
413  bool_constant<(std::is_integral<T>::value && std::is_integral<U>::value) ||
414  (std::is_floating_point<T>::value &&
415  std::is_floating_point<U>::value)>;
416 
417 FMT_NORETURN inline void throw_duration_error() {
418  FMT_THROW(format_error("cannot format duration"));
419 }
420 
421 // Cast one integral duration to another with an overflow check.
422 template <typename To, typename FromRep, typename FromPeriod,
423  FMT_ENABLE_IF(std::is_integral<FromRep>::value&&
424  std::is_integral<typename To::rep>::value)>
425 auto duration_cast(std::chrono::duration<FromRep, FromPeriod> from) -> To {
426 #if !FMT_SAFE_DURATION_CAST
427  return std::chrono::duration_cast<To>(from);
428 #else
429  // The conversion factor: to.count() == factor * from.count().
430  using factor = std::ratio_divide<FromPeriod, typename To::period>;
431 
432  using common_rep = typename std::common_type<FromRep, typename To::rep,
433  decltype(factor::num)>::type;
434  common_rep count = from.count(); // This conversion is lossless.
435 
436  // Multiply from.count() by factor and check for overflow.
437  if (const_check(factor::num != 1)) {
438  if (count > max_value<common_rep>() / factor::num) throw_duration_error();
439  const auto min = (std::numeric_limits<common_rep>::min)() / factor::num;
440  if (const_check(!std::is_unsigned<common_rep>::value) && count < min)
441  throw_duration_error();
442  count *= factor::num;
443  }
444  if (const_check(factor::den != 1)) count /= factor::den;
445  int ec = 0;
446  auto to =
447  To(safe_duration_cast::lossless_integral_conversion<typename To::rep>(
448  count, ec));
449  if (ec) throw_duration_error();
450  return to;
451 #endif
452 }
453 
454 template <typename To, typename FromRep, typename FromPeriod,
455  FMT_ENABLE_IF(std::is_floating_point<FromRep>::value&&
456  std::is_floating_point<typename To::rep>::value)>
457 auto duration_cast(std::chrono::duration<FromRep, FromPeriod> from) -> To {
458 #if FMT_SAFE_DURATION_CAST
459  // Preserve infinity and NaN.
460  if (!isfinite(from.count())) return static_cast<To>(from.count());
461  // Throwing version of safe_duration_cast is only available for
462  // integer to integer or float to float casts.
463  int ec;
464  To to = safe_duration_cast::safe_duration_cast<To>(from, ec);
465  if (ec) throw_duration_error();
466  return to;
467 #else
468  // Standard duration cast, may overflow.
469  return std::chrono::duration_cast<To>(from);
470 #endif
471 }
472 
473 template <typename To, typename FromRep, typename FromPeriod,
474  FMT_ENABLE_IF(
475  !is_similar_arithmetic_type<FromRep, typename To::rep>::value)>
476 auto duration_cast(std::chrono::duration<FromRep, FromPeriod> from) -> To {
477  // Mixed integer <-> float cast is not supported by safe duration_cast.
478  return std::chrono::duration_cast<To>(from);
479 }
480 
481 template <typename Duration>
482 auto to_time_t(sys_time<Duration> time_point) -> std::time_t {
483  // Cannot use std::chrono::system_clock::to_time_t since this would first
484  // require a cast to std::chrono::system_clock::time_point, which could
485  // overflow.
486  return detail::duration_cast<std::chrono::duration<std::time_t>>(
487  time_point.time_since_epoch())
488  .count();
489 }
490 
491 } // namespace detail
492 
493 FMT_BEGIN_EXPORT
494 
500 inline auto gmtime(std::time_t time) -> std::tm {
501  struct dispatcher {
502  std::time_t time_;
503  std::tm tm_;
504 
505  inline dispatcher(std::time_t t) : time_(t) {}
506 
507  inline auto run() -> bool {
508  using namespace fmt::detail;
509  return handle(gmtime_r(&time_, &tm_));
510  }
511 
512  inline auto handle(std::tm* tm) -> bool { return tm != nullptr; }
513 
514  inline auto handle(detail::null<>) -> bool {
515  using namespace fmt::detail;
516  return fallback(gmtime_s(&tm_, &time_));
517  }
518 
519  inline auto fallback(int res) -> bool { return res == 0; }
520 
521 #if !FMT_MSC_VERSION
522  inline auto fallback(detail::null<>) -> bool {
523  std::tm* tm = std::gmtime(&time_);
524  if (tm) tm_ = *tm;
525  return tm != nullptr;
526  }
527 #endif
528  };
529  auto gt = dispatcher(time);
530  // Too big time values may be unsupported.
531  if (!gt.run()) FMT_THROW(format_error("time_t value out of range"));
532  return gt.tm_;
533 }
534 
535 template <typename Duration>
536 inline auto gmtime(sys_time<Duration> time_point) -> std::tm {
537  return gmtime(detail::to_time_t(time_point));
538 }
539 
540 namespace detail {
541 
542 // Writes two-digit numbers a, b and c separated by sep to buf.
543 // The method by Pavel Novikov based on
544 // https://johnnylee-sde.github.io/Fast-unsigned-integer-to-time-string/.
545 inline void write_digit2_separated(char* buf, unsigned a, unsigned b,
546  unsigned c, char sep) {
547  unsigned long long digits =
548  a | (b << 24) | (static_cast<unsigned long long>(c) << 48);
549  // Convert each value to BCD.
550  // We have x = a * 10 + b and we want to convert it to BCD y = a * 16 + b.
551  // The difference is
552  // y - x = a * 6
553  // a can be found from x:
554  // a = floor(x / 10)
555  // then
556  // y = x + a * 6 = x + floor(x / 10) * 6
557  // floor(x / 10) is (x * 205) >> 11 (needs 16 bits).
558  digits += (((digits * 205) >> 11) & 0x000f00000f00000f) * 6;
559  // Put low nibbles to high bytes and high nibbles to low bytes.
560  digits = ((digits & 0x00f00000f00000f0) >> 4) |
561  ((digits & 0x000f00000f00000f) << 8);
562  auto usep = static_cast<unsigned long long>(sep);
563  // Add ASCII '0' to each digit byte and insert separators.
564  digits |= 0x3030003030003030 | (usep << 16) | (usep << 40);
565 
566  constexpr size_t len = 8;
567  if (const_check(is_big_endian())) {
568  char tmp[len];
569  std::memcpy(tmp, &digits, len);
570  std::reverse_copy(tmp, tmp + len, buf);
571  } else {
572  std::memcpy(buf, &digits, len);
573  }
574 }
575 
576 template <typename Period>
577 FMT_CONSTEXPR inline auto get_units() -> const char* {
578  if (std::is_same<Period, std::atto>::value) return "as";
579  if (std::is_same<Period, std::femto>::value) return "fs";
580  if (std::is_same<Period, std::pico>::value) return "ps";
581  if (std::is_same<Period, std::nano>::value) return "ns";
582  if (std::is_same<Period, std::micro>::value)
583  return detail::use_utf8 ? "µs" : "us";
584  if (std::is_same<Period, std::milli>::value) return "ms";
585  if (std::is_same<Period, std::centi>::value) return "cs";
586  if (std::is_same<Period, std::deci>::value) return "ds";
587  if (std::is_same<Period, std::ratio<1>>::value) return "s";
588  if (std::is_same<Period, std::deca>::value) return "das";
589  if (std::is_same<Period, std::hecto>::value) return "hs";
590  if (std::is_same<Period, std::kilo>::value) return "ks";
591  if (std::is_same<Period, std::mega>::value) return "Ms";
592  if (std::is_same<Period, std::giga>::value) return "Gs";
593  if (std::is_same<Period, std::tera>::value) return "Ts";
594  if (std::is_same<Period, std::peta>::value) return "Ps";
595  if (std::is_same<Period, std::exa>::value) return "Es";
596  if (std::is_same<Period, std::ratio<60>>::value) return "min";
597  if (std::is_same<Period, std::ratio<3600>>::value) return "h";
598  if (std::is_same<Period, std::ratio<86400>>::value) return "d";
599  return nullptr;
600 }
601 
602 enum class numeric_system {
603  standard,
604  // Alternative numeric system, e.g. 十二 instead of 12 in ja_JP locale.
605  alternative
606 };
607 
608 // Glibc extensions for formatting numeric values.
609 enum class pad_type {
610  // Pad a numeric result string with zeros (the default).
611  zero,
612  // Do not pad a numeric result string.
613  none,
614  // Pad a numeric result string with spaces.
615  space,
616 };
617 
618 template <typename OutputIt>
619 auto write_padding(OutputIt out, pad_type pad, int width) -> OutputIt {
620  if (pad == pad_type::none) return out;
621  return detail::fill_n(out, width, pad == pad_type::space ? ' ' : '0');
622 }
623 
624 template <typename OutputIt>
625 auto write_padding(OutputIt out, pad_type pad) -> OutputIt {
626  if (pad != pad_type::none) *out++ = pad == pad_type::space ? ' ' : '0';
627  return out;
628 }
629 
630 // Parses a put_time-like format string and invokes handler actions.
631 template <typename Char, typename Handler>
632 FMT_CONSTEXPR auto parse_chrono_format(const Char* begin, const Char* end,
633  Handler&& handler) -> const Char* {
634  if (begin == end || *begin == '}') return begin;
635  if (*begin != '%') FMT_THROW(format_error("invalid format"));
636  auto ptr = begin;
637  while (ptr != end) {
638  pad_type pad = pad_type::zero;
639  auto c = *ptr;
640  if (c == '}') break;
641  if (c != '%') {
642  ++ptr;
643  continue;
644  }
645  if (begin != ptr) handler.on_text(begin, ptr);
646  ++ptr; // consume '%'
647  if (ptr == end) FMT_THROW(format_error("invalid format"));
648  c = *ptr;
649  switch (c) {
650  case '_':
651  pad = pad_type::space;
652  ++ptr;
653  break;
654  case '-':
655  pad = pad_type::none;
656  ++ptr;
657  break;
658  }
659  if (ptr == end) FMT_THROW(format_error("invalid format"));
660  c = *ptr++;
661  switch (c) {
662  case '%': handler.on_text(ptr - 1, ptr); break;
663  case 'n': {
664  const Char newline[] = {'\n'};
665  handler.on_text(newline, newline + 1);
666  break;
667  }
668  case 't': {
669  const Char tab[] = {'\t'};
670  handler.on_text(tab, tab + 1);
671  break;
672  }
673  // Year:
674  case 'Y': handler.on_year(numeric_system::standard, pad); break;
675  case 'y': handler.on_short_year(numeric_system::standard); break;
676  case 'C': handler.on_century(numeric_system::standard); break;
677  case 'G': handler.on_iso_week_based_year(); break;
678  case 'g': handler.on_iso_week_based_short_year(); break;
679  // Day of the week:
680  case 'a': handler.on_abbr_weekday(); break;
681  case 'A': handler.on_full_weekday(); break;
682  case 'w': handler.on_dec0_weekday(numeric_system::standard); break;
683  case 'u': handler.on_dec1_weekday(numeric_system::standard); break;
684  // Month:
685  case 'b':
686  case 'h': handler.on_abbr_month(); break;
687  case 'B': handler.on_full_month(); break;
688  case 'm': handler.on_dec_month(numeric_system::standard, pad); break;
689  // Day of the year/month:
690  case 'U':
691  handler.on_dec0_week_of_year(numeric_system::standard, pad);
692  break;
693  case 'W':
694  handler.on_dec1_week_of_year(numeric_system::standard, pad);
695  break;
696  case 'V': handler.on_iso_week_of_year(numeric_system::standard, pad); break;
697  case 'j': handler.on_day_of_year(pad); break;
698  case 'd': handler.on_day_of_month(numeric_system::standard, pad); break;
699  case 'e':
700  handler.on_day_of_month(numeric_system::standard, pad_type::space);
701  break;
702  // Hour, minute, second:
703  case 'H': handler.on_24_hour(numeric_system::standard, pad); break;
704  case 'I': handler.on_12_hour(numeric_system::standard, pad); break;
705  case 'M': handler.on_minute(numeric_system::standard, pad); break;
706  case 'S': handler.on_second(numeric_system::standard, pad); break;
707  // Other:
708  case 'c': handler.on_datetime(numeric_system::standard); break;
709  case 'x': handler.on_loc_date(numeric_system::standard); break;
710  case 'X': handler.on_loc_time(numeric_system::standard); break;
711  case 'D': handler.on_us_date(); break;
712  case 'F': handler.on_iso_date(); break;
713  case 'r': handler.on_12_hour_time(); break;
714  case 'R': handler.on_24_hour_time(); break;
715  case 'T': handler.on_iso_time(); break;
716  case 'p': handler.on_am_pm(); break;
717  case 'Q': handler.on_duration_value(); break;
718  case 'q': handler.on_duration_unit(); break;
719  case 'z': handler.on_utc_offset(numeric_system::standard); break;
720  case 'Z': handler.on_tz_name(); break;
721  // Alternative representation:
722  case 'E': {
723  if (ptr == end) FMT_THROW(format_error("invalid format"));
724  c = *ptr++;
725  switch (c) {
726  case 'Y': handler.on_year(numeric_system::alternative, pad); break;
727  case 'y': handler.on_offset_year(); break;
728  case 'C': handler.on_century(numeric_system::alternative); break;
729  case 'c': handler.on_datetime(numeric_system::alternative); break;
730  case 'x': handler.on_loc_date(numeric_system::alternative); break;
731  case 'X': handler.on_loc_time(numeric_system::alternative); break;
732  case 'z': handler.on_utc_offset(numeric_system::alternative); break;
733  default: FMT_THROW(format_error("invalid format"));
734  }
735  break;
736  }
737  case 'O':
738  if (ptr == end) FMT_THROW(format_error("invalid format"));
739  c = *ptr++;
740  switch (c) {
741  case 'y': handler.on_short_year(numeric_system::alternative); break;
742  case 'm': handler.on_dec_month(numeric_system::alternative, pad); break;
743  case 'U':
744  handler.on_dec0_week_of_year(numeric_system::alternative, pad);
745  break;
746  case 'W':
747  handler.on_dec1_week_of_year(numeric_system::alternative, pad);
748  break;
749  case 'V':
750  handler.on_iso_week_of_year(numeric_system::alternative, pad);
751  break;
752  case 'd':
753  handler.on_day_of_month(numeric_system::alternative, pad);
754  break;
755  case 'e':
756  handler.on_day_of_month(numeric_system::alternative, pad_type::space);
757  break;
758  case 'w': handler.on_dec0_weekday(numeric_system::alternative); break;
759  case 'u': handler.on_dec1_weekday(numeric_system::alternative); break;
760  case 'H': handler.on_24_hour(numeric_system::alternative, pad); break;
761  case 'I': handler.on_12_hour(numeric_system::alternative, pad); break;
762  case 'M': handler.on_minute(numeric_system::alternative, pad); break;
763  case 'S': handler.on_second(numeric_system::alternative, pad); break;
764  case 'z': handler.on_utc_offset(numeric_system::alternative); break;
765  default: FMT_THROW(format_error("invalid format"));
766  }
767  break;
768  default: FMT_THROW(format_error("invalid format"));
769  }
770  begin = ptr;
771  }
772  if (begin != ptr) handler.on_text(begin, ptr);
773  return ptr;
774 }
775 
776 template <typename Derived> struct null_chrono_spec_handler {
777  FMT_CONSTEXPR void unsupported() {
778  static_cast<Derived*>(this)->unsupported();
779  }
780  FMT_CONSTEXPR void on_year(numeric_system, pad_type) { unsupported(); }
781  FMT_CONSTEXPR void on_short_year(numeric_system) { unsupported(); }
782  FMT_CONSTEXPR void on_offset_year() { unsupported(); }
783  FMT_CONSTEXPR void on_century(numeric_system) { unsupported(); }
784  FMT_CONSTEXPR void on_iso_week_based_year() { unsupported(); }
785  FMT_CONSTEXPR void on_iso_week_based_short_year() { unsupported(); }
786  FMT_CONSTEXPR void on_abbr_weekday() { unsupported(); }
787  FMT_CONSTEXPR void on_full_weekday() { unsupported(); }
788  FMT_CONSTEXPR void on_dec0_weekday(numeric_system) { unsupported(); }
789  FMT_CONSTEXPR void on_dec1_weekday(numeric_system) { unsupported(); }
790  FMT_CONSTEXPR void on_abbr_month() { unsupported(); }
791  FMT_CONSTEXPR void on_full_month() { unsupported(); }
792  FMT_CONSTEXPR void on_dec_month(numeric_system, pad_type) { unsupported(); }
793  FMT_CONSTEXPR void on_dec0_week_of_year(numeric_system, pad_type) {
794  unsupported();
795  }
796  FMT_CONSTEXPR void on_dec1_week_of_year(numeric_system, pad_type) {
797  unsupported();
798  }
799  FMT_CONSTEXPR void on_iso_week_of_year(numeric_system, pad_type) {
800  unsupported();
801  }
802  FMT_CONSTEXPR void on_day_of_year(pad_type) { unsupported(); }
803  FMT_CONSTEXPR void on_day_of_month(numeric_system, pad_type) {
804  unsupported();
805  }
806  FMT_CONSTEXPR void on_24_hour(numeric_system) { unsupported(); }
807  FMT_CONSTEXPR void on_12_hour(numeric_system) { unsupported(); }
808  FMT_CONSTEXPR void on_minute(numeric_system) { unsupported(); }
809  FMT_CONSTEXPR void on_second(numeric_system) { unsupported(); }
810  FMT_CONSTEXPR void on_datetime(numeric_system) { unsupported(); }
811  FMT_CONSTEXPR void on_loc_date(numeric_system) { unsupported(); }
812  FMT_CONSTEXPR void on_loc_time(numeric_system) { unsupported(); }
813  FMT_CONSTEXPR void on_us_date() { unsupported(); }
814  FMT_CONSTEXPR void on_iso_date() { unsupported(); }
815  FMT_CONSTEXPR void on_12_hour_time() { unsupported(); }
816  FMT_CONSTEXPR void on_24_hour_time() { unsupported(); }
817  FMT_CONSTEXPR void on_iso_time() { unsupported(); }
818  FMT_CONSTEXPR void on_am_pm() { unsupported(); }
819  FMT_CONSTEXPR void on_duration_value() { unsupported(); }
820  FMT_CONSTEXPR void on_duration_unit() { unsupported(); }
821  FMT_CONSTEXPR void on_utc_offset(numeric_system) { unsupported(); }
822  FMT_CONSTEXPR void on_tz_name() { unsupported(); }
823 };
824 
825 class tm_format_checker : public null_chrono_spec_handler<tm_format_checker> {
826  private:
827  bool has_timezone_ = false;
828 
829  public:
830  constexpr explicit tm_format_checker(bool has_timezone)
831  : has_timezone_(has_timezone) {}
832 
833  FMT_NORETURN inline void unsupported() {
834  FMT_THROW(format_error("no format"));
835  }
836 
837  template <typename Char>
838  FMT_CONSTEXPR void on_text(const Char*, const Char*) {}
839  FMT_CONSTEXPR void on_year(numeric_system, pad_type) {}
840  FMT_CONSTEXPR void on_short_year(numeric_system) {}
841  FMT_CONSTEXPR void on_offset_year() {}
842  FMT_CONSTEXPR void on_century(numeric_system) {}
843  FMT_CONSTEXPR void on_iso_week_based_year() {}
844  FMT_CONSTEXPR void on_iso_week_based_short_year() {}
845  FMT_CONSTEXPR void on_abbr_weekday() {}
846  FMT_CONSTEXPR void on_full_weekday() {}
847  FMT_CONSTEXPR void on_dec0_weekday(numeric_system) {}
848  FMT_CONSTEXPR void on_dec1_weekday(numeric_system) {}
849  FMT_CONSTEXPR void on_abbr_month() {}
850  FMT_CONSTEXPR void on_full_month() {}
851  FMT_CONSTEXPR void on_dec_month(numeric_system, pad_type) {}
852  FMT_CONSTEXPR void on_dec0_week_of_year(numeric_system, pad_type) {}
853  FMT_CONSTEXPR void on_dec1_week_of_year(numeric_system, pad_type) {}
854  FMT_CONSTEXPR void on_iso_week_of_year(numeric_system, pad_type) {}
855  FMT_CONSTEXPR void on_day_of_year(pad_type) {}
856  FMT_CONSTEXPR void on_day_of_month(numeric_system, pad_type) {}
857  FMT_CONSTEXPR void on_24_hour(numeric_system, pad_type) {}
858  FMT_CONSTEXPR void on_12_hour(numeric_system, pad_type) {}
859  FMT_CONSTEXPR void on_minute(numeric_system, pad_type) {}
860  FMT_CONSTEXPR void on_second(numeric_system, pad_type) {}
861  FMT_CONSTEXPR void on_datetime(numeric_system) {}
862  FMT_CONSTEXPR void on_loc_date(numeric_system) {}
863  FMT_CONSTEXPR void on_loc_time(numeric_system) {}
864  FMT_CONSTEXPR void on_us_date() {}
865  FMT_CONSTEXPR void on_iso_date() {}
866  FMT_CONSTEXPR void on_12_hour_time() {}
867  FMT_CONSTEXPR void on_24_hour_time() {}
868  FMT_CONSTEXPR void on_iso_time() {}
869  FMT_CONSTEXPR void on_am_pm() {}
870  FMT_CONSTEXPR void on_utc_offset(numeric_system) {
871  if (!has_timezone_) FMT_THROW(format_error("no timezone"));
872  }
873  FMT_CONSTEXPR void on_tz_name() {
874  if (!has_timezone_) FMT_THROW(format_error("no timezone"));
875  }
876 };
877 
878 inline auto tm_wday_full_name(int wday) -> const char* {
879  static constexpr const char* full_name_list[] = {
880  "Sunday", "Monday", "Tuesday", "Wednesday",
881  "Thursday", "Friday", "Saturday"};
882  return wday >= 0 && wday <= 6 ? full_name_list[wday] : "?";
883 }
884 inline auto tm_wday_short_name(int wday) -> const char* {
885  static constexpr const char* short_name_list[] = {"Sun", "Mon", "Tue", "Wed",
886  "Thu", "Fri", "Sat"};
887  return wday >= 0 && wday <= 6 ? short_name_list[wday] : "???";
888 }
889 
890 inline auto tm_mon_full_name(int mon) -> const char* {
891  static constexpr const char* full_name_list[] = {
892  "January", "February", "March", "April", "May", "June",
893  "July", "August", "September", "October", "November", "December"};
894  return mon >= 0 && mon <= 11 ? full_name_list[mon] : "?";
895 }
896 inline auto tm_mon_short_name(int mon) -> const char* {
897  static constexpr const char* short_name_list[] = {
898  "Jan", "Feb", "Mar", "Apr", "May", "Jun",
899  "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
900  };
901  return mon >= 0 && mon <= 11 ? short_name_list[mon] : "???";
902 }
903 
904 template <typename T, typename = void>
905 struct has_tm_gmtoff : std::false_type {};
906 template <typename T>
907 struct has_tm_gmtoff<T, void_t<decltype(T::tm_gmtoff)>> : std::true_type {};
908 
909 template <typename T, typename = void> struct has_tm_zone : std::false_type {};
910 template <typename T>
911 struct has_tm_zone<T, void_t<decltype(T::tm_zone)>> : std::true_type {};
912 
913 template <typename T, FMT_ENABLE_IF(has_tm_zone<T>::value)>
914 auto set_tm_zone(T& time, char* tz) -> bool {
915  time.tm_zone = tz;
916  return true;
917 }
918 template <typename T, FMT_ENABLE_IF(!has_tm_zone<T>::value)>
919 auto set_tm_zone(T&, char*) -> bool {
920  return false;
921 }
922 
923 inline auto utc() -> char* {
924  static char tz[] = "UTC";
925  return tz;
926 }
927 
928 // Converts value to Int and checks that it's in the range [0, upper).
929 template <typename T, typename Int, FMT_ENABLE_IF(std::is_integral<T>::value)>
930 inline auto to_nonnegative_int(T value, Int upper) -> Int {
931  if (!std::is_unsigned<Int>::value &&
932  (value < 0 || to_unsigned(value) > to_unsigned(upper))) {
933  FMT_THROW(format_error("chrono value is out of range"));
934  }
935  return static_cast<Int>(value);
936 }
937 template <typename T, typename Int, FMT_ENABLE_IF(!std::is_integral<T>::value)>
938 inline auto to_nonnegative_int(T value, Int upper) -> Int {
939  auto int_value = static_cast<Int>(value);
940  if (int_value < 0 || value > static_cast<T>(upper))
941  FMT_THROW(format_error("invalid value"));
942  return int_value;
943 }
944 
945 constexpr auto pow10(std::uint32_t n) -> long long {
946  return n == 0 ? 1 : 10 * pow10(n - 1);
947 }
948 
949 // Counts the number of fractional digits in the range [0, 18] according to the
950 // C++20 spec. If more than 18 fractional digits are required then returns 6 for
951 // microseconds precision.
952 template <long long Num, long long Den, int N = 0,
953  bool Enabled = (N < 19) && (Num <= max_value<long long>() / 10)>
954 struct count_fractional_digits {
955  static constexpr int value =
956  Num % Den == 0 ? N : count_fractional_digits<Num * 10, Den, N + 1>::value;
957 };
958 
959 // Base case that doesn't instantiate any more templates
960 // in order to avoid overflow.
961 template <long long Num, long long Den, int N>
962 struct count_fractional_digits<Num, Den, N, false> {
963  static constexpr int value = (Num % Den == 0) ? N : 6;
964 };
965 
966 // Format subseconds which are given as an integer type with an appropriate
967 // number of digits.
968 template <typename Char, typename OutputIt, typename Duration>
969 void write_fractional_seconds(OutputIt& out, Duration d, int precision = -1) {
970  constexpr auto num_fractional_digits =
971  count_fractional_digits<Duration::period::num,
972  Duration::period::den>::value;
973 
974  using subsecond_precision = std::chrono::duration<
975  typename std::common_type<typename Duration::rep,
976  std::chrono::seconds::rep>::type,
977  std::ratio<1, pow10(num_fractional_digits)>>;
978 
979  const auto fractional = d - detail::duration_cast<std::chrono::seconds>(d);
980  const auto subseconds =
981  std::chrono::treat_as_floating_point<
982  typename subsecond_precision::rep>::value
983  ? fractional.count()
984  : detail::duration_cast<subsecond_precision>(fractional).count();
985  auto n = static_cast<uint32_or_64_or_128_t<long long>>(subseconds);
986  const int num_digits = count_digits(n);
987 
988  int leading_zeroes = (std::max)(0, num_fractional_digits - num_digits);
989  if (precision < 0) {
990  FMT_ASSERT(!std::is_floating_point<typename Duration::rep>::value, "");
991  if (std::ratio_less<typename subsecond_precision::period,
992  std::chrono::seconds::period>::value) {
993  *out++ = '.';
994  out = detail::fill_n(out, leading_zeroes, '0');
995  out = format_decimal<Char>(out, n, num_digits);
996  }
997  } else if (precision > 0) {
998  *out++ = '.';
999  leading_zeroes = min_of(leading_zeroes, precision);
1000  int remaining = precision - leading_zeroes;
1001  out = detail::fill_n(out, leading_zeroes, '0');
1002  if (remaining < num_digits) {
1003  int num_truncated_digits = num_digits - remaining;
1004  n /= to_unsigned(pow10(to_unsigned(num_truncated_digits)));
1005  if (n != 0) out = format_decimal<Char>(out, n, remaining);
1006  return;
1007  }
1008  if (n != 0) {
1009  out = format_decimal<Char>(out, n, num_digits);
1010  remaining -= num_digits;
1011  }
1012  out = detail::fill_n(out, remaining, '0');
1013  }
1014 }
1015 
1016 // Format subseconds which are given as a floating point type with an
1017 // appropriate number of digits. We cannot pass the Duration here, as we
1018 // explicitly need to pass the Rep value in the duration_formatter.
1019 template <typename Duration>
1020 void write_floating_seconds(memory_buffer& buf, Duration duration,
1021  int num_fractional_digits = -1) {
1022  using rep = typename Duration::rep;
1023  FMT_ASSERT(std::is_floating_point<rep>::value, "");
1024 
1025  auto val = duration.count();
1026 
1027  if (num_fractional_digits < 0) {
1028  // For `std::round` with fallback to `round`:
1029  // On some toolchains `std::round` is not available (e.g. GCC 6).
1030  using namespace std;
1031  num_fractional_digits =
1032  count_fractional_digits<Duration::period::num,
1033  Duration::period::den>::value;
1034  if (num_fractional_digits < 6 && static_cast<rep>(round(val)) != val)
1035  num_fractional_digits = 6;
1036  }
1037 
1038  fmt::format_to(std::back_inserter(buf), FMT_STRING("{:.{}f}"),
1039  std::fmod(val * static_cast<rep>(Duration::period::num) /
1040  static_cast<rep>(Duration::period::den),
1041  static_cast<rep>(60)),
1042  num_fractional_digits);
1043 }
1044 
1045 template <typename OutputIt, typename Char,
1046  typename Duration = std::chrono::seconds>
1047 class tm_writer {
1048  private:
1049  static constexpr int days_per_week = 7;
1050 
1051  const std::locale& loc_;
1052  bool is_classic_;
1053  OutputIt out_;
1054  const Duration* subsecs_;
1055  const std::tm& tm_;
1056 
1057  auto tm_sec() const noexcept -> int {
1058  FMT_ASSERT(tm_.tm_sec >= 0 && tm_.tm_sec <= 61, "");
1059  return tm_.tm_sec;
1060  }
1061  auto tm_min() const noexcept -> int {
1062  FMT_ASSERT(tm_.tm_min >= 0 && tm_.tm_min <= 59, "");
1063  return tm_.tm_min;
1064  }
1065  auto tm_hour() const noexcept -> int {
1066  FMT_ASSERT(tm_.tm_hour >= 0 && tm_.tm_hour <= 23, "");
1067  return tm_.tm_hour;
1068  }
1069  auto tm_mday() const noexcept -> int {
1070  FMT_ASSERT(tm_.tm_mday >= 1 && tm_.tm_mday <= 31, "");
1071  return tm_.tm_mday;
1072  }
1073  auto tm_mon() const noexcept -> int {
1074  FMT_ASSERT(tm_.tm_mon >= 0 && tm_.tm_mon <= 11, "");
1075  return tm_.tm_mon;
1076  }
1077  auto tm_year() const noexcept -> long long { return 1900ll + tm_.tm_year; }
1078  auto tm_wday() const noexcept -> int {
1079  FMT_ASSERT(tm_.tm_wday >= 0 && tm_.tm_wday <= 6, "");
1080  return tm_.tm_wday;
1081  }
1082  auto tm_yday() const noexcept -> int {
1083  FMT_ASSERT(tm_.tm_yday >= 0 && tm_.tm_yday <= 365, "");
1084  return tm_.tm_yday;
1085  }
1086 
1087  auto tm_hour12() const noexcept -> int {
1088  auto h = tm_hour();
1089  auto z = h < 12 ? h : h - 12;
1090  return z == 0 ? 12 : z;
1091  }
1092 
1093  // POSIX and the C Standard are unclear or inconsistent about what %C and %y
1094  // do if the year is negative or exceeds 9999. Use the convention that %C
1095  // concatenated with %y yields the same output as %Y, and that %Y contains at
1096  // least 4 characters, with more only if necessary.
1097  auto split_year_lower(long long year) const noexcept -> int {
1098  auto l = year % 100;
1099  if (l < 0) l = -l; // l in [0, 99]
1100  return static_cast<int>(l);
1101  }
1102 
1103  // Algorithm: https://en.wikipedia.org/wiki/ISO_week_date.
1104  auto iso_year_weeks(long long curr_year) const noexcept -> int {
1105  auto prev_year = curr_year - 1;
1106  auto curr_p =
1107  (curr_year + curr_year / 4 - curr_year / 100 + curr_year / 400) %
1108  days_per_week;
1109  auto prev_p =
1110  (prev_year + prev_year / 4 - prev_year / 100 + prev_year / 400) %
1111  days_per_week;
1112  return 52 + ((curr_p == 4 || prev_p == 3) ? 1 : 0);
1113  }
1114  auto iso_week_num(int tm_yday, int tm_wday) const noexcept -> int {
1115  return (tm_yday + 11 - (tm_wday == 0 ? days_per_week : tm_wday)) /
1116  days_per_week;
1117  }
1118  auto tm_iso_week_year() const noexcept -> long long {
1119  auto year = tm_year();
1120  auto w = iso_week_num(tm_yday(), tm_wday());
1121  if (w < 1) return year - 1;
1122  if (w > iso_year_weeks(year)) return year + 1;
1123  return year;
1124  }
1125  auto tm_iso_week_of_year() const noexcept -> int {
1126  auto year = tm_year();
1127  auto w = iso_week_num(tm_yday(), tm_wday());
1128  if (w < 1) return iso_year_weeks(year - 1);
1129  if (w > iso_year_weeks(year)) return 1;
1130  return w;
1131  }
1132 
1133  void write1(int value) {
1134  *out_++ = static_cast<char>('0' + to_unsigned(value) % 10);
1135  }
1136  void write2(int value) {
1137  const char* d = digits2(to_unsigned(value) % 100);
1138  *out_++ = *d++;
1139  *out_++ = *d;
1140  }
1141  void write2(int value, pad_type pad) {
1142  unsigned int v = to_unsigned(value) % 100;
1143  if (v >= 10) {
1144  const char* d = digits2(v);
1145  *out_++ = *d++;
1146  *out_++ = *d;
1147  } else {
1148  out_ = detail::write_padding(out_, pad);
1149  *out_++ = static_cast<char>('0' + v);
1150  }
1151  }
1152 
1153  void write_year_extended(long long year, pad_type pad) {
1154  // At least 4 characters.
1155  int width = 4;
1156  bool negative = year < 0;
1157  if (negative) {
1158  year = 0 - year;
1159  --width;
1160  }
1161  uint32_or_64_or_128_t<long long> n = to_unsigned(year);
1162  const int num_digits = count_digits(n);
1163  if (negative && pad == pad_type::zero) *out_++ = '-';
1164  if (width > num_digits)
1165  out_ = detail::write_padding(out_, pad, width - num_digits);
1166  if (negative && pad != pad_type::zero) *out_++ = '-';
1167  out_ = format_decimal<Char>(out_, n, num_digits);
1168  }
1169  void write_year(long long year, pad_type pad) {
1170  write_year_extended(year, pad);
1171  }
1172 
1173  void write_utc_offset(long long offset, numeric_system ns) {
1174  if (offset < 0) {
1175  *out_++ = '-';
1176  offset = -offset;
1177  } else {
1178  *out_++ = '+';
1179  }
1180  offset /= 60;
1181  write2(static_cast<int>(offset / 60));
1182  if (ns != numeric_system::standard) *out_++ = ':';
1183  write2(static_cast<int>(offset % 60));
1184  }
1185 
1186  template <typename T, FMT_ENABLE_IF(has_tm_gmtoff<T>::value)>
1187  void format_utc_offset(const T& tm, numeric_system ns) {
1188  write_utc_offset(tm.tm_gmtoff, ns);
1189  }
1190  template <typename T, FMT_ENABLE_IF(!has_tm_gmtoff<T>::value)>
1191  void format_utc_offset(const T&, numeric_system ns) {
1192  write_utc_offset(0, ns);
1193  }
1194 
1195  template <typename T, FMT_ENABLE_IF(has_tm_zone<T>::value)>
1196  void format_tz_name(const T& tm) {
1197  out_ = write_tm_str<Char>(out_, tm.tm_zone, loc_);
1198  }
1199  template <typename T, FMT_ENABLE_IF(!has_tm_zone<T>::value)>
1200  void format_tz_name(const T&) {
1201  out_ = std::copy_n(utc(), 3, out_);
1202  }
1203 
1204  void format_localized(char format, char modifier = 0) {
1205  out_ = write<Char>(out_, tm_, loc_, format, modifier);
1206  }
1207 
1208  public:
1209  tm_writer(const std::locale& loc, OutputIt out, const std::tm& tm,
1210  const Duration* subsecs = nullptr)
1211  : loc_(loc),
1212  is_classic_(loc_ == get_classic_locale()),
1213  out_(out),
1214  subsecs_(subsecs),
1215  tm_(tm) {}
1216 
1217  auto out() const -> OutputIt { return out_; }
1218 
1219  FMT_CONSTEXPR void on_text(const Char* begin, const Char* end) {
1220  out_ = copy<Char>(begin, end, out_);
1221  }
1222 
1223  void on_abbr_weekday() {
1224  if (is_classic_)
1225  out_ = write(out_, tm_wday_short_name(tm_wday()));
1226  else
1227  format_localized('a');
1228  }
1229  void on_full_weekday() {
1230  if (is_classic_)
1231  out_ = write(out_, tm_wday_full_name(tm_wday()));
1232  else
1233  format_localized('A');
1234  }
1235  void on_dec0_weekday(numeric_system ns) {
1236  if (is_classic_ || ns == numeric_system::standard) return write1(tm_wday());
1237  format_localized('w', 'O');
1238  }
1239  void on_dec1_weekday(numeric_system ns) {
1240  if (is_classic_ || ns == numeric_system::standard) {
1241  auto wday = tm_wday();
1242  write1(wday == 0 ? days_per_week : wday);
1243  } else {
1244  format_localized('u', 'O');
1245  }
1246  }
1247 
1248  void on_abbr_month() {
1249  if (is_classic_)
1250  out_ = write(out_, tm_mon_short_name(tm_mon()));
1251  else
1252  format_localized('b');
1253  }
1254  void on_full_month() {
1255  if (is_classic_)
1256  out_ = write(out_, tm_mon_full_name(tm_mon()));
1257  else
1258  format_localized('B');
1259  }
1260 
1261  void on_datetime(numeric_system ns) {
1262  if (is_classic_) {
1263  on_abbr_weekday();
1264  *out_++ = ' ';
1265  on_abbr_month();
1266  *out_++ = ' ';
1267  on_day_of_month(numeric_system::standard, pad_type::space);
1268  *out_++ = ' ';
1269  on_iso_time();
1270  *out_++ = ' ';
1271  on_year(numeric_system::standard, pad_type::space);
1272  } else {
1273  format_localized('c', ns == numeric_system::standard ? '\0' : 'E');
1274  }
1275  }
1276  void on_loc_date(numeric_system ns) {
1277  if (is_classic_)
1278  on_us_date();
1279  else
1280  format_localized('x', ns == numeric_system::standard ? '\0' : 'E');
1281  }
1282  void on_loc_time(numeric_system ns) {
1283  if (is_classic_)
1284  on_iso_time();
1285  else
1286  format_localized('X', ns == numeric_system::standard ? '\0' : 'E');
1287  }
1288  void on_us_date() {
1289  char buf[8];
1290  write_digit2_separated(buf, to_unsigned(tm_mon() + 1),
1291  to_unsigned(tm_mday()),
1292  to_unsigned(split_year_lower(tm_year())), '/');
1293  out_ = copy<Char>(std::begin(buf), std::end(buf), out_);
1294  }
1295  void on_iso_date() {
1296  auto year = tm_year();
1297  char buf[10];
1298  size_t offset = 0;
1299  if (year >= 0 && year < 10000) {
1300  write2digits(buf, static_cast<size_t>(year / 100));
1301  } else {
1302  offset = 4;
1303  write_year_extended(year, pad_type::zero);
1304  year = 0;
1305  }
1306  write_digit2_separated(buf + 2, static_cast<unsigned>(year % 100),
1307  to_unsigned(tm_mon() + 1), to_unsigned(tm_mday()),
1308  '-');
1309  out_ = copy<Char>(std::begin(buf) + offset, std::end(buf), out_);
1310  }
1311 
1312  void on_utc_offset(numeric_system ns) { format_utc_offset(tm_, ns); }
1313  void on_tz_name() { format_tz_name(tm_); }
1314 
1315  void on_year(numeric_system ns, pad_type pad) {
1316  if (is_classic_ || ns == numeric_system::standard)
1317  return write_year(tm_year(), pad);
1318  format_localized('Y', 'E');
1319  }
1320  void on_short_year(numeric_system ns) {
1321  if (is_classic_ || ns == numeric_system::standard)
1322  return write2(split_year_lower(tm_year()));
1323  format_localized('y', 'O');
1324  }
1325  void on_offset_year() {
1326  if (is_classic_) return write2(split_year_lower(tm_year()));
1327  format_localized('y', 'E');
1328  }
1329 
1330  void on_century(numeric_system ns) {
1331  if (is_classic_ || ns == numeric_system::standard) {
1332  auto year = tm_year();
1333  auto upper = year / 100;
1334  if (year >= -99 && year < 0) {
1335  // Zero upper on negative year.
1336  *out_++ = '-';
1337  *out_++ = '0';
1338  } else if (upper >= 0 && upper < 100) {
1339  write2(static_cast<int>(upper));
1340  } else {
1341  out_ = write<Char>(out_, upper);
1342  }
1343  } else {
1344  format_localized('C', 'E');
1345  }
1346  }
1347 
1348  void on_dec_month(numeric_system ns, pad_type pad) {
1349  if (is_classic_ || ns == numeric_system::standard)
1350  return write2(tm_mon() + 1, pad);
1351  format_localized('m', 'O');
1352  }
1353 
1354  void on_dec0_week_of_year(numeric_system ns, pad_type pad) {
1355  if (is_classic_ || ns == numeric_system::standard)
1356  return write2((tm_yday() + days_per_week - tm_wday()) / days_per_week,
1357  pad);
1358  format_localized('U', 'O');
1359  }
1360  void on_dec1_week_of_year(numeric_system ns, pad_type pad) {
1361  if (is_classic_ || ns == numeric_system::standard) {
1362  auto wday = tm_wday();
1363  write2((tm_yday() + days_per_week -
1364  (wday == 0 ? (days_per_week - 1) : (wday - 1))) /
1365  days_per_week,
1366  pad);
1367  } else {
1368  format_localized('W', 'O');
1369  }
1370  }
1371  void on_iso_week_of_year(numeric_system ns, pad_type pad) {
1372  if (is_classic_ || ns == numeric_system::standard)
1373  return write2(tm_iso_week_of_year(), pad);
1374  format_localized('V', 'O');
1375  }
1376 
1377  void on_iso_week_based_year() {
1378  write_year(tm_iso_week_year(), pad_type::zero);
1379  }
1380  void on_iso_week_based_short_year() {
1381  write2(split_year_lower(tm_iso_week_year()));
1382  }
1383 
1384  void on_day_of_year(pad_type pad) {
1385  auto yday = tm_yday() + 1;
1386  auto digit1 = yday / 100;
1387  if (digit1 != 0)
1388  write1(digit1);
1389  else
1390  out_ = detail::write_padding(out_, pad);
1391  write2(yday % 100, pad);
1392  }
1393 
1394  void on_day_of_month(numeric_system ns, pad_type pad) {
1395  if (is_classic_ || ns == numeric_system::standard)
1396  return write2(tm_mday(), pad);
1397  format_localized('d', 'O');
1398  }
1399 
1400  void on_24_hour(numeric_system ns, pad_type pad) {
1401  if (is_classic_ || ns == numeric_system::standard)
1402  return write2(tm_hour(), pad);
1403  format_localized('H', 'O');
1404  }
1405  void on_12_hour(numeric_system ns, pad_type pad) {
1406  if (is_classic_ || ns == numeric_system::standard)
1407  return write2(tm_hour12(), pad);
1408  format_localized('I', 'O');
1409  }
1410  void on_minute(numeric_system ns, pad_type pad) {
1411  if (is_classic_ || ns == numeric_system::standard)
1412  return write2(tm_min(), pad);
1413  format_localized('M', 'O');
1414  }
1415 
1416  void on_second(numeric_system ns, pad_type pad) {
1417  if (is_classic_ || ns == numeric_system::standard) {
1418  write2(tm_sec(), pad);
1419  if (subsecs_) {
1420  if (std::is_floating_point<typename Duration::rep>::value) {
1421  auto buf = memory_buffer();
1422  write_floating_seconds(buf, *subsecs_);
1423  if (buf.size() > 1) {
1424  // Remove the leading "0", write something like ".123".
1425  out_ = copy<Char>(buf.begin() + 1, buf.end(), out_);
1426  }
1427  } else {
1428  write_fractional_seconds<Char>(out_, *subsecs_);
1429  }
1430  }
1431  } else {
1432  // Currently no formatting of subseconds when a locale is set.
1433  format_localized('S', 'O');
1434  }
1435  }
1436 
1437  void on_12_hour_time() {
1438  if (is_classic_) {
1439  char buf[8];
1440  write_digit2_separated(buf, to_unsigned(tm_hour12()),
1441  to_unsigned(tm_min()), to_unsigned(tm_sec()), ':');
1442  out_ = copy<Char>(std::begin(buf), std::end(buf), out_);
1443  *out_++ = ' ';
1444  on_am_pm();
1445  } else {
1446  format_localized('r');
1447  }
1448  }
1449  void on_24_hour_time() {
1450  write2(tm_hour());
1451  *out_++ = ':';
1452  write2(tm_min());
1453  }
1454  void on_iso_time() {
1455  on_24_hour_time();
1456  *out_++ = ':';
1457  on_second(numeric_system::standard, pad_type::zero);
1458  }
1459 
1460  void on_am_pm() {
1461  if (is_classic_) {
1462  *out_++ = tm_hour() < 12 ? 'A' : 'P';
1463  *out_++ = 'M';
1464  } else {
1465  format_localized('p');
1466  }
1467  }
1468 
1469  // These apply to chrono durations but not tm.
1470  void on_duration_value() {}
1471  void on_duration_unit() {}
1472 };
1473 
1474 struct chrono_format_checker : null_chrono_spec_handler<chrono_format_checker> {
1475  bool has_precision_integral = false;
1476 
1477  FMT_NORETURN inline void unsupported() { FMT_THROW(format_error("no date")); }
1478 
1479  template <typename Char>
1480  FMT_CONSTEXPR void on_text(const Char*, const Char*) {}
1481  FMT_CONSTEXPR void on_day_of_year(pad_type) {}
1482  FMT_CONSTEXPR void on_24_hour(numeric_system, pad_type) {}
1483  FMT_CONSTEXPR void on_12_hour(numeric_system, pad_type) {}
1484  FMT_CONSTEXPR void on_minute(numeric_system, pad_type) {}
1485  FMT_CONSTEXPR void on_second(numeric_system, pad_type) {}
1486  FMT_CONSTEXPR void on_12_hour_time() {}
1487  FMT_CONSTEXPR void on_24_hour_time() {}
1488  FMT_CONSTEXPR void on_iso_time() {}
1489  FMT_CONSTEXPR void on_am_pm() {}
1490  FMT_CONSTEXPR void on_duration_value() const {
1491  if (has_precision_integral)
1492  FMT_THROW(format_error("precision not allowed for this argument type"));
1493  }
1494  FMT_CONSTEXPR void on_duration_unit() {}
1495 };
1496 
1497 template <typename T,
1498  FMT_ENABLE_IF(std::is_integral<T>::value&& has_isfinite<T>::value)>
1499 inline auto isfinite(T) -> bool {
1500  return true;
1501 }
1502 
1503 template <typename T, FMT_ENABLE_IF(std::is_integral<T>::value)>
1504 inline auto mod(T x, int y) -> T {
1505  return x % static_cast<T>(y);
1506 }
1507 template <typename T, FMT_ENABLE_IF(std::is_floating_point<T>::value)>
1508 inline auto mod(T x, int y) -> T {
1509  return std::fmod(x, static_cast<T>(y));
1510 }
1511 
1512 // If T is an integral type, maps T to its unsigned counterpart, otherwise
1513 // leaves it unchanged (unlike std::make_unsigned).
1514 template <typename T, bool INTEGRAL = std::is_integral<T>::value>
1515 struct make_unsigned_or_unchanged {
1516  using type = T;
1517 };
1518 
1519 template <typename T> struct make_unsigned_or_unchanged<T, true> {
1520  using type = typename std::make_unsigned<T>::type;
1521 };
1522 
1523 template <typename Rep, typename Period,
1524  FMT_ENABLE_IF(std::is_integral<Rep>::value)>
1525 inline auto get_milliseconds(std::chrono::duration<Rep, Period> d)
1526  -> std::chrono::duration<Rep, std::milli> {
1527  // This may overflow and/or the result may not fit in the target type.
1528 #if FMT_SAFE_DURATION_CAST
1529  using common_seconds_type =
1530  typename std::common_type<decltype(d), std::chrono::seconds>::type;
1531  auto d_as_common = detail::duration_cast<common_seconds_type>(d);
1532  auto d_as_whole_seconds =
1533  detail::duration_cast<std::chrono::seconds>(d_as_common);
1534  // This conversion should be nonproblematic.
1535  auto diff = d_as_common - d_as_whole_seconds;
1536  auto ms = detail::duration_cast<std::chrono::duration<Rep, std::milli>>(diff);
1537  return ms;
1538 #else
1539  auto s = detail::duration_cast<std::chrono::seconds>(d);
1540  return detail::duration_cast<std::chrono::milliseconds>(d - s);
1541 #endif
1542 }
1543 
1544 template <typename Char, typename Rep, typename OutputIt,
1545  FMT_ENABLE_IF(std::is_integral<Rep>::value)>
1546 auto format_duration_value(OutputIt out, Rep val, int) -> OutputIt {
1547  return write<Char>(out, val);
1548 }
1549 
1550 template <typename Char, typename Rep, typename OutputIt,
1551  FMT_ENABLE_IF(std::is_floating_point<Rep>::value)>
1552 auto format_duration_value(OutputIt out, Rep val, int precision) -> OutputIt {
1553  auto specs = format_specs();
1554  specs.precision = precision;
1555  specs.set_type(precision >= 0 ? presentation_type::fixed
1556  : presentation_type::general);
1557  return write<Char>(out, val, specs);
1558 }
1559 
1560 template <typename Char, typename OutputIt>
1561 auto copy_unit(string_view unit, OutputIt out, Char) -> OutputIt {
1562  return copy<Char>(unit.begin(), unit.end(), out);
1563 }
1564 
1565 template <typename OutputIt>
1566 auto copy_unit(string_view unit, OutputIt out, wchar_t) -> OutputIt {
1567  // This works when wchar_t is UTF-32 because units only contain characters
1568  // that have the same representation in UTF-16 and UTF-32.
1569  utf8_to_utf16 u(unit);
1570  return copy<wchar_t>(u.c_str(), u.c_str() + u.size(), out);
1571 }
1572 
1573 template <typename Char, typename Period, typename OutputIt>
1574 auto format_duration_unit(OutputIt out) -> OutputIt {
1575  if (const char* unit = get_units<Period>())
1576  return copy_unit(string_view(unit), out, Char());
1577  *out++ = '[';
1578  out = write<Char>(out, Period::num);
1579  if (const_check(Period::den != 1)) {
1580  *out++ = '/';
1581  out = write<Char>(out, Period::den);
1582  }
1583  *out++ = ']';
1584  *out++ = 's';
1585  return out;
1586 }
1587 
1588 class get_locale {
1589  private:
1590  union {
1591  std::locale locale_;
1592  };
1593  bool has_locale_ = false;
1594 
1595  public:
1596  inline get_locale(bool localized, locale_ref loc) : has_locale_(localized) {
1597  if (localized)
1598  ::new (&locale_) std::locale(loc.template get<std::locale>());
1599  }
1600  inline ~get_locale() {
1601  if (has_locale_) locale_.~locale();
1602  }
1603  inline operator const std::locale&() const {
1604  return has_locale_ ? locale_ : get_classic_locale();
1605  }
1606 };
1607 
1608 template <typename Char, typename Rep, typename Period>
1609 struct duration_formatter {
1610  using iterator = basic_appender<Char>;
1611  iterator out;
1612  // rep is unsigned to avoid overflow.
1613  using rep =
1614  conditional_t<std::is_integral<Rep>::value && sizeof(Rep) < sizeof(int),
1615  unsigned, typename make_unsigned_or_unchanged<Rep>::type>;
1616  rep val;
1617  int precision;
1618  locale_ref locale;
1619  bool localized = false;
1620  using seconds = std::chrono::duration<rep>;
1621  seconds s;
1622  using milliseconds = std::chrono::duration<rep, std::milli>;
1623  bool negative;
1624 
1625  using tm_writer_type = tm_writer<iterator, Char>;
1626 
1627  duration_formatter(iterator o, std::chrono::duration<Rep, Period> d,
1628  locale_ref loc)
1629  : out(o), val(static_cast<rep>(d.count())), locale(loc), negative(false) {
1630  if (d.count() < 0) {
1631  val = 0 - val;
1632  negative = true;
1633  }
1634 
1635  // this may overflow and/or the result may not fit in the
1636  // target type.
1637  // might need checked conversion (rep!=Rep)
1638  s = detail::duration_cast<seconds>(std::chrono::duration<rep, Period>(val));
1639  }
1640 
1641  // returns true if nan or inf, writes to out.
1642  auto handle_nan_inf() -> bool {
1643  if (isfinite(val)) return false;
1644  if (isnan(val)) {
1645  write_nan();
1646  return true;
1647  }
1648  // must be +-inf
1649  if (val > 0)
1650  std::copy_n("inf", 3, out);
1651  else
1652  std::copy_n("-inf", 4, out);
1653  return true;
1654  }
1655 
1656  auto days() const -> Rep { return static_cast<Rep>(s.count() / 86400); }
1657  auto hour() const -> Rep {
1658  return static_cast<Rep>(mod((s.count() / 3600), 24));
1659  }
1660 
1661  auto hour12() const -> Rep {
1662  Rep hour = static_cast<Rep>(mod((s.count() / 3600), 12));
1663  return hour <= 0 ? 12 : hour;
1664  }
1665 
1666  auto minute() const -> Rep {
1667  return static_cast<Rep>(mod((s.count() / 60), 60));
1668  }
1669  auto second() const -> Rep { return static_cast<Rep>(mod(s.count(), 60)); }
1670 
1671  auto time() const -> std::tm {
1672  auto time = std::tm();
1673  time.tm_hour = to_nonnegative_int(hour(), 24);
1674  time.tm_min = to_nonnegative_int(minute(), 60);
1675  time.tm_sec = to_nonnegative_int(second(), 60);
1676  return time;
1677  }
1678 
1679  void write_sign() {
1680  if (!negative) return;
1681  *out++ = '-';
1682  negative = false;
1683  }
1684 
1685  void write(Rep value, int width, pad_type pad = pad_type::zero) {
1686  write_sign();
1687  if (isnan(value)) return write_nan();
1688  uint32_or_64_or_128_t<int> n =
1689  to_unsigned(to_nonnegative_int(value, max_value<int>()));
1690  int num_digits = detail::count_digits(n);
1691  if (width > num_digits) {
1692  out = detail::write_padding(out, pad, width - num_digits);
1693  }
1694  out = format_decimal<Char>(out, n, num_digits);
1695  }
1696 
1697  void write_nan() { std::copy_n("nan", 3, out); }
1698 
1699  template <typename Callback, typename... Args>
1700  void format_tm(const tm& time, Callback cb, Args... args) {
1701  if (isnan(val)) return write_nan();
1702  get_locale loc(localized, locale);
1703  auto w = tm_writer_type(loc, out, time);
1704  (w.*cb)(args...);
1705  out = w.out();
1706  }
1707 
1708  void on_text(const Char* begin, const Char* end) {
1709  copy<Char>(begin, end, out);
1710  }
1711 
1712  // These are not implemented because durations don't have date information.
1713  void on_abbr_weekday() {}
1714  void on_full_weekday() {}
1715  void on_dec0_weekday(numeric_system) {}
1716  void on_dec1_weekday(numeric_system) {}
1717  void on_abbr_month() {}
1718  void on_full_month() {}
1719  void on_datetime(numeric_system) {}
1720  void on_loc_date(numeric_system) {}
1721  void on_loc_time(numeric_system) {}
1722  void on_us_date() {}
1723  void on_iso_date() {}
1724  void on_utc_offset(numeric_system) {}
1725  void on_tz_name() {}
1726  void on_year(numeric_system, pad_type) {}
1727  void on_short_year(numeric_system) {}
1728  void on_offset_year() {}
1729  void on_century(numeric_system) {}
1730  void on_iso_week_based_year() {}
1731  void on_iso_week_based_short_year() {}
1732  void on_dec_month(numeric_system, pad_type) {}
1733  void on_dec0_week_of_year(numeric_system, pad_type) {}
1734  void on_dec1_week_of_year(numeric_system, pad_type) {}
1735  void on_iso_week_of_year(numeric_system, pad_type) {}
1736  void on_day_of_month(numeric_system, pad_type) {}
1737 
1738  void on_day_of_year(pad_type) {
1739  if (handle_nan_inf()) return;
1740  write(days(), 0);
1741  }
1742 
1743  void on_24_hour(numeric_system ns, pad_type pad) {
1744  if (handle_nan_inf()) return;
1745 
1746  if (ns == numeric_system::standard) return write(hour(), 2, pad);
1747  auto time = tm();
1748  time.tm_hour = to_nonnegative_int(hour(), 24);
1749  format_tm(time, &tm_writer_type::on_24_hour, ns, pad);
1750  }
1751 
1752  void on_12_hour(numeric_system ns, pad_type pad) {
1753  if (handle_nan_inf()) return;
1754 
1755  if (ns == numeric_system::standard) return write(hour12(), 2, pad);
1756  auto time = tm();
1757  time.tm_hour = to_nonnegative_int(hour12(), 12);
1758  format_tm(time, &tm_writer_type::on_12_hour, ns, pad);
1759  }
1760 
1761  void on_minute(numeric_system ns, pad_type pad) {
1762  if (handle_nan_inf()) return;
1763 
1764  if (ns == numeric_system::standard) return write(minute(), 2, pad);
1765  auto time = tm();
1766  time.tm_min = to_nonnegative_int(minute(), 60);
1767  format_tm(time, &tm_writer_type::on_minute, ns, pad);
1768  }
1769 
1770  void on_second(numeric_system ns, pad_type pad) {
1771  if (handle_nan_inf()) return;
1772 
1773  if (ns == numeric_system::standard) {
1774  if (std::is_floating_point<rep>::value) {
1775  auto buf = memory_buffer();
1776  write_floating_seconds(buf, std::chrono::duration<rep, Period>(val),
1777  precision);
1778  if (negative) *out++ = '-';
1779  if (buf.size() < 2 || buf[1] == '.')
1780  out = detail::write_padding(out, pad);
1781  out = copy<Char>(buf.begin(), buf.end(), out);
1782  } else {
1783  write(second(), 2, pad);
1784  write_fractional_seconds<Char>(
1785  out, std::chrono::duration<rep, Period>(val), precision);
1786  }
1787  return;
1788  }
1789  auto time = tm();
1790  time.tm_sec = to_nonnegative_int(second(), 60);
1791  format_tm(time, &tm_writer_type::on_second, ns, pad);
1792  }
1793 
1794  void on_12_hour_time() {
1795  if (handle_nan_inf()) return;
1796  format_tm(time(), &tm_writer_type::on_12_hour_time);
1797  }
1798 
1799  void on_24_hour_time() {
1800  if (handle_nan_inf()) {
1801  *out++ = ':';
1802  handle_nan_inf();
1803  return;
1804  }
1805 
1806  write(hour(), 2);
1807  *out++ = ':';
1808  write(minute(), 2);
1809  }
1810 
1811  void on_iso_time() {
1812  on_24_hour_time();
1813  *out++ = ':';
1814  if (handle_nan_inf()) return;
1815  on_second(numeric_system::standard, pad_type::zero);
1816  }
1817 
1818  void on_am_pm() {
1819  if (handle_nan_inf()) return;
1820  format_tm(time(), &tm_writer_type::on_am_pm);
1821  }
1822 
1823  void on_duration_value() {
1824  if (handle_nan_inf()) return;
1825  write_sign();
1826  out = format_duration_value<Char>(out, val, precision);
1827  }
1828 
1829  void on_duration_unit() { out = format_duration_unit<Char, Period>(out); }
1830 };
1831 
1832 } // namespace detail
1833 
1834 #if defined(__cpp_lib_chrono) && __cpp_lib_chrono >= 201907
1835 using weekday = std::chrono::weekday;
1836 using day = std::chrono::day;
1837 using month = std::chrono::month;
1838 using year = std::chrono::year;
1839 using year_month_day = std::chrono::year_month_day;
1840 #else
1841 // A fallback version of weekday.
1842 class weekday {
1843  private:
1844  unsigned char value_;
1845 
1846  public:
1847  weekday() = default;
1848  constexpr explicit weekday(unsigned wd) noexcept
1849  : value_(static_cast<unsigned char>(wd != 7 ? wd : 0)) {}
1850  constexpr auto c_encoding() const noexcept -> unsigned { return value_; }
1851 };
1852 
1853 class day {
1854  private:
1855  unsigned char value_;
1856 
1857  public:
1858  day() = default;
1859  constexpr explicit day(unsigned d) noexcept
1860  : value_(static_cast<unsigned char>(d)) {}
1861  constexpr explicit operator unsigned() const noexcept { return value_; }
1862 };
1863 
1864 class month {
1865  private:
1866  unsigned char value_;
1867 
1868  public:
1869  month() = default;
1870  constexpr explicit month(unsigned m) noexcept
1871  : value_(static_cast<unsigned char>(m)) {}
1872  constexpr explicit operator unsigned() const noexcept { return value_; }
1873 };
1874 
1875 class year {
1876  private:
1877  int value_;
1878 
1879  public:
1880  year() = default;
1881  constexpr explicit year(int y) noexcept : value_(y) {}
1882  constexpr explicit operator int() const noexcept { return value_; }
1883 };
1884 
1885 class year_month_day {
1886  private:
1887  fmt::year year_;
1888  fmt::month month_;
1889  fmt::day day_;
1890 
1891  public:
1892  year_month_day() = default;
1893  constexpr year_month_day(const year& y, const month& m, const day& d) noexcept
1894  : year_(y), month_(m), day_(d) {}
1895  constexpr auto year() const noexcept -> fmt::year { return year_; }
1896  constexpr auto month() const noexcept -> fmt::month { return month_; }
1897  constexpr auto day() const noexcept -> fmt::day { return day_; }
1898 };
1899 #endif // __cpp_lib_chrono >= 201907
1900 
1901 template <typename Char>
1902 struct formatter<weekday, Char> : private formatter<std::tm, Char> {
1903  private:
1904  bool use_tm_formatter_ = false;
1905 
1906  public:
1907  FMT_CONSTEXPR auto parse(parse_context<Char>& ctx) -> const Char* {
1908  auto it = ctx.begin(), end = ctx.end();
1909  if (it != end && *it == 'L') {
1910  ++it;
1911  this->set_localized();
1912  }
1913  use_tm_formatter_ = it != end && *it != '}';
1914  return use_tm_formatter_ ? formatter<std::tm, Char>::parse(ctx) : it;
1915  }
1916 
1917  template <typename FormatContext>
1918  auto format(weekday wd, FormatContext& ctx) const -> decltype(ctx.out()) {
1919  auto time = std::tm();
1920  time.tm_wday = static_cast<int>(wd.c_encoding());
1921  if (use_tm_formatter_) return formatter<std::tm, Char>::format(time, ctx);
1922  detail::get_locale loc(this->localized(), ctx.locale());
1923  auto w = detail::tm_writer<decltype(ctx.out()), Char>(loc, ctx.out(), time);
1924  w.on_abbr_weekday();
1925  return w.out();
1926  }
1927 };
1928 
1929 template <typename Char>
1930 struct formatter<day, Char> : private formatter<std::tm, Char> {
1931  private:
1932  bool use_tm_formatter_ = false;
1933 
1934  public:
1935  FMT_CONSTEXPR auto parse(parse_context<Char>& ctx) -> const Char* {
1936  auto it = ctx.begin(), end = ctx.end();
1937  use_tm_formatter_ = it != end && *it != '}';
1938  return use_tm_formatter_ ? formatter<std::tm, Char>::parse(ctx) : it;
1939  }
1940 
1941  template <typename FormatContext>
1942  auto format(day d, FormatContext& ctx) const -> decltype(ctx.out()) {
1943  auto time = std::tm();
1944  time.tm_mday = static_cast<int>(static_cast<unsigned>(d));
1945  if (use_tm_formatter_) return formatter<std::tm, Char>::format(time, ctx);
1946  detail::get_locale loc(false, ctx.locale());
1947  auto w = detail::tm_writer<decltype(ctx.out()), Char>(loc, ctx.out(), time);
1948  w.on_day_of_month(detail::numeric_system::standard, detail::pad_type::zero);
1949  return w.out();
1950  }
1951 };
1952 
1953 template <typename Char>
1954 struct formatter<month, Char> : private formatter<std::tm, Char> {
1955  private:
1956  bool use_tm_formatter_ = false;
1957 
1958  public:
1959  FMT_CONSTEXPR auto parse(parse_context<Char>& ctx) -> const Char* {
1960  auto it = ctx.begin(), end = ctx.end();
1961  if (it != end && *it == 'L') {
1962  ++it;
1963  this->set_localized();
1964  }
1965  use_tm_formatter_ = it != end && *it != '}';
1966  return use_tm_formatter_ ? formatter<std::tm, Char>::parse(ctx) : it;
1967  }
1968 
1969  template <typename FormatContext>
1970  auto format(month m, FormatContext& ctx) const -> decltype(ctx.out()) {
1971  auto time = std::tm();
1972  time.tm_mon = static_cast<int>(static_cast<unsigned>(m)) - 1;
1973  if (use_tm_formatter_) return formatter<std::tm, Char>::format(time, ctx);
1974  detail::get_locale loc(this->localized(), ctx.locale());
1975  auto w = detail::tm_writer<decltype(ctx.out()), Char>(loc, ctx.out(), time);
1976  w.on_abbr_month();
1977  return w.out();
1978  }
1979 };
1980 
1981 template <typename Char>
1982 struct formatter<year, Char> : private formatter<std::tm, Char> {
1983  private:
1984  bool use_tm_formatter_ = false;
1985 
1986  public:
1987  FMT_CONSTEXPR auto parse(parse_context<Char>& ctx) -> const Char* {
1988  auto it = ctx.begin(), end = ctx.end();
1989  use_tm_formatter_ = it != end && *it != '}';
1990  return use_tm_formatter_ ? formatter<std::tm, Char>::parse(ctx) : it;
1991  }
1992 
1993  template <typename FormatContext>
1994  auto format(year y, FormatContext& ctx) const -> decltype(ctx.out()) {
1995  auto time = std::tm();
1996  time.tm_year = static_cast<int>(y) - 1900;
1997  if (use_tm_formatter_) return formatter<std::tm, Char>::format(time, ctx);
1998  detail::get_locale loc(false, ctx.locale());
1999  auto w = detail::tm_writer<decltype(ctx.out()), Char>(loc, ctx.out(), time);
2000  w.on_year(detail::numeric_system::standard, detail::pad_type::zero);
2001  return w.out();
2002  }
2003 };
2004 
2005 template <typename Char>
2006 struct formatter<year_month_day, Char> : private formatter<std::tm, Char> {
2007  private:
2008  bool use_tm_formatter_ = false;
2009 
2010  public:
2011  FMT_CONSTEXPR auto parse(parse_context<Char>& ctx) -> const Char* {
2012  auto it = ctx.begin(), end = ctx.end();
2013  use_tm_formatter_ = it != end && *it != '}';
2014  return use_tm_formatter_ ? formatter<std::tm, Char>::parse(ctx) : it;
2015  }
2016 
2017  template <typename FormatContext>
2018  auto format(year_month_day val, FormatContext& ctx) const
2019  -> decltype(ctx.out()) {
2020  auto time = std::tm();
2021  time.tm_year = static_cast<int>(val.year()) - 1900;
2022  time.tm_mon = static_cast<int>(static_cast<unsigned>(val.month())) - 1;
2023  time.tm_mday = static_cast<int>(static_cast<unsigned>(val.day()));
2024  if (use_tm_formatter_) return formatter<std::tm, Char>::format(time, ctx);
2025  detail::get_locale loc(true, ctx.locale());
2026  auto w = detail::tm_writer<decltype(ctx.out()), Char>(loc, ctx.out(), time);
2027  w.on_iso_date();
2028  return w.out();
2029  }
2030 };
2031 
2032 template <typename Rep, typename Period, typename Char>
2033 struct formatter<std::chrono::duration<Rep, Period>, Char> {
2034  private:
2035  format_specs specs_;
2036  detail::arg_ref<Char> width_ref_;
2037  detail::arg_ref<Char> precision_ref_;
2039 
2040  public:
2041  FMT_CONSTEXPR auto parse(parse_context<Char>& ctx) -> const Char* {
2042  auto it = ctx.begin(), end = ctx.end();
2043  if (it == end || *it == '}') return it;
2044 
2045  it = detail::parse_align(it, end, specs_);
2046  if (it == end) return it;
2047 
2048  Char c = *it;
2049  if ((c >= '0' && c <= '9') || c == '{') {
2050  it = detail::parse_width(it, end, specs_, width_ref_, ctx);
2051  if (it == end) return it;
2052  }
2053 
2054  auto checker = detail::chrono_format_checker();
2055  if (*it == '.') {
2056  checker.has_precision_integral = !std::is_floating_point<Rep>::value;
2057  it = detail::parse_precision(it, end, specs_, precision_ref_, ctx);
2058  }
2059  if (it != end && *it == 'L') {
2060  specs_.set_localized();
2061  ++it;
2062  }
2063  end = detail::parse_chrono_format(it, end, checker);
2064  fmt_ = {it, detail::to_unsigned(end - it)};
2065  return end;
2066  }
2067 
2068  template <typename FormatContext>
2069  auto format(std::chrono::duration<Rep, Period> d, FormatContext& ctx) const
2070  -> decltype(ctx.out()) {
2071  auto specs = specs_;
2072  auto precision = specs.precision;
2073  specs.precision = -1;
2074  auto begin = fmt_.begin(), end = fmt_.end();
2075  // As a possible future optimization, we could avoid extra copying if width
2076  // is not specified.
2077  auto buf = basic_memory_buffer<Char>();
2078  auto out = basic_appender<Char>(buf);
2079  detail::handle_dynamic_spec(specs.dynamic_width(), specs.width, width_ref_,
2080  ctx);
2081  detail::handle_dynamic_spec(specs.dynamic_precision(), precision,
2082  precision_ref_, ctx);
2083  if (begin == end || *begin == '}') {
2084  out = detail::format_duration_value<Char>(out, d.count(), precision);
2085  detail::format_duration_unit<Char, Period>(out);
2086  } else {
2087  auto f =
2088  detail::duration_formatter<Char, Rep, Period>(out, d, ctx.locale());
2089  f.precision = precision;
2090  f.localized = specs_.localized();
2091  detail::parse_chrono_format(begin, end, f);
2092  }
2093  return detail::write(
2094  ctx.out(), basic_string_view<Char>(buf.data(), buf.size()), specs);
2095  }
2096 };
2097 
2098 template <typename Char> struct formatter<std::tm, Char> {
2099  private:
2100  format_specs specs_;
2101  detail::arg_ref<Char> width_ref_;
2103  detail::string_literal<Char, '%', 'F', ' ', '%', 'T'>();
2104 
2105  protected:
2106  auto localized() const -> bool { return specs_.localized(); }
2107  FMT_CONSTEXPR void set_localized() { specs_.set_localized(); }
2108 
2109  FMT_CONSTEXPR auto do_parse(parse_context<Char>& ctx, bool has_timezone)
2110  -> const Char* {
2111  auto it = ctx.begin(), end = ctx.end();
2112  if (it == end || *it == '}') return it;
2113 
2114  it = detail::parse_align(it, end, specs_);
2115  if (it == end) return it;
2116 
2117  Char c = *it;
2118  if ((c >= '0' && c <= '9') || c == '{') {
2119  it = detail::parse_width(it, end, specs_, width_ref_, ctx);
2120  if (it == end) return it;
2121  }
2122 
2123  if (*it == 'L') {
2124  specs_.set_localized();
2125  ++it;
2126  }
2127 
2128  end = detail::parse_chrono_format(it, end,
2129  detail::tm_format_checker(has_timezone));
2130  // Replace the default format string only if the new spec is not empty.
2131  if (end != it) fmt_ = {it, detail::to_unsigned(end - it)};
2132  return end;
2133  }
2134 
2135  template <typename Duration, typename FormatContext>
2136  auto do_format(const std::tm& tm, FormatContext& ctx,
2137  const Duration* subsecs) const -> decltype(ctx.out()) {
2138  auto specs = specs_;
2139  auto buf = basic_memory_buffer<Char>();
2140  auto out = basic_appender<Char>(buf);
2141  detail::handle_dynamic_spec(specs.dynamic_width(), specs.width, width_ref_,
2142  ctx);
2143 
2144  auto loc_ref = specs.localized() ? ctx.locale() : locale_ref();
2145  detail::get_locale loc(static_cast<bool>(loc_ref), loc_ref);
2146  auto w = detail::tm_writer<basic_appender<Char>, Char, Duration>(
2147  loc, out, tm, subsecs);
2148  detail::parse_chrono_format(fmt_.begin(), fmt_.end(), w);
2149  return detail::write(
2150  ctx.out(), basic_string_view<Char>(buf.data(), buf.size()), specs);
2151  }
2152 
2153  public:
2154  FMT_CONSTEXPR auto parse(parse_context<Char>& ctx) -> const Char* {
2155  return do_parse(ctx, detail::has_tm_gmtoff<std::tm>::value);
2156  }
2157 
2158  template <typename FormatContext>
2159  auto format(const std::tm& tm, FormatContext& ctx) const
2160  -> decltype(ctx.out()) {
2161  return do_format<std::chrono::seconds>(tm, ctx, nullptr);
2162  }
2163 };
2164 
2165 // DEPRECATED! Reversed order of template parameters.
2166 template <typename Char, typename Duration>
2167 struct formatter<sys_time<Duration>, Char> : private formatter<std::tm, Char> {
2168  FMT_CONSTEXPR auto parse(parse_context<Char>& ctx) -> const Char* {
2169  return this->do_parse(ctx, true);
2170  }
2171 
2172  template <typename FormatContext>
2173  auto format(sys_time<Duration> val, FormatContext& ctx) const
2174  -> decltype(ctx.out()) {
2175  std::tm tm = gmtime(val);
2176  using period = typename Duration::period;
2177  if (detail::const_check(
2178  period::num == 1 && period::den == 1 &&
2179  !std::is_floating_point<typename Duration::rep>::value)) {
2180  detail::set_tm_zone(tm, detail::utc());
2181  return formatter<std::tm, Char>::format(tm, ctx);
2182  }
2183  Duration epoch = val.time_since_epoch();
2184  Duration subsecs = detail::duration_cast<Duration>(
2185  epoch - detail::duration_cast<std::chrono::seconds>(epoch));
2186  if (subsecs.count() < 0) {
2187  auto second = detail::duration_cast<Duration>(std::chrono::seconds(1));
2188  if (tm.tm_sec != 0) {
2189  --tm.tm_sec;
2190  } else {
2191  tm = gmtime(val - second);
2192  detail::set_tm_zone(tm, detail::utc());
2193  }
2194  subsecs += second;
2195  }
2196  return formatter<std::tm, Char>::do_format(tm, ctx, &subsecs);
2197  }
2198 };
2199 
2200 template <typename Duration, typename Char>
2201 struct formatter<utc_time<Duration>, Char>
2202  : formatter<sys_time<Duration>, Char> {
2203  template <typename FormatContext>
2204  auto format(utc_time<Duration> val, FormatContext& ctx) const
2205  -> decltype(ctx.out()) {
2206  return formatter<sys_time<Duration>, Char>::format(
2207  detail::utc_clock::to_sys(val), ctx);
2208  }
2209 };
2210 
2211 template <typename Duration, typename Char>
2212 struct formatter<local_time<Duration>, Char>
2213  : private formatter<std::tm, Char> {
2214  FMT_CONSTEXPR auto parse(parse_context<Char>& ctx) -> const Char* {
2215  return this->do_parse(ctx, false);
2216  }
2217 
2218  template <typename FormatContext>
2219  auto format(local_time<Duration> val, FormatContext& ctx) const
2220  -> decltype(ctx.out()) {
2221  auto time_since_epoch = val.time_since_epoch();
2222  auto seconds_since_epoch =
2223  detail::duration_cast<std::chrono::seconds>(time_since_epoch);
2224  // Use gmtime to prevent time zone conversion since local_time has an
2225  // unspecified time zone.
2226  std::tm t = gmtime(seconds_since_epoch.count());
2227  using period = typename Duration::period;
2228  if (period::num == 1 && period::den == 1 &&
2229  !std::is_floating_point<typename Duration::rep>::value) {
2230  return formatter<std::tm, Char>::format(t, ctx);
2231  }
2232  auto subsecs =
2233  detail::duration_cast<Duration>(time_since_epoch - seconds_since_epoch);
2234  return formatter<std::tm, Char>::do_format(t, ctx, &subsecs);
2235  }
2236 };
2237 
2238 FMT_END_EXPORT
2239 FMT_END_NAMESPACE
2240 
2241 #endif // FMT_CHRONO_H_
A dynamically growing memory buffer for trivially copyable/constructible types with the first SIZE el...
Definition: format.h:785
Parsing context consisting of a format string range being parsed and an argument counter for automati...
Definition: base.h:623
STL namespace.
constexpr auto data() const noexcept -> const Char *
Returns a pointer to the string data.
Definition: base.h:557
constexpr auto size() const noexcept -> size_t
Returns the string size.
Definition: base.h:560
Converts a string literal into a format string that will be parsed at compile time and converted into...
Definition: args.h:20
Definition: chrono.h:39
constexpr auto size() const noexcept -> size_t
Returns the size of this buffer.
Definition: base.h:1805
int round(const float x)
Implements rounding of floating point numbers.
Definition: round.inl:59
FMT_CONSTEXPR auto data() noexcept -> T *
Returns a pointer to the buffer data (not null-terminated).
Definition: base.h:1811