20# include <type_traits>
28#ifndef FMT_USE_LOCAL_TIME
29# ifdef __cpp_lib_chrono
30# define FMT_USE_LOCAL_TIME (__cpp_lib_chrono >= 201907L)
32# define FMT_USE_LOCAL_TIME 0
37#ifndef FMT_USE_UTC_TIME
38# ifdef __cpp_lib_chrono
39# define FMT_USE_UTC_TIME (__cpp_lib_chrono >= 201907L)
41# define FMT_USE_UTC_TIME 0
48# if FMT_HAS_INCLUDE("winapifamily.h")
49# include <winapifamily.h>
51# if defined(_WIN32) && (!defined(WINAPI_FAMILY) || \
52 (WINAPI_FAMILY == WINAPI_FAMILY_DESKTOP_APP))
53# define FMT_USE_TZSET 1
55# define FMT_USE_TZSET 0
60#ifndef FMT_SAFE_DURATION_CAST
61# define FMT_SAFE_DURATION_CAST 1
63#if FMT_SAFE_DURATION_CAST
71namespace safe_duration_cast {
73template <
typename To,
typename From,
74 FMT_ENABLE_IF(!std::is_same<From, To>::value &&
75 std::numeric_limits<From>::is_signed ==
76 std::numeric_limits<To>::is_signed)>
77FMT_CONSTEXPR
auto lossless_integral_conversion(
const From from,
int& ec)
80 using F = std::numeric_limits<From>;
81 using T = std::numeric_limits<To>;
82 static_assert(F::is_integer,
"From must be integral");
83 static_assert(T::is_integer,
"To must be integral");
86 if (detail::const_check(F::digits <= T::digits)) {
90 if (from < (T::min)() || from > (T::max)()) {
96 return static_cast<To
>(from);
103template <
typename To,
typename From,
104 FMT_ENABLE_IF(!std::is_same<From, To>::value &&
105 std::numeric_limits<From>::is_signed !=
106 std::numeric_limits<To>::is_signed)>
107FMT_CONSTEXPR
auto lossless_integral_conversion(
const From from,
int& ec)
110 using F = std::numeric_limits<From>;
111 using T = std::numeric_limits<To>;
112 static_assert(F::is_integer,
"From must be integral");
113 static_assert(T::is_integer,
"To must be integral");
115 if (detail::const_check(F::is_signed && !T::is_signed)) {
117 if (fmt::detail::is_negative(from)) {
122 if (detail::const_check(F::digits > T::digits) &&
123 from >
static_cast<From
>(detail::max_value<To>())) {
129 if (detail::const_check(!F::is_signed && T::is_signed &&
130 F::digits >= T::digits) &&
131 from >
static_cast<From
>(detail::max_value<To>())) {
135 return static_cast<To
>(from);
138template <
typename To,
typename From,
139 FMT_ENABLE_IF(std::is_same<From, To>::value)>
140FMT_CONSTEXPR
auto lossless_integral_conversion(
const From from,
int& ec)
160template <
typename To,
typename From,
161 FMT_ENABLE_IF(!std::is_same<From, To>::value)>
162FMT_CONSTEXPR
auto safe_float_conversion(
const From from,
int& ec) -> To {
164 using T = std::numeric_limits<To>;
165 static_assert(std::is_floating_point<From>::value,
"From must be floating");
166 static_assert(std::is_floating_point<To>::value,
"To must be floating");
169 if (std::isfinite(from)) {
170 if (from >= T::lowest() && from <= (T::max)()) {
171 return static_cast<To
>(from);
179 return static_cast<To
>(from);
182template <
typename To,
typename From,
183 FMT_ENABLE_IF(std::is_same<From, To>::value)>
184FMT_CONSTEXPR
auto safe_float_conversion(
const From from,
int& ec) -> To {
186 static_assert(std::is_floating_point<From>::value,
"From must be floating");
193template <
typename To,
typename FromRep,
typename FromPeriod,
194 FMT_ENABLE_IF(std::is_integral<FromRep>::value),
195 FMT_ENABLE_IF(std::is_integral<typename To::rep>::value)>
196auto safe_duration_cast(std::chrono::duration<FromRep, FromPeriod> from,
198 using From = std::chrono::duration<FromRep, FromPeriod>;
203 : std::ratio_divide<typename From::period, typename To::period> {};
205 static_assert(Factor::num > 0,
"num must be positive");
206 static_assert(Factor::den > 0,
"den must be positive");
212 using IntermediateRep =
213 typename std::common_type<
typename From::rep,
typename To::rep,
214 decltype(Factor::num)>::type;
217 IntermediateRep count =
218 lossless_integral_conversion<IntermediateRep>(from.count(), ec);
221 if (detail::const_check(Factor::num != 1)) {
222 const auto max1 = detail::max_value<IntermediateRep>() / Factor::num;
228 (std::numeric_limits<IntermediateRep>::min)() / Factor::num;
229 if (detail::const_check(!std::is_unsigned<IntermediateRep>::value) &&
234 count *= Factor::num;
237 if (detail::const_check(Factor::den != 1)) count /= Factor::den;
238 auto tocount = lossless_integral_conversion<typename To::rep>(count, ec);
239 return ec ? To() : To(tocount);
245template <
typename To,
typename FromRep,
typename FromPeriod,
246 FMT_ENABLE_IF(std::is_floating_point<FromRep>::value),
247 FMT_ENABLE_IF(std::is_floating_point<typename To::rep>::value)>
248auto safe_duration_cast(std::chrono::duration<FromRep, FromPeriod> from,
250 using From = std::chrono::duration<FromRep, FromPeriod>;
252 if (std::isnan(from.count())) {
254 return To{std::numeric_limits<typename To::rep>::quiet_NaN()};
260 if (std::isinf(from.count())) {
261 return To{from.count()};
267 : std::ratio_divide<typename From::period, typename To::period> {};
269 static_assert(Factor::num > 0,
"num must be positive");
270 static_assert(Factor::den > 0,
"den must be positive");
276 using IntermediateRep =
277 typename std::common_type<
typename From::rep,
typename To::rep,
278 decltype(Factor::num)>::type;
282 IntermediateRep count =
283 safe_float_conversion<IntermediateRep>(from.count(), ec);
289 if (detail::const_check(Factor::num != 1)) {
290 constexpr auto max1 = detail::max_value<IntermediateRep>() /
291 static_cast<IntermediateRep
>(Factor::num);
296 constexpr auto min1 = std::numeric_limits<IntermediateRep>::lowest() /
297 static_cast<IntermediateRep
>(Factor::num);
302 count *=
static_cast<IntermediateRep
>(Factor::num);
306 if (detail::const_check(Factor::den != 1)) {
307 using common_t =
typename std::common_type<IntermediateRep, intmax_t>::type;
308 count /=
static_cast<common_t
>(Factor::den);
312 using ToRep =
typename To::rep;
314 const ToRep tocount = safe_float_conversion<ToRep>(count, ec);
328template <
typename T =
void>
struct null {};
329inline auto localtime_r FMT_NOMACRO(...) ->
null<> {
return null<>(); }
330inline auto localtime_s(...) -> null<> {
return null<>(); }
331inline auto gmtime_r(...) -> null<> {
return null<>(); }
332inline auto gmtime_s(...) -> null<> {
return null<>(); }
336template <
typename Streambuf>
class formatbuf :
public Streambuf {
338 using char_type =
typename Streambuf::char_type;
339 using streamsize =
decltype(std::declval<Streambuf>().sputn(
nullptr, 0));
340 using int_type =
typename Streambuf::int_type;
341 using traits_type =
typename Streambuf::traits_type;
355 auto overflow(int_type ch) -> int_type
override {
356 if (!traits_type::eq_int_type(ch, traits_type::eof()))
357 buffer_.push_back(
static_cast<char_type
>(ch));
361 auto xsputn(
const char_type* s, streamsize count) -> streamsize
override {
362 buffer_.
append(s, s + count);
367inline auto get_classic_locale() ->
const std::locale& {
368 static const auto& locale = std::locale::classic();
373 static constexpr const size_t max_size = 32;
374 CodeUnit buf[max_size];
378template <
typename CodeUnit>
380 const std::locale& loc) {
382# pragma clang diagnostic push
383# pragma clang diagnostic ignored "-Wdeprecated"
384 auto& f = std::use_facet<std::codecvt<CodeUnit, char, std::mbstate_t>>(loc);
385# pragma clang diagnostic pop
387 auto& f = std::use_facet<std::codecvt<CodeUnit, char, std::mbstate_t>>(loc);
389 auto mb = std::mbstate_t();
390 const char* from_next =
nullptr;
391 auto result = f.in(mb, in_buf.begin(), in_buf.end(), from_next,
392 std::begin(out.buf), std::end(out.buf), out.end);
393 if (result != std::codecvt_base::ok)
394 FMT_THROW(format_error(
"failed to format time"));
397template <
typename OutputIt>
398auto write_encoded_tm_str(OutputIt out,
string_view in,
const std::locale& loc)
400 if (detail::use_utf8() && loc != get_classic_locale()) {
403#if FMT_MSC_VERSION != 0 || \
404 (defined(__GLIBCXX__) && \
405 (!defined(_GLIBCXX_USE_DUAL_ABI) || _GLIBCXX_USE_DUAL_ABI == 0))
408 using code_unit = wchar_t;
410 using code_unit = char32_t;
413 using unit_t = codecvt_result<code_unit>;
415 write_codecvt(unit, in, loc);
418 to_utf8<code_unit, basic_memory_buffer<char, unit_t::max_size * 4>>();
419 if (!u.convert({unit.buf, to_unsigned(unit.end - unit.buf)}))
420 FMT_THROW(format_error(
"failed to format time"));
421 return copy<char>(u.c_str(), u.c_str() + u.size(), out);
423 return copy<char>(in.data(), in.data() + in.size(), out);
426template <
typename Char,
typename OutputIt,
427 FMT_ENABLE_IF(!std::is_same<Char, char>::value)>
428auto write_tm_str(OutputIt out,
string_view sv,
const std::locale& loc)
430 codecvt_result<Char> unit;
431 write_codecvt(unit, sv, loc);
432 return copy<Char>(unit.buf, unit.end, out);
435template <
typename Char,
typename OutputIt,
436 FMT_ENABLE_IF(std::is_same<Char, char>::value)>
437auto write_tm_str(OutputIt out,
string_view sv,
const std::locale& loc)
439 return write_encoded_tm_str(out, sv, loc);
442template <
typename Char>
443inline void do_write(buffer<Char>& buf,
const std::tm& time,
444 const std::locale& loc,
char format,
char modifier) {
445 auto&& format_buf = formatbuf<std::basic_streambuf<Char>>(buf);
446 auto&& os = std::basic_ostream<Char>(&format_buf);
448 const auto& facet = std::use_facet<std::time_put<Char>>(loc);
449 auto end = facet.put(os, os, Char(
' '), &time, format, modifier);
450 if (end.failed()) FMT_THROW(format_error(
"failed to format time"));
453template <
typename Char,
typename OutputIt,
454 FMT_ENABLE_IF(!std::is_same<Char, char>::value)>
455auto write(OutputIt out,
const std::tm& time,
const std::locale& loc,
456 char format,
char modifier = 0) -> OutputIt {
457 auto&& buf = get_buffer<Char>(out);
458 do_write<Char>(buf, time, loc, format, modifier);
459 return get_iterator(buf, out);
462template <
typename Char,
typename OutputIt,
463 FMT_ENABLE_IF(std::is_same<Char, char>::value)>
464auto write(OutputIt out,
const std::tm& time,
const std::locale& loc,
465 char format,
char modifier = 0) -> OutputIt {
467 do_write<char>(buf, time, loc, format, modifier);
468 return write_encoded_tm_str(out,
string_view(buf.data(), buf.size()), loc);
471template <
typename Rep1,
typename Rep2>
473 :
public std::integral_constant<bool,
474 (std::is_integral<Rep1>::value &&
475 std::is_integral<Rep2>::value) ||
476 (std::is_floating_point<Rep1>::value &&
477 std::is_floating_point<Rep2>::value)> {
481 typename To,
typename FromRep,
typename FromPeriod,
483auto fmt_duration_cast(std::chrono::duration<FromRep, FromPeriod> from) -> To {
484#if FMT_SAFE_DURATION_CAST
488 To to = safe_duration_cast::safe_duration_cast<To>(from, ec);
489 if (ec) FMT_THROW(format_error(
"cannot format duration"));
493 return std::chrono::duration_cast<To>(from);
498 typename To,
typename FromRep,
typename FromPeriod,
499 FMT_ENABLE_IF(!is_same_arithmetic_type<FromRep, typename To::rep>::value)>
500auto fmt_duration_cast(std::chrono::duration<FromRep, FromPeriod> from) -> To {
502 return std::chrono::duration_cast<To>(from);
505template <
typename Duration>
507 std::chrono::time_point<std::chrono::system_clock, Duration> time_point)
512 return fmt_duration_cast<std::chrono::duration<std::time_t>>(
513 time_point.time_since_epoch())
525inline auto localtime(std::time_t time) -> std::tm {
530 dispatcher(std::time_t t) : time_(t) {}
533 using namespace fmt::detail;
534 return handle(localtime_r(&time_, &tm_));
537 auto handle(std::tm* tm) ->
bool {
return tm !=
nullptr; }
540 using namespace fmt::detail;
541 return fallback(localtime_s(&tm_, &time_));
544 auto fallback(
int res) ->
bool {
return res == 0; }
548 using namespace fmt::detail;
549 std::tm* tm = std::localtime(&time_);
551 return tm !=
nullptr;
557 if (!lt.run()) FMT_THROW(format_error(
"time_t value out of range"));
561#if FMT_USE_LOCAL_TIME
562template <
typename Duration>
563inline auto localtime(std::chrono::local_time<Duration> time) -> std::tm {
565 detail::to_time_t(std::chrono::current_zone()->to_sys(time)));
574inline auto gmtime(std::time_t time) -> std::tm {
579 dispatcher(std::time_t t) : time_(t) {}
582 using namespace fmt::detail;
583 return handle(gmtime_r(&time_, &tm_));
586 auto handle(std::tm* tm) ->
bool {
return tm !=
nullptr; }
589 using namespace fmt::detail;
590 return fallback(gmtime_s(&tm_, &time_));
593 auto fallback(
int res) ->
bool {
return res == 0; }
597 std::tm* tm = std::gmtime(&time_);
599 return tm !=
nullptr;
603 auto gt = dispatcher(time);
605 if (!gt.run()) FMT_THROW(format_error(
"time_t value out of range"));
609template <
typename Duration>
611 std::chrono::time_point<std::chrono::system_clock, Duration> time_point)
613 return gmtime(detail::to_time_t(time_point));
621inline void write_digit2_separated(
char* buf,
unsigned a,
unsigned b,
622 unsigned c,
char sep) {
623 unsigned long long digits =
624 a | (b << 24) | (static_cast<unsigned long long>(c) << 48);
634 digits += (((digits * 205) >> 11) & 0x000f00000f00000f) * 6;
636 digits = ((digits & 0x00f00000f00000f0) >> 4) |
637 ((digits & 0x000f00000f00000f) << 8);
638 auto usep =
static_cast<unsigned long long>(sep);
640 digits |= 0x3030003030003030 | (usep << 16) | (usep << 40);
642 constexpr const size_t len = 8;
643 if (const_check(is_big_endian())) {
645 std::memcpy(tmp, &digits, len);
646 std::reverse_copy(tmp, tmp + len, buf);
648 std::memcpy(buf, &digits, len);
652template <
typename Period>
653FMT_CONSTEXPR
inline auto get_units() ->
const char* {
654 if (std::is_same<Period, std::atto>::value)
return "as";
655 if (std::is_same<Period, std::femto>::value)
return "fs";
656 if (std::is_same<Period, std::pico>::value)
return "ps";
657 if (std::is_same<Period, std::nano>::value)
return "ns";
658 if (std::is_same<Period, std::micro>::value)
return "µs";
659 if (std::is_same<Period, std::milli>::value)
return "ms";
660 if (std::is_same<Period, std::centi>::value)
return "cs";
661 if (std::is_same<Period, std::deci>::value)
return "ds";
662 if (std::is_same<Period, std::ratio<1>>::value)
return "s";
663 if (std::is_same<Period, std::deca>::value)
return "das";
664 if (std::is_same<Period, std::hecto>::value)
return "hs";
665 if (std::is_same<Period, std::kilo>::value)
return "ks";
666 if (std::is_same<Period, std::mega>::value)
return "Ms";
667 if (std::is_same<Period, std::giga>::value)
return "Gs";
668 if (std::is_same<Period, std::tera>::value)
return "Ts";
669 if (std::is_same<Period, std::peta>::value)
return "Ps";
670 if (std::is_same<Period, std::exa>::value)
return "Es";
671 if (std::is_same<Period, std::ratio<60>>::value)
return "min";
672 if (std::is_same<Period, std::ratio<3600>>::value)
return "h";
673 if (std::is_same<Period, std::ratio<86400>>::value)
return "d";
677enum class numeric_system {
695template <
typename OutputIt>
696auto write_padding(OutputIt out, pad_type pad,
int width) -> OutputIt {
697 if (pad == pad_type::none)
return out;
698 return detail::fill_n(out, width, pad == pad_type::space ?
' ' :
'0');
701template <
typename OutputIt>
702auto write_padding(OutputIt out, pad_type pad) -> OutputIt {
703 if (pad != pad_type::none) *out++ = pad == pad_type::space ?
' ' :
'0';
708template <
typename Char,
typename Handler>
709FMT_CONSTEXPR
auto parse_chrono_format(
const Char* begin,
const Char* end,
710 Handler&& handler) ->
const Char* {
711 if (begin == end || *begin ==
'}')
return begin;
712 if (*begin !=
'%') FMT_THROW(format_error(
"invalid format"));
715 pad_type pad = pad_type::unspecified;
722 if (begin != ptr) handler.on_text(begin, ptr);
724 if (ptr == end) FMT_THROW(format_error(
"invalid format"));
728 pad = pad_type::space;
732 pad = pad_type::none;
736 pad = pad_type::zero;
740 if (ptr == end) FMT_THROW(format_error(
"invalid format"));
744 handler.on_text(ptr - 1, ptr);
747 const Char newline[] = {
'\n'};
748 handler.on_text(newline, newline + 1);
752 const Char tab[] = {
'\t'};
753 handler.on_text(tab, tab + 1);
758 handler.on_year(numeric_system::standard);
761 handler.on_short_year(numeric_system::standard);
764 handler.on_century(numeric_system::standard);
767 handler.on_iso_week_based_year();
770 handler.on_iso_week_based_short_year();
774 handler.on_abbr_weekday();
777 handler.on_full_weekday();
780 handler.on_dec0_weekday(numeric_system::standard);
783 handler.on_dec1_weekday(numeric_system::standard);
788 handler.on_abbr_month();
791 handler.on_full_month();
794 handler.on_dec_month(numeric_system::standard);
798 handler.on_dec0_week_of_year(numeric_system::standard);
801 handler.on_dec1_week_of_year(numeric_system::standard);
804 handler.on_iso_week_of_year(numeric_system::standard);
807 handler.on_day_of_year();
810 handler.on_day_of_month(numeric_system::standard);
813 handler.on_day_of_month_space(numeric_system::standard);
817 handler.on_24_hour(numeric_system::standard, pad);
820 handler.on_12_hour(numeric_system::standard, pad);
823 handler.on_minute(numeric_system::standard, pad);
826 handler.on_second(numeric_system::standard, pad);
830 handler.on_datetime(numeric_system::standard);
833 handler.on_loc_date(numeric_system::standard);
836 handler.on_loc_time(numeric_system::standard);
839 handler.on_us_date();
842 handler.on_iso_date();
845 handler.on_12_hour_time();
848 handler.on_24_hour_time();
851 handler.on_iso_time();
857 handler.on_duration_value();
860 handler.on_duration_unit();
863 handler.on_utc_offset(numeric_system::standard);
866 handler.on_tz_name();
870 if (ptr == end) FMT_THROW(format_error(
"invalid format"));
874 handler.on_year(numeric_system::alternative);
877 handler.on_offset_year();
880 handler.on_century(numeric_system::alternative);
883 handler.on_datetime(numeric_system::alternative);
886 handler.on_loc_date(numeric_system::alternative);
889 handler.on_loc_time(numeric_system::alternative);
892 handler.on_utc_offset(numeric_system::alternative);
895 FMT_THROW(format_error(
"invalid format"));
900 if (ptr == end) FMT_THROW(format_error(
"invalid format"));
904 handler.on_short_year(numeric_system::alternative);
907 handler.on_dec_month(numeric_system::alternative);
910 handler.on_dec0_week_of_year(numeric_system::alternative);
913 handler.on_dec1_week_of_year(numeric_system::alternative);
916 handler.on_iso_week_of_year(numeric_system::alternative);
919 handler.on_day_of_month(numeric_system::alternative);
922 handler.on_day_of_month_space(numeric_system::alternative);
925 handler.on_dec0_weekday(numeric_system::alternative);
928 handler.on_dec1_weekday(numeric_system::alternative);
931 handler.on_24_hour(numeric_system::alternative, pad);
934 handler.on_12_hour(numeric_system::alternative, pad);
937 handler.on_minute(numeric_system::alternative, pad);
940 handler.on_second(numeric_system::alternative, pad);
943 handler.on_utc_offset(numeric_system::alternative);
946 FMT_THROW(format_error(
"invalid format"));
950 FMT_THROW(format_error(
"invalid format"));
954 if (begin != ptr) handler.on_text(begin, ptr);
959 FMT_CONSTEXPR
void unsupported() {
960 static_cast<Derived*
>(
this)->unsupported();
962 FMT_CONSTEXPR
void on_year(numeric_system) { unsupported(); }
963 FMT_CONSTEXPR
void on_short_year(numeric_system) { unsupported(); }
964 FMT_CONSTEXPR
void on_offset_year() { unsupported(); }
965 FMT_CONSTEXPR
void on_century(numeric_system) { unsupported(); }
966 FMT_CONSTEXPR
void on_iso_week_based_year() { unsupported(); }
967 FMT_CONSTEXPR
void on_iso_week_based_short_year() { unsupported(); }
968 FMT_CONSTEXPR
void on_abbr_weekday() { unsupported(); }
969 FMT_CONSTEXPR
void on_full_weekday() { unsupported(); }
970 FMT_CONSTEXPR
void on_dec0_weekday(numeric_system) { unsupported(); }
971 FMT_CONSTEXPR
void on_dec1_weekday(numeric_system) { unsupported(); }
972 FMT_CONSTEXPR
void on_abbr_month() { unsupported(); }
973 FMT_CONSTEXPR
void on_full_month() { unsupported(); }
974 FMT_CONSTEXPR
void on_dec_month(numeric_system) { unsupported(); }
975 FMT_CONSTEXPR
void on_dec0_week_of_year(numeric_system) { unsupported(); }
976 FMT_CONSTEXPR
void on_dec1_week_of_year(numeric_system) { unsupported(); }
977 FMT_CONSTEXPR
void on_iso_week_of_year(numeric_system) { unsupported(); }
978 FMT_CONSTEXPR
void on_day_of_year() { unsupported(); }
979 FMT_CONSTEXPR
void on_day_of_month(numeric_system) { unsupported(); }
980 FMT_CONSTEXPR
void on_day_of_month_space(numeric_system) { unsupported(); }
981 FMT_CONSTEXPR
void on_24_hour(numeric_system) { unsupported(); }
982 FMT_CONSTEXPR
void on_12_hour(numeric_system) { unsupported(); }
983 FMT_CONSTEXPR
void on_minute(numeric_system) { unsupported(); }
984 FMT_CONSTEXPR
void on_second(numeric_system) { unsupported(); }
985 FMT_CONSTEXPR
void on_datetime(numeric_system) { unsupported(); }
986 FMT_CONSTEXPR
void on_loc_date(numeric_system) { unsupported(); }
987 FMT_CONSTEXPR
void on_loc_time(numeric_system) { unsupported(); }
988 FMT_CONSTEXPR
void on_us_date() { unsupported(); }
989 FMT_CONSTEXPR
void on_iso_date() { unsupported(); }
990 FMT_CONSTEXPR
void on_12_hour_time() { unsupported(); }
991 FMT_CONSTEXPR
void on_24_hour_time() { unsupported(); }
992 FMT_CONSTEXPR
void on_iso_time() { unsupported(); }
993 FMT_CONSTEXPR
void on_am_pm() { unsupported(); }
994 FMT_CONSTEXPR
void on_duration_value() { unsupported(); }
995 FMT_CONSTEXPR
void on_duration_unit() { unsupported(); }
996 FMT_CONSTEXPR
void on_utc_offset(numeric_system) { unsupported(); }
997 FMT_CONSTEXPR
void on_tz_name() { unsupported(); }
1001 FMT_NORETURN
void unsupported() { FMT_THROW(format_error(
"no format")); }
1003 template <
typename Char>
1004 FMT_CONSTEXPR
void on_text(
const Char*,
const Char*) {}
1005 FMT_CONSTEXPR
void on_year(numeric_system) {}
1006 FMT_CONSTEXPR
void on_short_year(numeric_system) {}
1007 FMT_CONSTEXPR
void on_offset_year() {}
1008 FMT_CONSTEXPR
void on_century(numeric_system) {}
1009 FMT_CONSTEXPR
void on_iso_week_based_year() {}
1010 FMT_CONSTEXPR
void on_iso_week_based_short_year() {}
1011 FMT_CONSTEXPR
void on_abbr_weekday() {}
1012 FMT_CONSTEXPR
void on_full_weekday() {}
1013 FMT_CONSTEXPR
void on_dec0_weekday(numeric_system) {}
1014 FMT_CONSTEXPR
void on_dec1_weekday(numeric_system) {}
1015 FMT_CONSTEXPR
void on_abbr_month() {}
1016 FMT_CONSTEXPR
void on_full_month() {}
1017 FMT_CONSTEXPR
void on_dec_month(numeric_system) {}
1018 FMT_CONSTEXPR
void on_dec0_week_of_year(numeric_system) {}
1019 FMT_CONSTEXPR
void on_dec1_week_of_year(numeric_system) {}
1020 FMT_CONSTEXPR
void on_iso_week_of_year(numeric_system) {}
1021 FMT_CONSTEXPR
void on_day_of_year() {}
1022 FMT_CONSTEXPR
void on_day_of_month(numeric_system) {}
1023 FMT_CONSTEXPR
void on_day_of_month_space(numeric_system) {}
1024 FMT_CONSTEXPR
void on_24_hour(numeric_system, pad_type) {}
1025 FMT_CONSTEXPR
void on_12_hour(numeric_system, pad_type) {}
1026 FMT_CONSTEXPR
void on_minute(numeric_system, pad_type) {}
1027 FMT_CONSTEXPR
void on_second(numeric_system, pad_type) {}
1028 FMT_CONSTEXPR
void on_datetime(numeric_system) {}
1029 FMT_CONSTEXPR
void on_loc_date(numeric_system) {}
1030 FMT_CONSTEXPR
void on_loc_time(numeric_system) {}
1031 FMT_CONSTEXPR
void on_us_date() {}
1032 FMT_CONSTEXPR
void on_iso_date() {}
1033 FMT_CONSTEXPR
void on_12_hour_time() {}
1034 FMT_CONSTEXPR
void on_24_hour_time() {}
1035 FMT_CONSTEXPR
void on_iso_time() {}
1036 FMT_CONSTEXPR
void on_am_pm() {}
1037 FMT_CONSTEXPR
void on_utc_offset(numeric_system) {}
1038 FMT_CONSTEXPR
void on_tz_name() {}
1041inline auto tm_wday_full_name(
int wday) ->
const char* {
1042 static constexpr const char* full_name_list[] = {
1043 "Sunday",
"Monday",
"Tuesday",
"Wednesday",
1044 "Thursday",
"Friday",
"Saturday"};
1045 return wday >= 0 && wday <= 6 ? full_name_list[wday] :
"?";
1047inline auto tm_wday_short_name(
int wday) ->
const char* {
1048 static constexpr const char* short_name_list[] = {
"Sun",
"Mon",
"Tue",
"Wed",
1049 "Thu",
"Fri",
"Sat"};
1050 return wday >= 0 && wday <= 6 ? short_name_list[wday] :
"???";
1053inline auto tm_mon_full_name(
int mon) ->
const char* {
1054 static constexpr const char* full_name_list[] = {
1055 "January",
"February",
"March",
"April",
"May",
"June",
1056 "July",
"August",
"September",
"October",
"November",
"December"};
1057 return mon >= 0 && mon <= 11 ? full_name_list[mon] :
"?";
1059inline auto tm_mon_short_name(
int mon) ->
const char* {
1060 static constexpr const char* short_name_list[] = {
1061 "Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
1062 "Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec",
1064 return mon >= 0 && mon <= 11 ? short_name_list[mon] :
"???";
1067template <
typename T,
typename =
void>
1069template <
typename T>
1071 : std::true_type {};
1073template <
typename T,
typename =
void>
1075template <
typename T>
1077 : std::true_type {};
1080inline void tzset_once() {
1081 static bool init = []() ->
bool {
1085 ignore_unused(init);
1090template <
typename T,
typename Int, FMT_ENABLE_IF(std::is_
integral<T>::value)>
1091inline auto to_nonnegative_int(T value, Int upper) -> Int {
1092 if (!std::is_unsigned<Int>::value &&
1093 (value < 0 || to_unsigned(value) > to_unsigned(upper))) {
1094 FMT_THROW(fmt::format_error(
"chrono value is out of range"));
1096 return static_cast<Int
>(value);
1098template <
typename T,
typename Int, FMT_ENABLE_IF(!std::is_
integral<T>::value)>
1099inline auto to_nonnegative_int(T value, Int upper) -> Int {
1100 auto int_value =
static_cast<Int
>(value);
1101 if (int_value < 0 || value >
static_cast<T
>(upper))
1102 FMT_THROW(format_error(
"invalid value"));
1106constexpr auto pow10(std::uint32_t n) ->
long long {
1107 return n == 0 ? 1 : 10 * pow10(n - 1);
1113template <
long long Num,
long long Den,
int N = 0,
1114 bool Enabled = (N < 19) && (Num <= max_value<long long>() / 10)>
1116 static constexpr int value =
1122template <
long long Num,
long long Den,
int N>
1124 static constexpr int value = (Num % Den == 0) ? N : 6;
1129template <
typename Char,
typename OutputIt,
typename Duration>
1130void write_fractional_seconds(OutputIt& out, Duration d,
int precision = -1) {
1131 constexpr auto num_fractional_digits =
1133 Duration::period::den>::value;
1135 using subsecond_precision = std::chrono::duration<
1136 typename std::common_type<
typename Duration::rep,
1137 std::chrono::seconds::rep>::type,
1138 std::ratio<1, detail::pow10(num_fractional_digits)>>;
1140 const auto fractional = d - fmt_duration_cast<std::chrono::seconds>(d);
1141 const auto subseconds =
1142 std::chrono::treat_as_floating_point<
1143 typename subsecond_precision::rep>::value
1144 ? fractional.count()
1145 : fmt_duration_cast<subsecond_precision>(fractional).count();
1146 auto n =
static_cast<uint32_or_64_or_128_t<long long>
>(subseconds);
1147 const int num_digits = detail::count_digits(n);
1149 int leading_zeroes = (std::max)(0, num_fractional_digits - num_digits);
1150 if (precision < 0) {
1151 FMT_ASSERT(!std::is_floating_point<typename Duration::rep>::value,
"");
1152 if (std::ratio_less<
typename subsecond_precision::period,
1153 std::chrono::seconds::period>::value) {
1155 out = detail::fill_n(out, leading_zeroes,
'0');
1156 out = format_decimal<Char>(out, n, num_digits).end;
1158 }
else if (precision > 0) {
1160 leading_zeroes = (std::min)(leading_zeroes, precision);
1161 int remaining = precision - leading_zeroes;
1162 out = detail::fill_n(out, leading_zeroes,
'0');
1163 if (remaining < num_digits) {
1164 int num_truncated_digits = num_digits - remaining;
1165 n /= to_unsigned(detail::pow10(to_unsigned(num_truncated_digits)));
1167 out = format_decimal<Char>(out, n, remaining).end;
1172 out = format_decimal<Char>(out, n, num_digits).end;
1173 remaining -= num_digits;
1175 out = detail::fill_n(out, remaining,
'0');
1182template <
typename Duration>
1183void write_floating_seconds(
memory_buffer& buf, Duration duration,
1184 int num_fractional_digits = -1) {
1185 using rep =
typename Duration::rep;
1186 FMT_ASSERT(std::is_floating_point<rep>::value,
"");
1188 auto val = duration.count();
1190 if (num_fractional_digits < 0) {
1193 using namespace std;
1194 num_fractional_digits =
1195 count_fractional_digits<Duration::period::num,
1196 Duration::period::den>::value;
1197 if (num_fractional_digits < 6 &&
static_cast<rep
>(round(val)) != val)
1198 num_fractional_digits = 6;
1201 fmt::format_to(std::back_inserter(buf), FMT_STRING(
"{:.{}f}"),
1202 std::fmod(val *
static_cast<rep
>(Duration::period::num) /
1203 static_cast<rep
>(Duration::period::den),
1204 static_cast<rep
>(60)),
1205 num_fractional_digits);
1208template <
typename OutputIt,
typename Char,
1209 typename Duration = std::chrono::seconds>
1212 static constexpr int days_per_week = 7;
1214 const std::locale& loc_;
1215 const bool is_classic_;
1217 const Duration* subsecs_;
1220 auto tm_sec()
const noexcept ->
int {
1221 FMT_ASSERT(tm_.tm_sec >= 0 && tm_.tm_sec <= 61,
"");
1224 auto tm_min()
const noexcept ->
int {
1225 FMT_ASSERT(tm_.tm_min >= 0 && tm_.tm_min <= 59,
"");
1228 auto tm_hour()
const noexcept ->
int {
1229 FMT_ASSERT(tm_.tm_hour >= 0 && tm_.tm_hour <= 23,
"");
1232 auto tm_mday()
const noexcept ->
int {
1233 FMT_ASSERT(tm_.tm_mday >= 1 && tm_.tm_mday <= 31,
"");
1236 auto tm_mon()
const noexcept ->
int {
1237 FMT_ASSERT(tm_.tm_mon >= 0 && tm_.tm_mon <= 11,
"");
1240 auto tm_year()
const noexcept ->
long long {
return 1900ll + tm_.tm_year; }
1241 auto tm_wday()
const noexcept ->
int {
1242 FMT_ASSERT(tm_.tm_wday >= 0 && tm_.tm_wday <= 6,
"");
1245 auto tm_yday()
const noexcept ->
int {
1246 FMT_ASSERT(tm_.tm_yday >= 0 && tm_.tm_yday <= 365,
"");
1250 auto tm_hour12()
const noexcept ->
int {
1251 const auto h = tm_hour();
1252 const auto z = h < 12 ? h : h - 12;
1253 return z == 0 ? 12 : z;
1260 auto split_year_lower(
long long year)
const noexcept ->
int {
1261 auto l =
year % 100;
1263 return static_cast<int>(l);
1267 auto iso_year_weeks(
long long curr_year)
const noexcept ->
int {
1268 const auto prev_year = curr_year - 1;
1270 (curr_year + curr_year / 4 - curr_year / 100 + curr_year / 400) %
1273 (prev_year + prev_year / 4 - prev_year / 100 + prev_year / 400) %
1275 return 52 + ((curr_p == 4 || prev_p == 3) ? 1 : 0);
1277 auto iso_week_num(
int tm_yday,
int tm_wday)
const noexcept ->
int {
1278 return (tm_yday + 11 - (tm_wday == 0 ? days_per_week : tm_wday)) /
1281 auto tm_iso_week_year()
const noexcept ->
long long {
1282 const auto year = tm_year();
1283 const auto w = iso_week_num(tm_yday(), tm_wday());
1284 if (w < 1)
return year - 1;
1285 if (w > iso_year_weeks(
year))
return year + 1;
1288 auto tm_iso_week_of_year()
const noexcept ->
int {
1289 const auto year = tm_year();
1290 const auto w = iso_week_num(tm_yday(), tm_wday());
1291 if (w < 1)
return iso_year_weeks(
year - 1);
1292 if (w > iso_year_weeks(
year))
return 1;
1296 void write1(
int value) {
1297 *out_++ =
static_cast<char>(
'0' + to_unsigned(
value) % 10);
1299 void write2(
int value) {
1300 const char* d = digits2(to_unsigned(
value) % 100);
1304 void write2(
int value, pad_type pad) {
1305 unsigned int v = to_unsigned(
value) % 100;
1307 const char* d = digits2(v);
1311 out_ = detail::write_padding(out_, pad);
1312 *out_++ =
static_cast<char>(
'0' + v);
1316 void write_year_extended(
long long year) {
1324 uint32_or_64_or_128_t<long long> n = to_unsigned(
year);
1325 const int num_digits = count_digits(n);
1326 if (width > num_digits)
1327 out_ = detail::fill_n(out_, width - num_digits,
'0');
1328 out_ = format_decimal<Char>(out_, n, num_digits).end;
1330 void write_year(
long long year) {
1332 write2(
static_cast<int>(
year / 100));
1333 write2(
static_cast<int>(
year % 100));
1335 write_year_extended(
year);
1339 void write_utc_offset(
long offset, numeric_system ns) {
1347 write2(
static_cast<int>(offset / 60));
1348 if (ns != numeric_system::standard) *out_++ =
':';
1349 write2(
static_cast<int>(offset % 60));
1351 template <
typename T, FMT_ENABLE_IF(has_member_data_tm_gmtoff<T>::value)>
1352 void format_utc_offset_impl(
const T& tm, numeric_system ns) {
1353 write_utc_offset(tm.tm_gmtoff, ns);
1355 template <
typename T, FMT_ENABLE_IF(!has_member_data_tm_gmtoff<T>::value)>
1356 void format_utc_offset_impl(
const T& tm, numeric_system ns) {
1357#if defined(_WIN32) && defined(_UCRT)
1362 _get_timezone(&offset);
1365 _get_dstbias(&dstbias);
1368 write_utc_offset(-offset, ns);
1370 if (ns == numeric_system::standard)
return format_localized(
'z');
1374 std::time_t gt = std::mktime(>m);
1375 std::tm ltm = gmtime(gt);
1376 std::time_t lt = std::mktime(<m);
1377 long offset = gt - lt;
1378 write_utc_offset(offset, ns);
1382 template <
typename T, FMT_ENABLE_IF(has_member_data_tm_zone<T>::value)>
1383 void format_tz_name_impl(
const T& tm) {
1385 out_ = write_tm_str<Char>(out_, tm.tm_zone, loc_);
1387 format_localized(
'Z');
1389 template <
typename T, FMT_ENABLE_IF(!has_member_data_tm_zone<T>::value)>
1390 void format_tz_name_impl(
const T&) {
1391 format_localized(
'Z');
1394 void format_localized(
char format,
char modifier = 0) {
1395 out_ = write<Char>(out_, tm_, loc_, format, modifier);
1399 tm_writer(
const std::locale& loc, OutputIt out,
const std::tm& tm,
1400 const Duration* subsecs =
nullptr)
1402 is_classic_(loc_ == get_classic_locale()),
1407 auto out()
const -> OutputIt {
return out_; }
1409 FMT_CONSTEXPR
void on_text(
const Char* begin,
const Char* end) {
1410 out_ = copy<Char>(begin, end, out_);
1413 void on_abbr_weekday() {
1415 out_ = write(out_, tm_wday_short_name(tm_wday()));
1417 format_localized(
'a');
1419 void on_full_weekday() {
1421 out_ = write(out_, tm_wday_full_name(tm_wday()));
1423 format_localized(
'A');
1425 void on_dec0_weekday(numeric_system ns) {
1426 if (is_classic_ || ns == numeric_system::standard)
return write1(tm_wday());
1427 format_localized(
'w',
'O');
1429 void on_dec1_weekday(numeric_system ns) {
1430 if (is_classic_ || ns == numeric_system::standard) {
1431 auto wday = tm_wday();
1432 write1(wday == 0 ? days_per_week : wday);
1434 format_localized(
'u',
'O');
1438 void on_abbr_month() {
1440 out_ = write(out_, tm_mon_short_name(tm_mon()));
1442 format_localized(
'b');
1444 void on_full_month() {
1446 out_ = write(out_, tm_mon_full_name(tm_mon()));
1448 format_localized(
'B');
1451 void on_datetime(numeric_system ns) {
1457 on_day_of_month_space(numeric_system::standard);
1461 on_year(numeric_system::standard);
1463 format_localized(
'c', ns == numeric_system::standard ?
'\0' :
'E');
1466 void on_loc_date(numeric_system ns) {
1470 format_localized(
'x', ns == numeric_system::standard ?
'\0' :
'E');
1472 void on_loc_time(numeric_system ns) {
1476 format_localized(
'X', ns == numeric_system::standard ?
'\0' :
'E');
1480 write_digit2_separated(buf, to_unsigned(tm_mon() + 1),
1481 to_unsigned(tm_mday()),
1482 to_unsigned(split_year_lower(tm_year())),
'/');
1483 out_ = copy<Char>(std::begin(buf), std::end(buf), out_);
1485 void on_iso_date() {
1486 auto year = tm_year();
1490 copy2(buf, digits2(
static_cast<size_t>(
year / 100)));
1493 write_year_extended(
year);
1496 write_digit2_separated(buf + 2,
static_cast<unsigned>(
year % 100),
1497 to_unsigned(tm_mon() + 1), to_unsigned(tm_mday()),
1499 out_ = copy<Char>(std::begin(buf) + offset, std::end(buf), out_);
1502 void on_utc_offset(numeric_system ns) { format_utc_offset_impl(tm_, ns); }
1503 void on_tz_name() { format_tz_name_impl(tm_); }
1505 void on_year(numeric_system ns) {
1506 if (is_classic_ || ns == numeric_system::standard)
1507 return write_year(tm_year());
1508 format_localized(
'Y',
'E');
1510 void on_short_year(numeric_system ns) {
1511 if (is_classic_ || ns == numeric_system::standard)
1512 return write2(split_year_lower(tm_year()));
1513 format_localized(
'y',
'O');
1515 void on_offset_year() {
1516 if (is_classic_)
return write2(split_year_lower(tm_year()));
1517 format_localized(
'y',
'E');
1520 void on_century(numeric_system ns) {
1521 if (is_classic_ || ns == numeric_system::standard) {
1522 auto year = tm_year();
1523 auto upper =
year / 100;
1528 }
else if (upper >= 0 && upper < 100) {
1529 write2(
static_cast<int>(upper));
1531 out_ = write<Char>(out_, upper);
1534 format_localized(
'C',
'E');
1538 void on_dec_month(numeric_system ns) {
1539 if (is_classic_ || ns == numeric_system::standard)
1540 return write2(tm_mon() + 1);
1541 format_localized(
'm',
'O');
1544 void on_dec0_week_of_year(numeric_system ns) {
1545 if (is_classic_ || ns == numeric_system::standard)
1546 return write2((tm_yday() + days_per_week - tm_wday()) / days_per_week);
1547 format_localized(
'U',
'O');
1549 void on_dec1_week_of_year(numeric_system ns) {
1550 if (is_classic_ || ns == numeric_system::standard) {
1551 auto wday = tm_wday();
1552 write2((tm_yday() + days_per_week -
1553 (wday == 0 ? (days_per_week - 1) : (wday - 1))) /
1556 format_localized(
'W',
'O');
1559 void on_iso_week_of_year(numeric_system ns) {
1560 if (is_classic_ || ns == numeric_system::standard)
1561 return write2(tm_iso_week_of_year());
1562 format_localized(
'V',
'O');
1565 void on_iso_week_based_year() { write_year(tm_iso_week_year()); }
1566 void on_iso_week_based_short_year() {
1567 write2(split_year_lower(tm_iso_week_year()));
1570 void on_day_of_year() {
1571 auto yday = tm_yday() + 1;
1575 void on_day_of_month(numeric_system ns) {
1576 if (is_classic_ || ns == numeric_system::standard)
return write2(tm_mday());
1577 format_localized(
'd',
'O');
1579 void on_day_of_month_space(numeric_system ns) {
1580 if (is_classic_ || ns == numeric_system::standard) {
1581 auto mday = to_unsigned(tm_mday()) % 100;
1582 const char* d2 = digits2(mday);
1583 *out_++ = mday < 10 ?
' ' : d2[0];
1586 format_localized(
'e',
'O');
1590 void on_24_hour(numeric_system ns, pad_type pad) {
1591 if (is_classic_ || ns == numeric_system::standard)
1592 return write2(tm_hour(), pad);
1593 format_localized(
'H',
'O');
1595 void on_12_hour(numeric_system ns, pad_type pad) {
1596 if (is_classic_ || ns == numeric_system::standard)
1597 return write2(tm_hour12(), pad);
1598 format_localized(
'I',
'O');
1600 void on_minute(numeric_system ns, pad_type pad) {
1601 if (is_classic_ || ns == numeric_system::standard)
1602 return write2(tm_min(), pad);
1603 format_localized(
'M',
'O');
1606 void on_second(numeric_system ns, pad_type pad) {
1607 if (is_classic_ || ns == numeric_system::standard) {
1608 write2(tm_sec(), pad);
1610 if (std::is_floating_point<typename Duration::rep>::value) {
1612 write_floating_seconds(buf, *subsecs_);
1613 if (buf.
size() > 1) {
1615 out_ = std::copy(buf.begin() + 1, buf.end(), out_);
1618 write_fractional_seconds<Char>(out_, *subsecs_);
1623 format_localized(
'S',
'O');
1627 void on_12_hour_time() {
1630 write_digit2_separated(buf, to_unsigned(tm_hour12()),
1631 to_unsigned(tm_min()), to_unsigned(tm_sec()),
':');
1632 out_ = copy<Char>(std::begin(buf), std::end(buf), out_);
1636 format_localized(
'r');
1639 void on_24_hour_time() {
1644 void on_iso_time() {
1647 on_second(numeric_system::standard, pad_type::unspecified);
1652 *out_++ = tm_hour() < 12 ?
'A' :
'P';
1655 format_localized(
'p');
1660 void on_duration_value() {}
1661 void on_duration_unit() {}
1665 bool has_precision_integral =
false;
1667 FMT_NORETURN
void unsupported() { FMT_THROW(format_error(
"no date")); }
1669 template <
typename Char>
1670 FMT_CONSTEXPR
void on_text(
const Char*,
const Char*) {}
1671 FMT_CONSTEXPR
void on_day_of_year() {}
1672 FMT_CONSTEXPR
void on_24_hour(numeric_system, pad_type) {}
1673 FMT_CONSTEXPR
void on_12_hour(numeric_system, pad_type) {}
1674 FMT_CONSTEXPR
void on_minute(numeric_system, pad_type) {}
1675 FMT_CONSTEXPR
void on_second(numeric_system, pad_type) {}
1676 FMT_CONSTEXPR
void on_12_hour_time() {}
1677 FMT_CONSTEXPR
void on_24_hour_time() {}
1678 FMT_CONSTEXPR
void on_iso_time() {}
1679 FMT_CONSTEXPR
void on_am_pm() {}
1680 FMT_CONSTEXPR
void on_duration_value()
const {
1681 if (has_precision_integral) {
1682 FMT_THROW(format_error(
"precision not allowed for this argument type"));
1685 FMT_CONSTEXPR
void on_duration_unit() {}
1688template <
typename T,
1690inline auto isfinite(T) ->
bool {
1694template <
typename T, FMT_ENABLE_IF(std::is_
integral<T>::value)>
1695inline auto mod(T x,
int y) -> T {
1696 return x %
static_cast<T
>(y);
1698template <
typename T, FMT_ENABLE_IF(std::is_
floating_po
int<T>::value)>
1699inline auto mod(T x,
int y) -> T {
1700 return std::fmod(x,
static_cast<T
>(y));
1705template <typename T, bool INTEGRAL = std::is_integral<T>::value>
1711 using type =
typename std::make_unsigned<T>::type;
1714template <
typename Rep,
typename Period,
1715 FMT_ENABLE_IF(std::is_integral<Rep>::value)>
1716inline auto get_milliseconds(std::chrono::duration<Rep, Period> d)
1717 -> std::chrono::duration<Rep, std::milli> {
1720#if FMT_SAFE_DURATION_CAST
1721 using CommonSecondsType =
1722 typename std::common_type<
decltype(d), std::chrono::seconds>::type;
1723 const auto d_as_common = fmt_duration_cast<CommonSecondsType>(d);
1724 const auto d_as_whole_seconds =
1725 fmt_duration_cast<std::chrono::seconds>(d_as_common);
1727 const auto diff = d_as_common - d_as_whole_seconds;
1729 fmt_duration_cast<std::chrono::duration<Rep, std::milli>>(diff);
1732 auto s = fmt_duration_cast<std::chrono::seconds>(d);
1733 return fmt_duration_cast<std::chrono::milliseconds>(d - s);
1737template <
typename Char,
typename Rep,
typename OutputIt,
1738 FMT_ENABLE_IF(std::is_integral<Rep>::value)>
1739auto format_duration_value(OutputIt out, Rep val,
int) -> OutputIt {
1740 return write<Char>(out, val);
1743template <
typename Char,
typename Rep,
typename OutputIt,
1744 FMT_ENABLE_IF(std::is_floating_point<Rep>::value)>
1745auto format_duration_value(OutputIt out, Rep val,
int precision) -> OutputIt {
1747 specs.precision = precision;
1749 precision >= 0 ? presentation_type::fixed : presentation_type::general;
1750 return write<Char>(out, val, specs);
1753template <
typename Char,
typename OutputIt>
1754auto copy_unit(
string_view unit, OutputIt out, Char) -> OutputIt {
1755 return std::copy(unit.begin(), unit.end(), out);
1758template <
typename OutputIt>
1759auto copy_unit(
string_view unit, OutputIt out,
wchar_t) -> OutputIt {
1762 utf8_to_utf16 u(unit);
1763 return std::copy(u.c_str(), u.c_str() + u.size(), out);
1766template <
typename Char,
typename Period,
typename OutputIt>
1767auto format_duration_unit(OutputIt out) -> OutputIt {
1768 if (
const char* unit = get_units<Period>())
1771 out = write<Char>(out, Period::num);
1772 if (const_check(Period::den != 1)) {
1774 out = write<Char>(out, Period::den);
1784 std::locale locale_;
1786 bool has_locale_ =
false;
1791 ::new (&locale_) std::locale(loc.template get<std::locale>());
1794 if (has_locale_) locale_.~locale();
1796 operator const std::locale&()
const {
1797 return has_locale_ ? locale_ : get_classic_locale();
1801template <
typename FormatContext,
typename OutputIt,
typename Rep,
1807 bool localized =
false;
1810 conditional_t<std::is_integral<Rep>::value &&
sizeof(Rep) <
sizeof(
int),
1811 unsigned,
typename make_unsigned_or_unchanged<Rep>::type>;
1813 using seconds = std::chrono::duration<rep>;
1815 using milliseconds = std::chrono::duration<rep, std::milli>;
1818 using char_type =
typename FormatContext::char_type;
1822 std::chrono::duration<Rep, Period> d)
1825 val(
static_cast<rep
>(d.count())),
1827 if (d.count() < 0) {
1835 s = fmt_duration_cast<seconds>(std::chrono::duration<rep, Period>(val));
1839 auto handle_nan_inf() ->
bool {
1840 if (isfinite(val)) {
1856 auto days()
const -> Rep {
return static_cast<Rep
>(s.count() / 86400); }
1857 auto hour()
const -> Rep {
1858 return static_cast<Rep
>(mod((s.count() / 3600), 24));
1861 auto hour12()
const -> Rep {
1862 Rep hour =
static_cast<Rep
>(mod((s.count() / 3600), 12));
1863 return hour <= 0 ? 12 : hour;
1866 auto minute()
const -> Rep {
1867 return static_cast<Rep
>(mod((s.count() / 60), 60));
1869 auto second()
const -> Rep {
return static_cast<Rep
>(mod(s.count(), 60)); }
1871 auto time()
const -> std::tm {
1872 auto time = std::tm();
1873 time.tm_hour = to_nonnegative_int(hour(), 24);
1874 time.tm_min = to_nonnegative_int(minute(), 60);
1875 time.tm_sec = to_nonnegative_int(second(), 60);
1886 void write(Rep
value,
int width, pad_type pad = pad_type::unspecified) {
1888 if (isnan(
value))
return write_nan();
1889 uint32_or_64_or_128_t<int> n =
1890 to_unsigned(to_nonnegative_int(
value, max_value<int>()));
1891 int num_digits = detail::count_digits(n);
1892 if (width > num_digits) {
1893 out = detail::write_padding(out, pad, width - num_digits);
1895 out = format_decimal<char_type>(out, n, num_digits).end;
1898 void write_nan() { std::copy_n(
"nan", 3, out); }
1899 void write_pinf() { std::copy_n(
"inf", 3, out); }
1900 void write_ninf() { std::copy_n(
"-inf", 4, out); }
1902 template <
typename Callback,
typename... Args>
1903 void format_tm(
const tm& time,
Callback cb, Args... args) {
1904 if (isnan(val))
return write_nan();
1911 void on_text(
const char_type* begin,
const char_type* end) {
1912 std::copy(begin, end, out);
1916 void on_abbr_weekday() {}
1917 void on_full_weekday() {}
1918 void on_dec0_weekday(numeric_system) {}
1919 void on_dec1_weekday(numeric_system) {}
1920 void on_abbr_month() {}
1921 void on_full_month() {}
1922 void on_datetime(numeric_system) {}
1923 void on_loc_date(numeric_system) {}
1924 void on_loc_time(numeric_system) {}
1925 void on_us_date() {}
1926 void on_iso_date() {}
1927 void on_utc_offset(numeric_system) {}
1928 void on_tz_name() {}
1929 void on_year(numeric_system) {}
1930 void on_short_year(numeric_system) {}
1931 void on_offset_year() {}
1932 void on_century(numeric_system) {}
1933 void on_iso_week_based_year() {}
1934 void on_iso_week_based_short_year() {}
1935 void on_dec_month(numeric_system) {}
1936 void on_dec0_week_of_year(numeric_system) {}
1937 void on_dec1_week_of_year(numeric_system) {}
1938 void on_iso_week_of_year(numeric_system) {}
1939 void on_day_of_month(numeric_system) {}
1940 void on_day_of_month_space(numeric_system) {}
1942 void on_day_of_year() {
1943 if (handle_nan_inf())
return;
1947 void on_24_hour(numeric_system ns, pad_type pad) {
1948 if (handle_nan_inf())
return;
1950 if (ns == numeric_system::standard)
return write(hour(), 2, pad);
1952 time.tm_hour = to_nonnegative_int(hour(), 24);
1953 format_tm(time, &tm_writer_type::on_24_hour, ns, pad);
1956 void on_12_hour(numeric_system ns, pad_type pad) {
1957 if (handle_nan_inf())
return;
1959 if (ns == numeric_system::standard)
return write(hour12(), 2, pad);
1961 time.tm_hour = to_nonnegative_int(hour12(), 12);
1962 format_tm(time, &tm_writer_type::on_12_hour, ns, pad);
1965 void on_minute(numeric_system ns, pad_type pad) {
1966 if (handle_nan_inf())
return;
1968 if (ns == numeric_system::standard)
return write(minute(), 2, pad);
1970 time.tm_min = to_nonnegative_int(minute(), 60);
1971 format_tm(time, &tm_writer_type::on_minute, ns, pad);
1974 void on_second(numeric_system ns, pad_type pad) {
1975 if (handle_nan_inf())
return;
1977 if (ns == numeric_system::standard) {
1978 if (std::is_floating_point<rep>::value) {
1980 write_floating_seconds(buf, std::chrono::duration<rep, Period>(val),
1982 if (negative) *out++ =
'-';
1983 if (buf.
size() < 2 || buf[1] ==
'.') {
1984 out = detail::write_padding(out, pad);
1986 out = std::copy(buf.begin(), buf.end(), out);
1988 write(second(), 2, pad);
1989 write_fractional_seconds<char_type>(
1990 out, std::chrono::duration<rep, Period>(val), precision);
1995 time.tm_sec = to_nonnegative_int(second(), 60);
1996 format_tm(time, &tm_writer_type::on_second, ns, pad);
1999 void on_12_hour_time() {
2000 if (handle_nan_inf())
return;
2001 format_tm(time(), &tm_writer_type::on_12_hour_time);
2004 void on_24_hour_time() {
2005 if (handle_nan_inf()) {
2016 void on_iso_time() {
2019 if (handle_nan_inf())
return;
2020 on_second(numeric_system::standard, pad_type::unspecified);
2024 if (handle_nan_inf())
return;
2025 format_tm(time(), &tm_writer_type::on_am_pm);
2028 void on_duration_value() {
2029 if (handle_nan_inf())
return;
2031 out = format_duration_value<char_type>(out, val, precision);
2034 void on_duration_unit() {
2035 out = format_duration_unit<char_type, Period>(out);
2041#if defined(__cpp_lib_chrono) && __cpp_lib_chrono >= 201907
2042using weekday = std::chrono::weekday;
2043using day = std::chrono::day;
2044using month = std::chrono::month;
2045using year = std::chrono::year;
2051 unsigned char value_;
2055 constexpr explicit weekday(
unsigned wd) noexcept
2056 : value_(
static_cast<unsigned char>(wd != 7 ? wd : 0)) {}
2057 constexpr auto c_encoding()
const noexcept ->
unsigned {
return value_; }
2062 unsigned char value_;
2066 constexpr explicit day(
unsigned d) noexcept
2067 : value_(
static_cast<unsigned char>(d)) {}
2068 constexpr explicit operator unsigned()
const noexcept {
return value_; }
2073 unsigned char value_;
2077 constexpr explicit month(
unsigned m) noexcept
2078 : value_(
static_cast<unsigned char>(m)) {}
2079 constexpr explicit operator unsigned()
const noexcept {
return value_; }
2088 constexpr explicit year(
int y) noexcept : value_(y) {}
2089 constexpr explicit operator int()
const noexcept {
return value_; }
2101 : year_(y), month_(m), day_(d) {}
2102 constexpr auto year()
const noexcept -> fmt::year {
return year_; }
2103 constexpr auto month()
const noexcept -> fmt::month {
return month_; }
2104 constexpr auto day()
const noexcept -> fmt::day {
return day_; }
2108template <
typename Char>
2111 bool localized_ =
false;
2112 bool use_tm_formatter_ =
false;
2116 ->
decltype(ctx.
begin()) {
2117 auto it = ctx.
begin(), end = ctx.
end();
2118 if (it != end && *it ==
'L') {
2123 use_tm_formatter_ = it != end && *it !=
'}';
2127 template <
typename FormatContext>
2128 auto format(
weekday wd, FormatContext& ctx)
const ->
decltype(ctx.out()) {
2129 auto time = std::tm();
2130 time.tm_wday =
static_cast<int>(wd.c_encoding());
2134 w.on_abbr_weekday();
2139template <
typename Char>
2142 bool use_tm_formatter_ =
false;
2146 ->
decltype(ctx.
begin()) {
2147 auto it = ctx.
begin(), end = ctx.
end();
2148 use_tm_formatter_ = it != end && *it !=
'}';
2152 template <
typename FormatContext>
2153 auto format(
day d, FormatContext& ctx)
const ->
decltype(ctx.out()) {
2154 auto time = std::tm();
2155 time.tm_mday =
static_cast<int>(
static_cast<unsigned>(d));
2159 w.on_day_of_month(detail::numeric_system::standard);
2164template <
typename Char>
2167 bool localized_ =
false;
2168 bool use_tm_formatter_ =
false;
2172 ->
decltype(ctx.
begin()) {
2173 auto it = ctx.
begin(), end = ctx.
end();
2174 if (it != end && *it ==
'L') {
2179 use_tm_formatter_ = it != end && *it !=
'}';
2183 template <
typename FormatContext>
2184 auto format(
month m, FormatContext& ctx)
const ->
decltype(ctx.out()) {
2185 auto time = std::tm();
2186 time.tm_mon =
static_cast<int>(
static_cast<unsigned>(m)) - 1;
2195template <
typename Char>
2198 bool use_tm_formatter_ =
false;
2202 ->
decltype(ctx.
begin()) {
2203 auto it = ctx.
begin(), end = ctx.
end();
2204 use_tm_formatter_ = it != end && *it !=
'}';
2208 template <
typename FormatContext>
2209 auto format(
year y, FormatContext& ctx)
const ->
decltype(ctx.out()) {
2210 auto time = std::tm();
2211 time.tm_year =
static_cast<int>(y) - 1900;
2215 w.on_year(detail::numeric_system::standard);
2220template <
typename Char>
2223 bool use_tm_formatter_ =
false;
2227 ->
decltype(ctx.
begin()) {
2228 auto it = ctx.
begin(), end = ctx.
end();
2229 use_tm_formatter_ = it != end && *it !=
'}';
2233 template <
typename FormatContext>
2235 ->
decltype(ctx.out()) {
2236 auto time = std::tm();
2237 time.tm_year =
static_cast<int>(val.year()) - 1900;
2238 time.tm_mon =
static_cast<int>(
static_cast<unsigned>(val.month())) - 1;
2239 time.tm_mday =
static_cast<int>(
static_cast<unsigned>(val.day()));
2248template <
typename Rep,
typename Period,
typename Char>
2254 bool localized_ =
false;
2259 ->
decltype(ctx.
begin()) {
2260 auto it = ctx.
begin(), end = ctx.
end();
2261 if (it == end || *it ==
'}')
return it;
2263 it = detail::parse_align(it, end, specs_);
2264 if (it == end)
return it;
2266 it = detail::parse_dynamic_spec(it, end, specs_.width, width_ref_, ctx);
2267 if (it == end)
return it;
2271 checker.has_precision_integral = !std::is_floating_point<Rep>::value;
2272 it = detail::parse_precision(it, end, specs_.precision, precision_ref_,
2275 if (it != end && *it ==
'L') {
2279 end = detail::parse_chrono_format(it, end, checker);
2280 format_str_ = {it, detail::to_unsigned(end - it)};
2284 template <
typename FormatContext>
2285 auto format(std::chrono::duration<Rep, Period> d, FormatContext& ctx)
const
2286 ->
decltype(ctx.out()) {
2287 auto specs = specs_;
2288 auto precision = specs.precision;
2289 specs.precision = -1;
2290 auto begin = format_str_.begin(), end = format_str_.end();
2294 auto out = std::back_inserter(buf);
2295 detail::handle_dynamic_spec<detail::width_checker>(specs.width, width_ref_,
2297 detail::handle_dynamic_spec<detail::precision_checker>(precision,
2298 precision_ref_, ctx);
2299 if (begin == end || *begin ==
'}') {
2300 out = detail::format_duration_value<Char>(out, d.count(), precision);
2301 detail::format_duration_unit<Char, Period>(out);
2303 using chrono_formatter =
2305 auto f = chrono_formatter(ctx, out, d);
2306 f.precision = precision;
2307 f.localized = localized_;
2308 detail::parse_chrono_format(begin, end, f);
2310 return detail::write(
2315template <
typename Char,
typename Duration>
2316struct formatter<std::chrono::time_point<std::chrono::system_clock, Duration>,
2322 template <
typename FormatContext>
2323 auto format(std::chrono::time_point<std::chrono::system_clock, Duration> val,
2324 FormatContext& ctx)
const ->
decltype(ctx.out()) {
2325 std::tm tm = gmtime(val);
2326 using period =
typename Duration::period;
2327 if (detail::const_check(
2328 period::num == 1 && period::den == 1 &&
2329 !std::is_floating_point<typename Duration::rep>::value)) {
2332 Duration epoch = val.time_since_epoch();
2333 Duration subsecs = detail::fmt_duration_cast<Duration>(
2334 epoch - detail::fmt_duration_cast<std::chrono::seconds>(epoch));
2335 if (subsecs.count() < 0) {
2337 detail::fmt_duration_cast<Duration>(std::chrono::seconds(1));
2341 tm = gmtime(val - second);
2342 subsecs += detail::fmt_duration_cast<Duration>(std::chrono::seconds(1));
2348#if FMT_USE_LOCAL_TIME
2349template <
typename Char,
typename Duration>
2350struct formatter<std::chrono::local_time<Duration>, Char>
2356 template <
typename FormatContext>
2357 auto format(std::chrono::local_time<Duration> val, FormatContext& ctx)
const
2358 ->
decltype(ctx.out()) {
2359 using period =
typename Duration::period;
2360 if (period::num != 1 || period::den != 1 ||
2361 std::is_floating_point<typename Duration::rep>::value) {
2362 const auto epoch = val.time_since_epoch();
2363 const auto subsecs = detail::fmt_duration_cast<Duration>(
2364 epoch - detail::fmt_duration_cast<std::chrono::seconds>(epoch));
2375template <
typename Char,
typename Duration>
2376struct formatter<std::chrono::time_point<std::chrono::utc_clock, Duration>,
2378 :
formatter<std::chrono::time_point<std::chrono::system_clock, Duration>,
2380 template <
typename FormatContext>
2381 auto format(std::chrono::time_point<std::chrono::utc_clock, Duration> val,
2382 FormatContext& ctx)
const ->
decltype(ctx.out()) {
2384 std::chrono::time_point<std::chrono::system_clock, Duration>,
2385 Char>::format(std::chrono::utc_clock::to_sys(val), ctx);
2398 template <
typename FormatContext,
typename Duration>
2399 auto do_format(
const std::tm& tm, FormatContext& ctx,
2400 const Duration* subsecs)
const ->
decltype(ctx.out()) {
2401 auto specs = specs_;
2403 auto out = std::back_inserter(buf);
2404 detail::handle_dynamic_spec<detail::width_checker>(specs.width, width_ref_,
2407 auto loc_ref = ctx.locale();
2411 detail::parse_chrono_format(format_str_.begin(), format_str_.end(), w);
2412 return detail::write(
2418 ->
decltype(ctx.begin()) {
2419 auto it = ctx.begin(), end = ctx.end();
2420 if (it == end || *it ==
'}')
return it;
2422 it = detail::parse_align(it, end, specs_);
2423 if (it == end)
return it;
2425 it = detail::parse_dynamic_spec(it, end, specs_.width, width_ref_, ctx);
2426 if (it == end)
return it;
2430 if (end != it) format_str_ = {it, detail::to_unsigned(end - it)};
2434 template <
typename FormatContext>
2435 auto format(
const std::tm& tm, FormatContext& ctx)
const
2436 ->
decltype(ctx.out()) {
2437 return do_format<FormatContext, std::chrono::seconds>(tm, ctx,
nullptr);
constexpr auto end() const noexcept -> iterator
Definition base.h:753
constexpr auto begin() const noexcept -> iterator
Definition base.h:746
void append(const U *begin, const U *end)
Definition base.h:913
constexpr auto size() const noexcept -> size_t
Definition base.h:880
FMT_CONSTEXPR auto data() noexcept -> T *
Definition base.h:886
GLM_FUNC_DECL GLM_CONSTEXPR genType zero()
Definition constants.inl:6