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