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