libstdc++: Fix rounding in chrono::parse
[official-gcc.git] / libstdc++-v3 / include / bits / chrono_io.h
blob362bb5aa9e98df8d5fd217daff589cb299eaa1a3
1 // <chrono> Formatting -*- C++ -*-
3 // Copyright The GNU Toolchain Authors.
4 //
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
9 // any later version.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
25 /** @file include/bits/chrono_io.h
26 * This is an internal header file, included by other library headers.
27 * Do not attempt to use it directly. @headername{chrono}
30 #ifndef _GLIBCXX_CHRONO_IO_H
31 #define _GLIBCXX_CHRONO_IO_H 1
33 #ifdef _GLIBCXX_SYSHDR
34 #pragma GCC system_header
35 #endif
37 #if __cplusplus >= 202002L
39 #include <sstream> // ostringstream
40 #include <iomanip> // setw, setfill
41 #include <format>
42 #include <charconv> // from_chars
43 #include <stdexcept> // __sso_string
45 #include <bits/streambuf_iterator.h>
46 #include <bits/unique_ptr.h>
48 namespace std _GLIBCXX_VISIBILITY(default)
50 _GLIBCXX_BEGIN_NAMESPACE_VERSION
52 namespace chrono
54 /// @addtogroup chrono
55 /// @{
57 /// @cond undocumented
58 namespace __detail
60 // STATICALLY-WIDEN, see C++20 [time.general]
61 // It doesn't matter for format strings (which can only be char or wchar_t)
62 // but this returns the narrow string for anything that isn't wchar_t. This
63 // is done because const char* can be inserted into any ostream type, and
64 // will be widened at runtime if necessary.
65 template<typename _CharT>
66 consteval auto
67 _Widen(const char* __narrow, const wchar_t* __wide)
69 if constexpr (is_same_v<_CharT, wchar_t>)
70 return __wide;
71 else
72 return __narrow;
74 #define _GLIBCXX_WIDEN_(C, S) ::std::chrono::__detail::_Widen<C>(S, L##S)
75 #define _GLIBCXX_WIDEN(S) _GLIBCXX_WIDEN_(_CharT, S)
77 template<typename _Period, typename _CharT>
78 constexpr basic_string_view<_CharT>
79 __units_suffix() noexcept
81 // The standard say these are all narrow strings, which would need to
82 // be widened at run-time when inserted into a wide stream. We use
83 // STATICALLY-WIDEN to widen at compile-time.
84 #define _GLIBCXX_UNITS_SUFFIX(period, suffix) \
85 if constexpr (is_same_v<_Period, period>) \
86 return _GLIBCXX_WIDEN(suffix); \
87 else
89 _GLIBCXX_UNITS_SUFFIX(atto, "as")
90 _GLIBCXX_UNITS_SUFFIX(femto, "fs")
91 _GLIBCXX_UNITS_SUFFIX(pico, "ps")
92 _GLIBCXX_UNITS_SUFFIX(nano, "ns")
93 _GLIBCXX_UNITS_SUFFIX(milli, "ms")
94 #if _GLIBCXX_USE_ALT_MICROSECONDS_SUFFIX
95 // Deciding this at compile-time is wrong, maybe use nl_langinfo(CODESET)
96 // to check runtime environment and return u8"\u00b5s", "\xb5s", or "us".
97 _GLIBCXX_UNITS_SUFFIX(micro, "\u00b5s")
98 #else
99 _GLIBCXX_UNITS_SUFFIX(micro, "us")
100 #endif
101 _GLIBCXX_UNITS_SUFFIX(centi, "cs")
102 _GLIBCXX_UNITS_SUFFIX(deci, "ds")
103 _GLIBCXX_UNITS_SUFFIX(ratio<1>, "s")
104 _GLIBCXX_UNITS_SUFFIX(deca, "das")
105 _GLIBCXX_UNITS_SUFFIX(hecto, "hs")
106 _GLIBCXX_UNITS_SUFFIX(kilo, "ks")
107 _GLIBCXX_UNITS_SUFFIX(mega, "Ms")
108 _GLIBCXX_UNITS_SUFFIX(giga, "Gs")
109 _GLIBCXX_UNITS_SUFFIX(tera, "Ts")
110 _GLIBCXX_UNITS_SUFFIX(tera, "Ts")
111 _GLIBCXX_UNITS_SUFFIX(peta, "Ps")
112 _GLIBCXX_UNITS_SUFFIX(exa, "Es")
113 _GLIBCXX_UNITS_SUFFIX(ratio<60>, "min")
114 _GLIBCXX_UNITS_SUFFIX(ratio<3600>, "h")
115 _GLIBCXX_UNITS_SUFFIX(ratio<86400>, "d")
116 #undef _GLIBCXX_UNITS_SUFFIX
117 return {};
120 template<typename _Period, typename _CharT, typename _Out>
121 inline _Out
122 __fmt_units_suffix(_Out __out) noexcept
124 if (auto __s = __detail::__units_suffix<_Period, _CharT>(); __s.size())
125 return __format::__write(std::move(__out), __s);
126 else if constexpr (_Period::den == 1)
127 return std::format_to(std::move(__out), _GLIBCXX_WIDEN("[{}]s"),
128 (uintmax_t)_Period::num);
129 else
130 return std::format_to(std::move(__out), _GLIBCXX_WIDEN("[{}/{}]s"),
131 (uintmax_t)_Period::num,
132 (uintmax_t)_Period::den);
134 } // namespace __detail
135 /// @endcond
137 /** Write a `chrono::duration` to an ostream.
139 * @since C++20
141 template<typename _CharT, typename _Traits,
142 typename _Rep, typename _Period>
143 inline basic_ostream<_CharT, _Traits>&
144 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
145 const duration<_Rep, _Period>& __d)
147 using _Out = ostreambuf_iterator<_CharT, _Traits>;
148 using period = typename _Period::type;
149 std::basic_ostringstream<_CharT, _Traits> __s;
150 __s.flags(__os.flags());
151 __s.imbue(__os.getloc());
152 __s.precision(__os.precision());
153 __s << __d.count();
154 __detail::__fmt_units_suffix<period, _CharT>(_Out(__s));
155 __os << std::move(__s).str();
156 return __os;
159 /// @cond undocumented
160 namespace __detail
162 // An unspecified type returned by `chrono::local_time_format`.
163 // This is called `local-time-format-t` in the standard.
164 template<typename _Duration>
165 struct __local_time_fmt
167 local_time<_Duration> _M_time;
168 const string* _M_abbrev;
169 const seconds* _M_offset_sec;
172 // _GLIBCXX_RESOLVE_LIB_DEFECTS
173 // 4124. Cannot format zoned_time with resolution coarser than seconds
174 template<typename _Duration>
175 using __local_time_fmt_for
176 = __local_time_fmt<common_type_t<_Duration, seconds>>;
178 /// @endcond
180 /** Return an object that asssociates timezone info with a local time.
182 * A `chrono::local_time` object has no timezone associated with it. This
183 * function creates an object that allows formatting a `local_time` as
184 * though it refers to a timezone with the given abbreviated name and
185 * offset from UTC.
187 * @since C++20
189 template<typename _Duration>
190 inline __detail::__local_time_fmt<_Duration>
191 local_time_format(local_time<_Duration> __time,
192 const string* __abbrev = nullptr,
193 const seconds* __offset_sec = nullptr)
194 { return {__time, __abbrev, __offset_sec}; }
196 /// @}
197 } // namespace chrono
199 /// @cond undocumented
200 namespace __format
202 [[noreturn,__gnu__::__always_inline__]]
203 inline void
204 __no_timezone_available()
205 { __throw_format_error("format error: no timezone available for %Z or %z"); }
207 [[noreturn,__gnu__::__always_inline__]]
208 inline void
209 __not_valid_for_duration()
210 { __throw_format_error("format error: chrono-format-spec not valid for "
211 "chrono::duration"); }
213 [[noreturn,__gnu__::__always_inline__]]
214 inline void
215 __invalid_chrono_spec()
216 { __throw_format_error("format error: chrono-format-spec not valid for "
217 "argument type"); }
219 template<typename _CharT>
220 struct _ChronoSpec : _Spec<_CharT>
222 basic_string_view<_CharT> _M_chrono_specs;
224 // Use one of the reserved bits in __format::_Spec<C>.
225 // This indicates that a locale-dependent conversion specifier such as
226 // %a is used in the chrono-specs. This is not the same as the
227 // _Spec<C>::_M_localized member which indicates that "L" was present
228 // in the format-spec, e.g. "{:L%a}" is localized and locale-specific,
229 // but "{:L}" is only localized and "{:%a}" is only locale-specific.
230 constexpr bool
231 _M_locale_specific() const noexcept
232 { return this->_M_reserved; }
234 constexpr void
235 _M_locale_specific(bool __b) noexcept
236 { this->_M_reserved = __b; }
239 // Represents the information provided by a chrono type.
240 // e.g. month_weekday has month and weekday but no year or time of day,
241 // hh_mm_ss has time of day but no date, sys_time is time_point+timezone.
242 enum _ChronoParts {
243 _Year = 1, _Month = 2, _Day = 4, _Weekday = 8, _TimeOfDay = 16,
244 _TimeZone = 32,
245 _Date = _Year | _Month | _Day | _Weekday,
246 _DateTime = _Date | _TimeOfDay,
247 _ZonedDateTime = _DateTime | _TimeZone,
248 _Duration = 128 // special case
251 constexpr _ChronoParts
252 operator|(_ChronoParts __x, _ChronoParts __y) noexcept
253 { return static_cast<_ChronoParts>((int)__x | (int)__y); }
255 constexpr _ChronoParts&
256 operator|=(_ChronoParts& __x, _ChronoParts __y) noexcept
257 { return __x = __x | __y; }
259 // TODO rename this to chrono::__formatter? or chrono::__detail::__formatter?
260 template<typename _CharT>
261 struct __formatter_chrono
263 using __string_view = basic_string_view<_CharT>;
264 using __string = basic_string<_CharT>;
266 template<typename _ParseContext>
267 constexpr typename _ParseContext::iterator
268 _M_parse(_ParseContext& __pc, _ChronoParts __parts)
270 auto __first = __pc.begin();
271 auto __last = __pc.end();
273 _ChronoSpec<_CharT> __spec{};
275 auto __finalize = [this, &__spec] {
276 _M_spec = __spec;
279 auto __finished = [&] {
280 if (__first == __last || *__first == '}')
282 __finalize();
283 return true;
285 return false;
288 if (__finished())
289 return __first;
291 __first = __spec._M_parse_fill_and_align(__first, __last);
292 if (__finished())
293 return __first;
295 __first = __spec._M_parse_width(__first, __last, __pc);
296 if (__finished())
297 return __first;
299 if (__parts & _ChronoParts::_Duration)
301 __first = __spec._M_parse_precision(__first, __last, __pc);
302 if (__finished())
303 return __first;
306 __first = __spec._M_parse_locale(__first, __last);
307 if (__finished())
308 return __first;
310 // Everything up to the end of the string or the first '}' is a
311 // chrono-specs string. Check it is valid.
313 __string_view __str(__first, __last - __first);
314 auto __end = __str.find('}');
315 if (__end != __str.npos)
317 __str.remove_suffix(__str.length() - __end);
318 __last = __first + __end;
320 if (__str.find('{') != __str.npos)
321 __throw_format_error("chrono format error: '{' in chrono-specs");
324 // Parse chrono-specs in [first,last), checking each conversion-spec
325 // against __parts (so fail for %Y if no year in parts).
326 // Save range in __spec._M_chrono_specs.
328 const auto __chrono_specs = __first++; // Skip leading '%'
329 if (*__chrono_specs != '%')
330 __throw_format_error("chrono format error: no '%' at start of "
331 "chrono-specs");
333 _CharT __mod{};
334 bool __conv = true;
335 int __needed = 0;
336 bool __locale_specific = false;
338 while (__first != __last)
340 enum _Mods { _Mod_none, _Mod_E, _Mod_O, _Mod_E_O };
341 _Mods __allowed_mods = _Mod_none;
343 _CharT __c = *__first++;
344 switch (__c)
346 case 'a':
347 case 'A':
348 __needed = _Weekday;
349 __locale_specific = true;
350 break;
351 case 'b':
352 case 'h':
353 case 'B':
354 __needed = _Month;
355 __locale_specific = true;
356 break;
357 case 'c':
358 __needed = _DateTime;
359 __allowed_mods = _Mod_E;
360 __locale_specific = true;
361 break;
362 case 'C':
363 __needed = _Year;
364 __allowed_mods = _Mod_E;
365 break;
366 case 'd':
367 case 'e':
368 __needed = _Day;
369 __allowed_mods = _Mod_O;
370 break;
371 case 'D':
372 case 'F':
373 __needed = _Date;
374 break;
375 case 'g':
376 case 'G':
377 __needed = _Date;
378 break;
379 case 'H':
380 case 'I':
381 __needed = _TimeOfDay;
382 __allowed_mods = _Mod_O;
383 break;
384 case 'j':
385 if (!(__parts & _Duration))
386 __needed = _Date;
387 break;
388 case 'm':
389 __needed = _Month;
390 __allowed_mods = _Mod_O;
391 break;
392 case 'M':
393 __needed = _TimeOfDay;
394 __allowed_mods = _Mod_O;
395 break;
396 case 'p':
397 case 'r':
398 __locale_specific = true;
399 [[fallthrough]];
400 case 'R':
401 case 'T':
402 __needed = _TimeOfDay;
403 break;
404 case 'q':
405 case 'Q':
406 __needed = _Duration;
407 break;
408 case 'S':
409 __needed = _TimeOfDay;
410 __allowed_mods = _Mod_O;
411 break;
412 case 'u':
413 case 'w':
414 __needed = _Weekday;
415 __allowed_mods = _Mod_O;
416 break;
417 case 'U':
418 case 'V':
419 case 'W':
420 __needed = _Date;
421 __allowed_mods = _Mod_O;
422 break;
423 case 'x':
424 __needed = _Date;
425 __locale_specific = true;
426 __allowed_mods = _Mod_E;
427 break;
428 case 'X':
429 __needed = _TimeOfDay;
430 __locale_specific = true;
431 __allowed_mods = _Mod_E;
432 break;
433 case 'y':
434 __needed = _Year;
435 __allowed_mods = _Mod_E_O;
436 break;
437 case 'Y':
438 __needed = _Year;
439 __allowed_mods = _Mod_E;
440 break;
441 case 'z':
442 __needed = _TimeZone;
443 __allowed_mods = _Mod_E_O;
444 break;
445 case 'Z':
446 __needed = _TimeZone;
447 break;
448 case 'n':
449 case 't':
450 case '%':
451 break;
452 case 'O':
453 case 'E':
454 if (__mod) [[unlikely]]
456 __allowed_mods = _Mod_none;
457 break;
459 __mod = __c;
460 continue;
461 default:
462 __throw_format_error("chrono format error: invalid "
463 " specifier in chrono-specs");
466 if ((__mod == 'E' && !(__allowed_mods & _Mod_E))
467 || (__mod == 'O' && !(__allowed_mods & _Mod_O)))
468 __throw_format_error("chrono format error: invalid "
469 " modifier in chrono-specs");
470 if (__mod && __c != 'z')
471 __locale_specific = true;
472 __mod = _CharT();
474 if ((__parts & __needed) != __needed)
475 __throw_format_error("chrono format error: format argument "
476 "does not contain the information "
477 "required by the chrono-specs");
479 // Scan for next '%', ignoring literal-chars before it.
480 size_t __pos = __string_view(__first, __last - __first).find('%');
481 if (__pos == 0)
482 ++__first;
483 else
485 if (__pos == __string_view::npos)
487 __first = __last;
488 __conv = false;
490 else
491 __first += __pos + 1;
495 // Check for a '%' conversion-spec without a type.
496 if (__conv || __mod != _CharT())
497 __throw_format_error("chrono format error: unescaped '%' in "
498 "chrono-specs");
500 _M_spec = __spec;
501 _M_spec._M_chrono_specs
502 = __string_view(__chrono_specs, __first - __chrono_specs);
503 _M_spec._M_locale_specific(__locale_specific);
505 return __first;
508 // TODO this function template is instantiated for every different _Tp.
509 // Consider creating a polymorphic interface for calendar types so
510 // that we instantiate fewer different specializations. Similar to
511 // _Sink_iter for std::format. Replace each _S_year, _S_day etc. with
512 // member functions of that type.
513 template<typename _Tp, typename _FormatContext>
514 typename _FormatContext::iterator
515 _M_format(const _Tp& __t, _FormatContext& __fc,
516 bool __is_neg = false) const
518 auto __first = _M_spec._M_chrono_specs.begin();
519 const auto __last = _M_spec._M_chrono_specs.end();
520 if (__first == __last)
521 return _M_format_to_ostream(__t, __fc, __is_neg);
523 #if defined _GLIBCXX_USE_NL_LANGINFO_L && __CHAR_BIT__ == 8
524 // _GLIBCXX_RESOLVE_LIB_DEFECTS
525 // 3565. Handling of encodings in localized formatting
526 // of chrono types is underspecified
527 if constexpr (is_same_v<_CharT, char>)
528 if constexpr (__unicode::__literal_encoding_is_utf8())
529 if (_M_spec._M_localized && _M_spec._M_locale_specific())
531 extern locale __with_encoding_conversion(const locale&);
533 // Allocate and cache the necessary state to convert strings
534 // in the locale's encoding to UTF-8.
535 locale __loc = __fc.locale();
536 if (__loc != locale::classic())
537 __fc._M_loc = __with_encoding_conversion(__loc);
539 #endif
541 _Sink_iter<_CharT> __out;
542 __format::_Str_sink<_CharT> __sink;
543 bool __write_direct = false;
544 if constexpr (is_same_v<typename _FormatContext::iterator,
545 _Sink_iter<_CharT>>)
547 if (_M_spec._M_width_kind == __format::_WP_none)
549 __out = __fc.out();
550 __write_direct = true;
552 else
553 __out = __sink.out();
555 else
556 __out = __sink.out();
558 // formatter<duration> passes the correct value of __is_neg
559 // for durations but for hh_mm_ss we decide it here.
560 if constexpr (__is_specialization_of<_Tp, chrono::hh_mm_ss>)
561 __is_neg = __t.is_negative();
563 auto __print_sign = [&__is_neg, &__out] {
564 if constexpr (chrono::__is_duration_v<_Tp>
565 || __is_specialization_of<_Tp, chrono::hh_mm_ss>)
566 if (__is_neg)
568 *__out++ = _S_plus_minus[1];
569 __is_neg = false;
571 return std::move(__out);
574 // Characters to output for "%n", "%t" and "%%" specifiers.
575 constexpr const _CharT* __literals = _GLIBCXX_WIDEN("\n\t%");
577 ++__first; // Skip leading '%' at start of chrono-specs.
579 _CharT __mod{};
582 _CharT __c = *__first++;
583 switch (__c)
585 case 'a':
586 case 'A':
587 __out = _M_a_A(__t, std::move(__out), __fc, __c == 'A');
588 break;
589 case 'b':
590 case 'h':
591 case 'B':
592 __out = _M_b_B(__t, std::move(__out), __fc, __c == 'B');
593 break;
594 case 'c':
595 __out = _M_c(__t, std::move(__out), __fc, __mod == 'E');
596 break;
597 case 'C':
598 case 'y':
599 case 'Y':
600 __out = _M_C_y_Y(__t, std::move(__out), __fc, __c, __mod);
601 break;
602 case 'd':
603 case 'e':
604 __out = _M_d_e(__t, std::move(__out), __fc, __c, __mod == 'O');
605 break;
606 case 'D':
607 __out = _M_D(__t, std::move(__out), __fc);
608 break;
609 case 'F':
610 __out = _M_F(__t, std::move(__out), __fc);
611 break;
612 case 'g':
613 case 'G':
614 __out = _M_g_G(__t, std::move(__out), __fc, __c == 'G');
615 break;
616 case 'H':
617 case 'I':
618 __out = _M_H_I(__t, __print_sign(), __fc, __c, __mod == 'O');
619 break;
620 case 'j':
621 __out = _M_j(__t, __print_sign(), __fc);
622 break;
623 case 'm':
624 __out = _M_m(__t, std::move(__out), __fc, __mod == 'O');
625 break;
626 case 'M':
627 __out = _M_M(__t, __print_sign(), __fc, __mod == 'O');
628 break;
629 case 'p':
630 __out = _M_p(__t, std::move(__out), __fc);
631 break;
632 case 'q':
633 __out = _M_q(__t, std::move(__out), __fc);
634 break;
635 case 'Q':
636 // %Q The duration's numeric value.
637 if constexpr (chrono::__is_duration_v<_Tp>)
638 __out = std::format_to(__print_sign(), _S_empty_spec,
639 __t.count());
640 else
641 __throw_format_error("chrono format error: argument is "
642 "not a duration");
643 break;
644 case 'r':
645 __out = _M_r(__t, __print_sign(), __fc);
646 break;
647 case 'R':
648 case 'T':
649 __out = _M_R_T(__t, __print_sign(), __fc, __c == 'T');
650 break;
651 case 'S':
652 __out = _M_S(__t, __print_sign(), __fc, __mod == 'O');
653 break;
654 case 'u':
655 case 'w':
656 __out = _M_u_w(__t, std::move(__out), __fc, __c, __mod == 'O');
657 break;
658 case 'U':
659 case 'V':
660 case 'W':
661 __out = _M_U_V_W(__t, std::move(__out), __fc, __c,
662 __mod == 'O');
663 break;
664 case 'x':
665 __out = _M_x(__t, std::move(__out), __fc, __mod == 'E');
666 break;
667 case 'X':
668 __out = _M_X(__t, __print_sign(), __fc, __mod == 'E');
669 break;
670 case 'z':
671 __out = _M_z(__t, std::move(__out), __fc, (bool)__mod);
672 break;
673 case 'Z':
674 __out = _M_Z(__t, std::move(__out), __fc);
675 break;
676 case 'n':
677 *__out++ = __literals[0];
678 break;
679 case 't':
680 *__out++ = __literals[1];
681 break;
682 case '%':
683 *__out++ = __literals[2];
684 break;
685 case 'O':
686 case 'E':
687 __mod = __c;
688 continue;
689 case '}':
690 __first = __last;
691 break;
693 __mod = _CharT();
694 // Scan for next '%' and write out everything before it.
695 __string_view __str(__first, __last - __first);
696 size_t __pos = __str.find('%');
697 if (__pos == 0)
698 ++__first;
699 else
701 if (__pos == __str.npos)
702 __first = __last;
703 else
705 __str.remove_suffix(__str.length() - __pos);
706 __first += __pos + 1;
708 __out = __format::__write(std::move(__out), __str);
711 while (__first != __last);
713 if constexpr (is_same_v<typename _FormatContext::iterator,
714 _Sink_iter<_CharT>>)
715 if (__write_direct)
716 return __out;
718 auto __str = std::move(__sink).get();
719 return __format::__write_padded_as_spec(__str, __str.size(),
720 __fc, _M_spec);
723 _ChronoSpec<_CharT> _M_spec;
725 private:
726 // Return the formatting locale.
727 template<typename _FormatContext>
728 std::locale
729 _M_locale(_FormatContext& __fc) const
731 if (!_M_spec._M_localized)
732 return std::locale::classic();
733 else
734 return __fc.locale();
737 // Format for empty chrono-specs, e.g. "{}" (C++20 [time.format] p6).
738 // TODO: consider moving body of every operator<< into this function
739 // and use std::format("{}", t) to implement those operators. That
740 // would avoid std::format("{}", t) calling operator<< which calls
741 // std::format again.
742 template<typename _Tp, typename _FormatContext>
743 typename _FormatContext::iterator
744 _M_format_to_ostream(const _Tp& __t, _FormatContext& __fc,
745 bool __is_neg) const
747 using ::std::chrono::__detail::__utc_leap_second;
748 using ::std::chrono::__detail::__local_time_fmt;
750 basic_ostringstream<_CharT> __os;
751 __os.imbue(_M_locale(__fc));
753 if constexpr (__is_specialization_of<_Tp, __local_time_fmt>)
755 // Format as "{:L%F %T}"
756 auto __days = chrono::floor<chrono::days>(__t._M_time);
757 __os << chrono::year_month_day(__days) << ' '
758 << chrono::hh_mm_ss(__t._M_time - __days);
760 // For __local_time_fmt the __is_neg flags says whether to
761 // append " %Z" to the result.
762 if (__is_neg)
764 if (!__t._M_abbrev) [[unlikely]]
765 __format::__no_timezone_available();
766 else if constexpr (is_same_v<_CharT, char>)
767 __os << ' ' << *__t._M_abbrev;
768 else
770 __os << L' ';
771 for (char __c : *__t._M_abbrev)
772 __os << __c;
776 else
778 if constexpr (__is_specialization_of<_Tp, __utc_leap_second>)
779 __os << __t._M_date << ' ' << __t._M_time;
780 else if constexpr (chrono::__is_time_point_v<_Tp>)
782 // Need to be careful here because not all specializations
783 // of chrono::sys_time can be written to an ostream.
784 // For the specializations of time_point that can be
785 // formatted with an empty chrono-specs, either it's a
786 // sys_time with period greater or equal to days:
787 if constexpr (is_convertible_v<_Tp, chrono::sys_days>)
788 __os << _S_date(__t);
789 else // Or it's formatted as "{:L%F %T}":
791 auto __days = chrono::floor<chrono::days>(__t);
792 __os << chrono::year_month_day(__days) << ' '
793 << chrono::hh_mm_ss(__t - __days);
796 else
798 if constexpr (chrono::__is_duration_v<_Tp>)
799 if (__is_neg) [[unlikely]]
800 __os << _S_plus_minus[1];
801 __os << __t;
805 auto __str = std::move(__os).str();
806 return __format::__write_padded_as_spec(__str, __str.size(),
807 __fc, _M_spec);
810 static constexpr const _CharT* _S_chars
811 = _GLIBCXX_WIDEN("0123456789+-:/ {}");
812 static constexpr const _CharT* _S_plus_minus = _S_chars + 10;
813 static constexpr _CharT _S_colon = _S_chars[12];
814 static constexpr _CharT _S_slash = _S_chars[13];
815 static constexpr _CharT _S_space = _S_chars[14];
816 static constexpr const _CharT* _S_empty_spec = _S_chars + 15;
818 template<typename _OutIter>
819 _OutIter
820 _M_write(_OutIter __out, const locale& __loc, __string_view __s) const
822 #if defined _GLIBCXX_USE_NL_LANGINFO_L && __CHAR_BIT__ == 8
823 __sso_string __buf;
824 // _GLIBCXX_RESOLVE_LIB_DEFECTS
825 // 3565. Handling of encodings in localized formatting
826 // of chrono types is underspecified
827 if constexpr (is_same_v<_CharT, char>)
828 if constexpr (__unicode::__literal_encoding_is_utf8())
829 if (_M_spec._M_localized && _M_spec._M_locale_specific()
830 && __loc != locale::classic())
832 extern string_view
833 __locale_encoding_to_utf8(const locale&, string_view, void*);
835 __s = __locale_encoding_to_utf8(__loc, __s, &__buf);
837 #endif
838 return __format::__write(std::move(__out), __s);
841 template<typename _Tp, typename _FormatContext>
842 typename _FormatContext::iterator
843 _M_a_A(const _Tp& __t, typename _FormatContext::iterator __out,
844 _FormatContext& __ctx, bool __full) const
846 // %a Locale's abbreviated weekday name.
847 // %A Locale's full weekday name.
848 chrono::weekday __wd = _S_weekday(__t);
849 if (!__wd.ok())
850 __throw_format_error("format error: invalid weekday");
852 locale __loc = _M_locale(__ctx);
853 const auto& __tp = use_facet<__timepunct<_CharT>>(__loc);
854 const _CharT* __days[7];
855 if (__full)
856 __tp._M_days(__days);
857 else
858 __tp._M_days_abbreviated(__days);
859 __string_view __str(__days[__wd.c_encoding()]);
860 return _M_write(std::move(__out), __loc, __str);
863 template<typename _Tp, typename _FormatContext>
864 typename _FormatContext::iterator
865 _M_b_B(const _Tp& __t, typename _FormatContext::iterator __out,
866 _FormatContext& __ctx, bool __full) const
868 // %b Locale's abbreviated month name.
869 // %B Locale's full month name.
870 chrono::month __m = _S_month(__t);
871 if (!__m.ok())
872 __throw_format_error("format error: invalid month");
873 locale __loc = _M_locale(__ctx);
874 const auto& __tp = use_facet<__timepunct<_CharT>>(__loc);
875 const _CharT* __months[12];
876 if (__full)
877 __tp._M_months(__months);
878 else
879 __tp._M_months_abbreviated(__months);
880 __string_view __str(__months[(unsigned)__m - 1]);
881 return _M_write(std::move(__out), __loc, __str);
884 template<typename _Tp, typename _FormatContext>
885 typename _FormatContext::iterator
886 _M_c(const _Tp& __tt, typename _FormatContext::iterator __out,
887 _FormatContext& __ctx, bool __mod = false) const
889 // %c Locale's date and time representation.
890 // %Ec Locale's alternate date and time representation.
892 auto __t = _S_floor_seconds(__tt);
893 locale __loc = _M_locale(__ctx);
894 const auto& __tp = use_facet<__timepunct<_CharT>>(__loc);
895 const _CharT* __formats[2];
896 __tp._M_date_time_formats(__formats);
897 const _CharT* __rep = __formats[__mod];
898 if (!*__rep)
899 __rep = _GLIBCXX_WIDEN("%a %b %e %H:%M:%S %Y");
900 basic_string<_CharT> __fmt(_S_empty_spec);
901 __fmt.insert(1u, 1u, _S_colon);
902 __fmt.insert(2u, __rep);
903 return std::vformat_to(std::move(__out), __loc, __fmt,
904 std::make_format_args<_FormatContext>(__t));
907 template<typename _Tp, typename _FormatContext>
908 typename _FormatContext::iterator
909 _M_C_y_Y(const _Tp& __t, typename _FormatContext::iterator __out,
910 _FormatContext& __ctx, _CharT __conv, _CharT __mod = 0) const
912 // %C Year divided by 100 using floored division.
913 // %EC Locale's alternative preresentation of the century (era name).
914 // %y Last two decimal digits of the year.
915 // %Oy Locale's alternative representation.
916 // %Ey Locale's alternative representation of offset from %EC.
917 // %Y Year as a decimal number.
918 // %EY Locale's alternative full year representation.
920 chrono::year __y = _S_year(__t);
922 if (__mod && _M_spec._M_localized) [[unlikely]]
923 if (auto __loc = __ctx.locale(); __loc != locale::classic())
925 struct tm __tm{};
926 __tm.tm_year = (int)__y - 1900;
927 return _M_locale_fmt(std::move(__out), __loc, __tm,
928 __conv, __mod);
931 basic_string<_CharT> __s;
932 int __yi = (int)__y;
933 const bool __is_neg = __yi < 0;
934 __yi = __builtin_abs(__yi);
936 if (__conv == 'Y' || __conv == 'C')
938 int __ci = __yi / 100;
939 if (__is_neg) [[unlikely]]
941 __s.assign(1, _S_plus_minus[1]);
942 // For floored division -123//100 is -2 and -100//100 is -1
943 if (__conv == 'C' && (__ci * 100) != __yi)
944 ++__ci;
946 if (__ci >= 100) [[unlikely]]
948 __s += std::format(_S_empty_spec, __ci / 100);
949 __ci %= 100;
951 __s += _S_two_digits(__ci);
954 if (__conv == 'Y' || __conv == 'y')
955 __s += _S_two_digits(__yi % 100);
957 return __format::__write(std::move(__out), __string_view(__s));
960 template<typename _Tp, typename _FormatContext>
961 typename _FormatContext::iterator
962 _M_D(const _Tp& __t, typename _FormatContext::iterator __out,
963 _FormatContext&) const
965 auto __ymd = _S_date(__t);
966 basic_string<_CharT> __s;
967 #if ! _GLIBCXX_USE_CXX11_ABI
968 __s.reserve(8);
969 #endif
970 __s = _S_two_digits((unsigned)__ymd.month());
971 __s += _S_slash;
972 __s += _S_two_digits((unsigned)__ymd.day());
973 __s += _S_slash;
974 __s += _S_two_digits(__builtin_abs((int)__ymd.year()) % 100);
975 return __format::__write(std::move(__out), __string_view(__s));
978 template<typename _Tp, typename _FormatContext>
979 typename _FormatContext::iterator
980 _M_d_e(const _Tp& __t, typename _FormatContext::iterator __out,
981 _FormatContext& __ctx, _CharT __conv, bool __mod = false) const
983 // %d The day of month as a decimal number.
984 // %Od Locale's alternative representation.
985 // %e Day of month as decimal number, padded with space.
986 // %Oe Locale's alternative digits.
988 chrono::day __d = _S_day(__t);
989 unsigned __i = (unsigned)__d;
991 if (__mod && _M_spec._M_localized) [[unlikely]]
992 if (auto __loc = __ctx.locale(); __loc != locale::classic())
994 struct tm __tm{};
995 __tm.tm_mday = __i;
996 return _M_locale_fmt(std::move(__out), __loc, __tm,
997 (char)__conv, 'O');
1000 auto __sv = _S_two_digits(__i);
1001 _CharT __buf[2];
1002 if (__conv == _CharT('e') && __i < 10)
1004 __buf[0] = _S_space;
1005 __buf[1] = __sv[1];
1006 __sv = {__buf, 2};
1008 return __format::__write(std::move(__out), __sv);
1011 template<typename _Tp, typename _FormatContext>
1012 typename _FormatContext::iterator
1013 _M_F(const _Tp& __t, typename _FormatContext::iterator __out,
1014 _FormatContext&) const
1016 auto __ymd = _S_date(__t);
1017 auto __s = std::format(_GLIBCXX_WIDEN("{:04d}- - "),
1018 (int)__ymd.year());
1019 auto __sv = _S_two_digits((unsigned)__ymd.month());
1020 __s[__s.size() - 5] = __sv[0];
1021 __s[__s.size() - 4] = __sv[1];
1022 __sv = _S_two_digits((unsigned)__ymd.day());
1023 __s[__s.size() - 2] = __sv[0];
1024 __s[__s.size() - 1] = __sv[1];
1025 __sv = __s;
1026 return __format::__write(std::move(__out), __sv);
1029 template<typename _Tp, typename _FormatContext>
1030 typename _FormatContext::iterator
1031 _M_g_G(const _Tp& __t, typename _FormatContext::iterator __out,
1032 _FormatContext& __ctx, bool __full) const
1034 // %g last two decimal digits of the ISO week-based year.
1035 // %G ISO week-based year.
1036 using namespace chrono;
1037 auto __d = _S_days(__t);
1038 // Move to nearest Thursday:
1039 __d -= (weekday(__d) - Monday) - days(3);
1040 // ISO week-based year is the year that contains that Thursday:
1041 year __y = year_month_day(__d).year();
1042 return _M_C_y_Y(__y, std::move(__out), __ctx, "yY"[__full]);
1045 template<typename _Tp, typename _FormatContext>
1046 typename _FormatContext::iterator
1047 _M_H_I(const _Tp& __t, typename _FormatContext::iterator __out,
1048 _FormatContext& __ctx, _CharT __conv, bool __mod = false) const
1050 // %H The hour (24-hour clock) as a decimal number.
1051 // %OH Locale's alternative representation.
1052 // %I The hour (12-hour clock) as a decimal number.
1053 // %OI Locale's alternative representation.
1055 const auto __hms = _S_hms(__t);
1056 int __i = __hms.hours().count();
1058 if (__mod && _M_spec._M_localized) [[unlikely]]
1059 if (auto __loc = __ctx.locale(); __loc != locale::classic())
1061 struct tm __tm{};
1062 __tm.tm_hour = __i;
1063 return _M_locale_fmt(std::move(__out), __loc, __tm,
1064 (char)__conv, 'O');
1067 if (__conv == _CharT('I'))
1069 if (__i == 0)
1070 __i = 12;
1071 else if (__i > 12)
1072 __i -= 12;
1074 return __format::__write(std::move(__out), _S_two_digits(__i));
1077 template<typename _Tp, typename _FormatContext>
1078 typename _FormatContext::iterator
1079 _M_j(const _Tp& __t, typename _FormatContext::iterator __out,
1080 _FormatContext&) const
1082 if constexpr (chrono::__is_duration_v<_Tp>)
1084 // Decimal number of days, without padding.
1085 unsigned __d = chrono::duration_cast<chrono::days>(__t).count();
1086 return std::format_to(std::move(__out), _S_empty_spec, __d);
1088 else
1090 // Day of the year as a decimal number, padding with zero.
1091 using namespace chrono;
1092 auto __day = _S_days(__t);
1093 auto __ymd = _S_date(__t);
1094 days __d;
1095 // See "Calculating Ordinal Dates" at
1096 // https://github.com/HowardHinnant/date/wiki/Examples-and-Recipes
1097 if constexpr (is_same_v<typename decltype(__day)::clock, local_t>)
1098 __d = __day - local_days(__ymd.year()/January/0);
1099 else
1100 __d = __day - sys_days(__ymd.year()/January/0);
1101 return std::format_to(std::move(__out), _GLIBCXX_WIDEN("{:03d}"),
1102 __d.count());
1106 template<typename _Tp, typename _FormatContext>
1107 typename _FormatContext::iterator
1108 _M_m(const _Tp& __t, typename _FormatContext::iterator __out,
1109 _FormatContext& __ctx, bool __mod) const
1111 // %m month as a decimal number.
1112 // %Om Locale's alternative representation.
1114 auto __m = _S_month(__t);
1115 auto __i = (unsigned)__m;
1117 if (__mod && _M_spec._M_localized) [[unlikely]] // %Om
1118 if (auto __loc = __ctx.locale(); __loc != locale::classic())
1120 struct tm __tm{};
1121 __tm.tm_mon = __i - 1;
1122 return _M_locale_fmt(std::move(__out), __loc, __tm,
1123 'm', 'O');
1126 return __format::__write(std::move(__out), _S_two_digits(__i));
1129 template<typename _Tp, typename _FormatContext>
1130 typename _FormatContext::iterator
1131 _M_M(const _Tp& __t, typename _FormatContext::iterator __out,
1132 _FormatContext& __ctx, bool __mod) const
1134 // %M The minute as a decimal number.
1135 // %OM Locale's alternative representation.
1137 auto __m = _S_hms(__t).minutes();
1138 auto __i = __m.count();
1140 if (__mod && _M_spec._M_localized) [[unlikely]] // %OM
1141 if (auto __loc = __ctx.locale(); __loc != locale::classic())
1143 struct tm __tm{};
1144 __tm.tm_min = __i;
1145 return _M_locale_fmt(std::move(__out), __loc, __tm,
1146 'M', 'O');
1149 return __format::__write(std::move(__out), _S_two_digits(__i));
1152 template<typename _Tp, typename _FormatContext>
1153 typename _FormatContext::iterator
1154 _M_p(const _Tp& __t, typename _FormatContext::iterator __out,
1155 _FormatContext& __ctx) const
1157 // %p The locale's equivalent of the AM/PM designations.
1158 auto __hms = _S_hms(__t);
1159 locale __loc = _M_locale(__ctx);
1160 const auto& __tp = use_facet<__timepunct<_CharT>>(__loc);
1161 const _CharT* __ampm[2];
1162 __tp._M_am_pm(__ampm);
1163 return _M_write(std::move(__out), __loc,
1164 __ampm[__hms.hours().count() >= 12]);
1167 template<typename _Tp, typename _FormatContext>
1168 typename _FormatContext::iterator
1169 _M_q(const _Tp&, typename _FormatContext::iterator __out,
1170 _FormatContext&) const
1172 // %q The duration's unit suffix
1173 if constexpr (!chrono::__is_duration_v<_Tp>)
1174 __throw_format_error("format error: argument is not a duration");
1175 else
1177 namespace __d = chrono::__detail;
1178 using period = typename _Tp::period;
1179 return __d::__fmt_units_suffix<period, _CharT>(std::move(__out));
1183 // %Q handled in _M_format
1185 template<typename _Tp, typename _FormatContext>
1186 typename _FormatContext::iterator
1187 _M_r(const _Tp& __tt, typename _FormatContext::iterator __out,
1188 _FormatContext& __ctx) const
1190 // %r locale's 12-hour clock time.
1191 auto __t = _S_floor_seconds(__tt);
1192 locale __loc = _M_locale(__ctx);
1193 const auto& __tp = use_facet<__timepunct<_CharT>>(__loc);
1194 const _CharT* __ampm_fmt;
1195 __tp._M_am_pm_format(&__ampm_fmt);
1196 basic_string<_CharT> __fmt(_S_empty_spec);
1197 __fmt.insert(1u, 1u, _S_colon);
1198 __fmt.insert(2u, __ampm_fmt);
1199 using _FmtStr = _Runtime_format_string<_CharT>;
1200 return _M_write(std::move(__out), __loc,
1201 std::format(__loc, _FmtStr(__fmt), __t));
1204 template<typename _Tp, typename _FormatContext>
1205 typename _FormatContext::iterator
1206 _M_R_T(const _Tp& __t, typename _FormatContext::iterator __out,
1207 _FormatContext& __ctx, bool __secs) const
1209 // %R Equivalent to %H:%M
1210 // %T Equivalent to %H:%M:%S
1211 auto __hms = _S_hms(__t);
1213 auto __s = std::format(_GLIBCXX_WIDEN("{:02d}:00"),
1214 __hms.hours().count());
1215 auto __sv = _S_two_digits(__hms.minutes().count());
1216 __s[__s.size() - 2] = __sv[0];
1217 __s[__s.size() - 1] = __sv[1];
1218 __sv = __s;
1219 __out = __format::__write(std::move(__out), __sv);
1220 if (__secs)
1222 *__out++ = _S_colon;
1223 __out = _M_S(__hms, std::move(__out), __ctx);
1225 return __out;
1228 template<typename _Tp, typename _FormatContext>
1229 typename _FormatContext::iterator
1230 _M_S(const _Tp& __t, typename _FormatContext::iterator __out,
1231 _FormatContext& __ctx, bool __mod = false) const
1233 // %S Seconds as a decimal number.
1234 // %OS The locale's alternative representation.
1235 auto __hms = _S_hms(__t);
1236 auto __s = __hms.seconds();
1238 if (__mod) [[unlikely]] // %OS
1240 if (_M_spec._M_localized)
1241 if (auto __loc = __ctx.locale(); __loc != locale::classic())
1243 struct tm __tm{};
1244 __tm.tm_sec = (int)__s.count();
1245 return _M_locale_fmt(std::move(__out), __loc, __tm,
1246 'S', 'O');
1249 // %OS formats don't include subseconds, so just format that:
1250 return __format::__write(std::move(__out),
1251 _S_two_digits(__s.count()));
1254 if constexpr (__hms.fractional_width == 0)
1255 __out = __format::__write(std::move(__out),
1256 _S_two_digits(__s.count()));
1257 else
1259 locale __loc = _M_locale(__ctx);
1260 auto __ss = __hms.subseconds();
1261 using rep = typename decltype(__ss)::rep;
1262 if constexpr (is_floating_point_v<rep>)
1264 chrono::duration<rep> __fs = __s + __ss;
1265 __out = std::format_to(std::move(__out), __loc,
1266 _GLIBCXX_WIDEN("{:#0{}.{}Lf}"),
1267 __fs.count(),
1268 3 + __hms.fractional_width,
1269 __hms.fractional_width);
1271 else
1273 const auto& __np
1274 = use_facet<numpunct<_CharT>>(__loc);
1275 __out = __format::__write(std::move(__out),
1276 _S_two_digits(__s.count()));
1277 *__out++ = __np.decimal_point();
1278 if constexpr (is_integral_v<rep>)
1279 __out = std::format_to(std::move(__out),
1280 _GLIBCXX_WIDEN("{:0{}}"),
1281 __ss.count(),
1282 __hms.fractional_width);
1283 else
1285 auto __str = std::format(_S_empty_spec, __ss.count());
1286 __out = std::format_to(_GLIBCXX_WIDEN("{:0>{}s}"),
1287 __str,
1288 __hms.fractional_width);
1292 return __out;
1295 // %t handled in _M_format
1297 template<typename _Tp, typename _FormatContext>
1298 typename _FormatContext::iterator
1299 _M_u_w(const _Tp& __t, typename _FormatContext::iterator __out,
1300 _FormatContext& __ctx, _CharT __conv, bool __mod = false) const
1302 // %u ISO weekday as a decimal number (1-7), where Monday is 1.
1303 // %Ou Locale's alternative numeric rep.
1304 // %w Weekday as a decimal number (0-6), where Sunday is 0.
1305 // %Ow Locale's alternative numeric rep.
1307 chrono::weekday __wd = _S_weekday(__t);
1309 if (__mod && _M_spec._M_localized) [[unlikely]]
1310 if (auto __loc = __ctx.locale(); __loc != locale::classic())
1312 struct tm __tm{};
1313 __tm.tm_wday = __wd.c_encoding();
1314 return _M_locale_fmt(std::move(__out), __loc, __tm,
1315 (char)__conv, 'O');
1318 unsigned __wdi = __conv == 'u' ? __wd.iso_encoding()
1319 : __wd.c_encoding();
1320 const _CharT __d = _S_digit(__wdi);
1321 return __format::__write(std::move(__out), __string_view(&__d, 1));
1324 template<typename _Tp, typename _FormatContext>
1325 typename _FormatContext::iterator
1326 _M_U_V_W(const _Tp& __t, typename _FormatContext::iterator __out,
1327 _FormatContext& __ctx, _CharT __conv, bool __mod = false) const
1329 // %U Week number of the year as a decimal number, from first Sunday.
1330 // %OU Locale's alternative numeric rep.
1331 // %V ISO week-based week number as a decimal number.
1332 // %OV Locale's alternative numeric rep.
1333 // %W Week number of the year as a decimal number, from first Monday.
1334 // %OW Locale's alternative numeric rep.
1335 using namespace chrono;
1336 auto __d = _S_days(__t);
1337 using _TDays = decltype(__d); // Either sys_days or local_days.
1339 if (__mod && _M_spec._M_localized) [[unlikely]]
1340 if (auto __loc = __ctx.locale(); __loc != locale::classic())
1342 const year_month_day __ymd(__d);
1343 const year __y = __ymd.year();
1344 struct tm __tm{};
1345 __tm.tm_year = (int)__y - 1900;
1346 __tm.tm_yday = (__d - _TDays(__y/January/1)).count();
1347 __tm.tm_wday = weekday(__d).c_encoding();
1348 return _M_locale_fmt(std::move(__out), __loc, __tm,
1349 (char)__conv, 'O');
1352 _TDays __first; // First day of week 1.
1353 if (__conv == 'V') // W01 begins on Monday before first Thursday.
1355 // Move to nearest Thursday:
1356 __d -= (weekday(__d) - Monday) - days(3);
1357 // ISO week of __t is number of weeks since January 1 of the
1358 // same year as that nearest Thursday.
1359 __first = _TDays(year_month_day(__d).year()/January/1);
1361 else
1363 year __y;
1364 if constexpr (requires { __t.year(); })
1365 __y = __t.year();
1366 else
1367 __y = year_month_day(__d).year();
1368 const weekday __weekstart = __conv == 'U' ? Sunday : Monday;
1369 __first = _TDays(__y/January/__weekstart[1]);
1371 auto __weeks = chrono::floor<weeks>(__d - __first);
1372 __string_view __sv = _S_two_digits(__weeks.count() + 1);
1373 return __format::__write(std::move(__out), __sv);
1376 template<typename _Tp, typename _FormatContext>
1377 typename _FormatContext::iterator
1378 _M_x(const _Tp& __t, typename _FormatContext::iterator __out,
1379 _FormatContext& __ctx, bool __mod = false) const
1381 // %x Locale's date rep
1382 // %Ex Locale's alternative date representation.
1383 locale __loc = _M_locale(__ctx);
1384 const auto& __tp = use_facet<__timepunct<_CharT>>(__loc);
1385 const _CharT* __date_reps[2];
1386 __tp._M_date_formats(__date_reps);
1387 const _CharT* __rep = __date_reps[__mod];
1388 if (!*__rep)
1389 return _M_D(__t, std::move(__out), __ctx);
1391 basic_string<_CharT> __fmt(_S_empty_spec);
1392 __fmt.insert(1u, 1u, _S_colon);
1393 __fmt.insert(2u, __rep);
1394 using _FmtStr = _Runtime_format_string<_CharT>;
1395 return _M_write(std::move(__out), __loc,
1396 std::format(__loc, _FmtStr(__fmt), __t));
1399 template<typename _Tp, typename _FormatContext>
1400 typename _FormatContext::iterator
1401 _M_X(const _Tp& __tt, typename _FormatContext::iterator __out,
1402 _FormatContext& __ctx, bool __mod = false) const
1404 // %X Locale's time rep
1405 // %EX Locale's alternative time representation.
1406 auto __t = _S_floor_seconds(__tt);
1407 locale __loc = _M_locale(__ctx);
1408 const auto& __tp = use_facet<__timepunct<_CharT>>(__loc);
1409 const _CharT* __time_reps[2];
1410 __tp._M_time_formats(__time_reps);
1411 const _CharT* __rep = __time_reps[__mod];
1412 if (!*__rep)
1413 return _M_R_T(__t, std::move(__out), __ctx, true);
1415 basic_string<_CharT> __fmt(_S_empty_spec);
1416 __fmt.insert(1u, 1u, _S_colon);
1417 __fmt.insert(2u, __rep);
1418 using _FmtStr = _Runtime_format_string<_CharT>;
1419 return _M_write(std::move(__out), __loc,
1420 std::format(__loc, _FmtStr(__fmt), __t));
1423 template<typename _Tp, typename _FormatContext>
1424 typename _FormatContext::iterator
1425 _M_z(const _Tp& __t, typename _FormatContext::iterator __out,
1426 _FormatContext&, bool __mod = false) const
1428 using ::std::chrono::__detail::__utc_leap_second;
1429 using ::std::chrono::__detail::__local_time_fmt;
1431 auto __utc = __mod ? __string_view(_GLIBCXX_WIDEN("+00:00"), 6)
1432 : __string_view(_GLIBCXX_WIDEN("+0000"), 5);
1434 if constexpr (chrono::__is_time_point_v<_Tp>)
1436 if constexpr (is_same_v<typename _Tp::clock,
1437 chrono::system_clock>)
1438 return __format::__write(std::move(__out), __utc);
1440 else if constexpr (__is_specialization_of<_Tp, __local_time_fmt>)
1442 if (__t._M_offset_sec)
1444 auto __sv = __utc;
1445 basic_string<_CharT> __s;
1446 if (*__t._M_offset_sec != 0s)
1448 chrono:: hh_mm_ss __hms(*__t._M_offset_sec);
1449 __s = _S_plus_minus[__hms.is_negative()];
1450 __s += _S_two_digits(__hms.hours().count());
1451 if (__mod)
1452 __s += _S_colon;
1453 __s += _S_two_digits(__hms.minutes().count());
1454 __sv = __s;
1456 return __format::__write(std::move(__out), __sv);
1459 else if constexpr (__is_specialization_of<_Tp, __utc_leap_second>)
1460 return __format::__write(std::move(__out), __utc);
1462 __no_timezone_available();
1465 template<typename _Tp, typename _FormatContext>
1466 typename _FormatContext::iterator
1467 _M_Z(const _Tp& __t, typename _FormatContext::iterator __out,
1468 _FormatContext& __ctx) const
1470 using ::std::chrono::__detail::__utc_leap_second;
1471 using ::std::chrono::__detail::__local_time_fmt;
1473 __string_view __utc(_GLIBCXX_WIDEN("UTC"), 3);
1474 if constexpr (chrono::__is_time_point_v<_Tp>)
1476 if constexpr (is_same_v<typename _Tp::clock,
1477 chrono::system_clock>)
1478 return __format::__write(std::move(__out), __utc);
1480 else if constexpr (__is_specialization_of<_Tp, __local_time_fmt>)
1482 if (__t._M_abbrev)
1484 string_view __sv = *__t._M_abbrev;
1485 if constexpr (is_same_v<_CharT, char>)
1486 return __format::__write(std::move(__out), __sv);
1487 else
1489 // TODO use resize_and_overwrite
1490 basic_string<_CharT> __ws(__sv.size(), _CharT());
1491 auto& __ct = use_facet<ctype<_CharT>>(_M_locale(__ctx));
1492 __ct.widen(__sv.begin(), __sv.end(), __ws.data());
1493 __string_view __wsv = __ws;
1494 return __format::__write(std::move(__out), __wsv);
1498 else if constexpr (__is_specialization_of<_Tp, __utc_leap_second>)
1499 return __format::__write(std::move(__out), __utc);
1501 __no_timezone_available();
1504 // %% handled in _M_format
1506 // A single digit character in the range '0'..'9'.
1507 static _CharT
1508 _S_digit(int __n) noexcept
1510 // Extra 9s avoid past-the-end read on bad input.
1511 return _GLIBCXX_WIDEN("0123456789999999")[__n & 0xf];
1514 // A string view of two digit characters, "00".."99".
1515 static basic_string_view<_CharT>
1516 _S_two_digits(int __n) noexcept
1518 return {
1519 _GLIBCXX_WIDEN("0001020304050607080910111213141516171819"
1520 "2021222324252627282930313233343536373839"
1521 "4041424344454647484950515253545556575859"
1522 "6061626364656667686970717273747576777879"
1523 "8081828384858687888990919293949596979899"
1524 "9999999999999999999999999999999999999999"
1525 "9999999999999999") + 2 * (__n & 0x7f),
1530 // Accessors for the components of chrono types:
1532 // Returns a hh_mm_ss.
1533 template<typename _Tp>
1534 static decltype(auto)
1535 _S_hms(const _Tp& __t)
1537 using ::std::chrono::__detail::__utc_leap_second;
1538 using ::std::chrono::__detail::__local_time_fmt;
1540 if constexpr (__is_specialization_of<_Tp, chrono::hh_mm_ss>)
1541 return __t;
1542 else if constexpr (__is_specialization_of<_Tp, __utc_leap_second>)
1543 return __t._M_time;
1544 else if constexpr (chrono::__is_duration_v<_Tp>)
1545 return chrono::hh_mm_ss<_Tp>(__t);
1546 else if constexpr (chrono::__is_time_point_v<_Tp>)
1547 return chrono::hh_mm_ss(__t - chrono::floor<chrono::days>(__t));
1548 else if constexpr (__is_specialization_of<_Tp, __local_time_fmt>)
1549 return _S_hms(__t._M_time);
1550 else
1552 __invalid_chrono_spec();
1553 return chrono::hh_mm_ss<chrono::seconds>();
1557 // Returns a sys_days or local_days.
1558 template<typename _Tp>
1559 static auto
1560 _S_days(const _Tp& __t)
1562 using namespace chrono;
1563 using ::std::chrono::__detail::__utc_leap_second;
1564 using ::std::chrono::__detail::__local_time_fmt;
1566 if constexpr (__is_time_point_v<_Tp>)
1567 return chrono::floor<days>(__t);
1568 else if constexpr (__is_specialization_of<_Tp, __utc_leap_second>)
1569 return __t._M_date;
1570 else if constexpr (__is_specialization_of<_Tp, __local_time_fmt>)
1571 return chrono::floor<days>(__t._M_time);
1572 else if constexpr (is_same_v<_Tp, year_month_day>
1573 || is_same_v<_Tp, year_month_day_last>
1574 || is_same_v<_Tp, year_month_weekday>
1575 || is_same_v<_Tp, year_month_weekday_last>)
1576 return sys_days(__t);
1577 else
1579 if constexpr (__is_duration_v<_Tp>)
1580 __not_valid_for_duration();
1581 else
1582 __invalid_chrono_spec();
1583 return chrono::sys_days();
1587 // Returns a year_month_day.
1588 template<typename _Tp>
1589 static chrono::year_month_day
1590 _S_date(const _Tp& __t)
1592 if constexpr (is_same_v<_Tp, chrono::year_month_day>)
1593 return __t;
1594 else
1595 return chrono::year_month_day(_S_days(__t));
1598 template<typename _Tp>
1599 static chrono::day
1600 _S_day(const _Tp& __t)
1602 using namespace chrono;
1604 if constexpr (is_same_v<_Tp, day>)
1605 return __t;
1606 else if constexpr (requires { __t.day(); })
1607 return __t.day();
1608 else
1609 return _S_date(__t).day();
1612 template<typename _Tp>
1613 static chrono::month
1614 _S_month(const _Tp& __t)
1616 using namespace chrono;
1618 if constexpr (is_same_v<_Tp, month>)
1619 return __t;
1620 else if constexpr (requires { __t.month(); })
1621 return __t.month();
1622 else
1623 return _S_date(__t).month();
1626 template<typename _Tp>
1627 static chrono::year
1628 _S_year(const _Tp& __t)
1630 using namespace chrono;
1632 if constexpr (is_same_v<_Tp, year>)
1633 return __t;
1634 else if constexpr (requires { __t.year(); })
1635 return __t.year();
1636 else
1637 return _S_date(__t).year();
1640 template<typename _Tp>
1641 static chrono::weekday
1642 _S_weekday(const _Tp& __t)
1644 using namespace ::std::chrono;
1645 using ::std::chrono::__detail::__local_time_fmt;
1647 if constexpr (is_same_v<_Tp, weekday>)
1648 return __t;
1649 else if constexpr (requires { __t.weekday(); })
1650 return __t.weekday();
1651 else if constexpr (is_same_v<_Tp, month_weekday>)
1652 return __t.weekday_indexed().weekday();
1653 else if constexpr (is_same_v<_Tp, month_weekday_last>)
1654 return __t.weekday_last().weekday();
1655 else
1656 return weekday(_S_days(__t));
1659 // Remove subsecond precision from a time_point.
1660 template<typename _Tp>
1661 static auto
1662 _S_floor_seconds(const _Tp& __t)
1664 using chrono::__detail::__local_time_fmt;
1665 if constexpr (chrono::__is_time_point_v<_Tp>
1666 || chrono::__is_duration_v<_Tp>)
1668 if constexpr (_Tp::period::den != 1)
1669 return chrono::floor<chrono::seconds>(__t);
1670 else
1671 return __t;
1673 else if constexpr (__is_specialization_of<_Tp, chrono::hh_mm_ss>)
1675 if constexpr (_Tp::fractional_width != 0)
1676 return chrono::floor<chrono::seconds>(__t.to_duration());
1677 else
1678 return __t;
1680 else if constexpr (__is_specialization_of<_Tp, __local_time_fmt>)
1681 return _S_floor_seconds(__t._M_time);
1682 else
1683 return __t;
1686 // Use the formatting locale's std::time_put facet to produce
1687 // a locale-specific representation.
1688 template<typename _Iter>
1689 _Iter
1690 _M_locale_fmt(_Iter __out, const locale& __loc, const struct tm& __tm,
1691 char __fmt, char __mod) const
1693 basic_ostringstream<_CharT> __os;
1694 const auto& __tp = use_facet<time_put<_CharT>>(__loc);
1695 __tp.put(__os, __os, _S_space, &__tm, __fmt, __mod);
1696 if (__os)
1697 __out = _M_write(std::move(__out), __loc, __os.view());
1698 return __out;
1702 } // namespace __format
1703 /// @endcond
1705 template<typename _Rep, typename _Period, typename _CharT>
1706 struct formatter<chrono::duration<_Rep, _Period>, _CharT>
1708 constexpr typename basic_format_parse_context<_CharT>::iterator
1709 parse(basic_format_parse_context<_CharT>& __pc)
1711 using namespace __format;
1712 auto __it = _M_f._M_parse(__pc, _Duration|_TimeOfDay);
1713 if constexpr (!is_floating_point_v<_Rep>)
1714 if (_M_f._M_spec._M_prec_kind != __format::_WP_none)
1715 __throw_format_error("format error: invalid precision for duration");
1716 return __it;
1719 template<typename _Out>
1720 typename basic_format_context<_Out, _CharT>::iterator
1721 format(const chrono::duration<_Rep, _Period>& __d,
1722 basic_format_context<_Out, _CharT>& __fc) const
1724 if constexpr (numeric_limits<_Rep>::is_signed)
1725 if (__d < __d.zero()) [[unlikely]]
1727 if constexpr (is_integral_v<_Rep>)
1729 // -d is undefined for the most negative integer.
1730 // Convert duration to corresponding unsigned rep.
1731 using _URep = make_unsigned_t<_Rep>;
1732 auto __ucnt = -static_cast<_URep>(__d.count());
1733 auto __ud = chrono::duration<_URep, _Period>(__ucnt);
1734 return _M_f._M_format(__ud, __fc, true);
1736 else
1737 return _M_f._M_format(-__d, __fc, true);
1739 return _M_f._M_format(__d, __fc, false);
1742 private:
1743 __format::__formatter_chrono<_CharT> _M_f;
1746 template<typename _CharT>
1747 struct formatter<chrono::day, _CharT>
1749 template<typename _ParseContext>
1750 constexpr typename _ParseContext::iterator
1751 parse(_ParseContext& __pc)
1752 { return _M_f._M_parse(__pc, __format::_Day); }
1754 template<typename _FormatContext>
1755 typename _FormatContext::iterator
1756 format(const chrono::day& __t, _FormatContext& __fc) const
1757 { return _M_f._M_format(__t, __fc); }
1759 private:
1760 __format::__formatter_chrono<_CharT> _M_f;
1763 template<typename _CharT>
1764 struct formatter<chrono::month, _CharT>
1766 template<typename _ParseContext>
1767 constexpr typename _ParseContext::iterator
1768 parse(_ParseContext& __pc)
1769 { return _M_f._M_parse(__pc, __format::_Month); }
1771 template<typename _FormatContext>
1772 typename _FormatContext::iterator
1773 format(const chrono::month& __t, _FormatContext& __fc) const
1774 { return _M_f._M_format(__t, __fc); }
1776 private:
1777 __format::__formatter_chrono<_CharT> _M_f;
1780 template<typename _CharT>
1781 struct formatter<chrono::year, _CharT>
1783 template<typename _ParseContext>
1784 constexpr typename _ParseContext::iterator
1785 parse(_ParseContext& __pc)
1786 { return _M_f._M_parse(__pc, __format::_Year); }
1788 template<typename _FormatContext>
1789 typename _FormatContext::iterator
1790 format(const chrono::year& __t, _FormatContext& __fc) const
1791 { return _M_f._M_format(__t, __fc); }
1793 private:
1794 __format::__formatter_chrono<_CharT> _M_f;
1797 template<typename _CharT>
1798 struct formatter<chrono::weekday, _CharT>
1800 template<typename _ParseContext>
1801 constexpr typename _ParseContext::iterator
1802 parse(_ParseContext& __pc)
1803 { return _M_f._M_parse(__pc, __format::_Weekday); }
1805 template<typename _FormatContext>
1806 typename _FormatContext::iterator
1807 format(const chrono::weekday& __t, _FormatContext& __fc) const
1808 { return _M_f._M_format(__t, __fc); }
1810 private:
1811 __format::__formatter_chrono<_CharT> _M_f;
1814 template<typename _CharT>
1815 struct formatter<chrono::weekday_indexed, _CharT>
1817 template<typename _ParseContext>
1818 constexpr typename _ParseContext::iterator
1819 parse(_ParseContext& __pc)
1820 { return _M_f._M_parse(__pc, __format::_Weekday); }
1822 template<typename _FormatContext>
1823 typename _FormatContext::iterator
1824 format(const chrono::weekday_indexed& __t, _FormatContext& __fc) const
1825 { return _M_f._M_format(__t, __fc); }
1827 private:
1828 __format::__formatter_chrono<_CharT> _M_f;
1831 template<typename _CharT>
1832 struct formatter<chrono::weekday_last, _CharT>
1834 template<typename _ParseContext>
1835 constexpr typename _ParseContext::iterator
1836 parse(_ParseContext& __pc)
1837 { return _M_f._M_parse(__pc, __format::_Weekday); }
1839 template<typename _FormatContext>
1840 typename _FormatContext::iterator
1841 format(const chrono::weekday_last& __t, _FormatContext& __fc) const
1842 { return _M_f._M_format(__t, __fc); }
1844 private:
1845 __format::__formatter_chrono<_CharT> _M_f;
1848 template<typename _CharT>
1849 struct formatter<chrono::month_day, _CharT>
1851 template<typename _ParseContext>
1852 constexpr typename _ParseContext::iterator
1853 parse(_ParseContext& __pc)
1854 { return _M_f._M_parse(__pc, __format::_Month|__format::_Day); }
1856 template<typename _FormatContext>
1857 typename _FormatContext::iterator
1858 format(const chrono::month_day& __t, _FormatContext& __fc) const
1859 { return _M_f._M_format(__t, __fc); }
1861 private:
1862 __format::__formatter_chrono<_CharT> _M_f;
1865 template<typename _CharT>
1866 struct formatter<chrono::month_day_last, _CharT>
1868 template<typename _ParseContext>
1869 constexpr typename _ParseContext::iterator
1870 parse(_ParseContext& __pc)
1871 { return _M_f._M_parse(__pc, __format::_Month|__format::_Day); }
1873 template<typename _FormatContext>
1874 typename _FormatContext::iterator
1875 format(const chrono::month_day_last& __t, _FormatContext& __fc) const
1876 { return _M_f._M_format(__t, __fc); }
1878 private:
1879 __format::__formatter_chrono<_CharT> _M_f;
1882 template<typename _CharT>
1883 struct formatter<chrono::month_weekday, _CharT>
1885 template<typename _ParseContext>
1886 constexpr typename _ParseContext::iterator
1887 parse(_ParseContext& __pc)
1888 { return _M_f._M_parse(__pc, __format::_Month|__format::_Weekday); }
1890 template<typename _FormatContext>
1891 typename _FormatContext::iterator
1892 format(const chrono::month_weekday& __t, _FormatContext& __fc) const
1893 { return _M_f._M_format(__t, __fc); }
1895 private:
1896 __format::__formatter_chrono<_CharT> _M_f;
1899 template<typename _CharT>
1900 struct formatter<chrono::month_weekday_last, _CharT>
1902 template<typename _ParseContext>
1903 constexpr typename _ParseContext::iterator
1904 parse(_ParseContext& __pc)
1905 { return _M_f._M_parse(__pc, __format::_Month|__format::_Weekday); }
1907 template<typename _FormatContext>
1908 typename _FormatContext::iterator
1909 format(const chrono::month_weekday_last& __t,
1910 _FormatContext& __fc) const
1911 { return _M_f._M_format(__t, __fc); }
1913 private:
1914 __format::__formatter_chrono<_CharT> _M_f;
1917 template<typename _CharT>
1918 struct formatter<chrono::year_month, _CharT>
1920 template<typename _ParseContext>
1921 constexpr typename _ParseContext::iterator
1922 parse(_ParseContext& __pc)
1923 { return _M_f._M_parse(__pc, __format::_Year|__format::_Month); }
1925 template<typename _FormatContext>
1926 typename _FormatContext::iterator
1927 format(const chrono::year_month& __t, _FormatContext& __fc) const
1928 { return _M_f._M_format(__t, __fc); }
1930 private:
1931 __format::__formatter_chrono<_CharT> _M_f;
1934 template<typename _CharT>
1935 struct formatter<chrono::year_month_day, _CharT>
1937 template<typename _ParseContext>
1938 constexpr typename _ParseContext::iterator
1939 parse(_ParseContext& __pc)
1940 { return _M_f._M_parse(__pc, __format::_Date); }
1942 template<typename _FormatContext>
1943 typename _FormatContext::iterator
1944 format(const chrono::year_month_day& __t, _FormatContext& __fc) const
1945 { return _M_f._M_format(__t, __fc); }
1947 private:
1948 __format::__formatter_chrono<_CharT> _M_f;
1951 template<typename _CharT>
1952 struct formatter<chrono::year_month_day_last, _CharT>
1954 template<typename _ParseContext>
1955 constexpr typename _ParseContext::iterator
1956 parse(_ParseContext& __pc)
1957 { return _M_f._M_parse(__pc, __format::_Date); }
1959 template<typename _FormatContext>
1960 typename _FormatContext::iterator
1961 format(const chrono::year_month_day_last& __t,
1962 _FormatContext& __fc) const
1963 { return _M_f._M_format(__t, __fc); }
1965 private:
1966 __format::__formatter_chrono<_CharT> _M_f;
1969 template<typename _CharT>
1970 struct formatter<chrono::year_month_weekday, _CharT>
1972 template<typename _ParseContext>
1973 constexpr typename _ParseContext::iterator
1974 parse(_ParseContext& __pc)
1975 { return _M_f._M_parse(__pc, __format::_Date); }
1977 template<typename _FormatContext>
1978 typename _FormatContext::iterator
1979 format(const chrono::year_month_weekday& __t,
1980 _FormatContext& __fc) const
1981 { return _M_f._M_format(__t, __fc); }
1983 private:
1984 __format::__formatter_chrono<_CharT> _M_f;
1987 template<typename _CharT>
1988 struct formatter<chrono::year_month_weekday_last, _CharT>
1990 template<typename _ParseContext>
1991 constexpr typename _ParseContext::iterator
1992 parse(_ParseContext& __pc)
1993 { return _M_f._M_parse(__pc, __format::_Date); }
1995 template<typename _FormatContext>
1996 typename _FormatContext::iterator
1997 format(const chrono::year_month_weekday_last& __t,
1998 _FormatContext& __fc) const
1999 { return _M_f._M_format(__t, __fc); }
2001 private:
2002 __format::__formatter_chrono<_CharT> _M_f;
2005 template<typename _Rep, typename _Period, typename _CharT>
2006 struct formatter<chrono::hh_mm_ss<chrono::duration<_Rep, _Period>>, _CharT>
2008 template<typename _ParseContext>
2009 constexpr typename _ParseContext::iterator
2010 parse(_ParseContext& __pc)
2011 { return _M_f._M_parse(__pc, __format::_TimeOfDay); }
2013 template<typename _FormatContext>
2014 typename _FormatContext::iterator
2015 format(const chrono::hh_mm_ss<chrono::duration<_Rep, _Period>>& __t,
2016 _FormatContext& __fc) const
2017 { return _M_f._M_format(__t, __fc); }
2019 private:
2020 __format::__formatter_chrono<_CharT> _M_f;
2023 #if _GLIBCXX_USE_CXX11_ABI || ! _GLIBCXX_USE_DUAL_ABI
2024 template<typename _CharT>
2025 struct formatter<chrono::sys_info, _CharT>
2027 template<typename _ParseContext>
2028 constexpr typename _ParseContext::iterator
2029 parse(_ParseContext& __pc)
2030 { return _M_f._M_parse(__pc, __format::_ChronoParts{}); }
2032 template<typename _FormatContext>
2033 typename _FormatContext::iterator
2034 format(const chrono::sys_info& __i, _FormatContext& __fc) const
2035 { return _M_f._M_format(__i, __fc); }
2037 private:
2038 __format::__formatter_chrono<_CharT> _M_f;
2041 template<typename _CharT>
2042 struct formatter<chrono::local_info, _CharT>
2044 template<typename _ParseContext>
2045 constexpr typename _ParseContext::iterator
2046 parse(_ParseContext& __pc)
2047 { return _M_f._M_parse(__pc, __format::_ChronoParts{}); }
2049 template<typename _FormatContext>
2050 typename _FormatContext::iterator
2051 format(const chrono::local_info& __i, _FormatContext& __fc) const
2052 { return _M_f._M_format(__i, __fc); }
2054 private:
2055 __format::__formatter_chrono<_CharT> _M_f;
2057 #endif
2059 template<typename _Duration, typename _CharT>
2060 struct formatter<chrono::sys_time<_Duration>, _CharT>
2062 template<typename _ParseContext>
2063 constexpr typename _ParseContext::iterator
2064 parse(_ParseContext& __pc)
2066 auto __next = _M_f._M_parse(__pc, __format::_ZonedDateTime);
2067 if constexpr (!__stream_insertable)
2068 if (_M_f._M_spec._M_chrono_specs.empty())
2069 __format::__invalid_chrono_spec(); // chrono-specs can't be empty
2070 return __next;
2073 template<typename _FormatContext>
2074 typename _FormatContext::iterator
2075 format(const chrono::sys_time<_Duration>& __t,
2076 _FormatContext& __fc) const
2077 { return _M_f._M_format(__t, __fc); }
2079 private:
2080 static constexpr bool __stream_insertable
2081 = requires (basic_ostream<_CharT>& __os,
2082 chrono::sys_time<_Duration> __t) { __os << __t; };
2084 __format::__formatter_chrono<_CharT> _M_f;
2087 template<typename _Duration, typename _CharT>
2088 struct formatter<chrono::utc_time<_Duration>, _CharT>
2089 : __format::__formatter_chrono<_CharT>
2091 template<typename _ParseContext>
2092 constexpr typename _ParseContext::iterator
2093 parse(_ParseContext& __pc)
2094 { return _M_f._M_parse(__pc, __format::_ZonedDateTime); }
2096 template<typename _FormatContext>
2097 typename _FormatContext::iterator
2098 format(const chrono::utc_time<_Duration>& __t,
2099 _FormatContext& __fc) const
2101 // Adjust by removing leap seconds to get equivalent sys_time.
2102 // We can't just use clock_cast because we want to know if the time
2103 // falls within a leap second insertion, and format seconds as "60".
2104 using chrono::__detail::__utc_leap_second;
2105 using chrono::seconds;
2106 using chrono::sys_time;
2107 using _CDur = common_type_t<_Duration, seconds>;
2108 const auto __li = chrono::get_leap_second_info(__t);
2109 sys_time<_CDur> __s{__t.time_since_epoch() - __li.elapsed};
2110 if (!__li.is_leap_second) [[likely]]
2111 return _M_f._M_format(__s, __fc);
2112 else
2113 return _M_f._M_format(__utc_leap_second(__s), __fc);
2116 private:
2117 friend formatter<chrono::__detail::__utc_leap_second<_Duration>, _CharT>;
2119 __format::__formatter_chrono<_CharT> _M_f;
2122 template<typename _Duration, typename _CharT>
2123 struct formatter<chrono::tai_time<_Duration>, _CharT>
2124 : __format::__formatter_chrono<_CharT>
2126 template<typename _ParseContext>
2127 constexpr typename _ParseContext::iterator
2128 parse(_ParseContext& __pc)
2129 { return _M_f._M_parse(__pc, __format::_ZonedDateTime); }
2131 template<typename _FormatContext>
2132 typename _FormatContext::iterator
2133 format(const chrono::tai_time<_Duration>& __t,
2134 _FormatContext& __fc) const
2136 // Convert to __local_time_fmt with abbrev "TAI" and offset 0s.
2137 // We use __local_time_fmt and not sys_time (as the standard implies)
2138 // because %Z for sys_time would print "UTC" and we want "TAI" here.
2140 // Offset is 1970y/January/1 - 1958y/January/1
2141 constexpr chrono::days __tai_offset = chrono::days(4383);
2142 using _CDur = common_type_t<_Duration, chrono::days>;
2143 chrono::local_time<_CDur> __lt(__t.time_since_epoch() - __tai_offset);
2144 const string __abbrev("TAI", 3);
2145 const chrono::seconds __off = 0s;
2146 const auto __lf = chrono::local_time_format(__lt, &__abbrev, &__off);
2147 return _M_f._M_format(__lf, __fc);
2150 private:
2151 __format::__formatter_chrono<_CharT> _M_f;
2154 template<typename _Duration, typename _CharT>
2155 struct formatter<chrono::gps_time<_Duration>, _CharT>
2156 : __format::__formatter_chrono<_CharT>
2158 template<typename _ParseContext>
2159 constexpr typename _ParseContext::iterator
2160 parse(_ParseContext& __pc)
2161 { return _M_f._M_parse(__pc, __format::_ZonedDateTime); }
2163 template<typename _FormatContext>
2164 typename _FormatContext::iterator
2165 format(const chrono::gps_time<_Duration>& __t,
2166 _FormatContext& __fc) const
2168 // Convert to __local_time_fmt with abbrev "GPS" and offset 0s.
2169 // We use __local_time_fmt and not sys_time (as the standard implies)
2170 // because %Z for sys_time would print "UTC" and we want "GPS" here.
2172 // Offset is 1980y/January/Sunday[1] - 1970y/January/1
2173 constexpr chrono::days __gps_offset = chrono::days(3657);
2174 using _CDur = common_type_t<_Duration, chrono::days>;
2175 chrono::local_time<_CDur> __lt(__t.time_since_epoch() + __gps_offset);
2176 const string __abbrev("GPS", 3);
2177 const chrono::seconds __off = 0s;
2178 const auto __lf = chrono::local_time_format(__lt, &__abbrev, &__off);
2179 return _M_f._M_format(__lf, __fc);
2182 private:
2183 __format::__formatter_chrono<_CharT> _M_f;
2186 template<typename _Duration, typename _CharT>
2187 struct formatter<chrono::file_time<_Duration>, _CharT>
2189 template<typename _ParseContext>
2190 constexpr typename _ParseContext::iterator
2191 parse(_ParseContext& __pc)
2192 { return _M_f._M_parse(__pc, __format::_ZonedDateTime); }
2194 template<typename _FormatContext>
2195 typename _FormatContext::iterator
2196 format(const chrono::file_time<_Duration>& __t,
2197 _FormatContext& __ctx) const
2199 using namespace chrono;
2200 return _M_f._M_format(chrono::clock_cast<system_clock>(__t), __ctx);
2203 private:
2204 __format::__formatter_chrono<_CharT> _M_f;
2207 template<typename _Duration, typename _CharT>
2208 struct formatter<chrono::local_time<_Duration>, _CharT>
2210 template<typename _ParseContext>
2211 constexpr typename _ParseContext::iterator
2212 parse(_ParseContext& __pc)
2213 { return _M_f._M_parse(__pc, __format::_DateTime); }
2215 template<typename _FormatContext>
2216 typename _FormatContext::iterator
2217 format(const chrono::local_time<_Duration>& __t,
2218 _FormatContext& __ctx) const
2219 { return _M_f._M_format(__t, __ctx); }
2221 private:
2222 __format::__formatter_chrono<_CharT> _M_f;
2225 template<typename _Duration, typename _CharT>
2226 struct formatter<chrono::__detail::__local_time_fmt<_Duration>, _CharT>
2228 template<typename _ParseContext>
2229 constexpr typename _ParseContext::iterator
2230 parse(_ParseContext& __pc)
2231 { return _M_f._M_parse(__pc, __format::_ZonedDateTime); }
2233 template<typename _FormatContext>
2234 typename _FormatContext::iterator
2235 format(const chrono::__detail::__local_time_fmt<_Duration>& __t,
2236 _FormatContext& __ctx) const
2237 { return _M_f._M_format(__t, __ctx, /* use %Z for {} */ true); }
2239 private:
2240 __format::__formatter_chrono<_CharT> _M_f;
2243 #if _GLIBCXX_USE_CXX11_ABI || ! _GLIBCXX_USE_DUAL_ABI
2244 template<typename _Duration, typename _TimeZonePtr, typename _CharT>
2245 struct formatter<chrono::zoned_time<_Duration, _TimeZonePtr>, _CharT>
2246 : formatter<chrono::__detail::__local_time_fmt_for<_Duration>, _CharT>
2248 template<typename _FormatContext>
2249 typename _FormatContext::iterator
2250 format(const chrono::zoned_time<_Duration, _TimeZonePtr>& __tp,
2251 _FormatContext& __ctx) const
2253 using _Ltf = chrono::__detail::__local_time_fmt_for<_Duration>;
2254 using _Base = formatter<_Ltf, _CharT>;
2255 const chrono::sys_info __info = __tp.get_info();
2256 const auto __lf = chrono::local_time_format(__tp.get_local_time(),
2257 &__info.abbrev,
2258 &__info.offset);
2259 return _Base::format(__lf, __ctx);
2262 #endif
2264 // Partial specialization needed for %c formatting of __utc_leap_second.
2265 template<typename _Duration, typename _CharT>
2266 struct formatter<chrono::__detail::__utc_leap_second<_Duration>, _CharT>
2267 : formatter<chrono::utc_time<_Duration>, _CharT>
2269 template<typename _FormatContext>
2270 typename _FormatContext::iterator
2271 format(const chrono::__detail::__utc_leap_second<_Duration>& __t,
2272 _FormatContext& __fc) const
2273 { return this->_M_f._M_format(__t, __fc); }
2276 namespace chrono
2278 /// @addtogroup chrono
2279 /// @{
2281 /// @cond undocumented
2282 namespace __detail
2284 template<typename _Duration = seconds>
2285 struct _Parser
2287 static_assert(is_same_v<common_type_t<_Duration, seconds>, _Duration>);
2289 explicit
2290 _Parser(__format::_ChronoParts __need) : _M_need(__need) { }
2292 _Parser(_Parser&&) = delete;
2293 void operator=(_Parser&&) = delete;
2295 _Duration _M_time{}; // since midnight
2296 sys_days _M_sys_days{};
2297 year_month_day _M_ymd{};
2298 weekday _M_wd{};
2299 __format::_ChronoParts _M_need;
2300 unsigned _M_is_leap_second : 1 {};
2301 unsigned _M_reserved : 15 {};
2303 template<typename _CharT, typename _Traits, typename _Alloc>
2304 basic_istream<_CharT, _Traits>&
2305 operator()(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt,
2306 basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr,
2307 minutes* __offset = nullptr);
2309 private:
2310 // Read an unsigned integer from the stream and return it.
2311 // Extract no more than __n digits. Set failbit if an integer isn't read.
2312 template<typename _CharT, typename _Traits>
2313 static int_least32_t
2314 _S_read_unsigned(basic_istream<_CharT, _Traits>& __is,
2315 ios_base::iostate& __err, int __n)
2317 int_least32_t __val = _S_try_read_digit(__is, __err);
2318 if (__val == -1) [[unlikely]]
2319 __err |= ios_base::failbit;
2320 else
2322 int __n1 = (std::min)(__n, 9);
2323 // Cannot overflow __val unless we read more than 9 digits
2324 for (int __i = 1; __i < __n1; ++__i)
2325 if (auto __dig = _S_try_read_digit(__is, __err); __dig != -1)
2327 __val *= 10;
2328 __val += __dig;
2331 while (__n1++ < __n) [[unlikely]]
2332 if (auto __dig = _S_try_read_digit(__is, __err); __dig != -1)
2334 if (__builtin_mul_overflow(__val, 10, &__val)
2335 || __builtin_add_overflow(__val, __dig, &__val))
2337 __err |= ios_base::failbit;
2338 return -1;
2342 return __val;
2345 // Read an unsigned integer from the stream and return it.
2346 // Extract no more than __n digits. Set failbit if an integer isn't read.
2347 template<typename _CharT, typename _Traits>
2348 static int_least32_t
2349 _S_read_signed(basic_istream<_CharT, _Traits>& __is,
2350 ios_base::iostate& __err, int __n)
2352 auto __sign = __is.peek();
2353 if (__sign == '-' || __sign == '+')
2354 (void) __is.get();
2355 int_least32_t __val = _S_read_unsigned(__is, __err, __n);
2356 if (__err & ios_base::failbit)
2358 if (__sign == '-') [[unlikely]]
2359 __val *= -1;
2361 return __val;
2364 // Read a digit from the stream and return it, or return -1.
2365 // If no digit is read eofbit will be set (but not failbit).
2366 template<typename _CharT, typename _Traits>
2367 static int_least32_t
2368 _S_try_read_digit(basic_istream<_CharT, _Traits>& __is,
2369 ios_base::iostate& __err)
2371 int_least32_t __val = -1;
2372 auto __i = __is.peek();
2373 if (!_Traits::eq_int_type(__i, _Traits::eof())) [[likely]]
2375 _CharT __c = _Traits::to_char_type(__i);
2376 if (_CharT('0') <= __c && __c <= _CharT('9')) [[likely]]
2378 (void) __is.get();
2379 __val = __c - _CharT('0');
2382 else
2383 __err |= ios_base::eofbit;
2384 return __val;
2387 // Read the specified character and return true.
2388 // If the character is not found, set failbit and return false.
2389 template<typename _CharT, typename _Traits>
2390 static bool
2391 _S_read_chr(basic_istream<_CharT, _Traits>& __is,
2392 ios_base::iostate& __err, _CharT __c)
2394 auto __i = __is.peek();
2395 if (_Traits::eq_int_type(__i, _Traits::eof()))
2396 __err |= ios_base::eofbit;
2397 else if (_Traits::to_char_type(__i) == __c) [[likely]]
2399 (void) __is.get();
2400 return true;
2402 __err |= ios_base::failbit;
2403 return false;
2407 template<typename _Duration>
2408 using _Parser_t = _Parser<common_type_t<_Duration, seconds>>;
2410 template<typename _Duration>
2411 consteval bool
2412 __use_floor()
2414 if constexpr (_Duration::period::den == 1)
2416 switch (_Duration::period::num)
2418 case minutes::period::num:
2419 case hours::period::num:
2420 case days::period::num:
2421 case weeks::period::num:
2422 case years::period::num:
2423 return true;
2426 return false;
2429 // A "do the right thing" rounding function for duration and time_point
2430 // values extracted by from_stream. When treat_as_floating_point is true
2431 // we don't want to do anything, just a straightforward conversion.
2432 // When the destination type has a period of minutes, hours, days, weeks,
2433 // or years, we use chrono::floor to truncate towards negative infinity.
2434 // This ensures that an extracted timestamp such as 2024-09-05 13:00:00
2435 // will produce 2024-09-05 when rounded to days, rather than rounding up
2436 // to 2024-09-06 (a different day).
2437 // Otherwise, use chrono::round to get the nearest value representable
2438 // in the destination type.
2439 template<typename _ToDur, typename _Tp>
2440 constexpr auto
2441 __round(const _Tp& __t)
2443 if constexpr (__is_duration_v<_Tp>)
2445 if constexpr (treat_as_floating_point_v<typename _Tp::rep>)
2446 return chrono::duration_cast<_ToDur>(__t);
2447 else if constexpr (__detail::__use_floor<_ToDur>())
2448 return chrono::floor<_ToDur>(__t);
2449 else
2450 return chrono::round<_ToDur>(__t);
2452 else
2454 static_assert(__is_time_point_v<_Tp>);
2455 using _Tpt = time_point<typename _Tp::clock, _ToDur>;
2456 return _Tpt(__detail::__round<_ToDur>(__t.time_since_epoch()));
2460 } // namespace __detail
2461 /// @endcond
2463 template<typename _CharT, typename _Traits, typename _Rep, typename _Period,
2464 typename _Alloc = allocator<_CharT>>
2465 inline basic_istream<_CharT, _Traits>&
2466 from_stream(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt,
2467 duration<_Rep, _Period>& __d,
2468 basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr,
2469 minutes* __offset = nullptr)
2471 auto __need = __format::_ChronoParts::_TimeOfDay;
2472 __detail::_Parser_t<duration<_Rep, _Period>> __p(__need);
2473 if (__p(__is, __fmt, __abbrev, __offset))
2474 __d = __detail::__round<duration<_Rep, _Period>>(__p._M_time);
2475 return __is;
2478 template<typename _CharT, typename _Traits>
2479 inline basic_ostream<_CharT, _Traits>&
2480 operator<<(basic_ostream<_CharT, _Traits>& __os, const day& __d)
2482 using _Ctx = __format::__format_context<_CharT>;
2483 using _Str = basic_string_view<_CharT>;
2484 _Str __s = _GLIBCXX_WIDEN("{:02d} is not a valid day");
2485 if (__d.ok())
2486 __s = __s.substr(0, 6);
2487 auto __u = (unsigned)__d;
2488 __os << std::vformat(__s, make_format_args<_Ctx>(__u));
2489 return __os;
2492 template<typename _CharT, typename _Traits,
2493 typename _Alloc = allocator<_CharT>>
2494 inline basic_istream<_CharT, _Traits>&
2495 from_stream(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt,
2496 day& __d,
2497 basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr,
2498 minutes* __offset = nullptr)
2500 __detail::_Parser<> __p(__format::_ChronoParts::_Day);
2501 if (__p(__is, __fmt, __abbrev, __offset))
2502 __d = __p._M_ymd.day();
2503 return __is;
2506 template<typename _CharT, typename _Traits>
2507 inline basic_ostream<_CharT, _Traits>&
2508 operator<<(basic_ostream<_CharT, _Traits>& __os, const month& __m)
2510 using _Ctx = __format::__format_context<_CharT>;
2511 using _Str = basic_string_view<_CharT>;
2512 _Str __s = _GLIBCXX_WIDEN("{:L%b}{} is not a valid month");
2513 if (__m.ok())
2514 __os << std::vformat(__os.getloc(), __s.substr(0, 6),
2515 make_format_args<_Ctx>(__m));
2516 else
2518 auto __u = (unsigned)__m;
2519 __os << std::vformat(__s.substr(6), make_format_args<_Ctx>(__u));
2521 return __os;
2524 template<typename _CharT, typename _Traits,
2525 typename _Alloc = allocator<_CharT>>
2526 inline basic_istream<_CharT, _Traits>&
2527 from_stream(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt,
2528 month& __m,
2529 basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr,
2530 minutes* __offset = nullptr)
2532 __detail::_Parser<> __p(__format::_ChronoParts::_Month);
2533 if (__p(__is, __fmt, __abbrev, __offset))
2534 __m = __p._M_ymd.month();
2535 return __is;
2538 template<typename _CharT, typename _Traits>
2539 inline basic_ostream<_CharT, _Traits>&
2540 operator<<(basic_ostream<_CharT, _Traits>& __os, const year& __y)
2542 using _Ctx = __format::__format_context<_CharT>;
2543 using _Str = basic_string_view<_CharT>;
2544 _Str __s = _GLIBCXX_WIDEN("-{:04d} is not a valid year");
2545 if (__y.ok())
2546 __s = __s.substr(0, 7);
2547 int __i = (int)__y;
2548 if (__i >= 0) [[likely]]
2549 __s.remove_prefix(1);
2550 else
2551 __i = -__i;
2552 __os << std::vformat(__s, make_format_args<_Ctx>(__i));
2553 return __os;
2556 template<typename _CharT, typename _Traits,
2557 typename _Alloc = allocator<_CharT>>
2558 inline basic_istream<_CharT, _Traits>&
2559 from_stream(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt,
2560 year& __y,
2561 basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr,
2562 minutes* __offset = nullptr)
2564 __detail::_Parser<> __p(__format::_ChronoParts::_Year);
2565 if (__p(__is, __fmt, __abbrev, __offset))
2566 __y = __p._M_ymd.year();
2567 return __is;
2570 template<typename _CharT, typename _Traits>
2571 inline basic_ostream<_CharT, _Traits>&
2572 operator<<(basic_ostream<_CharT, _Traits>& __os, const weekday& __wd)
2574 using _Ctx = __format::__format_context<_CharT>;
2575 using _Str = basic_string_view<_CharT>;
2576 _Str __s = _GLIBCXX_WIDEN("{:L%a}{} is not a valid weekday");
2577 if (__wd.ok())
2578 __os << std::vformat(__os.getloc(), __s.substr(0, 6),
2579 make_format_args<_Ctx>(__wd));
2580 else
2582 auto __c = __wd.c_encoding();
2583 __os << std::vformat(__s.substr(6), make_format_args<_Ctx>(__c));
2585 return __os;
2588 template<typename _CharT, typename _Traits,
2589 typename _Alloc = allocator<_CharT>>
2590 inline basic_istream<_CharT, _Traits>&
2591 from_stream(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt,
2592 weekday& __wd,
2593 basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr,
2594 minutes* __offset = nullptr)
2596 __detail::_Parser<> __p(__format::_ChronoParts::_Weekday);
2597 if (__p(__is, __fmt, __abbrev, __offset))
2598 __wd = __p._M_wd;
2599 return __is;
2602 template<typename _CharT, typename _Traits>
2603 inline basic_ostream<_CharT, _Traits>&
2604 operator<<(basic_ostream<_CharT, _Traits>& __os,
2605 const weekday_indexed& __wdi)
2607 // The standard says to format wdi.weekday() and wdi.index() using
2608 // either "{:L}[{}]" or "{:L}[{} is not a valid index]". The {:L} spec
2609 // means to format the weekday using ostringstream, so just do that.
2610 basic_stringstream<_CharT> __os2;
2611 __os2.imbue(__os.getloc());
2612 __os2 << __wdi.weekday();
2613 const auto __i = __wdi.index();
2614 basic_string_view<_CharT> __s
2615 = _GLIBCXX_WIDEN("[ is not a valid index]");
2616 __os2 << __s[0];
2617 __os2 << std::format(_GLIBCXX_WIDEN("{}"), __i);
2618 if (__i >= 1 && __i <= 5)
2619 __os2 << __s.back();
2620 else
2621 __os2 << __s.substr(1);
2622 __os << __os2.view();
2623 return __os;
2626 template<typename _CharT, typename _Traits>
2627 inline basic_ostream<_CharT, _Traits>&
2628 operator<<(basic_ostream<_CharT, _Traits>& __os,
2629 const weekday_last& __wdl)
2631 // As above, just write straight to a stringstream, as if by "{:L}[last]"
2632 basic_stringstream<_CharT> __os2;
2633 __os2.imbue(__os.getloc());
2634 __os2 << __wdl.weekday() << _GLIBCXX_WIDEN("[last]");
2635 __os << __os2.view();
2636 return __os;
2639 template<typename _CharT, typename _Traits>
2640 inline basic_ostream<_CharT, _Traits>&
2641 operator<<(basic_ostream<_CharT, _Traits>& __os, const month_day& __md)
2643 // As above, just write straight to a stringstream, as if by "{:L}/{}"
2644 basic_stringstream<_CharT> __os2;
2645 __os2.imbue(__os.getloc());
2646 __os2 << __md.month();
2647 if constexpr (is_same_v<_CharT, char>)
2648 __os2 << '/';
2649 else
2650 __os2 << L'/';
2651 __os2 << __md.day();
2652 __os << __os2.view();
2653 return __os;
2656 template<typename _CharT, typename _Traits,
2657 typename _Alloc = allocator<_CharT>>
2658 inline basic_istream<_CharT, _Traits>&
2659 from_stream(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt,
2660 month_day& __md,
2661 basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr,
2662 minutes* __offset = nullptr)
2664 using __format::_ChronoParts;
2665 auto __need = _ChronoParts::_Month | _ChronoParts::_Day;
2666 __detail::_Parser<> __p(__need);
2667 if (__p(__is, __fmt, __abbrev, __offset))
2668 __md = month_day(__p._M_ymd.month(), __p._M_ymd.day());
2669 return __is;
2672 template<typename _CharT, typename _Traits>
2673 inline basic_ostream<_CharT, _Traits>&
2674 operator<<(basic_ostream<_CharT, _Traits>& __os,
2675 const month_day_last& __mdl)
2677 // As above, just write straight to a stringstream, as if by "{:L}/last"
2678 basic_stringstream<_CharT> __os2;
2679 __os2.imbue(__os.getloc());
2680 __os2 << __mdl.month() << _GLIBCXX_WIDEN("/last");
2681 __os << __os2.view();
2682 return __os;
2685 template<typename _CharT, typename _Traits>
2686 inline basic_ostream<_CharT, _Traits>&
2687 operator<<(basic_ostream<_CharT, _Traits>& __os,
2688 const month_weekday& __mwd)
2690 // As above, just write straight to a stringstream, as if by "{:L}/{:L}"
2691 basic_stringstream<_CharT> __os2;
2692 __os2.imbue(__os.getloc());
2693 __os2 << __mwd.month();
2694 if constexpr (is_same_v<_CharT, char>)
2695 __os2 << '/';
2696 else
2697 __os2 << L'/';
2698 __os2 << __mwd.weekday_indexed();
2699 __os << __os2.view();
2700 return __os;
2703 template<typename _CharT, typename _Traits>
2704 inline basic_ostream<_CharT, _Traits>&
2705 operator<<(basic_ostream<_CharT, _Traits>& __os,
2706 const month_weekday_last& __mwdl)
2708 // As above, just write straight to a stringstream, as if by "{:L}/{:L}"
2709 basic_stringstream<_CharT> __os2;
2710 __os2.imbue(__os.getloc());
2711 __os2 << __mwdl.month();
2712 if constexpr (is_same_v<_CharT, char>)
2713 __os2 << '/';
2714 else
2715 __os2 << L'/';
2716 __os2 << __mwdl.weekday_last();
2717 __os << __os2.view();
2718 return __os;
2721 template<typename _CharT, typename _Traits>
2722 inline basic_ostream<_CharT, _Traits>&
2723 operator<<(basic_ostream<_CharT, _Traits>& __os, const year_month& __ym)
2725 // As above, just write straight to a stringstream, as if by "{}/{:L}"
2726 basic_stringstream<_CharT> __os2;
2727 __os2.imbue(__os.getloc());
2728 __os2 << __ym.year();
2729 if constexpr (is_same_v<_CharT, char>)
2730 __os2 << '/';
2731 else
2732 __os2 << L'/';
2733 __os2 << __ym.month();
2734 __os << __os2.view();
2735 return __os;
2738 template<typename _CharT, typename _Traits,
2739 typename _Alloc = allocator<_CharT>>
2740 inline basic_istream<_CharT, _Traits>&
2741 from_stream(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt,
2742 year_month& __ym,
2743 basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr,
2744 minutes* __offset = nullptr)
2746 using __format::_ChronoParts;
2747 auto __need = _ChronoParts::_Year | _ChronoParts::_Month;
2748 __detail::_Parser<> __p(__need);
2749 if (__p(__is, __fmt, __abbrev, __offset))
2750 __ym = year_month(__p._M_ymd.year(), __p._M_ymd.month());
2751 return __is;
2754 template<typename _CharT, typename _Traits>
2755 inline basic_ostream<_CharT, _Traits>&
2756 operator<<(basic_ostream<_CharT, _Traits>& __os,
2757 const year_month_day& __ymd)
2759 using _Ctx = __format::__format_context<_CharT>;
2760 using _Str = basic_string_view<_CharT>;
2761 _Str __s = _GLIBCXX_WIDEN("{:%F} is not a valid date");
2762 __os << std::vformat(__ymd.ok() ? __s.substr(0, 5) : __s,
2763 make_format_args<_Ctx>(__ymd));
2764 return __os;
2767 template<typename _CharT, typename _Traits,
2768 typename _Alloc = allocator<_CharT>>
2769 inline basic_istream<_CharT, _Traits>&
2770 from_stream(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt,
2771 year_month_day& __ymd,
2772 basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr,
2773 minutes* __offset = nullptr)
2775 using __format::_ChronoParts;
2776 auto __need = _ChronoParts::_Year | _ChronoParts::_Month
2777 | _ChronoParts::_Day;
2778 __detail::_Parser<> __p(__need);
2779 if (__p(__is, __fmt, __abbrev, __offset))
2780 __ymd = __p._M_ymd;
2781 return __is;
2784 template<typename _CharT, typename _Traits>
2785 inline basic_ostream<_CharT, _Traits>&
2786 operator<<(basic_ostream<_CharT, _Traits>& __os,
2787 const year_month_day_last& __ymdl)
2789 // As above, just write straight to a stringstream, as if by "{}/{:L}"
2790 basic_stringstream<_CharT> __os2;
2791 __os2.imbue(__os.getloc());
2792 __os2 << __ymdl.year();
2793 if constexpr (is_same_v<_CharT, char>)
2794 __os2 << '/';
2795 else
2796 __os2 << L'/';
2797 __os2 << __ymdl.month_day_last();
2798 __os << __os2.view();
2799 return __os;
2802 template<typename _CharT, typename _Traits>
2803 inline basic_ostream<_CharT, _Traits>&
2804 operator<<(basic_ostream<_CharT, _Traits>& __os,
2805 const year_month_weekday& __ymwd)
2807 // As above, just write straight to a stringstream, as if by
2808 // "{}/{:L}/{:L}"
2809 basic_stringstream<_CharT> __os2;
2810 __os2.imbue(__os.getloc());
2811 _CharT __slash;
2812 if constexpr (is_same_v<_CharT, char>)
2813 __slash = '/';
2814 else
2815 __slash = L'/';
2816 __os2 << __ymwd.year() << __slash << __ymwd.month() << __slash
2817 << __ymwd.weekday_indexed();
2818 __os << __os2.view();
2819 return __os;
2822 template<typename _CharT, typename _Traits>
2823 inline basic_ostream<_CharT, _Traits>&
2824 operator<<(basic_ostream<_CharT, _Traits>& __os,
2825 const year_month_weekday_last& __ymwdl)
2827 // As above, just write straight to a stringstream, as if by
2828 // "{}/{:L}/{:L}"
2829 basic_stringstream<_CharT> __os2;
2830 __os2.imbue(__os.getloc());
2831 _CharT __slash;
2832 if constexpr (is_same_v<_CharT, char>)
2833 __slash = '/';
2834 else
2835 __slash = L'/';
2836 __os2 << __ymwdl.year() << __slash << __ymwdl.month() << __slash
2837 << __ymwdl.weekday_last();
2838 __os << __os2.view();
2839 return __os;
2842 template<typename _CharT, typename _Traits, typename _Duration>
2843 inline basic_ostream<_CharT, _Traits>&
2844 operator<<(basic_ostream<_CharT, _Traits>& __os,
2845 const hh_mm_ss<_Duration>& __hms)
2847 return __os << format(__os.getloc(), _GLIBCXX_WIDEN("{:L%T}"), __hms);
2850 #if _GLIBCXX_USE_CXX11_ABI || ! _GLIBCXX_USE_DUAL_ABI
2851 /// Writes a sys_info object to an ostream in an unspecified format.
2852 template<typename _CharT, typename _Traits>
2853 basic_ostream<_CharT, _Traits>&
2854 operator<<(basic_ostream<_CharT, _Traits>& __os, const sys_info& __i)
2856 __os << '[' << __i.begin << ',' << __i.end
2857 << ',' << hh_mm_ss(__i.offset) << ',' << __i.save
2858 << ',' << __i.abbrev << ']';
2859 return __os;
2862 /// Writes a local_info object to an ostream in an unspecified format.
2863 template<typename _CharT, typename _Traits>
2864 basic_ostream<_CharT, _Traits>&
2865 operator<<(basic_ostream<_CharT, _Traits>& __os, const local_info& __li)
2867 __os << '[';
2868 if (__li.result == local_info::unique)
2869 __os << __li.first;
2870 else
2872 if (__li.result == local_info::nonexistent)
2873 __os << "nonexistent";
2874 else
2875 __os << "ambiguous";
2876 __os << " local time between " << __li.first;
2877 __os << " and " << __li.second;
2879 __os << ']';
2880 return __os;
2883 template<typename _CharT, typename _Traits, typename _Duration,
2884 typename _TimeZonePtr>
2885 inline basic_ostream<_CharT, _Traits>&
2886 operator<<(basic_ostream<_CharT, _Traits>& __os,
2887 const zoned_time<_Duration, _TimeZonePtr>& __t)
2889 __os << format(__os.getloc(), _GLIBCXX_WIDEN("{:L%F %T %Z}"), __t);
2890 return __os;
2892 #endif
2894 template<typename _CharT, typename _Traits, typename _Duration>
2895 requires (!treat_as_floating_point_v<typename _Duration::rep>)
2896 && ratio_less_v<typename _Duration::period, days::period>
2897 inline basic_ostream<_CharT, _Traits>&
2898 operator<<(basic_ostream<_CharT, _Traits>& __os,
2899 const sys_time<_Duration>& __tp)
2901 __os << std::format(__os.getloc(), _GLIBCXX_WIDEN("{:L%F %T}"), __tp);
2902 return __os;
2905 template<typename _CharT, typename _Traits>
2906 inline basic_ostream<_CharT, _Traits>&
2907 operator<<(basic_ostream<_CharT, _Traits>& __os, const sys_days& __dp)
2909 __os << year_month_day{__dp};
2910 return __os;
2913 template<typename _CharT, typename _Traits, typename _Duration,
2914 typename _Alloc = allocator<_CharT>>
2915 basic_istream<_CharT, _Traits>&
2916 from_stream(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt,
2917 sys_time<_Duration>& __tp,
2918 basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr,
2919 minutes* __offset = nullptr)
2921 minutes __off{};
2922 if (!__offset)
2923 __offset = &__off;
2924 using __format::_ChronoParts;
2925 auto __need = _ChronoParts::_Year | _ChronoParts::_Month
2926 | _ChronoParts::_Day | _ChronoParts::_TimeOfDay;
2927 __detail::_Parser_t<_Duration> __p(__need);
2928 if (__p(__is, __fmt, __abbrev, __offset))
2930 if (__p._M_is_leap_second)
2931 __is.setstate(ios_base::failbit);
2932 else
2934 auto __st = __p._M_sys_days + __p._M_time - *__offset;
2935 __tp = __detail::__round<_Duration>(__st);
2938 return __is;
2941 template<typename _CharT, typename _Traits, typename _Duration>
2942 inline basic_ostream<_CharT, _Traits>&
2943 operator<<(basic_ostream<_CharT, _Traits>& __os,
2944 const utc_time<_Duration>& __t)
2946 __os << std::format(__os.getloc(), _GLIBCXX_WIDEN("{:L%F %T}"), __t);
2947 return __os;
2950 template<typename _CharT, typename _Traits, typename _Duration,
2951 typename _Alloc = allocator<_CharT>>
2952 inline basic_istream<_CharT, _Traits>&
2953 from_stream(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt,
2954 utc_time<_Duration>& __tp,
2955 basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr,
2956 minutes* __offset = nullptr)
2958 minutes __off{};
2959 if (!__offset)
2960 __offset = &__off;
2961 using __format::_ChronoParts;
2962 auto __need = _ChronoParts::_Year | _ChronoParts::_Month
2963 | _ChronoParts::_Day | _ChronoParts::_TimeOfDay;
2964 __detail::_Parser_t<_Duration> __p(__need);
2965 if (__p(__is, __fmt, __abbrev, __offset))
2967 // Converting to utc_time before adding _M_time is necessary for
2968 // "23:59:60" to correctly produce a time within a leap second.
2969 auto __ut = utc_clock::from_sys(__p._M_sys_days) + __p._M_time
2970 - *__offset;
2971 __tp = __detail::__round<_Duration>(__ut);
2973 return __is;
2976 template<typename _CharT, typename _Traits, typename _Duration>
2977 inline basic_ostream<_CharT, _Traits>&
2978 operator<<(basic_ostream<_CharT, _Traits>& __os,
2979 const tai_time<_Duration>& __t)
2981 __os << std::format(__os.getloc(), _GLIBCXX_WIDEN("{:L%F %T}"), __t);
2982 return __os;
2985 template<typename _CharT, typename _Traits, typename _Duration,
2986 typename _Alloc = allocator<_CharT>>
2987 inline basic_istream<_CharT, _Traits>&
2988 from_stream(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt,
2989 tai_time<_Duration>& __tp,
2990 basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr,
2991 minutes* __offset = nullptr)
2993 minutes __off{};
2994 if (!__offset)
2995 __offset = &__off;
2996 using __format::_ChronoParts;
2997 auto __need = _ChronoParts::_Year | _ChronoParts::_Month
2998 | _ChronoParts::_Day | _ChronoParts::_TimeOfDay;
2999 __detail::_Parser_t<_Duration> __p(__need);
3000 if (__p(__is, __fmt, __abbrev, __offset))
3002 if (__p._M_is_leap_second)
3003 __is.setstate(ios_base::failbit);
3004 else
3006 constexpr sys_days __epoch(-days(4383)); // 1958y/1/1
3007 auto __d = __p._M_sys_days - __epoch + __p._M_time - *__offset;
3008 tai_time<common_type_t<_Duration, seconds>> __tt(__d);
3009 __tp = __detail::__round<_Duration>(__tt);
3012 return __is;
3015 template<typename _CharT, typename _Traits, typename _Duration>
3016 inline basic_ostream<_CharT, _Traits>&
3017 operator<<(basic_ostream<_CharT, _Traits>& __os,
3018 const gps_time<_Duration>& __t)
3020 __os << std::format(__os.getloc(), _GLIBCXX_WIDEN("{:L%F %T}"), __t);
3021 return __os;
3024 template<typename _CharT, typename _Traits, typename _Duration,
3025 typename _Alloc = allocator<_CharT>>
3026 inline basic_istream<_CharT, _Traits>&
3027 from_stream(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt,
3028 gps_time<_Duration>& __tp,
3029 basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr,
3030 minutes* __offset = nullptr)
3032 minutes __off{};
3033 if (!__offset)
3034 __offset = &__off;
3035 using __format::_ChronoParts;
3036 auto __need = _ChronoParts::_Year | _ChronoParts::_Month
3037 | _ChronoParts::_Day | _ChronoParts::_TimeOfDay;
3038 __detail::_Parser_t<_Duration> __p(__need);
3039 if (__p(__is, __fmt, __abbrev, __offset))
3041 if (__p._M_is_leap_second)
3042 __is.setstate(ios_base::failbit);
3043 else
3045 constexpr sys_days __epoch(days(3657)); // 1980y/1/Sunday[1]
3046 auto __d = __p._M_sys_days - __epoch + __p._M_time - *__offset;
3047 gps_time<common_type_t<_Duration, seconds>> __gt(__d);
3048 __tp = __detail::__round<_Duration>(__gt);
3051 return __is;
3054 template<typename _CharT, typename _Traits, typename _Duration>
3055 inline basic_ostream<_CharT, _Traits>&
3056 operator<<(basic_ostream<_CharT, _Traits>& __os,
3057 const file_time<_Duration>& __t)
3059 __os << std::format(__os.getloc(), _GLIBCXX_WIDEN("{:L%F %T}"), __t);
3060 return __os;
3063 template<typename _CharT, typename _Traits, typename _Duration,
3064 typename _Alloc = allocator<_CharT>>
3065 inline basic_istream<_CharT, _Traits>&
3066 from_stream(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt,
3067 file_time<_Duration>& __tp,
3068 basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr,
3069 minutes* __offset = nullptr)
3071 sys_time<_Duration> __st;
3072 if (chrono::from_stream(__is, __fmt, __st, __abbrev, __offset))
3073 __tp = __detail::__round<_Duration>(file_clock::from_sys(__st));
3074 return __is;
3077 template<typename _CharT, typename _Traits, typename _Duration>
3078 inline basic_ostream<_CharT, _Traits>&
3079 operator<<(basic_ostream<_CharT, _Traits>& __os,
3080 const local_time<_Duration>& __lt)
3082 __os << sys_time<_Duration>{__lt.time_since_epoch()};
3083 return __os;
3086 template<typename _CharT, typename _Traits, typename _Duration,
3087 typename _Alloc = allocator<_CharT>>
3088 basic_istream<_CharT, _Traits>&
3089 from_stream(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt,
3090 local_time<_Duration>& __tp,
3091 basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr,
3092 minutes* __offset = nullptr)
3094 using __format::_ChronoParts;
3095 auto __need = _ChronoParts::_Year | _ChronoParts::_Month
3096 | _ChronoParts::_Day | _ChronoParts::_TimeOfDay;
3097 __detail::_Parser_t<_Duration> __p(__need);
3098 if (__p(__is, __fmt, __abbrev, __offset))
3100 days __d = __p._M_sys_days.time_since_epoch();
3101 auto __t = local_days(__d) + __p._M_time; // ignore offset
3102 __tp = __detail::__round<_Duration>(__t);
3104 return __is;
3107 // [time.parse] parsing
3109 namespace __detail
3111 template<typename _Parsable, typename _CharT,
3112 typename _Traits = std::char_traits<_CharT>,
3113 typename... _OptArgs>
3114 concept __parsable = requires (basic_istream<_CharT, _Traits>& __is,
3115 const _CharT* __fmt, _Parsable& __tp,
3116 _OptArgs*... __args)
3117 { from_stream(__is, __fmt, __tp, __args...); };
3119 template<typename _Parsable, typename _CharT,
3120 typename _Traits = char_traits<_CharT>,
3121 typename _Alloc = allocator<_CharT>>
3122 struct _Parse
3124 private:
3125 using __string_type = basic_string<_CharT, _Traits, _Alloc>;
3127 public:
3128 _Parse(const _CharT* __fmt, _Parsable& __tp,
3129 basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr,
3130 minutes* __offset = nullptr)
3131 : _M_fmt(__fmt), _M_tp(std::__addressof(__tp)),
3132 _M_abbrev(__abbrev), _M_offset(__offset)
3135 _Parse(_Parse&&) = delete;
3136 _Parse& operator=(_Parse&&) = delete;
3138 private:
3139 using __stream_type = basic_istream<_CharT, _Traits>;
3141 const _CharT* const _M_fmt;
3142 _Parsable* const _M_tp;
3143 __string_type* const _M_abbrev;
3144 minutes* const _M_offset;
3146 friend __stream_type&
3147 operator>>(__stream_type& __is, _Parse&& __p)
3149 if (__p._M_offset)
3150 from_stream(__is, __p._M_fmt, *__p._M_tp, __p._M_abbrev,
3151 __p._M_offset);
3152 else if (__p._M_abbrev)
3153 from_stream(__is, __p._M_fmt, *__p._M_tp, __p._M_abbrev);
3154 else
3155 from_stream(__is, __p._M_fmt, *__p._M_tp);
3156 return __is;
3159 friend void operator>>(__stream_type&, _Parse&) = delete;
3160 friend void operator>>(__stream_type&, const _Parse&) = delete;
3162 } // namespace __detail
3164 template<typename _CharT, __detail::__parsable<_CharT> _Parsable>
3165 [[nodiscard, __gnu__::__access__(__read_only__, 1)]]
3166 inline auto
3167 parse(const _CharT* __fmt, _Parsable& __tp)
3168 { return __detail::_Parse<_Parsable, _CharT>(__fmt, __tp); }
3170 template<typename _CharT, typename _Traits, typename _Alloc,
3171 __detail::__parsable<_CharT, _Traits> _Parsable>
3172 [[nodiscard]]
3173 inline auto
3174 parse(const basic_string<_CharT, _Traits, _Alloc>& __fmt, _Parsable& __tp)
3176 return __detail::_Parse<_Parsable, _CharT, _Traits>(__fmt.c_str(), __tp);
3179 template<typename _CharT, typename _Traits, typename _Alloc,
3180 typename _StrT = basic_string<_CharT, _Traits, _Alloc>,
3181 __detail::__parsable<_CharT, _Traits, _StrT> _Parsable>
3182 [[nodiscard, __gnu__::__access__(__read_only__, 1)]]
3183 inline auto
3184 parse(const _CharT* __fmt, _Parsable& __tp,
3185 basic_string<_CharT, _Traits, _Alloc>& __abbrev)
3187 auto __pa = std::__addressof(__abbrev);
3188 return __detail::_Parse<_Parsable, _CharT, _Traits, _Alloc>(__fmt, __tp,
3189 __pa);
3192 template<typename _CharT, typename _Traits, typename _Alloc,
3193 typename _StrT = basic_string<_CharT, _Traits, _Alloc>,
3194 __detail::__parsable<_CharT, _Traits, _StrT> _Parsable>
3195 [[nodiscard]]
3196 inline auto
3197 parse(const basic_string<_CharT, _Traits, _Alloc>& __fmt, _Parsable& __tp,
3198 basic_string<_CharT, _Traits, _Alloc>& __abbrev)
3200 auto __pa = std::__addressof(__abbrev);
3201 return __detail::_Parse<_Parsable, _CharT, _Traits, _Alloc>(__fmt.c_str(),
3202 __tp, __pa);
3205 template<typename _CharT, typename _Traits = char_traits<_CharT>,
3206 typename _StrT = basic_string<_CharT, _Traits>,
3207 __detail::__parsable<_CharT, _Traits, _StrT, minutes> _Parsable>
3208 [[nodiscard, __gnu__::__access__(__read_only__, 1)]]
3209 inline auto
3210 parse(const _CharT* __fmt, _Parsable& __tp, minutes& __offset)
3212 return __detail::_Parse<_Parsable, _CharT>(__fmt, __tp, nullptr,
3213 &__offset);
3216 template<typename _CharT, typename _Traits, typename _Alloc,
3217 typename _StrT = basic_string<_CharT, _Traits>,
3218 __detail::__parsable<_CharT, _Traits, _StrT, minutes> _Parsable>
3219 [[nodiscard]]
3220 inline auto
3221 parse(const basic_string<_CharT, _Traits, _Alloc>& __fmt, _Parsable& __tp,
3222 minutes& __offset)
3224 return __detail::_Parse<_Parsable, _CharT, _Traits, _Alloc>(__fmt.c_str(),
3225 __tp, nullptr,
3226 &__offset);
3229 template<typename _CharT, typename _Traits, typename _Alloc,
3230 typename _StrT = basic_string<_CharT, _Traits, _Alloc>,
3231 __detail::__parsable<_CharT, _Traits, _StrT, minutes> _Parsable>
3232 [[nodiscard, __gnu__::__access__(__read_only__, 1)]]
3233 inline auto
3234 parse(const _CharT* __fmt, _Parsable& __tp,
3235 basic_string<_CharT, _Traits, _Alloc>& __abbrev, minutes& __offset)
3237 auto __pa = std::__addressof(__abbrev);
3238 return __detail::_Parse<_Parsable, _CharT, _Traits, _Alloc>(__fmt, __tp,
3239 __pa,
3240 &__offset);
3243 template<typename _CharT, typename _Traits, typename _Alloc,
3244 typename _StrT = basic_string<_CharT, _Traits, _Alloc>,
3245 __detail::__parsable<_CharT, _Traits, _StrT, minutes> _Parsable>
3246 [[nodiscard]]
3247 inline auto
3248 parse(const basic_string<_CharT, _Traits, _Alloc>& __fmt, _Parsable& __tp,
3249 basic_string<_CharT, _Traits, _Alloc>& __abbrev, minutes& __offset)
3251 auto __pa = std::__addressof(__abbrev);
3252 return __detail::_Parse<_Parsable, _CharT, _Traits, _Alloc>(__fmt.c_str(),
3253 __tp, __pa,
3254 &__offset);
3257 /// @cond undocumented
3258 template<typename _Duration>
3259 template<typename _CharT, typename _Traits, typename _Alloc>
3260 basic_istream<_CharT, _Traits>&
3261 __detail::_Parser<_Duration>::
3262 operator()(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt,
3263 basic_string<_CharT, _Traits, _Alloc>* __abbrev,
3264 minutes* __offset)
3266 using sentry = typename basic_istream<_CharT, _Traits>::sentry;
3267 ios_base::iostate __err = ios_base::goodbit;
3268 if (sentry __cerb(__is, true); __cerb)
3270 locale __loc = __is.getloc();
3271 auto& __tmget = std::use_facet<std::time_get<_CharT>>(__loc);
3272 auto& __tmpunct = std::use_facet<std::__timepunct<_CharT>>(__loc);
3274 // RAII type to save and restore stream state.
3275 struct _Stream_state
3277 explicit
3278 _Stream_state(basic_istream<_CharT, _Traits>& __i)
3279 : _M_is(__i),
3280 _M_flags(__i.flags(ios_base::skipws | ios_base::dec)),
3281 _M_w(__i.width(0))
3284 ~_Stream_state()
3286 _M_is.flags(_M_flags);
3287 _M_is.width(_M_w);
3290 _Stream_state(_Stream_state&&) = delete;
3292 basic_istream<_CharT, _Traits>& _M_is;
3293 ios_base::fmtflags _M_flags;
3294 streamsize _M_w;
3297 auto __is_failed = [](ios_base::iostate __e) {
3298 return static_cast<bool>(__e & ios_base::failbit);
3301 // Read an unsigned integer from the stream and return it.
3302 // Extract no more than __n digits. Set __err on error.
3303 auto __read_unsigned = [&] (int __n) {
3304 return _S_read_unsigned(__is, __err, __n);
3307 // Read a signed integer from the stream and return it.
3308 // Extract no more than __n digits. Set __err on error.
3309 auto __read_signed = [&] (int __n) {
3310 return _S_read_signed(__is, __err, __n);
3313 // Read an expected character from the stream.
3314 auto __read_chr = [&__is, &__err] (_CharT __c) {
3315 return _S_read_chr(__is, __err, __c);
3318 using __format::_ChronoParts;
3319 _ChronoParts __parts{};
3321 const year __bad_y = --year::min(); // SHRT_MIN
3322 const month __bad_mon(255);
3323 const day __bad_day(255);
3324 const weekday __bad_wday(255);
3325 const hours __bad_h(-1);
3326 const minutes __bad_min(-9999);
3327 const seconds __bad_sec(-1);
3329 year __y = __bad_y, __yy = __bad_y; // %Y, %yy
3330 year __iso_y = __bad_y, __iso_yy = __bad_y; // %G, %g
3331 month __m = __bad_mon; // %m
3332 day __d = __bad_day; // %d
3333 weekday __wday = __bad_wday; // %a %A %u %w
3334 hours __h = __bad_h, __h12 = __bad_h; // %H, %I
3335 minutes __min = __bad_min; // %M
3336 _Duration __s = __bad_sec; // %S
3337 int __ampm = 0; // %p
3338 int __iso_wk = -1, __sunday_wk = -1, __monday_wk = -1; // %V, %U, %W
3339 int __century = -1; // %C
3340 int __dayofyear = -1; // %j (for non-duration)
3342 minutes __tz_offset = __bad_min;
3343 basic_string<_CharT, _Traits> __tz_abbr;
3345 if ((_M_need & _ChronoParts::_TimeOfDay)
3346 && (_M_need & _ChronoParts::_Year))
3348 // For time_points assume "00:00:00" is implicitly present,
3349 // so we don't fail to parse if it's not (PR libstdc++/114240).
3350 // We will still fail to parse if there's no year+month+day.
3351 __h = hours(0);
3352 __parts = _ChronoParts::_TimeOfDay;
3355 // bool __is_neg = false; // TODO: how is this handled for parsing?
3357 _CharT __mod{}; // One of 'E' or 'O' or nul.
3358 unsigned __num = 0; // Non-zero for N modifier.
3359 bool __is_flag = false; // True if we're processing a % flag.
3361 constexpr bool __is_floating
3362 = treat_as_floating_point_v<typename _Duration::rep>;
3364 // If an out-of-range value is extracted (e.g. 61min for %M),
3365 // do not set failbit immediately because we might not need it
3366 // (e.g. parsing chrono::year doesn't care about invalid %M values).
3367 // Instead set the variable back to its initial 'bad' state,
3368 // and also set related variables corresponding to the same field
3369 // (e.g. a bad %M value for __min should also reset __h and __s).
3370 // If a valid value is needed later the bad value will cause failure.
3372 // For some fields we don't know the correct range when parsing and
3373 // we have to be liberal in what we accept, e.g. we allow 366 for
3374 // day-of-year because that's valid in leap years, and we allow 31
3375 // for day-of-month. If those values are needed to determine the
3376 // result then we can do a correct range check at the end when we
3377 // know the how many days the relevant year or month actually has.
3379 while (*__fmt)
3381 _CharT __c = *__fmt++;
3382 if (!__is_flag)
3384 if (__c == '%')
3385 __is_flag = true; // This is the start of a flag.
3386 else if (std::isspace(__c, __loc))
3387 std::ws(__is); // Match zero or more whitespace characters.
3388 else if (!__read_chr(__c)) [[unlikely]]
3389 break; // Failed to match the expected character.
3391 continue; // Process next character in the format string.
3394 // Now processing a flag.
3395 switch (__c)
3397 case 'a': // Locale's weekday name
3398 case 'A': // (full or abbreviated, matched case-insensitively).
3399 if (__mod || __num) [[unlikely]]
3400 __err = ios_base::failbit;
3401 else
3403 struct tm __tm{};
3404 __tmget.get(__is, {}, __is, __err, &__tm,
3405 __fmt - 2, __fmt);
3406 if (!__is_failed(__err))
3407 __wday = weekday(__tm.tm_wday);
3409 __parts |= _ChronoParts::_Weekday;
3410 break;
3412 case 'b': // Locale's month name
3413 case 'h': // (full or abbreviated, matched case-insensitively).
3414 case 'B':
3415 if (__mod || __num) [[unlikely]]
3416 __err = ios_base::failbit;
3417 else
3419 // strptime behaves differently for %b and %B,
3420 // but chrono::parse says they're equivalent.
3421 // Luckily libstdc++ std::time_get works as needed.
3422 struct tm __tm{};
3423 __tmget.get(__is, {}, __is, __err, &__tm,
3424 __fmt - 2, __fmt);
3425 if (!__is_failed(__err))
3426 __m = month(__tm.tm_mon + 1);
3428 __parts |= _ChronoParts::_Month;
3429 break;
3431 case 'c': // Locale's date and time representation.
3432 if (__mod == 'O' || __num) [[unlikely]]
3433 __err |= ios_base::failbit;
3434 else
3436 struct tm __tm{};
3437 __tmget.get(__is, {}, __is, __err, &__tm,
3438 __fmt - 2 - (__mod == 'E'), __fmt);
3439 if (!__is_failed(__err))
3441 __y = year(__tm.tm_year + 1900);
3442 __m = month(__tm.tm_mon + 1);
3443 __d = day(__tm.tm_mday);
3444 __h = hours(__tm.tm_hour);
3445 __min = minutes(__tm.tm_min);
3446 __s = seconds(__tm.tm_sec);
3449 __parts |= _ChronoParts::_DateTime;
3450 break;
3452 case 'C': // Century
3453 if (!__mod) [[likely]]
3455 auto __v = __read_signed(__num ? __num : 2);
3456 if (!__is_failed(__err))
3458 int __cmin = (int)year::min() / 100;
3459 int __cmax = (int)year::max() / 100;
3460 if (__cmin <= __v && __v <= __cmax)
3461 __century = __v * 100;
3462 else
3463 __century = -2; // This prevents guessing century.
3466 else if (__mod == 'E')
3468 struct tm __tm{};
3469 __tmget.get(__is, {}, __is, __err, &__tm,
3470 __fmt - 3, __fmt);
3471 if (!__is_failed(__err))
3472 __century = __tm.tm_year;
3474 else [[unlikely]]
3475 __err |= ios_base::failbit;
3476 // N.B. don't set this here: __parts |= _ChronoParts::_Year;
3477 break;
3479 case 'd': // Day of month (1-31)
3480 case 'e':
3481 if (!__mod) [[likely]]
3483 auto __v = __read_unsigned(__num ? __num : 2);
3484 if (!__is_failed(__err))
3485 __d = day(__v);
3487 else if (__mod == 'O')
3489 struct tm __tm{};
3490 __tmget.get(__is, {}, __is, __err, &__tm,
3491 __fmt - 3, __fmt);
3492 if (!__is_failed(__err))
3493 __d = day(__tm.tm_mday);
3495 else [[unlikely]]
3496 __err |= ios_base::failbit;
3497 __parts |= _ChronoParts::_Day;
3498 break;
3500 case 'D': // %m/%d/%y
3501 if (__mod || __num) [[unlikely]]
3502 __err |= ios_base::failbit;
3503 else
3505 auto __month = __read_unsigned(2); // %m
3506 __read_chr('/');
3507 auto __day = __read_unsigned(2); // %d
3508 __read_chr('/');
3509 auto __year = __read_unsigned(2); // %y
3510 if (__is_failed(__err))
3511 break;
3512 __y = year(__year + 1900 + 100 * int(__year < 69));
3513 __m = month(__month);
3514 __d = day(__day);
3515 if (!year_month_day(__y, __m, __d).ok())
3517 __y = __yy = __iso_y = __iso_yy = __bad_y;
3518 __m = __bad_mon;
3519 __d = __bad_day;
3520 break;
3523 __parts |= _ChronoParts::_Date;
3524 break;
3526 case 'F': // %Y-%m-%d - any N modifier only applies to %Y.
3527 if (__mod) [[unlikely]]
3528 __err |= ios_base::failbit;
3529 else
3531 auto __year = __read_signed(__num ? __num : 4); // %Y
3532 __read_chr('-');
3533 auto __month = __read_unsigned(2); // %m
3534 __read_chr('-');
3535 auto __day = __read_unsigned(2); // %d
3536 if (__is_failed(__err))
3537 break;
3538 __y = year(__year);
3539 __m = month(__month);
3540 __d = day(__day);
3541 if (!year_month_day(__y, __m, __d).ok())
3543 __y = __yy = __iso_y = __iso_yy = __bad_y;
3544 __m = __bad_mon;
3545 __d = __bad_day;
3546 break;
3549 __parts |= _ChronoParts::_Date;
3550 break;
3552 case 'g': // Last two digits of ISO week-based year.
3553 if (__mod) [[unlikely]]
3554 __err |= ios_base::failbit;
3555 else
3557 auto __val = __read_unsigned(__num ? __num : 2);
3558 if (__val >= 0 && __val <= 99)
3560 __iso_yy = year(__val);
3561 if (__century == -1) // No %C has been parsed yet.
3562 __century = 2000;
3564 else
3565 __iso_yy = __iso_y = __y = __yy = __bad_y;
3567 __parts |= _ChronoParts::_Year;
3568 break;
3570 case 'G': // ISO week-based year.
3571 if (__mod) [[unlikely]]
3572 __err |= ios_base::failbit;
3573 else
3574 __iso_y = year(__read_unsigned(__num ? __num : 4));
3575 __parts |= _ChronoParts::_Year;
3576 break;
3578 case 'H': // 24-hour (00-23)
3579 case 'I': // 12-hour (1-12)
3580 if (__mod == 'E') [[unlikely]]
3581 __err |= ios_base::failbit;
3582 else if (__mod == 'O')
3584 #if 0
3585 struct tm __tm{};
3586 __tm.tm_ampm = 1;
3587 __tmget.get(__is, {}, __is, __err, &__tm,
3588 __fmt - 3, __fmt);
3589 if (!__is_failed(__err))
3591 if (__c == 'I')
3593 __h12 = hours(__tm.tm_hour);
3594 __h = __bad_h;
3596 else
3597 __h = hours(__tm.tm_hour);
3599 #else
3600 // XXX %OI seems to be unimplementable.
3601 __err |= ios_base::failbit;
3602 #endif
3604 else
3606 auto __val = __read_unsigned(__num ? __num : 2);
3607 if (__c == 'I' && __val >= 1 && __val <= 12)
3609 __h12 = hours(__val);
3610 __h = __bad_h;
3612 else if (__c == 'H' && __val >= 0 && __val <= 23)
3614 __h = hours(__val);
3615 __h12 = __bad_h;
3617 else
3619 if (_M_need & _ChronoParts::_TimeOfDay)
3620 __err |= ios_base::failbit;
3621 break;
3624 __parts |= _ChronoParts::_TimeOfDay;
3625 break;
3627 case 'j': // For duration, count of days, otherwise day of year
3628 if (__mod) [[unlikely]]
3629 __err |= ios_base::failbit;
3630 else if (_M_need == _ChronoParts::_TimeOfDay) // duration
3632 auto __val = __read_signed(__num ? __num : 3);
3633 if (!__is_failed(__err))
3635 __h = days(__val); // __h will get added to _M_time
3636 __parts |= _ChronoParts::_TimeOfDay;
3639 else
3641 __dayofyear = __read_unsigned(__num ? __num : 3);
3642 // N.B. do not alter __parts here, done after loop.
3643 // No need for range checking here either.
3645 break;
3647 case 'm': // Month (1-12)
3648 if (__mod == 'E') [[unlikely]]
3649 __err |= ios_base::failbit;
3650 else if (__mod == 'O')
3652 struct tm __tm{};
3653 __tmget.get(__is, {}, __is, __err, &__tm,
3654 __fmt - 2, __fmt);
3655 if (!__is_failed(__err))
3656 __m = month(__tm.tm_mon + 1);
3658 else
3660 auto __val = __read_unsigned(__num ? __num : 2);
3661 if (__val >= 1 && __val <= 12)
3662 __m = month(__val);
3663 else
3664 __m = __bad_mon;
3666 __parts |= _ChronoParts::_Month;
3667 break;
3669 case 'M': // Minutes
3670 if (__mod == 'E') [[unlikely]]
3671 __err |= ios_base::failbit;
3672 else if (__mod == 'O')
3674 struct tm __tm{};
3675 __tmget.get(__is, {}, __is, __err, &__tm,
3676 __fmt - 2, __fmt);
3677 if (!__is_failed(__err))
3678 __min = minutes(__tm.tm_min);
3680 else
3682 auto __val = __read_unsigned(__num ? __num : 2);
3683 if (0 <= __val && __val < 60)
3684 __min = minutes(__val);
3685 else
3687 if (_M_need & _ChronoParts::_TimeOfDay)
3688 __err |= ios_base::failbit;
3689 break;
3692 __parts |= _ChronoParts::_TimeOfDay;
3693 break;
3695 case 'p': // Locale's AM/PM designation for 12-hour clock.
3696 if (__mod || __num)
3697 __err |= ios_base::failbit;
3698 else
3700 // Can't use std::time_get here as it can't parse %p
3701 // in isolation without %I. This might be faster anyway.
3702 const _CharT* __ampms[2];
3703 __tmpunct._M_am_pm(__ampms);
3704 int __n = 0, __which = 3;
3705 while (__which != 0)
3707 auto __i = __is.peek();
3708 if (_Traits::eq_int_type(__i, _Traits::eof()))
3710 __err |= ios_base::eofbit | ios_base::failbit;
3711 break;
3713 __i = std::toupper(_Traits::to_char_type(__i), __loc);
3714 if (__which & 1)
3716 if (__i != std::toupper(__ampms[0][__n], __loc))
3717 __which ^= 1;
3718 else if (__ampms[0][__n + 1] == _CharT())
3720 __which = 1;
3721 (void) __is.get();
3722 break;
3725 if (__which & 2)
3727 if (__i != std::toupper(__ampms[1][__n], __loc))
3728 __which ^= 2;
3729 else if (__ampms[1][__n + 1] == _CharT())
3731 __which = 2;
3732 (void) __is.get();
3733 break;
3736 if (__which)
3737 (void) __is.get();
3738 ++__n;
3740 if (__which == 0 || __which == 3)
3741 __err |= ios_base::failbit;
3742 else
3743 __ampm = __which;
3745 break;
3747 case 'r': // Locale's 12-hour time.
3748 if (__mod || __num)
3749 __err |= ios_base::failbit;
3750 else
3752 struct tm __tm{};
3753 __tmget.get(__is, {}, __is, __err, &__tm,
3754 __fmt - 2, __fmt);
3755 if (!__is_failed(__err))
3757 __h = hours(__tm.tm_hour);
3758 __min = minutes(__tm.tm_min);
3759 __s = seconds(__tm.tm_sec);
3762 __parts |= _ChronoParts::_TimeOfDay;
3763 break;
3765 case 'R': // %H:%M
3766 case 'T': // %H:%M:%S
3767 if (__mod || __num) [[unlikely]]
3769 __err |= ios_base::failbit;
3770 break;
3772 else
3774 auto __val = __read_unsigned(2);
3775 if (__val == -1 || __val > 23) [[unlikely]]
3777 if (_M_need & _ChronoParts::_TimeOfDay)
3778 __err |= ios_base::failbit;
3779 break;
3781 if (!__read_chr(':')) [[unlikely]]
3782 break;
3783 __h = hours(__val);
3785 __val = __read_unsigned(2);
3786 if (__val == -1 || __val > 60) [[unlikely]]
3788 if (_M_need & _ChronoParts::_TimeOfDay)
3789 __err |= ios_base::failbit;
3790 break;
3792 __min = minutes(__val);
3794 if (__c == 'R')
3796 __parts |= _ChronoParts::_TimeOfDay;
3797 break;
3799 else if (!__read_chr(':')) [[unlikely]]
3800 break;
3802 [[fallthrough]];
3804 case 'S': // Seconds
3805 if (__mod == 'E') [[unlikely]]
3806 __err |= ios_base::failbit;
3807 else if (__mod == 'O')
3809 struct tm __tm{};
3810 __tmget.get(__is, {}, __is, __err, &__tm,
3811 __fmt - 3, __fmt);
3812 if (!__is_failed(__err))
3813 __s = seconds(__tm.tm_sec);
3815 else if constexpr (_Duration::period::den == 1
3816 && !__is_floating)
3818 auto __val = __read_unsigned(__num ? __num : 2);
3819 if (0 <= __val && __val <= 59) [[likely]]
3820 __s = seconds(__val);
3821 else
3823 if (_M_need & _ChronoParts::_TimeOfDay)
3824 __err |= ios_base::failbit;
3825 break;
3828 else // Read fractional seconds
3830 basic_stringstream<_CharT> __buf;
3831 auto __digit = _S_try_read_digit(__is, __err);
3832 if (__digit != -1)
3834 __buf.put(_CharT('0') + __digit);
3835 __digit = _S_try_read_digit(__is, __err);
3836 if (__digit != -1)
3837 __buf.put(_CharT('0') + __digit);
3840 auto __i = __is.peek();
3841 if (_Traits::eq_int_type(__i, _Traits::eof()))
3842 __err |= ios_base::eofbit;
3843 else
3845 _CharT __dp = '.';
3846 if (__loc != locale::classic())
3848 auto& __np = use_facet<numpunct<_CharT>>(__loc);
3849 __dp = __np.decimal_point();
3851 _CharT __c = _Traits::to_char_type(__i);
3852 if (__c == __dp)
3854 (void) __is.get();
3855 __buf.put('.');
3856 int __prec
3857 = hh_mm_ss<_Duration>::fractional_width;
3860 __digit = _S_try_read_digit(__is, __err);
3861 if (__digit != -1)
3862 __buf.put(_CharT('0') + __digit);
3863 else
3864 break;
3866 while (--__prec);
3870 if (!__is_failed(__err)) [[likely]]
3872 long double __val{};
3873 #if __cpp_lib_to_chars
3874 string __str = std::move(__buf).str();
3875 auto __first = __str.data();
3876 auto __last = __first + __str.size();
3877 using enum chars_format;
3878 auto [ptr, ec] = std::from_chars(__first, __last,
3879 __val, fixed);
3880 if ((bool)ec || ptr != __last) [[unlikely]]
3881 __err |= ios_base::failbit;
3882 else
3883 #else
3884 if (__buf >> __val)
3885 #endif
3887 duration<long double> __fs(__val);
3888 if constexpr (__is_floating)
3889 __s = __fs;
3890 else
3891 __s = chrono::round<_Duration>(__fs);
3895 __parts |= _ChronoParts::_TimeOfDay;
3896 break;
3898 case 'u': // ISO weekday (1-7)
3899 case 'w': // Weekday (0-6)
3900 if (__mod == 'E') [[unlikely]]
3901 __err |= ios_base::failbit;
3902 else if (__mod == 'O')
3904 if (__c == 'w')
3906 struct tm __tm{};
3907 __tmget.get(__is, {}, __is, __err, &__tm,
3908 __fmt - 3, __fmt);
3909 if (!__is_failed(__err))
3910 __wday = weekday(__tm.tm_wday);
3912 else
3913 __err |= ios_base::failbit;
3915 else
3917 const int __lo = __c == 'u' ? 1 : 0;
3918 const int __hi = __lo + 6;
3919 auto __val = __read_unsigned(__num ? __num : 1);
3920 if (__lo <= __val && __val <= __hi)
3921 __wday = weekday(__val);
3922 else
3924 __wday = __bad_wday;
3925 break;
3928 __parts |= _ChronoParts::_Weekday;
3929 break;
3931 case 'U': // Week number of the year (from first Sunday).
3932 case 'V': // ISO week-based week number.
3933 case 'W': // Week number of the year (from first Monday).
3934 if (__mod == 'E') [[unlikely]]
3935 __err |= ios_base::failbit;
3936 else if (__mod == 'O')
3938 if (__c == 'V') [[unlikely]]
3939 __err |= ios_base::failbit;
3940 else
3942 // TODO nl_langinfo_l(ALT_DIGITS) ?
3943 // Not implementable using std::time_get.
3946 else
3948 const int __lo = __c == 'V' ? 1 : 0;
3949 const int __hi = 53;
3950 auto __val = __read_unsigned(__num ? __num : 2);
3951 if (__lo <= __val && __val <= __hi)
3953 switch (__c)
3955 case 'U':
3956 __sunday_wk = __val;
3957 break;
3958 case 'V':
3959 __iso_wk = __val;
3960 break;
3961 case 'W':
3962 __monday_wk = __val;
3963 break;
3966 else
3967 __iso_wk = __sunday_wk = __monday_wk = -1;
3969 // N.B. do not alter __parts here, done after loop.
3970 break;
3972 case 'x': // Locale's date representation.
3973 if (__mod == 'O' || __num) [[unlikely]]
3974 __err |= ios_base::failbit;
3975 else
3977 struct tm __tm{};
3978 __tmget.get(__is, {}, __is, __err, &__tm,
3979 __fmt - 2 - (__mod == 'E'), __fmt);
3980 if (!__is_failed(__err))
3982 __y = year(__tm.tm_year + 1900);
3983 __m = month(__tm.tm_mon + 1);
3984 __d = day(__tm.tm_mday);
3987 __parts |= _ChronoParts::_Date;
3988 break;
3990 case 'X': // Locale's time representation.
3991 if (__mod == 'O' || __num) [[unlikely]]
3992 __err |= ios_base::failbit;
3993 else
3995 struct tm __tm{};
3996 __tmget.get(__is, {}, __is, __err, &__tm,
3997 __fmt - 2 - (__mod == 'E'), __fmt);
3998 if (!__is_failed(__err))
4000 __h = hours(__tm.tm_hour);
4001 __min = minutes(__tm.tm_min);
4002 __s = seconds(__tm.tm_sec);
4005 __parts |= _ChronoParts::_TimeOfDay;
4006 break;
4008 case 'y': // Last two digits of year.
4009 if (__mod) [[unlikely]]
4011 struct tm __tm{};
4012 __tmget.get(__is, {}, __is, __err, &__tm,
4013 __fmt - 3, __fmt);
4014 if (!__is_failed(__err))
4016 int __cent = __tm.tm_year < 2000 ? 1900 : 2000;
4017 __yy = year(__tm.tm_year - __cent);
4018 if (__century == -1) // No %C has been parsed yet.
4019 __century = __cent;
4022 else
4024 auto __val = __read_unsigned(__num ? __num : 2);
4025 if (__val >= 0 && __val <= 99)
4027 __yy = year(__val);
4028 if (__century == -1) // No %C has been parsed yet.
4029 __century = __val < 69 ? 2000 : 1900;
4031 else
4032 __y = __yy = __iso_yy = __iso_y = __bad_y;
4034 __parts |= _ChronoParts::_Year;
4035 break;
4037 case 'Y': // Year
4038 if (__mod == 'O') [[unlikely]]
4039 __err |= ios_base::failbit;
4040 else if (__mod == 'E')
4042 struct tm __tm{};
4043 __tmget.get(__is, {}, __is, __err, &__tm,
4044 __fmt - 3, __fmt);
4045 if (!__is_failed(__err))
4046 __y = year(__tm.tm_year);
4048 else
4050 auto __val = __read_unsigned(__num ? __num : 4);
4051 if (!__is_failed(__err))
4052 __y = year(__val);
4054 __parts |= _ChronoParts::_Year;
4055 break;
4057 case 'z':
4058 if (__num) [[unlikely]]
4059 __err |= ios_base::failbit;
4060 else
4062 // For %Ez and %Oz read [+|-][h]h[:mm].
4063 // For %z read [+|-]hh[mm].
4065 auto __i = __is.peek();
4066 if (_Traits::eq_int_type(__i, _Traits::eof()))
4068 __err |= ios_base::eofbit | ios_base::failbit;
4069 break;
4071 _CharT __ic = _Traits::to_char_type(__i);
4072 const bool __neg = __ic == _CharT('-');
4073 if (__ic == _CharT('-') || __ic == _CharT('+'))
4074 (void) __is.get();
4076 int_least32_t __hh;
4077 if (__mod)
4079 // Read h[h]
4080 __hh = __read_unsigned(2);
4082 else
4084 // Read hh
4085 __hh = 10 * _S_try_read_digit(__is, __err);
4086 __hh += _S_try_read_digit(__is, __err);
4089 if (__is_failed(__err))
4090 break;
4092 __i = __is.peek();
4093 if (_Traits::eq_int_type(__i, _Traits::eof()))
4095 __err |= ios_base::eofbit;
4096 __tz_offset = minutes(__hh * (__neg ? -60 : 60));
4097 break;
4099 __ic = _Traits::to_char_type(__i);
4101 bool __read_mm = false;
4102 if (__mod)
4104 if (__ic == _GLIBCXX_WIDEN(":")[0])
4106 // Read [:mm] part.
4107 (void) __is.get();
4108 __read_mm = true;
4111 else if (_CharT('0') <= __ic && __ic <= _CharT('9'))
4113 // Read [mm] part.
4114 __read_mm = true;
4117 int_least32_t __mm = 0;
4118 if (__read_mm)
4120 __mm = 10 * _S_try_read_digit(__is, __err);
4121 __mm += _S_try_read_digit(__is, __err);
4124 if (!__is_failed(__err))
4126 auto __z = __hh * 60 + __mm;
4127 __tz_offset = minutes(__neg ? -__z : __z);
4130 break;
4132 case 'Z':
4133 if (__mod || __num) [[unlikely]]
4134 __err |= ios_base::failbit;
4135 else
4137 basic_string_view<_CharT> __x = _GLIBCXX_WIDEN("_/-+");
4138 __tz_abbr.clear();
4139 while (true)
4141 auto __i = __is.peek();
4142 if (!_Traits::eq_int_type(__i, _Traits::eof()))
4144 _CharT __a = _Traits::to_char_type(__i);
4145 if (std::isalnum(__a, __loc)
4146 || __x.find(__a) != __x.npos)
4148 __tz_abbr.push_back(__a);
4149 (void) __is.get();
4150 continue;
4153 else
4154 __err |= ios_base::eofbit;
4155 break;
4157 if (__tz_abbr.empty())
4158 __err |= ios_base::failbit;
4160 break;
4162 case 'n': // Exactly one whitespace character.
4163 if (__mod || __num) [[unlikely]]
4164 __err |= ios_base::failbit;
4165 else
4167 _CharT __i = __is.peek();
4168 if (_Traits::eq_int_type(__i, _Traits::eof()))
4169 __err |= ios_base::eofbit | ios_base::failbit;
4170 else if (std::isspace(_Traits::to_char_type(__i), __loc))
4171 (void) __is.get();
4172 else
4173 __err |= ios_base::failbit;
4175 break;
4177 case 't': // Zero or one whitespace characters.
4178 if (__mod || __num) [[unlikely]]
4179 __err |= ios_base::failbit;
4180 else
4182 _CharT __i = __is.peek();
4183 if (_Traits::eq_int_type(__i, _Traits::eof()))
4184 __err |= ios_base::eofbit;
4185 else if (std::isspace(_Traits::to_char_type(__i), __loc))
4186 (void) __is.get();
4188 break;
4190 case '%': // A % character.
4191 if (__mod || __num) [[unlikely]]
4192 __err |= ios_base::failbit;
4193 else
4194 __read_chr('%');
4195 break;
4197 case 'O': // Modifiers
4198 case 'E':
4199 if (__mod || __num) [[unlikely]]
4201 __err |= ios_base::failbit;
4202 break;
4204 __mod = __c;
4205 continue;
4207 default:
4208 if (_CharT('1') <= __c && __c <= _CharT('9'))
4210 if (!__mod) [[likely]]
4212 // %Nx - extract positive decimal integer N
4213 auto __end = __fmt + _Traits::length(__fmt);
4214 auto [__v, __ptr]
4215 = __format::__parse_integer(__fmt - 1, __end);
4216 if (__ptr) [[likely]]
4218 __num = __v;
4219 __fmt = __ptr;
4220 continue;
4224 __err |= ios_base::failbit;
4227 if (__is_failed(__err)) [[unlikely]]
4228 break;
4230 __is_flag = false;
4231 __num = 0;
4232 __mod = _CharT();
4235 if (__century >= 0)
4237 if (__yy != __bad_y && __y == __bad_y)
4238 __y = years(__century) + __yy; // Use %y instead of %Y
4239 if (__iso_yy != __bad_y && __iso_y == __bad_y)
4240 __iso_y = years(__century) + __iso_yy; // Use %g instead of %G
4243 bool __can_use_doy = false;
4244 bool __can_use_iso_wk = false;
4245 bool __can_use_sun_wk = false;
4246 bool __can_use_mon_wk = false;
4248 // A year + day-of-year can be converted to a full date.
4249 if (__y != __bad_y && __dayofyear >= 0)
4251 __can_use_doy = true;
4252 __parts |= _ChronoParts::_Date;
4254 else if (__y != __bad_y && __wday != __bad_wday && __sunday_wk >= 0)
4256 __can_use_sun_wk = true;
4257 __parts |= _ChronoParts::_Date;
4259 else if (__y != __bad_y && __wday != __bad_wday && __monday_wk >= 0)
4261 __can_use_mon_wk = true;
4262 __parts |= _ChronoParts::_Date;
4264 else if (__iso_y != __bad_y && __wday != __bad_wday && __iso_wk > 0)
4266 // An ISO week date can be converted to a full date.
4267 __can_use_iso_wk = true;
4268 __parts |= _ChronoParts::_Date;
4271 if (__is_failed(__err)) [[unlikely]]
4272 ; // Don't bother doing any more work.
4273 else if (__is_flag) [[unlikely]] // incomplete format flag
4274 __err |= ios_base::failbit;
4275 else if ((_M_need & __parts) == _M_need) [[likely]]
4277 // We try to avoid calculating _M_sys_days and _M_ymd unless
4278 // necessary, because converting sys_days to year_month_day
4279 // (or vice versa) requires non-trivial calculations.
4280 // If we have y/m/d values then use them to populate _M_ymd
4281 // and only convert it to _M_sys_days if the caller needs that.
4282 // But if we don't have y/m/d and need to calculate the date
4283 // from the day-of-year or a week+weekday then we set _M_sys_days
4284 // and only convert it to _M_ymd if the caller needs that.
4286 // We do more error checking here, but only for the fields that
4287 // we actually need to use. For example, we will not diagnose
4288 // an invalid dayofyear==366 for non-leap years unless actually
4289 // using __dayofyear. This should mean we never produce invalid
4290 // results, but it means not all invalid inputs are diagnosed,
4291 // e.g. "2023-01-01 366" >> "%F %j" ignores the invalid 366.
4292 // We also do not diagnose inconsistent values for the same
4293 // field, e.g. "2021 2022 2023" >> "%C%y %Y %Y" just uses 2023.
4295 // Whether the caller wants _M_wd.
4296 // The _Weekday bit is only set for chrono::weekday.
4297 const bool __need_wday = _M_need & _ChronoParts::_Weekday;
4299 // Whether the caller wants _M_sys_days and _M_time.
4300 // Only true for durations and time_points.
4301 const bool __need_time = _M_need & _ChronoParts::_TimeOfDay;
4303 if (__need_wday && __wday != __bad_wday)
4304 _M_wd = __wday; // Caller only wants a weekday and we have one.
4305 else if (_M_need & _ChronoParts::_Date) // subsumes __need_wday
4307 // Whether the caller wants _M_ymd.
4308 // True for chrono::year etc., false for time_points.
4309 const bool __need_ymd = !__need_wday && !__need_time;
4311 if ((_M_need & _ChronoParts::_Year && __y == __bad_y)
4312 || (_M_need & _ChronoParts::_Month && __m == __bad_mon)
4313 || (_M_need & _ChronoParts::_Day && __d == __bad_day))
4315 // Missing at least one of y/m/d so calculate sys_days
4316 // from the other data we have available.
4318 if (__can_use_doy)
4320 if ((0 < __dayofyear && __dayofyear <= 365)
4321 || (__dayofyear == 366 && __y.is_leap()))
4322 [[likely]]
4324 _M_sys_days = sys_days(__y/January/1)
4325 + days(__dayofyear - 1);
4326 if (__need_ymd)
4327 _M_ymd = year_month_day(_M_sys_days);
4329 else
4330 __err |= ios_base::failbit;
4332 else if (__can_use_iso_wk)
4334 // Calculate y/m/d from ISO week date.
4336 if (__iso_wk == 53)
4338 // A year has 53 weeks iff Jan 1st is a Thursday
4339 // or Jan 1 is a Wednesday and it's a leap year.
4340 const sys_days __jan4(__iso_y/January/4);
4341 weekday __wd1(__jan4 - days(3));
4342 if (__wd1 != Thursday)
4343 if (__wd1 != Wednesday || !__iso_y.is_leap())
4344 __err |= ios_base::failbit;
4347 if (!__is_failed(__err)) [[likely]]
4349 // First Thursday is always in week one:
4350 sys_days __w(Thursday[1]/January/__iso_y);
4351 // First day of week-based year:
4352 __w -= Thursday - Monday;
4353 __w += days(weeks(__iso_wk - 1));
4354 __w += __wday - Monday;
4355 _M_sys_days = __w;
4357 if (__need_ymd)
4358 _M_ymd = year_month_day(_M_sys_days);
4361 else if (__can_use_sun_wk)
4363 // Calculate y/m/d from week number + weekday.
4364 sys_days __wk1(__y/January/Sunday[1]);
4365 _M_sys_days = __wk1 + weeks(__sunday_wk - 1)
4366 + days(__wday.c_encoding());
4367 _M_ymd = year_month_day(_M_sys_days);
4368 if (_M_ymd.year() != __y) [[unlikely]]
4369 __err |= ios_base::failbit;
4371 else if (__can_use_mon_wk)
4373 // Calculate y/m/d from week number + weekday.
4374 sys_days __wk1(__y/January/Monday[1]);
4375 _M_sys_days = __wk1 + weeks(__monday_wk - 1)
4376 + days(__wday.c_encoding() - 1);
4377 _M_ymd = year_month_day(_M_sys_days);
4378 if (_M_ymd.year() != __y) [[unlikely]]
4379 __err |= ios_base::failbit;
4381 else // Should not be able to get here.
4382 __err |= ios_base::failbit;
4384 else
4386 // We know that all fields the caller needs are present,
4387 // but check that their values are in range.
4388 // Make unwanted fields valid so that _M_ymd.ok() is true.
4390 if (_M_need & _ChronoParts::_Year)
4392 if (!__y.ok()) [[unlikely]]
4393 __err |= ios_base::failbit;
4395 else if (__y == __bad_y)
4396 __y = 1972y; // Leap year so that Feb 29 is valid.
4398 if (_M_need & _ChronoParts::_Month)
4400 if (!__m.ok()) [[unlikely]]
4401 __err |= ios_base::failbit;
4403 else if (__m == __bad_mon)
4404 __m = January;
4406 if (_M_need & _ChronoParts::_Day)
4408 if (__d < day(1) || __d > (__y/__m/last).day())
4409 __err |= ios_base::failbit;
4411 else if (__d == __bad_day)
4412 __d = 1d;
4414 if (year_month_day __ymd(__y, __m, __d); __ymd.ok())
4416 _M_ymd = __ymd;
4417 if (__need_wday || __need_time)
4418 _M_sys_days = sys_days(_M_ymd);
4420 else [[unlikely]]
4421 __err |= ios_base::failbit;
4424 if (__need_wday)
4425 _M_wd = weekday(_M_sys_days);
4428 // Need to set _M_time for both durations and time_points.
4429 if (__need_time)
4431 if (__h == __bad_h && __h12 != __bad_h)
4433 if (__ampm == 1)
4434 __h = __h12 == hours(12) ? hours(0) : __h12;
4435 else if (__ampm == 2)
4436 __h = __h12 == hours(12) ? __h12 : __h12 + hours(12);
4437 else [[unlikely]]
4438 __err |= ios_base::failbit;
4441 auto __t = _M_time.zero();
4442 bool __ok = false;
4444 if (__h != __bad_h)
4446 __ok = true;
4447 __t += __h;
4450 if (__min != __bad_min)
4452 __ok = true;
4453 __t += __min;
4456 if (__s != __bad_sec)
4458 __ok = true;
4459 __t += __s;
4460 _M_is_leap_second = __s >= seconds(60);
4463 if (__ok)
4464 _M_time = __t;
4465 else
4466 __err |= ios_base::failbit;
4469 if (!__is_failed(__err)) [[likely]]
4471 if (__offset && __tz_offset != __bad_min)
4472 *__offset = __tz_offset;
4473 if (__abbrev && !__tz_abbr.empty())
4474 *__abbrev = std::move(__tz_abbr);
4477 else
4478 __err |= ios_base::failbit;
4480 if (__err)
4481 __is.setstate(__err);
4482 return __is;
4484 /// @endcond
4485 #undef _GLIBCXX_WIDEN
4487 /// @} group chrono
4488 } // namespace chrono
4490 _GLIBCXX_END_NAMESPACE_VERSION
4491 } // namespace std
4493 #endif // C++20
4495 #endif //_GLIBCXX_CHRONO_IO_H