30 #ifndef _GLIBCXX_CHRONO_IO_H
31 #define _GLIBCXX_CHRONO_IO_H 1
33 #pragma GCC system_header
35 #if __cplusplus >= 202002L
44 namespace std _GLIBCXX_VISIBILITY(default)
46 _GLIBCXX_BEGIN_NAMESPACE_VERSION
61 template<
typename _CharT>
63 _Widen(
const char* __narrow,
const wchar_t* __wide)
65 if constexpr (is_same_v<_CharT, wchar_t>)
70 #define _GLIBCXX_WIDEN_(C, S) ::std::chrono::__detail::_Widen<C>(S, L##S)
71 #define _GLIBCXX_WIDEN(S) _GLIBCXX_WIDEN_(_CharT, S)
73 template<
typename _Period,
typename _CharT>
75 __units_suffix() noexcept
80 #define _GLIBCXX_UNITS_SUFFIX(period, suffix) \
81 if constexpr (is_same_v<_Period, period>) \
82 return _GLIBCXX_WIDEN(suffix); \
85 _GLIBCXX_UNITS_SUFFIX(
atto,
"as")
86 _GLIBCXX_UNITS_SUFFIX(
femto,
"fs")
87 _GLIBCXX_UNITS_SUFFIX(
pico,
"ps")
88 _GLIBCXX_UNITS_SUFFIX(
nano,
"ns")
89 _GLIBCXX_UNITS_SUFFIX(
milli,
"ms")
90 #if _GLIBCXX_USE_ALT_MICROSECONDS_SUFFIX
93 _GLIBCXX_UNITS_SUFFIX(
micro,
"\u00b5s")
95 _GLIBCXX_UNITS_SUFFIX(
micro,
"us")
97 _GLIBCXX_UNITS_SUFFIX(
centi,
"cs")
98 _GLIBCXX_UNITS_SUFFIX(
deci,
"ds")
100 _GLIBCXX_UNITS_SUFFIX(
deca,
"das")
101 _GLIBCXX_UNITS_SUFFIX(
hecto,
"hs")
102 _GLIBCXX_UNITS_SUFFIX(
kilo,
"ks")
103 _GLIBCXX_UNITS_SUFFIX(
mega,
"Ms")
104 _GLIBCXX_UNITS_SUFFIX(
giga,
"Gs")
105 _GLIBCXX_UNITS_SUFFIX(
tera,
"Ts")
106 _GLIBCXX_UNITS_SUFFIX(
tera,
"Ts")
107 _GLIBCXX_UNITS_SUFFIX(
peta,
"Ps")
108 _GLIBCXX_UNITS_SUFFIX(
exa,
"Es")
112 #undef _GLIBCXX_UNITS_SUFFIX
116 template<
typename _Period,
typename _CharT,
typename _Out>
118 __fmt_units_suffix(_Out __out) noexcept
120 if (
auto __s = __detail::__units_suffix<_Period, _CharT>(); __s.size())
121 return __format::__write(
std::move(__out), __s);
122 else if constexpr (_Period::den == 1)
123 return std::format_to(
std::move(__out), _GLIBCXX_WIDEN(
"[{}]s"),
124 (uintmax_t)_Period::num);
126 return std::format_to(
std::move(__out), _GLIBCXX_WIDEN(
"[{}/{}]s"),
127 (uintmax_t)_Period::num,
128 (uintmax_t)_Period::den);
137 template<
typename _CharT,
typename _Traits,
138 typename _Rep,
typename _Period>
144 using period =
typename _Period::type;
146 __s.
flags(__os.flags());
147 __s.
imbue(__os.getloc());
150 __detail::__fmt_units_suffix<period, _CharT>(_Out(__s));
159 template<
typename _Duration>
160 struct __local_time_fmt
163 const string* _M_abbrev;
167 struct __local_fmt_t;
180 template<
typename _Duration>
181 inline __detail::__local_time_fmt<_Duration>
183 const string* __abbrev =
nullptr,
184 const seconds* __offset_sec =
nullptr)
185 {
return {__time, __abbrev, __offset_sec}; }
193 [[noreturn,__gnu__::__always_inline__]]
195 __no_timezone_available()
196 { __throw_format_error(
"format error: no timezone available for %Z or %z"); }
198 [[noreturn,__gnu__::__always_inline__]]
200 __not_valid_for_duration()
201 { __throw_format_error(
"format error: chrono-format-spec not valid for "
202 "chrono::duration"); }
204 [[noreturn,__gnu__::__always_inline__]]
206 __invalid_chrono_spec()
207 { __throw_format_error(
"format error: chrono-format-spec not valid for "
210 template<
typename _CharT>
211 struct _ChronoSpec : _Spec<_CharT>
213 basic_string_view<_CharT> _M_chrono_specs;
220 _Year = 1, _Month = 2, _Day = 4, _Weekday = 8, _TimeOfDay = 16,
222 _Date = _Year | _Month | _Day | _Weekday,
223 _DateTime = _Date | _TimeOfDay,
224 _ZonedDateTime = _DateTime | _TimeZone,
228 constexpr _ChronoParts
229 operator|(_ChronoParts __x, _ChronoParts __y) noexcept
230 {
return static_cast<_ChronoParts
>((int)__x | (
int)__y); }
232 constexpr _ChronoParts&
233 operator|=(_ChronoParts& __x, _ChronoParts __y) noexcept
234 {
return __x = __x | __y; }
237 template<
typename _CharT>
238 struct __formatter_chrono
240 using __string_view = basic_string_view<_CharT>;
241 using __string = basic_string<_CharT>;
243 template<
typename _ParseContext>
244 constexpr
typename _ParseContext::iterator
245 _M_parse(_ParseContext& __pc, _ChronoParts __parts)
247 auto __first = __pc.
begin();
248 auto __last = __pc.end();
250 _ChronoSpec<_CharT> __spec{};
252 auto __finalize = [
this, &__spec] {
256 auto __finished = [&] {
257 if (__first == __last || *__first ==
'}')
268 __first = __spec._M_parse_fill_and_align(__first, __last);
272 __first = __spec._M_parse_width(__first, __last, __pc);
276 if (__parts & _ChronoParts::_Duration)
278 __first = __spec._M_parse_precision(__first, __last, __pc);
283 __first = __spec._M_parse_locale(__first, __last);
290 __string_view __str(__first, __last - __first);
291 auto __end = __str.find(
'}');
292 if (__end != __str.npos)
294 __str.remove_suffix(__str.length() - __end);
295 __last = __first + __end;
297 if (__str.find(
'{') != __str.npos)
298 __throw_format_error(
"chrono format error: '{' in chrono-specs");
305 const auto __chrono_specs = __first++;
306 if (*__chrono_specs !=
'%')
307 __throw_format_error(
"chrono format error: no '%' at start of "
314 while (__first != __last)
316 enum _Mods { _Mod_none, _Mod_E, _Mod_O, _Mod_E_O };
317 _Mods __allowed_mods = _Mod_none;
319 _CharT __c = *__first++;
332 __needed = _DateTime;
333 __allowed_mods = _Mod_E;
337 __allowed_mods = _Mod_E;
342 __allowed_mods = _Mod_O;
354 __needed = _TimeOfDay;
355 __allowed_mods = _Mod_O;
358 if (!(__parts & _Duration))
363 __allowed_mods = _Mod_O;
366 __needed = _TimeOfDay;
367 __allowed_mods = _Mod_O;
373 __needed = _TimeOfDay;
377 __needed = _Duration;
380 __needed = _TimeOfDay;
381 __allowed_mods = _Mod_O;
386 __allowed_mods = _Mod_O;
392 __allowed_mods = _Mod_O;
396 __allowed_mods = _Mod_E;
399 __needed = _TimeOfDay;
400 __allowed_mods = _Mod_E;
404 __allowed_mods = _Mod_E_O;
408 __allowed_mods = _Mod_E;
411 __needed = _TimeZone;
412 __allowed_mods = _Mod_E_O;
415 __needed = _TimeZone;
423 if (__mod) [[unlikely]]
425 __allowed_mods = _Mod_none;
431 __throw_format_error(
"chrono format error: invalid "
432 " specifier in chrono-specs");
435 if ((__mod ==
'E' && !(__allowed_mods & _Mod_E))
436 || (__mod ==
'O' && !(__allowed_mods & _Mod_O)))
437 __throw_format_error(
"chrono format error: invalid "
438 " modifier in chrono-specs");
441 if ((__parts & __needed) != __needed)
442 __throw_format_error(
"chrono format error: format argument "
443 "does not contain the information "
444 "required by the chrono-specs");
447 size_t __pos = __string_view(__first, __last - __first).find(
'%');
452 if (__pos == __string_view::npos)
458 __first += __pos + 1;
463 if (__conv || __mod != _CharT())
464 __throw_format_error(
"chrono format error: unescaped '%' in "
468 _M_spec._M_chrono_specs
469 = __string_view(__chrono_specs, __first - __chrono_specs);
479 template<
typename _Tp,
typename _FormatContext>
480 typename _FormatContext::iterator
481 _M_format(
const _Tp& __t, _FormatContext& __fc,
482 bool __is_neg =
false)
const
484 auto __first = _M_spec._M_chrono_specs.begin();
485 const auto __last = _M_spec._M_chrono_specs.end();
486 if (__first == __last)
487 return _M_format_to_ostream(__t, __fc, __is_neg);
489 _Sink_iter<_CharT> __out;
490 __format::_Str_sink<_CharT> __sink;
491 bool __write_direct =
false;
492 if constexpr (is_same_v<
typename _FormatContext::iterator,
495 if (_M_spec._M_width_kind == __format::_WP_none)
498 __write_direct =
true;
501 __out = __sink.out();
504 __out = __sink.out();
508 if constexpr (__is_specialization_of<_Tp, chrono::hh_mm_ss>)
509 __is_neg = __t.is_negative();
511 auto __print_sign = [&__is_neg, &__out] {
512 if constexpr (chrono::__is_duration_v<_Tp>
513 || __is_specialization_of<_Tp, chrono::hh_mm_ss>)
516 *__out++ = _S_plus_minus[1];
523 constexpr
const _CharT* __literals = _GLIBCXX_WIDEN(
"\n\t%");
530 _CharT __c = *__first++;
535 __out = _M_a_A(__t,
std::move(__out), __fc, __c ==
'A');
540 __out = _M_b_B(__t,
std::move(__out), __fc, __c ==
'B');
543 __out = _M_c(__t,
std::move(__out), __fc, __mod ==
'E');
548 __out = _M_C_y_Y(__t,
std::move(__out), __fc, __c, __mod);
552 __out = _M_d_e(__t,
std::move(__out), __fc, __c, __mod ==
'O');
555 __out = _M_D(__t,
std::move(__out), __fc);
558 __out = _M_F(__t,
std::move(__out), __fc);
562 __out = _M_g_G(__t,
std::move(__out), __fc, __c ==
'G');
566 __out = _M_H_I(__t, __print_sign(), __fc, __c, __mod ==
'O');
569 __out = _M_j(__t, __print_sign(), __fc);
572 __out = _M_m(__t,
std::move(__out), __fc, __mod ==
'O');
575 __out = _M_M(__t, __print_sign(), __fc, __mod ==
'O');
578 __out = _M_p(__t,
std::move(__out), __fc);
581 __out = _M_q(__t,
std::move(__out), __fc);
585 if constexpr (chrono::__is_duration_v<_Tp>)
586 __out = std::format_to(__print_sign(), _S_empty_spec,
589 __throw_format_error(
"chrono format error: argument is "
593 __out = _M_r(__t, __print_sign(), __fc);
597 __out = _M_R_T(__t, __print_sign(), __fc, __c ==
'T');
600 __out = _M_S(__t, __print_sign(), __fc, __mod ==
'O');
604 __out = _M_u_w(__t,
std::move(__out), __fc, __c, __mod ==
'O');
609 __out = _M_U_V_W(__t,
std::move(__out), __fc, __c,
613 __out = _M_x(__t,
std::move(__out), __fc, __mod ==
'E');
616 __out = _M_X(__t, __print_sign(), __fc, __mod ==
'E');
619 __out = _M_z(__t,
std::move(__out), __fc, (
bool)__mod);
622 __out = _M_Z(__t,
std::move(__out), __fc);
625 *__out++ = __literals[0];
628 *__out++ = __literals[1];
631 *__out++ = __literals[2];
643 __string_view __str(__first, __last - __first);
644 size_t __pos = __str.find(
'%');
649 if (__pos == __str.npos)
653 __str.remove_suffix(__str.length() - __pos);
654 __first += __pos + 1;
656 __out = __format::__write(
std::move(__out), __str);
659 while (__first != __last);
661 if constexpr (is_same_v<
typename _FormatContext::iterator,
666 auto __str =
std::
move(__sink).get();
667 return __format::__write_padded_as_spec(__str, __str.
size(),
671 _ChronoSpec<_CharT> _M_spec;
675 template<typename _FormatContext>
677 _M_locale(_FormatContext& __fc)
const
679 if (!_M_spec._M_localized)
682 return __fc.locale();
690 template<
typename _Tp,
typename _FormatContext>
691 typename _FormatContext::iterator
692 _M_format_to_ostream(
const _Tp& __t, _FormatContext& __fc,
695 using ::std::chrono::__detail::__utc_leap_second;
696 using ::std::chrono::__detail::__local_time_fmt;
698 if constexpr (__is_specialization_of<_Tp, __local_time_fmt>)
699 return _M_format_to_ostream(__t._M_time, __fc,
false);
702 basic_ostringstream<_CharT> __os;
703 __os.imbue(_M_locale(__fc));
705 if constexpr (__is_specialization_of<_Tp, __utc_leap_second>)
706 __os << __t._M_date <<
' ' << __t._M_time;
707 else if constexpr (chrono::__is_time_point_v<_Tp>)
714 if constexpr (is_convertible_v<_Tp, chrono::sys_days>)
715 __os << _S_date(__t);
718 auto __days = chrono::floor<chrono::days>(__t);
719 __os << chrono::year_month_day(__days) <<
' '
720 << chrono::hh_mm_ss(__t - __days);
725 if constexpr (chrono::__is_duration_v<_Tp>)
726 if (__is_neg) [[unlikely]]
727 __os << _S_plus_minus[1];
732 return __format::__write_padded_as_spec(__str, __str.size(),
737 static constexpr
const _CharT* _S_chars
738 = _GLIBCXX_WIDEN(
"0123456789+-:/ {}");
739 static constexpr
const _CharT* _S_plus_minus = _S_chars + 10;
740 static constexpr _CharT _S_colon = _S_chars[12];
741 static constexpr _CharT _S_slash = _S_chars[13];
742 static constexpr _CharT _S_space = _S_chars[14];
743 static constexpr
const _CharT* _S_empty_spec = _S_chars + 15;
745 template<
typename _Tp,
typename _FormatContext>
746 typename _FormatContext::iterator
747 _M_a_A(
const _Tp& __t,
typename _FormatContext::iterator __out,
748 _FormatContext& __ctx,
bool __full)
const
752 chrono::weekday __wd = _S_weekday(__t);
754 __throw_format_error(
"format error: invalid weekday");
756 locale __loc = _M_locale(__ctx);
757 const auto& __tp = use_facet<__timepunct<_CharT>>(__loc);
758 const _CharT* __days[7];
760 __tp._M_days(__days);
762 __tp._M_days_abbreviated(__days);
763 __string_view __str(__days[__wd.c_encoding()]);
764 return __format::__write(
std::move(__out), __str);
767 template<
typename _Tp,
typename _FormatContext>
768 typename _FormatContext::iterator
769 _M_b_B(
const _Tp& __t,
typename _FormatContext::iterator __out,
770 _FormatContext& __ctx,
bool __full)
const
774 chrono::month __m = _S_month(__t);
776 __throw_format_error(
"format error: invalid month");
777 locale __loc = _M_locale(__ctx);
778 const auto& __tp = use_facet<__timepunct<_CharT>>(__loc);
779 const _CharT* __months[12];
781 __tp._M_months(__months);
783 __tp._M_months_abbreviated(__months);
784 __string_view __str(__months[(
unsigned)__m - 1]);
785 return __format::__write(
std::move(__out), __str);
788 template<
typename _Tp,
typename _FormatContext>
789 typename _FormatContext::iterator
790 _M_c(
const _Tp& __tt,
typename _FormatContext::iterator __out,
791 _FormatContext& __ctx,
bool __mod =
false)
const
796 basic_string<_CharT> __fmt;
797 auto __t = _S_floor_seconds(__tt);
798 locale __loc = _M_locale(__ctx);
799 const auto& __tp = use_facet<__timepunct<_CharT>>(__loc);
800 const _CharT* __formats[2];
801 __tp._M_date_time_formats(__formats);
802 if (*__formats[__mod]) [[likely]]
804 __fmt = _GLIBCXX_WIDEN(
"{:L}");
805 __fmt.insert(3u, __formats[__mod]);
808 __fmt = _GLIBCXX_WIDEN(
"{:L%a %b %e %T %Y}");
809 return std::vformat_to(
std::move(__out), __loc, __fmt,
810 std::make_format_args<_FormatContext>(__t));
813 template<
typename _Tp,
typename _FormatContext>
814 typename _FormatContext::iterator
815 _M_C_y_Y(
const _Tp& __t,
typename _FormatContext::iterator __out,
816 _FormatContext& __ctx, _CharT __conv, _CharT __mod = 0)
const
826 chrono::year __y = _S_year(__t);
828 if (__mod) [[unlikely]]
831 __tm.tm_year = (int)__y - 1900;
832 return _M_locale_fmt(
std::move(__out), _M_locale(__ctx), __tm,
836 basic_string<_CharT> __s;
838 const bool __is_neg = __yi < 0;
839 __yi = __builtin_abs(__yi);
841 if (__conv ==
'Y' || __conv ==
'C')
843 int __ci = __yi / 100;
844 if (__is_neg) [[unlikely]]
846 __s.assign(1, _S_plus_minus[1]);
848 if (__conv ==
'C' && (__ci * 100) != __yi)
851 if (__ci >= 100) [[unlikely]]
853 __s += std::format(_S_empty_spec, __ci / 100);
856 __s += _S_two_digits(__ci);
859 if (__conv ==
'Y' || __conv ==
'y')
860 __s += _S_two_digits(__yi % 100);
862 return __format::__write(
std::move(__out), __string_view(__s));
865 template<
typename _Tp,
typename _FormatContext>
866 typename _FormatContext::iterator
867 _M_D(
const _Tp& __t,
typename _FormatContext::iterator __out,
868 _FormatContext&)
const
870 auto __ymd = _S_date(__t);
871 basic_string<_CharT> __s;
872 #if ! _GLIBCXX_USE_CXX11_ABI
875 __s = _S_two_digits((
unsigned)__ymd.month());
877 __s += _S_two_digits((
unsigned)__ymd.day());
879 __s += _S_two_digits(__builtin_abs((
int)__ymd.year()) % 100);
880 return __format::__write(
std::move(__out), __string_view(__s));
883 template<
typename _Tp,
typename _FormatContext>
884 typename _FormatContext::iterator
885 _M_d_e(
const _Tp& __t,
typename _FormatContext::iterator __out,
886 _FormatContext& __ctx, _CharT __conv,
bool __mod =
false)
const
893 chrono::day __d = _S_day(__t);
894 unsigned __i = (unsigned)__d;
896 if (__mod) [[unlikely]]
900 return _M_locale_fmt(
std::move(__out), _M_locale(__ctx), __tm,
904 auto __sv = _S_two_digits(__i);
906 if (__conv == _CharT(
'e') && __i < 10)
912 return __format::__write(
std::move(__out), __sv);
915 template<
typename _Tp,
typename _FormatContext>
916 typename _FormatContext::iterator
917 _M_F(
const _Tp& __t,
typename _FormatContext::iterator __out,
918 _FormatContext&)
const
920 auto __ymd = _S_date(__t);
921 auto __s = std::format(_GLIBCXX_WIDEN(
"{:04d}- - "),
923 auto __sv = _S_two_digits((
unsigned)__ymd.month());
924 __s[__s.size() - 5] = __sv[0];
925 __s[__s.size() - 4] = __sv[1];
926 __sv = _S_two_digits((
unsigned)__ymd.day());
927 __s[__s.size() - 2] = __sv[0];
928 __s[__s.size() - 1] = __sv[1];
930 return __format::__write(
std::move(__out), __sv);
933 template<
typename _Tp,
typename _FormatContext>
934 typename _FormatContext::iterator
935 _M_g_G(
const _Tp& __t,
typename _FormatContext::iterator __out,
936 _FormatContext& __ctx,
bool __full)
const
940 using namespace chrono;
941 auto __d = _S_days(__t);
943 __d -= (weekday(__d) - Monday) -
days(3);
945 year __y = year_month_day(__d).year();
946 return _M_C_y_Y(__y,
std::move(__out), __ctx,
"yY"[__full]);
949 template<
typename _Tp,
typename _FormatContext>
950 typename _FormatContext::iterator
951 _M_H_I(
const _Tp& __t,
typename _FormatContext::iterator __out,
952 _FormatContext& __ctx, _CharT __conv,
bool __mod =
false)
const
959 const auto __hms = _S_hms(__t);
960 int __i = __hms.hours().count();
962 if (__mod) [[unlikely]]
966 return _M_locale_fmt(
std::move(__out), _M_locale(__ctx), __tm,
970 if (__conv == _CharT(
'I'))
977 return __format::__write(
std::move(__out), _S_two_digits(__i));
980 template<
typename _Tp,
typename _FormatContext>
981 typename _FormatContext::iterator
982 _M_j(
const _Tp& __t,
typename _FormatContext::iterator __out,
983 _FormatContext&)
const
985 if constexpr (chrono::__is_duration_v<_Tp>)
988 unsigned __d = chrono::duration_cast<chrono::days>(__t).count();
989 return std::format_to(
std::move(__out), _S_empty_spec, __d);
994 using namespace chrono;
995 auto __day = _S_days(__t);
996 auto __ymd = _S_date(__t);
1000 if constexpr (is_same_v<
typename decltype(__day)::clock, local_t>)
1001 __d = __day - local_days(__ymd.year()/January/0);
1003 __d = __day - sys_days(__ymd.year()/January/0);
1004 return
std::format_to(
std::
move(__out), _GLIBCXX_WIDEN("{:03d}
"),
1009 template<typename _Tp, typename _FormatContext>
1010 typename _FormatContext::iterator
1011 _M_m(const _Tp& __t, typename _FormatContext::iterator __out,
1012 _FormatContext& __ctx, bool __mod) const
1014 // %m month as a decimal number.
1015 // %Om Locale's alternative representation.
1017 auto __m = _S_month(__t);
1018 auto __i = (unsigned)__m;
1020 if (__mod) [[unlikely]] // %Om
1023 __tm.tm_mon = __i - 1;
1024 return _M_locale_fmt(std::move(__out), _M_locale(__ctx), __tm,
1028 return __format::__write(std::move(__out), _S_two_digits(__i));
1031 template<typename _Tp, typename _FormatContext>
1032 typename _FormatContext::iterator
1033 _M_M(const _Tp& __t, typename _FormatContext::iterator __out,
1034 _FormatContext& __ctx, bool __mod) const
1036 // %M The minute as a decimal number.
1037 // %OM Locale's alternative representation.
1039 auto __m = _S_hms(__t).minutes();
1040 auto __i = __m.count();
1042 if (__mod) [[unlikely]] // %OM
1046 return _M_locale_fmt(std::move(__out), _M_locale(__ctx), __tm,
1050 return __format::__write(std::move(__out), _S_two_digits(__i));
1053 template<typename _Tp, typename _FormatContext>
1054 typename _FormatContext::iterator
1055 _M_p(const _Tp& __t, typename _FormatContext::iterator __out,
1056 _FormatContext& __ctx) const
1058 // %p The locale's equivalent of the AM/PM designations.
1059 auto __hms = _S_hms(__t);
1060 locale __loc = _M_locale(__ctx);
1061 const auto& __tp = use_facet<__timepunct<_CharT>>(__loc);
1062 const _CharT* __ampm[2];
1063 __tp._M_am_pm(__ampm);
1064 return std::format_to(std::move(__out), _S_empty_spec,
1065 __ampm[__hms.hours().count() >= 12]);
1068 template<typename _Tp, typename _FormatContext>
1069 typename _FormatContext::iterator
1070 _M_q(const _Tp&, typename _FormatContext::iterator __out,
1071 _FormatContext&) const
1073 // %q The duration's unit suffix
1074 if constexpr (!chrono::__is_duration_v<_Tp>)
1075 __throw_format_error("format error: argument is not a duration
");
1078 namespace __d = chrono::__detail;
1079 using period = typename _Tp::period;
1080 return __d::__fmt_units_suffix<period, _CharT>(std::move(__out));
1084 // %Q handled in _M_format
1086 template<typename _Tp, typename _FormatContext>
1087 typename _FormatContext::iterator
1088 _M_r(const _Tp& __tt, typename _FormatContext::iterator __out,
1089 _FormatContext& __ctx) const
1091 // %r locale's 12-hour clock time.
1092 auto __t = _S_floor_seconds(__tt);
1093 locale __loc = _M_locale(__ctx);
1094 const auto& __tp = use_facet<__timepunct<_CharT>>(__loc);
1095 const _CharT* __ampm_fmt;
1096 __tp._M_am_pm_format(&__ampm_fmt);
1097 basic_string<_CharT> __fmt(_S_empty_spec);
1098 __fmt.insert(1u, 1u, _S_colon);
1099 __fmt.insert(2u, __ampm_fmt);
1100 return std::vformat_to(std::move(__out), __fmt,
1101 std::make_format_args<_FormatContext>(__t));
1104 template<typename _Tp, typename _FormatContext>
1105 typename _FormatContext::iterator
1106 _M_R_T(const _Tp& __t, typename _FormatContext::iterator __out,
1107 _FormatContext& __ctx, bool __secs) const
1109 // %R Equivalent to %H:%M
1110 // %T Equivalent to %H:%M:%S
1111 auto __hms = _S_hms(__t);
1113 auto __s = std::format(_GLIBCXX_WIDEN("{:02d}:00
"),
1114 __hms.hours().count());
1115 auto __sv = _S_two_digits(__hms.minutes().count());
1116 __s[__s.size() - 2] = __sv[0];
1117 __s[__s.size() - 1] = __sv[1];
1119 __out = __format::__write(std::move(__out), __sv);
1122 *__out++ = _S_colon;
1123 __out = _M_S(__hms, std::move(__out), __ctx);
1128 template<typename _Tp, typename _FormatContext>
1129 typename _FormatContext::iterator
1130 _M_S(const _Tp& __t, typename _FormatContext::iterator __out,
1131 _FormatContext& __ctx, bool __mod = false) const
1133 // %S Seconds as a decimal number.
1134 // %OS The locale's alternative representation.
1135 auto __hms = _S_hms(__t);
1137 if (__mod) [[unlikely]] // %OS
1140 __tm.tm_sec = (int)__hms.seconds().count();
1141 return _M_locale_fmt(std::move(__out), _M_locale(__ctx), __tm,
1145 if constexpr (__hms.fractional_width == 0)
1146 __out = __format::__write(std::move(__out),
1147 _S_two_digits(__hms.seconds().count()));
1150 locale __loc = _M_locale(__ctx);
1151 auto __s = __hms.seconds();
1152 auto __ss = __hms.subseconds();
1153 using rep = typename decltype(__ss)::rep;
1154 if constexpr (is_floating_point_v<rep>)
1156 chrono::duration<rep> __fs = __s + __ss;
1157 __out = std::format_to(std::move(__out), __loc,
1158 _GLIBCXX_WIDEN("{:#0{}.{}Lf}
"),
1160 3 + __hms.fractional_width,
1161 __hms.fractional_width);
1166 = use_facet<numpunct<_CharT>>(__loc);
1167 __out = __format::__write(std::move(__out),
1168 _S_two_digits(__s.count()));
1169 *__out++ = __np.decimal_point();
1170 if constexpr (is_integral_v<rep>)
1171 __out = std::format_to(std::move(__out),
1172 _GLIBCXX_WIDEN("{:0{}}
"),
1174 __hms.fractional_width);
1177 auto __str = std::format(_S_empty_spec, __ss.count());
1178 __out = std::format_to(_GLIBCXX_WIDEN("{:0>{}s}
"),
1180 __hms.fractional_width);
1187 // %t handled in _M_format
1189 template<typename _Tp, typename _FormatContext>
1190 typename _FormatContext::iterator
1191 _M_u_w(const _Tp& __t, typename _FormatContext::iterator __out,
1192 _FormatContext& __ctx, _CharT __conv, bool __mod = false) const
1194 // %u ISO weekday as a decimal number (1-7), where Monday is 1.
1195 // %Ou Locale's alternative numeric rep.
1196 // %w Weekday as a decimal number (0-6), where Sunday is 0.
1197 // %Ow Locale's alternative numeric rep.
1199 chrono::weekday __wd = _S_weekday(__t);
1201 if (__mod) [[unlikely]]
1204 __tm.tm_wday = __wd.c_encoding();
1205 return _M_locale_fmt(std::move(__out), _M_locale(__ctx), __tm,
1209 unsigned __wdi = __conv == 'u' ? __wd.iso_encoding()
1210 : __wd.c_encoding();
1211 const _CharT __d = _S_digit(__wdi);
1212 return __format::__write(std::move(__out), __string_view(&__d, 1));
1215 template<typename _Tp, typename _FormatContext>
1216 typename _FormatContext::iterator
1217 _M_U_V_W(const _Tp& __t, typename _FormatContext::iterator __out,
1218 _FormatContext& __ctx, _CharT __conv, bool __mod = false) const
1220 // %U Week number of the year as a decimal number, from first Sunday.
1221 // %OU Locale's alternative numeric rep.
1222 // %V ISO week-based week number as a decimal number.
1223 // %OV Locale's alternative numeric rep.
1224 // %W Week number of the year as a decimal number, from first Monday.
1225 // %OW Locale's alternative numeric rep.
1226 using namespace chrono;
1227 auto __d = _S_days(__t);
1228 using _TDays = decltype(__d); // Either sys_days or local_days.
1230 if (__mod) [[unlikely]]
1232 const year_month_day __ymd(__d);
1233 const year __y = __ymd.year();
1235 __tm.tm_year = (int)__y - 1900;
1236 __tm.tm_yday = (__d - _TDays(__y/January/1)).count();
1237 __tm.tm_wday = weekday(__d).c_encoding();
1238 return _M_locale_fmt(std::move(__out), _M_locale(__ctx), __tm,
1242 _TDays __first; // First day of week 1.
1243 if (__conv == 'V') // W01 begins on Monday before first Thursday.
1245 // Move to nearest Thursday:
1246 __d -= (weekday(__d) - Monday) - days(3);
1247 // ISO week of __t is number of weeks since January 1 of the
1248 // same year as that nearest Thursday.
1249 __first = _TDays(year_month_day(__d).year()/January/1);
1254 if constexpr (requires { __t.year(); })
1257 __y = year_month_day(__d).year();
1258 const weekday __weekstart = __conv == 'U' ? Sunday : Monday;
1259 __first = _TDays(__y/January/__weekstart[1]);
1261 auto __weeks = chrono::floor<weeks>(__d - __first);
1262 __string_view __sv = _S_two_digits(__weeks.count() + 1);
1263 return __format::__write(std::move(__out), __sv);
1266 template<typename _Tp, typename _FormatContext>
1267 typename _FormatContext::iterator
1268 _M_x(const _Tp& __t, typename _FormatContext::iterator __out,
1269 _FormatContext& __ctx, bool __mod = false) const
1271 // %x Locale's date rep
1272 // %Ex Locale's alternative date representation.
1273 locale __loc = _M_locale(__ctx);
1274 const auto& __tp = use_facet<__timepunct<_CharT>>(__loc);
1275 const _CharT* __date_reps[2];
1276 __tp._M_date_formats(__date_reps);
1277 const _CharT* __rep = __date_reps[__mod];
1279 return _M_D(__t, std::move(__out), __ctx);
1281 basic_string<_CharT> __fmt(_S_empty_spec);
1282 __fmt.insert(1u, 1u, _S_colon);
1283 __fmt.insert(2u, __rep);
1284 return std::vformat_to(std::move(__out), __fmt,
1285 std::make_format_args<_FormatContext>(__t));
1288 template<typename _Tp, typename _FormatContext>
1289 typename _FormatContext::iterator
1290 _M_X(const _Tp& __tt, typename _FormatContext::iterator __out,
1291 _FormatContext& __ctx, bool __mod = false) const
1293 // %X Locale's time rep
1294 // %EX Locale's alternative time representation.
1295 auto __t = _S_floor_seconds(__tt);
1296 locale __loc = _M_locale(__ctx);
1297 const auto& __tp = use_facet<__timepunct<_CharT>>(__loc);
1298 const _CharT* __time_reps[2];
1299 __tp._M_time_formats(__time_reps);
1300 const _CharT* __rep = __time_reps[__mod];
1302 return _M_R_T(__t, std::move(__out), __ctx, true);
1304 basic_string<_CharT> __fmt(_S_empty_spec);
1305 __fmt.insert(1u, 1u, _S_colon);
1306 __fmt.insert(2u, __rep);
1307 return std::vformat_to(std::move(__out), __fmt,
1308 std::make_format_args<_FormatContext>(__t));
1311 template<typename _Tp, typename _FormatContext>
1312 typename _FormatContext::iterator
1313 _M_z(const _Tp& __t, typename _FormatContext::iterator __out,
1314 _FormatContext&, bool __mod = false) const
1316 using ::std::chrono::__detail::__utc_leap_second;
1317 using ::std::chrono::__detail::__local_time_fmt;
1319 auto __utc = __mod ? __string_view(_GLIBCXX_WIDEN("+00:00
"), 6)
1320 : __string_view(_GLIBCXX_WIDEN("+0000
"), 5);
1322 if constexpr (chrono::__is_time_point_v<_Tp>)
1324 if constexpr (is_same_v<typename _Tp::clock,
1325 chrono::system_clock>)
1326 return __format::__write(std::move(__out), __utc);
1328 else if constexpr (__is_specialization_of<_Tp, __local_time_fmt>)
1330 if (__t._M_offset_sec)
1333 basic_string<_CharT> __s;
1334 if (*__t._M_offset_sec != 0s)
1336 chrono:: hh_mm_ss __hms(*__t._M_offset_sec);
1337 __s = _S_plus_minus[__hms.is_negative()];
1338 __s += _S_two_digits(__hms.hours().count());
1341 __s += _S_two_digits(__hms.minutes().count());
1344 return __format::__write(std::move(__out), __sv);
1347 else if constexpr (__is_specialization_of<_Tp, __utc_leap_second>)
1348 return __format::__write(std::move(__out), __utc);
1350 __no_timezone_available();
1353 template<typename _Tp, typename _FormatContext>
1354 typename _FormatContext::iterator
1355 _M_Z(const _Tp& __t, typename _FormatContext::iterator __out,
1356 _FormatContext& __ctx) const
1358 using ::std::chrono::__detail::__utc_leap_second;
1359 using ::std::chrono::__detail::__local_time_fmt;
1361 __string_view __utc(_GLIBCXX_WIDEN("UTC
"), 3);
1362 if constexpr (chrono::__is_time_point_v<_Tp>)
1364 if constexpr (is_same_v<typename _Tp::clock,
1365 chrono::system_clock>)
1366 return __format::__write(std::move(__out), __utc);
1368 else if constexpr (__is_specialization_of<_Tp, __local_time_fmt>)
1372 string_view __sv = *__t._M_abbrev;
1373 if constexpr (is_same_v<_CharT, char>)
1374 return __format::__write(std::move(__out), __sv);
1377 // TODO use resize_and_overwrite
1378 basic_string<_CharT> __ws(__sv.size(), _CharT());
1379 auto& __ct = use_facet<ctype<_CharT>>(_M_locale(__ctx));
1380 __ct.widen(__sv.begin(), __sv.end(), __ws.data());
1381 __string_view __wsv = __ws;
1382 return __format::__write(std::move(__out), __wsv);
1386 else if constexpr (__is_specialization_of<_Tp, __utc_leap_second>)
1387 return __format::__write(std::move(__out), __utc);
1389 __no_timezone_available();
1392 // %% handled in _M_format
1394 // A single digit character in the range '0'..'9'.
1396 _S_digit(int __n) noexcept
1398 // Extra 9s avoid past-the-end read on bad input.
1399 return _GLIBCXX_WIDEN("0123456789999999
")[__n & 0xf];
1402 // A string view of two digit characters, "00
".."99
".
1403 static basic_string_view<_CharT>
1404 _S_two_digits(int __n) noexcept
1407 _GLIBCXX_WIDEN("0001020304050607080910111213141516171819
"
1408 "2021222324252627282930313233343536373839
"
1409 "4041424344454647484950515253545556575859
"
1410 "6061626364656667686970717273747576777879
"
1411 "8081828384858687888990919293949596979899
"
1412 "9999999999999999999999999999999999999999
"
1413 "9999999999999999
") + 2 * (__n & 0x7f),
1418 // Accessors for the components of chrono types:
1420 // Returns a hh_mm_ss.
1421 template<typename _Tp>
1422 static decltype(auto)
1423 _S_hms(const _Tp& __t)
1425 using ::std::chrono::__detail::__utc_leap_second;
1426 using ::std::chrono::__detail::__local_time_fmt;
1428 if constexpr (__is_specialization_of<_Tp, chrono::hh_mm_ss>)
1430 else if constexpr (__is_specialization_of<_Tp, __utc_leap_second>)
1432 else if constexpr (chrono::__is_duration_v<_Tp>)
1433 return chrono::hh_mm_ss<_Tp>(__t);
1434 else if constexpr (chrono::__is_time_point_v<_Tp>)
1435 return chrono::hh_mm_ss(__t - chrono::floor<chrono::days>(__t));
1436 else if constexpr (__is_specialization_of<_Tp, __local_time_fmt>)
1437 return _S_hms(__t._M_time);
1440 __invalid_chrono_spec();
1441 return chrono::hh_mm_ss<chrono::seconds>();
1445 // Returns a sys_days or local_days.
1446 template<typename _Tp>
1448 _S_days(const _Tp& __t)
1450 using namespace chrono;
1451 using ::std::chrono::__detail::__utc_leap_second;
1452 using ::std::chrono::__detail::__local_time_fmt;
1454 if constexpr (__is_time_point_v<_Tp>)
1455 return chrono::floor<days>(__t);
1456 else if constexpr (__is_specialization_of<_Tp, __utc_leap_second>)
1458 else if constexpr (__is_specialization_of<_Tp, __local_time_fmt>)
1459 return chrono::floor<days>(__t._M_time);
1460 else if constexpr (is_same_v<_Tp, year_month_day>
1461 || is_same_v<_Tp, year_month_day_last>
1462 || is_same_v<_Tp, year_month_weekday>
1463 || is_same_v<_Tp, year_month_weekday_last>)
1464 return sys_days(__t);
1467 if constexpr (__is_duration_v<_Tp>)
1468 __not_valid_for_duration();
1470 __invalid_chrono_spec();
1471 return chrono::sys_days();
1475 // Returns a year_month_day.
1476 template<typename _Tp>
1477 static chrono::year_month_day
1478 _S_date(const _Tp& __t)
1480 if constexpr (is_same_v<_Tp, chrono::year_month_day>)
1483 return chrono::year_month_day(_S_days(__t));
1486 template<typename _Tp>
1488 _S_day(const _Tp& __t)
1490 using namespace chrono;
1492 if constexpr (is_same_v<_Tp, day>)
1494 else if constexpr (requires { __t.day(); })
1497 return _S_date(__t).day();
1500 template<typename _Tp>
1501 static chrono::month
1502 _S_month(const _Tp& __t)
1504 using namespace chrono;
1506 if constexpr (is_same_v<_Tp, month>)
1508 else if constexpr (requires { __t.month(); })
1511 return _S_date(__t).month();
1514 template<typename _Tp>
1516 _S_year(const _Tp& __t)
1518 using namespace chrono;
1520 if constexpr (is_same_v<_Tp, year>)
1522 else if constexpr (requires { __t.year(); })
1525 return _S_date(__t).year();
1528 template<typename _Tp>
1529 static chrono::weekday
1530 _S_weekday(const _Tp& __t)
1532 using namespace ::std::chrono;
1533 using ::std::chrono::__detail::__local_time_fmt;
1535 if constexpr (is_same_v<_Tp, weekday>)
1537 else if constexpr (requires { __t.weekday(); })
1538 return __t.weekday();
1539 else if constexpr (is_same_v<_Tp, month_weekday>)
1540 return __t.weekday_indexed().weekday();
1541 else if constexpr (is_same_v<_Tp, month_weekday_last>)
1542 return __t.weekday_last().weekday();
1544 return weekday(_S_days(__t));
1547 // Remove subsecond precision from a time_point.
1548 template<typename _Tp>
1550 _S_floor_seconds(const _Tp& __t)
1552 using chrono::__detail::__local_time_fmt;
1553 if constexpr (chrono::__is_time_point_v<_Tp>
1554 || chrono::__is_duration_v<_Tp>)
1556 if constexpr (_Tp::period::den != 1)
1557 return chrono::floor<chrono::seconds>(__t);
1561 else if constexpr (__is_specialization_of<_Tp, chrono::hh_mm_ss>)
1563 if constexpr (_Tp::fractional_width != 0)
1564 return chrono::floor<chrono::seconds>(__t.to_duration());
1568 else if constexpr (__is_specialization_of<_Tp, __local_time_fmt>)
1569 return _S_floor_seconds(__t._M_time);
1574 // Use the formatting locale's std::time_put facet to produce
1575 // a locale-specific representation.
1576 template<typename _Iter>
1578 _M_locale_fmt(_Iter __out, const locale& __loc, const struct tm& __tm,
1579 char __fmt, char __mod) const
1581 basic_ostringstream<_CharT> __os;
1582 const auto& __tp = use_facet<time_put<_CharT>>(__loc);
1583 __tp.put(__os, __os, _S_space, &__tm, __fmt, __mod);
1585 __out = __format::__write(std::move(__out), __os.view());
1590 } // namespace __format
1593 template<typename _Rep, typename _Period, typename _CharT>
1594 struct formatter<chrono::duration<_Rep, _Period>, _CharT>
1596 constexpr typename basic_format_parse_context<_CharT>::iterator
1597 parse(basic_format_parse_context<_CharT>& __pc)
1599 using namespace __format;
1600 auto __it = _M_f._M_parse(__pc, _Duration|_TimeOfDay);
1601 if constexpr (!is_floating_point_v<_Rep>)
1602 if (_M_f._M_spec._M_prec_kind != __format::_WP_none)
1603 __throw_format_error("format error: invalid precision
for duration
");
1607 template<typename _Out>
1608 typename basic_format_context<_Out, _CharT>::iterator
1609 format(const chrono::duration<_Rep, _Period>& __d,
1610 basic_format_context<_Out, _CharT>& __fc) const
1612 if constexpr (numeric_limits<_Rep>::is_signed)
1613 if (__d < __d.zero()) [[unlikely]]
1615 if constexpr (is_integral_v<_Rep>)
1617 // -d is undefined for the most negative integer.
1618 // Convert duration to corresponding unsigned rep.
1619 using _URep = make_unsigned_t<_Rep>;
1620 auto __ucnt = -static_cast<_URep>(__d.count());
1621 auto __ud = chrono::duration<_URep, _Period>(__ucnt);
1622 return _M_f._M_format(__ud, __fc, true);
1625 return _M_f._M_format(-__d, __fc, true);
1627 return _M_f._M_format(__d, __fc, false);
1631 __format::__formatter_chrono<_CharT> _M_f;
1634 template<typename _CharT>
1635 struct formatter<chrono::day, _CharT>
1637 template<typename _ParseContext>
1638 constexpr typename _ParseContext::iterator
1639 parse(_ParseContext& __pc)
1640 { return _M_f._M_parse(__pc, __format::_Day); }
1642 template<typename _FormatContext>
1643 typename _FormatContext::iterator
1644 format(const chrono::day& __t, _FormatContext& __fc) const
1645 { return _M_f._M_format(__t, __fc); }
1648 __format::__formatter_chrono<_CharT> _M_f;
1651 template<typename _CharT>
1652 struct formatter<chrono::month, _CharT>
1654 template<typename _ParseContext>
1655 constexpr typename _ParseContext::iterator
1656 parse(_ParseContext& __pc)
1657 { return _M_f._M_parse(__pc, __format::_Month); }
1659 template<typename _FormatContext>
1660 typename _FormatContext::iterator
1661 format(const chrono::month& __t, _FormatContext& __fc) const
1662 { return _M_f._M_format(__t, __fc); }
1665 __format::__formatter_chrono<_CharT> _M_f;
1668 template<typename _CharT>
1669 struct formatter<chrono::year, _CharT>
1671 template<typename _ParseContext>
1672 constexpr typename _ParseContext::iterator
1673 parse(_ParseContext& __pc)
1674 { return _M_f._M_parse(__pc, __format::_Year); }
1676 template<typename _FormatContext>
1677 typename _FormatContext::iterator
1678 format(const chrono::year& __t, _FormatContext& __fc) const
1679 { return _M_f._M_format(__t, __fc); }
1682 __format::__formatter_chrono<_CharT> _M_f;
1685 template<typename _CharT>
1686 struct formatter<chrono::weekday, _CharT>
1688 template<typename _ParseContext>
1689 constexpr typename _ParseContext::iterator
1690 parse(_ParseContext& __pc)
1691 { return _M_f._M_parse(__pc, __format::_Weekday); }
1693 template<typename _FormatContext>
1694 typename _FormatContext::iterator
1695 format(const chrono::weekday& __t, _FormatContext& __fc) const
1696 { return _M_f._M_format(__t, __fc); }
1699 __format::__formatter_chrono<_CharT> _M_f;
1702 template<typename _CharT>
1703 struct formatter<chrono::weekday_indexed, _CharT>
1705 template<typename _ParseContext>
1706 constexpr typename _ParseContext::iterator
1707 parse(_ParseContext& __pc)
1708 { return _M_f._M_parse(__pc, __format::_Weekday); }
1710 template<typename _FormatContext>
1711 typename _FormatContext::iterator
1712 format(const chrono::weekday_indexed& __t, _FormatContext& __fc) const
1713 { return _M_f._M_format(__t, __fc); }
1716 __format::__formatter_chrono<_CharT> _M_f;
1719 template<typename _CharT>
1720 struct formatter<chrono::weekday_last, _CharT>
1722 template<typename _ParseContext>
1723 constexpr typename _ParseContext::iterator
1724 parse(_ParseContext& __pc)
1725 { return _M_f._M_parse(__pc, __format::_Weekday); }
1727 template<typename _FormatContext>
1728 typename _FormatContext::iterator
1729 format(const chrono::weekday_last& __t, _FormatContext& __fc) const
1730 { return _M_f._M_format(__t, __fc); }
1733 __format::__formatter_chrono<_CharT> _M_f;
1736 template<typename _CharT>
1737 struct formatter<chrono::month_day, _CharT>
1739 template<typename _ParseContext>
1740 constexpr typename _ParseContext::iterator
1741 parse(_ParseContext& __pc)
1742 { return _M_f._M_parse(__pc, __format::_Month|__format::_Day); }
1744 template<typename _FormatContext>
1745 typename _FormatContext::iterator
1746 format(const chrono::month_day& __t, _FormatContext& __fc) const
1747 { return _M_f._M_format(__t, __fc); }
1750 __format::__formatter_chrono<_CharT> _M_f;
1753 template<typename _CharT>
1754 struct formatter<chrono::month_day_last, _CharT>
1756 template<typename _ParseContext>
1757 constexpr typename _ParseContext::iterator
1758 parse(_ParseContext& __pc)
1759 { return _M_f._M_parse(__pc, __format::_Month|__format::_Day); }
1761 template<typename _FormatContext>
1762 typename _FormatContext::iterator
1763 format(const chrono::month_day_last& __t, _FormatContext& __fc) const
1764 { return _M_f._M_format(__t, __fc); }
1767 __format::__formatter_chrono<_CharT> _M_f;
1770 template<typename _CharT>
1771 struct formatter<chrono::month_weekday, _CharT>
1773 template<typename _ParseContext>
1774 constexpr typename _ParseContext::iterator
1775 parse(_ParseContext& __pc)
1776 { return _M_f._M_parse(__pc, __format::_Month|__format::_Weekday); }
1778 template<typename _FormatContext>
1779 typename _FormatContext::iterator
1780 format(const chrono::month_weekday& __t, _FormatContext& __fc) const
1781 { return _M_f._M_format(__t, __fc); }
1784 __format::__formatter_chrono<_CharT> _M_f;
1787 template<typename _CharT>
1788 struct formatter<chrono::month_weekday_last, _CharT>
1790 template<typename _ParseContext>
1791 constexpr typename _ParseContext::iterator
1792 parse(_ParseContext& __pc)
1793 { return _M_f._M_parse(__pc, __format::_Month|__format::_Weekday); }
1795 template<typename _FormatContext>
1796 typename _FormatContext::iterator
1797 format(const chrono::month_weekday_last& __t,
1798 _FormatContext& __fc) const
1799 { return _M_f._M_format(__t, __fc); }
1802 __format::__formatter_chrono<_CharT> _M_f;
1805 template<typename _CharT>
1806 struct formatter<chrono::year_month, _CharT>
1808 template<typename _ParseContext>
1809 constexpr typename _ParseContext::iterator
1810 parse(_ParseContext& __pc)
1811 { return _M_f._M_parse(__pc, __format::_Year|__format::_Month); }
1813 template<typename _FormatContext>
1814 typename _FormatContext::iterator
1815 format(const chrono::year_month& __t, _FormatContext& __fc) const
1816 { return _M_f._M_format(__t, __fc); }
1819 __format::__formatter_chrono<_CharT> _M_f;
1822 template<typename _CharT>
1823 struct formatter<chrono::year_month_day, _CharT>
1825 template<typename _ParseContext>
1826 constexpr typename _ParseContext::iterator
1827 parse(_ParseContext& __pc)
1828 { return _M_f._M_parse(__pc, __format::_Date); }
1830 template<typename _FormatContext>
1831 typename _FormatContext::iterator
1832 format(const chrono::year_month_day& __t, _FormatContext& __fc) const
1833 { return _M_f._M_format(__t, __fc); }
1836 __format::__formatter_chrono<_CharT> _M_f;
1839 template<typename _CharT>
1840 struct formatter<chrono::year_month_day_last, _CharT>
1842 template<typename _ParseContext>
1843 constexpr typename _ParseContext::iterator
1844 parse(_ParseContext& __pc)
1845 { return _M_f._M_parse(__pc, __format::_Date); }
1847 template<typename _FormatContext>
1848 typename _FormatContext::iterator
1849 format(const chrono::year_month_day_last& __t,
1850 _FormatContext& __fc) const
1851 { return _M_f._M_format(__t, __fc); }
1854 __format::__formatter_chrono<_CharT> _M_f;
1857 template<typename _CharT>
1858 struct formatter<chrono::year_month_weekday, _CharT>
1860 template<typename _ParseContext>
1861 constexpr typename _ParseContext::iterator
1862 parse(_ParseContext& __pc)
1863 { return _M_f._M_parse(__pc, __format::_Date); }
1865 template<typename _FormatContext>
1866 typename _FormatContext::iterator
1867 format(const chrono::year_month_weekday& __t,
1868 _FormatContext& __fc) const
1869 { return _M_f._M_format(__t, __fc); }
1872 __format::__formatter_chrono<_CharT> _M_f;
1875 template<typename _CharT>
1876 struct formatter<chrono::year_month_weekday_last, _CharT>
1878 template<typename _ParseContext>
1879 constexpr typename _ParseContext::iterator
1880 parse(_ParseContext& __pc)
1881 { return _M_f._M_parse(__pc, __format::_Date); }
1883 template<typename _FormatContext>
1884 typename _FormatContext::iterator
1885 format(const chrono::year_month_weekday_last& __t,
1886 _FormatContext& __fc) const
1887 { return _M_f._M_format(__t, __fc); }
1890 __format::__formatter_chrono<_CharT> _M_f;
1893 template<typename _Rep, typename _Period, typename _CharT>
1894 struct formatter<chrono::hh_mm_ss<chrono::duration<_Rep, _Period>>, _CharT>
1896 template<typename _ParseContext>
1897 constexpr typename _ParseContext::iterator
1898 parse(_ParseContext& __pc)
1899 { return _M_f._M_parse(__pc, __format::_TimeOfDay); }
1901 template<typename _FormatContext>
1902 typename _FormatContext::iterator
1903 format(const chrono::hh_mm_ss<chrono::duration<_Rep, _Period>>& __t,
1904 _FormatContext& __fc) const
1905 { return _M_f._M_format(__t, __fc); }
1908 __format::__formatter_chrono<_CharT> _M_f;
1911 #if _GLIBCXX_USE_CXX11_ABI || ! _GLIBCXX_USE_DUAL_ABI
1912 template<typename _CharT>
1913 struct formatter<chrono::sys_info, _CharT>
1915 template<typename _ParseContext>
1916 constexpr typename _ParseContext::iterator
1917 parse(_ParseContext& __pc)
1918 { return _M_f._M_parse(__pc, __format::_ChronoParts{}); }
1920 template<typename _FormatContext>
1921 typename _FormatContext::iterator
1922 format(const chrono::sys_info& __i, _FormatContext& __fc) const
1923 { return _M_f._M_format(__i, __fc); }
1926 __format::__formatter_chrono<_CharT> _M_f;
1929 template<typename _CharT>
1930 struct formatter<chrono::local_info, _CharT>
1932 template<typename _ParseContext>
1933 constexpr typename _ParseContext::iterator
1934 parse(_ParseContext& __pc)
1935 { return _M_f._M_parse(__pc, __format::_ChronoParts{}); }
1937 template<typename _FormatContext>
1938 typename _FormatContext::iterator
1939 format(const chrono::local_info& __i, _FormatContext& __fc) const
1940 { return _M_f._M_format(__i, __fc); }
1943 __format::__formatter_chrono<_CharT> _M_f;
1947 template<typename _Duration, typename _CharT>
1948 struct formatter<chrono::sys_time<_Duration>, _CharT>
1950 template<typename _ParseContext>
1951 constexpr typename _ParseContext::iterator
1952 parse(_ParseContext& __pc)
1954 auto __next = _M_f._M_parse(__pc, __format::_ZonedDateTime);
1955 if constexpr (!__stream_insertable)
1956 if (_M_f._M_spec._M_chrono_specs.empty())
1957 __format::__invalid_chrono_spec(); // chrono-specs can't be empty
1961 template<typename _FormatContext>
1962 typename _FormatContext::iterator
1963 format(const chrono::sys_time<_Duration>& __t,
1964 _FormatContext& __fc) const
1965 { return _M_f._M_format(__t, __fc); }
1968 static constexpr bool __stream_insertable
1969 = requires (basic_ostream<_CharT>& __os,
1970 chrono::sys_time<_Duration> __t) { __os << __t; };
1972 __format::__formatter_chrono<_CharT> _M_f;
1975 template<typename _Duration, typename _CharT>
1976 struct formatter<chrono::utc_time<_Duration>, _CharT>
1977 : __format::__formatter_chrono<_CharT>
1979 template<typename _ParseContext>
1980 constexpr typename _ParseContext::iterator
1981 parse(_ParseContext& __pc)
1982 { return _M_f._M_parse(__pc, __format::_ZonedDateTime); }
1984 template<typename _FormatContext>
1985 typename _FormatContext::iterator
1986 format(const chrono::utc_time<_Duration>& __t,
1987 _FormatContext& __fc) const
1989 // Adjust by removing leap seconds to get equivalent sys_time.
1990 // We can't just use clock_cast because we want to know if the time
1991 // falls within a leap second insertion, and format seconds as "60
".
1992 using chrono::__detail::__utc_leap_second;
1993 using chrono::seconds;
1994 using chrono::sys_time;
1995 using _CDur = common_type_t<_Duration, seconds>;
1996 const auto __li = chrono::get_leap_second_info(__t);
1997 sys_time<_CDur> __s{__t.time_since_epoch() - __li.elapsed};
1998 if (!__li.is_leap_second) [[likely]]
1999 return _M_f._M_format(__s, __fc);
2001 return _M_f._M_format(__utc_leap_second(__s), __fc);
2005 friend formatter<chrono::__detail::__utc_leap_second<_Duration>, _CharT>;
2007 __format::__formatter_chrono<_CharT> _M_f;
2010 template<typename _Duration, typename _CharT>
2011 struct formatter<chrono::tai_time<_Duration>, _CharT>
2012 : __format::__formatter_chrono<_CharT>
2014 template<typename _ParseContext>
2015 constexpr typename _ParseContext::iterator
2016 parse(_ParseContext& __pc)
2017 { return _M_f._M_parse(__pc, __format::_ZonedDateTime); }
2019 template<typename _FormatContext>
2020 typename _FormatContext::iterator
2021 format(const chrono::tai_time<_Duration>& __t,
2022 _FormatContext& __fc) const
2024 // Convert to __local_time_fmt with abbrev "TAI
" and offset 0s.
2026 // Offset is 1970y/January/1 - 1958y/January/1
2027 constexpr chrono::days __tai_offset = chrono::days(4383);
2028 using _CDur = common_type_t<_Duration, chrono::days>;
2029 chrono::local_time<_CDur> __lt(__t.time_since_epoch() - __tai_offset);
2030 const string __abbrev("TAI
", 3);
2031 const chrono::seconds __off = 0s;
2032 const auto __lf = chrono::local_time_format(__lt, &__abbrev, &__off);
2033 return _M_f._M_format(__lf, __fc);
2037 __format::__formatter_chrono<_CharT> _M_f;
2040 template<typename _Duration, typename _CharT>
2041 struct formatter<chrono::gps_time<_Duration>, _CharT>
2042 : __format::__formatter_chrono<_CharT>
2044 template<typename _ParseContext>
2045 constexpr typename _ParseContext::iterator
2046 parse(_ParseContext& __pc)
2047 { return _M_f._M_parse(__pc, __format::_ZonedDateTime); }
2049 template<typename _FormatContext>
2050 typename _FormatContext::iterator
2051 format(const chrono::gps_time<_Duration>& __t,
2052 _FormatContext& __fc) const
2054 // Convert to __local_time_fmt with abbrev "GPS
" and offset 0s.
2056 // Offset is 1980y/January/Sunday[1] - 1970y/January/1
2057 constexpr chrono::days __gps_offset = chrono::days(3657);
2058 using _CDur = common_type_t<_Duration, chrono::days>;
2059 chrono::local_time<_CDur> __lt(__t.time_since_epoch() + __gps_offset);
2060 const string __abbrev("GPS
", 3);
2061 const chrono::seconds __off = 0s;
2062 const auto __lf = chrono::local_time_format(__lt, &__abbrev, &__off);
2063 return _M_f._M_format(__lf, __fc);
2067 __format::__formatter_chrono<_CharT> _M_f;
2070 template<typename _Duration, typename _CharT>
2071 struct formatter<chrono::file_time<_Duration>, _CharT>
2073 template<typename _ParseContext>
2074 constexpr typename _ParseContext::iterator
2075 parse(_ParseContext& __pc)
2076 { return _M_f._M_parse(__pc, __format::_ZonedDateTime); }
2078 template<typename _FormatContext>
2079 typename _FormatContext::iterator
2080 format(const chrono::file_time<_Duration>& __t,
2081 _FormatContext& __ctx) const
2083 using namespace chrono;
2084 return _M_f._M_format(chrono::clock_cast<system_clock>(__t), __ctx);
2088 __format::__formatter_chrono<_CharT> _M_f;
2091 template<typename _Duration, typename _CharT>
2092 struct formatter<chrono::local_time<_Duration>, _CharT>
2094 template<typename _ParseContext>
2095 constexpr typename _ParseContext::iterator
2096 parse(_ParseContext& __pc)
2097 { return _M_f._M_parse(__pc, __format::_DateTime); }
2099 template<typename _FormatContext>
2100 typename _FormatContext::iterator
2101 format(const chrono::local_time<_Duration>& __t,
2102 _FormatContext& __ctx) const
2103 { return _M_f._M_format(__t, __ctx); }
2106 __format::__formatter_chrono<_CharT> _M_f;
2109 template<typename _Duration, typename _CharT>
2110 struct formatter<chrono::__detail::__local_time_fmt<_Duration>, _CharT>
2112 template<typename _ParseContext>
2113 constexpr typename _ParseContext::iterator
2114 parse(_ParseContext& __pc)
2115 { return _M_f._M_parse(__pc, __format::_ZonedDateTime); }
2117 template<typename _FormatContext>
2118 typename _FormatContext::iterator
2119 format(const chrono::__detail::__local_time_fmt<_Duration>& __t,
2120 _FormatContext& __ctx) const
2121 { return _M_f._M_format(__t, __ctx); }
2124 __format::__formatter_chrono<_CharT> _M_f;
2127 #if _GLIBCXX_USE_CXX11_ABI || ! _GLIBCXX_USE_DUAL_ABI
2128 template<typename _Duration, typename _TimeZonePtr, typename _CharT>
2129 struct formatter<chrono::zoned_time<_Duration, _TimeZonePtr>, _CharT>
2130 : formatter<chrono::__detail::__local_time_fmt<_Duration>, _CharT>
2132 template<typename _FormatContext>
2133 typename _FormatContext::iterator
2134 format(const chrono::zoned_time<_Duration, _TimeZonePtr>& __tp,
2135 _FormatContext& __ctx) const
2137 using chrono::__detail::__local_time_fmt;
2138 using _Base = formatter<__local_time_fmt<_Duration>, _CharT>;
2139 const chrono::sys_info __info = __tp.get_info();
2140 const auto __lf = chrono::local_time_format(__tp.get_local_time(),
2143 return _Base::format(__lf, __ctx);
2148 // Partial specialization needed for %c formatting of __utc_leap_second.
2149 template<typename _Duration, typename _CharT>
2150 struct formatter<chrono::__detail::__utc_leap_second<_Duration>, _CharT>
2151 : formatter<chrono::utc_time<_Duration>, _CharT>
2153 template<typename _FormatContext>
2154 typename _FormatContext::iterator
2155 format(const chrono::__detail::__utc_leap_second<_Duration>& __t,
2156 _FormatContext& __fc) const
2157 { return this->_M_f._M_format(__t, __fc); }
2168 template<typename _Duration = seconds>
2171 static_assert(is_same_v<common_type_t<_Duration, seconds>, _Duration>);
2174 _Parser(__format::_ChronoParts __need) : _M_need(__need) { }
2176 _Parser(_Parser&&) = delete;
2177 void operator=(_Parser&&) = delete;
2179 _Duration _M_time{}; // since midnight
2180 sys_days _M_sys_days{};
2181 year_month_day _M_ymd{};
2183 __format::_ChronoParts _M_need;
2184 unsigned _M_is_leap_second : 1 {};
2185 unsigned _M_reserved : 15 {};
2187 template<typename _CharT, typename _Traits, typename _Alloc>
2188 basic_istream<_CharT, _Traits>&
2189 operator()(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt,
2190 basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr,
2191 minutes* __offset = nullptr);
2194 // Read an unsigned integer from the stream and return it.
2195 // Extract no more than __n digits. Set failbit if an integer isn't read.
2196 template<typename _CharT, typename _Traits>
2197 static int_least32_t
2198 _S_read_unsigned(basic_istream<_CharT, _Traits>& __is,
2199 ios_base::iostate& __err, int __n)
2201 int_least32_t __val = _S_try_read_digit(__is, __err);
2202 if (__val == -1) [[unlikely]]
2203 __err |= ios_base::failbit;
2206 int __n1 = (std::min)(__n, 9);
2207 // Cannot overflow __val unless we read more than 9 digits
2208 for (int __i = 1; __i < __n1; ++__i)
2209 if (auto __dig = _S_try_read_digit(__is, __err); __dig != -1)
2215 while (__n1++ < __n) [[unlikely]]
2216 if (auto __dig = _S_try_read_digit(__is, __err); __dig != -1)
2218 if (__builtin_mul_overflow(__val, 10, &__val)
2219 || __builtin_add_overflow(__val, __dig, &__val))
2221 __err |= ios_base::failbit;
2229 // Read an unsigned integer from the stream and return it.
2230 // Extract no more than __n digits. Set failbit if an integer isn't read.
2231 template<typename _CharT, typename _Traits>
2232 static int_least32_t
2233 _S_read_signed(basic_istream<_CharT, _Traits>& __is,
2234 ios_base::iostate& __err, int __n)
2236 auto __sign = __is.peek();
2237 if (__sign == '-' || __sign == '+')
2239 int_least32_t __val = _S_read_unsigned(__is, __err, __n);
2240 if (__err & ios_base::failbit)
2242 if (__sign == '-') [[unlikely]]
2248 // Read a digit from the stream and return it, or return -1.
2249 // If no digit is read eofbit will be set (but not failbit).
2250 template<typename _CharT, typename _Traits>
2251 static int_least32_t
2252 _S_try_read_digit(basic_istream<_CharT, _Traits>& __is,
2253 ios_base::iostate& __err)
2255 int_least32_t __val = -1;
2256 auto __i = __is.peek();
2257 if (!_Traits::eq_int_type(__i, _Traits::eof())) [[likely]]
2259 _CharT __c = _Traits::to_char_type(__i);
2260 if (_CharT('0') <= __c && __c <= _CharT('9')) [[likely]]
2263 __val = __c - _CharT('0');
2267 __err |= ios_base::eofbit;
2271 // Read the specified character and return true.
2272 // If the character is not found, set failbit and return false.
2273 template<typename _CharT, typename _Traits>
2275 _S_read_chr(basic_istream<_CharT, _Traits>& __is,
2276 ios_base::iostate& __err, _CharT __c)
2278 auto __i = __is.peek();
2279 if (_Traits::eq_int_type(__i, _Traits::eof()))
2280 __err |= ios_base::eofbit;
2281 else if (_Traits::to_char_type(__i) == __c) [[likely]]
2286 __err |= ios_base::failbit;
2291 template<typename _Duration>
2292 using _Parser_t = _Parser<common_type_t<_Duration, seconds>>;
2294 } // namespace __detail
2297 template<typename _CharT, typename _Traits, typename _Rep, typename _Period,
2298 typename _Alloc = allocator<_CharT>>
2299 inline basic_istream<_CharT, _Traits>&
2300 from_stream(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt,
2301 duration<_Rep, _Period>& __d,
2302 basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr,
2303 minutes* __offset = nullptr)
2305 auto __need = __format::_ChronoParts::_TimeOfDay;
2306 __detail::_Parser_t<duration<_Rep, _Period>> __p(__need);
2307 if (__p(__is, __fmt, __abbrev, __offset))
2308 __d = chrono::duration_cast<duration<_Rep, _Period>>(__p._M_time);
2312 template<typename _CharT, typename _Traits>
2313 inline basic_ostream<_CharT, _Traits>&
2314 operator<<(basic_ostream<_CharT, _Traits>& __os, const day& __d)
2316 using _Ctx = __format::__format_context<_CharT>;
2317 using _Str = basic_string_view<_CharT>;
2318 _Str __s = _GLIBCXX_WIDEN("{:02d} is not a valid day
");
2320 __s = __s.substr(0, 6);
2321 auto __u = (unsigned)__d;
2322 __os << std::vformat(__s, make_format_args<_Ctx>(__u));
2326 template<typename _CharT, typename _Traits,
2327 typename _Alloc = allocator<_CharT>>
2328 inline basic_istream<_CharT, _Traits>&
2329 from_stream(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt,
2331 basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr,
2332 minutes* __offset = nullptr)
2334 __detail::_Parser<> __p(__format::_ChronoParts::_Day);
2335 if (__p(__is, __fmt, __abbrev, __offset))
2336 __d = __p._M_ymd.day();
2340 template<typename _CharT, typename _Traits>
2341 inline basic_ostream<_CharT, _Traits>&
2342 operator<<(basic_ostream<_CharT, _Traits>& __os, const month& __m)
2344 using _Ctx = __format::__format_context<_CharT>;
2345 using _Str = basic_string_view<_CharT>;
2346 _Str __s = _GLIBCXX_WIDEN("{:L%b}{} is not a valid month
");
2348 __os << std::vformat(__os.getloc(), __s.substr(0, 6),
2349 make_format_args<_Ctx>(__m));
2352 auto __u = (unsigned)__m;
2353 __os << std::vformat(__s.substr(6), make_format_args<_Ctx>(__u));
2358 template<typename _CharT, typename _Traits,
2359 typename _Alloc = allocator<_CharT>>
2360 inline basic_istream<_CharT, _Traits>&
2361 from_stream(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt,
2363 basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr,
2364 minutes* __offset = nullptr)
2366 __detail::_Parser<> __p(__format::_ChronoParts::_Month);
2367 if (__p(__is, __fmt, __abbrev, __offset))
2368 __m = __p._M_ymd.month();
2372 template<typename _CharT, typename _Traits>
2373 inline basic_ostream<_CharT, _Traits>&
2374 operator<<(basic_ostream<_CharT, _Traits>& __os, const year& __y)
2376 using _Ctx = __format::__format_context<_CharT>;
2377 using _Str = basic_string_view<_CharT>;
2378 _Str __s = _GLIBCXX_WIDEN("-{:04d} is not a valid year
");
2380 __s = __s.substr(0, 7);
2382 if (__i >= 0) [[likely]]
2383 __s.remove_prefix(1);
2386 __os << std::vformat(__s, make_format_args<_Ctx>(__i));
2390 template<typename _CharT, typename _Traits,
2391 typename _Alloc = allocator<_CharT>>
2392 inline basic_istream<_CharT, _Traits>&
2393 from_stream(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt,
2395 basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr,
2396 minutes* __offset = nullptr)
2398 __detail::_Parser<> __p(__format::_ChronoParts::_Year);
2399 if (__p(__is, __fmt, __abbrev, __offset))
2400 __y = __p._M_ymd.year();
2404 template<typename _CharT, typename _Traits>
2405 inline basic_ostream<_CharT, _Traits>&
2406 operator<<(basic_ostream<_CharT, _Traits>& __os, const weekday& __wd)
2408 using _Ctx = __format::__format_context<_CharT>;
2409 using _Str = basic_string_view<_CharT>;
2410 _Str __s = _GLIBCXX_WIDEN("{:L%a}{} is not a valid weekday
");
2412 __os << std::vformat(__os.getloc(), __s.substr(0, 6),
2413 make_format_args<_Ctx>(__wd));
2416 auto __c = __wd.c_encoding();
2417 __os << std::vformat(__s.substr(6), make_format_args<_Ctx>(__c));
2422 template<typename _CharT, typename _Traits,
2423 typename _Alloc = allocator<_CharT>>
2424 inline basic_istream<_CharT, _Traits>&
2425 from_stream(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt,
2427 basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr,
2428 minutes* __offset = nullptr)
2430 __detail::_Parser<> __p(__format::_ChronoParts::_Weekday);
2431 if (__p(__is, __fmt, __abbrev, __offset))
2436 template<typename _CharT, typename _Traits>
2437 inline basic_ostream<_CharT, _Traits>&
2438 operator<<(basic_ostream<_CharT, _Traits>& __os,
2439 const weekday_indexed& __wdi)
2441 // The standard says to format wdi.weekday() and wdi.index() using
2442 // either "{:L}[{}]
" or "{:L}[{} is not a valid index]
". The {:L} spec
2443 // means to format the weekday using ostringstream, so just do that.
2444 basic_stringstream<_CharT> __os2;
2445 __os2.imbue(__os.getloc());
2446 __os2 << __wdi.weekday();
2447 const auto __i = __wdi.index();
2448 basic_string_view<_CharT> __s
2449 = _GLIBCXX_WIDEN("[ is not a valid index]
");
2451 __os2 << std::format(_GLIBCXX_WIDEN("{}
"), __i);
2452 if (__i >= 1 && __i <= 5)
2453 __os2 << __s.back();
2455 __os2 << __s.substr(1);
2456 __os << __os2.view();
2460 template<typename _CharT, typename _Traits>
2461 inline basic_ostream<_CharT, _Traits>&
2462 operator<<(basic_ostream<_CharT, _Traits>& __os,
2463 const weekday_last& __wdl)
2465 // As above, just write straight to a stringstream, as if by "{:L}[last]
"
2466 basic_stringstream<_CharT> __os2;
2467 __os2.imbue(__os.getloc());
2468 __os2 << __wdl.weekday() << _GLIBCXX_WIDEN("[last]
");
2469 __os << __os2.view();
2473 template<typename _CharT, typename _Traits>
2474 inline basic_ostream<_CharT, _Traits>&
2475 operator<<(basic_ostream<_CharT, _Traits>& __os, const month_day& __md)
2477 // As above, just write straight to a stringstream, as if by "{:L}/{}
"
2478 basic_stringstream<_CharT> __os2;
2479 __os2.imbue(__os.getloc());
2480 __os2 << __md.month();
2481 if constexpr (is_same_v<_CharT, char>)
2485 __os2 << __md.day();
2486 __os << __os2.view();
2490 template<typename _CharT, typename _Traits,
2491 typename _Alloc = allocator<_CharT>>
2492 inline basic_istream<_CharT, _Traits>&
2493 from_stream(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt,
2495 basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr,
2496 minutes* __offset = nullptr)
2498 using __format::_ChronoParts;
2499 auto __need = _ChronoParts::_Month | _ChronoParts::_Day;
2500 __detail::_Parser<> __p(__need);
2501 if (__p(__is, __fmt, __abbrev, __offset))
2502 __md = month_day(__p._M_ymd.month(), __p._M_ymd.day());
2506 template<typename _CharT, typename _Traits>
2507 inline basic_ostream<_CharT, _Traits>&
2508 operator<<(basic_ostream<_CharT, _Traits>& __os,
2509 const month_day_last& __mdl)
2511 // As above, just write straight to a stringstream, as if by "{:L}/last
"
2512 basic_stringstream<_CharT> __os2;
2513 __os2.imbue(__os.getloc());
2514 __os2 << __mdl.month() << _GLIBCXX_WIDEN("/last
");
2515 __os << __os2.view();
2519 template<typename _CharT, typename _Traits>
2520 inline basic_ostream<_CharT, _Traits>&
2521 operator<<(basic_ostream<_CharT, _Traits>& __os,
2522 const month_weekday& __mwd)
2524 // As above, just write straight to a stringstream, as if by "{:L}/{:L}
"
2525 basic_stringstream<_CharT> __os2;
2526 __os2.imbue(__os.getloc());
2527 __os2 << __mwd.month();
2528 if constexpr (is_same_v<_CharT, char>)
2532 __os2 << __mwd.weekday_indexed();
2533 __os << __os2.view();
2537 template<typename _CharT, typename _Traits>
2538 inline basic_ostream<_CharT, _Traits>&
2539 operator<<(basic_ostream<_CharT, _Traits>& __os,
2540 const month_weekday_last& __mwdl)
2542 // As above, just write straight to a stringstream, as if by "{:L}/{:L}
"
2543 basic_stringstream<_CharT> __os2;
2544 __os2.imbue(__os.getloc());
2545 __os2 << __mwdl.month();
2546 if constexpr (is_same_v<_CharT, char>)
2550 __os2 << __mwdl.weekday_last();
2551 __os << __os2.view();
2555 template<typename _CharT, typename _Traits>
2556 inline basic_ostream<_CharT, _Traits>&
2557 operator<<(basic_ostream<_CharT, _Traits>& __os, const year_month& __ym)
2559 // As above, just write straight to a stringstream, as if by "{}/{:L}
"
2560 basic_stringstream<_CharT> __os2;
2561 __os2.imbue(__os.getloc());
2562 __os2 << __ym.year();
2563 if constexpr (is_same_v<_CharT, char>)
2567 __os2 << __ym.month();
2568 __os << __os2.view();
2572 template<typename _CharT, typename _Traits,
2573 typename _Alloc = allocator<_CharT>>
2574 inline basic_istream<_CharT, _Traits>&
2575 from_stream(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt,
2577 basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr,
2578 minutes* __offset = nullptr)
2580 using __format::_ChronoParts;
2581 auto __need = _ChronoParts::_Year | _ChronoParts::_Month;
2582 __detail::_Parser<> __p(__need);
2583 if (__p(__is, __fmt, __abbrev, __offset))
2584 __ym = year_month(__p._M_ymd.year(), __p._M_ymd.month());
2588 template<typename _CharT, typename _Traits>
2589 inline basic_ostream<_CharT, _Traits>&
2590 operator<<(basic_ostream<_CharT, _Traits>& __os,
2591 const year_month_day& __ymd)
2593 using _Ctx = __format::__format_context<_CharT>;
2594 using _Str = basic_string_view<_CharT>;
2595 _Str __s = _GLIBCXX_WIDEN("{:%F} is not a valid date
");
2596 __os << std::vformat(__ymd.ok() ? __s.substr(0, 5) : __s,
2597 make_format_args<_Ctx>(__ymd));
2601 template<typename _CharT, typename _Traits,
2602 typename _Alloc = allocator<_CharT>>
2603 inline basic_istream<_CharT, _Traits>&
2604 from_stream(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt,
2605 year_month_day& __ymd,
2606 basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr,
2607 minutes* __offset = nullptr)
2609 using __format::_ChronoParts;
2610 auto __need = _ChronoParts::_Year | _ChronoParts::_Month
2611 | _ChronoParts::_Day;
2612 __detail::_Parser<> __p(__need);
2613 if (__p(__is, __fmt, __abbrev, __offset))
2618 template<typename _CharT, typename _Traits>
2619 inline basic_ostream<_CharT, _Traits>&
2620 operator<<(basic_ostream<_CharT, _Traits>& __os,
2621 const year_month_day_last& __ymdl)
2623 // As above, just write straight to a stringstream, as if by "{}/{:L}
"
2624 basic_stringstream<_CharT> __os2;
2625 __os2.imbue(__os.getloc());
2626 __os2 << __ymdl.year();
2627 if constexpr (is_same_v<_CharT, char>)
2631 __os2 << __ymdl.month_day_last();
2632 __os << __os2.view();
2636 template<typename _CharT, typename _Traits>
2637 inline basic_ostream<_CharT, _Traits>&
2638 operator<<(basic_ostream<_CharT, _Traits>& __os,
2639 const year_month_weekday& __ymwd)
2641 // As above, just write straight to a stringstream, as if by
2643 basic_stringstream<_CharT> __os2;
2644 __os2.imbue(__os.getloc());
2646 if constexpr (is_same_v<_CharT, char>)
2650 __os2 << __ymwd.year() << __slash << __ymwd.month() << __slash
2651 << __ymwd.weekday_indexed();
2652 __os << __os2.view();
2656 template<typename _CharT, typename _Traits>
2657 inline basic_ostream<_CharT, _Traits>&
2658 operator<<(basic_ostream<_CharT, _Traits>& __os,
2659 const year_month_weekday_last& __ymwdl)
2661 // As above, just write straight to a stringstream, as if by
2663 basic_stringstream<_CharT> __os2;
2664 __os2.imbue(__os.getloc());
2666 if constexpr (is_same_v<_CharT, char>)
2670 __os2 << __ymwdl.year() << __slash << __ymwdl.month() << __slash
2671 << __ymwdl.weekday_last();
2672 __os << __os2.view();
2676 template<typename _CharT, typename _Traits, typename _Duration>
2677 inline basic_ostream<_CharT, _Traits>&
2678 operator<<(basic_ostream<_CharT, _Traits>& __os,
2679 const hh_mm_ss<_Duration>& __hms)
2681 return __os << format(__os.getloc(), _GLIBCXX_WIDEN("{:L%T}
"), __hms);
2684 #if _GLIBCXX_USE_CXX11_ABI || ! _GLIBCXX_USE_DUAL_ABI
2686 template<typename _CharT, typename _Traits>
2687 basic_ostream<_CharT, _Traits>&
2688 operator<<(basic_ostream<_CharT, _Traits>& __os, const sys_info& __i)
2690 __os << '[' << __i.begin << ',' << __i.end
2691 << ',' << hh_mm_ss(__i.offset) << ',' << __i.save
2692 << ',' << __i.abbrev << ']';
2697 template<typename _CharT, typename _Traits>
2698 basic_ostream<_CharT, _Traits>&
2699 operator<<(basic_ostream<_CharT, _Traits>& __os, const local_info& __li)
2702 if (__li.result == local_info::unique)
2706 if (__li.result == local_info::nonexistent)
2707 __os << "nonexistent
";
2709 __os << "ambiguous
";
2710 __os << " local time between
" << __li.first;
2711 __os << " and
" << __li.second;
2717 template<typename _CharT, typename _Traits, typename _Duration,
2718 typename _TimeZonePtr>
2719 inline basic_ostream<_CharT, _Traits>&
2720 operator<<(basic_ostream<_CharT, _Traits>& __os,
2721 const zoned_time<_Duration, _TimeZonePtr>& __t)
2723 __os << format(__os.getloc(), _GLIBCXX_WIDEN("{:L%F %T %Z}
"), __t);
2728 template<typename _CharT, typename _Traits, typename _Duration>
2729 requires (!treat_as_floating_point_v<typename _Duration::rep>)
2730 && ratio_less_v<typename _Duration::period, days::period>
2731 inline basic_ostream<_CharT, _Traits>&
2732 operator<<(basic_ostream<_CharT, _Traits>& __os,
2733 const sys_time<_Duration>& __tp)
2735 __os << std::format(__os.getloc(), _GLIBCXX_WIDEN("{:L%F %T}
"), __tp);
2739 template<typename _CharT, typename _Traits>
2740 inline basic_ostream<_CharT, _Traits>&
2741 operator<<(basic_ostream<_CharT, _Traits>& __os, const sys_days& __dp)
2743 __os << year_month_day{__dp};
2747 template<typename _CharT, typename _Traits, typename _Duration,
2748 typename _Alloc = allocator<_CharT>>
2749 basic_istream<_CharT, _Traits>&
2750 from_stream(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt,
2751 sys_time<_Duration>& __tp,
2752 basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr,
2753 minutes* __offset = nullptr)
2758 using __format::_ChronoParts;
2759 auto __need = _ChronoParts::_Year | _ChronoParts::_Month
2760 | _ChronoParts::_Day | _ChronoParts::_TimeOfDay;
2761 __detail::_Parser_t<_Duration> __p(__need);
2762 if (__p(__is, __fmt, __abbrev, __offset))
2764 if (__p._M_is_leap_second)
2765 __is.setstate(ios_base::failbit);
2768 auto __st = __p._M_sys_days + __p._M_time - *__offset;
2769 __tp = chrono::time_point_cast<_Duration>(__st);
2775 template<typename _CharT, typename _Traits, typename _Duration>
2776 inline basic_ostream<_CharT, _Traits>&
2777 operator<<(basic_ostream<_CharT, _Traits>& __os,
2778 const utc_time<_Duration>& __t)
2780 __os << std::format(__os.getloc(), _GLIBCXX_WIDEN("{:L%F %T}
"), __t);
2784 template<typename _CharT, typename _Traits, typename _Duration,
2785 typename _Alloc = allocator<_CharT>>
2786 inline basic_istream<_CharT, _Traits>&
2787 from_stream(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt,
2788 utc_time<_Duration>& __tp,
2789 basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr,
2790 minutes* __offset = nullptr)
2795 using __format::_ChronoParts;
2796 auto __need = _ChronoParts::_Year | _ChronoParts::_Month
2797 | _ChronoParts::_Day | _ChronoParts::_TimeOfDay;
2798 __detail::_Parser_t<_Duration> __p(__need);
2799 if (__p(__is, __fmt, __abbrev, __offset))
2801 // Converting to utc_time before adding _M_time is necessary for
2802 // "23:59:60
" to correctly produce a time within a leap second.
2803 auto __ut = utc_clock::from_sys(__p._M_sys_days) + __p._M_time
2805 __tp = chrono::time_point_cast<_Duration>(__ut);
2810 template<typename _CharT, typename _Traits, typename _Duration>
2811 inline basic_ostream<_CharT, _Traits>&
2812 operator<<(basic_ostream<_CharT, _Traits>& __os,
2813 const tai_time<_Duration>& __t)
2815 __os << std::format(__os.getloc(), _GLIBCXX_WIDEN("{:L%F %T}
"), __t);
2819 template<typename _CharT, typename _Traits, typename _Duration,
2820 typename _Alloc = allocator<_CharT>>
2821 inline basic_istream<_CharT, _Traits>&
2822 from_stream(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt,
2823 tai_time<_Duration>& __tp,
2824 basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr,
2825 minutes* __offset = nullptr)
2830 using __format::_ChronoParts;
2831 auto __need = _ChronoParts::_Year | _ChronoParts::_Month
2832 | _ChronoParts::_Day | _ChronoParts::_TimeOfDay;
2833 __detail::_Parser_t<_Duration> __p(__need);
2834 if (__p(__is, __fmt, __abbrev, __offset))
2836 if (__p._M_is_leap_second)
2837 __is.setstate(ios_base::failbit);
2840 auto __st = __p._M_sys_days + __p._M_time - *__offset;
2841 auto __tt = tai_clock::from_utc(utc_clock::from_sys(__st));
2842 __tp = chrono::time_point_cast<_Duration>(__tt);
2848 template<typename _CharT, typename _Traits, typename _Duration>
2849 inline basic_ostream<_CharT, _Traits>&
2850 operator<<(basic_ostream<_CharT, _Traits>& __os,
2851 const gps_time<_Duration>& __t)
2853 __os << std::format(__os.getloc(), _GLIBCXX_WIDEN("{:L%F %T}
"), __t);
2857 template<typename _CharT, typename _Traits, typename _Duration,
2858 typename _Alloc = allocator<_CharT>>
2859 inline basic_istream<_CharT, _Traits>&
2860 from_stream(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt,
2861 gps_time<_Duration>& __tp,
2862 basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr,
2863 minutes* __offset = nullptr)
2868 using __format::_ChronoParts;
2869 auto __need = _ChronoParts::_Year | _ChronoParts::_Month
2870 | _ChronoParts::_Day | _ChronoParts::_TimeOfDay;
2871 __detail::_Parser_t<_Duration> __p(__need);
2872 if (__p(__is, __fmt, __abbrev, __offset))
2874 if (__p._M_is_leap_second)
2875 __is.setstate(ios_base::failbit);
2878 auto __st = __p._M_sys_days + __p._M_time - *__offset;
2879 auto __tt = gps_clock::from_utc(utc_clock::from_sys(__st));
2880 __tp = chrono::time_point_cast<_Duration>(__tt);
2886 template<typename _CharT, typename _Traits, typename _Duration>
2887 inline basic_ostream<_CharT, _Traits>&
2888 operator<<(basic_ostream<_CharT, _Traits>& __os,
2889 const file_time<_Duration>& __t)
2891 __os << std::format(__os.getloc(), _GLIBCXX_WIDEN("{:L%F %T}
"), __t);
2895 template<typename _CharT, typename _Traits, typename _Duration,
2896 typename _Alloc = allocator<_CharT>>
2897 inline basic_istream<_CharT, _Traits>&
2898 from_stream(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt,
2899 file_time<_Duration>& __tp,
2900 basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr,
2901 minutes* __offset = nullptr)
2903 sys_time<_Duration> __st;
2904 if (chrono::from_stream(__is, __fmt, __st, __abbrev, __offset))
2905 __tp = chrono::time_point_cast<_Duration>(file_clock::from_sys(__st));
2909 template<typename _CharT, typename _Traits, typename _Duration>
2910 inline basic_ostream<_CharT, _Traits>&
2911 operator<<(basic_ostream<_CharT, _Traits>& __os,
2912 const local_time<_Duration>& __lt)
2914 __os << sys_time<_Duration>{__lt.time_since_epoch()};
2918 template<typename _CharT, typename _Traits, typename _Duration,
2919 typename _Alloc = allocator<_CharT>>
2920 basic_istream<_CharT, _Traits>&
2921 from_stream(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt,
2922 local_time<_Duration>& __tp,
2923 basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr,
2924 minutes* __offset = nullptr)
2926 using __format::_ChronoParts;
2927 auto __need = _ChronoParts::_Year | _ChronoParts::_Month
2928 | _ChronoParts::_Day | _ChronoParts::_TimeOfDay;
2929 __detail::_Parser_t<_Duration> __p(__need);
2930 if (__p(__is, __fmt, __abbrev, __offset))
2932 days __d = __p._M_sys_days.time_since_epoch();
2933 auto __t = local_days(__d) + __p._M_time; // ignore offset
2934 __tp = chrono::time_point_cast<_Duration>(__t);
2939 // [time.parse] parsing
2943 template<typename _Parsable, typename _CharT,
2944 typename _Traits = std::char_traits<_CharT>,
2945 typename... _OptArgs>
2946 concept __parsable = requires (basic_istream<_CharT, _Traits>& __is,
2947 const _CharT* __fmt, _Parsable& __tp,
2948 _OptArgs*... __args)
2949 { from_stream(__is, __fmt, __tp, __args...); };
2951 template<typename _Parsable, typename _CharT,
2952 typename _Traits = char_traits<_CharT>,
2953 typename _Alloc = allocator<_CharT>>
2957 using __string_type = basic_string<_CharT, _Traits, _Alloc>;
2960 _Parse(const _CharT* __fmt, _Parsable& __tp,
2961 basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr,
2962 minutes* __offset = nullptr)
2963 : _M_fmt(__fmt), _M_tp(std::__addressof(__tp)),
2964 _M_abbrev(__abbrev), _M_offset(__offset)
2967 _Parse(_Parse&&) = delete;
2968 _Parse& operator=(_Parse&&) = delete;
2971 using __stream_type = basic_istream<_CharT, _Traits>;
2973 const _CharT* const _M_fmt;
2974 _Parsable* const _M_tp;
2975 __string_type* const _M_abbrev;
2976 minutes* const _M_offset;
2978 friend __stream_type&
2979 operator>>(__stream_type& __is, _Parse&& __p)
2982 from_stream(__is, __p._M_fmt, *__p._M_tp, __p._M_abbrev,
2984 else if (__p._M_abbrev)
2985 from_stream(__is, __p._M_fmt, *__p._M_tp, __p._M_abbrev);
2987 from_stream(__is, __p._M_fmt, *__p._M_tp);
2991 friend void operator>>(__stream_type&, _Parse&) = delete;
2992 friend void operator>>(__stream_type&, const _Parse&) = delete;
2994 } // namespace __detail
2996 template<typename _CharT, __detail::__parsable<_CharT> _Parsable>
2997 [[nodiscard, __gnu__::__access__(__read_only__, 1)]]
2999 parse(const _CharT* __fmt, _Parsable& __tp)
3000 { return __detail::_Parse<_Parsable, _CharT>(__fmt, __tp); }
3002 template<typename _CharT, typename _Traits, typename _Alloc,
3003 __detail::__parsable<_CharT, _Traits> _Parsable>
3006 parse(const basic_string<_CharT, _Traits, _Alloc>& __fmt, _Parsable& __tp)
3008 return __detail::_Parse<_Parsable, _CharT, _Traits>(__fmt.c_str(), __tp);
3011 template<typename _CharT, typename _Traits, typename _Alloc,
3012 typename _StrT = basic_string<_CharT, _Traits, _Alloc>,
3013 __detail::__parsable<_CharT, _Traits, _StrT> _Parsable>
3014 [[nodiscard, __gnu__::__access__(__read_only__, 1)]]
3016 parse(const _CharT* __fmt, _Parsable& __tp,
3017 basic_string<_CharT, _Traits, _Alloc>& __abbrev)
3019 auto __pa = std::__addressof(__abbrev);
3020 return __detail::_Parse<_Parsable, _CharT, _Traits, _Alloc>(__fmt, __tp,
3024 template<typename _CharT, typename _Traits, typename _Alloc,
3025 typename _StrT = basic_string<_CharT, _Traits, _Alloc>,
3026 __detail::__parsable<_CharT, _Traits, _StrT> _Parsable>
3029 parse(const basic_string<_CharT, _Traits, _Alloc>& __fmt, _Parsable& __tp,
3030 basic_string<_CharT, _Traits, _Alloc>& __abbrev)
3032 auto __pa = std::__addressof(__abbrev);
3033 return __detail::_Parse<_Parsable, _CharT, _Traits, _Alloc>(__fmt.c_str(),
3037 template<typename _CharT, typename _Traits = char_traits<_CharT>,
3038 typename _StrT = basic_string<_CharT, _Traits>,
3039 __detail::__parsable<_CharT, _Traits, _StrT, minutes> _Parsable>
3040 [[nodiscard, __gnu__::__access__(__read_only__, 1)]]
3042 parse(const _CharT* __fmt, _Parsable& __tp, minutes& __offset)
3044 return __detail::_Parse<_Parsable, _CharT>(__fmt, __tp, nullptr,
3048 template<typename _CharT, typename _Traits, typename _Alloc,
3049 typename _StrT = basic_string<_CharT, _Traits>,
3050 __detail::__parsable<_CharT, _Traits, _StrT, minutes> _Parsable>
3053 parse(const basic_string<_CharT, _Traits, _Alloc>& __fmt, _Parsable& __tp,
3056 return __detail::_Parse<_Parsable, _CharT, _Traits, _Alloc>(__fmt.c_str(),
3061 template<typename _CharT, typename _Traits, typename _Alloc,
3062 typename _StrT = basic_string<_CharT, _Traits, _Alloc>,
3063 __detail::__parsable<_CharT, _Traits, _StrT, minutes> _Parsable>
3064 [[nodiscard, __gnu__::__access__(__read_only__, 1)]]
3066 parse(const _CharT* __fmt, _Parsable& __tp,
3067 basic_string<_CharT, _Traits, _Alloc>& __abbrev, minutes& __offset)
3069 auto __pa = std::__addressof(__abbrev);
3070 return __detail::_Parse<_Parsable, _CharT, _Traits, _Alloc>(__fmt, __tp,
3075 template<typename _CharT, typename _Traits, typename _Alloc,
3076 typename _StrT = basic_string<_CharT, _Traits, _Alloc>,
3077 __detail::__parsable<_CharT, _Traits, _StrT, minutes> _Parsable>
3080 parse(const basic_string<_CharT, _Traits, _Alloc>& __fmt, _Parsable& __tp,
3081 basic_string<_CharT, _Traits, _Alloc>& __abbrev, minutes& __offset)
3083 auto __pa = std::__addressof(__abbrev);
3084 return __detail::_Parse<_Parsable, _CharT, _Traits, _Alloc>(__fmt.c_str(),
3090 template<typename _Duration>
3091 template<typename _CharT, typename _Traits, typename _Alloc>
3092 basic_istream<_CharT, _Traits>&
3093 __detail::_Parser<_Duration>::
3094 operator()(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt,
3095 basic_string<_CharT, _Traits, _Alloc>* __abbrev,
3098 using sentry = typename basic_istream<_CharT, _Traits>::sentry;
3099 ios_base::iostate __err = ios_base::goodbit;
3100 if (sentry __cerb(__is, true); __cerb)
3102 locale __loc = __is.getloc();
3103 auto& __tmget = std::use_facet<std::time_get<_CharT>>(__loc);
3104 auto& __tmpunct = std::use_facet<std::__timepunct<_CharT>>(__loc);
3106 // RAII type to save and restore stream state.
3107 struct _Stream_state
3110 _Stream_state(basic_istream<_CharT, _Traits>& __i)
3112 _M_flags(__i.flags(ios_base::skipws | ios_base::dec)),
3118 _M_is.flags(_M_flags);
3122 _Stream_state(_Stream_state&&) = delete;
3124 basic_istream<_CharT, _Traits>& _M_is;
3125 ios_base::fmtflags _M_flags;
3129 auto __is_failed = [](ios_base::iostate __e) {
3130 return static_cast<bool>(__e & ios_base::failbit);
3133 // Read an unsigned integer from the stream and return it.
3134 // Extract no more than __n digits. Set __err on error.
3135 auto __read_unsigned = [&] (int __n) {
3136 return _S_read_unsigned(__is, __err, __n);
3139 // Read a signed integer from the stream and return it.
3140 // Extract no more than __n digits. Set __err on error.
3141 auto __read_signed = [&] (int __n) {
3142 return _S_read_signed(__is, __err, __n);
3145 // Read an expected character from the stream.
3146 auto __read_chr = [&__is, &__err] (_CharT __c) {
3147 return _S_read_chr(__is, __err, __c);
3150 using __format::_ChronoParts;
3151 _ChronoParts __parts{};
3153 const year __bad_y = --year::min(); // SHRT_MIN
3154 const month __bad_mon(255);
3155 const day __bad_day(255);
3156 const weekday __bad_wday(255);
3157 const hours __bad_h(-1);
3158 const minutes __bad_min(-9999);
3159 const seconds __bad_sec(-1);
3161 year __y = __bad_y, __yy = __bad_y; // %Y, %yy
3162 year __iso_y = __bad_y, __iso_yy = __bad_y; // %G, %g
3163 month __m = __bad_mon; // %m
3164 day __d = __bad_day; // %d
3165 weekday __wday = __bad_wday; // %a %A %u %w
3166 hours __h = __bad_h, __h12 = __bad_h; // %H, %I
3167 minutes __min = __bad_min; // %M
3168 _Duration __s = __bad_sec; // %S
3169 int __ampm = 0; // %p
3170 int __iso_wk = -1, __sunday_wk = -1, __monday_wk = -1; // %V, %U, %W
3171 int __century = -1; // %C
3172 int __dayofyear = -1; // %j (for non-duration)
3174 minutes __tz_offset = __bad_min;
3175 basic_string<_CharT, _Traits> __tz_abbr;
3177 if ((_M_need & _ChronoParts::_TimeOfDay)
3178 && (_M_need & _ChronoParts::_Year))
3180 // For time_points assume "00:00:00
" is implicitly present,
3181 // so we don't fail to parse if it's not (PR libstdc++/114240).
3182 // We will still fail to parse if there's no year+month+day.
3184 __parts = _ChronoParts::_TimeOfDay;
3187 // bool __is_neg = false; // TODO: how is this handled for parsing?
3189 _CharT __mod{}; // One of 'E' or 'O' or nul.
3190 unsigned __num = 0; // Non-zero for N modifier.
3191 bool __is_flag = false; // True if we're processing a % flag.
3193 constexpr bool __is_floating
3194 = treat_as_floating_point_v<typename _Duration::rep>;
3196 // If an out-of-range value is extracted (e.g. 61min for %M),
3197 // do not set failbit immediately because we might not need it
3198 // (e.g. parsing chrono::year doesn't care about invalid %M values).
3199 // Instead set the variable back to its initial 'bad' state,
3200 // and also set related variables corresponding to the same field
3201 // (e.g. a bad %M value for __min should also reset __h and __s).
3202 // If a valid value is needed later the bad value will cause failure.
3204 // For some fields we don't know the correct range when parsing and
3205 // we have to be liberal in what we accept, e.g. we allow 366 for
3206 // day-of-year because that's valid in leap years, and we allow 31
3207 // for day-of-month. If those values are needed to determine the
3208 // result then we can do a correct range check at the end when we
3209 // know the how many days the relevant year or month actually has.
3213 _CharT __c = *__fmt++;
3217 __is_flag = true; // This is the start of a flag.
3218 else if (std::isspace(__c, __loc))
3219 std::ws(__is); // Match zero or more whitespace characters.
3220 else if (!__read_chr(__c)) [[unlikely]]
3221 break; // Failed to match the expected character.
3223 continue; // Process next character in the format string.
3226 // Now processing a flag.
3229 case 'a': // Locale's weekday name
3230 case 'A': // (full or abbreviated, matched case-insensitively).
3231 if (__mod || __num) [[unlikely]]
3232 __err = ios_base::failbit;
3236 __tmget.get(__is, {}, __is, __err, &__tm,
3238 if (!__is_failed(__err))
3239 __wday = weekday(__tm.tm_wday);
3241 __parts |= _ChronoParts::_Weekday;
3244 case 'b': // Locale's month name
3245 case 'h': // (full or abbreviated, matched case-insensitively).
3247 if (__mod || __num) [[unlikely]]
3248 __err = ios_base::failbit;
3251 // strptime behaves differently for %b and %B,
3252 // but chrono::parse says they're equivalent.
3253 // Luckily libstdc++ std::time_get works as needed.
3255 __tmget.get(__is, {}, __is, __err, &__tm,
3257 if (!__is_failed(__err))
3258 __m = month(__tm.tm_mon + 1);
3260 __parts |= _ChronoParts::_Month;
3263 case 'c': // Locale's date and time representation.
3264 if (__mod == 'O' || __num) [[unlikely]]
3265 __err |= ios_base::failbit;
3269 __tmget.get(__is, {}, __is, __err, &__tm,
3270 __fmt - 2 - (__mod == 'E'), __fmt);
3271 if (!__is_failed(__err))
3273 __y = year(__tm.tm_year + 1900);
3274 __m = month(__tm.tm_mon + 1);
3275 __d = day(__tm.tm_mday);
3276 __h = hours(__tm.tm_hour);
3277 __min = minutes(__tm.tm_min);
3278 __s = seconds(__tm.tm_sec);
3281 __parts |= _ChronoParts::_DateTime;
3284 case 'C': // Century
3285 if (!__mod) [[likely]]
3287 auto __v = __read_signed(__num ? __num : 2);
3288 if (!__is_failed(__err))
3290 int __cmin = (int)year::min() / 100;
3291 int __cmax = (int)year::max() / 100;
3292 if (__cmin <= __v && __v <= __cmax)
3293 __century = __v * 100;
3295 __century = -2; // This prevents guessing century.
3298 else if (__mod == 'E')
3301 __tmget.get(__is, {}, __is, __err, &__tm,
3303 if (!__is_failed(__err))
3304 __century = __tm.tm_year;
3307 __err |= ios_base::failbit;
3308 // N.B. don't set this here: __parts |= _ChronoParts::_Year;
3311 case 'd': // Day of month (1-31)
3313 if (!__mod) [[likely]]
3315 auto __v = __read_unsigned(__num ? __num : 2);
3316 if (!__is_failed(__err))
3319 else if (__mod == 'O')
3322 __tmget.get(__is, {}, __is, __err, &__tm,
3324 if (!__is_failed(__err))
3325 __d = day(__tm.tm_mday);
3328 __err |= ios_base::failbit;
3329 __parts |= _ChronoParts::_Day;
3332 case 'D': // %m/%d/%y
3333 if (__mod || __num) [[unlikely]]
3334 __err |= ios_base::failbit;
3337 auto __month = __read_unsigned(2); // %m
3339 auto __day = __read_unsigned(2); // %d
3341 auto __year = __read_unsigned(2); // %y
3342 if (__is_failed(__err))
3344 __y = year(__year + 1900 + 100 * int(__year < 69));
3345 __m = month(__month);
3347 if (!year_month_day(__y, __m, __d).ok())
3349 __y = __yy = __iso_y = __iso_yy = __bad_y;
3355 __parts |= _ChronoParts::_Date;
3358 case 'F': // %Y-%m-%d - any N modifier only applies to %Y.
3359 if (__mod) [[unlikely]]
3360 __err |= ios_base::failbit;
3363 auto __year = __read_signed(__num ? __num : 4); // %Y
3365 auto __month = __read_unsigned(2); // %m
3367 auto __day = __read_unsigned(2); // %d
3368 if (__is_failed(__err))
3371 __m = month(__month);
3373 if (!year_month_day(__y, __m, __d).ok())
3375 __y = __yy = __iso_y = __iso_yy = __bad_y;
3381 __parts |= _ChronoParts::_Date;
3384 case 'g': // Last two digits of ISO week-based year.
3385 if (__mod) [[unlikely]]
3386 __err |= ios_base::failbit;
3389 auto __val = __read_unsigned(__num ? __num : 2);
3390 if (__val >= 0 && __val <= 99)
3392 __iso_yy = year(__val);
3393 if (__century == -1) // No %C has been parsed yet.
3397 __iso_yy = __iso_y = __y = __yy = __bad_y;
3399 __parts |= _ChronoParts::_Year;
3402 case 'G': // ISO week-based year.
3403 if (__mod) [[unlikely]]
3404 __err |= ios_base::failbit;
3406 __iso_y = year(__read_unsigned(__num ? __num : 4));
3407 __parts |= _ChronoParts::_Year;
3410 case 'H': // 24-hour (00-23)
3411 case 'I': // 12-hour (1-12)
3412 if (__mod == 'E') [[unlikely]]
3413 __err |= ios_base::failbit;
3414 else if (__mod == 'O')
3419 __tmget.get(__is, {}, __is, __err, &__tm,
3421 if (!__is_failed(__err))
3425 __h12 = hours(__tm.tm_hour);
3429 __h = hours(__tm.tm_hour);
3432 // XXX %OI seems to be unimplementable.
3433 __err |= ios_base::failbit;
3438 auto __val = __read_unsigned(__num ? __num : 2);
3439 if (__c == 'I' && __val >= 1 && __val <= 12)
3441 __h12 = hours(__val);
3444 else if (__c == 'H' && __val >= 0 && __val <= 23)
3451 if (_M_need & _ChronoParts::_TimeOfDay)
3452 __err |= ios_base::failbit;
3456 __parts |= _ChronoParts::_TimeOfDay;
3459 case 'j': // For duration, count of days, otherwise day of year
3460 if (__mod) [[unlikely]]
3461 __err |= ios_base::failbit;
3462 else if (_M_need == _ChronoParts::_TimeOfDay) // duration
3464 auto __val = __read_signed(__num ? __num : 3);
3465 if (!__is_failed(__err))
3467 __h = days(__val); // __h will get added to _M_time
3468 __parts |= _ChronoParts::_TimeOfDay;
3473 __dayofyear = __read_unsigned(__num ? __num : 3);
3474 // N.B. do not alter __parts here, done after loop.
3475 // No need for range checking here either.
3479 case 'm': // Month (1-12)
3480 if (__mod == 'E') [[unlikely]]
3481 __err |= ios_base::failbit;
3482 else if (__mod == 'O')
3485 __tmget.get(__is, {}, __is, __err, &__tm,
3487 if (!__is_failed(__err))
3488 __m = month(__tm.tm_mon + 1);
3492 auto __val = __read_unsigned(__num ? __num : 2);
3493 if (__val >= 1 && __val <= 12)
3498 __parts |= _ChronoParts::_Month;
3501 case 'M': // Minutes
3502 if (__mod == 'E') [[unlikely]]
3503 __err |= ios_base::failbit;
3504 else if (__mod == 'O')
3507 __tmget.get(__is, {}, __is, __err, &__tm,
3509 if (!__is_failed(__err))
3510 __min = minutes(__tm.tm_min);
3514 auto __val = __read_unsigned(__num ? __num : 2);
3515 if (0 <= __val && __val < 60)
3516 __min = minutes(__val);
3519 if (_M_need & _ChronoParts::_TimeOfDay)
3520 __err |= ios_base::failbit;
3524 __parts |= _ChronoParts::_TimeOfDay;
3527 case 'p': // Locale's AM/PM designation for 12-hour clock.
3529 __err |= ios_base::failbit;
3532 // Can't use std::time_get here as it can't parse %p
3533 // in isolation without %I. This might be faster anyway.
3534 const _CharT* __ampms[2];
3535 __tmpunct._M_am_pm(__ampms);
3536 int __n = 0, __which = 3;
3537 while (__which != 0)
3539 auto __i = __is.peek();
3540 if (_Traits::eq_int_type(__i, _Traits::eof()))
3542 __err |= ios_base::eofbit | ios_base::failbit;
3545 __i = std::toupper(_Traits::to_char_type(__i), __loc);
3548 if (__i != std::toupper(__ampms[0][__n], __loc))
3550 else if (__ampms[0][__n + 1] == _CharT())
3559 if (__i != std::toupper(__ampms[1][__n], __loc))
3561 else if (__ampms[1][__n + 1] == _CharT())
3572 if (__which == 0 || __which == 3)
3573 __err |= ios_base::failbit;
3579 case 'r': // Locale's 12-hour time.
3581 __err |= ios_base::failbit;
3585 __tmget.get(__is, {}, __is, __err, &__tm,
3587 if (!__is_failed(__err))
3589 __h = hours(__tm.tm_hour);
3590 __min = minutes(__tm.tm_min);
3591 __s = seconds(__tm.tm_sec);
3594 __parts |= _ChronoParts::_TimeOfDay;
3598 case 'T': // %H:%M:%S
3599 if (__mod || __num) [[unlikely]]
3601 __err |= ios_base::failbit;
3606 auto __val = __read_unsigned(2);
3607 if (__val == -1 || __val > 23) [[unlikely]]
3609 if (_M_need & _ChronoParts::_TimeOfDay)
3610 __err |= ios_base::failbit;
3613 if (!__read_chr(':')) [[unlikely]]
3617 __val = __read_unsigned(2);
3618 if (__val == -1 || __val > 60) [[unlikely]]
3620 if (_M_need & _ChronoParts::_TimeOfDay)
3621 __err |= ios_base::failbit;
3624 __min = minutes(__val);
3628 __parts |= _ChronoParts::_TimeOfDay;
3631 else if (!__read_chr(':')) [[unlikely]]
3636 case 'S': // Seconds
3637 if (__mod == 'E') [[unlikely]]
3638 __err |= ios_base::failbit;
3639 else if (__mod == 'O')
3642 __tmget.get(__is, {}, __is, __err, &__tm,
3644 if (!__is_failed(__err))
3645 __s = seconds(__tm.tm_sec);
3647 else if constexpr (_Duration::period::den == 1
3650 auto __val = __read_unsigned(__num ? __num : 2);
3651 if (0 <= __val && __val <= 59) [[likely]]
3652 __s = seconds(__val);
3655 if (_M_need & _ChronoParts::_TimeOfDay)
3656 __err |= ios_base::failbit;
3660 else // Read fractional seconds
3662 basic_stringstream<_CharT> __buf;
3663 auto __digit = _S_try_read_digit(__is, __err);
3666 __buf.put(_CharT('0') + __digit);
3667 __digit = _S_try_read_digit(__is, __err);
3669 __buf.put(_CharT('0') + __digit);
3672 auto __i = __is.peek();
3673 if (_Traits::eq_int_type(__i, _Traits::eof()))
3674 __err |= ios_base::eofbit;
3678 if (__loc != locale::classic())
3680 auto& __np = use_facet<numpunct<_CharT>>(__loc);
3681 __dp = __np.decimal_point();
3683 _CharT __c = _Traits::to_char_type(__i);
3689 = hh_mm_ss<_Duration>::fractional_width;
3692 __digit = _S_try_read_digit(__is, __err);
3694 __buf.put(_CharT('0') + __digit);
3702 if (!__is_failed(__err)) [[likely]]
3704 long double __val{};
3705 #if __cpp_lib_to_chars
3706 string __str = std::move(__buf).str();
3707 auto __first = __str.data();
3708 auto __last = __first + __str.size();
3709 using enum chars_format;
3710 auto [ptr, ec] = std::from_chars(__first, __last,
3712 if ((bool)ec || ptr != __last) [[unlikely]]
3713 __err |= ios_base::failbit;
3719 duration<long double> __fs(__val);
3720 if constexpr (__is_floating)
3723 __s = chrono::round<_Duration>(__fs);
3727 __parts |= _ChronoParts::_TimeOfDay;
3730 case 'u': // ISO weekday (1-7)
3731 case 'w': // Weekday (0-6)
3732 if (__mod == 'E') [[unlikely]]
3733 __err |= ios_base::failbit;
3734 else if (__mod == 'O')
3739 __tmget.get(__is, {}, __is, __err, &__tm,
3741 if (!__is_failed(__err))
3742 __wday = weekday(__tm.tm_wday);
3745 __err |= ios_base::failbit;
3749 const int __lo = __c == 'u' ? 1 : 0;
3750 const int __hi = __lo + 6;
3751 auto __val = __read_unsigned(__num ? __num : 1);
3752 if (__lo <= __val && __val <= __hi)
3753 __wday = weekday(__val);
3756 __wday = __bad_wday;
3760 __parts |= _ChronoParts::_Weekday;
3763 case 'U': // Week number of the year (from first Sunday).
3764 case 'V': // ISO week-based week number.
3765 case 'W': // Week number of the year (from first Monday).
3766 if (__mod == 'E') [[unlikely]]
3767 __err |= ios_base::failbit;
3768 else if (__mod == 'O')
3770 if (__c == 'V') [[unlikely]]
3771 __err |= ios_base::failbit;
3774 // TODO nl_langinfo_l(ALT_DIGITS) ?
3775 // Not implementable using std::time_get.
3780 const int __lo = __c == 'V' ? 1 : 0;
3781 const int __hi = 53;
3782 auto __val = __read_unsigned(__num ? __num : 2);
3783 if (__lo <= __val && __val <= __hi)
3788 __sunday_wk = __val;
3794 __monday_wk = __val;
3799 __iso_wk = __sunday_wk = __monday_wk = -1;
3801 // N.B. do not alter __parts here, done after loop.
3804 case 'x': // Locale's date representation.
3805 if (__mod == 'O' || __num) [[unlikely]]
3806 __err |= ios_base::failbit;
3810 __tmget.get(__is, {}, __is, __err, &__tm,
3811 __fmt - 2 - (__mod == 'E'), __fmt);
3812 if (!__is_failed(__err))
3814 __y = year(__tm.tm_year + 1900);
3815 __m = month(__tm.tm_mon + 1);
3816 __d = day(__tm.tm_mday);
3819 __parts |= _ChronoParts::_Date;
3822 case 'X': // Locale's time representation.
3823 if (__mod == 'O' || __num) [[unlikely]]
3824 __err |= ios_base::failbit;
3828 __tmget.get(__is, {}, __is, __err, &__tm,
3829 __fmt - 2 - (__mod == 'E'), __fmt);
3830 if (!__is_failed(__err))
3832 __h = hours(__tm.tm_hour);
3833 __min = minutes(__tm.tm_min);
3834 __s = seconds(__tm.tm_sec);
3837 __parts |= _ChronoParts::_TimeOfDay;
3840 case 'y': // Last two digits of year.
3841 if (__mod) [[unlikely]]
3844 __tmget.get(__is, {}, __is, __err, &__tm,
3846 if (!__is_failed(__err))
3848 int __cent = __tm.tm_year < 2000 ? 1900 : 2000;
3849 __yy = year(__tm.tm_year - __cent);
3850 if (__century == -1) // No %C has been parsed yet.
3856 auto __val = __read_unsigned(__num ? __num : 2);
3857 if (__val >= 0 && __val <= 99)
3860 if (__century == -1) // No %C has been parsed yet.
3861 __century = __val < 69 ? 2000 : 1900;
3864 __y = __yy = __iso_yy = __iso_y = __bad_y;
3866 __parts |= _ChronoParts::_Year;
3870 if (__mod == 'O') [[unlikely]]
3871 __err |= ios_base::failbit;
3872 else if (__mod == 'E')
3875 __tmget.get(__is, {}, __is, __err, &__tm,
3877 if (!__is_failed(__err))
3878 __y = year(__tm.tm_year);
3882 auto __val = __read_unsigned(__num ? __num : 4);
3883 if (!__is_failed(__err))
3886 __parts |= _ChronoParts::_Year;
3890 if (__num) [[unlikely]]
3891 __err |= ios_base::failbit;
3894 // For %Ez and %Oz read [+|-][h]h[:mm].
3895 // For %z read [+|-]hh[mm].
3897 auto __i = __is.peek();
3898 if (_Traits::eq_int_type(__i, _Traits::eof()))
3900 __err |= ios_base::eofbit | ios_base::failbit;
3903 _CharT __ic = _Traits::to_char_type(__i);
3904 const bool __neg = __ic == _CharT('-');
3905 if (__ic == _CharT('-') || __ic == _CharT('+'))
3912 __hh = __read_unsigned(2);
3917 __hh = 10 * _S_try_read_digit(__is, __err);
3918 __hh += _S_try_read_digit(__is, __err);
3921 if (__is_failed(__err))
3925 if (_Traits::eq_int_type(__i, _Traits::eof()))
3927 __err |= ios_base::eofbit;
3928 __tz_offset = minutes(__hh * (__neg ? -60 : 60));
3931 __ic = _Traits::to_char_type(__i);
3933 bool __read_mm = false;
3936 if (__ic == _GLIBCXX_WIDEN(":
")[0])
3943 else if (_CharT('0') <= __ic && __ic <= _CharT('9'))
3949 int_least32_t __mm = 0;
3952 __mm = 10 * _S_try_read_digit(__is, __err);
3953 __mm += _S_try_read_digit(__is, __err);
3956 if (!__is_failed(__err))
3958 auto __z = __hh * 60 + __mm;
3959 __tz_offset = minutes(__neg ? -__z : __z);
3965 if (__mod || __num) [[unlikely]]
3966 __err |= ios_base::failbit;
3969 basic_string_view<_CharT> __x = _GLIBCXX_WIDEN("_/-+
");
3973 auto __i = __is.peek();
3974 if (!_Traits::eq_int_type(__i, _Traits::eof()))
3976 _CharT __a = _Traits::to_char_type(__i);
3977 if (std::isalnum(__a, __loc)
3978 || __x.find(__a) != __x.npos)
3980 __tz_abbr.push_back(__a);
3986 __err |= ios_base::eofbit;
3989 if (__tz_abbr.empty())
3990 __err |= ios_base::failbit;
3994 case 'n': // Exactly one whitespace character.
3995 if (__mod || __num) [[unlikely]]
3996 __err |= ios_base::failbit;
3999 _CharT __i = __is.peek();
4000 if (_Traits::eq_int_type(__i, _Traits::eof()))
4001 __err |= ios_base::eofbit | ios_base::failbit;
4002 else if (std::isspace(_Traits::to_char_type(__i), __loc))
4005 __err |= ios_base::failbit;
4009 case 't': // Zero or one whitespace characters.
4010 if (__mod || __num) [[unlikely]]
4011 __err |= ios_base::failbit;
4014 _CharT __i = __is.peek();
4015 if (_Traits::eq_int_type(__i, _Traits::eof()))
4016 __err |= ios_base::eofbit;
4017 else if (std::isspace(_Traits::to_char_type(__i), __loc))
4022 case '%': // A % character.
4023 if (__mod || __num) [[unlikely]]
4024 __err |= ios_base::failbit;
4029 case 'O': // Modifiers
4031 if (__mod || __num) [[unlikely]]
4033 __err |= ios_base::failbit;
4040 if (_CharT('1') <= __c && __c <= _CharT('9'))
4042 if (!__mod) [[likely]]
4044 // %Nx - extract positive decimal integer N
4045 auto __end = __fmt + _Traits::length(__fmt);
4047 = __format::__parse_integer(__fmt - 1, __end);
4048 if (__ptr) [[likely]]
4056 __err |= ios_base::failbit;
4059 if (__is_failed(__err)) [[unlikely]]
4069 if (__yy != __bad_y && __y == __bad_y)
4070 __y = years(__century) + __yy; // Use %y instead of %Y
4071 if (__iso_yy != __bad_y && __iso_y == __bad_y)
4072 __iso_y = years(__century) + __iso_yy; // Use %g instead of %G
4075 bool __can_use_doy = false;
4076 bool __can_use_iso_wk = false;
4077 bool __can_use_sun_wk = false;
4078 bool __can_use_mon_wk = false;
4080 // A year + day-of-year can be converted to a full date.
4081 if (__y != __bad_y && __dayofyear >= 0)
4083 __can_use_doy = true;
4084 __parts |= _ChronoParts::_Date;
4086 else if (__y != __bad_y && __wday != __bad_wday && __sunday_wk >= 0)
4088 __can_use_sun_wk = true;
4089 __parts |= _ChronoParts::_Date;
4091 else if (__y != __bad_y && __wday != __bad_wday && __monday_wk >= 0)
4093 __can_use_mon_wk = true;
4094 __parts |= _ChronoParts::_Date;
4096 else if (__iso_y != __bad_y && __wday != __bad_wday && __iso_wk > 0)
4098 // An ISO week date can be converted to a full date.
4099 __can_use_iso_wk = true;
4100 __parts |= _ChronoParts::_Date;
4103 if (__is_failed(__err)) [[unlikely]]
4104 ; // Don't bother doing any more work.
4105 else if (__is_flag) [[unlikely]] // incomplete format flag
4106 __err |= ios_base::failbit;
4107 else if ((_M_need & __parts) == _M_need) [[likely]]
4109 // We try to avoid calculating _M_sys_days and _M_ymd unless
4110 // necessary, because converting sys_days to year_month_day
4111 // (or vice versa) requires non-trivial calculations.
4112 // If we have y/m/d values then use them to populate _M_ymd
4113 // and only convert it to _M_sys_days if the caller needs that.
4114 // But if we don't have y/m/d and need to calculate the date
4115 // from the day-of-year or a week+weekday then we set _M_sys_days
4116 // and only convert it to _M_ymd if the caller needs that.
4118 // We do more error checking here, but only for the fields that
4119 // we actually need to use. For example, we will not diagnose
4120 // an invalid dayofyear==366 for non-leap years unless actually
4121 // using __dayofyear. This should mean we never produce invalid
4122 // results, but it means not all invalid inputs are diagnosed,
4123 // e.g. "2023-01-01 366
" >> "%F %j
" ignores the invalid 366.
4124 // We also do not diagnose inconsistent values for the same
4125 // field, e.g. "2021 2022 2023
" >> "%C%y %Y %Y
" just uses 2023.
4127 // Whether the caller wants _M_wd.
4128 // The _Weekday bit is only set for chrono::weekday.
4129 const bool __need_wday = _M_need & _ChronoParts::_Weekday;
4131 // Whether the caller wants _M_sys_days and _M_time.
4132 // Only true for durations and time_points.
4133 const bool __need_time = _M_need & _ChronoParts::_TimeOfDay;
4135 if (__need_wday && __wday != __bad_wday)
4136 _M_wd = __wday; // Caller only wants a weekday and we have one.
4137 else if (_M_need & _ChronoParts::_Date) // subsumes __need_wday
4139 // Whether the caller wants _M_ymd.
4140 // True for chrono::year etc., false for time_points.
4141 const bool __need_ymd = !__need_wday && !__need_time;
4143 if ((_M_need & _ChronoParts::_Year && __y == __bad_y)
4144 || (_M_need & _ChronoParts::_Month && __m == __bad_mon)
4145 || (_M_need & _ChronoParts::_Day && __d == __bad_day))
4147 // Missing at least one of y/m/d so calculate sys_days
4148 // from the other data we have available.
4152 if ((0 < __dayofyear && __dayofyear <= 365)
4153 || (__dayofyear == 366 && __y.is_leap()))
4156 _M_sys_days = sys_days(__y/January/1)
4157 + days(__dayofyear - 1);
4159 _M_ymd = year_month_day(_M_sys_days);
4162 __err |= ios_base::failbit;
4164 else if (__can_use_iso_wk)
4166 // Calculate y/m/d from ISO week date.
4170 // A year has 53 weeks iff Jan 1st is a Thursday
4171 // or Jan 1 is a Wednesday and it's a leap year.
4172 const sys_days __jan4(__iso_y/January/4);
4173 weekday __wd1(__jan4 - days(3));
4174 if (__wd1 != Thursday)
4175 if (__wd1 != Wednesday || !__iso_y.is_leap())
4176 __err |= ios_base::failbit;
4179 if (!__is_failed(__err)) [[likely]]
4181 // First Thursday is always in week one:
4182 sys_days __w(Thursday[1]/January/__iso_y);
4183 // First day of week-based year:
4184 __w -= Thursday - Monday;
4185 __w += days(weeks(__iso_wk - 1));
4186 __w += __wday - Monday;
4190 _M_ymd = year_month_day(_M_sys_days);
4193 else if (__can_use_sun_wk)
4195 // Calculate y/m/d from week number + weekday.
4196 sys_days __wk1(__y/January/Sunday[1]);
4197 _M_sys_days = __wk1 + weeks(__sunday_wk - 1)
4198 + days(__wday.c_encoding());
4199 _M_ymd = year_month_day(_M_sys_days);
4200 if (_M_ymd.year() != __y) [[unlikely]]
4201 __err |= ios_base::failbit;
4203 else if (__can_use_mon_wk)
4205 // Calculate y/m/d from week number + weekday.
4206 sys_days __wk1(__y/January/Monday[1]);
4207 _M_sys_days = __wk1 + weeks(__monday_wk - 1)
4208 + days(__wday.c_encoding() - 1);
4209 _M_ymd = year_month_day(_M_sys_days);
4210 if (_M_ymd.year() != __y) [[unlikely]]
4211 __err |= ios_base::failbit;
4213 else // Should not be able to get here.
4214 __err |= ios_base::failbit;
4218 // We know that all fields the caller needs are present,
4219 // but check that their values are in range.
4220 // Make unwanted fields valid so that _M_ymd.ok() is true.
4222 if (_M_need & _ChronoParts::_Year)
4224 if (!__y.ok()) [[unlikely]]
4225 __err |= ios_base::failbit;
4227 else if (__y == __bad_y)
4228 __y = 1972y; // Leap year so that Feb 29 is valid.
4230 if (_M_need & _ChronoParts::_Month)
4232 if (!__m.ok()) [[unlikely]]
4233 __err |= ios_base::failbit;
4235 else if (__m == __bad_mon)
4238 if (_M_need & _ChronoParts::_Day)
4240 if (__d < day(1) || __d > (__y/__m/last).day())
4241 __err |= ios_base::failbit;
4243 else if (__d == __bad_day)
4246 if (year_month_day __ymd(__y, __m, __d); __ymd.ok())
4249 if (__need_wday || __need_time)
4250 _M_sys_days = sys_days(_M_ymd);
4253 __err |= ios_base::failbit;
4257 _M_wd = weekday(_M_sys_days);
4260 // Need to set _M_time for both durations and time_points.
4263 if (__h == __bad_h && __h12 != __bad_h)
4266 __h = __h12 == hours(12) ? hours(0) : __h12;
4267 else if (__ampm == 2)
4268 __h = __h12 == hours(12) ? __h12 : __h12 + hours(12);
4270 __err |= ios_base::failbit;
4273 auto __t = _M_time.zero();
4282 if (__min != __bad_min)
4288 if (__s != __bad_sec)
4292 _M_is_leap_second = __s >= seconds(60);
4298 __err |= ios_base::failbit;
4301 if (!__is_failed(__err)) [[likely]]
4303 if (__offset && __tz_offset != __bad_min)
4304 *__offset = __tz_offset;
4305 if (__abbrev && !__tz_abbr.empty())
4306 *__abbrev = std::move(__tz_abbr);
4310 __err |= ios_base::failbit;
4313 __is.setstate(__err);
4317 #undef _GLIBCXX_WIDEN
4320 } // namespace chrono
4322 _GLIBCXX_END_NAMESPACE_VERSION
4327 #endif //_GLIBCXX_CHRONO_IO_H
basic_ostream< _CharT, _Traits > & operator<<(std::basic_ostream< _CharT, _Traits > &__os, const duration< _Rep, _Period > &__d)
duration< int64_t, ratio< 86400 > > days
days
__detail::__local_time_fmt< _Duration > local_time_format(local_time< _Duration > __time, const string *__abbrev=nullptr, const seconds *__offset_sec=nullptr)
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
ISO C++ entities toplevel namespace is std.
constexpr bitset< _Nb > operator|(const bitset< _Nb > &__x, const bitset< _Nb > &__y) noexcept
Global bitwise operations on bitsets.
constexpr auto size(const _Container &__cont) noexcept(noexcept(__cont.size())) -> decltype(__cont.size())
Return the size of a container.
locale imbue(const locale &__loc)
Moves to a new locale.
Template class basic_ostream.
Controlling output for std::string.
Provides output iterator semantics for streambufs.
Provides compile-time rational arithmetic.
A non-owning reference to a string.
chrono::duration represents a distance between two points in time
chrono::time_point represents a point in time as measured by a clock
streamsize precision() const
Flags access.
fmtflags flags() const
Access to format flags.
static const locale & classic()
Return reference to the C locale.