libstdc++
chrono_io.h
Go to the documentation of this file.
1// <chrono> Formatting -*- C++ -*-
2
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.
10
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.
15
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.
19
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/>.
24
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}
28 */
29
30#ifndef _GLIBCXX_CHRONO_IO_H
31#define _GLIBCXX_CHRONO_IO_H 1
32
33#ifdef _GLIBCXX_SYSHDR
34#pragma GCC system_header
35#endif
36
37#if __cplusplus >= 202002L
38
39#include <sstream> // ostringstream
40#include <iomanip> // setw, setfill
41#include <format>
42#include <charconv> // from_chars
43#include <stdexcept> // __sso_string
44
46#include <bits/unique_ptr.h>
47
48namespace std _GLIBCXX_VISIBILITY(default)
49{
50_GLIBCXX_BEGIN_NAMESPACE_VERSION
51
52namespace chrono
53{
54/// @addtogroup chrono
55/// @{
56
57/// @cond undocumented
58namespace __detail
59{
60#define _GLIBCXX_WIDEN_(C, S) ::std::__format::_Widen<C>(S, L##S)
61#define _GLIBCXX_WIDEN(S) _GLIBCXX_WIDEN_(_CharT, S)
62
63 template<typename _Period, typename _CharT>
65 __units_suffix() noexcept
66 {
67 // The standard say these are all narrow strings, which would need to
68 // be widened at run-time when inserted into a wide stream. We use
69 // STATICALLY-WIDEN to widen at compile-time.
70#define _GLIBCXX_UNITS_SUFFIX(period, suffix) \
71 if constexpr (is_same_v<_Period, period>) \
72 return _GLIBCXX_WIDEN(suffix); \
73 else
74
80#if _GLIBCXX_USE_ALT_MICROSECONDS_SUFFIX
81 // Deciding this at compile-time is wrong, maybe use nl_langinfo(CODESET)
82 // to check runtime environment and return u8"\u00b5s", "\xb5s", or "us".
84#else
86#endif
102#undef _GLIBCXX_UNITS_SUFFIX
103 return {};
104 }
105
106 template<typename _Period, typename _CharT, typename _Out>
107 inline _Out
108 __fmt_units_suffix(_Out __out) noexcept
109 {
110 if (auto __s = __detail::__units_suffix<_Period, _CharT>(); __s.size())
111 return __format::__write(std::move(__out), __s);
112 else if constexpr (_Period::den == 1)
114 (uintmax_t)_Period::num);
115 else
116 return std::format_to(std::move(__out), _GLIBCXX_WIDEN("[{}/{}]s"),
117 (uintmax_t)_Period::num,
118 (uintmax_t)_Period::den);
119 }
120} // namespace __detail
121/// @endcond
122
123 /** Write a `chrono::duration` to an ostream.
124 *
125 * @since C++20
126 */
127 template<typename _CharT, typename _Traits,
128 typename _Rep, typename _Period>
131 const duration<_Rep, _Period>& __d)
132 {
134 using period = typename _Period::type;
136 __s.flags(__os.flags());
137 __s.imbue(__os.getloc());
138 __s.precision(__os.precision());
139 // _GLIBCXX_RESOLVE_LIB_DEFECTS
140 // 4118. How should duration formatters format custom rep types?
141 __s << +__d.count();
142 __detail::__fmt_units_suffix<period, _CharT>(_Out(__s));
143 __os << std::move(__s).str();
144 return __os;
145 }
146
147/// @cond undocumented
148namespace __detail
149{
150 // An unspecified type returned by `chrono::local_time_format`.
151 // This is called `local-time-format-t` in the standard.
152 template<typename _Duration>
153 struct __local_time_fmt
154 {
156 const string* _M_abbrev;
157 const seconds* _M_offset_sec;
158 };
159
160 // _GLIBCXX_RESOLVE_LIB_DEFECTS
161 // 4124. Cannot format zoned_time with resolution coarser than seconds
162 template<typename _Duration>
165}
166/// @endcond
167
168 /** Return an object that asssociates timezone info with a local time.
169 *
170 * A `chrono::local_time` object has no timezone associated with it. This
171 * function creates an object that allows formatting a `local_time` as
172 * though it refers to a timezone with the given abbreviated name and
173 * offset from UTC.
174 *
175 * @since C++20
176 */
177 template<typename _Duration>
178 inline __detail::__local_time_fmt<_Duration>
180 const string* __abbrev = nullptr,
181 const seconds* __offset_sec = nullptr)
182 { return {__time, __abbrev, __offset_sec}; }
183
184 /// @}
185} // namespace chrono
186
187/// @cond undocumented
188namespace __format
189{
190 [[noreturn,__gnu__::__always_inline__]]
191 inline void
192 __no_timezone_available()
193 { __throw_format_error("format error: no timezone available for %Z or %z"); }
194
195 [[noreturn,__gnu__::__always_inline__]]
196 inline void
197 __not_valid_for_duration()
198 { __throw_format_error("format error: chrono-format-spec not valid for "
199 "chrono::duration"); }
200
201 [[noreturn,__gnu__::__always_inline__]]
202 inline void
203 __invalid_chrono_spec()
204 { __throw_format_error("format error: chrono-format-spec not valid for "
205 "argument type"); }
206
207 template<typename _CharT>
208 struct _ChronoSpec : _Spec<_CharT>
209 {
210 basic_string_view<_CharT> _M_chrono_specs;
211
212 // Use one of the reserved bits in __format::_Spec<C>.
213 // This indicates that a locale-dependent conversion specifier such as
214 // %a is used in the chrono-specs. This is not the same as the
215 // _Spec<C>::_M_localized member which indicates that "L" was present
216 // in the format-spec, e.g. "{:L%a}" is localized and locale-specific,
217 // but "{:L}" is only localized and "{:%a}" is only locale-specific.
218 constexpr bool
219 _M_locale_specific() const noexcept
220 { return this->_M_reserved; }
221
222 constexpr void
223 _M_locale_specific(bool __b) noexcept
224 { this->_M_reserved = __b; }
225 };
226
227 // Represents the information provided by a chrono type.
228 // e.g. month_weekday has month and weekday but no year or time of day,
229 // hh_mm_ss has time of day but no date, sys_time is time_point+timezone.
230 enum _ChronoParts {
231 _Year = 1, _Month = 2, _Day = 4, _Weekday = 8, _TimeOfDay = 16,
232 _TimeZone = 32,
233 _Date = _Year | _Month | _Day | _Weekday,
234 _DateTime = _Date | _TimeOfDay,
235 _ZonedDateTime = _DateTime | _TimeZone,
236 _Duration = 128 // special case
237 };
238
239 constexpr _ChronoParts
240 operator|(_ChronoParts __x, _ChronoParts __y) noexcept
241 { return static_cast<_ChronoParts>((int)__x | (int)__y); }
242
243 constexpr _ChronoParts&
244 operator|=(_ChronoParts& __x, _ChronoParts __y) noexcept
245 { return __x = __x | __y; }
246
247 // TODO rename this to chrono::__formatter? or chrono::__detail::__formatter?
248 template<typename _CharT>
249 struct __formatter_chrono
250 {
251 using __string_view = basic_string_view<_CharT>;
252 using __string = basic_string<_CharT>;
253
254 template<typename _ParseContext>
255 constexpr typename _ParseContext::iterator
256 _M_parse(_ParseContext& __pc, _ChronoParts __parts)
257 {
258 auto __first = __pc.begin();
259 auto __last = __pc.end();
260
261 _ChronoSpec<_CharT> __spec{};
262
263 auto __finalize = [this, &__spec] {
264 _M_spec = __spec;
265 };
266
267 auto __finished = [&] {
268 if (__first == __last || *__first == '}')
269 {
270 __finalize();
271 return true;
272 }
273 return false;
274 };
275
276 if (__finished())
277 return __first;
278
279 __first = __spec._M_parse_fill_and_align(__first, __last);
280 if (__finished())
281 return __first;
282
283 __first = __spec._M_parse_width(__first, __last, __pc);
284 if (__finished())
285 return __first;
286
287 if (__parts & _ChronoParts::_Duration)
288 {
289 __first = __spec._M_parse_precision(__first, __last, __pc);
290 if (__finished())
291 return __first;
292 }
293
294 __first = __spec._M_parse_locale(__first, __last);
295 if (__finished())
296 return __first;
297
298 // Everything up to the end of the string or the first '}' is a
299 // chrono-specs string. Check it is valid.
300 {
301 __string_view __str(__first, __last - __first);
302 auto __end = __str.find('}');
303 if (__end != __str.npos)
304 {
305 __str.remove_suffix(__str.length() - __end);
306 __last = __first + __end;
307 }
308 if (__str.find('{') != __str.npos)
309 __throw_format_error("chrono format error: '{' in chrono-specs");
310 }
311
312 // Parse chrono-specs in [first,last), checking each conversion-spec
313 // against __parts (so fail for %Y if no year in parts).
314 // Save range in __spec._M_chrono_specs.
315
316 const auto __chrono_specs = __first++; // Skip leading '%'
317 if (*__chrono_specs != '%')
318 __throw_format_error("chrono format error: no '%' at start of "
319 "chrono-specs");
320
321 _CharT __mod{};
322 bool __conv = true;
323 int __needed = 0;
324 bool __locale_specific = false;
325
326 while (__first != __last)
327 {
328 enum _Mods { _Mod_none, _Mod_E, _Mod_O, _Mod_E_O };
329 _Mods __allowed_mods = _Mod_none;
330
331 _CharT __c = *__first++;
332 switch (__c)
333 {
334 case 'a':
335 case 'A':
336 __needed = _Weekday;
337 __locale_specific = true;
338 break;
339 case 'b':
340 case 'h':
341 case 'B':
342 __needed = _Month;
343 __locale_specific = true;
344 break;
345 case 'c':
346 __needed = _DateTime;
347 __allowed_mods = _Mod_E;
348 __locale_specific = true;
349 break;
350 case 'C':
351 __needed = _Year;
352 __allowed_mods = _Mod_E;
353 break;
354 case 'd':
355 case 'e':
356 __needed = _Day;
357 __allowed_mods = _Mod_O;
358 break;
359 case 'D':
360 case 'F':
361 __needed = _Date;
362 break;
363 case 'g':
364 case 'G':
365 __needed = _Date;
366 break;
367 case 'H':
368 case 'I':
369 __needed = _TimeOfDay;
370 __allowed_mods = _Mod_O;
371 break;
372 case 'j':
373 if (!(__parts & _Duration))
374 __needed = _Date;
375 break;
376 case 'm':
377 __needed = _Month;
378 __allowed_mods = _Mod_O;
379 break;
380 case 'M':
381 __needed = _TimeOfDay;
382 __allowed_mods = _Mod_O;
383 break;
384 case 'p':
385 case 'r':
386 __locale_specific = true;
387 [[fallthrough]];
388 case 'R':
389 case 'T':
390 __needed = _TimeOfDay;
391 break;
392 case 'q':
393 case 'Q':
394 __needed = _Duration;
395 break;
396 case 'S':
397 __needed = _TimeOfDay;
398 __allowed_mods = _Mod_O;
399 break;
400 case 'u':
401 case 'w':
402 __needed = _Weekday;
403 __allowed_mods = _Mod_O;
404 break;
405 case 'U':
406 case 'V':
407 case 'W':
408 __needed = _Date;
409 __allowed_mods = _Mod_O;
410 break;
411 case 'x':
412 __needed = _Date;
413 __locale_specific = true;
414 __allowed_mods = _Mod_E;
415 break;
416 case 'X':
417 __needed = _TimeOfDay;
418 __locale_specific = true;
419 __allowed_mods = _Mod_E;
420 break;
421 case 'y':
422 __needed = _Year;
423 __allowed_mods = _Mod_E_O;
424 break;
425 case 'Y':
426 __needed = _Year;
427 __allowed_mods = _Mod_E;
428 break;
429 case 'z':
430 __needed = _TimeZone;
431 __allowed_mods = _Mod_E_O;
432 break;
433 case 'Z':
434 __needed = _TimeZone;
435 break;
436 case 'n':
437 case 't':
438 case '%':
439 break;
440 case 'O':
441 case 'E':
442 if (__mod) [[unlikely]]
443 {
444 __allowed_mods = _Mod_none;
445 break;
446 }
447 __mod = __c;
448 continue;
449 default:
450 __throw_format_error("chrono format error: invalid "
451 " specifier in chrono-specs");
452 }
453
454 if ((__mod == 'E' && !(__allowed_mods & _Mod_E))
455 || (__mod == 'O' && !(__allowed_mods & _Mod_O)))
456 __throw_format_error("chrono format error: invalid "
457 " modifier in chrono-specs");
458 if (__mod && __c != 'z')
459 __locale_specific = true;
460 __mod = _CharT();
461
462 if ((__parts & __needed) != __needed)
463 __throw_format_error("chrono format error: format argument "
464 "does not contain the information "
465 "required by the chrono-specs");
466
467 // Scan for next '%', ignoring literal-chars before it.
468 size_t __pos = __string_view(__first, __last - __first).find('%');
469 if (__pos == 0)
470 ++__first;
471 else
472 {
473 if (__pos == __string_view::npos)
474 {
475 __first = __last;
476 __conv = false;
477 }
478 else
479 __first += __pos + 1;
480 }
481 }
482
483 // Check for a '%' conversion-spec without a type.
484 if (__conv || __mod != _CharT())
485 __throw_format_error("chrono format error: unescaped '%' in "
486 "chrono-specs");
487
488 _M_spec = __spec;
489 _M_spec._M_chrono_specs
490 = __string_view(__chrono_specs, __first - __chrono_specs);
491 _M_spec._M_locale_specific(__locale_specific);
492
493 return __first;
494 }
495
496 // TODO this function template is instantiated for every different _Tp.
497 // Consider creating a polymorphic interface for calendar types so
498 // that we instantiate fewer different specializations. Similar to
499 // _Sink_iter for std::format. Replace each _S_year, _S_day etc. with
500 // member functions of that type.
501 template<typename _Tp, typename _FormatContext>
502 typename _FormatContext::iterator
503 _M_format(const _Tp& __t, _FormatContext& __fc,
504 bool __is_neg = false) const
505 {
506 auto __first = _M_spec._M_chrono_specs.begin();
507 const auto __last = _M_spec._M_chrono_specs.end();
508 if (__first == __last)
509 return _M_format_to_ostream(__t, __fc, __is_neg);
510
511#if defined _GLIBCXX_USE_NL_LANGINFO_L && __CHAR_BIT__ == 8
512 // _GLIBCXX_RESOLVE_LIB_DEFECTS
513 // 3565. Handling of encodings in localized formatting
514 // of chrono types is underspecified
515 if constexpr (is_same_v<_CharT, char>)
516 if constexpr (__unicode::__literal_encoding_is_utf8())
517 if (_M_spec._M_localized && _M_spec._M_locale_specific())
518 {
519 extern locale __with_encoding_conversion(const locale&);
520
521 // Allocate and cache the necessary state to convert strings
522 // in the locale's encoding to UTF-8.
523 locale __loc = __fc.locale();
524 if (__loc != locale::classic())
525 __fc._M_loc = __with_encoding_conversion(__loc);
526 }
527#endif
528
529 _Sink_iter<_CharT> __out;
530 __format::_Str_sink<_CharT> __sink;
531 bool __write_direct = false;
532 if constexpr (is_same_v<typename _FormatContext::iterator,
533 _Sink_iter<_CharT>>)
534 {
535 if (_M_spec._M_width_kind == __format::_WP_none)
536 {
537 __out = __fc.out();
538 __write_direct = true;
539 }
540 else
541 __out = __sink.out();
542 }
543 else
544 __out = __sink.out();
545
546 // formatter<duration> passes the correct value of __is_neg
547 // for durations but for hh_mm_ss we decide it here.
548 if constexpr (__is_specialization_of<_Tp, chrono::hh_mm_ss>)
549 __is_neg = __t.is_negative();
550
551 auto __print_sign = [&__is_neg, &__out] {
552 if constexpr (chrono::__is_duration_v<_Tp>
553 || __is_specialization_of<_Tp, chrono::hh_mm_ss>)
554 if (__is_neg)
555 {
556 *__out++ = _S_plus_minus[1];
557 __is_neg = false;
558 }
559 return std::move(__out);
560 };
561
562 // Characters to output for "%n", "%t" and "%%" specifiers.
563 constexpr const _CharT* __literals = _GLIBCXX_WIDEN("\n\t%");
564
565 ++__first; // Skip leading '%' at start of chrono-specs.
566
567 _CharT __mod{};
568 do
569 {
570 _CharT __c = *__first++;
571 switch (__c)
572 {
573 case 'a':
574 case 'A':
575 __out = _M_a_A(__t, std::move(__out), __fc, __c == 'A');
576 break;
577 case 'b':
578 case 'h':
579 case 'B':
580 __out = _M_b_B(__t, std::move(__out), __fc, __c == 'B');
581 break;
582 case 'c':
583 __out = _M_c(__t, std::move(__out), __fc, __mod == 'E');
584 break;
585 case 'C':
586 case 'y':
587 case 'Y':
588 __out = _M_C_y_Y(__t, std::move(__out), __fc, __c, __mod);
589 break;
590 case 'd':
591 case 'e':
592 __out = _M_d_e(__t, std::move(__out), __fc, __c, __mod == 'O');
593 break;
594 case 'D':
595 __out = _M_D(__t, std::move(__out), __fc);
596 break;
597 case 'F':
598 __out = _M_F(__t, std::move(__out), __fc);
599 break;
600 case 'g':
601 case 'G':
602 __out = _M_g_G(__t, std::move(__out), __fc, __c == 'G');
603 break;
604 case 'H':
605 case 'I':
606 __out = _M_H_I(__t, __print_sign(), __fc, __c, __mod == 'O');
607 break;
608 case 'j':
609 __out = _M_j(__t, __print_sign(), __fc);
610 break;
611 case 'm':
612 __out = _M_m(__t, std::move(__out), __fc, __mod == 'O');
613 break;
614 case 'M':
615 __out = _M_M(__t, __print_sign(), __fc, __mod == 'O');
616 break;
617 case 'p':
618 __out = _M_p(__t, std::move(__out), __fc);
619 break;
620 case 'q':
621 __out = _M_q(__t, std::move(__out), __fc);
622 break;
623 case 'Q':
624 // %Q The duration's numeric value.
625 if constexpr (chrono::__is_duration_v<_Tp>)
626 // _GLIBCXX_RESOLVE_LIB_DEFECTS
627 // 4118. How should duration formatters format custom rep?
628 __out = std::format_to(__print_sign(), _S_empty_spec,
629 +__t.count());
630 else
631 __throw_format_error("chrono format error: argument is "
632 "not a duration");
633 break;
634 case 'r':
635 __out = _M_r(__t, __print_sign(), __fc);
636 break;
637 case 'R':
638 case 'T':
639 __out = _M_R_T(__t, __print_sign(), __fc, __c == 'T');
640 break;
641 case 'S':
642 __out = _M_S(__t, __print_sign(), __fc, __mod == 'O');
643 break;
644 case 'u':
645 case 'w':
646 __out = _M_u_w(__t, std::move(__out), __fc, __c, __mod == 'O');
647 break;
648 case 'U':
649 case 'V':
650 case 'W':
651 __out = _M_U_V_W(__t, std::move(__out), __fc, __c,
652 __mod == 'O');
653 break;
654 case 'x':
655 __out = _M_x(__t, std::move(__out), __fc, __mod == 'E');
656 break;
657 case 'X':
658 __out = _M_X(__t, __print_sign(), __fc, __mod == 'E');
659 break;
660 case 'z':
661 __out = _M_z(__t, std::move(__out), __fc, (bool)__mod);
662 break;
663 case 'Z':
664 __out = _M_Z(__t, std::move(__out), __fc);
665 break;
666 case 'n':
667 *__out++ = __literals[0];
668 break;
669 case 't':
670 *__out++ = __literals[1];
671 break;
672 case '%':
673 *__out++ = __literals[2];
674 break;
675 case 'O':
676 case 'E':
677 __mod = __c;
678 continue;
679 case '}':
680 __first = __last;
681 break;
682 }
683 __mod = _CharT();
684 // Scan for next '%' and write out everything before it.
685 __string_view __str(__first, __last - __first);
686 size_t __pos = __str.find('%');
687 if (__pos == 0)
688 ++__first;
689 else
690 {
691 if (__pos == __str.npos)
692 __first = __last;
693 else
694 {
695 __str.remove_suffix(__str.length() - __pos);
696 __first += __pos + 1;
697 }
698 __out = __format::__write(std::move(__out), __str);
699 }
700 }
701 while (__first != __last);
702
703 if constexpr (is_same_v<typename _FormatContext::iterator,
704 _Sink_iter<_CharT>>)
705 if (__write_direct)
706 return __out;
707
708 auto __span = __sink.view();
709 __string_view __str(__span.data(), __span.size());
710 size_t __width;
711 if constexpr (__unicode::__literal_encoding_is_unicode<_CharT>())
712 __width = __unicode::__field_width(__str);
713 else
714 __width = __str.size();
715 return __format::__write_padded_as_spec(__str, __width,
716 __fc, _M_spec);
717 }
718
719 _ChronoSpec<_CharT> _M_spec;
720
721 private:
722 // Return the formatting locale.
723 template<typename _FormatContext>
725 _M_locale(_FormatContext& __fc) const
726 {
727 if (!_M_spec._M_localized)
728 return std::locale::classic();
729 else
730 return __fc.locale();
731 }
732
733 // Format for empty chrono-specs, e.g. "{}" (C++20 [time.format] p6).
734 // TODO: consider moving body of every operator<< into this function
735 // and use std::format("{}", t) to implement those operators. That
736 // would avoid std::format("{}", t) calling operator<< which calls
737 // std::format again.
738 template<typename _Tp, typename _FormatContext>
739 typename _FormatContext::iterator
740 _M_format_to_ostream(const _Tp& __t, _FormatContext& __fc,
741 bool __is_neg) const
742 {
743 using ::std::chrono::__detail::__utc_leap_second;
744 using ::std::chrono::__detail::__local_time_fmt;
745
746 basic_ostringstream<_CharT> __os;
747 __os.imbue(_M_locale(__fc));
748
749 if constexpr (__is_specialization_of<_Tp, __local_time_fmt>)
750 {
751 // Format as "{:L%F %T}"
752 auto __days = chrono::floor<chrono::days>(__t._M_time);
753 __os << chrono::year_month_day(__days) << ' '
754 << chrono::hh_mm_ss(__t._M_time - __days);
755
756 // For __local_time_fmt the __is_neg flags says whether to
757 // append " %Z" to the result.
758 if (__is_neg)
759 {
760 if (!__t._M_abbrev) [[unlikely]]
761 __format::__no_timezone_available();
762 else if constexpr (is_same_v<_CharT, char>)
763 __os << ' ' << *__t._M_abbrev;
764 else
765 {
766 __os << L' ';
767 for (char __c : *__t._M_abbrev)
768 __os << __c;
769 }
770 }
771 }
772 else
773 {
774 if constexpr (__is_specialization_of<_Tp, __utc_leap_second>)
775 __os << __t._M_date << ' ' << __t._M_time;
776 else if constexpr (chrono::__is_time_point_v<_Tp>)
777 {
778 // Need to be careful here because not all specializations
779 // of chrono::sys_time can be written to an ostream.
780 // For the specializations of time_point that can be
781 // formatted with an empty chrono-specs, either it's a
782 // sys_time with period greater or equal to days:
783 if constexpr (is_convertible_v<_Tp, chrono::sys_days>)
784 __os << _S_date(__t);
785 else // Or it's formatted as "{:L%F %T}":
786 {
787 auto __days = chrono::floor<chrono::days>(__t);
788 __os << chrono::year_month_day(__days) << ' '
789 << chrono::hh_mm_ss(__t - __days);
790 }
791 }
792 else
793 {
794 if constexpr (chrono::__is_duration_v<_Tp>)
795 if (__is_neg) [[unlikely]]
796 __os << _S_plus_minus[1];
797 __os << __t;
798 }
799 }
800
801 auto __str = std::move(__os).str();
802 return __format::__write_padded_as_spec(__str, __str.size(),
803 __fc, _M_spec);
804 }
805
806 static constexpr const _CharT* _S_chars
807 = _GLIBCXX_WIDEN("0123456789+-:/ {}");
808 static constexpr const _CharT* _S_plus_minus = _S_chars + 10;
809 static constexpr _CharT _S_colon = _S_chars[12];
810 static constexpr _CharT _S_slash = _S_chars[13];
811 static constexpr _CharT _S_space = _S_chars[14];
812 static constexpr const _CharT* _S_empty_spec = _S_chars + 15;
813
814 template<typename _OutIter>
815 _OutIter
816 _M_write(_OutIter __out, const locale& __loc, __string_view __s) const
817 {
818#if defined _GLIBCXX_USE_NL_LANGINFO_L && __CHAR_BIT__ == 8
819 __sso_string __buf;
820 // _GLIBCXX_RESOLVE_LIB_DEFECTS
821 // 3565. Handling of encodings in localized formatting
822 // of chrono types is underspecified
823 if constexpr (is_same_v<_CharT, char>)
824 if constexpr (__unicode::__literal_encoding_is_utf8())
825 if (_M_spec._M_localized && _M_spec._M_locale_specific()
826 && __loc != locale::classic())
827 {
828 extern string_view
829 __locale_encoding_to_utf8(const locale&, string_view, void*);
830
831 __s = __locale_encoding_to_utf8(__loc, __s, &__buf);
832 }
833#endif
834 return __format::__write(std::move(__out), __s);
835 }
836
837 template<typename _Tp, typename _FormatContext>
838 typename _FormatContext::iterator
839 _M_a_A(const _Tp& __t, typename _FormatContext::iterator __out,
840 _FormatContext& __ctx, bool __full) const
841 {
842 // %a Locale's abbreviated weekday name.
843 // %A Locale's full weekday name.
844 chrono::weekday __wd = _S_weekday(__t);
845 if (!__wd.ok())
846 __throw_format_error("format error: invalid weekday");
847
848 locale __loc = _M_locale(__ctx);
849 const auto& __tp = use_facet<__timepunct<_CharT>>(__loc);
850 const _CharT* __days[7];
851 if (__full)
852 __tp._M_days(__days);
853 else
854 __tp._M_days_abbreviated(__days);
855 __string_view __str(__days[__wd.c_encoding()]);
856 return _M_write(std::move(__out), __loc, __str);
857 }
858
859 template<typename _Tp, typename _FormatContext>
860 typename _FormatContext::iterator
861 _M_b_B(const _Tp& __t, typename _FormatContext::iterator __out,
862 _FormatContext& __ctx, bool __full) const
863 {
864 // %b Locale's abbreviated month name.
865 // %B Locale's full month name.
866 chrono::month __m = _S_month(__t);
867 if (!__m.ok())
868 __throw_format_error("format error: invalid month");
869 locale __loc = _M_locale(__ctx);
870 const auto& __tp = use_facet<__timepunct<_CharT>>(__loc);
871 const _CharT* __months[12];
872 if (__full)
873 __tp._M_months(__months);
874 else
875 __tp._M_months_abbreviated(__months);
876 __string_view __str(__months[(unsigned)__m - 1]);
877 return _M_write(std::move(__out), __loc, __str);
878 }
879
880 template<typename _Tp, typename _FormatContext>
881 typename _FormatContext::iterator
882 _M_c(const _Tp& __t, typename _FormatContext::iterator __out,
883 _FormatContext& __ctx, bool __mod = false) const
884 {
885 // %c Locale's date and time representation.
886 // %Ec Locale's alternate date and time representation.
887
888 using namespace chrono;
889 using ::std::chrono::__detail::__utc_leap_second;
890 using ::std::chrono::__detail::__local_time_fmt;
891
892 struct tm __tm{};
893
894 // Some locales use %Z in their %c format but we don't want strftime
895 // to use the system's local time zone (from /etc/localtime or $TZ)
896 // as the output for %Z. Setting tm_isdst to -1 says there is no
897 // time zone info available for the time in __tm.
898 __tm.tm_isdst = -1;
899
900#ifdef _GLIBCXX_USE_STRUCT_TM_TM_ZONE
901 // POSIX.1-2024 adds tm.tm_zone which will be used for %Z.
902 // BSD has had tm_zone since 1987 but as char* so cast away const.
903 if constexpr (__is_time_point_v<_Tp>)
904 {
905 // One of sys_time, utc_time, or local_time.
906 if constexpr (!is_same_v<typename _Tp::clock, local_t>)
907 __tm.tm_zone = const_cast<char*>("UTC");
908 }
909 else if constexpr (__is_specialization_of<_Tp, __local_time_fmt>)
910 {
911 // local-time-format-t is used to provide time zone info for
912 // one of zoned_time, tai_time, gps_time, or local_time.
913 if (__t._M_abbrev)
914 __tm.tm_zone = const_cast<char*>(__t._M_abbrev->c_str());
915 }
916 else
917 __tm.tm_zone = const_cast<char*>("UTC");
918#endif
919
920 auto __d = _S_days(__t); // Either sys_days or local_days.
921 using _TDays = decltype(__d);
922 const year_month_day __ymd(__d);
923 const auto __y = __ymd.year();
924 const auto __hms = _S_hms(__t);
925
926 __tm.tm_year = (int)__y - 1900;
927 __tm.tm_yday = (__d - _TDays(__y/January/1)).count();
928 __tm.tm_mon = (unsigned)__ymd.month() - 1;
929 __tm.tm_mday = (unsigned)__ymd.day();
930 __tm.tm_wday = weekday(__d).c_encoding();
931 __tm.tm_hour = __hms.hours().count();
932 __tm.tm_min = __hms.minutes().count();
933 __tm.tm_sec = __hms.seconds().count();
934
935 return _M_locale_fmt(std::move(__out), _M_locale(__ctx), __tm, 'c',
936 __mod ? 'E' : '\0');
937 }
938
939 template<typename _Tp, typename _FormatContext>
940 typename _FormatContext::iterator
941 _M_C_y_Y(const _Tp& __t, typename _FormatContext::iterator __out,
942 _FormatContext& __ctx, _CharT __conv, _CharT __mod = 0) const
943 {
944 // %C Year divided by 100 using floored division.
945 // %EC Locale's alternative preresentation of the century (era name).
946 // %y Last two decimal digits of the year.
947 // %Oy Locale's alternative representation.
948 // %Ey Locale's alternative representation of offset from %EC.
949 // %Y Year as a decimal number.
950 // %EY Locale's alternative full year representation.
951
952 chrono::year __y = _S_year(__t);
953
954 if (__mod && _M_spec._M_localized) [[unlikely]]
955 if (auto __loc = __ctx.locale(); __loc != locale::classic())
956 {
957 struct tm __tm{};
958 __tm.tm_year = (int)__y - 1900;
959 return _M_locale_fmt(std::move(__out), __loc, __tm,
960 __conv, __mod);
961 }
962
963 basic_string<_CharT> __s;
964 int __yi = (int)__y;
965 const bool __is_neg = __yi < 0;
966 __yi = __builtin_abs(__yi);
967
968 if (__conv == 'Y' || __conv == 'C')
969 {
970 int __ci = __yi / 100;
971 if (__is_neg) [[unlikely]]
972 {
973 __s.assign(1, _S_plus_minus[1]);
974 // For floored division -123//100 is -2 and -100//100 is -1
975 if (__conv == 'C' && (__ci * 100) != __yi)
976 ++__ci;
977 }
978 if (__ci >= 100) [[unlikely]]
979 {
980 __s += std::format(_S_empty_spec, __ci / 100);
981 __ci %= 100;
982 }
983 __s += _S_two_digits(__ci);
984 }
985
986 if (__conv == 'Y' || __conv == 'y')
987 __s += _S_two_digits(__yi % 100);
988
989 return __format::__write(std::move(__out), __string_view(__s));
990 }
991
992 template<typename _Tp, typename _FormatContext>
993 typename _FormatContext::iterator
994 _M_D(const _Tp& __t, typename _FormatContext::iterator __out,
995 _FormatContext&) const
996 {
997 auto __ymd = _S_date(__t);
998 basic_string<_CharT> __s;
999#if ! _GLIBCXX_USE_CXX11_ABI
1000 __s.reserve(8);
1001#endif
1002 __s = _S_two_digits((unsigned)__ymd.month());
1003 __s += _S_slash;
1004 __s += _S_two_digits((unsigned)__ymd.day());
1005 __s += _S_slash;
1006 __s += _S_two_digits(__builtin_abs((int)__ymd.year()) % 100);
1007 return __format::__write(std::move(__out), __string_view(__s));
1008 }
1009
1010 template<typename _Tp, typename _FormatContext>
1011 typename _FormatContext::iterator
1012 _M_d_e(const _Tp& __t, typename _FormatContext::iterator __out,
1013 _FormatContext& __ctx, _CharT __conv, bool __mod = false) const
1014 {
1015 // %d The day of month as a decimal number.
1016 // %Od Locale's alternative representation.
1017 // %e Day of month as decimal number, padded with space.
1018 // %Oe Locale's alternative digits.
1019
1020 chrono::day __d = _S_day(__t);
1021 unsigned __i = (unsigned)__d;
1022
1023 if (__mod && _M_spec._M_localized) [[unlikely]]
1024 if (auto __loc = __ctx.locale(); __loc != locale::classic())
1025 {
1026 struct tm __tm{};
1027 __tm.tm_mday = __i;
1028 return _M_locale_fmt(std::move(__out), __loc, __tm,
1029 (char)__conv, 'O');
1030 }
1031
1032 auto __sv = _S_two_digits(__i);
1033 _CharT __buf[2];
1034 if (__conv == _CharT('e') && __i < 10)
1035 {
1036 __buf[0] = _S_space;
1037 __buf[1] = __sv[1];
1038 __sv = {__buf, 2};
1039 }
1040 return __format::__write(std::move(__out), __sv);
1041 }
1042
1043 template<typename _Tp, typename _FormatContext>
1044 typename _FormatContext::iterator
1045 _M_F(const _Tp& __t, typename _FormatContext::iterator __out,
1046 _FormatContext&) const
1047 {
1048 auto __ymd = _S_date(__t);
1049 auto __s = std::format(_GLIBCXX_WIDEN("{:04d}- - "),
1050 (int)__ymd.year());
1051 auto __sv = _S_two_digits((unsigned)__ymd.month());
1052 __s[__s.size() - 5] = __sv[0];
1053 __s[__s.size() - 4] = __sv[1];
1054 __sv = _S_two_digits((unsigned)__ymd.day());
1055 __s[__s.size() - 2] = __sv[0];
1056 __s[__s.size() - 1] = __sv[1];
1057 __sv = __s;
1058 return __format::__write(std::move(__out), __sv);
1059 }
1060
1061 template<typename _Tp, typename _FormatContext>
1062 typename _FormatContext::iterator
1063 _M_g_G(const _Tp& __t, typename _FormatContext::iterator __out,
1064 _FormatContext& __ctx, bool __full) const
1065 {
1066 // %g last two decimal digits of the ISO week-based year.
1067 // %G ISO week-based year.
1068 using namespace chrono;
1069 auto __d = _S_days(__t);
1070 // Move to nearest Thursday:
1071 __d -= (weekday(__d) - Monday) - days(3);
1072 // ISO week-based year is the year that contains that Thursday:
1073 year __y = year_month_day(__d).year();
1074 return _M_C_y_Y(__y, std::move(__out), __ctx, "yY"[__full]);
1075 }
1076
1077 template<typename _Tp, typename _FormatContext>
1078 typename _FormatContext::iterator
1079 _M_H_I(const _Tp& __t, typename _FormatContext::iterator __out,
1080 _FormatContext& __ctx, _CharT __conv, bool __mod = false) const
1081 {
1082 // %H The hour (24-hour clock) as a decimal number.
1083 // %OH Locale's alternative representation.
1084 // %I The hour (12-hour clock) as a decimal number.
1085 // %OI Locale's alternative representation.
1086
1087 const auto __hms = _S_hms(__t);
1088 int __i = __hms.hours().count();
1089
1090 if (__mod && _M_spec._M_localized) [[unlikely]]
1091 if (auto __loc = __ctx.locale(); __loc != locale::classic())
1092 {
1093 struct tm __tm{};
1094 __tm.tm_hour = __i;
1095 return _M_locale_fmt(std::move(__out), __loc, __tm,
1096 (char)__conv, 'O');
1097 }
1098
1099 if (__conv == _CharT('I'))
1100 {
1101 if (__i == 0)
1102 __i = 12;
1103 else if (__i > 12)
1104 __i -= 12;
1105 }
1106 return __format::__write(std::move(__out), _S_two_digits(__i));
1107 }
1108
1109 template<typename _Tp, typename _FormatContext>
1110 typename _FormatContext::iterator
1111 _M_j(const _Tp& __t, typename _FormatContext::iterator __out,
1112 _FormatContext&) const
1113 {
1114 if constexpr (chrono::__is_duration_v<_Tp>)
1115 {
1116 // Decimal number of days, without padding.
1117 unsigned __d = chrono::duration_cast<chrono::days>(__t).count();
1118 return std::format_to(std::move(__out), _S_empty_spec, __d);
1119 }
1120 else
1121 {
1122 // Day of the year as a decimal number, padding with zero.
1123 using namespace chrono;
1124 auto __day = _S_days(__t);
1125 auto __ymd = _S_date(__t);
1126 days __d;
1127 // See "Calculating Ordinal Dates" at
1128 // https://github.com/HowardHinnant/date/wiki/Examples-and-Recipes
1129 if constexpr (is_same_v<typename decltype(__day)::clock, local_t>)
1130 __d = __day - local_days(__ymd.year()/January/0);
1131 else
1132 __d = __day - sys_days(__ymd.year()/January/0);
1133 return std::format_to(std::move(__out), _GLIBCXX_WIDEN("{:03d}"),
1134 __d.count());
1135 }
1136 }
1137
1138 template<typename _Tp, typename _FormatContext>
1139 typename _FormatContext::iterator
1140 _M_m(const _Tp& __t, typename _FormatContext::iterator __out,
1141 _FormatContext& __ctx, bool __mod) const
1142 {
1143 // %m month as a decimal number.
1144 // %Om Locale's alternative representation.
1145
1146 auto __m = _S_month(__t);
1147 auto __i = (unsigned)__m;
1148
1149 if (__mod && _M_spec._M_localized) [[unlikely]] // %Om
1150 if (auto __loc = __ctx.locale(); __loc != locale::classic())
1151 {
1152 struct tm __tm{};
1153 __tm.tm_mon = __i - 1;
1154 return _M_locale_fmt(std::move(__out), __loc, __tm,
1155 'm', 'O');
1156 }
1157
1158 return __format::__write(std::move(__out), _S_two_digits(__i));
1159 }
1160
1161 template<typename _Tp, typename _FormatContext>
1162 typename _FormatContext::iterator
1163 _M_M(const _Tp& __t, typename _FormatContext::iterator __out,
1164 _FormatContext& __ctx, bool __mod) const
1165 {
1166 // %M The minute as a decimal number.
1167 // %OM Locale's alternative representation.
1168
1169 auto __m = _S_hms(__t).minutes();
1170 auto __i = __m.count();
1171
1172 if (__mod && _M_spec._M_localized) [[unlikely]] // %OM
1173 if (auto __loc = __ctx.locale(); __loc != locale::classic())
1174 {
1175 struct tm __tm{};
1176 __tm.tm_min = __i;
1177 return _M_locale_fmt(std::move(__out), __loc, __tm,
1178 'M', 'O');
1179 }
1180
1181 return __format::__write(std::move(__out), _S_two_digits(__i));
1182 }
1183
1184 template<typename _Tp, typename _FormatContext>
1185 typename _FormatContext::iterator
1186 _M_p(const _Tp& __t, typename _FormatContext::iterator __out,
1187 _FormatContext& __ctx) const
1188 {
1189 // %p The locale's equivalent of the AM/PM designations.
1190 auto __hms = _S_hms(__t);
1191 locale __loc = _M_locale(__ctx);
1192 const auto& __tp = use_facet<__timepunct<_CharT>>(__loc);
1193 const _CharT* __ampm[2];
1194 __tp._M_am_pm(__ampm);
1195 return _M_write(std::move(__out), __loc,
1196 __ampm[__hms.hours().count() >= 12]);
1197 }
1198
1199 template<typename _Tp, typename _FormatContext>
1200 typename _FormatContext::iterator
1201 _M_q(const _Tp&, typename _FormatContext::iterator __out,
1202 _FormatContext&) const
1203 {
1204 // %q The duration's unit suffix
1205 if constexpr (!chrono::__is_duration_v<_Tp>)
1206 __throw_format_error("format error: argument is not a duration");
1207 else
1208 {
1209 namespace __d = chrono::__detail;
1210 using period = typename _Tp::period;
1211 return __d::__fmt_units_suffix<period, _CharT>(std::move(__out));
1212 }
1213 }
1214
1215 // %Q handled in _M_format
1216
1217 template<typename _Tp, typename _FormatContext>
1218 typename _FormatContext::iterator
1219 _M_r(const _Tp& __tt, typename _FormatContext::iterator __out,
1220 _FormatContext& __ctx) const
1221 {
1222 // %r locale's 12-hour clock time.
1223 auto __t = _S_floor_seconds(__tt);
1224 locale __loc = _M_locale(__ctx);
1225 const auto& __tp = use_facet<__timepunct<_CharT>>(__loc);
1226 const _CharT* __ampm_fmt;
1227 __tp._M_am_pm_format(&__ampm_fmt);
1228 basic_string<_CharT> __fmt(_S_empty_spec);
1229 __fmt.insert(1u, 1u, _S_colon);
1230 __fmt.insert(2u, __ampm_fmt);
1231 using _FmtStr = _Runtime_format_string<_CharT>;
1232 return _M_write(std::move(__out), __loc,
1233 std::format(__loc, _FmtStr(__fmt), __t));
1234 }
1235
1236 template<typename _Tp, typename _FormatContext>
1237 typename _FormatContext::iterator
1238 _M_R_T(const _Tp& __t, typename _FormatContext::iterator __out,
1239 _FormatContext& __ctx, bool __secs) const
1240 {
1241 // %R Equivalent to %H:%M
1242 // %T Equivalent to %H:%M:%S
1243 auto __hms = _S_hms(__t);
1244
1245 auto __s = std::format(_GLIBCXX_WIDEN("{:02d}:00"),
1246 __hms.hours().count());
1247 auto __sv = _S_two_digits(__hms.minutes().count());
1248 __s[__s.size() - 2] = __sv[0];
1249 __s[__s.size() - 1] = __sv[1];
1250 __sv = __s;
1251 __out = __format::__write(std::move(__out), __sv);
1252 if (__secs)
1253 {
1254 *__out++ = _S_colon;
1255 __out = _M_S(__hms, std::move(__out), __ctx);
1256 }
1257 return __out;
1258 }
1259
1260 template<typename _Tp, typename _FormatContext>
1261 typename _FormatContext::iterator
1262 _M_S(const _Tp& __t, typename _FormatContext::iterator __out,
1263 _FormatContext& __ctx, bool __mod = false) const
1264 {
1265 // %S Seconds as a decimal number.
1266 // %OS The locale's alternative representation.
1267 auto __hms = _S_hms(__t);
1268 auto __s = __hms.seconds();
1269
1270 if (__mod) [[unlikely]] // %OS
1271 {
1272 if (_M_spec._M_localized)
1273 if (auto __loc = __ctx.locale(); __loc != locale::classic())
1274 {
1275 struct tm __tm{};
1276 __tm.tm_sec = (int)__s.count();
1277 return _M_locale_fmt(std::move(__out), __loc, __tm,
1278 'S', 'O');
1279 }
1280
1281 // %OS formats don't include subseconds, so just format that:
1282 return __format::__write(std::move(__out),
1283 _S_two_digits(__s.count()));
1284 }
1285
1286 if constexpr (__hms.fractional_width == 0)
1287 __out = __format::__write(std::move(__out),
1288 _S_two_digits(__s.count()));
1289 else
1290 {
1291 locale __loc = _M_locale(__ctx);
1292 auto __ss = __hms.subseconds();
1293 using rep = typename decltype(__ss)::rep;
1294 if constexpr (is_floating_point_v<rep>)
1295 {
1296 chrono::duration<rep> __fs = __s + __ss;
1297 __out = std::format_to(std::move(__out), __loc,
1298 _GLIBCXX_WIDEN("{:#0{}.{}Lf}"),
1299 __fs.count(),
1300 3 + __hms.fractional_width,
1301 __hms.fractional_width);
1302 }
1303 else
1304 {
1305 const auto& __np
1306 = use_facet<numpunct<_CharT>>(__loc);
1307 __out = __format::__write(std::move(__out),
1308 _S_two_digits(__s.count()));
1309 *__out++ = __np.decimal_point();
1310 if constexpr (is_integral_v<rep>)
1311 __out = std::format_to(std::move(__out),
1312 _GLIBCXX_WIDEN("{:0{}}"),
1313 __ss.count(),
1314 __hms.fractional_width);
1315 else
1316 {
1317 auto __str = std::format(_S_empty_spec, __ss.count());
1318 __out = std::format_to(_GLIBCXX_WIDEN("{:0>{}s}"),
1319 __str,
1320 __hms.fractional_width);
1321 }
1322 }
1323 }
1324 return __out;
1325 }
1326
1327 // %t handled in _M_format
1328
1329 template<typename _Tp, typename _FormatContext>
1330 typename _FormatContext::iterator
1331 _M_u_w(const _Tp& __t, typename _FormatContext::iterator __out,
1332 _FormatContext& __ctx, _CharT __conv, bool __mod = false) const
1333 {
1334 // %u ISO weekday as a decimal number (1-7), where Monday is 1.
1335 // %Ou Locale's alternative numeric rep.
1336 // %w Weekday as a decimal number (0-6), where Sunday is 0.
1337 // %Ow Locale's alternative numeric rep.
1338
1339 chrono::weekday __wd = _S_weekday(__t);
1340
1341 if (__mod && _M_spec._M_localized) [[unlikely]]
1342 if (auto __loc = __ctx.locale(); __loc != locale::classic())
1343 {
1344 struct tm __tm{};
1345 __tm.tm_wday = __wd.c_encoding();
1346 return _M_locale_fmt(std::move(__out), __loc, __tm,
1347 (char)__conv, 'O');
1348 }
1349
1350 unsigned __wdi = __conv == 'u' ? __wd.iso_encoding()
1351 : __wd.c_encoding();
1352 const _CharT __d = _S_digit(__wdi);
1353 return __format::__write(std::move(__out), __string_view(&__d, 1));
1354 }
1355
1356 template<typename _Tp, typename _FormatContext>
1357 typename _FormatContext::iterator
1358 _M_U_V_W(const _Tp& __t, typename _FormatContext::iterator __out,
1359 _FormatContext& __ctx, _CharT __conv, bool __mod = false) const
1360 {
1361 // %U Week number of the year as a decimal number, from first Sunday.
1362 // %OU Locale's alternative numeric rep.
1363 // %V ISO week-based week number as a decimal number.
1364 // %OV Locale's alternative numeric rep.
1365 // %W Week number of the year as a decimal number, from first Monday.
1366 // %OW Locale's alternative numeric rep.
1367 using namespace chrono;
1368 auto __d = _S_days(__t);
1369 using _TDays = decltype(__d); // Either sys_days or local_days.
1370
1371 if (__mod && _M_spec._M_localized) [[unlikely]]
1372 if (auto __loc = __ctx.locale(); __loc != locale::classic())
1373 {
1374 const year_month_day __ymd(__d);
1375 const year __y = __ymd.year();
1376 struct tm __tm{};
1377 __tm.tm_year = (int)__y - 1900;
1378 __tm.tm_yday = (__d - _TDays(__y/January/1)).count();
1379 __tm.tm_wday = weekday(__d).c_encoding();
1380 return _M_locale_fmt(std::move(__out), __loc, __tm,
1381 (char)__conv, 'O');
1382 }
1383
1384 _TDays __first; // First day of week 1.
1385 if (__conv == 'V') // W01 begins on Monday before first Thursday.
1386 {
1387 // Move to nearest Thursday:
1388 __d -= (weekday(__d) - Monday) - days(3);
1389 // ISO week of __t is number of weeks since January 1 of the
1390 // same year as that nearest Thursday.
1391 __first = _TDays(year_month_day(__d).year()/January/1);
1392 }
1393 else
1394 {
1395 year __y;
1396 if constexpr (requires { __t.year(); })
1397 __y = __t.year();
1398 else
1399 __y = year_month_day(__d).year();
1400 const weekday __weekstart = __conv == 'U' ? Sunday : Monday;
1401 __first = _TDays(__y/January/__weekstart[1]);
1402 }
1403 auto __weeks = chrono::floor<weeks>(__d - __first);
1404 __string_view __sv = _S_two_digits(__weeks.count() + 1);
1405 return __format::__write(std::move(__out), __sv);
1406 }
1407
1408 template<typename _Tp, typename _FormatContext>
1409 typename _FormatContext::iterator
1410 _M_x(const _Tp& __t, typename _FormatContext::iterator __out,
1411 _FormatContext& __ctx, bool __mod = false) const
1412 {
1413 // %x Locale's date rep
1414 // %Ex Locale's alternative date representation.
1415 locale __loc = _M_locale(__ctx);
1416 const auto& __tp = use_facet<__timepunct<_CharT>>(__loc);
1417 const _CharT* __date_reps[2];
1418 __tp._M_date_formats(__date_reps);
1419 const _CharT* __rep = __date_reps[__mod];
1420 if (!*__rep)
1421 return _M_D(__t, std::move(__out), __ctx);
1422
1423 basic_string<_CharT> __fmt(_S_empty_spec);
1424 __fmt.insert(1u, 1u, _S_colon);
1425 __fmt.insert(2u, __rep);
1426 using _FmtStr = _Runtime_format_string<_CharT>;
1427 return _M_write(std::move(__out), __loc,
1428 std::format(__loc, _FmtStr(__fmt), __t));
1429 }
1430
1431 template<typename _Tp, typename _FormatContext>
1432 typename _FormatContext::iterator
1433 _M_X(const _Tp& __tt, typename _FormatContext::iterator __out,
1434 _FormatContext& __ctx, bool __mod = false) const
1435 {
1436 // %X Locale's time rep
1437 // %EX Locale's alternative time representation.
1438 auto __t = _S_floor_seconds(__tt);
1439 locale __loc = _M_locale(__ctx);
1440 const auto& __tp = use_facet<__timepunct<_CharT>>(__loc);
1441 const _CharT* __time_reps[2];
1442 __tp._M_time_formats(__time_reps);
1443 const _CharT* __rep = __time_reps[__mod];
1444 if (!*__rep)
1445 return _M_R_T(__t, std::move(__out), __ctx, true);
1446
1447 basic_string<_CharT> __fmt(_S_empty_spec);
1448 __fmt.insert(1u, 1u, _S_colon);
1449 __fmt.insert(2u, __rep);
1450 using _FmtStr = _Runtime_format_string<_CharT>;
1451 return _M_write(std::move(__out), __loc,
1452 std::format(__loc, _FmtStr(__fmt), __t));
1453 }
1454
1455 template<typename _Tp, typename _FormatContext>
1456 typename _FormatContext::iterator
1457 _M_z(const _Tp& __t, typename _FormatContext::iterator __out,
1458 _FormatContext&, bool __mod = false) const
1459 {
1460 using ::std::chrono::__detail::__utc_leap_second;
1461 using ::std::chrono::__detail::__local_time_fmt;
1462
1463 auto __utc = __mod ? __string_view(_GLIBCXX_WIDEN("+00:00"), 6)
1464 : __string_view(_GLIBCXX_WIDEN("+0000"), 5);
1465
1466 if constexpr (chrono::__is_time_point_v<_Tp>)
1467 {
1468 if constexpr (is_same_v<typename _Tp::clock,
1469 chrono::system_clock>)
1470 return __format::__write(std::move(__out), __utc);
1471 }
1472 else if constexpr (__is_specialization_of<_Tp, __local_time_fmt>)
1473 {
1474 if (__t._M_offset_sec)
1475 {
1476 auto __sv = __utc;
1477 basic_string<_CharT> __s;
1478 if (*__t._M_offset_sec != 0s)
1479 {
1480 chrono:: hh_mm_ss __hms(*__t._M_offset_sec);
1481 __s = _S_plus_minus[__hms.is_negative()];
1482 __s += _S_two_digits(__hms.hours().count());
1483 if (__mod)
1484 __s += _S_colon;
1485 __s += _S_two_digits(__hms.minutes().count());
1486 __sv = __s;
1487 }
1488 return __format::__write(std::move(__out), __sv);
1489 }
1490 }
1491 else if constexpr (__is_specialization_of<_Tp, __utc_leap_second>)
1492 return __format::__write(std::move(__out), __utc);
1493
1494 __no_timezone_available();
1495 }
1496
1497 template<typename _Tp, typename _FormatContext>
1498 typename _FormatContext::iterator
1499 _M_Z(const _Tp& __t, typename _FormatContext::iterator __out,
1500 _FormatContext& __ctx) const
1501 {
1502 using ::std::chrono::__detail::__utc_leap_second;
1503 using ::std::chrono::__detail::__local_time_fmt;
1504
1505 __string_view __utc(_GLIBCXX_WIDEN("UTC"), 3);
1506 if constexpr (chrono::__is_time_point_v<_Tp>)
1507 {
1508 if constexpr (is_same_v<typename _Tp::clock,
1509 chrono::system_clock>)
1510 return __format::__write(std::move(__out), __utc);
1511 }
1512 else if constexpr (__is_specialization_of<_Tp, __local_time_fmt>)
1513 {
1514 if (__t._M_abbrev)
1515 {
1516 string_view __sv = *__t._M_abbrev;
1517 if constexpr (is_same_v<_CharT, char>)
1518 return __format::__write(std::move(__out), __sv);
1519 else
1520 {
1521 // TODO use resize_and_overwrite
1522 basic_string<_CharT> __ws(__sv.size(), _CharT());
1523 auto& __ct = use_facet<ctype<_CharT>>(_M_locale(__ctx));
1524 __ct.widen(__sv.begin(), __sv.end(), __ws.data());
1525 __string_view __wsv = __ws;
1526 return __format::__write(std::move(__out), __wsv);
1527 }
1528 }
1529 }
1530 else if constexpr (__is_specialization_of<_Tp, __utc_leap_second>)
1531 return __format::__write(std::move(__out), __utc);
1532
1533 __no_timezone_available();
1534 }
1535
1536 // %% handled in _M_format
1537
1538 // A single digit character in the range '0'..'9'.
1539 static _CharT
1540 _S_digit(int __n) noexcept
1541 {
1542 // Extra 9s avoid past-the-end read on bad input.
1543 return _GLIBCXX_WIDEN("0123456789999999")[__n & 0xf];
1544 }
1545
1546 // A string view of two digit characters, "00".."99".
1547 static basic_string_view<_CharT>
1548 _S_two_digits(int __n) noexcept
1549 {
1550 return {
1551 _GLIBCXX_WIDEN("0001020304050607080910111213141516171819"
1552 "2021222324252627282930313233343536373839"
1553 "4041424344454647484950515253545556575859"
1554 "6061626364656667686970717273747576777879"
1555 "8081828384858687888990919293949596979899"
1556 "9999999999999999999999999999999999999999"
1557 "9999999999999999") + 2 * (__n & 0x7f),
1558 2
1559 };
1560 }
1561
1562 // Accessors for the components of chrono types:
1563
1564 // Returns a hh_mm_ss.
1565 template<typename _Tp>
1566 static decltype(auto)
1567 _S_hms(const _Tp& __t)
1568 {
1569 using ::std::chrono::__detail::__utc_leap_second;
1570 using ::std::chrono::__detail::__local_time_fmt;
1571
1572 if constexpr (__is_specialization_of<_Tp, chrono::hh_mm_ss>)
1573 return __t;
1574 else if constexpr (__is_specialization_of<_Tp, __utc_leap_second>)
1575 return __t._M_time;
1576 else if constexpr (chrono::__is_duration_v<_Tp>)
1577 return chrono::hh_mm_ss<_Tp>(__t);
1578 else if constexpr (chrono::__is_time_point_v<_Tp>)
1579 return chrono::hh_mm_ss(__t - chrono::floor<chrono::days>(__t));
1580 else if constexpr (__is_specialization_of<_Tp, __local_time_fmt>)
1581 return _S_hms(__t._M_time);
1582 else
1583 {
1584 __invalid_chrono_spec();
1585 return chrono::hh_mm_ss<chrono::seconds>();
1586 }
1587 }
1588
1589 // Returns a sys_days or local_days.
1590 template<typename _Tp>
1591 static auto
1592 _S_days(const _Tp& __t)
1593 {
1594 using namespace chrono;
1595 using ::std::chrono::__detail::__utc_leap_second;
1596 using ::std::chrono::__detail::__local_time_fmt;
1597
1598 if constexpr (__is_time_point_v<_Tp>)
1599 return chrono::floor<days>(__t);
1600 else if constexpr (__is_specialization_of<_Tp, __utc_leap_second>)
1601 return __t._M_date;
1602 else if constexpr (__is_specialization_of<_Tp, __local_time_fmt>)
1603 return chrono::floor<days>(__t._M_time);
1604 else if constexpr (is_same_v<_Tp, year_month_day>
1605 || is_same_v<_Tp, year_month_day_last>
1606 || is_same_v<_Tp, year_month_weekday>
1607 || is_same_v<_Tp, year_month_weekday_last>)
1608 return sys_days(__t);
1609 else
1610 {
1611 if constexpr (__is_duration_v<_Tp>)
1612 __not_valid_for_duration();
1613 else
1614 __invalid_chrono_spec();
1615 return chrono::sys_days();
1616 }
1617 }
1618
1619 // Returns a year_month_day.
1620 template<typename _Tp>
1621 static chrono::year_month_day
1622 _S_date(const _Tp& __t)
1623 {
1624 if constexpr (is_same_v<_Tp, chrono::year_month_day>)
1625 return __t;
1626 else
1627 return chrono::year_month_day(_S_days(__t));
1628 }
1629
1630 template<typename _Tp>
1631 static chrono::day
1632 _S_day(const _Tp& __t)
1633 {
1634 using namespace chrono;
1635
1636 if constexpr (is_same_v<_Tp, day>)
1637 return __t;
1638 else if constexpr (requires { __t.day(); })
1639 return __t.day();
1640 else
1641 return _S_date(__t).day();
1642 }
1643
1644 template<typename _Tp>
1645 static chrono::month
1646 _S_month(const _Tp& __t)
1647 {
1648 using namespace chrono;
1649
1650 if constexpr (is_same_v<_Tp, month>)
1651 return __t;
1652 else if constexpr (requires { __t.month(); })
1653 return __t.month();
1654 else
1655 return _S_date(__t).month();
1656 }
1657
1658 template<typename _Tp>
1659 static chrono::year
1660 _S_year(const _Tp& __t)
1661 {
1662 using namespace chrono;
1663
1664 if constexpr (is_same_v<_Tp, year>)
1665 return __t;
1666 else if constexpr (requires { __t.year(); })
1667 return __t.year();
1668 else
1669 return _S_date(__t).year();
1670 }
1671
1672 template<typename _Tp>
1673 static chrono::weekday
1674 _S_weekday(const _Tp& __t)
1675 {
1676 using namespace ::std::chrono;
1677 using ::std::chrono::__detail::__local_time_fmt;
1678
1679 if constexpr (is_same_v<_Tp, weekday>)
1680 return __t;
1681 else if constexpr (requires { __t.weekday(); })
1682 return __t.weekday();
1683 else if constexpr (is_same_v<_Tp, month_weekday>)
1684 return __t.weekday_indexed().weekday();
1685 else if constexpr (is_same_v<_Tp, month_weekday_last>)
1686 return __t.weekday_last().weekday();
1687 else
1688 return weekday(_S_days(__t));
1689 }
1690
1691 // Remove subsecond precision from a time_point.
1692 template<typename _Tp>
1693 static auto
1694 _S_floor_seconds(const _Tp& __t)
1695 {
1696 using chrono::__detail::__local_time_fmt;
1697 if constexpr (chrono::__is_time_point_v<_Tp>
1698 || chrono::__is_duration_v<_Tp>)
1699 {
1700 if constexpr (_Tp::period::den != 1)
1701 return chrono::floor<chrono::seconds>(__t);
1702 else
1703 return __t;
1704 }
1705 else if constexpr (__is_specialization_of<_Tp, chrono::hh_mm_ss>)
1706 {
1707 if constexpr (_Tp::fractional_width != 0)
1708 return chrono::floor<chrono::seconds>(__t.to_duration());
1709 else
1710 return __t;
1711 }
1712 else if constexpr (__is_specialization_of<_Tp, __local_time_fmt>)
1713 return _S_floor_seconds(__t._M_time);
1714 else
1715 return __t;
1716 }
1717
1718 // Use the formatting locale's std::time_put facet to produce
1719 // a locale-specific representation.
1720 template<typename _Iter>
1721 _Iter
1722 _M_locale_fmt(_Iter __out, const locale& __loc, const struct tm& __tm,
1723 char __fmt, char __mod) const
1724 {
1725 basic_ostringstream<_CharT> __os;
1726 __os.imbue(__loc);
1727 const auto& __tp = use_facet<time_put<_CharT>>(__loc);
1728 __tp.put(__os, __os, _S_space, &__tm, __fmt, __mod);
1729 if (__os)
1730 __out = _M_write(std::move(__out), __loc, __os.view());
1731 return __out;
1732 }
1733 };
1734
1735} // namespace __format
1736/// @endcond
1737
1738 template<typename _Rep, typename _Period, typename _CharT>
1739 requires __format::__formattable_impl<_Rep, _CharT>
1740 struct formatter<chrono::duration<_Rep, _Period>, _CharT>
1741 {
1742 constexpr typename basic_format_parse_context<_CharT>::iterator
1743 parse(basic_format_parse_context<_CharT>& __pc)
1744 {
1745 using namespace __format;
1746 auto __it = _M_f._M_parse(__pc, _Duration|_TimeOfDay);
1747 if constexpr (!is_floating_point_v<_Rep>)
1748 if (_M_f._M_spec._M_prec_kind != __format::_WP_none)
1749 __throw_format_error("format error: invalid precision for duration");
1750 return __it;
1751 }
1752
1753 template<typename _Out>
1754 typename basic_format_context<_Out, _CharT>::iterator
1755 format(const chrono::duration<_Rep, _Period>& __d,
1756 basic_format_context<_Out, _CharT>& __fc) const
1757 {
1758 if constexpr (numeric_limits<_Rep>::is_signed)
1759 if (__d < __d.zero()) [[unlikely]]
1760 {
1761 if constexpr (is_integral_v<_Rep>)
1762 {
1763 // -d is undefined for the most negative integer.
1764 // Convert duration to corresponding unsigned rep.
1765 using _URep = make_unsigned_t<_Rep>;
1766 auto __ucnt = -static_cast<_URep>(__d.count());
1767 auto __ud = chrono::duration<_URep, _Period>(__ucnt);
1768 return _M_f._M_format(__ud, __fc, true);
1769 }
1770 else
1771 return _M_f._M_format(-__d, __fc, true);
1772 }
1773 return _M_f._M_format(__d, __fc, false);
1774 }
1775
1776 private:
1777 __format::__formatter_chrono<_CharT> _M_f;
1778 };
1779
1780 template<__format::__char _CharT>
1781 struct formatter<chrono::day, _CharT>
1782 {
1783 constexpr typename basic_format_parse_context<_CharT>::iterator
1784 parse(basic_format_parse_context<_CharT>& __pc)
1785 { return _M_f._M_parse(__pc, __format::_Day); }
1786
1787 template<typename _Out>
1788 typename basic_format_context<_Out, _CharT>::iterator
1789 format(const chrono::day& __t,
1790 basic_format_context<_Out, _CharT>& __fc) const
1791 { return _M_f._M_format(__t, __fc); }
1792
1793 private:
1794 __format::__formatter_chrono<_CharT> _M_f;
1795 };
1796
1797 template<__format::__char _CharT>
1798 struct formatter<chrono::month, _CharT>
1799 {
1800 constexpr typename basic_format_parse_context<_CharT>::iterator
1801 parse(basic_format_parse_context<_CharT>& __pc)
1802 { return _M_f._M_parse(__pc, __format::_Month); }
1803
1804 template<typename _Out>
1805 typename basic_format_context<_Out, _CharT>::iterator
1806 format(const chrono::month& __t,
1807 basic_format_context<_Out, _CharT>& __fc) const
1808 { return _M_f._M_format(__t, __fc); }
1809
1810 private:
1811 __format::__formatter_chrono<_CharT> _M_f;
1812 };
1813
1814 template<__format::__char _CharT>
1815 struct formatter<chrono::year, _CharT>
1816 {
1817 constexpr typename basic_format_parse_context<_CharT>::iterator
1818 parse(basic_format_parse_context<_CharT>& __pc)
1819 { return _M_f._M_parse(__pc, __format::_Year); }
1820
1821 template<typename _Out>
1822 typename basic_format_context<_Out, _CharT>::iterator
1823 format(const chrono::year& __t,
1824 basic_format_context<_Out, _CharT>& __fc) const
1825 { return _M_f._M_format(__t, __fc); }
1826
1827 private:
1828 __format::__formatter_chrono<_CharT> _M_f;
1829 };
1830
1831 template<__format::__char _CharT>
1832 struct formatter<chrono::weekday, _CharT>
1833 {
1834 constexpr typename basic_format_parse_context<_CharT>::iterator
1835 parse(basic_format_parse_context<_CharT>& __pc)
1836 { return _M_f._M_parse(__pc, __format::_Weekday); }
1837
1838 template<typename _Out>
1839 typename basic_format_context<_Out, _CharT>::iterator
1840 format(const chrono::weekday& __t,
1841 basic_format_context<_Out, _CharT>& __fc) const
1842 { return _M_f._M_format(__t, __fc); }
1843
1844 private:
1845 __format::__formatter_chrono<_CharT> _M_f;
1846 };
1847
1848 template<__format::__char _CharT>
1849 struct formatter<chrono::weekday_indexed, _CharT>
1850 {
1851 constexpr typename basic_format_parse_context<_CharT>::iterator
1852 parse(basic_format_parse_context<_CharT>& __pc)
1853 { return _M_f._M_parse(__pc, __format::_Weekday); }
1854
1855 template<typename _Out>
1856 typename basic_format_context<_Out, _CharT>::iterator
1857 format(const chrono::weekday_indexed& __t,
1858 basic_format_context<_Out, _CharT>& __fc) const
1859 { return _M_f._M_format(__t, __fc); }
1860
1861 private:
1862 __format::__formatter_chrono<_CharT> _M_f;
1863 };
1864
1865 template<__format::__char _CharT>
1866 struct formatter<chrono::weekday_last, _CharT>
1867 {
1868 constexpr typename basic_format_parse_context<_CharT>::iterator
1869 parse(basic_format_parse_context<_CharT>& __pc)
1870 { return _M_f._M_parse(__pc, __format::_Weekday); }
1871
1872 template<typename _Out>
1873 typename basic_format_context<_Out, _CharT>::iterator
1874 format(const chrono::weekday_last& __t,
1875 basic_format_context<_Out, _CharT>& __fc) const
1876 { return _M_f._M_format(__t, __fc); }
1877
1878 private:
1879 __format::__formatter_chrono<_CharT> _M_f;
1880 };
1881
1882 template<__format::__char _CharT>
1883 struct formatter<chrono::month_day, _CharT>
1884 {
1885 constexpr typename basic_format_parse_context<_CharT>::iterator
1886 parse(basic_format_parse_context<_CharT>& __pc)
1887 { return _M_f._M_parse(__pc, __format::_Month|__format::_Day); }
1888
1889 template<typename _Out>
1890 typename basic_format_context<_Out, _CharT>::iterator
1891 format(const chrono::month_day& __t,
1892 basic_format_context<_Out, _CharT>& __fc) const
1893 { return _M_f._M_format(__t, __fc); }
1894
1895 private:
1896 __format::__formatter_chrono<_CharT> _M_f;
1897 };
1898
1899 template<__format::__char _CharT>
1900 struct formatter<chrono::month_day_last, _CharT>
1901 {
1902 constexpr typename basic_format_parse_context<_CharT>::iterator
1903 parse(basic_format_parse_context<_CharT>& __pc)
1904 { return _M_f._M_parse(__pc, __format::_Month|__format::_Day); }
1905
1906 template<typename _Out>
1907 typename basic_format_context<_Out, _CharT>::iterator
1908 format(const chrono::month_day_last& __t,
1909 basic_format_context<_Out, _CharT>& __fc) const
1910 { return _M_f._M_format(__t, __fc); }
1911
1912 private:
1913 __format::__formatter_chrono<_CharT> _M_f;
1914 };
1915
1916 template<__format::__char _CharT>
1917 struct formatter<chrono::month_weekday, _CharT>
1918 {
1919 constexpr typename basic_format_parse_context<_CharT>::iterator
1920 parse(basic_format_parse_context<_CharT>& __pc)
1921 { return _M_f._M_parse(__pc, __format::_Month|__format::_Weekday); }
1922
1923 template<typename _Out>
1924 typename basic_format_context<_Out, _CharT>::iterator
1925 format(const chrono::month_weekday& __t,
1926 basic_format_context<_Out, _CharT>& __fc) const
1927 { return _M_f._M_format(__t, __fc); }
1928
1929 private:
1930 __format::__formatter_chrono<_CharT> _M_f;
1931 };
1932
1933 template<__format::__char _CharT>
1934 struct formatter<chrono::month_weekday_last, _CharT>
1935 {
1936 constexpr typename basic_format_parse_context<_CharT>::iterator
1937 parse(basic_format_parse_context<_CharT>& __pc)
1938 { return _M_f._M_parse(__pc, __format::_Month|__format::_Weekday); }
1939
1940 template<typename _Out>
1941 typename basic_format_context<_Out, _CharT>::iterator
1942 format(const chrono::month_weekday_last& __t,
1943 basic_format_context<_Out, _CharT>& __fc) const
1944 { return _M_f._M_format(__t, __fc); }
1945
1946 private:
1947 __format::__formatter_chrono<_CharT> _M_f;
1948 };
1949
1950 template<__format::__char _CharT>
1951 struct formatter<chrono::year_month, _CharT>
1952 {
1953 constexpr typename basic_format_parse_context<_CharT>::iterator
1954 parse(basic_format_parse_context<_CharT>& __pc)
1955 { return _M_f._M_parse(__pc, __format::_Year|__format::_Month); }
1956
1957 template<typename _Out>
1958 typename basic_format_context<_Out, _CharT>::iterator
1959 format(const chrono::year_month& __t,
1960 basic_format_context<_Out, _CharT>& __fc) const
1961 { return _M_f._M_format(__t, __fc); }
1962
1963 private:
1964 __format::__formatter_chrono<_CharT> _M_f;
1965 };
1966
1967 template<__format::__char _CharT>
1968 struct formatter<chrono::year_month_day, _CharT>
1969 {
1970 constexpr typename basic_format_parse_context<_CharT>::iterator
1971 parse(basic_format_parse_context<_CharT>& __pc)
1972 { return _M_f._M_parse(__pc, __format::_Date); }
1973
1974 template<typename _Out>
1975 typename basic_format_context<_Out, _CharT>::iterator
1976 format(const chrono::year_month_day& __t,
1977 basic_format_context<_Out, _CharT>& __fc) const
1978 { return _M_f._M_format(__t, __fc); }
1979
1980 private:
1981 __format::__formatter_chrono<_CharT> _M_f;
1982 };
1983
1984 template<__format::__char _CharT>
1985 struct formatter<chrono::year_month_day_last, _CharT>
1986 {
1987 constexpr typename basic_format_parse_context<_CharT>::iterator
1988 parse(basic_format_parse_context<_CharT>& __pc)
1989 { return _M_f._M_parse(__pc, __format::_Date); }
1990
1991 template<typename _Out>
1992 typename basic_format_context<_Out, _CharT>::iterator
1993 format(const chrono::year_month_day_last& __t,
1994 basic_format_context<_Out, _CharT>& __fc) const
1995 { return _M_f._M_format(__t, __fc); }
1996
1997 private:
1998 __format::__formatter_chrono<_CharT> _M_f;
1999 };
2000
2001 template<__format::__char _CharT>
2002 struct formatter<chrono::year_month_weekday, _CharT>
2003 {
2004 constexpr typename basic_format_parse_context<_CharT>::iterator
2005 parse(basic_format_parse_context<_CharT>& __pc)
2006 { return _M_f._M_parse(__pc, __format::_Date); }
2007
2008 template<typename _Out>
2009 typename basic_format_context<_Out, _CharT>::iterator
2010 format(const chrono::year_month_weekday& __t,
2011 basic_format_context<_Out, _CharT>& __fc) const
2012 { return _M_f._M_format(__t, __fc); }
2013
2014 private:
2015 __format::__formatter_chrono<_CharT> _M_f;
2016 };
2017
2018 template<__format::__char _CharT>
2019 struct formatter<chrono::year_month_weekday_last, _CharT>
2020 {
2021 constexpr typename basic_format_parse_context<_CharT>::iterator
2022 parse(basic_format_parse_context<_CharT>& __pc)
2023 { return _M_f._M_parse(__pc, __format::_Date); }
2024
2025 template<typename _Out>
2026 typename basic_format_context<_Out, _CharT>::iterator
2027 format(const chrono::year_month_weekday_last& __t,
2028 basic_format_context<_Out, _CharT>& __fc) const
2029 { return _M_f._M_format(__t, __fc); }
2030
2031 private:
2032 __format::__formatter_chrono<_CharT> _M_f;
2033 };
2034
2035 template<typename _Rep, typename _Period, __format::__char _CharT>
2036 struct formatter<chrono::hh_mm_ss<chrono::duration<_Rep, _Period>>, _CharT>
2037 {
2038 constexpr typename basic_format_parse_context<_CharT>::iterator
2039 parse(basic_format_parse_context<_CharT>& __pc)
2040 { return _M_f._M_parse(__pc, __format::_TimeOfDay); }
2041
2042 template<typename _Out>
2043 typename basic_format_context<_Out, _CharT>::iterator
2044 format(const chrono::hh_mm_ss<chrono::duration<_Rep, _Period>>& __t,
2045 basic_format_context<_Out, _CharT>& __fc) const
2046 { return _M_f._M_format(__t, __fc); }
2047
2048 private:
2049 __format::__formatter_chrono<_CharT> _M_f;
2050 };
2051
2052#if _GLIBCXX_USE_CXX11_ABI || ! _GLIBCXX_USE_DUAL_ABI
2053 template<__format::__char _CharT>
2054 struct formatter<chrono::sys_info, _CharT>
2055 {
2056 constexpr typename basic_format_parse_context<_CharT>::iterator
2057 parse(basic_format_parse_context<_CharT>& __pc)
2058 { return _M_f._M_parse(__pc, __format::_ChronoParts{}); }
2059
2060 template<typename _Out>
2061 typename basic_format_context<_Out, _CharT>::iterator
2062 format(const chrono::sys_info& __i,
2063 basic_format_context<_Out, _CharT>& __fc) const
2064 { return _M_f._M_format(__i, __fc); }
2065
2066 private:
2067 __format::__formatter_chrono<_CharT> _M_f;
2068 };
2069
2070 template<__format::__char _CharT>
2071 struct formatter<chrono::local_info, _CharT>
2072 {
2073 constexpr typename basic_format_parse_context<_CharT>::iterator
2074 parse(basic_format_parse_context<_CharT>& __pc)
2075 { return _M_f._M_parse(__pc, __format::_ChronoParts{}); }
2076
2077 template<typename _Out>
2078 typename basic_format_context<_Out, _CharT>::iterator
2079 format(const chrono::local_info& __i,
2080 basic_format_context<_Out, _CharT>& __fc) const
2081 { return _M_f._M_format(__i, __fc); }
2082
2083 private:
2084 __format::__formatter_chrono<_CharT> _M_f;
2085 };
2086#endif
2087
2088 template<typename _Duration, __format::__char _CharT>
2089 struct formatter<chrono::sys_time<_Duration>, _CharT>
2090 {
2091 constexpr typename basic_format_parse_context<_CharT>::iterator
2092 parse(basic_format_parse_context<_CharT>& __pc)
2093 {
2094 auto __next = _M_f._M_parse(__pc, __format::_ZonedDateTime);
2095 if constexpr (!__stream_insertable)
2096 if (_M_f._M_spec._M_chrono_specs.empty())
2097 __format::__invalid_chrono_spec(); // chrono-specs can't be empty
2098 return __next;
2099 }
2100
2101 template<typename _Out>
2102 typename basic_format_context<_Out, _CharT>::iterator
2103 format(const chrono::sys_time<_Duration>& __t,
2104 basic_format_context<_Out, _CharT>& __fc) const
2105 { return _M_f._M_format(__t, __fc); }
2106
2107 private:
2108 static constexpr bool __stream_insertable
2109 = requires (basic_ostream<_CharT>& __os,
2110 chrono::sys_time<_Duration> __t) { __os << __t; };
2111
2112 __format::__formatter_chrono<_CharT> _M_f;
2113 };
2114
2115 template<typename _Duration, __format::__char _CharT>
2116 struct formatter<chrono::utc_time<_Duration>, _CharT>
2117 : __format::__formatter_chrono<_CharT>
2118 {
2119 constexpr typename basic_format_parse_context<_CharT>::iterator
2120 parse(basic_format_parse_context<_CharT>& __pc)
2121 { return _M_f._M_parse(__pc, __format::_ZonedDateTime); }
2122
2123 template<typename _Out>
2124 typename basic_format_context<_Out, _CharT>::iterator
2125 format(const chrono::utc_time<_Duration>& __t,
2126 basic_format_context<_Out, _CharT>& __fc) const
2127 {
2128 // Adjust by removing leap seconds to get equivalent sys_time.
2129 // We can't just use clock_cast because we want to know if the time
2130 // falls within a leap second insertion, and format seconds as "60".
2131 using chrono::__detail::__utc_leap_second;
2132 using chrono::seconds;
2133 using chrono::sys_time;
2134 using _CDur = common_type_t<_Duration, seconds>;
2135 const auto __li = chrono::get_leap_second_info(__t);
2136 sys_time<_CDur> __s{__t.time_since_epoch() - __li.elapsed};
2137 if (!__li.is_leap_second) [[likely]]
2138 return _M_f._M_format(__s, __fc);
2139 else
2140 return _M_f._M_format(__utc_leap_second(__s), __fc);
2141 }
2142
2143 private:
2144 friend formatter<chrono::__detail::__utc_leap_second<_Duration>, _CharT>;
2145
2146 __format::__formatter_chrono<_CharT> _M_f;
2147 };
2148
2149 template<typename _Duration, __format::__char _CharT>
2150 struct formatter<chrono::tai_time<_Duration>, _CharT>
2151 : __format::__formatter_chrono<_CharT>
2152 {
2153 constexpr typename basic_format_parse_context<_CharT>::iterator
2154 parse(basic_format_parse_context<_CharT>& __pc)
2155 { return _M_f._M_parse(__pc, __format::_ZonedDateTime); }
2156
2157 template<typename _Out>
2158 typename basic_format_context<_Out, _CharT>::iterator
2159 format(const chrono::tai_time<_Duration>& __t,
2160 basic_format_context<_Out, _CharT>& __fc) const
2161 {
2162 // Convert to __local_time_fmt with abbrev "TAI" and offset 0s.
2163 // We use __local_time_fmt and not sys_time (as the standard implies)
2164 // because %Z for sys_time would print "UTC" and we want "TAI" here.
2165
2166 // Offset is 1970y/January/1 - 1958y/January/1
2167 constexpr chrono::days __tai_offset = chrono::days(4383);
2168 using _CDur = common_type_t<_Duration, chrono::days>;
2169 chrono::local_time<_CDur> __lt(__t.time_since_epoch() - __tai_offset);
2170 const string __abbrev("TAI", 3);
2171 const chrono::seconds __off = 0s;
2172 const auto __lf = chrono::local_time_format(__lt, &__abbrev, &__off);
2173 return _M_f._M_format(__lf, __fc);
2174 }
2175
2176 private:
2177 __format::__formatter_chrono<_CharT> _M_f;
2178 };
2179
2180 template<typename _Duration, __format::__char _CharT>
2181 struct formatter<chrono::gps_time<_Duration>, _CharT>
2182 : __format::__formatter_chrono<_CharT>
2183 {
2184 constexpr typename basic_format_parse_context<_CharT>::iterator
2185 parse(basic_format_parse_context<_CharT>& __pc)
2186 { return _M_f._M_parse(__pc, __format::_ZonedDateTime); }
2187
2188 template<typename _Out>
2189 typename basic_format_context<_Out, _CharT>::iterator
2190 format(const chrono::gps_time<_Duration>& __t,
2191 basic_format_context<_Out, _CharT>& __fc) const
2192 {
2193 // Convert to __local_time_fmt with abbrev "GPS" and offset 0s.
2194 // We use __local_time_fmt and not sys_time (as the standard implies)
2195 // because %Z for sys_time would print "UTC" and we want "GPS" here.
2196
2197 // Offset is 1980y/January/Sunday[1] - 1970y/January/1
2198 constexpr chrono::days __gps_offset = chrono::days(3657);
2199 using _CDur = common_type_t<_Duration, chrono::days>;
2200 chrono::local_time<_CDur> __lt(__t.time_since_epoch() + __gps_offset);
2201 const string __abbrev("GPS", 3);
2202 const chrono::seconds __off = 0s;
2203 const auto __lf = chrono::local_time_format(__lt, &__abbrev, &__off);
2204 return _M_f._M_format(__lf, __fc);
2205 }
2206
2207 private:
2208 __format::__formatter_chrono<_CharT> _M_f;
2209 };
2210
2211 template<typename _Duration, __format::__char _CharT>
2212 struct formatter<chrono::file_time<_Duration>, _CharT>
2213 {
2214 constexpr typename basic_format_parse_context<_CharT>::iterator
2215 parse(basic_format_parse_context<_CharT>& __pc)
2216 { return _M_f._M_parse(__pc, __format::_ZonedDateTime); }
2217
2218 template<typename _Out>
2219 typename basic_format_context<_Out, _CharT>::iterator
2220 format(const chrono::file_time<_Duration>& __t,
2221 basic_format_context<_Out, _CharT>& __fc) const
2222 {
2223 using namespace chrono;
2224 return _M_f._M_format(chrono::clock_cast<system_clock>(__t), __fc);
2225 }
2226
2227 private:
2228 __format::__formatter_chrono<_CharT> _M_f;
2229 };
2230
2231 template<typename _Duration, __format::__char _CharT>
2232 struct formatter<chrono::local_time<_Duration>, _CharT>
2233 {
2234 constexpr typename basic_format_parse_context<_CharT>::iterator
2235 parse(basic_format_parse_context<_CharT>& __pc)
2236 { return _M_f._M_parse(__pc, __format::_DateTime); }
2237
2238 template<typename _Out>
2239 typename basic_format_context<_Out, _CharT>::iterator
2240 format(const chrono::local_time<_Duration>& __t,
2241 basic_format_context<_Out, _CharT>& __fc) const
2242 { return _M_f._M_format(__t, __fc); }
2243
2244 private:
2245 __format::__formatter_chrono<_CharT> _M_f;
2246 };
2247
2248 template<typename _Duration, __format::__char _CharT>
2249 struct formatter<chrono::__detail::__local_time_fmt<_Duration>, _CharT>
2250 {
2251 constexpr typename basic_format_parse_context<_CharT>::iterator
2252 parse(basic_format_parse_context<_CharT>& __pc)
2253 { return _M_f._M_parse(__pc, __format::_ZonedDateTime); }
2254
2255 template<typename _Out>
2256 typename basic_format_context<_Out, _CharT>::iterator
2257 format(const chrono::__detail::__local_time_fmt<_Duration>& __t,
2258 basic_format_context<_Out, _CharT>& __fc) const
2259 { return _M_f._M_format(__t, __fc, /* use %Z for {} */ true); }
2260
2261 private:
2262 __format::__formatter_chrono<_CharT> _M_f;
2263 };
2264
2265#if _GLIBCXX_USE_CXX11_ABI || ! _GLIBCXX_USE_DUAL_ABI
2266 template<typename _Duration, typename _TimeZonePtr, __format::__char _CharT>
2267 struct formatter<chrono::zoned_time<_Duration, _TimeZonePtr>, _CharT>
2268 : formatter<chrono::__detail::__local_time_fmt_for<_Duration>, _CharT>
2269 {
2270 template<typename _Out>
2271 typename basic_format_context<_Out, _CharT>::iterator
2272 format(const chrono::zoned_time<_Duration, _TimeZonePtr>& __tp,
2273 basic_format_context<_Out, _CharT>& __fc) const
2274 {
2275 using _Ltf = chrono::__detail::__local_time_fmt_for<_Duration>;
2276 using _Base = formatter<_Ltf, _CharT>;
2277 const chrono::sys_info __info = __tp.get_info();
2278 const auto __lf = chrono::local_time_format(__tp.get_local_time(),
2279 &__info.abbrev,
2280 &__info.offset);
2281 return _Base::format(__lf, __fc);
2282 }
2283 };
2284#endif
2285
2286 // Partial specialization needed for %c formatting of __utc_leap_second.
2287 template<typename _Duration, __format::__char _CharT>
2288 struct formatter<chrono::__detail::__utc_leap_second<_Duration>, _CharT>
2289 : formatter<chrono::utc_time<_Duration>, _CharT>
2290 {
2291 template<typename _Out>
2292 typename basic_format_context<_Out, _CharT>::iterator
2293 format(const chrono::__detail::__utc_leap_second<_Duration>& __t,
2294 basic_format_context<_Out, _CharT>& __fc) const
2295 { return this->_M_f._M_format(__t, __fc); }
2296 };
2297
2298namespace chrono
2299{
2300/// @addtogroup chrono
2301/// @{
2302
2303/// @cond undocumented
2304namespace __detail
2305{
2306 template<typename _Duration = seconds>
2307 struct _Parser
2308 {
2309 static_assert(is_same_v<common_type_t<_Duration, seconds>, _Duration>);
2310
2311 explicit
2312 _Parser(__format::_ChronoParts __need) : _M_need(__need) { }
2313
2314 _Parser(_Parser&&) = delete;
2315 void operator=(_Parser&&) = delete;
2316
2317 _Duration _M_time{}; // since midnight
2318 sys_days _M_sys_days{};
2319 year_month_day _M_ymd{};
2320 weekday _M_wd{};
2321 __format::_ChronoParts _M_need;
2322 unsigned _M_is_leap_second : 1 {};
2323 unsigned _M_reserved : 15 {};
2324
2325 template<typename _CharT, typename _Traits, typename _Alloc>
2326 basic_istream<_CharT, _Traits>&
2327 operator()(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt,
2328 basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr,
2329 minutes* __offset = nullptr);
2330
2331 private:
2332 // Read an unsigned integer from the stream and return it.
2333 // Extract no more than __n digits. Set failbit if an integer isn't read.
2334 template<typename _CharT, typename _Traits>
2335 static int_least32_t
2336 _S_read_unsigned(basic_istream<_CharT, _Traits>& __is,
2337 ios_base::iostate& __err, int __n)
2338 {
2339 int_least32_t __val = _S_try_read_digit(__is, __err);
2340 if (__val == -1) [[unlikely]]
2341 __err |= ios_base::failbit;
2342 else
2343 {
2344 int __n1 = (std::min)(__n, 9);
2345 // Cannot overflow __val unless we read more than 9 digits
2346 for (int __i = 1; __i < __n1; ++__i)
2347 if (auto __dig = _S_try_read_digit(__is, __err); __dig != -1)
2348 {
2349 __val *= 10;
2350 __val += __dig;
2351 }
2352
2353 while (__n1++ < __n) [[unlikely]]
2354 if (auto __dig = _S_try_read_digit(__is, __err); __dig != -1)
2355 {
2356 if (__builtin_mul_overflow(__val, 10, &__val)
2357 || __builtin_add_overflow(__val, __dig, &__val))
2358 {
2359 __err |= ios_base::failbit;
2360 return -1;
2361 }
2362 }
2363 }
2364 return __val;
2365 }
2366
2367 // Read an unsigned integer from the stream and return it.
2368 // Extract no more than __n digits. Set failbit if an integer isn't read.
2369 template<typename _CharT, typename _Traits>
2370 static int_least32_t
2371 _S_read_signed(basic_istream<_CharT, _Traits>& __is,
2372 ios_base::iostate& __err, int __n)
2373 {
2374 auto __sign = __is.peek();
2375 if (__sign == '-' || __sign == '+')
2376 (void) __is.get();
2377 int_least32_t __val = _S_read_unsigned(__is, __err, __n);
2378 if (__err & ios_base::failbit)
2379 {
2380 if (__sign == '-') [[unlikely]]
2381 __val *= -1;
2382 }
2383 return __val;
2384 }
2385
2386 // Read a digit from the stream and return it, or return -1.
2387 // If no digit is read eofbit will be set (but not failbit).
2388 template<typename _CharT, typename _Traits>
2389 static int_least32_t
2390 _S_try_read_digit(basic_istream<_CharT, _Traits>& __is,
2391 ios_base::iostate& __err)
2392 {
2393 int_least32_t __val = -1;
2394 auto __i = __is.peek();
2395 if (!_Traits::eq_int_type(__i, _Traits::eof())) [[likely]]
2396 {
2397 _CharT __c = _Traits::to_char_type(__i);
2398 if (_CharT('0') <= __c && __c <= _CharT('9')) [[likely]]
2399 {
2400 (void) __is.get();
2401 __val = __c - _CharT('0');
2402 }
2403 }
2404 else
2405 __err |= ios_base::eofbit;
2406 return __val;
2407 }
2408
2409 // Read the specified character and return true.
2410 // If the character is not found, set failbit and return false.
2411 template<typename _CharT, typename _Traits>
2412 static bool
2413 _S_read_chr(basic_istream<_CharT, _Traits>& __is,
2414 ios_base::iostate& __err, _CharT __c)
2415 {
2416 auto __i = __is.peek();
2417 if (_Traits::eq_int_type(__i, _Traits::eof()))
2418 __err |= ios_base::eofbit;
2419 else if (_Traits::to_char_type(__i) == __c) [[likely]]
2420 {
2421 (void) __is.get();
2422 return true;
2423 }
2424 __err |= ios_base::failbit;
2425 return false;
2426 }
2427 };
2428
2429 template<typename _Duration>
2430 using _Parser_t = _Parser<common_type_t<_Duration, seconds>>;
2431
2432 template<typename _Duration>
2433 consteval bool
2434 __use_floor()
2435 {
2436 if constexpr (_Duration::period::den == 1)
2437 {
2438 switch (_Duration::period::num)
2439 {
2440 case minutes::period::num:
2441 case hours::period::num:
2442 case days::period::num:
2443 case weeks::period::num:
2444 case years::period::num:
2445 return true;
2446 }
2447 }
2448 return false;
2449 }
2450
2451 // A "do the right thing" rounding function for duration and time_point
2452 // values extracted by from_stream. When treat_as_floating_point is true
2453 // we don't want to do anything, just a straightforward conversion.
2454 // When the destination type has a period of minutes, hours, days, weeks,
2455 // or years, we use chrono::floor to truncate towards negative infinity.
2456 // This ensures that an extracted timestamp such as 2024-09-05 13:00:00
2457 // will produce 2024-09-05 when rounded to days, rather than rounding up
2458 // to 2024-09-06 (a different day).
2459 // Otherwise, use chrono::round to get the nearest value representable
2460 // in the destination type.
2461 template<typename _ToDur, typename _Tp>
2462 constexpr auto
2463 __round(const _Tp& __t)
2464 {
2465 if constexpr (__is_duration_v<_Tp>)
2466 {
2467 if constexpr (treat_as_floating_point_v<typename _Tp::rep>)
2468 return chrono::duration_cast<_ToDur>(__t);
2469 else if constexpr (__detail::__use_floor<_ToDur>())
2470 return chrono::floor<_ToDur>(__t);
2471 else
2472 return chrono::round<_ToDur>(__t);
2473 }
2474 else
2475 {
2476 static_assert(__is_time_point_v<_Tp>);
2477 using _Tpt = time_point<typename _Tp::clock, _ToDur>;
2478 return _Tpt(__detail::__round<_ToDur>(__t.time_since_epoch()));
2479 }
2480 }
2481
2482} // namespace __detail
2483/// @endcond
2484
2485 template<typename _CharT, typename _Traits, typename _Rep, typename _Period,
2486 typename _Alloc = allocator<_CharT>>
2487 inline basic_istream<_CharT, _Traits>&
2488 from_stream(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt,
2489 duration<_Rep, _Period>& __d,
2490 basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr,
2491 minutes* __offset = nullptr)
2492 {
2493 auto __need = __format::_ChronoParts::_TimeOfDay;
2494 __detail::_Parser_t<duration<_Rep, _Period>> __p(__need);
2495 if (__p(__is, __fmt, __abbrev, __offset))
2496 __d = __detail::__round<duration<_Rep, _Period>>(__p._M_time);
2497 return __is;
2498 }
2499
2500 template<typename _CharT, typename _Traits>
2501 inline basic_ostream<_CharT, _Traits>&
2502 operator<<(basic_ostream<_CharT, _Traits>& __os, const day& __d)
2503 {
2504 using _Ctx = __format::__format_context<_CharT>;
2505 using _Str = basic_string_view<_CharT>;
2506 _Str __s = _GLIBCXX_WIDEN("{:02d} is not a valid day");
2507 if (__d.ok())
2508 __s = __s.substr(0, 6);
2509 auto __u = (unsigned)__d;
2510 __os << std::vformat(__s, make_format_args<_Ctx>(__u));
2511 return __os;
2512 }
2513
2514 template<typename _CharT, typename _Traits,
2515 typename _Alloc = allocator<_CharT>>
2516 inline basic_istream<_CharT, _Traits>&
2517 from_stream(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt,
2518 day& __d,
2519 basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr,
2520 minutes* __offset = nullptr)
2521 {
2522 __detail::_Parser<> __p(__format::_ChronoParts::_Day);
2523 if (__p(__is, __fmt, __abbrev, __offset))
2524 __d = __p._M_ymd.day();
2525 return __is;
2526 }
2527
2528 template<typename _CharT, typename _Traits>
2529 inline basic_ostream<_CharT, _Traits>&
2530 operator<<(basic_ostream<_CharT, _Traits>& __os, const month& __m)
2531 {
2532 using _Ctx = __format::__format_context<_CharT>;
2533 using _Str = basic_string_view<_CharT>;
2534 _Str __s = _GLIBCXX_WIDEN("{:L%b}{} is not a valid month");
2535 if (__m.ok())
2536 __os << std::vformat(__os.getloc(), __s.substr(0, 6),
2537 make_format_args<_Ctx>(__m));
2538 else
2539 {
2540 auto __u = (unsigned)__m;
2541 __os << std::vformat(__s.substr(6), make_format_args<_Ctx>(__u));
2542 }
2543 return __os;
2544 }
2545
2546 template<typename _CharT, typename _Traits,
2547 typename _Alloc = allocator<_CharT>>
2548 inline basic_istream<_CharT, _Traits>&
2549 from_stream(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt,
2550 month& __m,
2551 basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr,
2552 minutes* __offset = nullptr)
2553 {
2554 __detail::_Parser<> __p(__format::_ChronoParts::_Month);
2555 if (__p(__is, __fmt, __abbrev, __offset))
2556 __m = __p._M_ymd.month();
2557 return __is;
2558 }
2559
2560 template<typename _CharT, typename _Traits>
2561 inline basic_ostream<_CharT, _Traits>&
2562 operator<<(basic_ostream<_CharT, _Traits>& __os, const year& __y)
2563 {
2564 using _Ctx = __format::__format_context<_CharT>;
2565 using _Str = basic_string_view<_CharT>;
2566 _Str __s = _GLIBCXX_WIDEN("-{:04d} is not a valid year");
2567 if (__y.ok())
2568 __s = __s.substr(0, 7);
2569 int __i = (int)__y;
2570 if (__i >= 0) [[likely]]
2571 __s.remove_prefix(1);
2572 else
2573 __i = -__i;
2574 __os << std::vformat(__s, make_format_args<_Ctx>(__i));
2575 return __os;
2576 }
2577
2578 template<typename _CharT, typename _Traits,
2579 typename _Alloc = allocator<_CharT>>
2580 inline basic_istream<_CharT, _Traits>&
2581 from_stream(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt,
2582 year& __y,
2583 basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr,
2584 minutes* __offset = nullptr)
2585 {
2586 __detail::_Parser<> __p(__format::_ChronoParts::_Year);
2587 if (__p(__is, __fmt, __abbrev, __offset))
2588 __y = __p._M_ymd.year();
2589 return __is;
2590 }
2591
2592 template<typename _CharT, typename _Traits>
2593 inline basic_ostream<_CharT, _Traits>&
2594 operator<<(basic_ostream<_CharT, _Traits>& __os, const weekday& __wd)
2595 {
2596 using _Ctx = __format::__format_context<_CharT>;
2597 using _Str = basic_string_view<_CharT>;
2598 _Str __s = _GLIBCXX_WIDEN("{:L%a}{} is not a valid weekday");
2599 if (__wd.ok())
2600 __os << std::vformat(__os.getloc(), __s.substr(0, 6),
2601 make_format_args<_Ctx>(__wd));
2602 else
2603 {
2604 auto __c = __wd.c_encoding();
2605 __os << std::vformat(__s.substr(6), make_format_args<_Ctx>(__c));
2606 }
2607 return __os;
2608 }
2609
2610 template<typename _CharT, typename _Traits,
2611 typename _Alloc = allocator<_CharT>>
2612 inline basic_istream<_CharT, _Traits>&
2613 from_stream(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt,
2614 weekday& __wd,
2615 basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr,
2616 minutes* __offset = nullptr)
2617 {
2618 __detail::_Parser<> __p(__format::_ChronoParts::_Weekday);
2619 if (__p(__is, __fmt, __abbrev, __offset))
2620 __wd = __p._M_wd;
2621 return __is;
2622 }
2623
2624 template<typename _CharT, typename _Traits>
2625 inline basic_ostream<_CharT, _Traits>&
2626 operator<<(basic_ostream<_CharT, _Traits>& __os,
2627 const weekday_indexed& __wdi)
2628 {
2629 // The standard says to format wdi.weekday() and wdi.index() using
2630 // either "{:L}[{}]" or "{:L}[{} is not a valid index]". The {:L} spec
2631 // means to format the weekday using ostringstream, so just do that.
2632 basic_stringstream<_CharT> __os2;
2633 __os2.imbue(__os.getloc());
2634 __os2 << __wdi.weekday();
2635 const auto __i = __wdi.index();
2636 basic_string_view<_CharT> __s
2637 = _GLIBCXX_WIDEN("[ is not a valid index]");
2638 __os2 << __s[0];
2639 __os2 << std::format(_GLIBCXX_WIDEN("{}"), __i);
2640 if (__i >= 1 && __i <= 5)
2641 __os2 << __s.back();
2642 else
2643 __os2 << __s.substr(1);
2644 __os << __os2.view();
2645 return __os;
2646 }
2647
2648 template<typename _CharT, typename _Traits>
2649 inline basic_ostream<_CharT, _Traits>&
2650 operator<<(basic_ostream<_CharT, _Traits>& __os,
2651 const weekday_last& __wdl)
2652 {
2653 // As above, just write straight to a stringstream, as if by "{:L}[last]"
2654 basic_stringstream<_CharT> __os2;
2655 __os2.imbue(__os.getloc());
2656 __os2 << __wdl.weekday() << _GLIBCXX_WIDEN("[last]");
2657 __os << __os2.view();
2658 return __os;
2659 }
2660
2661 template<typename _CharT, typename _Traits>
2662 inline basic_ostream<_CharT, _Traits>&
2663 operator<<(basic_ostream<_CharT, _Traits>& __os, const month_day& __md)
2664 {
2665 // As above, just write straight to a stringstream, as if by "{:L}/{}"
2666 basic_stringstream<_CharT> __os2;
2667 __os2.imbue(__os.getloc());
2668 __os2 << __md.month();
2669 if constexpr (is_same_v<_CharT, char>)
2670 __os2 << '/';
2671 else
2672 __os2 << L'/';
2673 __os2 << __md.day();
2674 __os << __os2.view();
2675 return __os;
2676 }
2677
2678 template<typename _CharT, typename _Traits,
2679 typename _Alloc = allocator<_CharT>>
2680 inline basic_istream<_CharT, _Traits>&
2681 from_stream(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt,
2682 month_day& __md,
2683 basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr,
2684 minutes* __offset = nullptr)
2685 {
2686 using __format::_ChronoParts;
2687 auto __need = _ChronoParts::_Month | _ChronoParts::_Day;
2688 __detail::_Parser<> __p(__need);
2689 if (__p(__is, __fmt, __abbrev, __offset))
2690 __md = month_day(__p._M_ymd.month(), __p._M_ymd.day());
2691 return __is;
2692 }
2693
2694 template<typename _CharT, typename _Traits>
2695 inline basic_ostream<_CharT, _Traits>&
2696 operator<<(basic_ostream<_CharT, _Traits>& __os,
2697 const month_day_last& __mdl)
2698 {
2699 // As above, just write straight to a stringstream, as if by "{:L}/last"
2700 basic_stringstream<_CharT> __os2;
2701 __os2.imbue(__os.getloc());
2702 __os2 << __mdl.month() << _GLIBCXX_WIDEN("/last");
2703 __os << __os2.view();
2704 return __os;
2705 }
2706
2707 template<typename _CharT, typename _Traits>
2708 inline basic_ostream<_CharT, _Traits>&
2709 operator<<(basic_ostream<_CharT, _Traits>& __os,
2710 const month_weekday& __mwd)
2711 {
2712 // As above, just write straight to a stringstream, as if by "{:L}/{:L}"
2713 basic_stringstream<_CharT> __os2;
2714 __os2.imbue(__os.getloc());
2715 __os2 << __mwd.month();
2716 if constexpr (is_same_v<_CharT, char>)
2717 __os2 << '/';
2718 else
2719 __os2 << L'/';
2720 __os2 << __mwd.weekday_indexed();
2721 __os << __os2.view();
2722 return __os;
2723 }
2724
2725 template<typename _CharT, typename _Traits>
2726 inline basic_ostream<_CharT, _Traits>&
2727 operator<<(basic_ostream<_CharT, _Traits>& __os,
2728 const month_weekday_last& __mwdl)
2729 {
2730 // As above, just write straight to a stringstream, as if by "{:L}/{:L}"
2731 basic_stringstream<_CharT> __os2;
2732 __os2.imbue(__os.getloc());
2733 __os2 << __mwdl.month();
2734 if constexpr (is_same_v<_CharT, char>)
2735 __os2 << '/';
2736 else
2737 __os2 << L'/';
2738 __os2 << __mwdl.weekday_last();
2739 __os << __os2.view();
2740 return __os;
2741 }
2742
2743 template<typename _CharT, typename _Traits>
2744 inline basic_ostream<_CharT, _Traits>&
2745 operator<<(basic_ostream<_CharT, _Traits>& __os, const year_month& __ym)
2746 {
2747 // As above, just write straight to a stringstream, as if by "{}/{:L}"
2748 basic_stringstream<_CharT> __os2;
2749 __os2.imbue(__os.getloc());
2750 __os2 << __ym.year();
2751 if constexpr (is_same_v<_CharT, char>)
2752 __os2 << '/';
2753 else
2754 __os2 << L'/';
2755 __os2 << __ym.month();
2756 __os << __os2.view();
2757 return __os;
2758 }
2759
2760 template<typename _CharT, typename _Traits,
2761 typename _Alloc = allocator<_CharT>>
2762 inline basic_istream<_CharT, _Traits>&
2763 from_stream(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt,
2764 year_month& __ym,
2765 basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr,
2766 minutes* __offset = nullptr)
2767 {
2768 using __format::_ChronoParts;
2769 auto __need = _ChronoParts::_Year | _ChronoParts::_Month;
2770 __detail::_Parser<> __p(__need);
2771 if (__p(__is, __fmt, __abbrev, __offset))
2772 __ym = year_month(__p._M_ymd.year(), __p._M_ymd.month());
2773 return __is;
2774 }
2775
2776 template<typename _CharT, typename _Traits>
2777 inline basic_ostream<_CharT, _Traits>&
2778 operator<<(basic_ostream<_CharT, _Traits>& __os,
2779 const year_month_day& __ymd)
2780 {
2781 using _Ctx = __format::__format_context<_CharT>;
2782 using _Str = basic_string_view<_CharT>;
2783 _Str __s = _GLIBCXX_WIDEN("{:%F} is not a valid date");
2784 __os << std::vformat(__ymd.ok() ? __s.substr(0, 5) : __s,
2785 make_format_args<_Ctx>(__ymd));
2786 return __os;
2787 }
2788
2789 template<typename _CharT, typename _Traits,
2790 typename _Alloc = allocator<_CharT>>
2791 inline basic_istream<_CharT, _Traits>&
2792 from_stream(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt,
2793 year_month_day& __ymd,
2795 minutes* __offset = nullptr)
2796 {
2797 using __format::_ChronoParts;
2798 auto __need = _ChronoParts::_Year | _ChronoParts::_Month
2799 | _ChronoParts::_Day;
2800 __detail::_Parser<> __p(__need);
2801 if (__p(__is, __fmt, __abbrev, __offset))
2802 __ymd = __p._M_ymd;
2803 return __is;
2804 }
2805
2806 template<typename _CharT, typename _Traits>
2809 const year_month_day_last& __ymdl)
2810 {
2811 // As above, just write straight to a stringstream, as if by "{}/{:L}"
2813 __os2.imbue(__os.getloc());
2814 __os2 << __ymdl.year();
2815 if constexpr (is_same_v<_CharT, char>)
2816 __os2 << '/';
2817 else
2818 __os2 << L'/';
2819 __os2 << __ymdl.month_day_last();
2820 __os << __os2.view();
2821 return __os;
2822 }
2823
2824 template<typename _CharT, typename _Traits>
2825 inline basic_ostream<_CharT, _Traits>&
2826 operator<<(basic_ostream<_CharT, _Traits>& __os,
2827 const year_month_weekday& __ymwd)
2828 {
2829 // As above, just write straight to a stringstream, as if by
2830 // "{}/{:L}/{:L}"
2831 basic_stringstream<_CharT> __os2;
2832 __os2.imbue(__os.getloc());
2833 _CharT __slash;
2834 if constexpr (is_same_v<_CharT, char>)
2835 __slash = '/';
2836 else
2837 __slash = L'/';
2838 __os2 << __ymwd.year() << __slash << __ymwd.month() << __slash
2839 << __ymwd.weekday_indexed();
2840 __os << __os2.view();
2841 return __os;
2842 }
2843
2844 template<typename _CharT, typename _Traits>
2845 inline basic_ostream<_CharT, _Traits>&
2846 operator<<(basic_ostream<_CharT, _Traits>& __os,
2847 const year_month_weekday_last& __ymwdl)
2848 {
2849 // As above, just write straight to a stringstream, as if by
2850 // "{}/{:L}/{:L}"
2851 basic_stringstream<_CharT> __os2;
2852 __os2.imbue(__os.getloc());
2853 _CharT __slash;
2854 if constexpr (is_same_v<_CharT, char>)
2855 __slash = '/';
2856 else
2857 __slash = L'/';
2858 __os2 << __ymwdl.year() << __slash << __ymwdl.month() << __slash
2859 << __ymwdl.weekday_last();
2860 __os << __os2.view();
2861 return __os;
2862 }
2863
2864 template<typename _CharT, typename _Traits, typename _Duration>
2865 inline basic_ostream<_CharT, _Traits>&
2866 operator<<(basic_ostream<_CharT, _Traits>& __os,
2867 const hh_mm_ss<_Duration>& __hms)
2868 {
2869 return __os << format(__os.getloc(), _GLIBCXX_WIDEN("{:L%T}"), __hms);
2870 }
2871
2872#if _GLIBCXX_USE_CXX11_ABI || ! _GLIBCXX_USE_DUAL_ABI
2873 /// Writes a sys_info object to an ostream in an unspecified format.
2874 template<typename _CharT, typename _Traits>
2875 basic_ostream<_CharT, _Traits>&
2876 operator<<(basic_ostream<_CharT, _Traits>& __os, const sys_info& __i)
2877 {
2878 __os << '[' << __i.begin << ',' << __i.end
2879 << ',' << hh_mm_ss(__i.offset) << ',' << __i.save
2880 << ',' << __i.abbrev << ']';
2881 return __os;
2882 }
2883
2884 /// Writes a local_info object to an ostream in an unspecified format.
2885 template<typename _CharT, typename _Traits>
2886 basic_ostream<_CharT, _Traits>&
2887 operator<<(basic_ostream<_CharT, _Traits>& __os, const local_info& __li)
2888 {
2889 __os << '[';
2890 if (__li.result == local_info::unique)
2891 __os << __li.first;
2892 else
2893 {
2894 if (__li.result == local_info::nonexistent)
2895 __os << "nonexistent";
2896 else
2897 __os << "ambiguous";
2898 __os << " local time between " << __li.first;
2899 __os << " and " << __li.second;
2900 }
2901 __os << ']';
2902 return __os;
2903 }
2904
2905 template<typename _CharT, typename _Traits, typename _Duration,
2906 typename _TimeZonePtr>
2907 inline basic_ostream<_CharT, _Traits>&
2908 operator<<(basic_ostream<_CharT, _Traits>& __os,
2909 const zoned_time<_Duration, _TimeZonePtr>& __t)
2910 {
2911 __os << format(__os.getloc(), _GLIBCXX_WIDEN("{:L%F %T %Z}"), __t);
2912 return __os;
2913 }
2914#endif
2915
2916 template<typename _CharT, typename _Traits, typename _Duration>
2917 requires (!treat_as_floating_point_v<typename _Duration::rep>)
2918 && ratio_less_v<typename _Duration::period, days::period>
2919 inline basic_ostream<_CharT, _Traits>&
2920 operator<<(basic_ostream<_CharT, _Traits>& __os,
2921 const sys_time<_Duration>& __tp)
2922 {
2923 __os << std::format(__os.getloc(), _GLIBCXX_WIDEN("{:L%F %T}"), __tp);
2924 return __os;
2925 }
2926
2927 template<typename _CharT, typename _Traits>
2928 inline basic_ostream<_CharT, _Traits>&
2929 operator<<(basic_ostream<_CharT, _Traits>& __os, const sys_days& __dp)
2930 {
2931 __os << year_month_day{__dp};
2932 return __os;
2933 }
2934
2935 template<typename _CharT, typename _Traits, typename _Duration,
2936 typename _Alloc = allocator<_CharT>>
2937 basic_istream<_CharT, _Traits>&
2938 from_stream(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt,
2939 sys_time<_Duration>& __tp,
2940 basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr,
2941 minutes* __offset = nullptr)
2942 {
2943 minutes __off{};
2944 if (!__offset)
2945 __offset = &__off;
2946 using __format::_ChronoParts;
2947 auto __need = _ChronoParts::_Year | _ChronoParts::_Month
2948 | _ChronoParts::_Day | _ChronoParts::_TimeOfDay;
2949 __detail::_Parser_t<_Duration> __p(__need);
2950 if (__p(__is, __fmt, __abbrev, __offset))
2951 {
2952 if (__p._M_is_leap_second)
2953 __is.setstate(ios_base::failbit);
2954 else
2955 {
2956 auto __st = __p._M_sys_days + __p._M_time - *__offset;
2957 __tp = __detail::__round<_Duration>(__st);
2958 }
2959 }
2960 return __is;
2961 }
2962
2963 template<typename _CharT, typename _Traits, typename _Duration>
2964 inline basic_ostream<_CharT, _Traits>&
2965 operator<<(basic_ostream<_CharT, _Traits>& __os,
2966 const utc_time<_Duration>& __t)
2967 {
2968 __os << std::format(__os.getloc(), _GLIBCXX_WIDEN("{:L%F %T}"), __t);
2969 return __os;
2970 }
2971
2972 template<typename _CharT, typename _Traits, typename _Duration,
2973 typename _Alloc = allocator<_CharT>>
2974 inline basic_istream<_CharT, _Traits>&
2975 from_stream(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt,
2976 utc_time<_Duration>& __tp,
2977 basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr,
2978 minutes* __offset = nullptr)
2979 {
2980 minutes __off{};
2981 if (!__offset)
2982 __offset = &__off;
2983 using __format::_ChronoParts;
2984 auto __need = _ChronoParts::_Year | _ChronoParts::_Month
2985 | _ChronoParts::_Day | _ChronoParts::_TimeOfDay;
2986 __detail::_Parser_t<_Duration> __p(__need);
2987 if (__p(__is, __fmt, __abbrev, __offset))
2988 {
2989 // Converting to utc_time before adding _M_time is necessary for
2990 // "23:59:60" to correctly produce a time within a leap second.
2991 auto __ut = utc_clock::from_sys(__p._M_sys_days) + __p._M_time
2992 - *__offset;
2993 __tp = __detail::__round<_Duration>(__ut);
2994 }
2995 return __is;
2996 }
2997
2998 template<typename _CharT, typename _Traits, typename _Duration>
2999 inline basic_ostream<_CharT, _Traits>&
3000 operator<<(basic_ostream<_CharT, _Traits>& __os,
3001 const tai_time<_Duration>& __t)
3002 {
3003 __os << std::format(__os.getloc(), _GLIBCXX_WIDEN("{:L%F %T}"), __t);
3004 return __os;
3005 }
3006
3007 template<typename _CharT, typename _Traits, typename _Duration,
3008 typename _Alloc = allocator<_CharT>>
3009 inline basic_istream<_CharT, _Traits>&
3010 from_stream(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt,
3011 tai_time<_Duration>& __tp,
3012 basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr,
3013 minutes* __offset = nullptr)
3014 {
3015 minutes __off{};
3016 if (!__offset)
3017 __offset = &__off;
3018 using __format::_ChronoParts;
3019 auto __need = _ChronoParts::_Year | _ChronoParts::_Month
3020 | _ChronoParts::_Day | _ChronoParts::_TimeOfDay;
3021 __detail::_Parser_t<_Duration> __p(__need);
3022 if (__p(__is, __fmt, __abbrev, __offset))
3023 {
3024 if (__p._M_is_leap_second)
3025 __is.setstate(ios_base::failbit);
3026 else
3027 {
3028 constexpr sys_days __epoch(-days(4383)); // 1958y/1/1
3029 auto __d = __p._M_sys_days - __epoch + __p._M_time - *__offset;
3030 tai_time<common_type_t<_Duration, seconds>> __tt(__d);
3031 __tp = __detail::__round<_Duration>(__tt);
3032 }
3033 }
3034 return __is;
3035 }
3036
3037 template<typename _CharT, typename _Traits, typename _Duration>
3038 inline basic_ostream<_CharT, _Traits>&
3039 operator<<(basic_ostream<_CharT, _Traits>& __os,
3040 const gps_time<_Duration>& __t)
3041 {
3042 __os << std::format(__os.getloc(), _GLIBCXX_WIDEN("{:L%F %T}"), __t);
3043 return __os;
3044 }
3045
3046 template<typename _CharT, typename _Traits, typename _Duration,
3047 typename _Alloc = allocator<_CharT>>
3048 inline basic_istream<_CharT, _Traits>&
3049 from_stream(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt,
3050 gps_time<_Duration>& __tp,
3051 basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr,
3052 minutes* __offset = nullptr)
3053 {
3054 minutes __off{};
3055 if (!__offset)
3056 __offset = &__off;
3057 using __format::_ChronoParts;
3058 auto __need = _ChronoParts::_Year | _ChronoParts::_Month
3059 | _ChronoParts::_Day | _ChronoParts::_TimeOfDay;
3060 __detail::_Parser_t<_Duration> __p(__need);
3061 if (__p(__is, __fmt, __abbrev, __offset))
3062 {
3063 if (__p._M_is_leap_second)
3064 __is.setstate(ios_base::failbit);
3065 else
3066 {
3067 constexpr sys_days __epoch(days(3657)); // 1980y/1/Sunday[1]
3068 auto __d = __p._M_sys_days - __epoch + __p._M_time - *__offset;
3069 gps_time<common_type_t<_Duration, seconds>> __gt(__d);
3070 __tp = __detail::__round<_Duration>(__gt);
3071 }
3072 }
3073 return __is;
3074 }
3075
3076 template<typename _CharT, typename _Traits, typename _Duration>
3077 inline basic_ostream<_CharT, _Traits>&
3078 operator<<(basic_ostream<_CharT, _Traits>& __os,
3079 const file_time<_Duration>& __t)
3080 {
3081 __os << std::format(__os.getloc(), _GLIBCXX_WIDEN("{:L%F %T}"), __t);
3082 return __os;
3083 }
3084
3085 template<typename _CharT, typename _Traits, typename _Duration,
3086 typename _Alloc = allocator<_CharT>>
3087 inline basic_istream<_CharT, _Traits>&
3088 from_stream(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt,
3089 file_time<_Duration>& __tp,
3090 basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr,
3091 minutes* __offset = nullptr)
3092 {
3093 sys_time<_Duration> __st;
3094 if (chrono::from_stream(__is, __fmt, __st, __abbrev, __offset))
3095 __tp = __detail::__round<_Duration>(file_clock::from_sys(__st));
3096 return __is;
3097 }
3098
3099 template<typename _CharT, typename _Traits, typename _Duration>
3100 inline basic_ostream<_CharT, _Traits>&
3101 operator<<(basic_ostream<_CharT, _Traits>& __os,
3102 const local_time<_Duration>& __lt)
3103 {
3104 __os << sys_time<_Duration>{__lt.time_since_epoch()};
3105 return __os;
3106 }
3107
3108 template<typename _CharT, typename _Traits, typename _Duration,
3109 typename _Alloc = allocator<_CharT>>
3110 basic_istream<_CharT, _Traits>&
3111 from_stream(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt,
3112 local_time<_Duration>& __tp,
3113 basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr,
3114 minutes* __offset = nullptr)
3115 {
3116 using __format::_ChronoParts;
3117 auto __need = _ChronoParts::_Year | _ChronoParts::_Month
3118 | _ChronoParts::_Day | _ChronoParts::_TimeOfDay;
3119 __detail::_Parser_t<_Duration> __p(__need);
3120 if (__p(__is, __fmt, __abbrev, __offset))
3121 {
3122 days __d = __p._M_sys_days.time_since_epoch();
3123 auto __t = local_days(__d) + __p._M_time; // ignore offset
3124 __tp = __detail::__round<_Duration>(__t);
3125 }
3126 return __is;
3127 }
3128
3129 // [time.parse] parsing
3130
3131namespace __detail
3132{
3133 // _GLIBCXX_RESOLVE_LIB_DEFECTS
3134 // 3956. chrono::parse uses from_stream as a customization point
3135 void from_stream() = delete;
3136
3137 template<typename _Parsable, typename _CharT,
3138 typename _Traits = std::char_traits<_CharT>,
3139 typename... _OptArgs>
3140 concept __parsable = requires (basic_istream<_CharT, _Traits>& __is,
3141 const _CharT* __fmt, _Parsable& __tp,
3142 _OptArgs*... __args)
3143 { from_stream(__is, __fmt, __tp, __args...); };
3144
3145 template<typename _Parsable, typename _CharT,
3146 typename _Traits = char_traits<_CharT>,
3147 typename _Alloc = allocator<_CharT>>
3148 struct _Parse
3149 {
3150 private:
3151 using __string_type = basic_string<_CharT, _Traits, _Alloc>;
3152
3153 public:
3154 _Parse(const _CharT* __fmt, _Parsable& __tp,
3155 basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr,
3156 minutes* __offset = nullptr)
3157 : _M_fmt(__fmt), _M_tp(std::__addressof(__tp)),
3158 _M_abbrev(__abbrev), _M_offset(__offset)
3159 { }
3160
3161 _Parse(_Parse&&) = delete;
3162 _Parse& operator=(_Parse&&) = delete;
3163
3164 private:
3165 using __stream_type = basic_istream<_CharT, _Traits>;
3166
3167 const _CharT* const _M_fmt;
3168 _Parsable* const _M_tp;
3169 __string_type* const _M_abbrev;
3170 minutes* const _M_offset;
3171
3172 friend __stream_type&
3173 operator>>(__stream_type& __is, _Parse&& __p)
3174 {
3175 if (__p._M_offset)
3176 from_stream(__is, __p._M_fmt, *__p._M_tp, __p._M_abbrev,
3177 __p._M_offset);
3178 else if (__p._M_abbrev)
3179 from_stream(__is, __p._M_fmt, *__p._M_tp, __p._M_abbrev);
3180 else
3181 from_stream(__is, __p._M_fmt, *__p._M_tp);
3182 return __is;
3183 }
3184
3185 friend void operator>>(__stream_type&, _Parse&) = delete;
3186 friend void operator>>(__stream_type&, const _Parse&) = delete;
3187 };
3188} // namespace __detail
3189
3190 template<typename _CharT, __detail::__parsable<_CharT> _Parsable>
3191 [[nodiscard, __gnu__::__access__(__read_only__, 1)]]
3192 inline auto
3193 parse(const _CharT* __fmt, _Parsable& __tp)
3194 { return __detail::_Parse<_Parsable, _CharT>(__fmt, __tp); }
3195
3196 template<typename _CharT, typename _Traits, typename _Alloc,
3197 __detail::__parsable<_CharT, _Traits> _Parsable>
3198 [[nodiscard]]
3199 inline auto
3200 parse(const basic_string<_CharT, _Traits, _Alloc>& __fmt, _Parsable& __tp)
3201 {
3202 return __detail::_Parse<_Parsable, _CharT, _Traits>(__fmt.c_str(), __tp);
3203 }
3204
3205 template<typename _CharT, typename _Traits, typename _Alloc,
3206 typename _StrT = basic_string<_CharT, _Traits, _Alloc>,
3207 __detail::__parsable<_CharT, _Traits, _StrT> _Parsable>
3208 [[nodiscard, __gnu__::__access__(__read_only__, 1)]]
3209 inline auto
3210 parse(const _CharT* __fmt, _Parsable& __tp,
3211 basic_string<_CharT, _Traits, _Alloc>& __abbrev)
3212 {
3213 auto __pa = std::__addressof(__abbrev);
3214 return __detail::_Parse<_Parsable, _CharT, _Traits, _Alloc>(__fmt, __tp,
3215 __pa);
3216 }
3217
3218 template<typename _CharT, typename _Traits, typename _Alloc,
3219 typename _StrT = basic_string<_CharT, _Traits, _Alloc>,
3220 __detail::__parsable<_CharT, _Traits, _StrT> _Parsable>
3221 [[nodiscard]]
3222 inline auto
3223 parse(const basic_string<_CharT, _Traits, _Alloc>& __fmt, _Parsable& __tp,
3224 basic_string<_CharT, _Traits, _Alloc>& __abbrev)
3225 {
3226 auto __pa = std::__addressof(__abbrev);
3227 return __detail::_Parse<_Parsable, _CharT, _Traits, _Alloc>(__fmt.c_str(),
3228 __tp, __pa);
3229 }
3230
3231 template<typename _CharT, typename _Traits = char_traits<_CharT>,
3232 typename _StrT = basic_string<_CharT, _Traits>,
3233 __detail::__parsable<_CharT, _Traits, _StrT, minutes> _Parsable>
3234 [[nodiscard, __gnu__::__access__(__read_only__, 1)]]
3235 inline auto
3236 parse(const _CharT* __fmt, _Parsable& __tp, minutes& __offset)
3237 {
3238 return __detail::_Parse<_Parsable, _CharT>(__fmt, __tp, nullptr,
3239 &__offset);
3240 }
3241
3242 template<typename _CharT, typename _Traits, typename _Alloc,
3243 typename _StrT = basic_string<_CharT, _Traits>,
3244 __detail::__parsable<_CharT, _Traits, _StrT, minutes> _Parsable>
3245 [[nodiscard]]
3246 inline auto
3247 parse(const basic_string<_CharT, _Traits, _Alloc>& __fmt, _Parsable& __tp,
3248 minutes& __offset)
3249 {
3250 return __detail::_Parse<_Parsable, _CharT, _Traits, _Alloc>(__fmt.c_str(),
3251 __tp, nullptr,
3252 &__offset);
3253 }
3254
3255 template<typename _CharT, typename _Traits, typename _Alloc,
3256 typename _StrT = basic_string<_CharT, _Traits, _Alloc>,
3257 __detail::__parsable<_CharT, _Traits, _StrT, minutes> _Parsable>
3258 [[nodiscard, __gnu__::__access__(__read_only__, 1)]]
3259 inline auto
3260 parse(const _CharT* __fmt, _Parsable& __tp,
3261 basic_string<_CharT, _Traits, _Alloc>& __abbrev, minutes& __offset)
3262 {
3263 auto __pa = std::__addressof(__abbrev);
3264 return __detail::_Parse<_Parsable, _CharT, _Traits, _Alloc>(__fmt, __tp,
3265 __pa,
3266 &__offset);
3267 }
3268
3269 template<typename _CharT, typename _Traits, typename _Alloc,
3270 typename _StrT = basic_string<_CharT, _Traits, _Alloc>,
3271 __detail::__parsable<_CharT, _Traits, _StrT, minutes> _Parsable>
3272 [[nodiscard]]
3273 inline auto
3274 parse(const basic_string<_CharT, _Traits, _Alloc>& __fmt, _Parsable& __tp,
3275 basic_string<_CharT, _Traits, _Alloc>& __abbrev, minutes& __offset)
3276 {
3277 auto __pa = std::__addressof(__abbrev);
3278 return __detail::_Parse<_Parsable, _CharT, _Traits, _Alloc>(__fmt.c_str(),
3279 __tp, __pa,
3280 &__offset);
3281 }
3282
3283 /// @cond undocumented
3284 template<typename _Duration>
3285 template<typename _CharT, typename _Traits, typename _Alloc>
3286 basic_istream<_CharT, _Traits>&
3287 __detail::_Parser<_Duration>::
3288 operator()(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt,
3289 basic_string<_CharT, _Traits, _Alloc>* __abbrev,
3290 minutes* __offset)
3291 {
3292 using sentry = typename basic_istream<_CharT, _Traits>::sentry;
3294 if (sentry __cerb(__is, true); __cerb)
3295 {
3296 locale __loc = __is.getloc();
3297 auto& __tmget = std::use_facet<std::time_get<_CharT>>(__loc);
3298 auto& __tmpunct = std::use_facet<std::__timepunct<_CharT>>(__loc);
3299
3300 // RAII type to save and restore stream state.
3301 struct _Stream_state
3302 {
3303 explicit
3304 _Stream_state(basic_istream<_CharT, _Traits>& __i)
3305 : _M_is(__i),
3306 _M_flags(__i.flags(ios_base::skipws | ios_base::dec)),
3307 _M_w(__i.width(0))
3308 { }
3309
3310 ~_Stream_state()
3311 {
3312 _M_is.flags(_M_flags);
3313 _M_is.width(_M_w);
3314 }
3315
3316 _Stream_state(_Stream_state&&) = delete;
3317
3318 basic_istream<_CharT, _Traits>& _M_is;
3319 ios_base::fmtflags _M_flags;
3320 streamsize _M_w;
3321 };
3322
3323 auto __is_failed = [](ios_base::iostate __e) {
3324 return static_cast<bool>(__e & ios_base::failbit);
3325 };
3326
3327 // Read an unsigned integer from the stream and return it.
3328 // Extract no more than __n digits. Set __err on error.
3329 auto __read_unsigned = [&] (int __n) {
3330 return _S_read_unsigned(__is, __err, __n);
3331 };
3332
3333 // Read a signed integer from the stream and return it.
3334 // Extract no more than __n digits. Set __err on error.
3335 auto __read_signed = [&] (int __n) {
3336 return _S_read_signed(__is, __err, __n);
3337 };
3338
3339 // Read an expected character from the stream.
3340 auto __read_chr = [&__is, &__err] (_CharT __c) {
3341 return _S_read_chr(__is, __err, __c);
3342 };
3343
3344 using __format::_ChronoParts;
3345 _ChronoParts __parts{};
3346
3347 const year __bad_y = --year::min(); // SHRT_MIN
3348 const month __bad_mon(255);
3349 const day __bad_day(255);
3350 const weekday __bad_wday(255);
3351 const hours __bad_h(-1);
3352 const minutes __bad_min(-9999);
3353 const seconds __bad_sec(-1);
3354
3355 year __y = __bad_y, __yy = __bad_y; // %Y, %yy
3356 year __iso_y = __bad_y, __iso_yy = __bad_y; // %G, %g
3357 month __m = __bad_mon; // %m
3358 day __d = __bad_day; // %d
3359 weekday __wday = __bad_wday; // %a %A %u %w
3360 hours __h = __bad_h, __h12 = __bad_h; // %H, %I
3361 minutes __min = __bad_min; // %M
3362 _Duration __s = __bad_sec; // %S
3363 int __ampm = 0; // %p
3364 int __iso_wk = -1, __sunday_wk = -1, __monday_wk = -1; // %V, %U, %W
3365 int __century = -1; // %C
3366 int __dayofyear = -1; // %j (for non-duration)
3367
3368 minutes __tz_offset = __bad_min;
3369 basic_string<_CharT, _Traits> __tz_abbr;
3370
3371 if ((_M_need & _ChronoParts::_TimeOfDay)
3372 && (_M_need & _ChronoParts::_Year))
3373 {
3374 // For time_points assume "00:00:00" is implicitly present,
3375 // so we don't fail to parse if it's not (PR libstdc++/114240).
3376 // We will still fail to parse if there's no year+month+day.
3377 __h = hours(0);
3378 __parts = _ChronoParts::_TimeOfDay;
3379 }
3380
3381 // bool __is_neg = false; // TODO: how is this handled for parsing?
3382
3383 _CharT __mod{}; // One of 'E' or 'O' or nul.
3384 unsigned __num = 0; // Non-zero for N modifier.
3385 bool __is_flag = false; // True if we're processing a % flag.
3386
3387 constexpr bool __is_floating
3388 = treat_as_floating_point_v<typename _Duration::rep>;
3389
3390 // If an out-of-range value is extracted (e.g. 61min for %M),
3391 // do not set failbit immediately because we might not need it
3392 // (e.g. parsing chrono::year doesn't care about invalid %M values).
3393 // Instead set the variable back to its initial 'bad' state,
3394 // and also set related variables corresponding to the same field
3395 // (e.g. a bad %M value for __min should also reset __h and __s).
3396 // If a valid value is needed later the bad value will cause failure.
3397
3398 // For some fields we don't know the correct range when parsing and
3399 // we have to be liberal in what we accept, e.g. we allow 366 for
3400 // day-of-year because that's valid in leap years, and we allow 31
3401 // for day-of-month. If those values are needed to determine the
3402 // result then we can do a correct range check at the end when we
3403 // know the how many days the relevant year or month actually has.
3404
3405 while (*__fmt)
3406 {
3407 _CharT __c = *__fmt++;
3408 if (!__is_flag)
3409 {
3410 if (__c == '%')
3411 __is_flag = true; // This is the start of a flag.
3412 else if (std::isspace(__c, __loc))
3413 std::ws(__is); // Match zero or more whitespace characters.
3414 else if (!__read_chr(__c)) [[unlikely]]
3415 break; // Failed to match the expected character.
3416
3417 continue; // Process next character in the format string.
3418 }
3419
3420 // Now processing a flag.
3421 switch (__c)
3422 {
3423 case 'a': // Locale's weekday name
3424 case 'A': // (full or abbreviated, matched case-insensitively).
3425 if (__mod || __num) [[unlikely]]
3426 __err = ios_base::failbit;
3427 else
3428 {
3429 struct tm __tm{};
3430 __tmget.get(__is, {}, __is, __err, &__tm,
3431 __fmt - 2, __fmt);
3432 if (!__is_failed(__err))
3433 __wday = weekday(__tm.tm_wday);
3434 }
3435 __parts |= _ChronoParts::_Weekday;
3436 break;
3437
3438 case 'b': // Locale's month name
3439 case 'h': // (full or abbreviated, matched case-insensitively).
3440 case 'B':
3441 if (__mod || __num) [[unlikely]]
3442 __err = ios_base::failbit;
3443 else
3444 {
3445 // strptime behaves differently for %b and %B,
3446 // but chrono::parse says they're equivalent.
3447 // Luckily libstdc++ std::time_get works as needed.
3448 struct tm __tm{};
3449 __tmget.get(__is, {}, __is, __err, &__tm,
3450 __fmt - 2, __fmt);
3451 if (!__is_failed(__err))
3452 __m = month(__tm.tm_mon + 1);
3453 }
3454 __parts |= _ChronoParts::_Month;
3455 break;
3456
3457 case 'c': // Locale's date and time representation.
3458 if (__mod == 'O' || __num) [[unlikely]]
3459 __err |= ios_base::failbit;
3460 else
3461 {
3462 struct tm __tm{};
3463 __tmget.get(__is, {}, __is, __err, &__tm,
3464 __fmt - 2 - (__mod == 'E'), __fmt);
3465 if (!__is_failed(__err))
3466 {
3467 __y = year(__tm.tm_year + 1900);
3468 __m = month(__tm.tm_mon + 1);
3469 __d = day(__tm.tm_mday);
3470 __h = hours(__tm.tm_hour);
3471 __min = minutes(__tm.tm_min);
3472 __s = seconds(__tm.tm_sec);
3473 }
3474 }
3475 __parts |= _ChronoParts::_DateTime;
3476 break;
3477
3478 case 'C': // Century
3479 if (!__mod) [[likely]]
3480 {
3481 auto __v = __read_signed(__num ? __num : 2);
3482 if (!__is_failed(__err))
3483 {
3484 int __cmin = (int)year::min() / 100;
3485 int __cmax = (int)year::max() / 100;
3486 if (__cmin <= __v && __v <= __cmax)
3487 __century = __v * 100;
3488 else
3489 __century = -2; // This prevents guessing century.
3490 }
3491 }
3492 else if (__mod == 'E')
3493 {
3494 struct tm __tm{};
3495 __tmget.get(__is, {}, __is, __err, &__tm,
3496 __fmt - 3, __fmt);
3497 if (!__is_failed(__err))
3498 __century = __tm.tm_year;
3499 }
3500 else [[unlikely]]
3501 __err |= ios_base::failbit;
3502 // N.B. don't set this here: __parts |= _ChronoParts::_Year;
3503 break;
3504
3505 case 'd': // Day of month (1-31)
3506 case 'e':
3507 if (!__mod) [[likely]]
3508 {
3509 auto __v = __read_unsigned(__num ? __num : 2);
3510 if (!__is_failed(__err))
3511 __d = day(__v);
3512 }
3513 else if (__mod == 'O')
3514 {
3515 struct tm __tm{};
3516 __tmget.get(__is, {}, __is, __err, &__tm,
3517 __fmt - 3, __fmt);
3518 if (!__is_failed(__err))
3519 __d = day(__tm.tm_mday);
3520 }
3521 else [[unlikely]]
3522 __err |= ios_base::failbit;
3523 __parts |= _ChronoParts::_Day;
3524 break;
3525
3526 case 'D': // %m/%d/%y
3527 if (__mod || __num) [[unlikely]]
3528 __err |= ios_base::failbit;
3529 else
3530 {
3531 auto __month = __read_unsigned(2); // %m
3532 __read_chr('/');
3533 auto __day = __read_unsigned(2); // %d
3534 __read_chr('/');
3535 auto __year = __read_unsigned(2); // %y
3536 if (__is_failed(__err))
3537 break;
3538 __y = year(__year + 1900 + 100 * int(__year < 69));
3539 __m = month(__month);
3540 __d = day(__day);
3541 if (!year_month_day(__y, __m, __d).ok())
3542 {
3543 __y = __yy = __iso_y = __iso_yy = __bad_y;
3544 __m = __bad_mon;
3545 __d = __bad_day;
3546 break;
3547 }
3548 }
3549 __parts |= _ChronoParts::_Date;
3550 break;
3551
3552 case 'F': // %Y-%m-%d - any N modifier only applies to %Y.
3553 if (__mod) [[unlikely]]
3554 __err |= ios_base::failbit;
3555 else
3556 {
3557 auto __year = __read_signed(__num ? __num : 4); // %Y
3558 __read_chr('-');
3559 auto __month = __read_unsigned(2); // %m
3560 __read_chr('-');
3561 auto __day = __read_unsigned(2); // %d
3562 if (__is_failed(__err))
3563 break;
3564 __y = year(__year);
3565 __m = month(__month);
3566 __d = day(__day);
3567 if (!year_month_day(__y, __m, __d).ok())
3568 {
3569 __y = __yy = __iso_y = __iso_yy = __bad_y;
3570 __m = __bad_mon;
3571 __d = __bad_day;
3572 break;
3573 }
3574 }
3575 __parts |= _ChronoParts::_Date;
3576 break;
3577
3578 case 'g': // Last two digits of ISO week-based year.
3579 if (__mod) [[unlikely]]
3580 __err |= ios_base::failbit;
3581 else
3582 {
3583 auto __val = __read_unsigned(__num ? __num : 2);
3584 if (__val >= 0 && __val <= 99)
3585 {
3586 __iso_yy = year(__val);
3587 if (__century == -1) // No %C has been parsed yet.
3588 __century = 2000;
3589 }
3590 else
3591 __iso_yy = __iso_y = __y = __yy = __bad_y;
3592 }
3593 __parts |= _ChronoParts::_Year;
3594 break;
3595
3596 case 'G': // ISO week-based year.
3597 if (__mod) [[unlikely]]
3598 __err |= ios_base::failbit;
3599 else
3600 __iso_y = year(__read_unsigned(__num ? __num : 4));
3601 __parts |= _ChronoParts::_Year;
3602 break;
3603
3604 case 'H': // 24-hour (00-23)
3605 case 'I': // 12-hour (1-12)
3606 if (__mod == 'E') [[unlikely]]
3607 __err |= ios_base::failbit;
3608 else if (__mod == 'O')
3609 {
3610#if 0
3611 struct tm __tm{};
3612 __tm.tm_ampm = 1;
3613 __tmget.get(__is, {}, __is, __err, &__tm,
3614 __fmt - 3, __fmt);
3615 if (!__is_failed(__err))
3616 {
3617 if (__c == 'I')
3618 {
3619 __h12 = hours(__tm.tm_hour);
3620 __h = __bad_h;
3621 }
3622 else
3623 __h = hours(__tm.tm_hour);
3624 }
3625#else
3626 // XXX %OI seems to be unimplementable.
3627 __err |= ios_base::failbit;
3628#endif
3629 }
3630 else
3631 {
3632 auto __val = __read_unsigned(__num ? __num : 2);
3633 if (__c == 'I' && __val >= 1 && __val <= 12)
3634 {
3635 __h12 = hours(__val);
3636 __h = __bad_h;
3637 }
3638 else if (__c == 'H' && __val >= 0 && __val <= 23)
3639 {
3640 __h = hours(__val);
3641 __h12 = __bad_h;
3642 }
3643 else
3644 {
3645 if (_M_need & _ChronoParts::_TimeOfDay)
3646 __err |= ios_base::failbit;
3647 break;
3648 }
3649 }
3650 __parts |= _ChronoParts::_TimeOfDay;
3651 break;
3652
3653 case 'j': // For duration, count of days, otherwise day of year
3654 if (__mod) [[unlikely]]
3655 __err |= ios_base::failbit;
3656 else if (_M_need == _ChronoParts::_TimeOfDay) // duration
3657 {
3658 auto __val = __read_signed(__num ? __num : 3);
3659 if (!__is_failed(__err))
3660 {
3661 __h = days(__val); // __h will get added to _M_time
3662 __parts |= _ChronoParts::_TimeOfDay;
3663 }
3664 }
3665 else
3666 {
3667 __dayofyear = __read_unsigned(__num ? __num : 3);
3668 // N.B. do not alter __parts here, done after loop.
3669 // No need for range checking here either.
3670 }
3671 break;
3672
3673 case 'm': // Month (1-12)
3674 if (__mod == 'E') [[unlikely]]
3675 __err |= ios_base::failbit;
3676 else if (__mod == 'O')
3677 {
3678 struct tm __tm{};
3679 __tmget.get(__is, {}, __is, __err, &__tm,
3680 __fmt - 2, __fmt);
3681 if (!__is_failed(__err))
3682 __m = month(__tm.tm_mon + 1);
3683 }
3684 else
3685 {
3686 auto __val = __read_unsigned(__num ? __num : 2);
3687 if (__val >= 1 && __val <= 12)
3688 __m = month(__val);
3689 else
3690 __m = __bad_mon;
3691 }
3692 __parts |= _ChronoParts::_Month;
3693 break;
3694
3695 case 'M': // Minutes
3696 if (__mod == 'E') [[unlikely]]
3697 __err |= ios_base::failbit;
3698 else if (__mod == 'O')
3699 {
3700 struct tm __tm{};
3701 __tmget.get(__is, {}, __is, __err, &__tm,
3702 __fmt - 2, __fmt);
3703 if (!__is_failed(__err))
3704 __min = minutes(__tm.tm_min);
3705 }
3706 else
3707 {
3708 auto __val = __read_unsigned(__num ? __num : 2);
3709 if (0 <= __val && __val < 60)
3710 __min = minutes(__val);
3711 else
3712 {
3713 if (_M_need & _ChronoParts::_TimeOfDay)
3714 __err |= ios_base::failbit;
3715 break;
3716 }
3717 }
3718 __parts |= _ChronoParts::_TimeOfDay;
3719 break;
3720
3721 case 'p': // Locale's AM/PM designation for 12-hour clock.
3722 if (__mod || __num)
3723 __err |= ios_base::failbit;
3724 else
3725 {
3726 // Can't use std::time_get here as it can't parse %p
3727 // in isolation without %I. This might be faster anyway.
3728 const _CharT* __ampms[2];
3729 __tmpunct._M_am_pm(__ampms);
3730 int __n = 0, __which = 3;
3731 while (__which != 0)
3732 {
3733 auto __i = __is.peek();
3734 if (_Traits::eq_int_type(__i, _Traits::eof()))
3735 {
3737 break;
3738 }
3739 __i = std::toupper(_Traits::to_char_type(__i), __loc);
3740 if (__which & 1)
3741 {
3742 if (__i != std::toupper(__ampms[0][__n], __loc))
3743 __which ^= 1;
3744 else if (__ampms[0][__n + 1] == _CharT())
3745 {
3746 __which = 1;
3747 (void) __is.get();
3748 break;
3749 }
3750 }
3751 if (__which & 2)
3752 {
3753 if (__i != std::toupper(__ampms[1][__n], __loc))
3754 __which ^= 2;
3755 else if (__ampms[1][__n + 1] == _CharT())
3756 {
3757 __which = 2;
3758 (void) __is.get();
3759 break;
3760 }
3761 }
3762 if (__which)
3763 (void) __is.get();
3764 ++__n;
3765 }
3766 if (__which == 0 || __which == 3)
3767 __err |= ios_base::failbit;
3768 else
3769 __ampm = __which;
3770 }
3771 break;
3772
3773 case 'r': // Locale's 12-hour time.
3774 if (__mod || __num)
3775 __err |= ios_base::failbit;
3776 else
3777 {
3778 struct tm __tm{};
3779 __tmget.get(__is, {}, __is, __err, &__tm,
3780 __fmt - 2, __fmt);
3781 if (!__is_failed(__err))
3782 {
3783 __h = hours(__tm.tm_hour);
3784 __min = minutes(__tm.tm_min);
3785 __s = seconds(__tm.tm_sec);
3786 }
3787 }
3788 __parts |= _ChronoParts::_TimeOfDay;
3789 break;
3790
3791 case 'R': // %H:%M
3792 case 'T': // %H:%M:%S
3793 if (__mod || __num) [[unlikely]]
3794 {
3795 __err |= ios_base::failbit;
3796 break;
3797 }
3798 else
3799 {
3800 auto __val = __read_unsigned(2);
3801 if (__val == -1 || __val > 23) [[unlikely]]
3802 {
3803 if (_M_need & _ChronoParts::_TimeOfDay)
3804 __err |= ios_base::failbit;
3805 break;
3806 }
3807 if (!__read_chr(':')) [[unlikely]]
3808 break;
3809 __h = hours(__val);
3810
3811 __val = __read_unsigned(2);
3812 if (__val == -1 || __val > 60) [[unlikely]]
3813 {
3814 if (_M_need & _ChronoParts::_TimeOfDay)
3815 __err |= ios_base::failbit;
3816 break;
3817 }
3818 __min = minutes(__val);
3819
3820 if (__c == 'R')
3821 {
3822 __parts |= _ChronoParts::_TimeOfDay;
3823 break;
3824 }
3825 else if (!__read_chr(':')) [[unlikely]]
3826 break;
3827 }
3828 [[fallthrough]];
3829
3830 case 'S': // Seconds
3831 if (__mod == 'E') [[unlikely]]
3832 __err |= ios_base::failbit;
3833 else if (__mod == 'O')
3834 {
3835 struct tm __tm{};
3836 __tmget.get(__is, {}, __is, __err, &__tm,
3837 __fmt - 3, __fmt);
3838 if (!__is_failed(__err))
3839 __s = seconds(__tm.tm_sec);
3840 }
3841 else if constexpr (_Duration::period::den == 1
3842 && !__is_floating)
3843 {
3844 auto __val = __read_unsigned(__num ? __num : 2);
3845 if (0 <= __val && __val <= 59) [[likely]]
3846 __s = seconds(__val);
3847 else
3848 {
3849 if (_M_need & _ChronoParts::_TimeOfDay)
3850 __err |= ios_base::failbit;
3851 break;
3852 }
3853 }
3854 else // Read fractional seconds
3855 {
3856 basic_stringstream<_CharT> __buf;
3857 auto __digit = _S_try_read_digit(__is, __err);
3858 if (__digit != -1)
3859 {
3860 __buf.put(_CharT('0') + __digit);
3861 __digit = _S_try_read_digit(__is, __err);
3862 if (__digit != -1)
3863 __buf.put(_CharT('0') + __digit);
3864 }
3865
3866 auto __i = __is.peek();
3867 if (_Traits::eq_int_type(__i, _Traits::eof()))
3868 __err |= ios_base::eofbit;
3869 else
3870 {
3871 _CharT __dp = '.';
3872 if (__loc != locale::classic())
3873 {
3874 auto& __np = use_facet<numpunct<_CharT>>(__loc);
3875 __dp = __np.decimal_point();
3876 }
3877 _CharT __c = _Traits::to_char_type(__i);
3878 if (__c == __dp)
3879 {
3880 (void) __is.get();
3881 __buf.put('.');
3882 int __prec
3883 = hh_mm_ss<_Duration>::fractional_width;
3884 do
3885 {
3886 __digit = _S_try_read_digit(__is, __err);
3887 if (__digit != -1)
3888 __buf.put(_CharT('0') + __digit);
3889 else
3890 break;
3891 }
3892 while (--__prec);
3893 }
3894 }
3895
3896 if (!__is_failed(__err)) [[likely]]
3897 {
3898 long double __val{};
3899#if __cpp_lib_to_chars
3900 string __str = std::move(__buf).str();
3901 auto __first = __str.data();
3902 auto __last = __first + __str.size();
3903 using enum chars_format;
3904 auto [ptr, ec] = std::from_chars(__first, __last,
3905 __val, fixed);
3906 if ((bool)ec || ptr != __last) [[unlikely]]
3907 __err |= ios_base::failbit;
3908 else
3909#else
3910 if (__buf >> __val)
3911#endif
3912 {
3913 duration<long double> __fs(__val);
3914 if constexpr (__is_floating)
3915 __s = __fs;
3916 else
3917 __s = chrono::round<_Duration>(__fs);
3918 }
3919 }
3920 }
3921 __parts |= _ChronoParts::_TimeOfDay;
3922 break;
3923
3924 case 'u': // ISO weekday (1-7)
3925 case 'w': // Weekday (0-6)
3926 if (__mod == 'E') [[unlikely]]
3927 __err |= ios_base::failbit;
3928 else if (__mod == 'O')
3929 {
3930 if (__c == 'w')
3931 {
3932 struct tm __tm{};
3933 __tmget.get(__is, {}, __is, __err, &__tm,
3934 __fmt - 3, __fmt);
3935 if (!__is_failed(__err))
3936 __wday = weekday(__tm.tm_wday);
3937 }
3938 else
3939 __err |= ios_base::failbit;
3940 }
3941 else
3942 {
3943 const int __lo = __c == 'u' ? 1 : 0;
3944 const int __hi = __lo + 6;
3945 auto __val = __read_unsigned(__num ? __num : 1);
3946 if (__lo <= __val && __val <= __hi)
3947 __wday = weekday(__val);
3948 else
3949 {
3950 __wday = __bad_wday;
3951 break;
3952 }
3953 }
3954 __parts |= _ChronoParts::_Weekday;
3955 break;
3956
3957 case 'U': // Week number of the year (from first Sunday).
3958 case 'V': // ISO week-based week number.
3959 case 'W': // Week number of the year (from first Monday).
3960 if (__mod == 'E') [[unlikely]]
3961 __err |= ios_base::failbit;
3962 else if (__mod == 'O')
3963 {
3964 if (__c == 'V') [[unlikely]]
3965 __err |= ios_base::failbit;
3966 else
3967 {
3968 // TODO nl_langinfo_l(ALT_DIGITS) ?
3969 // Not implementable using std::time_get.
3970 }
3971 }
3972 else
3973 {
3974 const int __lo = __c == 'V' ? 1 : 0;
3975 const int __hi = 53;
3976 auto __val = __read_unsigned(__num ? __num : 2);
3977 if (__lo <= __val && __val <= __hi)
3978 {
3979 switch (__c)
3980 {
3981 case 'U':
3982 __sunday_wk = __val;
3983 break;
3984 case 'V':
3985 __iso_wk = __val;
3986 break;
3987 case 'W':
3988 __monday_wk = __val;
3989 break;
3990 }
3991 }
3992 else
3993 __iso_wk = __sunday_wk = __monday_wk = -1;
3994 }
3995 // N.B. do not alter __parts here, done after loop.
3996 break;
3997
3998 case 'x': // Locale's date representation.
3999 if (__mod == 'O' || __num) [[unlikely]]
4000 __err |= ios_base::failbit;
4001 else
4002 {
4003 struct tm __tm{};
4004 __tmget.get(__is, {}, __is, __err, &__tm,
4005 __fmt - 2 - (__mod == 'E'), __fmt);
4006 if (!__is_failed(__err))
4007 {
4008 __y = year(__tm.tm_year + 1900);
4009 __m = month(__tm.tm_mon + 1);
4010 __d = day(__tm.tm_mday);
4011 }
4012 }
4013 __parts |= _ChronoParts::_Date;
4014 break;
4015
4016 case 'X': // Locale's time representation.
4017 if (__mod == 'O' || __num) [[unlikely]]
4018 __err |= ios_base::failbit;
4019 else
4020 {
4021 struct tm __tm{};
4022 __tmget.get(__is, {}, __is, __err, &__tm,
4023 __fmt - 2 - (__mod == 'E'), __fmt);
4024 if (!__is_failed(__err))
4025 {
4026 __h = hours(__tm.tm_hour);
4027 __min = minutes(__tm.tm_min);
4028 __s = seconds(__tm.tm_sec);
4029 }
4030 }
4031 __parts |= _ChronoParts::_TimeOfDay;
4032 break;
4033
4034 case 'y': // Last two digits of year.
4035 if (__mod) [[unlikely]]
4036 {
4037 struct tm __tm{};
4038 __tmget.get(__is, {}, __is, __err, &__tm,
4039 __fmt - 3, __fmt);
4040 if (!__is_failed(__err))
4041 {
4042 int __cent = __tm.tm_year < 2000 ? 1900 : 2000;
4043 __yy = year(__tm.tm_year - __cent);
4044 if (__century == -1) // No %C has been parsed yet.
4045 __century = __cent;
4046 }
4047 }
4048 else
4049 {
4050 auto __val = __read_unsigned(__num ? __num : 2);
4051 if (__val >= 0 && __val <= 99)
4052 {
4053 __yy = year(__val);
4054 if (__century == -1) // No %C has been parsed yet.
4055 __century = __val < 69 ? 2000 : 1900;
4056 }
4057 else
4058 __y = __yy = __iso_yy = __iso_y = __bad_y;
4059 }
4060 __parts |= _ChronoParts::_Year;
4061 break;
4062
4063 case 'Y': // Year
4064 if (__mod == 'O') [[unlikely]]
4065 __err |= ios_base::failbit;
4066 else if (__mod == 'E')
4067 {
4068 struct tm __tm{};
4069 __tmget.get(__is, {}, __is, __err, &__tm,
4070 __fmt - 3, __fmt);
4071 if (!__is_failed(__err))
4072 __y = year(__tm.tm_year);
4073 }
4074 else
4075 {
4076 auto __val = __read_unsigned(__num ? __num : 4);
4077 if (!__is_failed(__err))
4078 __y = year(__val);
4079 }
4080 __parts |= _ChronoParts::_Year;
4081 break;
4082
4083 case 'z':
4084 if (__num) [[unlikely]]
4085 __err |= ios_base::failbit;
4086 else
4087 {
4088 // For %Ez and %Oz read [+|-][h]h[:mm].
4089 // For %z read [+|-]hh[mm].
4090
4091 auto __i = __is.peek();
4092 if (_Traits::eq_int_type(__i, _Traits::eof()))
4093 {
4095 break;
4096 }
4097 _CharT __ic = _Traits::to_char_type(__i);
4098 const bool __neg = __ic == _CharT('-');
4099 if (__ic == _CharT('-') || __ic == _CharT('+'))
4100 (void) __is.get();
4101
4102 int_least32_t __hh;
4103 if (__mod)
4104 {
4105 // Read h[h]
4106 __hh = __read_unsigned(2);
4107 }
4108 else
4109 {
4110 // Read hh
4111 __hh = 10 * _S_try_read_digit(__is, __err);
4112 __hh += _S_try_read_digit(__is, __err);
4113 }
4114
4115 if (__is_failed(__err))
4116 break;
4117
4118 __i = __is.peek();
4119 if (_Traits::eq_int_type(__i, _Traits::eof()))
4120 {
4121 __err |= ios_base::eofbit;
4122 __tz_offset = minutes(__hh * (__neg ? -60 : 60));
4123 break;
4124 }
4125 __ic = _Traits::to_char_type(__i);
4126
4127 bool __read_mm = false;
4128 if (__mod)
4129 {
4130 if (__ic == _GLIBCXX_WIDEN(":")[0])
4131 {
4132 // Read [:mm] part.
4133 (void) __is.get();
4134 __read_mm = true;
4135 }
4136 }
4137 else if (_CharT('0') <= __ic && __ic <= _CharT('9'))
4138 {
4139 // Read [mm] part.
4140 __read_mm = true;
4141 }
4142
4143 int_least32_t __mm = 0;
4144 if (__read_mm)
4145 {
4146 __mm = 10 * _S_try_read_digit(__is, __err);
4147 __mm += _S_try_read_digit(__is, __err);
4148 }
4149
4150 if (!__is_failed(__err))
4151 {
4152 auto __z = __hh * 60 + __mm;
4153 __tz_offset = minutes(__neg ? -__z : __z);
4154 }
4155 }
4156 break;
4157
4158 case 'Z':
4159 if (__mod || __num) [[unlikely]]
4160 __err |= ios_base::failbit;
4161 else
4162 {
4163 basic_string_view<_CharT> __x = _GLIBCXX_WIDEN("_/-+");
4164 __tz_abbr.clear();
4165 while (true)
4166 {
4167 auto __i = __is.peek();
4168 if (!_Traits::eq_int_type(__i, _Traits::eof()))
4169 {
4170 _CharT __a = _Traits::to_char_type(__i);
4171 if (std::isalnum(__a, __loc)
4172 || __x.find(__a) != __x.npos)
4173 {
4174 __tz_abbr.push_back(__a);
4175 (void) __is.get();
4176 continue;
4177 }
4178 }
4179 else
4180 __err |= ios_base::eofbit;
4181 break;
4182 }
4183 if (__tz_abbr.empty())
4184 __err |= ios_base::failbit;
4185 }
4186 break;
4187
4188 case 'n': // Exactly one whitespace character.
4189 if (__mod || __num) [[unlikely]]
4190 __err |= ios_base::failbit;
4191 else
4192 {
4193 _CharT __i = __is.peek();
4194 if (_Traits::eq_int_type(__i, _Traits::eof()))
4196 else if (std::isspace(_Traits::to_char_type(__i), __loc))
4197 (void) __is.get();
4198 else
4199 __err |= ios_base::failbit;
4200 }
4201 break;
4202
4203 case 't': // Zero or one whitespace characters.
4204 if (__mod || __num) [[unlikely]]
4205 __err |= ios_base::failbit;
4206 else
4207 {
4208 _CharT __i = __is.peek();
4209 if (_Traits::eq_int_type(__i, _Traits::eof()))
4210 __err |= ios_base::eofbit;
4211 else if (std::isspace(_Traits::to_char_type(__i), __loc))
4212 (void) __is.get();
4213 }
4214 break;
4215
4216 case '%': // A % character.
4217 if (__mod || __num) [[unlikely]]
4218 __err |= ios_base::failbit;
4219 else
4220 __read_chr('%');
4221 break;
4222
4223 case 'O': // Modifiers
4224 case 'E':
4225 if (__mod || __num) [[unlikely]]
4226 {
4227 __err |= ios_base::failbit;
4228 break;
4229 }
4230 __mod = __c;
4231 continue;
4232
4233 default:
4234 if (_CharT('1') <= __c && __c <= _CharT('9'))
4235 {
4236 if (!__mod) [[likely]]
4237 {
4238 // %Nx - extract positive decimal integer N
4239 auto __end = __fmt + _Traits::length(__fmt);
4240 auto [__v, __ptr]
4241 = __format::__parse_integer(__fmt - 1, __end);
4242 if (__ptr) [[likely]]
4243 {
4244 __num = __v;
4245 __fmt = __ptr;
4246 continue;
4247 }
4248 }
4249 }
4250 __err |= ios_base::failbit;
4251 }
4252
4253 if (__is_failed(__err)) [[unlikely]]
4254 break;
4255
4256 __is_flag = false;
4257 __num = 0;
4258 __mod = _CharT();
4259 }
4260
4261 if (__century >= 0)
4262 {
4263 if (__yy != __bad_y && __y == __bad_y)
4264 __y = years(__century) + __yy; // Use %y instead of %Y
4265 if (__iso_yy != __bad_y && __iso_y == __bad_y)
4266 __iso_y = years(__century) + __iso_yy; // Use %g instead of %G
4267 }
4268
4269 bool __can_use_doy = false;
4270 bool __can_use_iso_wk = false;
4271 bool __can_use_sun_wk = false;
4272 bool __can_use_mon_wk = false;
4273
4274 // A year + day-of-year can be converted to a full date.
4275 if (__y != __bad_y && __dayofyear >= 0)
4276 {
4277 __can_use_doy = true;
4278 __parts |= _ChronoParts::_Date;
4279 }
4280 else if (__y != __bad_y && __wday != __bad_wday && __sunday_wk >= 0)
4281 {
4282 __can_use_sun_wk = true;
4283 __parts |= _ChronoParts::_Date;
4284 }
4285 else if (__y != __bad_y && __wday != __bad_wday && __monday_wk >= 0)
4286 {
4287 __can_use_mon_wk = true;
4288 __parts |= _ChronoParts::_Date;
4289 }
4290 else if (__iso_y != __bad_y && __wday != __bad_wday && __iso_wk > 0)
4291 {
4292 // An ISO week date can be converted to a full date.
4293 __can_use_iso_wk = true;
4294 __parts |= _ChronoParts::_Date;
4295 }
4296
4297 if (__is_failed(__err)) [[unlikely]]
4298 ; // Don't bother doing any more work.
4299 else if (__is_flag) [[unlikely]] // incomplete format flag
4300 __err |= ios_base::failbit;
4301 else if ((_M_need & __parts) == _M_need) [[likely]]
4302 {
4303 // We try to avoid calculating _M_sys_days and _M_ymd unless
4304 // necessary, because converting sys_days to year_month_day
4305 // (or vice versa) requires non-trivial calculations.
4306 // If we have y/m/d values then use them to populate _M_ymd
4307 // and only convert it to _M_sys_days if the caller needs that.
4308 // But if we don't have y/m/d and need to calculate the date
4309 // from the day-of-year or a week+weekday then we set _M_sys_days
4310 // and only convert it to _M_ymd if the caller needs that.
4311
4312 // We do more error checking here, but only for the fields that
4313 // we actually need to use. For example, we will not diagnose
4314 // an invalid dayofyear==366 for non-leap years unless actually
4315 // using __dayofyear. This should mean we never produce invalid
4316 // results, but it means not all invalid inputs are diagnosed,
4317 // e.g. "2023-01-01 366" >> "%F %j" ignores the invalid 366.
4318 // We also do not diagnose inconsistent values for the same
4319 // field, e.g. "2021 2022 2023" >> "%C%y %Y %Y" just uses 2023.
4320
4321 // Whether the caller wants _M_wd.
4322 // The _Weekday bit is only set for chrono::weekday.
4323 const bool __need_wday = _M_need & _ChronoParts::_Weekday;
4324
4325 // Whether the caller wants _M_sys_days and _M_time.
4326 // Only true for durations and time_points.
4327 const bool __need_time = _M_need & _ChronoParts::_TimeOfDay;
4328
4329 if (__need_wday && __wday != __bad_wday)
4330 _M_wd = __wday; // Caller only wants a weekday and we have one.
4331 else if (_M_need & _ChronoParts::_Date) // subsumes __need_wday
4332 {
4333 // Whether the caller wants _M_ymd.
4334 // True for chrono::year etc., false for time_points.
4335 const bool __need_ymd = !__need_wday && !__need_time;
4336
4337 if ((_M_need & _ChronoParts::_Year && __y == __bad_y)
4338 || (_M_need & _ChronoParts::_Month && __m == __bad_mon)
4339 || (_M_need & _ChronoParts::_Day && __d == __bad_day))
4340 {
4341 // Missing at least one of y/m/d so calculate sys_days
4342 // from the other data we have available.
4343
4344 if (__can_use_doy)
4345 {
4346 if ((0 < __dayofyear && __dayofyear <= 365)
4347 || (__dayofyear == 366 && __y.is_leap()))
4348 [[likely]]
4349 {
4350 _M_sys_days = sys_days(__y/January/1)
4351 + days(__dayofyear - 1);
4352 if (__need_ymd)
4353 _M_ymd = year_month_day(_M_sys_days);
4354 }
4355 else
4356 __err |= ios_base::failbit;
4357 }
4358 else if (__can_use_iso_wk)
4359 {
4360 // Calculate y/m/d from ISO week date.
4361
4362 if (__iso_wk == 53)
4363 {
4364 // A year has 53 weeks iff Jan 1st is a Thursday
4365 // or Jan 1 is a Wednesday and it's a leap year.
4366 const sys_days __jan4(__iso_y/January/4);
4367 weekday __wd1(__jan4 - days(3));
4368 if (__wd1 != Thursday)
4369 if (__wd1 != Wednesday || !__iso_y.is_leap())
4370 __err |= ios_base::failbit;
4371 }
4372
4373 if (!__is_failed(__err)) [[likely]]
4374 {
4375 // First Thursday is always in week one:
4376 sys_days __w(Thursday[1]/January/__iso_y);
4377 // First day of week-based year:
4378 __w -= Thursday - Monday;
4379 __w += days(weeks(__iso_wk - 1));
4380 __w += __wday - Monday;
4381 _M_sys_days = __w;
4382
4383 if (__need_ymd)
4384 _M_ymd = year_month_day(_M_sys_days);
4385 }
4386 }
4387 else if (__can_use_sun_wk)
4388 {
4389 // Calculate y/m/d from week number + weekday.
4390 sys_days __wk1(__y/January/Sunday[1]);
4391 _M_sys_days = __wk1 + weeks(__sunday_wk - 1)
4392 + days(__wday.c_encoding());
4393 _M_ymd = year_month_day(_M_sys_days);
4394 if (_M_ymd.year() != __y) [[unlikely]]
4395 __err |= ios_base::failbit;
4396 }
4397 else if (__can_use_mon_wk)
4398 {
4399 // Calculate y/m/d from week number + weekday.
4400 sys_days __wk1(__y/January/Monday[1]);
4401 _M_sys_days = __wk1 + weeks(__monday_wk - 1)
4402 + days(__wday.c_encoding() - 1);
4403 _M_ymd = year_month_day(_M_sys_days);
4404 if (_M_ymd.year() != __y) [[unlikely]]
4405 __err |= ios_base::failbit;
4406 }
4407 else // Should not be able to get here.
4408 __err |= ios_base::failbit;
4409 }
4410 else
4411 {
4412 // We know that all fields the caller needs are present,
4413 // but check that their values are in range.
4414 // Make unwanted fields valid so that _M_ymd.ok() is true.
4415
4416 if (_M_need & _ChronoParts::_Year)
4417 {
4418 if (!__y.ok()) [[unlikely]]
4419 __err |= ios_base::failbit;
4420 }
4421 else if (__y == __bad_y)
4422 __y = 1972y; // Leap year so that Feb 29 is valid.
4423
4424 if (_M_need & _ChronoParts::_Month)
4425 {
4426 if (!__m.ok()) [[unlikely]]
4427 __err |= ios_base::failbit;
4428 }
4429 else if (__m == __bad_mon)
4430 __m = January;
4431
4432 if (_M_need & _ChronoParts::_Day)
4433 {
4434 if (__d < day(1) || __d > (__y/__m/last).day())
4435 __err |= ios_base::failbit;
4436 }
4437 else if (__d == __bad_day)
4438 __d = 1d;
4439
4440 if (year_month_day __ymd(__y, __m, __d); __ymd.ok())
4441 {
4442 _M_ymd = __ymd;
4443 if (__need_wday || __need_time)
4444 _M_sys_days = sys_days(_M_ymd);
4445 }
4446 else [[unlikely]]
4447 __err |= ios_base::failbit;
4448 }
4449
4450 if (__need_wday)
4451 _M_wd = weekday(_M_sys_days);
4452 }
4453
4454 // Need to set _M_time for both durations and time_points.
4455 if (__need_time)
4456 {
4457 if (__h == __bad_h && __h12 != __bad_h)
4458 {
4459 if (__ampm == 1)
4460 __h = __h12 == hours(12) ? hours(0) : __h12;
4461 else if (__ampm == 2)
4462 __h = __h12 == hours(12) ? __h12 : __h12 + hours(12);
4463 else [[unlikely]]
4464 __err |= ios_base::failbit;
4465 }
4466
4467 auto __t = _M_time.zero();
4468 bool __ok = false;
4469
4470 if (__h != __bad_h)
4471 {
4472 __ok = true;
4473 __t += __h;
4474 }
4475
4476 if (__min != __bad_min)
4477 {
4478 __ok = true;
4479 __t += __min;
4480 }
4481
4482 if (__s != __bad_sec)
4483 {
4484 __ok = true;
4485 __t += __s;
4486 _M_is_leap_second = __s >= seconds(60);
4487 }
4488
4489 if (__ok)
4490 _M_time = __t;
4491 else
4492 __err |= ios_base::failbit;
4493 }
4494
4495 if (!__is_failed(__err)) [[likely]]
4496 {
4497 if (__offset && __tz_offset != __bad_min)
4498 *__offset = __tz_offset;
4499 if (__abbrev && !__tz_abbr.empty())
4500 *__abbrev = std::move(__tz_abbr);
4501 }
4502 }
4503 else
4504 __err |= ios_base::failbit;
4505 }
4506 if (__err)
4507 __is.setstate(__err);
4508 return __is;
4509 }
4510 /// @endcond
4511#undef _GLIBCXX_WIDEN
4512
4513 /// @} group chrono
4514} // namespace chrono
4515
4516_GLIBCXX_END_NAMESPACE_VERSION
4517} // namespace std
4518
4519#endif // C++20
4520
4521#endif //_GLIBCXX_CHRONO_IO_H
__detail::__local_time_fmt< _Duration > local_time_format(local_time< _Duration > __time, const string *__abbrev=nullptr, const seconds *__offset_sec=nullptr)
Definition chrono_io.h:179
duration< int64_t > seconds
seconds
Definition chrono.h:901
duration< int64_t, ratio< 604800 > > weeks
weeks
Definition chrono.h:914
duration< int64_t, ratio< 3600 > > hours
hours
Definition chrono.h:907
duration< int64_t, ratio< 86400 > > days
days
Definition chrono.h:911
basic_ostream< _CharT, _Traits > & operator<<(std::basic_ostream< _CharT, _Traits > &__os, const duration< _Rep, _Period > &__d)
Definition chrono_io.h:130
duration< int64_t, ratio< 60 > > minutes
minutes
Definition chrono.h:904
duration< int64_t, ratio< 31556952 > > years
years
Definition chrono.h:917
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
Definition move.h:138
constexpr _Tp * __addressof(_Tp &__r) noexcept
Same as C++11 std::addressof.
Definition move.h:52
constexpr const _Tp & min(const _Tp &, const _Tp &)
This does what you think it does.
ISO C++ entities toplevel namespace is std.
ptrdiff_t streamsize
Integral type for I/O operation counts and buffer sizes.
Definition postypes.h:73
chars_format
floating-point format for primitive numerical conversion
Definition charconv:626
bool isspace(_CharT __c, const locale &__loc)
Convenience interface to ctype.is(ctype_base::space, __c).
_CharT toupper(_CharT __c, const locale &__loc)
Convenience interface to ctype.toupper(__c).
bool isalnum(_CharT __c, const locale &__loc)
Convenience interface to ctype.is(ctype_base::alnum, __c).
ios_base & dec(ios_base &__base)
Calls base.setf(ios_base::dec, ios_base::basefield).
Definition ios_base.h:1094
ios_base & skipws(ios_base &__base)
Calls base.setf(ios_base::skipws).
Definition ios_base.h:1020
std::basic_istream< _CharT, _Traits > & operator>>(std::basic_istream< _CharT, _Traits > &__is, bitset< _Nb > &__x)
Global I/O operators for bitsets.
Definition bitset:1602
constexpr bitset< _Nb > operator|(const bitset< _Nb > &__x, const bitset< _Nb > &__y) noexcept
Global bitwise operations on bitsets.
Definition bitset:1572
basic_istream< _CharT, _Traits > & ws(basic_istream< _CharT, _Traits > &__is)
Quick and easy way to eat whitespace.
Definition istream.tcc:1078
constexpr from_chars_result from_chars(const char *__first, const char *__last, _Tp &__value, int __base=10)
std::from_chars for integral types.
Definition charconv:557
ISO C++ 2011 namespace for date and time utilities.
iterator begin()
Definition cow_string.h:844
_Ios_Iostate iostate
This is a bitmask type.
Definition ios_base.h:453
static const iostate eofbit
Indicates that an input operation reached the end of an input sequence.
Definition ios_base.h:460
static const iostate goodbit
Indicates all is well.
Definition ios_base.h:468
static const iostate failbit
Indicates that an input operation failed to read the expected characters, or that an output operation...
Definition ios_base.h:465
Container class for localization functionality.
static const locale & classic()
Return reference to the C locale.