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