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