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 auto __t = _S_floor_seconds(__tt);
797 locale __loc = _M_locale(__ctx);
798 const auto& __tp = use_facet<__timepunct<_CharT>>(__loc);
799 const _CharT* __formats[2];
800 __tp._M_date_time_formats(__formats);
801 const _CharT* __rep = __formats[__mod];
803 __rep = _GLIBCXX_WIDEN(
"%a %b %e %H:%M:%S %Y");
804 basic_string<_CharT> __fmt(_S_empty_spec);
805 __fmt.insert(1u, 1u, _S_colon);
806 __fmt.insert(2u, __rep);
807 return std::vformat_to(
std::move(__out), __loc, __fmt,
808 std::make_format_args<_FormatContext>(__t));
811 template<
typename _Tp,
typename _FormatContext>
812 typename _FormatContext::iterator
813 _M_C_y_Y(
const _Tp& __t,
typename _FormatContext::iterator __out,
814 _FormatContext& __ctx, _CharT __conv, _CharT __mod = 0)
const
824 chrono::year __y = _S_year(__t);
826 if (__mod) [[unlikely]]
829 __tm.tm_year = (int)__y - 1900;
830 return _M_locale_fmt(
std::move(__out), _M_locale(__ctx), __tm,
834 basic_string<_CharT> __s;
836 const bool __is_neg = __yi < 0;
837 __yi = __builtin_abs(__yi);
839 if (__conv ==
'Y' || __conv ==
'C')
841 int __ci = __yi / 100;
842 if (__is_neg) [[unlikely]]
844 __s.assign(1, _S_plus_minus[1]);
846 if (__conv ==
'C' && (__ci * 100) != __yi)
849 if (__ci >= 100) [[unlikely]]
851 __s += std::format(_S_empty_spec, __ci / 100);
854 __s += _S_two_digits(__ci);
857 if (__conv ==
'Y' || __conv ==
'y')
858 __s += _S_two_digits(__yi % 100);
860 return __format::__write(
std::move(__out), __string_view(__s));
863 template<
typename _Tp,
typename _FormatContext>
864 typename _FormatContext::iterator
865 _M_D(
const _Tp& __t,
typename _FormatContext::iterator __out,
866 _FormatContext&)
const
868 auto __ymd = _S_date(__t);
869 basic_string<_CharT> __s;
870 #if ! _GLIBCXX_USE_CXX11_ABI
873 __s = _S_two_digits((
unsigned)__ymd.month());
875 __s += _S_two_digits((
unsigned)__ymd.day());
877 __s += _S_two_digits(__builtin_abs((
int)__ymd.year()) % 100);
878 return __format::__write(
std::move(__out), __string_view(__s));
881 template<
typename _Tp,
typename _FormatContext>
882 typename _FormatContext::iterator
883 _M_d_e(
const _Tp& __t,
typename _FormatContext::iterator __out,
884 _FormatContext& __ctx, _CharT __conv,
bool __mod =
false)
const
891 chrono::day __d = _S_day(__t);
892 unsigned __i = (unsigned)__d;
894 if (__mod) [[unlikely]]
898 return _M_locale_fmt(
std::move(__out), _M_locale(__ctx), __tm,
902 auto __sv = _S_two_digits(__i);
904 if (__conv == _CharT(
'e') && __i < 10)
910 return __format::__write(
std::move(__out), __sv);
913 template<
typename _Tp,
typename _FormatContext>
914 typename _FormatContext::iterator
915 _M_F(
const _Tp& __t,
typename _FormatContext::iterator __out,
916 _FormatContext&)
const
918 auto __ymd = _S_date(__t);
919 auto __s = std::format(_GLIBCXX_WIDEN(
"{:04d}- - "),
921 auto __sv = _S_two_digits((
unsigned)__ymd.month());
922 __s[__s.size() - 5] = __sv[0];
923 __s[__s.size() - 4] = __sv[1];
924 __sv = _S_two_digits((
unsigned)__ymd.day());
925 __s[__s.size() - 2] = __sv[0];
926 __s[__s.size() - 1] = __sv[1];
928 return __format::__write(
std::move(__out), __sv);
931 template<
typename _Tp,
typename _FormatContext>
932 typename _FormatContext::iterator
933 _M_g_G(
const _Tp& __t,
typename _FormatContext::iterator __out,
934 _FormatContext& __ctx,
bool __full)
const
938 using namespace chrono;
939 auto __d = _S_days(__t);
941 __d -= (weekday(__d) - Monday) -
days(3);
943 year __y = year_month_day(__d).year();
944 return _M_C_y_Y(__y,
std::move(__out), __ctx,
"yY"[__full]);
947 template<
typename _Tp,
typename _FormatContext>
948 typename _FormatContext::iterator
949 _M_H_I(
const _Tp& __t,
typename _FormatContext::iterator __out,
950 _FormatContext& __ctx, _CharT __conv,
bool __mod =
false)
const
957 const auto __hms = _S_hms(__t);
958 int __i = __hms.hours().count();
960 if (__mod) [[unlikely]]
964 return _M_locale_fmt(
std::move(__out), _M_locale(__ctx), __tm,
968 if (__conv == _CharT(
'I'))
975 return __format::__write(
std::move(__out), _S_two_digits(__i));
978 template<
typename _Tp,
typename _FormatContext>
979 typename _FormatContext::iterator
980 _M_j(
const _Tp& __t,
typename _FormatContext::iterator __out,
981 _FormatContext&)
const
983 if constexpr (chrono::__is_duration_v<_Tp>)
986 unsigned __d = chrono::duration_cast<chrono::days>(__t).count();
987 return std::format_to(
std::move(__out), _S_empty_spec, __d);
992 using namespace chrono;
993 auto __day = _S_days(__t);
994 auto __ymd = _S_date(__t);
998 if constexpr (is_same_v<
typename decltype(__day)::clock, local_t>)
999 __d = __day - local_days(__ymd.year()/January/0);
1001 __d = __day - sys_days(__ymd.year()/January/0);
1002 return
std::format_to(
std::
move(__out), _GLIBCXX_WIDEN("{:03d}
"),
1007 template<typename _Tp, typename _FormatContext>
1008 typename _FormatContext::iterator
1009 _M_m(const _Tp& __t, typename _FormatContext::iterator __out,
1010 _FormatContext& __ctx, bool __mod) const
1012 // %m month as a decimal number.
1013 // %Om Locale's alternative representation.
1015 auto __m = _S_month(__t);
1016 auto __i = (unsigned)__m;
1018 if (__mod) [[unlikely]] // %Om
1021 __tm.tm_mon = __i - 1;
1022 return _M_locale_fmt(std::move(__out), _M_locale(__ctx), __tm,
1026 return __format::__write(std::move(__out), _S_two_digits(__i));
1029 template<typename _Tp, typename _FormatContext>
1030 typename _FormatContext::iterator
1031 _M_M(const _Tp& __t, typename _FormatContext::iterator __out,
1032 _FormatContext& __ctx, bool __mod) const
1034 // %M The minute as a decimal number.
1035 // %OM Locale's alternative representation.
1037 auto __m = _S_hms(__t).minutes();
1038 auto __i = __m.count();
1040 if (__mod) [[unlikely]] // %OM
1044 return _M_locale_fmt(std::move(__out), _M_locale(__ctx), __tm,
1048 return __format::__write(std::move(__out), _S_two_digits(__i));
1051 template<typename _Tp, typename _FormatContext>
1052 typename _FormatContext::iterator
1053 _M_p(const _Tp& __t, typename _FormatContext::iterator __out,
1054 _FormatContext& __ctx) const
1056 // %p The locale's equivalent of the AM/PM designations.
1057 auto __hms = _S_hms(__t);
1058 locale __loc = _M_locale(__ctx);
1059 const auto& __tp = use_facet<__timepunct<_CharT>>(__loc);
1060 const _CharT* __ampm[2];
1061 __tp._M_am_pm(__ampm);
1062 return std::format_to(std::move(__out), _S_empty_spec,
1063 __ampm[__hms.hours().count() >= 12]);
1066 template<typename _Tp, typename _FormatContext>
1067 typename _FormatContext::iterator
1068 _M_q(const _Tp&, typename _FormatContext::iterator __out,
1069 _FormatContext&) const
1071 // %q The duration's unit suffix
1072 if constexpr (!chrono::__is_duration_v<_Tp>)
1073 __throw_format_error("format error: argument is not a duration
");
1076 namespace __d = chrono::__detail;
1077 using period = typename _Tp::period;
1078 return __d::__fmt_units_suffix<period, _CharT>(std::move(__out));
1082 // %Q handled in _M_format
1084 template<typename _Tp, typename _FormatContext>
1085 typename _FormatContext::iterator
1086 _M_r(const _Tp& __tt, typename _FormatContext::iterator __out,
1087 _FormatContext& __ctx) const
1089 // %r locale's 12-hour clock time.
1090 auto __t = _S_floor_seconds(__tt);
1091 locale __loc = _M_locale(__ctx);
1092 const auto& __tp = use_facet<__timepunct<_CharT>>(__loc);
1093 const _CharT* __ampm_fmt;
1094 __tp._M_am_pm_format(&__ampm_fmt);
1095 basic_string<_CharT> __fmt(_S_empty_spec);
1096 __fmt.insert(1u, 1u, _S_colon);
1097 __fmt.insert(2u, __ampm_fmt);
1098 return std::vformat_to(std::move(__out), __fmt,
1099 std::make_format_args<_FormatContext>(__t));
1102 template<typename _Tp, typename _FormatContext>
1103 typename _FormatContext::iterator
1104 _M_R_T(const _Tp& __t, typename _FormatContext::iterator __out,
1105 _FormatContext& __ctx, bool __secs) const
1107 // %R Equivalent to %H:%M
1108 // %T Equivalent to %H:%M:%S
1109 auto __hms = _S_hms(__t);
1111 auto __s = std::format(_GLIBCXX_WIDEN("{:02d}:00
"),
1112 __hms.hours().count());
1113 auto __sv = _S_two_digits(__hms.minutes().count());
1114 __s[__s.size() - 2] = __sv[0];
1115 __s[__s.size() - 1] = __sv[1];
1117 __out = __format::__write(std::move(__out), __sv);
1120 *__out++ = _S_colon;
1121 __out = _M_S(__hms, std::move(__out), __ctx);
1126 template<typename _Tp, typename _FormatContext>
1127 typename _FormatContext::iterator
1128 _M_S(const _Tp& __t, typename _FormatContext::iterator __out,
1129 _FormatContext& __ctx, bool __mod = false) const
1131 // %S Seconds as a decimal number.
1132 // %OS The locale's alternative representation.
1133 auto __hms = _S_hms(__t);
1135 if (__mod) [[unlikely]] // %OS
1138 __tm.tm_sec = (int)__hms.seconds().count();
1139 return _M_locale_fmt(std::move(__out), _M_locale(__ctx), __tm,
1143 if constexpr (__hms.fractional_width == 0)
1144 __out = __format::__write(std::move(__out),
1145 _S_two_digits(__hms.seconds().count()));
1148 locale __loc = _M_locale(__ctx);
1149 auto __s = __hms.seconds();
1150 auto __ss = __hms.subseconds();
1151 using rep = typename decltype(__ss)::rep;
1152 if constexpr (is_floating_point_v<rep>)
1154 chrono::duration<rep> __fs = __s + __ss;
1155 __out = std::format_to(std::move(__out), __loc,
1156 _GLIBCXX_WIDEN("{:#0{}.{}Lf}
"),
1158 3 + __hms.fractional_width,
1159 __hms.fractional_width);
1164 = use_facet<numpunct<_CharT>>(__loc);
1165 __out = __format::__write(std::move(__out),
1166 _S_two_digits(__s.count()));
1167 *__out++ = __np.decimal_point();
1168 if constexpr (is_integral_v<rep>)
1169 __out = std::format_to(std::move(__out),
1170 _GLIBCXX_WIDEN("{:0{}}
"),
1172 __hms.fractional_width);
1175 auto __str = std::format(_S_empty_spec, __ss.count());
1176 __out = std::format_to(_GLIBCXX_WIDEN("{:0>{}s}
"),
1178 __hms.fractional_width);
1185 // %t handled in _M_format
1187 template<typename _Tp, typename _FormatContext>
1188 typename _FormatContext::iterator
1189 _M_u_w(const _Tp& __t, typename _FormatContext::iterator __out,
1190 _FormatContext& __ctx, _CharT __conv, bool __mod = false) const
1192 // %u ISO weekday as a decimal number (1-7), where Monday is 1.
1193 // %Ou Locale's alternative numeric rep.
1194 // %w Weekday as a decimal number (0-6), where Sunday is 0.
1195 // %Ow Locale's alternative numeric rep.
1197 chrono::weekday __wd = _S_weekday(__t);
1199 if (__mod) [[unlikely]]
1202 __tm.tm_wday = __wd.c_encoding();
1203 return _M_locale_fmt(std::move(__out), _M_locale(__ctx), __tm,
1207 unsigned __wdi = __conv == 'u' ? __wd.iso_encoding()
1208 : __wd.c_encoding();
1209 const _CharT __d = _S_digit(__wdi);
1210 return __format::__write(std::move(__out), __string_view(&__d, 1));
1213 template<typename _Tp, typename _FormatContext>
1214 typename _FormatContext::iterator
1215 _M_U_V_W(const _Tp& __t, typename _FormatContext::iterator __out,
1216 _FormatContext& __ctx, _CharT __conv, bool __mod = false) const
1218 // %U Week number of the year as a decimal number, from first Sunday.
1219 // %OU Locale's alternative numeric rep.
1220 // %V ISO week-based week number as a decimal number.
1221 // %OV Locale's alternative numeric rep.
1222 // %W Week number of the year as a decimal number, from first Monday.
1223 // %OW Locale's alternative numeric rep.
1224 using namespace chrono;
1225 auto __d = _S_days(__t);
1226 using _TDays = decltype(__d); // Either sys_days or local_days.
1228 if (__mod) [[unlikely]]
1230 const year_month_day __ymd(__d);
1231 const year __y = __ymd.year();
1233 __tm.tm_year = (int)__y - 1900;
1234 __tm.tm_yday = (__d - _TDays(__y/January/1)).count();
1235 __tm.tm_wday = weekday(__d).c_encoding();
1236 return _M_locale_fmt(std::move(__out), _M_locale(__ctx), __tm,
1240 _TDays __first; // First day of week 1.
1241 if (__conv == 'V') // W01 begins on Monday before first Thursday.
1243 // Move to nearest Thursday:
1244 __d -= (weekday(__d) - Monday) - days(3);
1245 // ISO week of __t is number of weeks since January 1 of the
1246 // same year as that nearest Thursday.
1247 __first = _TDays(year_month_day(__d).year()/January/1);
1252 if constexpr (requires { __t.year(); })
1255 __y = year_month_day(__d).year();
1256 const weekday __weekstart = __conv == 'U' ? Sunday : Monday;
1257 __first = _TDays(__y/January/__weekstart[1]);
1259 auto __weeks = chrono::floor<weeks>(__d - __first);
1260 __string_view __sv = _S_two_digits(__weeks.count() + 1);
1261 return __format::__write(std::move(__out), __sv);
1264 template<typename _Tp, typename _FormatContext>
1265 typename _FormatContext::iterator
1266 _M_x(const _Tp& __t, typename _FormatContext::iterator __out,
1267 _FormatContext& __ctx, bool __mod = false) const
1269 // %x Locale's date rep
1270 // %Ex Locale's alternative date representation.
1271 locale __loc = _M_locale(__ctx);
1272 const auto& __tp = use_facet<__timepunct<_CharT>>(__loc);
1273 const _CharT* __date_reps[2];
1274 __tp._M_date_formats(__date_reps);
1275 const _CharT* __rep = __date_reps[__mod];
1277 return _M_D(__t, std::move(__out), __ctx);
1279 basic_string<_CharT> __fmt(_S_empty_spec);
1280 __fmt.insert(1u, 1u, _S_colon);
1281 __fmt.insert(2u, __rep);
1282 return std::vformat_to(std::move(__out), __fmt,
1283 std::make_format_args<_FormatContext>(__t));
1286 template<typename _Tp, typename _FormatContext>
1287 typename _FormatContext::iterator
1288 _M_X(const _Tp& __tt, typename _FormatContext::iterator __out,
1289 _FormatContext& __ctx, bool __mod = false) const
1291 // %X Locale's time rep
1292 // %EX Locale's alternative time representation.
1293 auto __t = _S_floor_seconds(__tt);
1294 locale __loc = _M_locale(__ctx);
1295 const auto& __tp = use_facet<__timepunct<_CharT>>(__loc);
1296 const _CharT* __time_reps[2];
1297 __tp._M_time_formats(__time_reps);
1298 const _CharT* __rep = __time_reps[__mod];
1300 return _M_R_T(__t, std::move(__out), __ctx, true);
1302 basic_string<_CharT> __fmt(_S_empty_spec);
1303 __fmt.insert(1u, 1u, _S_colon);
1304 __fmt.insert(2u, __rep);
1305 return std::vformat_to(std::move(__out), __fmt,
1306 std::make_format_args<_FormatContext>(__t));
1309 template<typename _Tp, typename _FormatContext>
1310 typename _FormatContext::iterator
1311 _M_z(const _Tp& __t, typename _FormatContext::iterator __out,
1312 _FormatContext&, bool __mod = false) const
1314 using ::std::chrono::__detail::__utc_leap_second;
1315 using ::std::chrono::__detail::__local_time_fmt;
1317 auto __utc = __mod ? __string_view(_GLIBCXX_WIDEN("+00:00
"), 6)
1318 : __string_view(_GLIBCXX_WIDEN("+0000
"), 5);
1320 if constexpr (chrono::__is_time_point_v<_Tp>)
1322 if constexpr (is_same_v<typename _Tp::clock,
1323 chrono::system_clock>)
1324 return __format::__write(std::move(__out), __utc);
1326 else if constexpr (__is_specialization_of<_Tp, __local_time_fmt>)
1328 if (__t._M_offset_sec)
1331 basic_string<_CharT> __s;
1332 if (*__t._M_offset_sec != 0s)
1334 chrono:: hh_mm_ss __hms(*__t._M_offset_sec);
1335 __s = _S_plus_minus[__hms.is_negative()];
1336 __s += _S_two_digits(__hms.hours().count());
1339 __s += _S_two_digits(__hms.minutes().count());
1342 return __format::__write(std::move(__out), __sv);
1345 else if constexpr (__is_specialization_of<_Tp, __utc_leap_second>)
1346 return __format::__write(std::move(__out), __utc);
1348 __no_timezone_available();
1351 template<typename _Tp, typename _FormatContext>
1352 typename _FormatContext::iterator
1353 _M_Z(const _Tp& __t, typename _FormatContext::iterator __out,
1354 _FormatContext& __ctx) const
1356 using ::std::chrono::__detail::__utc_leap_second;
1357 using ::std::chrono::__detail::__local_time_fmt;
1359 __string_view __utc(_GLIBCXX_WIDEN("UTC
"), 3);
1360 if constexpr (chrono::__is_time_point_v<_Tp>)
1362 if constexpr (is_same_v<typename _Tp::clock,
1363 chrono::system_clock>)
1364 return __format::__write(std::move(__out), __utc);
1366 else if constexpr (__is_specialization_of<_Tp, __local_time_fmt>)
1370 string_view __sv = *__t._M_abbrev;
1371 if constexpr (is_same_v<_CharT, char>)
1372 return __format::__write(std::move(__out), __sv);
1375 // TODO use resize_and_overwrite
1376 basic_string<_CharT> __ws(__sv.size(), _CharT());
1377 auto& __ct = use_facet<ctype<_CharT>>(_M_locale(__ctx));
1378 __ct.widen(__sv.begin(), __sv.end(), __ws.data());
1379 __string_view __wsv = __ws;
1380 return __format::__write(std::move(__out), __wsv);
1384 else if constexpr (__is_specialization_of<_Tp, __utc_leap_second>)
1385 return __format::__write(std::move(__out), __utc);
1387 __no_timezone_available();
1390 // %% handled in _M_format
1392 // A single digit character in the range '0'..'9'.
1394 _S_digit(int __n) noexcept
1396 // Extra 9s avoid past-the-end read on bad input.
1397 return _GLIBCXX_WIDEN("0123456789999999
")[__n & 0xf];
1400 // A string view of two digit characters, "00
".."99
".
1401 static basic_string_view<_CharT>
1402 _S_two_digits(int __n) noexcept
1405 _GLIBCXX_WIDEN("0001020304050607080910111213141516171819
"
1406 "2021222324252627282930313233343536373839
"
1407 "4041424344454647484950515253545556575859
"
1408 "6061626364656667686970717273747576777879
"
1409 "8081828384858687888990919293949596979899
"
1410 "9999999999999999999999999999999999999999
"
1411 "9999999999999999
") + 2 * (__n & 0x7f),
1416 // Accessors for the components of chrono types:
1418 // Returns a hh_mm_ss.
1419 template<typename _Tp>
1420 static decltype(auto)
1421 _S_hms(const _Tp& __t)
1423 using ::std::chrono::__detail::__utc_leap_second;
1424 using ::std::chrono::__detail::__local_time_fmt;
1426 if constexpr (__is_specialization_of<_Tp, chrono::hh_mm_ss>)
1428 else if constexpr (__is_specialization_of<_Tp, __utc_leap_second>)
1430 else if constexpr (chrono::__is_duration_v<_Tp>)
1431 return chrono::hh_mm_ss<_Tp>(__t);
1432 else if constexpr (chrono::__is_time_point_v<_Tp>)
1433 return chrono::hh_mm_ss(__t - chrono::floor<chrono::days>(__t));
1434 else if constexpr (__is_specialization_of<_Tp, __local_time_fmt>)
1435 return _S_hms(__t._M_time);
1438 __invalid_chrono_spec();
1439 return chrono::hh_mm_ss<chrono::seconds>();
1443 // Returns a sys_days or local_days.
1444 template<typename _Tp>
1446 _S_days(const _Tp& __t)
1448 using namespace chrono;
1449 using ::std::chrono::__detail::__utc_leap_second;
1450 using ::std::chrono::__detail::__local_time_fmt;
1452 if constexpr (__is_time_point_v<_Tp>)
1453 return chrono::floor<days>(__t);
1454 else if constexpr (__is_specialization_of<_Tp, __utc_leap_second>)
1456 else if constexpr (__is_specialization_of<_Tp, __local_time_fmt>)
1457 return chrono::floor<days>(__t._M_time);
1458 else if constexpr (is_same_v<_Tp, year_month_day>
1459 || is_same_v<_Tp, year_month_day_last>
1460 || is_same_v<_Tp, year_month_weekday>
1461 || is_same_v<_Tp, year_month_weekday_last>)
1462 return sys_days(__t);
1465 if constexpr (__is_duration_v<_Tp>)
1466 __not_valid_for_duration();
1468 __invalid_chrono_spec();
1469 return chrono::sys_days();
1473 // Returns a year_month_day.
1474 template<typename _Tp>
1475 static chrono::year_month_day
1476 _S_date(const _Tp& __t)
1478 if constexpr (is_same_v<_Tp, chrono::year_month_day>)
1481 return chrono::year_month_day(_S_days(__t));
1484 template<typename _Tp>
1486 _S_day(const _Tp& __t)
1488 using namespace chrono;
1490 if constexpr (is_same_v<_Tp, day>)
1492 else if constexpr (requires { __t.day(); })
1495 return _S_date(__t).day();
1498 template<typename _Tp>
1499 static chrono::month
1500 _S_month(const _Tp& __t)
1502 using namespace chrono;
1504 if constexpr (is_same_v<_Tp, month>)
1506 else if constexpr (requires { __t.month(); })
1509 return _S_date(__t).month();
1512 template<typename _Tp>
1514 _S_year(const _Tp& __t)
1516 using namespace chrono;
1518 if constexpr (is_same_v<_Tp, year>)
1520 else if constexpr (requires { __t.year(); })
1523 return _S_date(__t).year();
1526 template<typename _Tp>
1527 static chrono::weekday
1528 _S_weekday(const _Tp& __t)
1530 using namespace ::std::chrono;
1531 using ::std::chrono::__detail::__local_time_fmt;
1533 if constexpr (is_same_v<_Tp, weekday>)
1535 else if constexpr (requires { __t.weekday(); })
1536 return __t.weekday();
1537 else if constexpr (is_same_v<_Tp, month_weekday>)
1538 return __t.weekday_indexed().weekday();
1539 else if constexpr (is_same_v<_Tp, month_weekday_last>)
1540 return __t.weekday_last().weekday();
1542 return weekday(_S_days(__t));
1545 // Remove subsecond precision from a time_point.
1546 template<typename _Tp>
1548 _S_floor_seconds(const _Tp& __t)
1550 using chrono::__detail::__local_time_fmt;
1551 if constexpr (chrono::__is_time_point_v<_Tp>
1552 || chrono::__is_duration_v<_Tp>)
1554 if constexpr (_Tp::period::den != 1)
1555 return chrono::floor<chrono::seconds>(__t);
1559 else if constexpr (__is_specialization_of<_Tp, chrono::hh_mm_ss>)
1561 if constexpr (_Tp::fractional_width != 0)
1562 return chrono::floor<chrono::seconds>(__t.to_duration());
1566 else if constexpr (__is_specialization_of<_Tp, __local_time_fmt>)
1567 return _S_floor_seconds(__t._M_time);
1572 // Use the formatting locale's std::time_put facet to produce
1573 // a locale-specific representation.
1574 template<typename _Iter>
1576 _M_locale_fmt(_Iter __out, const locale& __loc, const struct tm& __tm,
1577 char __fmt, char __mod) const
1579 basic_ostringstream<_CharT> __os;
1580 const auto& __tp = use_facet<time_put<_CharT>>(__loc);
1581 __tp.put(__os, __os, _S_space, &__tm, __fmt, __mod);
1583 __out = __format::__write(std::move(__out), __os.view());
1588 } // namespace __format
1591 template<typename _Rep, typename _Period, typename _CharT>
1592 struct formatter<chrono::duration<_Rep, _Period>, _CharT>
1594 constexpr typename basic_format_parse_context<_CharT>::iterator
1595 parse(basic_format_parse_context<_CharT>& __pc)
1597 using namespace __format;
1598 auto __it = _M_f._M_parse(__pc, _Duration|_TimeOfDay);
1599 if constexpr (!is_floating_point_v<_Rep>)
1600 if (_M_f._M_spec._M_prec_kind != __format::_WP_none)
1601 __throw_format_error("format error: invalid precision
for duration
");
1605 template<typename _Out>
1606 typename basic_format_context<_Out, _CharT>::iterator
1607 format(const chrono::duration<_Rep, _Period>& __d,
1608 basic_format_context<_Out, _CharT>& __fc) const
1610 if constexpr (numeric_limits<_Rep>::is_signed)
1611 if (__d < __d.zero())
1612 return _M_f._M_format(-__d, __fc, true);
1613 return _M_f._M_format(__d, __fc, false);
1617 __format::__formatter_chrono<_CharT> _M_f;
1620 template<typename _CharT>
1621 struct formatter<chrono::day, _CharT>
1623 template<typename _ParseContext>
1624 constexpr typename _ParseContext::iterator
1625 parse(_ParseContext& __pc)
1626 { return _M_f._M_parse(__pc, __format::_Day); }
1628 template<typename _FormatContext>
1629 typename _FormatContext::iterator
1630 format(const chrono::day& __t, _FormatContext& __fc) const
1631 { return _M_f._M_format(__t, __fc); }
1634 __format::__formatter_chrono<_CharT> _M_f;
1637 template<typename _CharT>
1638 struct formatter<chrono::month, _CharT>
1640 template<typename _ParseContext>
1641 constexpr typename _ParseContext::iterator
1642 parse(_ParseContext& __pc)
1643 { return _M_f._M_parse(__pc, __format::_Month); }
1645 template<typename _FormatContext>
1646 typename _FormatContext::iterator
1647 format(const chrono::month& __t, _FormatContext& __fc) const
1648 { return _M_f._M_format(__t, __fc); }
1651 __format::__formatter_chrono<_CharT> _M_f;
1654 template<typename _CharT>
1655 struct formatter<chrono::year, _CharT>
1657 template<typename _ParseContext>
1658 constexpr typename _ParseContext::iterator
1659 parse(_ParseContext& __pc)
1660 { return _M_f._M_parse(__pc, __format::_Year); }
1662 template<typename _FormatContext>
1663 typename _FormatContext::iterator
1664 format(const chrono::year& __t, _FormatContext& __fc) const
1665 { return _M_f._M_format(__t, __fc); }
1668 __format::__formatter_chrono<_CharT> _M_f;
1671 template<typename _CharT>
1672 struct formatter<chrono::weekday, _CharT>
1674 template<typename _ParseContext>
1675 constexpr typename _ParseContext::iterator
1676 parse(_ParseContext& __pc)
1677 { return _M_f._M_parse(__pc, __format::_Weekday); }
1679 template<typename _FormatContext>
1680 typename _FormatContext::iterator
1681 format(const chrono::weekday& __t, _FormatContext& __fc) const
1682 { return _M_f._M_format(__t, __fc); }
1685 __format::__formatter_chrono<_CharT> _M_f;
1688 template<typename _CharT>
1689 struct formatter<chrono::weekday_indexed, _CharT>
1691 template<typename _ParseContext>
1692 constexpr typename _ParseContext::iterator
1693 parse(_ParseContext& __pc)
1694 { return _M_f._M_parse(__pc, __format::_Weekday); }
1696 template<typename _FormatContext>
1697 typename _FormatContext::iterator
1698 format(const chrono::weekday_indexed& __t, _FormatContext& __fc) const
1699 { return _M_f._M_format(__t, __fc); }
1702 __format::__formatter_chrono<_CharT> _M_f;
1705 template<typename _CharT>
1706 struct formatter<chrono::weekday_last, _CharT>
1708 template<typename _ParseContext>
1709 constexpr typename _ParseContext::iterator
1710 parse(_ParseContext& __pc)
1711 { return _M_f._M_parse(__pc, __format::_Weekday); }
1713 template<typename _FormatContext>
1714 typename _FormatContext::iterator
1715 format(const chrono::weekday_last& __t, _FormatContext& __fc) const
1716 { return _M_f._M_format(__t, __fc); }
1719 __format::__formatter_chrono<_CharT> _M_f;
1722 template<typename _CharT>
1723 struct formatter<chrono::month_day, _CharT>
1725 template<typename _ParseContext>
1726 constexpr typename _ParseContext::iterator
1727 parse(_ParseContext& __pc)
1728 { return _M_f._M_parse(__pc, __format::_Month|__format::_Day); }
1730 template<typename _FormatContext>
1731 typename _FormatContext::iterator
1732 format(const chrono::month_day& __t, _FormatContext& __fc) const
1733 { return _M_f._M_format(__t, __fc); }
1736 __format::__formatter_chrono<_CharT> _M_f;
1739 template<typename _CharT>
1740 struct formatter<chrono::month_day_last, _CharT>
1742 template<typename _ParseContext>
1743 constexpr typename _ParseContext::iterator
1744 parse(_ParseContext& __pc)
1745 { return _M_f._M_parse(__pc, __format::_Month|__format::_Day); }
1747 template<typename _FormatContext>
1748 typename _FormatContext::iterator
1749 format(const chrono::month_day_last& __t, _FormatContext& __fc) const
1750 { return _M_f._M_format(__t, __fc); }
1753 __format::__formatter_chrono<_CharT> _M_f;
1756 template<typename _CharT>
1757 struct formatter<chrono::month_weekday, _CharT>
1759 template<typename _ParseContext>
1760 constexpr typename _ParseContext::iterator
1761 parse(_ParseContext& __pc)
1762 { return _M_f._M_parse(__pc, __format::_Month|__format::_Weekday); }
1764 template<typename _FormatContext>
1765 typename _FormatContext::iterator
1766 format(const chrono::month_weekday& __t, _FormatContext& __fc) const
1767 { return _M_f._M_format(__t, __fc); }
1770 __format::__formatter_chrono<_CharT> _M_f;
1773 template<typename _CharT>
1774 struct formatter<chrono::month_weekday_last, _CharT>
1776 template<typename _ParseContext>
1777 constexpr typename _ParseContext::iterator
1778 parse(_ParseContext& __pc)
1779 { return _M_f._M_parse(__pc, __format::_Month|__format::_Weekday); }
1781 template<typename _FormatContext>
1782 typename _FormatContext::iterator
1783 format(const chrono::month_weekday_last& __t,
1784 _FormatContext& __fc) const
1785 { return _M_f._M_format(__t, __fc); }
1788 __format::__formatter_chrono<_CharT> _M_f;
1791 template<typename _CharT>
1792 struct formatter<chrono::year_month, _CharT>
1794 template<typename _ParseContext>
1795 constexpr typename _ParseContext::iterator
1796 parse(_ParseContext& __pc)
1797 { return _M_f._M_parse(__pc, __format::_Year|__format::_Month); }
1799 template<typename _FormatContext>
1800 typename _FormatContext::iterator
1801 format(const chrono::year_month& __t, _FormatContext& __fc) const
1802 { return _M_f._M_format(__t, __fc); }
1805 __format::__formatter_chrono<_CharT> _M_f;
1808 template<typename _CharT>
1809 struct formatter<chrono::year_month_day, _CharT>
1811 template<typename _ParseContext>
1812 constexpr typename _ParseContext::iterator
1813 parse(_ParseContext& __pc)
1814 { return _M_f._M_parse(__pc, __format::_Date); }
1816 template<typename _FormatContext>
1817 typename _FormatContext::iterator
1818 format(const chrono::year_month_day& __t, _FormatContext& __fc) const
1819 { return _M_f._M_format(__t, __fc); }
1822 __format::__formatter_chrono<_CharT> _M_f;
1825 template<typename _CharT>
1826 struct formatter<chrono::year_month_day_last, _CharT>
1828 template<typename _ParseContext>
1829 constexpr typename _ParseContext::iterator
1830 parse(_ParseContext& __pc)
1831 { return _M_f._M_parse(__pc, __format::_Date); }
1833 template<typename _FormatContext>
1834 typename _FormatContext::iterator
1835 format(const chrono::year_month_day_last& __t,
1836 _FormatContext& __fc) const
1837 { return _M_f._M_format(__t, __fc); }
1840 __format::__formatter_chrono<_CharT> _M_f;
1843 template<typename _CharT>
1844 struct formatter<chrono::year_month_weekday, _CharT>
1846 template<typename _ParseContext>
1847 constexpr typename _ParseContext::iterator
1848 parse(_ParseContext& __pc)
1849 { return _M_f._M_parse(__pc, __format::_Date); }
1851 template<typename _FormatContext>
1852 typename _FormatContext::iterator
1853 format(const chrono::year_month_weekday& __t,
1854 _FormatContext& __fc) const
1855 { return _M_f._M_format(__t, __fc); }
1858 __format::__formatter_chrono<_CharT> _M_f;
1861 template<typename _CharT>
1862 struct formatter<chrono::year_month_weekday_last, _CharT>
1864 template<typename _ParseContext>
1865 constexpr typename _ParseContext::iterator
1866 parse(_ParseContext& __pc)
1867 { return _M_f._M_parse(__pc, __format::_Date); }
1869 template<typename _FormatContext>
1870 typename _FormatContext::iterator
1871 format(const chrono::year_month_weekday_last& __t,
1872 _FormatContext& __fc) const
1873 { return _M_f._M_format(__t, __fc); }
1876 __format::__formatter_chrono<_CharT> _M_f;
1879 template<typename _Rep, typename _Period, typename _CharT>
1880 struct formatter<chrono::hh_mm_ss<chrono::duration<_Rep, _Period>>, _CharT>
1882 template<typename _ParseContext>
1883 constexpr typename _ParseContext::iterator
1884 parse(_ParseContext& __pc)
1885 { return _M_f._M_parse(__pc, __format::_TimeOfDay); }
1887 template<typename _FormatContext>
1888 typename _FormatContext::iterator
1889 format(const chrono::hh_mm_ss<chrono::duration<_Rep, _Period>>& __t,
1890 _FormatContext& __fc) const
1891 { return _M_f._M_format(__t, __fc); }
1894 __format::__formatter_chrono<_CharT> _M_f;
1897 #if _GLIBCXX_USE_CXX11_ABI || ! _GLIBCXX_USE_DUAL_ABI
1898 template<typename _CharT>
1899 struct formatter<chrono::sys_info, _CharT>
1901 template<typename _ParseContext>
1902 constexpr typename _ParseContext::iterator
1903 parse(_ParseContext& __pc)
1904 { return _M_f._M_parse(__pc, __format::_ChronoParts{}); }
1906 template<typename _FormatContext>
1907 typename _FormatContext::iterator
1908 format(const chrono::sys_info& __i, _FormatContext& __fc) const
1909 { return _M_f._M_format(__i, __fc); }
1912 __format::__formatter_chrono<_CharT> _M_f;
1915 template<typename _CharT>
1916 struct formatter<chrono::local_info, _CharT>
1918 template<typename _ParseContext>
1919 constexpr typename _ParseContext::iterator
1920 parse(_ParseContext& __pc)
1921 { return _M_f._M_parse(__pc, __format::_ChronoParts{}); }
1923 template<typename _FormatContext>
1924 typename _FormatContext::iterator
1925 format(const chrono::local_info& __i, _FormatContext& __fc) const
1926 { return _M_f._M_format(__i, __fc); }
1929 __format::__formatter_chrono<_CharT> _M_f;
1933 template<typename _Duration, typename _CharT>
1934 struct formatter<chrono::sys_time<_Duration>, _CharT>
1936 template<typename _ParseContext>
1937 constexpr typename _ParseContext::iterator
1938 parse(_ParseContext& __pc)
1940 auto __next = _M_f._M_parse(__pc, __format::_ZonedDateTime);
1941 if constexpr (!__stream_insertable)
1942 if (_M_f._M_spec._M_chrono_specs.empty())
1943 __format::__invalid_chrono_spec(); // chrono-specs can't be empty
1947 template<typename _FormatContext>
1948 typename _FormatContext::iterator
1949 format(const chrono::sys_time<_Duration>& __t,
1950 _FormatContext& __fc) const
1951 { return _M_f._M_format(__t, __fc); }
1954 static constexpr bool __stream_insertable
1955 = requires (basic_ostream<_CharT>& __os,
1956 chrono::sys_time<_Duration> __t) { __os << __t; };
1958 __format::__formatter_chrono<_CharT> _M_f;
1961 template<typename _Duration, typename _CharT>
1962 struct formatter<chrono::utc_time<_Duration>, _CharT>
1963 : __format::__formatter_chrono<_CharT>
1965 template<typename _ParseContext>
1966 constexpr typename _ParseContext::iterator
1967 parse(_ParseContext& __pc)
1968 { return _M_f._M_parse(__pc, __format::_ZonedDateTime); }
1970 template<typename _FormatContext>
1971 typename _FormatContext::iterator
1972 format(const chrono::utc_time<_Duration>& __t,
1973 _FormatContext& __fc) const
1975 // Adjust by removing leap seconds to get equivalent sys_time.
1976 // We can't just use clock_cast because we want to know if the time
1977 // falls within a leap second insertion, and format seconds as "60
".
1978 using chrono::__detail::__utc_leap_second;
1979 using chrono::seconds;
1980 using chrono::sys_time;
1981 using _CDur = common_type_t<_Duration, seconds>;
1982 const auto __li = chrono::get_leap_second_info(__t);
1983 sys_time<_CDur> __s{__t.time_since_epoch() - __li.elapsed};
1984 if (!__li.is_leap_second) [[likely]]
1985 return _M_f._M_format(__s, __fc);
1987 return _M_f._M_format(__utc_leap_second(__s), __fc);
1991 friend formatter<chrono::__detail::__utc_leap_second<_Duration>, _CharT>;
1993 __format::__formatter_chrono<_CharT> _M_f;
1996 template<typename _Duration, typename _CharT>
1997 struct formatter<chrono::tai_time<_Duration>, _CharT>
1998 : __format::__formatter_chrono<_CharT>
2000 template<typename _ParseContext>
2001 constexpr typename _ParseContext::iterator
2002 parse(_ParseContext& __pc)
2003 { return _M_f._M_parse(__pc, __format::_ZonedDateTime); }
2005 template<typename _FormatContext>
2006 typename _FormatContext::iterator
2007 format(const chrono::tai_time<_Duration>& __t,
2008 _FormatContext& __fc) const
2010 // Convert to __local_time_fmt with abbrev "TAI
" and offset 0s.
2012 // Offset is 1970y/January/1 - 1958y/January/1
2013 constexpr chrono::days __tai_offset = chrono::days(4383);
2014 using _CDur = common_type_t<_Duration, chrono::days>;
2015 chrono::local_time<_CDur> __lt(__t.time_since_epoch() - __tai_offset);
2016 const string __abbrev("TAI
", 3);
2017 const chrono::seconds __off = 0s;
2018 const auto __lf = chrono::local_time_format(__lt, &__abbrev, &__off);
2019 return _M_f._M_format(__lf, __fc);
2023 __format::__formatter_chrono<_CharT> _M_f;
2026 template<typename _Duration, typename _CharT>
2027 struct formatter<chrono::gps_time<_Duration>, _CharT>
2028 : __format::__formatter_chrono<_CharT>
2030 template<typename _ParseContext>
2031 constexpr typename _ParseContext::iterator
2032 parse(_ParseContext& __pc)
2033 { return _M_f._M_parse(__pc, __format::_ZonedDateTime); }
2035 template<typename _FormatContext>
2036 typename _FormatContext::iterator
2037 format(const chrono::gps_time<_Duration>& __t,
2038 _FormatContext& __fc) const
2040 // Convert to __local_time_fmt with abbrev "GPS
" and offset 0s.
2042 // Offset is 1980y/January/Sunday[1] - 1970y/January/1
2043 constexpr chrono::days __gps_offset = chrono::days(3657);
2044 using _CDur = common_type_t<_Duration, chrono::days>;
2045 chrono::local_time<_CDur> __lt(__t.time_since_epoch() + __gps_offset);
2046 const string __abbrev("GPS
", 3);
2047 const chrono::seconds __off = 0s;
2048 const auto __lf = chrono::local_time_format(__lt, &__abbrev, &__off);
2049 return _M_f._M_format(__lf, __fc);
2053 __format::__formatter_chrono<_CharT> _M_f;
2056 template<typename _Duration, typename _CharT>
2057 struct formatter<chrono::file_time<_Duration>, _CharT>
2059 template<typename _ParseContext>
2060 constexpr typename _ParseContext::iterator
2061 parse(_ParseContext& __pc)
2062 { return _M_f._M_parse(__pc, __format::_ZonedDateTime); }
2064 template<typename _FormatContext>
2065 typename _FormatContext::iterator
2066 format(const chrono::file_time<_Duration>& __t,
2067 _FormatContext& __ctx) const
2069 using namespace chrono;
2070 return _M_f._M_format(chrono::clock_cast<system_clock>(__t), __ctx);
2074 __format::__formatter_chrono<_CharT> _M_f;
2077 template<typename _Duration, typename _CharT>
2078 struct formatter<chrono::local_time<_Duration>, _CharT>
2080 template<typename _ParseContext>
2081 constexpr typename _ParseContext::iterator
2082 parse(_ParseContext& __pc)
2083 { return _M_f._M_parse(__pc, __format::_DateTime); }
2085 template<typename _FormatContext>
2086 typename _FormatContext::iterator
2087 format(const chrono::local_time<_Duration>& __t,
2088 _FormatContext& __ctx) const
2089 { return _M_f._M_format(__t, __ctx); }
2092 __format::__formatter_chrono<_CharT> _M_f;
2095 template<typename _Duration, typename _CharT>
2096 struct formatter<chrono::__detail::__local_time_fmt<_Duration>, _CharT>
2098 template<typename _ParseContext>
2099 constexpr typename _ParseContext::iterator
2100 parse(_ParseContext& __pc)
2101 { return _M_f._M_parse(__pc, __format::_ZonedDateTime); }
2103 template<typename _FormatContext>
2104 typename _FormatContext::iterator
2105 format(const chrono::__detail::__local_time_fmt<_Duration>& __t,
2106 _FormatContext& __ctx) const
2107 { return _M_f._M_format(__t, __ctx); }
2110 __format::__formatter_chrono<_CharT> _M_f;
2113 #if _GLIBCXX_USE_CXX11_ABI || ! _GLIBCXX_USE_DUAL_ABI
2114 template<typename _Duration, typename _TimeZonePtr, typename _CharT>
2115 struct formatter<chrono::zoned_time<_Duration, _TimeZonePtr>, _CharT>
2116 : formatter<chrono::__detail::__local_time_fmt<_Duration>, _CharT>
2118 template<typename _FormatContext>
2119 typename _FormatContext::iterator
2120 format(const chrono::zoned_time<_Duration, _TimeZonePtr>& __tp,
2121 _FormatContext& __ctx) const
2123 using chrono::__detail::__local_time_fmt;
2124 using _Base = formatter<__local_time_fmt<_Duration>, _CharT>;
2125 const chrono::sys_info __info = __tp.get_info();
2126 const auto __lf = chrono::local_time_format(__tp.get_local_time(),
2129 return _Base::format(__lf, __ctx);
2134 // Partial specialization needed for %c formatting of __utc_leap_second.
2135 template<typename _Duration, typename _CharT>
2136 struct formatter<chrono::__detail::__utc_leap_second<_Duration>, _CharT>
2137 : formatter<chrono::utc_time<_Duration>, _CharT>
2139 template<typename _FormatContext>
2140 typename _FormatContext::iterator
2141 format(const chrono::__detail::__utc_leap_second<_Duration>& __t,
2142 _FormatContext& __fc) const
2143 { return this->_M_f._M_format(__t, __fc); }
2154 template<typename _Duration = seconds>
2157 static_assert(is_same_v<common_type_t<_Duration, seconds>, _Duration>);
2160 _Parser(__format::_ChronoParts __need) : _M_need(__need) { }
2162 _Parser(_Parser&&) = delete;
2163 void operator=(_Parser&&) = delete;
2165 _Duration _M_time{}; // since midnight
2166 sys_days _M_sys_days{};
2167 year_month_day _M_ymd{};
2169 __format::_ChronoParts _M_need;
2170 unsigned _M_is_leap_second : 1 {};
2171 unsigned _M_reserved : 15 {};
2173 template<typename _CharT, typename _Traits, typename _Alloc>
2174 basic_istream<_CharT, _Traits>&
2175 operator()(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt,
2176 basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr,
2177 minutes* __offset = nullptr);
2180 // Read an unsigned integer from the stream and return it.
2181 // Extract no more than __n digits. Set failbit if an integer isn't read.
2182 template<typename _CharT, typename _Traits>
2183 static int_least32_t
2184 _S_read_unsigned(basic_istream<_CharT, _Traits>& __is,
2185 ios_base::iostate& __err, int __n)
2187 int_least32_t __val = _S_try_read_digit(__is, __err);
2188 if (__val == -1) [[unlikely]]
2189 __err |= ios_base::failbit;
2192 int __n1 = (std::min)(__n, 9);
2193 // Cannot overflow __val unless we read more than 9 digits
2194 for (int __i = 1; __i < __n1; ++__i)
2195 if (auto __dig = _S_try_read_digit(__is, __err); __dig != -1)
2201 while (__n1++ < __n) [[unlikely]]
2202 if (auto __dig = _S_try_read_digit(__is, __err); __dig != -1)
2204 if (__builtin_mul_overflow(__val, 10, &__val)
2205 || __builtin_add_overflow(__val, __dig, &__val))
2207 __err |= ios_base::failbit;
2215 // Read an unsigned integer from the stream and return it.
2216 // Extract no more than __n digits. Set failbit if an integer isn't read.
2217 template<typename _CharT, typename _Traits>
2218 static int_least32_t
2219 _S_read_signed(basic_istream<_CharT, _Traits>& __is,
2220 ios_base::iostate& __err, int __n)
2222 auto __sign = __is.peek();
2223 if (__sign == '-' || __sign == '+')
2225 int_least32_t __val = _S_read_unsigned(__is, __err, __n);
2226 if (__err & ios_base::failbit)
2228 if (__sign == '-') [[unlikely]]
2234 // Read a digit from the stream and return it, or return -1.
2235 // If no digit is read eofbit will be set (but not failbit).
2236 template<typename _CharT, typename _Traits>
2237 static int_least32_t
2238 _S_try_read_digit(basic_istream<_CharT, _Traits>& __is,
2239 ios_base::iostate& __err)
2241 int_least32_t __val = -1;
2242 auto __i = __is.peek();
2243 if (!_Traits::eq_int_type(__i, _Traits::eof())) [[likely]]
2245 _CharT __c = _Traits::to_char_type(__i);
2246 if (_CharT('0') <= __c && __c <= _CharT('9')) [[likely]]
2249 __val = __c - _CharT('0');
2253 __err |= ios_base::eofbit;
2257 // Read the specified character and return true.
2258 // If the character is not found, set failbit and return false.
2259 template<typename _CharT, typename _Traits>
2261 _S_read_chr(basic_istream<_CharT, _Traits>& __is,
2262 ios_base::iostate& __err, _CharT __c)
2264 auto __i = __is.peek();
2265 if (_Traits::eq_int_type(__i, _Traits::eof()))
2266 __err |= ios_base::eofbit;
2267 else if (_Traits::to_char_type(__i) == __c) [[likely]]
2272 __err |= ios_base::failbit;
2277 template<typename _Duration>
2278 using _Parser_t = _Parser<common_type_t<_Duration, seconds>>;
2280 } // namespace __detail
2283 template<typename _CharT, typename _Traits, typename _Rep, typename _Period,
2284 typename _Alloc = allocator<_CharT>>
2285 inline basic_istream<_CharT, _Traits>&
2286 from_stream(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt,
2287 duration<_Rep, _Period>& __d,
2288 basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr,
2289 minutes* __offset = nullptr)
2291 auto __need = __format::_ChronoParts::_TimeOfDay;
2292 __detail::_Parser_t<duration<_Rep, _Period>> __p(__need);
2293 if (__p(__is, __fmt, __abbrev, __offset))
2294 __d = chrono::duration_cast<duration<_Rep, _Period>>(__p._M_time);
2298 template<typename _CharT, typename _Traits>
2299 inline basic_ostream<_CharT, _Traits>&
2300 operator<<(basic_ostream<_CharT, _Traits>& __os, const day& __d)
2302 using _Ctx = __format::__format_context<_CharT>;
2303 using _Str = basic_string_view<_CharT>;
2304 _Str __s = _GLIBCXX_WIDEN("{:02d} is not a valid day
");
2306 __s = __s.substr(0, 6);
2307 auto __u = (unsigned)__d;
2308 __os << std::vformat(__s, make_format_args<_Ctx>(__u));
2312 template<typename _CharT, typename _Traits,
2313 typename _Alloc = allocator<_CharT>>
2314 inline basic_istream<_CharT, _Traits>&
2315 from_stream(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt,
2317 basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr,
2318 minutes* __offset = nullptr)
2320 __detail::_Parser<> __p(__format::_ChronoParts::_Day);
2321 if (__p(__is, __fmt, __abbrev, __offset))
2322 __d = __p._M_ymd.day();
2326 template<typename _CharT, typename _Traits>
2327 inline basic_ostream<_CharT, _Traits>&
2328 operator<<(basic_ostream<_CharT, _Traits>& __os, const month& __m)
2330 using _Ctx = __format::__format_context<_CharT>;
2331 using _Str = basic_string_view<_CharT>;
2332 _Str __s = _GLIBCXX_WIDEN("{:L%b}{} is not a valid month
");
2334 __os << std::vformat(__os.getloc(), __s.substr(0, 6),
2335 make_format_args<_Ctx>(__m));
2338 auto __u = (unsigned)__m;
2339 __os << std::vformat(__s.substr(6), make_format_args<_Ctx>(__u));
2344 template<typename _CharT, typename _Traits,
2345 typename _Alloc = allocator<_CharT>>
2346 inline basic_istream<_CharT, _Traits>&
2347 from_stream(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt,
2349 basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr,
2350 minutes* __offset = nullptr)
2352 __detail::_Parser<> __p(__format::_ChronoParts::_Month);
2353 if (__p(__is, __fmt, __abbrev, __offset))
2354 __m = __p._M_ymd.month();
2358 template<typename _CharT, typename _Traits>
2359 inline basic_ostream<_CharT, _Traits>&
2360 operator<<(basic_ostream<_CharT, _Traits>& __os, const year& __y)
2362 using _Ctx = __format::__format_context<_CharT>;
2363 using _Str = basic_string_view<_CharT>;
2364 _Str __s = _GLIBCXX_WIDEN("-{:04d} is not a valid year
");
2366 __s = __s.substr(0, 7);
2368 if (__i >= 0) [[likely]]
2369 __s.remove_prefix(1);
2372 __os << std::vformat(__s, make_format_args<_Ctx>(__i));
2376 template<typename _CharT, typename _Traits,
2377 typename _Alloc = allocator<_CharT>>
2378 inline basic_istream<_CharT, _Traits>&
2379 from_stream(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt,
2381 basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr,
2382 minutes* __offset = nullptr)
2384 __detail::_Parser<> __p(__format::_ChronoParts::_Year);
2385 if (__p(__is, __fmt, __abbrev, __offset))
2386 __y = __p._M_ymd.year();
2390 template<typename _CharT, typename _Traits>
2391 inline basic_ostream<_CharT, _Traits>&
2392 operator<<(basic_ostream<_CharT, _Traits>& __os, const weekday& __wd)
2394 using _Ctx = __format::__format_context<_CharT>;
2395 using _Str = basic_string_view<_CharT>;
2396 _Str __s = _GLIBCXX_WIDEN("{:L%a}{} is not a valid weekday
");
2398 __os << std::vformat(__os.getloc(), __s.substr(0, 6),
2399 make_format_args<_Ctx>(__wd));
2402 auto __c = __wd.c_encoding();
2403 __os << std::vformat(__s.substr(6), make_format_args<_Ctx>(__c));
2408 template<typename _CharT, typename _Traits,
2409 typename _Alloc = allocator<_CharT>>
2410 inline basic_istream<_CharT, _Traits>&
2411 from_stream(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt,
2413 basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr,
2414 minutes* __offset = nullptr)
2416 __detail::_Parser<> __p(__format::_ChronoParts::_Weekday);
2417 if (__p(__is, __fmt, __abbrev, __offset))
2422 template<typename _CharT, typename _Traits>
2423 inline basic_ostream<_CharT, _Traits>&
2424 operator<<(basic_ostream<_CharT, _Traits>& __os,
2425 const weekday_indexed& __wdi)
2427 // The standard says to format wdi.weekday() and wdi.index() using
2428 // either "{:L}[{}]
" or "{:L}[{} is not a valid index]
". The {:L} spec
2429 // means to format the weekday using ostringstream, so just do that.
2430 basic_stringstream<_CharT> __os2;
2431 __os2.imbue(__os.getloc());
2432 __os2 << __wdi.weekday();
2433 const auto __i = __wdi.index();
2434 basic_string_view<_CharT> __s
2435 = _GLIBCXX_WIDEN("[ is not a valid index]
");
2437 __os2 << std::format(_GLIBCXX_WIDEN("{}
"), __i);
2438 if (__i >= 1 && __i <= 5)
2439 __os2 << __s.back();
2441 __os2 << __s.substr(1);
2442 __os << __os2.view();
2446 template<typename _CharT, typename _Traits>
2447 inline basic_ostream<_CharT, _Traits>&
2448 operator<<(basic_ostream<_CharT, _Traits>& __os,
2449 const weekday_last& __wdl)
2451 // As above, just write straight to a stringstream, as if by "{:L}[last]
"
2452 basic_stringstream<_CharT> __os2;
2453 __os2.imbue(__os.getloc());
2454 __os2 << __wdl.weekday() << _GLIBCXX_WIDEN("[last]
");
2455 __os << __os2.view();
2459 template<typename _CharT, typename _Traits>
2460 inline basic_ostream<_CharT, _Traits>&
2461 operator<<(basic_ostream<_CharT, _Traits>& __os, const month_day& __md)
2463 // As above, just write straight to a stringstream, as if by "{:L}/{}
"
2464 basic_stringstream<_CharT> __os2;
2465 __os2.imbue(__os.getloc());
2466 __os2 << __md.month();
2467 if constexpr (is_same_v<_CharT, char>)
2471 __os2 << __md.day();
2472 __os << __os2.view();
2476 template<typename _CharT, typename _Traits,
2477 typename _Alloc = allocator<_CharT>>
2478 inline basic_istream<_CharT, _Traits>&
2479 from_stream(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt,
2481 basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr,
2482 minutes* __offset = nullptr)
2484 using __format::_ChronoParts;
2485 auto __need = _ChronoParts::_Month | _ChronoParts::_Day;
2486 __detail::_Parser<> __p(__need);
2487 if (__p(__is, __fmt, __abbrev, __offset))
2488 __md = month_day(__p._M_ymd.month(), __p._M_ymd.day());
2492 template<typename _CharT, typename _Traits>
2493 inline basic_ostream<_CharT, _Traits>&
2494 operator<<(basic_ostream<_CharT, _Traits>& __os,
2495 const month_day_last& __mdl)
2497 // As above, just write straight to a stringstream, as if by "{:L}/last
"
2498 basic_stringstream<_CharT> __os2;
2499 __os2.imbue(__os.getloc());
2500 __os2 << __mdl.month() << _GLIBCXX_WIDEN("/last
");
2501 __os << __os2.view();
2505 template<typename _CharT, typename _Traits>
2506 inline basic_ostream<_CharT, _Traits>&
2507 operator<<(basic_ostream<_CharT, _Traits>& __os,
2508 const month_weekday& __mwd)
2510 // As above, just write straight to a stringstream, as if by "{:L}/{:L}
"
2511 basic_stringstream<_CharT> __os2;
2512 __os2.imbue(__os.getloc());
2513 __os2 << __mwd.month();
2514 if constexpr (is_same_v<_CharT, char>)
2518 __os2 << __mwd.weekday_indexed();
2519 __os << __os2.view();
2523 template<typename _CharT, typename _Traits>
2524 inline basic_ostream<_CharT, _Traits>&
2525 operator<<(basic_ostream<_CharT, _Traits>& __os,
2526 const month_weekday_last& __mwdl)
2528 // As above, just write straight to a stringstream, as if by "{:L}/{:L}
"
2529 basic_stringstream<_CharT> __os2;
2530 __os2.imbue(__os.getloc());
2531 __os2 << __mwdl.month();
2532 if constexpr (is_same_v<_CharT, char>)
2536 __os2 << __mwdl.weekday_last();
2537 __os << __os2.view();
2541 template<typename _CharT, typename _Traits>
2542 inline basic_ostream<_CharT, _Traits>&
2543 operator<<(basic_ostream<_CharT, _Traits>& __os, const year_month& __ym)
2545 // As above, just write straight to a stringstream, as if by "{}/{:L}
"
2546 basic_stringstream<_CharT> __os2;
2547 __os2.imbue(__os.getloc());
2548 __os2 << __ym.year();
2549 if constexpr (is_same_v<_CharT, char>)
2553 __os2 << __ym.month();
2554 __os << __os2.view();
2558 template<typename _CharT, typename _Traits,
2559 typename _Alloc = allocator<_CharT>>
2560 inline basic_istream<_CharT, _Traits>&
2561 from_stream(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt,
2563 basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr,
2564 minutes* __offset = nullptr)
2566 using __format::_ChronoParts;
2567 auto __need = _ChronoParts::_Year | _ChronoParts::_Month;
2568 __detail::_Parser<> __p(__need);
2569 if (__p(__is, __fmt, __abbrev, __offset))
2570 __ym = year_month(__p._M_ymd.year(), __p._M_ymd.month());
2574 template<typename _CharT, typename _Traits>
2575 inline basic_ostream<_CharT, _Traits>&
2576 operator<<(basic_ostream<_CharT, _Traits>& __os,
2577 const year_month_day& __ymd)
2579 using _Ctx = __format::__format_context<_CharT>;
2580 using _Str = basic_string_view<_CharT>;
2581 _Str __s = _GLIBCXX_WIDEN("{:%F} is not a valid date
");
2582 __os << std::vformat(__ymd.ok() ? __s.substr(0, 5) : __s,
2583 make_format_args<_Ctx>(__ymd));
2587 template<typename _CharT, typename _Traits,
2588 typename _Alloc = allocator<_CharT>>
2589 inline basic_istream<_CharT, _Traits>&
2590 from_stream(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt,
2591 year_month_day& __ymd,
2592 basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr,
2593 minutes* __offset = nullptr)
2595 using __format::_ChronoParts;
2596 auto __need = _ChronoParts::_Year | _ChronoParts::_Month
2597 | _ChronoParts::_Day;
2598 __detail::_Parser<> __p(__need);
2599 if (__p(__is, __fmt, __abbrev, __offset))
2604 template<typename _CharT, typename _Traits>
2605 inline basic_ostream<_CharT, _Traits>&
2606 operator<<(basic_ostream<_CharT, _Traits>& __os,
2607 const year_month_day_last& __ymdl)
2609 // As above, just write straight to a stringstream, as if by "{}/{:L}
"
2610 basic_stringstream<_CharT> __os2;
2611 __os2.imbue(__os.getloc());
2612 __os2 << __ymdl.year();
2613 if constexpr (is_same_v<_CharT, char>)
2617 __os2 << __ymdl.month_day_last();
2618 __os << __os2.view();
2622 template<typename _CharT, typename _Traits>
2623 inline basic_ostream<_CharT, _Traits>&
2624 operator<<(basic_ostream<_CharT, _Traits>& __os,
2625 const year_month_weekday& __ymwd)
2627 // As above, just write straight to a stringstream, as if by
2629 basic_stringstream<_CharT> __os2;
2630 __os2.imbue(__os.getloc());
2632 if constexpr (is_same_v<_CharT, char>)
2636 __os2 << __ymwd.year() << __slash << __ymwd.month() << __slash
2637 << __ymwd.weekday_indexed();
2638 __os << __os2.view();
2642 template<typename _CharT, typename _Traits>
2643 inline basic_ostream<_CharT, _Traits>&
2644 operator<<(basic_ostream<_CharT, _Traits>& __os,
2645 const year_month_weekday_last& __ymwdl)
2647 // As above, just write straight to a stringstream, as if by
2649 basic_stringstream<_CharT> __os2;
2650 __os2.imbue(__os.getloc());
2652 if constexpr (is_same_v<_CharT, char>)
2656 __os2 << __ymwdl.year() << __slash << __ymwdl.month() << __slash
2657 << __ymwdl.weekday_last();
2658 __os << __os2.view();
2662 template<typename _CharT, typename _Traits, typename _Duration>
2663 inline basic_ostream<_CharT, _Traits>&
2664 operator<<(basic_ostream<_CharT, _Traits>& __os,
2665 const hh_mm_ss<_Duration>& __hms)
2667 return __os << format(__os.getloc(), _GLIBCXX_WIDEN("{:L%T}
"), __hms);
2670 #if _GLIBCXX_USE_CXX11_ABI || ! _GLIBCXX_USE_DUAL_ABI
2672 template<typename _CharT, typename _Traits>
2673 basic_ostream<_CharT, _Traits>&
2674 operator<<(basic_ostream<_CharT, _Traits>& __os, const sys_info& __i)
2676 __os << '[' << __i.begin << ',' << __i.end
2677 << ',' << hh_mm_ss(__i.offset) << ',' << __i.save
2678 << ',' << __i.abbrev << ']';
2683 template<typename _CharT, typename _Traits>
2684 basic_ostream<_CharT, _Traits>&
2685 operator<<(basic_ostream<_CharT, _Traits>& __os, const local_info& __li)
2688 if (__li.result == local_info::unique)
2692 if (__li.result == local_info::nonexistent)
2693 __os << "nonexistent
";
2695 __os << "ambiguous
";
2696 __os << " local time between
" << __li.first;
2697 __os << " and
" << __li.second;
2703 template<typename _CharT, typename _Traits, typename _Duration,
2704 typename _TimeZonePtr>
2705 inline basic_ostream<_CharT, _Traits>&
2706 operator<<(basic_ostream<_CharT, _Traits>& __os,
2707 const zoned_time<_Duration, _TimeZonePtr>& __t)
2709 __os << format(__os.getloc(), _GLIBCXX_WIDEN("{:L%F %T %Z}
"), __t);
2714 template<typename _CharT, typename _Traits, typename _Duration>
2715 requires (!treat_as_floating_point_v<typename _Duration::rep>)
2716 && ratio_less_v<typename _Duration::period, days::period>
2717 inline basic_ostream<_CharT, _Traits>&
2718 operator<<(basic_ostream<_CharT, _Traits>& __os,
2719 const sys_time<_Duration>& __tp)
2721 __os << std::format(__os.getloc(), _GLIBCXX_WIDEN("{:L%F %T}
"), __tp);
2725 template<typename _CharT, typename _Traits>
2726 inline basic_ostream<_CharT, _Traits>&
2727 operator<<(basic_ostream<_CharT, _Traits>& __os, const sys_days& __dp)
2729 __os << year_month_day{__dp};
2733 template<typename _CharT, typename _Traits, typename _Duration,
2734 typename _Alloc = allocator<_CharT>>
2735 basic_istream<_CharT, _Traits>&
2736 from_stream(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt,
2737 sys_time<_Duration>& __tp,
2738 basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr,
2739 minutes* __offset = nullptr)
2744 using __format::_ChronoParts;
2745 auto __need = _ChronoParts::_Year | _ChronoParts::_Month
2746 | _ChronoParts::_Day | _ChronoParts::_TimeOfDay;
2747 __detail::_Parser_t<_Duration> __p(__need);
2748 if (__p(__is, __fmt, __abbrev, __offset))
2750 if (__p._M_is_leap_second)
2751 __is.setstate(ios_base::failbit);
2754 auto __st = __p._M_sys_days + __p._M_time - *__offset;
2755 __tp = chrono::time_point_cast<_Duration>(__st);
2761 template<typename _CharT, typename _Traits, typename _Duration>
2762 inline basic_ostream<_CharT, _Traits>&
2763 operator<<(basic_ostream<_CharT, _Traits>& __os,
2764 const utc_time<_Duration>& __t)
2766 __os << std::format(__os.getloc(), _GLIBCXX_WIDEN("{:L%F %T}
"), __t);
2770 template<typename _CharT, typename _Traits, typename _Duration,
2771 typename _Alloc = allocator<_CharT>>
2772 inline basic_istream<_CharT, _Traits>&
2773 from_stream(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt,
2774 utc_time<_Duration>& __tp,
2775 basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr,
2776 minutes* __offset = nullptr)
2781 using __format::_ChronoParts;
2782 auto __need = _ChronoParts::_Year | _ChronoParts::_Month
2783 | _ChronoParts::_Day | _ChronoParts::_TimeOfDay;
2784 __detail::_Parser_t<_Duration> __p(__need);
2785 if (__p(__is, __fmt, __abbrev, __offset))
2787 // Converting to utc_time before adding _M_time is necessary for
2788 // "23:59:60
" to correctly produce a time within a leap second.
2789 auto __ut = utc_clock::from_sys(__p._M_sys_days) + __p._M_time
2791 __tp = chrono::time_point_cast<_Duration>(__ut);
2796 template<typename _CharT, typename _Traits, typename _Duration>
2797 inline basic_ostream<_CharT, _Traits>&
2798 operator<<(basic_ostream<_CharT, _Traits>& __os,
2799 const tai_time<_Duration>& __t)
2801 __os << std::format(__os.getloc(), _GLIBCXX_WIDEN("{:L%F %T}
"), __t);
2805 template<typename _CharT, typename _Traits, typename _Duration,
2806 typename _Alloc = allocator<_CharT>>
2807 inline basic_istream<_CharT, _Traits>&
2808 from_stream(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt,
2809 tai_time<_Duration>& __tp,
2810 basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr,
2811 minutes* __offset = nullptr)
2816 using __format::_ChronoParts;
2817 auto __need = _ChronoParts::_Year | _ChronoParts::_Month
2818 | _ChronoParts::_Day | _ChronoParts::_TimeOfDay;
2819 __detail::_Parser_t<_Duration> __p(__need);
2820 if (__p(__is, __fmt, __abbrev, __offset))
2822 if (__p._M_is_leap_second)
2823 __is.setstate(ios_base::failbit);
2826 auto __st = __p._M_sys_days + __p._M_time - *__offset;
2827 auto __tt = tai_clock::from_utc(utc_clock::from_sys(__st));
2828 __tp = chrono::time_point_cast<_Duration>(__tt);
2834 template<typename _CharT, typename _Traits, typename _Duration>
2835 inline basic_ostream<_CharT, _Traits>&
2836 operator<<(basic_ostream<_CharT, _Traits>& __os,
2837 const gps_time<_Duration>& __t)
2839 __os << std::format(__os.getloc(), _GLIBCXX_WIDEN("{:L%F %T}
"), __t);
2843 template<typename _CharT, typename _Traits, typename _Duration,
2844 typename _Alloc = allocator<_CharT>>
2845 inline basic_istream<_CharT, _Traits>&
2846 from_stream(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt,
2847 gps_time<_Duration>& __tp,
2848 basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr,
2849 minutes* __offset = nullptr)
2854 using __format::_ChronoParts;
2855 auto __need = _ChronoParts::_Year | _ChronoParts::_Month
2856 | _ChronoParts::_Day | _ChronoParts::_TimeOfDay;
2857 __detail::_Parser_t<_Duration> __p(__need);
2858 if (__p(__is, __fmt, __abbrev, __offset))
2860 if (__p._M_is_leap_second)
2861 __is.setstate(ios_base::failbit);
2864 auto __st = __p._M_sys_days + __p._M_time - *__offset;
2865 auto __tt = gps_clock::from_utc(utc_clock::from_sys(__st));
2866 __tp = chrono::time_point_cast<_Duration>(__tt);
2872 template<typename _CharT, typename _Traits, typename _Duration>
2873 inline basic_ostream<_CharT, _Traits>&
2874 operator<<(basic_ostream<_CharT, _Traits>& __os,
2875 const file_time<_Duration>& __t)
2877 __os << std::format(__os.getloc(), _GLIBCXX_WIDEN("{:L%F %T}
"), __t);
2881 template<typename _CharT, typename _Traits, typename _Duration,
2882 typename _Alloc = allocator<_CharT>>
2883 inline basic_istream<_CharT, _Traits>&
2884 from_stream(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt,
2885 file_time<_Duration>& __tp,
2886 basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr,
2887 minutes* __offset = nullptr)
2889 sys_time<_Duration> __st;
2890 if (chrono::from_stream(__is, __fmt, __st, __abbrev, __offset))
2891 __tp = chrono::time_point_cast<_Duration>(file_clock::from_sys(__st));
2895 template<typename _CharT, typename _Traits, typename _Duration>
2896 inline basic_ostream<_CharT, _Traits>&
2897 operator<<(basic_ostream<_CharT, _Traits>& __os,
2898 const local_time<_Duration>& __lt)
2900 __os << sys_time<_Duration>{__lt.time_since_epoch()};
2904 template<typename _CharT, typename _Traits, typename _Duration,
2905 typename _Alloc = allocator<_CharT>>
2906 basic_istream<_CharT, _Traits>&
2907 from_stream(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt,
2908 local_time<_Duration>& __tp,
2909 basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr,
2910 minutes* __offset = nullptr)
2912 using __format::_ChronoParts;
2913 auto __need = _ChronoParts::_Year | _ChronoParts::_Month
2914 | _ChronoParts::_Day | _ChronoParts::_TimeOfDay;
2915 __detail::_Parser_t<_Duration> __p(__need);
2916 if (__p(__is, __fmt, __abbrev, __offset))
2918 days __d = __p._M_sys_days.time_since_epoch();
2919 auto __t = local_days(__d) + __p._M_time; // ignore offset
2920 __tp = chrono::time_point_cast<_Duration>(__t);
2925 // [time.parse] parsing
2929 template<typename _Parsable, typename _CharT,
2930 typename _Traits = std::char_traits<_CharT>,
2931 typename... _OptArgs>
2932 concept __parsable = requires (basic_istream<_CharT, _Traits>& __is,
2933 const _CharT* __fmt, _Parsable& __tp,
2934 _OptArgs*... __args)
2935 { from_stream(__is, __fmt, __tp, __args...); };
2937 template<typename _Parsable, typename _CharT,
2938 typename _Traits = char_traits<_CharT>,
2939 typename _Alloc = allocator<_CharT>>
2943 using __string_type = basic_string<_CharT, _Traits, _Alloc>;
2946 _Parse(const _CharT* __fmt, _Parsable& __tp,
2947 basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr,
2948 minutes* __offset = nullptr)
2949 : _M_fmt(__fmt), _M_tp(std::__addressof(__tp)),
2950 _M_abbrev(__abbrev), _M_offset(__offset)
2953 _Parse(_Parse&&) = delete;
2954 _Parse& operator=(_Parse&&) = delete;
2957 using __stream_type = basic_istream<_CharT, _Traits>;
2959 const _CharT* const _M_fmt;
2960 _Parsable* const _M_tp;
2961 __string_type* const _M_abbrev;
2962 minutes* const _M_offset;
2964 friend __stream_type&
2965 operator>>(__stream_type& __is, _Parse&& __p)
2968 from_stream(__is, __p._M_fmt, *__p._M_tp, __p._M_abbrev,
2970 else if (__p._M_abbrev)
2971 from_stream(__is, __p._M_fmt, *__p._M_tp, __p._M_abbrev);
2973 from_stream(__is, __p._M_fmt, *__p._M_tp);
2977 friend void operator>>(__stream_type&, _Parse&) = delete;
2978 friend void operator>>(__stream_type&, const _Parse&) = delete;
2980 } // namespace __detail
2982 template<typename _CharT, __detail::__parsable<_CharT> _Parsable>
2983 [[nodiscard, __gnu__::__access__(__read_only__, 1)]]
2985 parse(const _CharT* __fmt, _Parsable& __tp)
2986 { return __detail::_Parse<_Parsable, _CharT>(__fmt, __tp); }
2988 template<typename _CharT, typename _Traits, typename _Alloc,
2989 __detail::__parsable<_CharT, _Traits> _Parsable>
2992 parse(const basic_string<_CharT, _Traits, _Alloc>& __fmt, _Parsable& __tp)
2994 return __detail::_Parse<_Parsable, _CharT, _Traits>(__fmt.c_str(), __tp);
2997 template<typename _CharT, typename _Traits, typename _Alloc,
2998 typename _StrT = basic_string<_CharT, _Traits, _Alloc>,
2999 __detail::__parsable<_CharT, _Traits, _StrT> _Parsable>
3000 [[nodiscard, __gnu__::__access__(__read_only__, 1)]]
3002 parse(const _CharT* __fmt, _Parsable& __tp,
3003 basic_string<_CharT, _Traits, _Alloc>& __abbrev)
3005 auto __pa = std::__addressof(__abbrev);
3006 return __detail::_Parse<_Parsable, _CharT, _Traits, _Alloc>(__fmt, __tp,
3010 template<typename _CharT, typename _Traits, typename _Alloc,
3011 typename _StrT = basic_string<_CharT, _Traits, _Alloc>,
3012 __detail::__parsable<_CharT, _Traits, _StrT> _Parsable>
3015 parse(const basic_string<_CharT, _Traits, _Alloc>& __fmt, _Parsable& __tp,
3016 basic_string<_CharT, _Traits, _Alloc>& __abbrev)
3018 auto __pa = std::__addressof(__abbrev);
3019 return __detail::_Parse<_Parsable, _CharT, _Traits, _Alloc>(__fmt.c_str(),
3023 template<typename _CharT, typename _Traits = char_traits<_CharT>,
3024 typename _StrT = basic_string<_CharT, _Traits>,
3025 __detail::__parsable<_CharT, _Traits, _StrT, minutes> _Parsable>
3026 [[nodiscard, __gnu__::__access__(__read_only__, 1)]]
3028 parse(const _CharT* __fmt, _Parsable& __tp, minutes& __offset)
3030 return __detail::_Parse<_Parsable, _CharT>(__fmt, __tp, nullptr,
3034 template<typename _CharT, typename _Traits, typename _Alloc,
3035 typename _StrT = basic_string<_CharT, _Traits>,
3036 __detail::__parsable<_CharT, _Traits, _StrT, minutes> _Parsable>
3039 parse(const basic_string<_CharT, _Traits, _Alloc>& __fmt, _Parsable& __tp,
3042 return __detail::_Parse<_Parsable, _CharT, _Traits, _Alloc>(__fmt.c_str(),
3047 template<typename _CharT, typename _Traits, typename _Alloc,
3048 typename _StrT = basic_string<_CharT, _Traits, _Alloc>,
3049 __detail::__parsable<_CharT, _Traits, _StrT, minutes> _Parsable>
3050 [[nodiscard, __gnu__::__access__(__read_only__, 1)]]
3052 parse(const _CharT* __fmt, _Parsable& __tp,
3053 basic_string<_CharT, _Traits, _Alloc>& __abbrev, minutes& __offset)
3055 auto __pa = std::__addressof(__abbrev);
3056 return __detail::_Parse<_Parsable, _CharT, _Traits, _Alloc>(__fmt, __tp,
3061 template<typename _CharT, typename _Traits, typename _Alloc,
3062 typename _StrT = basic_string<_CharT, _Traits, _Alloc>,
3063 __detail::__parsable<_CharT, _Traits, _StrT, minutes> _Parsable>
3066 parse(const basic_string<_CharT, _Traits, _Alloc>& __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.c_str(),
3076 template<typename _Duration>
3077 template<typename _CharT, typename _Traits, typename _Alloc>
3078 basic_istream<_CharT, _Traits>&
3079 __detail::_Parser<_Duration>::
3080 operator()(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt,
3081 basic_string<_CharT, _Traits, _Alloc>* __abbrev,
3084 using sentry = typename basic_istream<_CharT, _Traits>::sentry;
3085 ios_base::iostate __err = ios_base::goodbit;
3086 if (sentry __cerb(__is, true); __cerb)
3088 locale __loc = __is.getloc();
3089 auto& __tmget = std::use_facet<std::time_get<_CharT>>(__loc);
3090 auto& __tmpunct = std::use_facet<std::__timepunct<_CharT>>(__loc);
3092 // RAII type to save and restore stream state.
3093 struct _Stream_state
3096 _Stream_state(basic_istream<_CharT, _Traits>& __i)
3098 _M_flags(__i.flags(ios_base::skipws | ios_base::dec)),
3104 _M_is.flags(_M_flags);
3108 _Stream_state(_Stream_state&&) = delete;
3110 basic_istream<_CharT, _Traits>& _M_is;
3111 ios_base::fmtflags _M_flags;
3115 auto __is_failed = [](ios_base::iostate __e) {
3116 return static_cast<bool>(__e & ios_base::failbit);
3119 // Read an unsigned integer from the stream and return it.
3120 // Extract no more than __n digits. Set __err on error.
3121 auto __read_unsigned = [&] (int __n) {
3122 return _S_read_unsigned(__is, __err, __n);
3125 // Read a signed integer from the stream and return it.
3126 // Extract no more than __n digits. Set __err on error.
3127 auto __read_signed = [&] (int __n) {
3128 return _S_read_signed(__is, __err, __n);
3131 // Read an expected character from the stream.
3132 auto __read_chr = [&__is, &__err] (_CharT __c) {
3133 return _S_read_chr(__is, __err, __c);
3136 using __format::_ChronoParts;
3137 _ChronoParts __parts{};
3139 const year __bad_y = --year::min(); // SHRT_MIN
3140 const month __bad_mon(255);
3141 const day __bad_day(255);
3142 const weekday __bad_wday(255);
3143 const hours __bad_h(-1);
3144 const minutes __bad_min(-9999);
3145 const seconds __bad_sec(-1);
3147 year __y = __bad_y, __yy = __bad_y; // %Y, %yy
3148 year __iso_y = __bad_y, __iso_yy = __bad_y; // %G, %g
3149 month __m = __bad_mon; // %m
3150 day __d = __bad_day; // %d
3151 weekday __wday = __bad_wday; // %a %A %u %w
3152 hours __h = __bad_h, __h12 = __bad_h; // %H, %I
3153 minutes __min = __bad_min; // %M
3154 _Duration __s = __bad_sec; // %S
3155 int __ampm = 0; // %p
3156 int __iso_wk = -1, __sunday_wk = -1, __monday_wk = -1; // %V, %U, %W
3157 int __century = -1; // %C
3158 int __dayofyear = -1; // %j (for non-duration)
3160 minutes __tz_offset = __bad_min;
3161 basic_string<_CharT, _Traits> __tz_abbr;
3163 if ((_M_need & _ChronoParts::_TimeOfDay)
3164 && (_M_need & _ChronoParts::_Year))
3166 // For time_points assume "00:00:00
" is implicitly present,
3167 // so we don't fail to parse if it's not (PR libstdc++/114240).
3168 // We will still fail to parse if there's no year+month+day.
3170 __parts = _ChronoParts::_TimeOfDay;
3173 // bool __is_neg = false; // TODO: how is this handled for parsing?
3175 _CharT __mod{}; // One of 'E' or 'O' or nul.
3176 unsigned __num = 0; // Non-zero for N modifier.
3177 bool __is_flag = false; // True if we're processing a % flag.
3179 constexpr bool __is_floating
3180 = treat_as_floating_point_v<typename _Duration::rep>;
3182 // If an out-of-range value is extracted (e.g. 61min for %M),
3183 // do not set failbit immediately because we might not need it
3184 // (e.g. parsing chrono::year doesn't care about invalid %M values).
3185 // Instead set the variable back to its initial 'bad' state,
3186 // and also set related variables corresponding to the same field
3187 // (e.g. a bad %M value for __min should also reset __h and __s).
3188 // If a valid value is needed later the bad value will cause failure.
3190 // For some fields we don't know the correct range when parsing and
3191 // we have to be liberal in what we accept, e.g. we allow 366 for
3192 // day-of-year because that's valid in leap years, and we allow 31
3193 // for day-of-month. If those values are needed to determine the
3194 // result then we can do a correct range check at the end when we
3195 // know the how many days the relevant year or month actually has.
3199 _CharT __c = *__fmt++;
3203 __is_flag = true; // This is the start of a flag.
3204 else if (std::isspace(__c, __loc))
3205 std::ws(__is); // Match zero or more whitespace characters.
3206 else if (!__read_chr(__c)) [[unlikely]]
3207 break; // Failed to match the expected character.
3209 continue; // Process next character in the format string.
3212 // Now processing a flag.
3215 case 'a': // Locale's weekday name
3216 case 'A': // (full or abbreviated, matched case-insensitively).
3217 if (__mod || __num) [[unlikely]]
3218 __err = ios_base::failbit;
3222 __tmget.get(__is, {}, __is, __err, &__tm,
3224 if (!__is_failed(__err))
3225 __wday = weekday(__tm.tm_wday);
3227 __parts |= _ChronoParts::_Weekday;
3230 case 'b': // Locale's month name
3231 case 'h': // (full or abbreviated, matched case-insensitively).
3233 if (__mod || __num) [[unlikely]]
3234 __err = ios_base::failbit;
3237 // strptime behaves differently for %b and %B,
3238 // but chrono::parse says they're equivalent.
3239 // Luckily libstdc++ std::time_get works as needed.
3241 __tmget.get(__is, {}, __is, __err, &__tm,
3243 if (!__is_failed(__err))
3244 __m = month(__tm.tm_mon + 1);
3246 __parts |= _ChronoParts::_Month;
3249 case 'c': // Locale's date and time representation.
3250 if (__mod == 'O' || __num) [[unlikely]]
3251 __err |= ios_base::failbit;
3255 __tmget.get(__is, {}, __is, __err, &__tm,
3256 __fmt - 2 - (__mod == 'E'), __fmt);
3257 if (!__is_failed(__err))
3259 __y = year(__tm.tm_year + 1900);
3260 __m = month(__tm.tm_mon + 1);
3261 __d = day(__tm.tm_mday);
3262 __h = hours(__tm.tm_hour);
3263 __min = minutes(__tm.tm_min);
3264 __s = seconds(__tm.tm_sec);
3267 __parts |= _ChronoParts::_DateTime;
3270 case 'C': // Century
3271 if (!__mod) [[likely]]
3273 auto __v = __read_signed(__num ? __num : 2);
3274 if (!__is_failed(__err))
3276 int __cmin = (int)year::min() / 100;
3277 int __cmax = (int)year::max() / 100;
3278 if (__cmin <= __v && __v <= __cmax)
3279 __century = __v * 100;
3281 __century = -2; // This prevents guessing century.
3284 else if (__mod == 'E')
3287 __tmget.get(__is, {}, __is, __err, &__tm,
3289 if (!__is_failed(__err))
3290 __century = __tm.tm_year;
3293 __err |= ios_base::failbit;
3294 // N.B. don't set this here: __parts |= _ChronoParts::_Year;
3297 case 'd': // Day of month (1-31)
3299 if (!__mod) [[likely]]
3301 auto __v = __read_unsigned(__num ? __num : 2);
3302 if (!__is_failed(__err))
3305 else if (__mod == 'O')
3308 __tmget.get(__is, {}, __is, __err, &__tm,
3310 if (!__is_failed(__err))
3311 __d = day(__tm.tm_mday);
3314 __err |= ios_base::failbit;
3315 __parts |= _ChronoParts::_Day;
3318 case 'D': // %m/%d/%y
3319 if (__mod || __num) [[unlikely]]
3320 __err |= ios_base::failbit;
3323 auto __month = __read_unsigned(2); // %m
3325 auto __day = __read_unsigned(2); // %d
3327 auto __year = __read_unsigned(2); // %y
3328 if (__is_failed(__err))
3330 __y = year(__year + 1900 + 100 * int(__year < 69));
3331 __m = month(__month);
3333 if (!year_month_day(__y, __m, __d).ok())
3335 __y = __yy = __iso_y = __iso_yy = __bad_y;
3341 __parts |= _ChronoParts::_Date;
3344 case 'F': // %Y-%m-%d - any N modifier only applies to %Y.
3345 if (__mod) [[unlikely]]
3346 __err |= ios_base::failbit;
3349 auto __year = __read_signed(__num ? __num : 4); // %Y
3351 auto __month = __read_unsigned(2); // %m
3353 auto __day = __read_unsigned(2); // %d
3354 if (__is_failed(__err))
3357 __m = month(__month);
3359 if (!year_month_day(__y, __m, __d).ok())
3361 __y = __yy = __iso_y = __iso_yy = __bad_y;
3367 __parts |= _ChronoParts::_Date;
3370 case 'g': // Last two digits of ISO week-based year.
3371 if (__mod) [[unlikely]]
3372 __err |= ios_base::failbit;
3375 auto __val = __read_unsigned(__num ? __num : 2);
3376 if (__val >= 0 && __val <= 99)
3378 __iso_yy = year(__val);
3379 if (__century == -1) // No %C has been parsed yet.
3383 __iso_yy = __iso_y = __y = __yy = __bad_y;
3385 __parts |= _ChronoParts::_Year;
3388 case 'G': // ISO week-based year.
3389 if (__mod) [[unlikely]]
3390 __err |= ios_base::failbit;
3392 __iso_y = year(__read_unsigned(__num ? __num : 4));
3393 __parts |= _ChronoParts::_Year;
3396 case 'H': // 24-hour (00-23)
3397 case 'I': // 12-hour (1-12)
3398 if (__mod == 'E') [[unlikely]]
3399 __err |= ios_base::failbit;
3400 else if (__mod == 'O')
3405 __tmget.get(__is, {}, __is, __err, &__tm,
3407 if (!__is_failed(__err))
3411 __h12 = hours(__tm.tm_hour);
3415 __h = hours(__tm.tm_hour);
3418 // XXX %OI seems to be unimplementable.
3419 __err |= ios_base::failbit;
3424 auto __val = __read_unsigned(__num ? __num : 2);
3425 if (__c == 'I' && __val >= 1 && __val <= 12)
3427 __h12 = hours(__val);
3430 else if (__c == 'H' && __val >= 0 && __val <= 23)
3437 if (_M_need & _ChronoParts::_TimeOfDay)
3438 __err |= ios_base::failbit;
3442 __parts |= _ChronoParts::_TimeOfDay;
3445 case 'j': // For duration, count of days, otherwise day of year
3446 if (__mod) [[unlikely]]
3447 __err |= ios_base::failbit;
3448 else if (_M_need == _ChronoParts::_TimeOfDay) // duration
3450 auto __val = __read_signed(__num ? __num : 3);
3451 if (!__is_failed(__err))
3453 __h = days(__val); // __h will get added to _M_time
3454 __parts |= _ChronoParts::_TimeOfDay;
3459 __dayofyear = __read_unsigned(__num ? __num : 3);
3460 // N.B. do not alter __parts here, done after loop.
3461 // No need for range checking here either.
3465 case 'm': // Month (1-12)
3466 if (__mod == 'E') [[unlikely]]
3467 __err |= ios_base::failbit;
3468 else if (__mod == 'O')
3471 __tmget.get(__is, {}, __is, __err, &__tm,
3473 if (!__is_failed(__err))
3474 __m = month(__tm.tm_mon + 1);
3478 auto __val = __read_unsigned(__num ? __num : 2);
3479 if (__val >= 1 && __val <= 12)
3484 __parts |= _ChronoParts::_Month;
3487 case 'M': // Minutes
3488 if (__mod == 'E') [[unlikely]]
3489 __err |= ios_base::failbit;
3490 else if (__mod == 'O')
3493 __tmget.get(__is, {}, __is, __err, &__tm,
3495 if (!__is_failed(__err))
3496 __min = minutes(__tm.tm_min);
3500 auto __val = __read_unsigned(__num ? __num : 2);
3501 if (0 <= __val && __val < 60)
3502 __min = minutes(__val);
3505 if (_M_need & _ChronoParts::_TimeOfDay)
3506 __err |= ios_base::failbit;
3510 __parts |= _ChronoParts::_TimeOfDay;
3513 case 'p': // Locale's AM/PM designation for 12-hour clock.
3515 __err |= ios_base::failbit;
3518 // Can't use std::time_get here as it can't parse %p
3519 // in isolation without %I. This might be faster anyway.
3520 const _CharT* __ampms[2];
3521 __tmpunct._M_am_pm(__ampms);
3522 int __n = 0, __which = 3;
3523 while (__which != 0)
3525 auto __i = __is.peek();
3526 if (_Traits::eq_int_type(__i, _Traits::eof()))
3528 __err |= ios_base::eofbit | ios_base::failbit;
3531 __i = std::toupper(_Traits::to_char_type(__i), __loc);
3534 if (__i != std::toupper(__ampms[0][__n], __loc))
3536 else if (__ampms[0][__n + 1] == _CharT())
3545 if (__i != std::toupper(__ampms[1][__n], __loc))
3547 else if (__ampms[1][__n + 1] == _CharT())
3558 if (__which == 0 || __which == 3)
3559 __err |= ios_base::failbit;
3565 case 'r': // Locale's 12-hour time.
3567 __err |= ios_base::failbit;
3571 __tmget.get(__is, {}, __is, __err, &__tm,
3573 if (!__is_failed(__err))
3575 __h = hours(__tm.tm_hour);
3576 __min = minutes(__tm.tm_min);
3577 __s = seconds(__tm.tm_sec);
3580 __parts |= _ChronoParts::_TimeOfDay;
3584 case 'T': // %H:%M:%S
3585 if (__mod || __num) [[unlikely]]
3587 __err |= ios_base::failbit;
3592 auto __val = __read_unsigned(2);
3593 if (__val == -1 || __val > 23) [[unlikely]]
3595 if (_M_need & _ChronoParts::_TimeOfDay)
3596 __err |= ios_base::failbit;
3599 if (!__read_chr(':')) [[unlikely]]
3603 __val = __read_unsigned(2);
3604 if (__val == -1 || __val > 60) [[unlikely]]
3606 if (_M_need & _ChronoParts::_TimeOfDay)
3607 __err |= ios_base::failbit;
3610 __min = minutes(__val);
3614 __parts |= _ChronoParts::_TimeOfDay;
3617 else if (!__read_chr(':')) [[unlikely]]
3622 case 'S': // Seconds
3623 if (__mod == 'E') [[unlikely]]
3624 __err |= ios_base::failbit;
3625 else if (__mod == 'O')
3628 __tmget.get(__is, {}, __is, __err, &__tm,
3630 if (!__is_failed(__err))
3631 __s = seconds(__tm.tm_sec);
3633 else if constexpr (_Duration::period::den == 1
3636 auto __val = __read_unsigned(__num ? __num : 2);
3637 if (0 <= __val && __val <= 59) [[likely]]
3638 __s = seconds(__val);
3641 if (_M_need & _ChronoParts::_TimeOfDay)
3642 __err |= ios_base::failbit;
3646 else // Read fractional seconds
3648 basic_stringstream<_CharT> __buf;
3649 auto __digit = _S_try_read_digit(__is, __err);
3652 __buf.put(_CharT('0') + __digit);
3653 __digit = _S_try_read_digit(__is, __err);
3655 __buf.put(_CharT('0') + __digit);
3658 auto __i = __is.peek();
3659 if (_Traits::eq_int_type(__i, _Traits::eof()))
3660 __err |= ios_base::eofbit;
3664 if (__loc != locale::classic())
3666 auto& __np = use_facet<numpunct<_CharT>>(__loc);
3667 __dp = __np.decimal_point();
3669 _CharT __c = _Traits::to_char_type(__i);
3675 = hh_mm_ss<_Duration>::fractional_width;
3678 __digit = _S_try_read_digit(__is, __err);
3680 __buf.put(_CharT('0') + __digit);
3688 if (!__is_failed(__err)) [[likely]]
3690 long double __val{};
3691 #if __cpp_lib_to_chars
3692 string __str = std::move(__buf).str();
3693 auto __first = __str.data();
3694 auto __last = __first + __str.size();
3695 using enum chars_format;
3696 auto [ptr, ec] = std::from_chars(__first, __last,
3698 if ((bool)ec || ptr != __last) [[unlikely]]
3699 __err |= ios_base::failbit;
3705 duration<long double> __fs(__val);
3706 if constexpr (__is_floating)
3709 __s = chrono::round<_Duration>(__fs);
3713 __parts |= _ChronoParts::_TimeOfDay;
3716 case 'u': // ISO weekday (1-7)
3717 case 'w': // Weekday (0-6)
3718 if (__mod == 'E') [[unlikely]]
3719 __err |= ios_base::failbit;
3720 else if (__mod == 'O')
3725 __tmget.get(__is, {}, __is, __err, &__tm,
3727 if (!__is_failed(__err))
3728 __wday = weekday(__tm.tm_wday);
3731 __err |= ios_base::failbit;
3735 const int __lo = __c == 'u' ? 1 : 0;
3736 const int __hi = __lo + 6;
3737 auto __val = __read_unsigned(__num ? __num : 1);
3738 if (__lo <= __val && __val <= __hi)
3739 __wday = weekday(__val);
3742 __wday = __bad_wday;
3746 __parts |= _ChronoParts::_Weekday;
3749 case 'U': // Week number of the year (from first Sunday).
3750 case 'V': // ISO week-based week number.
3751 case 'W': // Week number of the year (from first Monday).
3752 if (__mod == 'E') [[unlikely]]
3753 __err |= ios_base::failbit;
3754 else if (__mod == 'O')
3756 if (__c == 'V') [[unlikely]]
3757 __err |= ios_base::failbit;
3760 // TODO nl_langinfo_l(ALT_DIGITS) ?
3761 // Not implementable using std::time_get.
3766 const int __lo = __c == 'V' ? 1 : 0;
3767 const int __hi = 53;
3768 auto __val = __read_unsigned(__num ? __num : 2);
3769 if (__lo <= __val && __val <= __hi)
3774 __sunday_wk = __val;
3780 __monday_wk = __val;
3785 __iso_wk = __sunday_wk = __monday_wk = -1;
3787 // N.B. do not alter __parts here, done after loop.
3790 case 'x': // Locale's date representation.
3791 if (__mod == 'O' || __num) [[unlikely]]
3792 __err |= ios_base::failbit;
3796 __tmget.get(__is, {}, __is, __err, &__tm,
3797 __fmt - 2 - (__mod == 'E'), __fmt);
3798 if (!__is_failed(__err))
3800 __y = year(__tm.tm_year + 1900);
3801 __m = month(__tm.tm_mon + 1);
3802 __d = day(__tm.tm_mday);
3805 __parts |= _ChronoParts::_Date;
3808 case 'X': // Locale's time representation.
3809 if (__mod == 'O' || __num) [[unlikely]]
3810 __err |= ios_base::failbit;
3814 __tmget.get(__is, {}, __is, __err, &__tm,
3815 __fmt - 2 - (__mod == 'E'), __fmt);
3816 if (!__is_failed(__err))
3818 __h = hours(__tm.tm_hour);
3819 __min = minutes(__tm.tm_min);
3820 __s = seconds(__tm.tm_sec);
3823 __parts |= _ChronoParts::_TimeOfDay;
3826 case 'y': // Last two digits of year.
3827 if (__mod) [[unlikely]]
3830 __tmget.get(__is, {}, __is, __err, &__tm,
3832 if (!__is_failed(__err))
3834 int __cent = __tm.tm_year < 2000 ? 1900 : 2000;
3835 __yy = year(__tm.tm_year - __cent);
3836 if (__century == -1) // No %C has been parsed yet.
3842 auto __val = __read_unsigned(__num ? __num : 2);
3843 if (__val >= 0 && __val <= 99)
3846 if (__century == -1) // No %C has been parsed yet.
3847 __century = __val < 69 ? 2000 : 1900;
3850 __y = __yy = __iso_yy = __iso_y = __bad_y;
3852 __parts |= _ChronoParts::_Year;
3856 if (__mod == 'O') [[unlikely]]
3857 __err |= ios_base::failbit;
3858 else if (__mod == 'E')
3861 __tmget.get(__is, {}, __is, __err, &__tm,
3863 if (!__is_failed(__err))
3864 __y = year(__tm.tm_year);
3868 auto __val = __read_unsigned(__num ? __num : 4);
3869 if (!__is_failed(__err))
3872 __parts |= _ChronoParts::_Year;
3876 if (__num) [[unlikely]]
3877 __err |= ios_base::failbit;
3880 // For %Ez and %Oz read [+|-][h]h[:mm].
3881 // For %z read [+|-]hh[mm].
3883 auto __i = __is.peek();
3884 if (_Traits::eq_int_type(__i, _Traits::eof()))
3886 __err |= ios_base::eofbit | ios_base::failbit;
3889 _CharT __ic = _Traits::to_char_type(__i);
3890 const bool __neg = __ic == _CharT('-');
3891 if (__ic == _CharT('-') || __ic == _CharT('+'))
3898 __hh = __read_unsigned(2);
3903 __hh = 10 * _S_try_read_digit(__is, __err);
3904 __hh += _S_try_read_digit(__is, __err);
3907 if (__is_failed(__err))
3911 if (_Traits::eq_int_type(__i, _Traits::eof()))
3913 __err |= ios_base::eofbit;
3914 __tz_offset = minutes(__hh * (__neg ? -60 : 60));
3917 __ic = _Traits::to_char_type(__i);
3919 bool __read_mm = false;
3922 if (__ic == _GLIBCXX_WIDEN(":
")[0])
3929 else if (_CharT('0') <= __ic && __ic <= _CharT('9'))
3935 int_least32_t __mm = 0;
3938 __mm = 10 * _S_try_read_digit(__is, __err);
3939 __mm += _S_try_read_digit(__is, __err);
3942 if (!__is_failed(__err))
3944 auto __z = __hh * 60 + __mm;
3945 __tz_offset = minutes(__neg ? -__z : __z);
3951 if (__mod || __num) [[unlikely]]
3952 __err |= ios_base::failbit;
3955 basic_string_view<_CharT> __x = _GLIBCXX_WIDEN("_/-+
");
3959 auto __i = __is.peek();
3960 if (!_Traits::eq_int_type(__i, _Traits::eof()))
3962 _CharT __a = _Traits::to_char_type(__i);
3963 if (std::isalnum(__a, __loc)
3964 || __x.find(__a) != __x.npos)
3966 __tz_abbr.push_back(__a);
3972 __err |= ios_base::eofbit;
3975 if (__tz_abbr.empty())
3976 __err |= ios_base::failbit;
3980 case 'n': // Exactly one whitespace character.
3981 if (__mod || __num) [[unlikely]]
3982 __err |= ios_base::failbit;
3985 _CharT __i = __is.peek();
3986 if (_Traits::eq_int_type(__i, _Traits::eof()))
3987 __err |= ios_base::eofbit | ios_base::failbit;
3988 else if (std::isspace(_Traits::to_char_type(__i), __loc))
3991 __err |= ios_base::failbit;
3995 case 't': // Zero or one whitespace characters.
3996 if (__mod || __num) [[unlikely]]
3997 __err |= ios_base::failbit;
4000 _CharT __i = __is.peek();
4001 if (_Traits::eq_int_type(__i, _Traits::eof()))
4002 __err |= ios_base::eofbit;
4003 else if (std::isspace(_Traits::to_char_type(__i), __loc))
4008 case '%': // A % character.
4009 if (__mod || __num) [[unlikely]]
4010 __err |= ios_base::failbit;
4015 case 'O': // Modifiers
4017 if (__mod || __num) [[unlikely]]
4019 __err |= ios_base::failbit;
4026 if (_CharT('1') <= __c && __c <= _CharT('9'))
4028 if (!__mod) [[likely]]
4030 // %Nx - extract positive decimal integer N
4031 auto __end = __fmt + _Traits::length(__fmt);
4033 = __format::__parse_integer(__fmt - 1, __end);
4034 if (__ptr) [[likely]]
4042 __err |= ios_base::failbit;
4045 if (__is_failed(__err)) [[unlikely]]
4055 if (__yy != __bad_y && __y == __bad_y)
4056 __y = years(__century) + __yy; // Use %y instead of %Y
4057 if (__iso_yy != __bad_y && __iso_y == __bad_y)
4058 __iso_y = years(__century) + __iso_yy; // Use %g instead of %G
4061 bool __can_use_doy = false;
4062 bool __can_use_iso_wk = false;
4063 bool __can_use_sun_wk = false;
4064 bool __can_use_mon_wk = false;
4066 // A year + day-of-year can be converted to a full date.
4067 if (__y != __bad_y && __dayofyear >= 0)
4069 __can_use_doy = true;
4070 __parts |= _ChronoParts::_Date;
4072 else if (__y != __bad_y && __wday != __bad_wday && __sunday_wk >= 0)
4074 __can_use_sun_wk = true;
4075 __parts |= _ChronoParts::_Date;
4077 else if (__y != __bad_y && __wday != __bad_wday && __monday_wk >= 0)
4079 __can_use_mon_wk = true;
4080 __parts |= _ChronoParts::_Date;
4082 else if (__iso_y != __bad_y && __wday != __bad_wday && __iso_wk > 0)
4084 // An ISO week date can be converted to a full date.
4085 __can_use_iso_wk = true;
4086 __parts |= _ChronoParts::_Date;
4089 if (__is_failed(__err)) [[unlikely]]
4090 ; // Don't bother doing any more work.
4091 else if (__is_flag) [[unlikely]] // incomplete format flag
4092 __err |= ios_base::failbit;
4093 else if ((_M_need & __parts) == _M_need) [[likely]]
4095 // We try to avoid calculating _M_sys_days and _M_ymd unless
4096 // necessary, because converting sys_days to year_month_day
4097 // (or vice versa) requires non-trivial calculations.
4098 // If we have y/m/d values then use them to populate _M_ymd
4099 // and only convert it to _M_sys_days if the caller needs that.
4100 // But if we don't have y/m/d and need to calculate the date
4101 // from the day-of-year or a week+weekday then we set _M_sys_days
4102 // and only convert it to _M_ymd if the caller needs that.
4104 // We do more error checking here, but only for the fields that
4105 // we actually need to use. For example, we will not diagnose
4106 // an invalid dayofyear==366 for non-leap years unless actually
4107 // using __dayofyear. This should mean we never produce invalid
4108 // results, but it means not all invalid inputs are diagnosed,
4109 // e.g. "2023-01-01 366
" >> "%F %j
" ignores the invalid 366.
4110 // We also do not diagnose inconsistent values for the same
4111 // field, e.g. "2021 2022 2023
" >> "%C%y %Y %Y
" just uses 2023.
4113 // Whether the caller wants _M_wd.
4114 // The _Weekday bit is only set for chrono::weekday.
4115 const bool __need_wday = _M_need & _ChronoParts::_Weekday;
4117 // Whether the caller wants _M_sys_days and _M_time.
4118 // Only true for durations and time_points.
4119 const bool __need_time = _M_need & _ChronoParts::_TimeOfDay;
4121 if (__need_wday && __wday != __bad_wday)
4122 _M_wd = __wday; // Caller only wants a weekday and we have one.
4123 else if (_M_need & _ChronoParts::_Date) // subsumes __need_wday
4125 // Whether the caller wants _M_ymd.
4126 // True for chrono::year etc., false for time_points.
4127 const bool __need_ymd = !__need_wday && !__need_time;
4129 if ((_M_need & _ChronoParts::_Year && __y == __bad_y)
4130 || (_M_need & _ChronoParts::_Month && __m == __bad_mon)
4131 || (_M_need & _ChronoParts::_Day && __d == __bad_day))
4133 // Missing at least one of y/m/d so calculate sys_days
4134 // from the other data we have available.
4138 if ((0 < __dayofyear && __dayofyear <= 365)
4139 || (__dayofyear == 366 && __y.is_leap()))
4142 _M_sys_days = sys_days(__y/January/1)
4143 + days(__dayofyear - 1);
4145 _M_ymd = year_month_day(_M_sys_days);
4148 __err |= ios_base::failbit;
4150 else if (__can_use_iso_wk)
4152 // Calculate y/m/d from ISO week date.
4156 // A year has 53 weeks iff Jan 1st is a Thursday
4157 // or Jan 1 is a Wednesday and it's a leap year.
4158 const sys_days __jan4(__iso_y/January/4);
4159 weekday __wd1(__jan4 - days(3));
4160 if (__wd1 != Thursday)
4161 if (__wd1 != Wednesday || !__iso_y.is_leap())
4162 __err |= ios_base::failbit;
4165 if (!__is_failed(__err)) [[likely]]
4167 // First Thursday is always in week one:
4168 sys_days __w(Thursday[1]/January/__iso_y);
4169 // First day of week-based year:
4170 __w -= Thursday - Monday;
4171 __w += days(weeks(__iso_wk - 1));
4172 __w += __wday - Monday;
4176 _M_ymd = year_month_day(_M_sys_days);
4179 else if (__can_use_sun_wk)
4181 // Calculate y/m/d from week number + weekday.
4182 sys_days __wk1(__y/January/Sunday[1]);
4183 _M_sys_days = __wk1 + weeks(__sunday_wk - 1)
4184 + days(__wday.c_encoding());
4185 _M_ymd = year_month_day(_M_sys_days);
4186 if (_M_ymd.year() != __y) [[unlikely]]
4187 __err |= ios_base::failbit;
4189 else if (__can_use_mon_wk)
4191 // Calculate y/m/d from week number + weekday.
4192 sys_days __wk1(__y/January/Monday[1]);
4193 _M_sys_days = __wk1 + weeks(__monday_wk - 1)
4194 + days(__wday.c_encoding() - 1);
4195 _M_ymd = year_month_day(_M_sys_days);
4196 if (_M_ymd.year() != __y) [[unlikely]]
4197 __err |= ios_base::failbit;
4199 else // Should not be able to get here.
4200 __err |= ios_base::failbit;
4204 // We know that all fields the caller needs are present,
4205 // but check that their values are in range.
4206 // Make unwanted fields valid so that _M_ymd.ok() is true.
4208 if (_M_need & _ChronoParts::_Year)
4210 if (!__y.ok()) [[unlikely]]
4211 __err |= ios_base::failbit;
4213 else if (__y == __bad_y)
4214 __y = 1972y; // Leap year so that Feb 29 is valid.
4216 if (_M_need & _ChronoParts::_Month)
4218 if (!__m.ok()) [[unlikely]]
4219 __err |= ios_base::failbit;
4221 else if (__m == __bad_mon)
4224 if (_M_need & _ChronoParts::_Day)
4226 if (__d < day(1) || __d > (__y/__m/last).day())
4227 __err |= ios_base::failbit;
4229 else if (__d == __bad_day)
4232 if (year_month_day __ymd(__y, __m, __d); __ymd.ok())
4235 if (__need_wday || __need_time)
4236 _M_sys_days = sys_days(_M_ymd);
4239 __err |= ios_base::failbit;
4243 _M_wd = weekday(_M_sys_days);
4246 // Need to set _M_time for both durations and time_points.
4249 if (__h == __bad_h && __h12 != __bad_h)
4252 __h = __h12 == hours(12) ? hours(0) : __h12;
4253 else if (__ampm == 2)
4254 __h = __h12 == hours(12) ? __h12 : __h12 + hours(12);
4256 __err |= ios_base::failbit;
4259 auto __t = _M_time.zero();
4268 if (__min != __bad_min)
4274 if (__s != __bad_sec)
4278 _M_is_leap_second = __s >= seconds(60);
4284 __err |= ios_base::failbit;
4287 if (!__is_failed(__err)) [[likely]]
4289 if (__offset && __tz_offset != __bad_min)
4290 *__offset = __tz_offset;
4291 if (__abbrev && !__tz_abbr.empty())
4292 *__abbrev = std::move(__tz_abbr);
4296 __err |= ios_base::failbit;
4299 __is.setstate(__err);
4303 #undef _GLIBCXX_WIDEN
4306 } // namespace chrono
4308 _GLIBCXX_END_NAMESPACE_VERSION
4313 #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.