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_HAVE_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<typename _CharT>
1789 struct formatter<chrono::day, _CharT>
1790 {
1791 template<typename _ParseContext>
1792 constexpr typename _ParseContext::iterator
1793 parse(_ParseContext& __pc)
1794 { return _M_f._M_parse(__pc, __format::_Day); }
1795
1796 template<typename _FormatContext>
1797 typename _FormatContext::iterator
1798 format(const chrono::day& __t, _FormatContext& __fc) const
1799 { return _M_f._M_format(__t, __fc); }
1800
1801 private:
1802 __format::__formatter_chrono<_CharT> _M_f;
1803 };
1804
1805 template<typename _CharT>
1806 struct formatter<chrono::month, _CharT>
1807 {
1808 template<typename _ParseContext>
1809 constexpr typename _ParseContext::iterator
1810 parse(_ParseContext& __pc)
1811 { return _M_f._M_parse(__pc, __format::_Month); }
1812
1813 template<typename _FormatContext>
1814 typename _FormatContext::iterator
1815 format(const chrono::month& __t, _FormatContext& __fc) const
1816 { return _M_f._M_format(__t, __fc); }
1817
1818 private:
1819 __format::__formatter_chrono<_CharT> _M_f;
1820 };
1821
1822 template<typename _CharT>
1823 struct formatter<chrono::year, _CharT>
1824 {
1825 template<typename _ParseContext>
1826 constexpr typename _ParseContext::iterator
1827 parse(_ParseContext& __pc)
1828 { return _M_f._M_parse(__pc, __format::_Year); }
1829
1830 template<typename _FormatContext>
1831 typename _FormatContext::iterator
1832 format(const chrono::year& __t, _FormatContext& __fc) const
1833 { return _M_f._M_format(__t, __fc); }
1834
1835 private:
1836 __format::__formatter_chrono<_CharT> _M_f;
1837 };
1838
1839 template<typename _CharT>
1840 struct formatter<chrono::weekday, _CharT>
1841 {
1842 template<typename _ParseContext>
1843 constexpr typename _ParseContext::iterator
1844 parse(_ParseContext& __pc)
1845 { return _M_f._M_parse(__pc, __format::_Weekday); }
1846
1847 template<typename _FormatContext>
1848 typename _FormatContext::iterator
1849 format(const chrono::weekday& __t, _FormatContext& __fc) const
1850 { return _M_f._M_format(__t, __fc); }
1851
1852 private:
1853 __format::__formatter_chrono<_CharT> _M_f;
1854 };
1855
1856 template<typename _CharT>
1857 struct formatter<chrono::weekday_indexed, _CharT>
1858 {
1859 template<typename _ParseContext>
1860 constexpr typename _ParseContext::iterator
1861 parse(_ParseContext& __pc)
1862 { return _M_f._M_parse(__pc, __format::_Weekday); }
1863
1864 template<typename _FormatContext>
1865 typename _FormatContext::iterator
1866 format(const chrono::weekday_indexed& __t, _FormatContext& __fc) const
1867 { return _M_f._M_format(__t, __fc); }
1868
1869 private:
1870 __format::__formatter_chrono<_CharT> _M_f;
1871 };
1872
1873 template<typename _CharT>
1874 struct formatter<chrono::weekday_last, _CharT>
1875 {
1876 template<typename _ParseContext>
1877 constexpr typename _ParseContext::iterator
1878 parse(_ParseContext& __pc)
1879 { return _M_f._M_parse(__pc, __format::_Weekday); }
1880
1881 template<typename _FormatContext>
1882 typename _FormatContext::iterator
1883 format(const chrono::weekday_last& __t, _FormatContext& __fc) const
1884 { return _M_f._M_format(__t, __fc); }
1885
1886 private:
1887 __format::__formatter_chrono<_CharT> _M_f;
1888 };
1889
1890 template<typename _CharT>
1891 struct formatter<chrono::month_day, _CharT>
1892 {
1893 template<typename _ParseContext>
1894 constexpr typename _ParseContext::iterator
1895 parse(_ParseContext& __pc)
1896 { return _M_f._M_parse(__pc, __format::_Month|__format::_Day); }
1897
1898 template<typename _FormatContext>
1899 typename _FormatContext::iterator
1900 format(const chrono::month_day& __t, _FormatContext& __fc) const
1901 { return _M_f._M_format(__t, __fc); }
1902
1903 private:
1904 __format::__formatter_chrono<_CharT> _M_f;
1905 };
1906
1907 template<typename _CharT>
1908 struct formatter<chrono::month_day_last, _CharT>
1909 {
1910 template<typename _ParseContext>
1911 constexpr typename _ParseContext::iterator
1912 parse(_ParseContext& __pc)
1913 { return _M_f._M_parse(__pc, __format::_Month|__format::_Day); }
1914
1915 template<typename _FormatContext>
1916 typename _FormatContext::iterator
1917 format(const chrono::month_day_last& __t, _FormatContext& __fc) const
1918 { return _M_f._M_format(__t, __fc); }
1919
1920 private:
1921 __format::__formatter_chrono<_CharT> _M_f;
1922 };
1923
1924 template<typename _CharT>
1925 struct formatter<chrono::month_weekday, _CharT>
1926 {
1927 template<typename _ParseContext>
1928 constexpr typename _ParseContext::iterator
1929 parse(_ParseContext& __pc)
1930 { return _M_f._M_parse(__pc, __format::_Month|__format::_Weekday); }
1931
1932 template<typename _FormatContext>
1933 typename _FormatContext::iterator
1934 format(const chrono::month_weekday& __t, _FormatContext& __fc) const
1935 { return _M_f._M_format(__t, __fc); }
1936
1937 private:
1938 __format::__formatter_chrono<_CharT> _M_f;
1939 };
1940
1941 template<typename _CharT>
1942 struct formatter<chrono::month_weekday_last, _CharT>
1943 {
1944 template<typename _ParseContext>
1945 constexpr typename _ParseContext::iterator
1946 parse(_ParseContext& __pc)
1947 { return _M_f._M_parse(__pc, __format::_Month|__format::_Weekday); }
1948
1949 template<typename _FormatContext>
1950 typename _FormatContext::iterator
1951 format(const chrono::month_weekday_last& __t,
1952 _FormatContext& __fc) const
1953 { return _M_f._M_format(__t, __fc); }
1954
1955 private:
1956 __format::__formatter_chrono<_CharT> _M_f;
1957 };
1958
1959 template<typename _CharT>
1960 struct formatter<chrono::year_month, _CharT>
1961 {
1962 template<typename _ParseContext>
1963 constexpr typename _ParseContext::iterator
1964 parse(_ParseContext& __pc)
1965 { return _M_f._M_parse(__pc, __format::_Year|__format::_Month); }
1966
1967 template<typename _FormatContext>
1968 typename _FormatContext::iterator
1969 format(const chrono::year_month& __t, _FormatContext& __fc) const
1970 { return _M_f._M_format(__t, __fc); }
1971
1972 private:
1973 __format::__formatter_chrono<_CharT> _M_f;
1974 };
1975
1976 template<typename _CharT>
1977 struct formatter<chrono::year_month_day, _CharT>
1978 {
1979 template<typename _ParseContext>
1980 constexpr typename _ParseContext::iterator
1981 parse(_ParseContext& __pc)
1982 { return _M_f._M_parse(__pc, __format::_Date); }
1983
1984 template<typename _FormatContext>
1985 typename _FormatContext::iterator
1986 format(const chrono::year_month_day& __t, _FormatContext& __fc) const
1987 { return _M_f._M_format(__t, __fc); }
1988
1989 private:
1990 __format::__formatter_chrono<_CharT> _M_f;
1991 };
1992
1993 template<typename _CharT>
1994 struct formatter<chrono::year_month_day_last, _CharT>
1995 {
1996 template<typename _ParseContext>
1997 constexpr typename _ParseContext::iterator
1998 parse(_ParseContext& __pc)
1999 { return _M_f._M_parse(__pc, __format::_Date); }
2000
2001 template<typename _FormatContext>
2002 typename _FormatContext::iterator
2003 format(const chrono::year_month_day_last& __t,
2004 _FormatContext& __fc) const
2005 { return _M_f._M_format(__t, __fc); }
2006
2007 private:
2008 __format::__formatter_chrono<_CharT> _M_f;
2009 };
2010
2011 template<typename _CharT>
2012 struct formatter<chrono::year_month_weekday, _CharT>
2013 {
2014 template<typename _ParseContext>
2015 constexpr typename _ParseContext::iterator
2016 parse(_ParseContext& __pc)
2017 { return _M_f._M_parse(__pc, __format::_Date); }
2018
2019 template<typename _FormatContext>
2020 typename _FormatContext::iterator
2021 format(const chrono::year_month_weekday& __t,
2022 _FormatContext& __fc) const
2023 { return _M_f._M_format(__t, __fc); }
2024
2025 private:
2026 __format::__formatter_chrono<_CharT> _M_f;
2027 };
2028
2029 template<typename _CharT>
2030 struct formatter<chrono::year_month_weekday_last, _CharT>
2031 {
2032 template<typename _ParseContext>
2033 constexpr typename _ParseContext::iterator
2034 parse(_ParseContext& __pc)
2035 { return _M_f._M_parse(__pc, __format::_Date); }
2036
2037 template<typename _FormatContext>
2038 typename _FormatContext::iterator
2039 format(const chrono::year_month_weekday_last& __t,
2040 _FormatContext& __fc) const
2041 { return _M_f._M_format(__t, __fc); }
2042
2043 private:
2044 __format::__formatter_chrono<_CharT> _M_f;
2045 };
2046
2047 template<typename _Rep, typename _Period, typename _CharT>
2048 struct formatter<chrono::hh_mm_ss<chrono::duration<_Rep, _Period>>, _CharT>
2049 {
2050 template<typename _ParseContext>
2051 constexpr typename _ParseContext::iterator
2052 parse(_ParseContext& __pc)
2053 { return _M_f._M_parse(__pc, __format::_TimeOfDay); }
2054
2055 template<typename _FormatContext>
2056 typename _FormatContext::iterator
2057 format(const chrono::hh_mm_ss<chrono::duration<_Rep, _Period>>& __t,
2058 _FormatContext& __fc) const
2059 { return _M_f._M_format(__t, __fc); }
2060
2061 private:
2062 __format::__formatter_chrono<_CharT> _M_f;
2063 };
2064
2065#if _GLIBCXX_USE_CXX11_ABI || ! _GLIBCXX_USE_DUAL_ABI
2066 template<typename _CharT>
2067 struct formatter<chrono::sys_info, _CharT>
2068 {
2069 template<typename _ParseContext>
2070 constexpr typename _ParseContext::iterator
2071 parse(_ParseContext& __pc)
2072 { return _M_f._M_parse(__pc, __format::_ChronoParts{}); }
2073
2074 template<typename _FormatContext>
2075 typename _FormatContext::iterator
2076 format(const chrono::sys_info& __i, _FormatContext& __fc) const
2077 { return _M_f._M_format(__i, __fc); }
2078
2079 private:
2080 __format::__formatter_chrono<_CharT> _M_f;
2081 };
2082
2083 template<typename _CharT>
2084 struct formatter<chrono::local_info, _CharT>
2085 {
2086 template<typename _ParseContext>
2087 constexpr typename _ParseContext::iterator
2088 parse(_ParseContext& __pc)
2089 { return _M_f._M_parse(__pc, __format::_ChronoParts{}); }
2090
2091 template<typename _FormatContext>
2092 typename _FormatContext::iterator
2093 format(const chrono::local_info& __i, _FormatContext& __fc) const
2094 { return _M_f._M_format(__i, __fc); }
2095
2096 private:
2097 __format::__formatter_chrono<_CharT> _M_f;
2098 };
2099#endif
2100
2101 template<typename _Duration, typename _CharT>
2102 struct formatter<chrono::sys_time<_Duration>, _CharT>
2103 {
2104 template<typename _ParseContext>
2105 constexpr typename _ParseContext::iterator
2106 parse(_ParseContext& __pc)
2107 {
2108 auto __next = _M_f._M_parse(__pc, __format::_ZonedDateTime);
2109 if constexpr (!__stream_insertable)
2110 if (_M_f._M_spec._M_chrono_specs.empty())
2111 __format::__invalid_chrono_spec(); // chrono-specs can't be empty
2112 return __next;
2113 }
2114
2115 template<typename _FormatContext>
2116 typename _FormatContext::iterator
2117 format(const chrono::sys_time<_Duration>& __t,
2118 _FormatContext& __fc) const
2119 { return _M_f._M_format(__t, __fc); }
2120
2121 private:
2122 static constexpr bool __stream_insertable
2123 = requires (basic_ostream<_CharT>& __os,
2124 chrono::sys_time<_Duration> __t) { __os << __t; };
2125
2126 __format::__formatter_chrono<_CharT> _M_f;
2127 };
2128
2129 template<typename _Duration, typename _CharT>
2130 struct formatter<chrono::utc_time<_Duration>, _CharT>
2131 : __format::__formatter_chrono<_CharT>
2132 {
2133 template<typename _ParseContext>
2134 constexpr typename _ParseContext::iterator
2135 parse(_ParseContext& __pc)
2136 { return _M_f._M_parse(__pc, __format::_ZonedDateTime); }
2137
2138 template<typename _FormatContext>
2139 typename _FormatContext::iterator
2140 format(const chrono::utc_time<_Duration>& __t,
2141 _FormatContext& __fc) const
2142 {
2143 // Adjust by removing leap seconds to get equivalent sys_time.
2144 // We can't just use clock_cast because we want to know if the time
2145 // falls within a leap second insertion, and format seconds as "60".
2146 using chrono::__detail::__utc_leap_second;
2147 using chrono::seconds;
2148 using chrono::sys_time;
2149 using _CDur = common_type_t<_Duration, seconds>;
2150 const auto __li = chrono::get_leap_second_info(__t);
2151 sys_time<_CDur> __s{__t.time_since_epoch() - __li.elapsed};
2152 if (!__li.is_leap_second) [[likely]]
2153 return _M_f._M_format(__s, __fc);
2154 else
2155 return _M_f._M_format(__utc_leap_second(__s), __fc);
2156 }
2157
2158 private:
2159 friend formatter<chrono::__detail::__utc_leap_second<_Duration>, _CharT>;
2160
2161 __format::__formatter_chrono<_CharT> _M_f;
2162 };
2163
2164 template<typename _Duration, typename _CharT>
2165 struct formatter<chrono::tai_time<_Duration>, _CharT>
2166 : __format::__formatter_chrono<_CharT>
2167 {
2168 template<typename _ParseContext>
2169 constexpr typename _ParseContext::iterator
2170 parse(_ParseContext& __pc)
2171 { return _M_f._M_parse(__pc, __format::_ZonedDateTime); }
2172
2173 template<typename _FormatContext>
2174 typename _FormatContext::iterator
2175 format(const chrono::tai_time<_Duration>& __t,
2176 _FormatContext& __fc) const
2177 {
2178 // Convert to __local_time_fmt with abbrev "TAI" and offset 0s.
2179 // We use __local_time_fmt and not sys_time (as the standard implies)
2180 // because %Z for sys_time would print "UTC" and we want "TAI" here.
2181
2182 // Offset is 1970y/January/1 - 1958y/January/1
2183 constexpr chrono::days __tai_offset = chrono::days(4383);
2184 using _CDur = common_type_t<_Duration, chrono::days>;
2185 chrono::local_time<_CDur> __lt(__t.time_since_epoch() - __tai_offset);
2186 const string __abbrev("TAI", 3);
2187 const chrono::seconds __off = 0s;
2188 const auto __lf = chrono::local_time_format(__lt, &__abbrev, &__off);
2189 return _M_f._M_format(__lf, __fc);
2190 }
2191
2192 private:
2193 __format::__formatter_chrono<_CharT> _M_f;
2194 };
2195
2196 template<typename _Duration, typename _CharT>
2197 struct formatter<chrono::gps_time<_Duration>, _CharT>
2198 : __format::__formatter_chrono<_CharT>
2199 {
2200 template<typename _ParseContext>
2201 constexpr typename _ParseContext::iterator
2202 parse(_ParseContext& __pc)
2203 { return _M_f._M_parse(__pc, __format::_ZonedDateTime); }
2204
2205 template<typename _FormatContext>
2206 typename _FormatContext::iterator
2207 format(const chrono::gps_time<_Duration>& __t,
2208 _FormatContext& __fc) const
2209 {
2210 // Convert to __local_time_fmt with abbrev "GPS" and offset 0s.
2211 // We use __local_time_fmt and not sys_time (as the standard implies)
2212 // because %Z for sys_time would print "UTC" and we want "GPS" here.
2213
2214 // Offset is 1980y/January/Sunday[1] - 1970y/January/1
2215 constexpr chrono::days __gps_offset = chrono::days(3657);
2216 using _CDur = common_type_t<_Duration, chrono::days>;
2217 chrono::local_time<_CDur> __lt(__t.time_since_epoch() + __gps_offset);
2218 const string __abbrev("GPS", 3);
2219 const chrono::seconds __off = 0s;
2220 const auto __lf = chrono::local_time_format(__lt, &__abbrev, &__off);
2221 return _M_f._M_format(__lf, __fc);
2222 }
2223
2224 private:
2225 __format::__formatter_chrono<_CharT> _M_f;
2226 };
2227
2228 template<typename _Duration, typename _CharT>
2229 struct formatter<chrono::file_time<_Duration>, _CharT>
2230 {
2231 template<typename _ParseContext>
2232 constexpr typename _ParseContext::iterator
2233 parse(_ParseContext& __pc)
2234 { return _M_f._M_parse(__pc, __format::_ZonedDateTime); }
2235
2236 template<typename _FormatContext>
2237 typename _FormatContext::iterator
2238 format(const chrono::file_time<_Duration>& __t,
2239 _FormatContext& __ctx) const
2240 {
2241 using namespace chrono;
2242 return _M_f._M_format(chrono::clock_cast<system_clock>(__t), __ctx);
2243 }
2244
2245 private:
2246 __format::__formatter_chrono<_CharT> _M_f;
2247 };
2248
2249 template<typename _Duration, typename _CharT>
2250 struct formatter<chrono::local_time<_Duration>, _CharT>
2251 {
2252 template<typename _ParseContext>
2253 constexpr typename _ParseContext::iterator
2254 parse(_ParseContext& __pc)
2255 { return _M_f._M_parse(__pc, __format::_DateTime); }
2256
2257 template<typename _FormatContext>
2258 typename _FormatContext::iterator
2259 format(const chrono::local_time<_Duration>& __t,
2260 _FormatContext& __ctx) const
2261 { return _M_f._M_format(__t, __ctx); }
2262
2263 private:
2264 __format::__formatter_chrono<_CharT> _M_f;
2265 };
2266
2267 template<typename _Duration, typename _CharT>
2268 struct formatter<chrono::__detail::__local_time_fmt<_Duration>, _CharT>
2269 {
2270 template<typename _ParseContext>
2271 constexpr typename _ParseContext::iterator
2272 parse(_ParseContext& __pc)
2273 { return _M_f._M_parse(__pc, __format::_ZonedDateTime); }
2274
2275 template<typename _FormatContext>
2276 typename _FormatContext::iterator
2277 format(const chrono::__detail::__local_time_fmt<_Duration>& __t,
2278 _FormatContext& __ctx) const
2279 { return _M_f._M_format(__t, __ctx, /* use %Z for {} */ true); }
2280
2281 private:
2282 __format::__formatter_chrono<_CharT> _M_f;
2283 };
2284
2285#if _GLIBCXX_USE_CXX11_ABI || ! _GLIBCXX_USE_DUAL_ABI
2286 template<typename _Duration, typename _TimeZonePtr, typename _CharT>
2287 struct formatter<chrono::zoned_time<_Duration, _TimeZonePtr>, _CharT>
2288 : formatter<chrono::__detail::__local_time_fmt_for<_Duration>, _CharT>
2289 {
2290 template<typename _FormatContext>
2291 typename _FormatContext::iterator
2292 format(const chrono::zoned_time<_Duration, _TimeZonePtr>& __tp,
2293 _FormatContext& __ctx) const
2294 {
2295 using _Ltf = chrono::__detail::__local_time_fmt_for<_Duration>;
2296 using _Base = formatter<_Ltf, _CharT>;
2297 const chrono::sys_info __info = __tp.get_info();
2298 const auto __lf = chrono::local_time_format(__tp.get_local_time(),
2299 &__info.abbrev,
2300 &__info.offset);
2301 return _Base::format(__lf, __ctx);
2302 }
2303 };
2304#endif
2305
2306 // Partial specialization needed for %c formatting of __utc_leap_second.
2307 template<typename _Duration, typename _CharT>
2308 struct formatter<chrono::__detail::__utc_leap_second<_Duration>, _CharT>
2309 : formatter<chrono::utc_time<_Duration>, _CharT>
2310 {
2311 template<typename _FormatContext>
2312 typename _FormatContext::iterator
2313 format(const chrono::__detail::__utc_leap_second<_Duration>& __t,
2314 _FormatContext& __fc) const
2315 { return this->_M_f._M_format(__t, __fc); }
2316 };
2317
2318namespace chrono
2319{
2320/// @addtogroup chrono
2321/// @{
2322
2323/// @cond undocumented
2324namespace __detail
2325{
2326 template<typename _Duration = seconds>
2327 struct _Parser
2328 {
2329 static_assert(is_same_v<common_type_t<_Duration, seconds>, _Duration>);
2330
2331 explicit
2332 _Parser(__format::_ChronoParts __need) : _M_need(__need) { }
2333
2334 _Parser(_Parser&&) = delete;
2335 void operator=(_Parser&&) = delete;
2336
2337 _Duration _M_time{}; // since midnight
2338 sys_days _M_sys_days{};
2339 year_month_day _M_ymd{};
2340 weekday _M_wd{};
2341 __format::_ChronoParts _M_need;
2342 unsigned _M_is_leap_second : 1 {};
2343 unsigned _M_reserved : 15 {};
2344
2345 template<typename _CharT, typename _Traits, typename _Alloc>
2346 basic_istream<_CharT, _Traits>&
2347 operator()(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt,
2348 basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr,
2349 minutes* __offset = nullptr);
2350
2351 private:
2352 // Read an unsigned integer from the stream and return it.
2353 // Extract no more than __n digits. Set failbit if an integer isn't read.
2354 template<typename _CharT, typename _Traits>
2355 static int_least32_t
2356 _S_read_unsigned(basic_istream<_CharT, _Traits>& __is,
2357 ios_base::iostate& __err, int __n)
2358 {
2359 int_least32_t __val = _S_try_read_digit(__is, __err);
2360 if (__val == -1) [[unlikely]]
2361 __err |= ios_base::failbit;
2362 else
2363 {
2364 int __n1 = (std::min)(__n, 9);
2365 // Cannot overflow __val unless we read more than 9 digits
2366 for (int __i = 1; __i < __n1; ++__i)
2367 if (auto __dig = _S_try_read_digit(__is, __err); __dig != -1)
2368 {
2369 __val *= 10;
2370 __val += __dig;
2371 }
2372
2373 while (__n1++ < __n) [[unlikely]]
2374 if (auto __dig = _S_try_read_digit(__is, __err); __dig != -1)
2375 {
2376 if (__builtin_mul_overflow(__val, 10, &__val)
2377 || __builtin_add_overflow(__val, __dig, &__val))
2378 {
2379 __err |= ios_base::failbit;
2380 return -1;
2381 }
2382 }
2383 }
2384 return __val;
2385 }
2386
2387 // Read an unsigned integer from the stream and return it.
2388 // Extract no more than __n digits. Set failbit if an integer isn't read.
2389 template<typename _CharT, typename _Traits>
2390 static int_least32_t
2391 _S_read_signed(basic_istream<_CharT, _Traits>& __is,
2392 ios_base::iostate& __err, int __n)
2393 {
2394 auto __sign = __is.peek();
2395 if (__sign == '-' || __sign == '+')
2396 (void) __is.get();
2397 int_least32_t __val = _S_read_unsigned(__is, __err, __n);
2398 if (__err & ios_base::failbit)
2399 {
2400 if (__sign == '-') [[unlikely]]
2401 __val *= -1;
2402 }
2403 return __val;
2404 }
2405
2406 // Read a digit from the stream and return it, or return -1.
2407 // If no digit is read eofbit will be set (but not failbit).
2408 template<typename _CharT, typename _Traits>
2409 static int_least32_t
2410 _S_try_read_digit(basic_istream<_CharT, _Traits>& __is,
2411 ios_base::iostate& __err)
2412 {
2413 int_least32_t __val = -1;
2414 auto __i = __is.peek();
2415 if (!_Traits::eq_int_type(__i, _Traits::eof())) [[likely]]
2416 {
2417 _CharT __c = _Traits::to_char_type(__i);
2418 if (_CharT('0') <= __c && __c <= _CharT('9')) [[likely]]
2419 {
2420 (void) __is.get();
2421 __val = __c - _CharT('0');
2422 }
2423 }
2424 else
2425 __err |= ios_base::eofbit;
2426 return __val;
2427 }
2428
2429 // Read the specified character and return true.
2430 // If the character is not found, set failbit and return false.
2431 template<typename _CharT, typename _Traits>
2432 static bool
2433 _S_read_chr(basic_istream<_CharT, _Traits>& __is,
2434 ios_base::iostate& __err, _CharT __c)
2435 {
2436 auto __i = __is.peek();
2437 if (_Traits::eq_int_type(__i, _Traits::eof()))
2438 __err |= ios_base::eofbit;
2439 else if (_Traits::to_char_type(__i) == __c) [[likely]]
2440 {
2441 (void) __is.get();
2442 return true;
2443 }
2444 __err |= ios_base::failbit;
2445 return false;
2446 }
2447 };
2448
2449 template<typename _Duration>
2450 using _Parser_t = _Parser<common_type_t<_Duration, seconds>>;
2451
2452 template<typename _Duration>
2453 consteval bool
2454 __use_floor()
2455 {
2456 if constexpr (_Duration::period::den == 1)
2457 {
2458 switch (_Duration::period::num)
2459 {
2460 case minutes::period::num:
2461 case hours::period::num:
2462 case days::period::num:
2463 case weeks::period::num:
2464 case years::period::num:
2465 return true;
2466 }
2467 }
2468 return false;
2469 }
2470
2471 // A "do the right thing" rounding function for duration and time_point
2472 // values extracted by from_stream. When treat_as_floating_point is true
2473 // we don't want to do anything, just a straightforward conversion.
2474 // When the destination type has a period of minutes, hours, days, weeks,
2475 // or years, we use chrono::floor to truncate towards negative infinity.
2476 // This ensures that an extracted timestamp such as 2024-09-05 13:00:00
2477 // will produce 2024-09-05 when rounded to days, rather than rounding up
2478 // to 2024-09-06 (a different day).
2479 // Otherwise, use chrono::round to get the nearest value representable
2480 // in the destination type.
2481 template<typename _ToDur, typename _Tp>
2482 constexpr auto
2483 __round(const _Tp& __t)
2484 {
2485 if constexpr (__is_duration_v<_Tp>)
2486 {
2487 if constexpr (treat_as_floating_point_v<typename _Tp::rep>)
2489 else if constexpr (__detail::__use_floor<_ToDur>())
2490 return chrono::floor<_ToDur>(__t);
2491 else
2492 return chrono::round<_ToDur>(__t);
2493 }
2494 else
2495 {
2496 static_assert(__is_time_point_v<_Tp>);
2497 using _Tpt = time_point<typename _Tp::clock, _ToDur>;
2498 return _Tpt(__detail::__round<_ToDur>(__t.time_since_epoch()));
2499 }
2500 }
2501
2502} // namespace __detail
2503/// @endcond
2504
2505 template<typename _CharT, typename _Traits, typename _Rep, typename _Period,
2506 typename _Alloc = allocator<_CharT>>
2507 inline basic_istream<_CharT, _Traits>&
2508 from_stream(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt,
2510 basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr,
2511 minutes* __offset = nullptr)
2512 {
2513 auto __need = __format::_ChronoParts::_TimeOfDay;
2514 __detail::_Parser_t<duration<_Rep, _Period>> __p(__need);
2515 if (__p(__is, __fmt, __abbrev, __offset))
2516 __d = __detail::__round<duration<_Rep, _Period>>(__p._M_time);
2517 return __is;
2518 }
2519
2520 template<typename _CharT, typename _Traits>
2521 inline basic_ostream<_CharT, _Traits>&
2522 operator<<(basic_ostream<_CharT, _Traits>& __os, const day& __d)
2523 {
2524 using _Ctx = __format::__format_context<_CharT>;
2525 using _Str = basic_string_view<_CharT>;
2526 _Str __s = _GLIBCXX_WIDEN("{:02d} is not a valid day");
2527 if (__d.ok())
2528 __s = __s.substr(0, 6);
2529 auto __u = (unsigned)__d;
2530 __os << std::vformat(__s, make_format_args<_Ctx>(__u));
2531 return __os;
2532 }
2533
2534 template<typename _CharT, typename _Traits,
2535 typename _Alloc = allocator<_CharT>>
2536 inline basic_istream<_CharT, _Traits>&
2537 from_stream(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt,
2538 day& __d,
2539 basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr,
2540 minutes* __offset = nullptr)
2541 {
2542 __detail::_Parser<> __p(__format::_ChronoParts::_Day);
2543 if (__p(__is, __fmt, __abbrev, __offset))
2544 __d = __p._M_ymd.day();
2545 return __is;
2546 }
2547
2548 template<typename _CharT, typename _Traits>
2549 inline basic_ostream<_CharT, _Traits>&
2550 operator<<(basic_ostream<_CharT, _Traits>& __os, const month& __m)
2551 {
2552 using _Ctx = __format::__format_context<_CharT>;
2553 using _Str = basic_string_view<_CharT>;
2554 _Str __s = _GLIBCXX_WIDEN("{:L%b}{} is not a valid month");
2555 if (__m.ok())
2556 __os << std::vformat(__os.getloc(), __s.substr(0, 6),
2557 make_format_args<_Ctx>(__m));
2558 else
2559 {
2560 auto __u = (unsigned)__m;
2561 __os << std::vformat(__s.substr(6), make_format_args<_Ctx>(__u));
2562 }
2563 return __os;
2564 }
2565
2566 template<typename _CharT, typename _Traits,
2567 typename _Alloc = allocator<_CharT>>
2568 inline basic_istream<_CharT, _Traits>&
2569 from_stream(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt,
2570 month& __m,
2571 basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr,
2572 minutes* __offset = nullptr)
2573 {
2574 __detail::_Parser<> __p(__format::_ChronoParts::_Month);
2575 if (__p(__is, __fmt, __abbrev, __offset))
2576 __m = __p._M_ymd.month();
2577 return __is;
2578 }
2579
2580 template<typename _CharT, typename _Traits>
2581 inline basic_ostream<_CharT, _Traits>&
2582 operator<<(basic_ostream<_CharT, _Traits>& __os, const year& __y)
2583 {
2584 using _Ctx = __format::__format_context<_CharT>;
2585 using _Str = basic_string_view<_CharT>;
2586 _Str __s = _GLIBCXX_WIDEN("-{:04d} is not a valid year");
2587 if (__y.ok())
2588 __s = __s.substr(0, 7);
2589 int __i = (int)__y;
2590 if (__i >= 0) [[likely]]
2591 __s.remove_prefix(1);
2592 else
2593 __i = -__i;
2594 __os << std::vformat(__s, make_format_args<_Ctx>(__i));
2595 return __os;
2596 }
2597
2598 template<typename _CharT, typename _Traits,
2599 typename _Alloc = allocator<_CharT>>
2600 inline basic_istream<_CharT, _Traits>&
2601 from_stream(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt,
2602 year& __y,
2603 basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr,
2604 minutes* __offset = nullptr)
2605 {
2606 __detail::_Parser<> __p(__format::_ChronoParts::_Year);
2607 if (__p(__is, __fmt, __abbrev, __offset))
2608 __y = __p._M_ymd.year();
2609 return __is;
2610 }
2611
2612 template<typename _CharT, typename _Traits>
2613 inline basic_ostream<_CharT, _Traits>&
2614 operator<<(basic_ostream<_CharT, _Traits>& __os, const weekday& __wd)
2615 {
2616 using _Ctx = __format::__format_context<_CharT>;
2617 using _Str = basic_string_view<_CharT>;
2618 _Str __s = _GLIBCXX_WIDEN("{:L%a}{} is not a valid weekday");
2619 if (__wd.ok())
2620 __os << std::vformat(__os.getloc(), __s.substr(0, 6),
2621 make_format_args<_Ctx>(__wd));
2622 else
2623 {
2624 auto __c = __wd.c_encoding();
2625 __os << std::vformat(__s.substr(6), make_format_args<_Ctx>(__c));
2626 }
2627 return __os;
2628 }
2629
2630 template<typename _CharT, typename _Traits,
2631 typename _Alloc = allocator<_CharT>>
2632 inline basic_istream<_CharT, _Traits>&
2633 from_stream(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt,
2634 weekday& __wd,
2635 basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr,
2636 minutes* __offset = nullptr)
2637 {
2638 __detail::_Parser<> __p(__format::_ChronoParts::_Weekday);
2639 if (__p(__is, __fmt, __abbrev, __offset))
2640 __wd = __p._M_wd;
2641 return __is;
2642 }
2643
2644 template<typename _CharT, typename _Traits>
2645 inline basic_ostream<_CharT, _Traits>&
2646 operator<<(basic_ostream<_CharT, _Traits>& __os,
2647 const weekday_indexed& __wdi)
2648 {
2649 // The standard says to format wdi.weekday() and wdi.index() using
2650 // either "{:L}[{}]" or "{:L}[{} is not a valid index]". The {:L} spec
2651 // means to format the weekday using ostringstream, so just do that.
2652 basic_stringstream<_CharT> __os2;
2653 __os2.imbue(__os.getloc());
2654 __os2 << __wdi.weekday();
2655 const auto __i = __wdi.index();
2656 basic_string_view<_CharT> __s
2657 = _GLIBCXX_WIDEN("[ is not a valid index]");
2658 __os2 << __s[0];
2659 __os2 << std::format(_GLIBCXX_WIDEN("{}"), __i);
2660 if (__i >= 1 && __i <= 5)
2661 __os2 << __s.back();
2662 else
2663 __os2 << __s.substr(1);
2664 __os << __os2.view();
2665 return __os;
2666 }
2667
2668 template<typename _CharT, typename _Traits>
2669 inline basic_ostream<_CharT, _Traits>&
2670 operator<<(basic_ostream<_CharT, _Traits>& __os,
2671 const weekday_last& __wdl)
2672 {
2673 // As above, just write straight to a stringstream, as if by "{:L}[last]"
2674 basic_stringstream<_CharT> __os2;
2675 __os2.imbue(__os.getloc());
2676 __os2 << __wdl.weekday() << _GLIBCXX_WIDEN("[last]");
2677 __os << __os2.view();
2678 return __os;
2679 }
2680
2681 template<typename _CharT, typename _Traits>
2682 inline basic_ostream<_CharT, _Traits>&
2683 operator<<(basic_ostream<_CharT, _Traits>& __os, const month_day& __md)
2684 {
2685 // As above, just write straight to a stringstream, as if by "{:L}/{}"
2686 basic_stringstream<_CharT> __os2;
2687 __os2.imbue(__os.getloc());
2688 __os2 << __md.month();
2689 if constexpr (is_same_v<_CharT, char>)
2690 __os2 << '/';
2691 else
2692 __os2 << L'/';
2693 __os2 << __md.day();
2694 __os << __os2.view();
2695 return __os;
2696 }
2697
2698 template<typename _CharT, typename _Traits,
2699 typename _Alloc = allocator<_CharT>>
2700 inline basic_istream<_CharT, _Traits>&
2701 from_stream(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt,
2702 month_day& __md,
2703 basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr,
2704 minutes* __offset = nullptr)
2705 {
2706 using __format::_ChronoParts;
2707 auto __need = _ChronoParts::_Month | _ChronoParts::_Day;
2708 __detail::_Parser<> __p(__need);
2709 if (__p(__is, __fmt, __abbrev, __offset))
2710 __md = month_day(__p._M_ymd.month(), __p._M_ymd.day());
2711 return __is;
2712 }
2713
2714 template<typename _CharT, typename _Traits>
2715 inline basic_ostream<_CharT, _Traits>&
2716 operator<<(basic_ostream<_CharT, _Traits>& __os,
2717 const month_day_last& __mdl)
2718 {
2719 // As above, just write straight to a stringstream, as if by "{:L}/last"
2720 basic_stringstream<_CharT> __os2;
2721 __os2.imbue(__os.getloc());
2722 __os2 << __mdl.month() << _GLIBCXX_WIDEN("/last");
2723 __os << __os2.view();
2724 return __os;
2725 }
2726
2727 template<typename _CharT, typename _Traits>
2728 inline basic_ostream<_CharT, _Traits>&
2729 operator<<(basic_ostream<_CharT, _Traits>& __os,
2730 const month_weekday& __mwd)
2731 {
2732 // As above, just write straight to a stringstream, as if by "{:L}/{:L}"
2733 basic_stringstream<_CharT> __os2;
2734 __os2.imbue(__os.getloc());
2735 __os2 << __mwd.month();
2736 if constexpr (is_same_v<_CharT, char>)
2737 __os2 << '/';
2738 else
2739 __os2 << L'/';
2740 __os2 << __mwd.weekday_indexed();
2741 __os << __os2.view();
2742 return __os;
2743 }
2744
2745 template<typename _CharT, typename _Traits>
2746 inline basic_ostream<_CharT, _Traits>&
2747 operator<<(basic_ostream<_CharT, _Traits>& __os,
2748 const month_weekday_last& __mwdl)
2749 {
2750 // As above, just write straight to a stringstream, as if by "{:L}/{:L}"
2751 basic_stringstream<_CharT> __os2;
2752 __os2.imbue(__os.getloc());
2753 __os2 << __mwdl.month();
2754 if constexpr (is_same_v<_CharT, char>)
2755 __os2 << '/';
2756 else
2757 __os2 << L'/';
2758 __os2 << __mwdl.weekday_last();
2759 __os << __os2.view();
2760 return __os;
2761 }
2762
2763 template<typename _CharT, typename _Traits>
2764 inline basic_ostream<_CharT, _Traits>&
2765 operator<<(basic_ostream<_CharT, _Traits>& __os, const year_month& __ym)
2766 {
2767 // As above, just write straight to a stringstream, as if by "{}/{:L}"
2768 basic_stringstream<_CharT> __os2;
2769 __os2.imbue(__os.getloc());
2770 __os2 << __ym.year();
2771 if constexpr (is_same_v<_CharT, char>)
2772 __os2 << '/';
2773 else
2774 __os2 << L'/';
2775 __os2 << __ym.month();
2776 __os << __os2.view();
2777 return __os;
2778 }
2779
2780 template<typename _CharT, typename _Traits,
2781 typename _Alloc = allocator<_CharT>>
2782 inline basic_istream<_CharT, _Traits>&
2783 from_stream(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt,
2784 year_month& __ym,
2785 basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr,
2786 minutes* __offset = nullptr)
2787 {
2788 using __format::_ChronoParts;
2789 auto __need = _ChronoParts::_Year | _ChronoParts::_Month;
2790 __detail::_Parser<> __p(__need);
2791 if (__p(__is, __fmt, __abbrev, __offset))
2792 __ym = year_month(__p._M_ymd.year(), __p._M_ymd.month());
2793 return __is;
2794 }
2795
2796 template<typename _CharT, typename _Traits>
2797 inline basic_ostream<_CharT, _Traits>&
2798 operator<<(basic_ostream<_CharT, _Traits>& __os,
2799 const year_month_day& __ymd)
2800 {
2801 using _Ctx = __format::__format_context<_CharT>;
2802 using _Str = basic_string_view<_CharT>;
2803 _Str __s = _GLIBCXX_WIDEN("{:%F} is not a valid date");
2804 __os << std::vformat(__ymd.ok() ? __s.substr(0, 5) : __s,
2805 make_format_args<_Ctx>(__ymd));
2806 return __os;
2807 }
2808
2809 template<typename _CharT, typename _Traits,
2810 typename _Alloc = allocator<_CharT>>
2811 inline basic_istream<_CharT, _Traits>&
2812 from_stream(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt,
2813 year_month_day& __ymd,
2814 basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr,
2815 minutes* __offset = nullptr)
2816 {
2817 using __format::_ChronoParts;
2818 auto __need = _ChronoParts::_Year | _ChronoParts::_Month
2819 | _ChronoParts::_Day;
2820 __detail::_Parser<> __p(__need);
2821 if (__p(__is, __fmt, __abbrev, __offset))
2822 __ymd = __p._M_ymd;
2823 return __is;
2824 }
2825
2826 template<typename _CharT, typename _Traits>
2829 const year_month_day_last& __ymdl)
2830 {
2831 // As above, just write straight to a stringstream, as if by "{}/{:L}"
2833 __os2.imbue(__os.getloc());
2834 __os2 << __ymdl.year();
2835 if constexpr (is_same_v<_CharT, char>)
2836 __os2 << '/';
2837 else
2838 __os2 << L'/';
2839 __os2 << __ymdl.month_day_last();
2840 __os << __os2.view();
2841 return __os;
2842 }
2843
2844 template<typename _CharT, typename _Traits>
2845 inline basic_ostream<_CharT, _Traits>&
2846 operator<<(basic_ostream<_CharT, _Traits>& __os,
2847 const year_month_weekday& __ymwd)
2848 {
2849 // As above, just write straight to a stringstream, as if by
2850 // "{}/{:L}/{:L}"
2851 basic_stringstream<_CharT> __os2;
2852 __os2.imbue(__os.getloc());
2853 _CharT __slash;
2854 if constexpr (is_same_v<_CharT, char>)
2855 __slash = '/';
2856 else
2857 __slash = L'/';
2858 __os2 << __ymwd.year() << __slash << __ymwd.month() << __slash
2859 << __ymwd.weekday_indexed();
2860 __os << __os2.view();
2861 return __os;
2862 }
2863
2864 template<typename _CharT, typename _Traits>
2865 inline basic_ostream<_CharT, _Traits>&
2866 operator<<(basic_ostream<_CharT, _Traits>& __os,
2867 const year_month_weekday_last& __ymwdl)
2868 {
2869 // As above, just write straight to a stringstream, as if by
2870 // "{}/{:L}/{:L}"
2871 basic_stringstream<_CharT> __os2;
2872 __os2.imbue(__os.getloc());
2873 _CharT __slash;
2874 if constexpr (is_same_v<_CharT, char>)
2875 __slash = '/';
2876 else
2877 __slash = L'/';
2878 __os2 << __ymwdl.year() << __slash << __ymwdl.month() << __slash
2879 << __ymwdl.weekday_last();
2880 __os << __os2.view();
2881 return __os;
2882 }
2883
2884 template<typename _CharT, typename _Traits, typename _Duration>
2885 inline basic_ostream<_CharT, _Traits>&
2886 operator<<(basic_ostream<_CharT, _Traits>& __os,
2887 const hh_mm_ss<_Duration>& __hms)
2888 {
2889 return __os << format(__os.getloc(), _GLIBCXX_WIDEN("{:L%T}"), __hms);
2890 }
2891
2892#if _GLIBCXX_USE_CXX11_ABI || ! _GLIBCXX_USE_DUAL_ABI
2893 /// Writes a sys_info object to an ostream in an unspecified format.
2894 template<typename _CharT, typename _Traits>
2895 basic_ostream<_CharT, _Traits>&
2896 operator<<(basic_ostream<_CharT, _Traits>& __os, const sys_info& __i)
2897 {
2898 __os << '[' << __i.begin << ',' << __i.end
2899 << ',' << hh_mm_ss(__i.offset) << ',' << __i.save
2900 << ',' << __i.abbrev << ']';
2901 return __os;
2902 }
2903
2904 /// Writes a local_info object to an ostream in an unspecified format.
2905 template<typename _CharT, typename _Traits>
2906 basic_ostream<_CharT, _Traits>&
2907 operator<<(basic_ostream<_CharT, _Traits>& __os, const local_info& __li)
2908 {
2909 __os << '[';
2910 if (__li.result == local_info::unique)
2911 __os << __li.first;
2912 else
2913 {
2914 if (__li.result == local_info::nonexistent)
2915 __os << "nonexistent";
2916 else
2917 __os << "ambiguous";
2918 __os << " local time between " << __li.first;
2919 __os << " and " << __li.second;
2920 }
2921 __os << ']';
2922 return __os;
2923 }
2924
2925 template<typename _CharT, typename _Traits, typename _Duration,
2926 typename _TimeZonePtr>
2927 inline basic_ostream<_CharT, _Traits>&
2928 operator<<(basic_ostream<_CharT, _Traits>& __os,
2929 const zoned_time<_Duration, _TimeZonePtr>& __t)
2930 {
2931 __os << format(__os.getloc(), _GLIBCXX_WIDEN("{:L%F %T %Z}"), __t);
2932 return __os;
2933 }
2934#endif
2935
2936 template<typename _CharT, typename _Traits, typename _Duration>
2937 requires (!treat_as_floating_point_v<typename _Duration::rep>)
2938 && ratio_less_v<typename _Duration::period, days::period>
2939 inline basic_ostream<_CharT, _Traits>&
2940 operator<<(basic_ostream<_CharT, _Traits>& __os,
2941 const sys_time<_Duration>& __tp)
2942 {
2943 __os << std::format(__os.getloc(), _GLIBCXX_WIDEN("{:L%F %T}"), __tp);
2944 return __os;
2945 }
2946
2947 template<typename _CharT, typename _Traits>
2948 inline basic_ostream<_CharT, _Traits>&
2949 operator<<(basic_ostream<_CharT, _Traits>& __os, const sys_days& __dp)
2950 {
2951 __os << year_month_day{__dp};
2952 return __os;
2953 }
2954
2955 template<typename _CharT, typename _Traits, typename _Duration,
2956 typename _Alloc = allocator<_CharT>>
2957 basic_istream<_CharT, _Traits>&
2958 from_stream(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt,
2959 sys_time<_Duration>& __tp,
2960 basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr,
2961 minutes* __offset = nullptr)
2962 {
2963 minutes __off{};
2964 if (!__offset)
2965 __offset = &__off;
2966 using __format::_ChronoParts;
2967 auto __need = _ChronoParts::_Year | _ChronoParts::_Month
2968 | _ChronoParts::_Day | _ChronoParts::_TimeOfDay;
2969 __detail::_Parser_t<_Duration> __p(__need);
2970 if (__p(__is, __fmt, __abbrev, __offset))
2971 {
2972 if (__p._M_is_leap_second)
2973 __is.setstate(ios_base::failbit);
2974 else
2975 {
2976 auto __st = __p._M_sys_days + __p._M_time - *__offset;
2977 __tp = __detail::__round<_Duration>(__st);
2978 }
2979 }
2980 return __is;
2981 }
2982
2983 template<typename _CharT, typename _Traits, typename _Duration>
2984 inline basic_ostream<_CharT, _Traits>&
2985 operator<<(basic_ostream<_CharT, _Traits>& __os,
2986 const utc_time<_Duration>& __t)
2987 {
2988 __os << std::format(__os.getloc(), _GLIBCXX_WIDEN("{:L%F %T}"), __t);
2989 return __os;
2990 }
2991
2992 template<typename _CharT, typename _Traits, typename _Duration,
2993 typename _Alloc = allocator<_CharT>>
2994 inline basic_istream<_CharT, _Traits>&
2995 from_stream(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt,
2996 utc_time<_Duration>& __tp,
2997 basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr,
2998 minutes* __offset = nullptr)
2999 {
3000 minutes __off{};
3001 if (!__offset)
3002 __offset = &__off;
3003 using __format::_ChronoParts;
3004 auto __need = _ChronoParts::_Year | _ChronoParts::_Month
3005 | _ChronoParts::_Day | _ChronoParts::_TimeOfDay;
3006 __detail::_Parser_t<_Duration> __p(__need);
3007 if (__p(__is, __fmt, __abbrev, __offset))
3008 {
3009 // Converting to utc_time before adding _M_time is necessary for
3010 // "23:59:60" to correctly produce a time within a leap second.
3011 auto __ut = utc_clock::from_sys(__p._M_sys_days) + __p._M_time
3012 - *__offset;
3013 __tp = __detail::__round<_Duration>(__ut);
3014 }
3015 return __is;
3016 }
3017
3018 template<typename _CharT, typename _Traits, typename _Duration>
3019 inline basic_ostream<_CharT, _Traits>&
3020 operator<<(basic_ostream<_CharT, _Traits>& __os,
3021 const tai_time<_Duration>& __t)
3022 {
3023 __os << std::format(__os.getloc(), _GLIBCXX_WIDEN("{:L%F %T}"), __t);
3024 return __os;
3025 }
3026
3027 template<typename _CharT, typename _Traits, typename _Duration,
3028 typename _Alloc = allocator<_CharT>>
3029 inline basic_istream<_CharT, _Traits>&
3030 from_stream(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt,
3031 tai_time<_Duration>& __tp,
3032 basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr,
3033 minutes* __offset = nullptr)
3034 {
3035 minutes __off{};
3036 if (!__offset)
3037 __offset = &__off;
3038 using __format::_ChronoParts;
3039 auto __need = _ChronoParts::_Year | _ChronoParts::_Month
3040 | _ChronoParts::_Day | _ChronoParts::_TimeOfDay;
3041 __detail::_Parser_t<_Duration> __p(__need);
3042 if (__p(__is, __fmt, __abbrev, __offset))
3043 {
3044 if (__p._M_is_leap_second)
3045 __is.setstate(ios_base::failbit);
3046 else
3047 {
3048 constexpr sys_days __epoch(-days(4383)); // 1958y/1/1
3049 auto __d = __p._M_sys_days - __epoch + __p._M_time - *__offset;
3050 tai_time<common_type_t<_Duration, seconds>> __tt(__d);
3051 __tp = __detail::__round<_Duration>(__tt);
3052 }
3053 }
3054 return __is;
3055 }
3056
3057 template<typename _CharT, typename _Traits, typename _Duration>
3058 inline basic_ostream<_CharT, _Traits>&
3059 operator<<(basic_ostream<_CharT, _Traits>& __os,
3060 const gps_time<_Duration>& __t)
3061 {
3062 __os << std::format(__os.getloc(), _GLIBCXX_WIDEN("{:L%F %T}"), __t);
3063 return __os;
3064 }
3065
3066 template<typename _CharT, typename _Traits, typename _Duration,
3067 typename _Alloc = allocator<_CharT>>
3068 inline basic_istream<_CharT, _Traits>&
3069 from_stream(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt,
3070 gps_time<_Duration>& __tp,
3071 basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr,
3072 minutes* __offset = nullptr)
3073 {
3074 minutes __off{};
3075 if (!__offset)
3076 __offset = &__off;
3077 using __format::_ChronoParts;
3078 auto __need = _ChronoParts::_Year | _ChronoParts::_Month
3079 | _ChronoParts::_Day | _ChronoParts::_TimeOfDay;
3080 __detail::_Parser_t<_Duration> __p(__need);
3081 if (__p(__is, __fmt, __abbrev, __offset))
3082 {
3083 if (__p._M_is_leap_second)
3084 __is.setstate(ios_base::failbit);
3085 else
3086 {
3087 constexpr sys_days __epoch(days(3657)); // 1980y/1/Sunday[1]
3088 auto __d = __p._M_sys_days - __epoch + __p._M_time - *__offset;
3089 gps_time<common_type_t<_Duration, seconds>> __gt(__d);
3090 __tp = __detail::__round<_Duration>(__gt);
3091 }
3092 }
3093 return __is;
3094 }
3095
3096 template<typename _CharT, typename _Traits, typename _Duration>
3097 inline basic_ostream<_CharT, _Traits>&
3098 operator<<(basic_ostream<_CharT, _Traits>& __os,
3099 const file_time<_Duration>& __t)
3100 {
3101 __os << std::format(__os.getloc(), _GLIBCXX_WIDEN("{:L%F %T}"), __t);
3102 return __os;
3103 }
3104
3105 template<typename _CharT, typename _Traits, typename _Duration,
3106 typename _Alloc = allocator<_CharT>>
3107 inline basic_istream<_CharT, _Traits>&
3108 from_stream(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt,
3109 file_time<_Duration>& __tp,
3110 basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr,
3111 minutes* __offset = nullptr)
3112 {
3113 sys_time<_Duration> __st;
3114 if (chrono::from_stream(__is, __fmt, __st, __abbrev, __offset))
3115 __tp = __detail::__round<_Duration>(file_clock::from_sys(__st));
3116 return __is;
3117 }
3118
3119 template<typename _CharT, typename _Traits, typename _Duration>
3120 inline basic_ostream<_CharT, _Traits>&
3121 operator<<(basic_ostream<_CharT, _Traits>& __os,
3122 const local_time<_Duration>& __lt)
3123 {
3124 __os << sys_time<_Duration>{__lt.time_since_epoch()};
3125 return __os;
3126 }
3127
3128 template<typename _CharT, typename _Traits, typename _Duration,
3129 typename _Alloc = allocator<_CharT>>
3130 basic_istream<_CharT, _Traits>&
3131 from_stream(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt,
3132 local_time<_Duration>& __tp,
3133 basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr,
3134 minutes* __offset = nullptr)
3135 {
3136 using __format::_ChronoParts;
3137 auto __need = _ChronoParts::_Year | _ChronoParts::_Month
3138 | _ChronoParts::_Day | _ChronoParts::_TimeOfDay;
3139 __detail::_Parser_t<_Duration> __p(__need);
3140 if (__p(__is, __fmt, __abbrev, __offset))
3141 {
3142 days __d = __p._M_sys_days.time_since_epoch();
3143 auto __t = local_days(__d) + __p._M_time; // ignore offset
3144 __tp = __detail::__round<_Duration>(__t);
3145 }
3146 return __is;
3147 }
3148
3149 // [time.parse] parsing
3150
3151namespace __detail
3152{
3153 // _GLIBCXX_RESOLVE_LIB_DEFECTS
3154 // 3956. chrono::parse uses from_stream as a customization point
3155 void from_stream() = delete;
3156
3157 template<typename _Parsable, typename _CharT,
3158 typename _Traits = std::char_traits<_CharT>,
3159 typename... _OptArgs>
3160 concept __parsable = requires (basic_istream<_CharT, _Traits>& __is,
3161 const _CharT* __fmt, _Parsable& __tp,
3162 _OptArgs*... __args)
3163 { from_stream(__is, __fmt, __tp, __args...); };
3164
3165 template<typename _Parsable, typename _CharT,
3166 typename _Traits = char_traits<_CharT>,
3167 typename _Alloc = allocator<_CharT>>
3168 struct _Parse
3169 {
3170 private:
3171 using __string_type = basic_string<_CharT, _Traits, _Alloc>;
3172
3173 public:
3174 _Parse(const _CharT* __fmt, _Parsable& __tp,
3175 basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr,
3176 minutes* __offset = nullptr)
3177 : _M_fmt(__fmt), _M_tp(std::__addressof(__tp)),
3178 _M_abbrev(__abbrev), _M_offset(__offset)
3179 { }
3180
3181 _Parse(_Parse&&) = delete;
3182 _Parse& operator=(_Parse&&) = delete;
3183
3184 private:
3185 using __stream_type = basic_istream<_CharT, _Traits>;
3186
3187 const _CharT* const _M_fmt;
3188 _Parsable* const _M_tp;
3189 __string_type* const _M_abbrev;
3190 minutes* const _M_offset;
3191
3192 friend __stream_type&
3193 operator>>(__stream_type& __is, _Parse&& __p)
3194 {
3195 if (__p._M_offset)
3196 from_stream(__is, __p._M_fmt, *__p._M_tp, __p._M_abbrev,
3197 __p._M_offset);
3198 else if (__p._M_abbrev)
3199 from_stream(__is, __p._M_fmt, *__p._M_tp, __p._M_abbrev);
3200 else
3201 from_stream(__is, __p._M_fmt, *__p._M_tp);
3202 return __is;
3203 }
3204
3205 friend void operator>>(__stream_type&, _Parse&) = delete;
3206 friend void operator>>(__stream_type&, const _Parse&) = delete;
3207 };
3208} // namespace __detail
3209
3210 template<typename _CharT, __detail::__parsable<_CharT> _Parsable>
3211 [[nodiscard, __gnu__::__access__(__read_only__, 1)]]
3212 inline auto
3213 parse(const _CharT* __fmt, _Parsable& __tp)
3214 { return __detail::_Parse<_Parsable, _CharT>(__fmt, __tp); }
3215
3216 template<typename _CharT, typename _Traits, typename _Alloc,
3217 __detail::__parsable<_CharT, _Traits> _Parsable>
3218 [[nodiscard]]
3219 inline auto
3220 parse(const basic_string<_CharT, _Traits, _Alloc>& __fmt, _Parsable& __tp)
3221 {
3222 return __detail::_Parse<_Parsable, _CharT, _Traits>(__fmt.c_str(), __tp);
3223 }
3224
3225 template<typename _CharT, typename _Traits, typename _Alloc,
3226 typename _StrT = basic_string<_CharT, _Traits, _Alloc>,
3227 __detail::__parsable<_CharT, _Traits, _StrT> _Parsable>
3228 [[nodiscard, __gnu__::__access__(__read_only__, 1)]]
3229 inline auto
3230 parse(const _CharT* __fmt, _Parsable& __tp,
3231 basic_string<_CharT, _Traits, _Alloc>& __abbrev)
3232 {
3233 auto __pa = std::__addressof(__abbrev);
3234 return __detail::_Parse<_Parsable, _CharT, _Traits, _Alloc>(__fmt, __tp,
3235 __pa);
3236 }
3237
3238 template<typename _CharT, typename _Traits, typename _Alloc,
3239 typename _StrT = basic_string<_CharT, _Traits, _Alloc>,
3240 __detail::__parsable<_CharT, _Traits, _StrT> _Parsable>
3241 [[nodiscard]]
3242 inline auto
3243 parse(const basic_string<_CharT, _Traits, _Alloc>& __fmt, _Parsable& __tp,
3244 basic_string<_CharT, _Traits, _Alloc>& __abbrev)
3245 {
3246 auto __pa = std::__addressof(__abbrev);
3247 return __detail::_Parse<_Parsable, _CharT, _Traits, _Alloc>(__fmt.c_str(),
3248 __tp, __pa);
3249 }
3250
3251 template<typename _CharT, typename _Traits = char_traits<_CharT>,
3252 typename _StrT = basic_string<_CharT, _Traits>,
3253 __detail::__parsable<_CharT, _Traits, _StrT, minutes> _Parsable>
3254 [[nodiscard, __gnu__::__access__(__read_only__, 1)]]
3255 inline auto
3256 parse(const _CharT* __fmt, _Parsable& __tp, minutes& __offset)
3257 {
3258 return __detail::_Parse<_Parsable, _CharT>(__fmt, __tp, nullptr,
3259 &__offset);
3260 }
3261
3262 template<typename _CharT, typename _Traits, typename _Alloc,
3263 typename _StrT = basic_string<_CharT, _Traits>,
3264 __detail::__parsable<_CharT, _Traits, _StrT, minutes> _Parsable>
3265 [[nodiscard]]
3266 inline auto
3267 parse(const basic_string<_CharT, _Traits, _Alloc>& __fmt, _Parsable& __tp,
3268 minutes& __offset)
3269 {
3270 return __detail::_Parse<_Parsable, _CharT, _Traits, _Alloc>(__fmt.c_str(),
3271 __tp, nullptr,
3272 &__offset);
3273 }
3274
3275 template<typename _CharT, typename _Traits, typename _Alloc,
3276 typename _StrT = basic_string<_CharT, _Traits, _Alloc>,
3277 __detail::__parsable<_CharT, _Traits, _StrT, minutes> _Parsable>
3278 [[nodiscard, __gnu__::__access__(__read_only__, 1)]]
3279 inline auto
3280 parse(const _CharT* __fmt, _Parsable& __tp,
3281 basic_string<_CharT, _Traits, _Alloc>& __abbrev, minutes& __offset)
3282 {
3283 auto __pa = std::__addressof(__abbrev);
3284 return __detail::_Parse<_Parsable, _CharT, _Traits, _Alloc>(__fmt, __tp,
3285 __pa,
3286 &__offset);
3287 }
3288
3289 template<typename _CharT, typename _Traits, typename _Alloc,
3290 typename _StrT = basic_string<_CharT, _Traits, _Alloc>,
3291 __detail::__parsable<_CharT, _Traits, _StrT, minutes> _Parsable>
3292 [[nodiscard]]
3293 inline auto
3294 parse(const basic_string<_CharT, _Traits, _Alloc>& __fmt, _Parsable& __tp,
3295 basic_string<_CharT, _Traits, _Alloc>& __abbrev, minutes& __offset)
3296 {
3297 auto __pa = std::__addressof(__abbrev);
3298 return __detail::_Parse<_Parsable, _CharT, _Traits, _Alloc>(__fmt.c_str(),
3299 __tp, __pa,
3300 &__offset);
3301 }
3302
3303 /// @cond undocumented
3304 template<typename _Duration>
3305 template<typename _CharT, typename _Traits, typename _Alloc>
3306 basic_istream<_CharT, _Traits>&
3307 __detail::_Parser<_Duration>::
3308 operator()(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt,
3309 basic_string<_CharT, _Traits, _Alloc>* __abbrev,
3310 minutes* __offset)
3311 {
3312 using sentry = typename basic_istream<_CharT, _Traits>::sentry;
3314 if (sentry __cerb(__is, true); __cerb)
3315 {
3316 locale __loc = __is.getloc();
3317 auto& __tmget = std::use_facet<std::time_get<_CharT>>(__loc);
3318 auto& __tmpunct = std::use_facet<std::__timepunct<_CharT>>(__loc);
3319
3320 // RAII type to save and restore stream state.
3321 struct _Stream_state
3322 {
3323 explicit
3324 _Stream_state(basic_istream<_CharT, _Traits>& __i)
3325 : _M_is(__i),
3326 _M_flags(__i.flags(ios_base::skipws | ios_base::dec)),
3327 _M_w(__i.width(0))
3328 { }
3329
3330 ~_Stream_state()
3331 {
3332 _M_is.flags(_M_flags);
3333 _M_is.width(_M_w);
3334 }
3335
3336 _Stream_state(_Stream_state&&) = delete;
3337
3338 basic_istream<_CharT, _Traits>& _M_is;
3339 ios_base::fmtflags _M_flags;
3340 streamsize _M_w;
3341 };
3342
3343 auto __is_failed = [](ios_base::iostate __e) {
3344 return static_cast<bool>(__e & ios_base::failbit);
3345 };
3346
3347 // Read an unsigned integer from the stream and return it.
3348 // Extract no more than __n digits. Set __err on error.
3349 auto __read_unsigned = [&] (int __n) {
3350 return _S_read_unsigned(__is, __err, __n);
3351 };
3352
3353 // Read a signed integer from the stream and return it.
3354 // Extract no more than __n digits. Set __err on error.
3355 auto __read_signed = [&] (int __n) {
3356 return _S_read_signed(__is, __err, __n);
3357 };
3358
3359 // Read an expected character from the stream.
3360 auto __read_chr = [&__is, &__err] (_CharT __c) {
3361 return _S_read_chr(__is, __err, __c);
3362 };
3363
3364 using __format::_ChronoParts;
3365 _ChronoParts __parts{};
3366
3367 const year __bad_y = --year::min(); // SHRT_MIN
3368 const month __bad_mon(255);
3369 const day __bad_day(255);
3370 const weekday __bad_wday(255);
3371 const hours __bad_h(-1);
3372 const minutes __bad_min(-9999);
3373 const seconds __bad_sec(-1);
3374
3375 year __y = __bad_y, __yy = __bad_y; // %Y, %yy
3376 year __iso_y = __bad_y, __iso_yy = __bad_y; // %G, %g
3377 month __m = __bad_mon; // %m
3378 day __d = __bad_day; // %d
3379 weekday __wday = __bad_wday; // %a %A %u %w
3380 hours __h = __bad_h, __h12 = __bad_h; // %H, %I
3381 minutes __min = __bad_min; // %M
3382 _Duration __s = __bad_sec; // %S
3383 int __ampm = 0; // %p
3384 int __iso_wk = -1, __sunday_wk = -1, __monday_wk = -1; // %V, %U, %W
3385 int __century = -1; // %C
3386 int __dayofyear = -1; // %j (for non-duration)
3387
3388 minutes __tz_offset = __bad_min;
3389 basic_string<_CharT, _Traits> __tz_abbr;
3390
3391 if ((_M_need & _ChronoParts::_TimeOfDay)
3392 && (_M_need & _ChronoParts::_Year))
3393 {
3394 // For time_points assume "00:00:00" is implicitly present,
3395 // so we don't fail to parse if it's not (PR libstdc++/114240).
3396 // We will still fail to parse if there's no year+month+day.
3397 __h = hours(0);
3398 __parts = _ChronoParts::_TimeOfDay;
3399 }
3400
3401 // bool __is_neg = false; // TODO: how is this handled for parsing?
3402
3403 _CharT __mod{}; // One of 'E' or 'O' or nul.
3404 unsigned __num = 0; // Non-zero for N modifier.
3405 bool __is_flag = false; // True if we're processing a % flag.
3406
3407 constexpr bool __is_floating
3408 = treat_as_floating_point_v<typename _Duration::rep>;
3409
3410 // If an out-of-range value is extracted (e.g. 61min for %M),
3411 // do not set failbit immediately because we might not need it
3412 // (e.g. parsing chrono::year doesn't care about invalid %M values).
3413 // Instead set the variable back to its initial 'bad' state,
3414 // and also set related variables corresponding to the same field
3415 // (e.g. a bad %M value for __min should also reset __h and __s).
3416 // If a valid value is needed later the bad value will cause failure.
3417
3418 // For some fields we don't know the correct range when parsing and
3419 // we have to be liberal in what we accept, e.g. we allow 366 for
3420 // day-of-year because that's valid in leap years, and we allow 31
3421 // for day-of-month. If those values are needed to determine the
3422 // result then we can do a correct range check at the end when we
3423 // know the how many days the relevant year or month actually has.
3424
3425 while (*__fmt)
3426 {
3427 _CharT __c = *__fmt++;
3428 if (!__is_flag)
3429 {
3430 if (__c == '%')
3431 __is_flag = true; // This is the start of a flag.
3432 else if (std::isspace(__c, __loc))
3433 std::ws(__is); // Match zero or more whitespace characters.
3434 else if (!__read_chr(__c)) [[unlikely]]
3435 break; // Failed to match the expected character.
3436
3437 continue; // Process next character in the format string.
3438 }
3439
3440 // Now processing a flag.
3441 switch (__c)
3442 {
3443 case 'a': // Locale's weekday name
3444 case 'A': // (full or abbreviated, matched case-insensitively).
3445 if (__mod || __num) [[unlikely]]
3446 __err = ios_base::failbit;
3447 else
3448 {
3449 struct tm __tm{};
3450 __tmget.get(__is, {}, __is, __err, &__tm,
3451 __fmt - 2, __fmt);
3452 if (!__is_failed(__err))
3453 __wday = weekday(__tm.tm_wday);
3454 }
3455 __parts |= _ChronoParts::_Weekday;
3456 break;
3457
3458 case 'b': // Locale's month name
3459 case 'h': // (full or abbreviated, matched case-insensitively).
3460 case 'B':
3461 if (__mod || __num) [[unlikely]]
3462 __err = ios_base::failbit;
3463 else
3464 {
3465 // strptime behaves differently for %b and %B,
3466 // but chrono::parse says they're equivalent.
3467 // Luckily libstdc++ std::time_get works as needed.
3468 struct tm __tm{};
3469 __tmget.get(__is, {}, __is, __err, &__tm,
3470 __fmt - 2, __fmt);
3471 if (!__is_failed(__err))
3472 __m = month(__tm.tm_mon + 1);
3473 }
3474 __parts |= _ChronoParts::_Month;
3475 break;
3476
3477 case 'c': // Locale's date and time representation.
3478 if (__mod == 'O' || __num) [[unlikely]]
3479 __err |= ios_base::failbit;
3480 else
3481 {
3482 struct tm __tm{};
3483 __tmget.get(__is, {}, __is, __err, &__tm,
3484 __fmt - 2 - (__mod == 'E'), __fmt);
3485 if (!__is_failed(__err))
3486 {
3487 __y = year(__tm.tm_year + 1900);
3488 __m = month(__tm.tm_mon + 1);
3489 __d = day(__tm.tm_mday);
3490 __h = hours(__tm.tm_hour);
3491 __min = minutes(__tm.tm_min);
3492 __s = seconds(__tm.tm_sec);
3493 }
3494 }
3495 __parts |= _ChronoParts::_DateTime;
3496 break;
3497
3498 case 'C': // Century
3499 if (!__mod) [[likely]]
3500 {
3501 auto __v = __read_signed(__num ? __num : 2);
3502 if (!__is_failed(__err))
3503 {
3504 int __cmin = (int)year::min() / 100;
3505 int __cmax = (int)year::max() / 100;
3506 if (__cmin <= __v && __v <= __cmax)
3507 __century = __v * 100;
3508 else
3509 __century = -2; // This prevents guessing century.
3510 }
3511 }
3512 else if (__mod == 'E')
3513 {
3514 struct tm __tm{};
3515 __tmget.get(__is, {}, __is, __err, &__tm,
3516 __fmt - 3, __fmt);
3517 if (!__is_failed(__err))
3518 __century = __tm.tm_year;
3519 }
3520 else [[unlikely]]
3521 __err |= ios_base::failbit;
3522 // N.B. don't set this here: __parts |= _ChronoParts::_Year;
3523 break;
3524
3525 case 'd': // Day of month (1-31)
3526 case 'e':
3527 if (!__mod) [[likely]]
3528 {
3529 auto __v = __read_unsigned(__num ? __num : 2);
3530 if (!__is_failed(__err))
3531 __d = day(__v);
3532 }
3533 else if (__mod == 'O')
3534 {
3535 struct tm __tm{};
3536 __tmget.get(__is, {}, __is, __err, &__tm,
3537 __fmt - 3, __fmt);
3538 if (!__is_failed(__err))
3539 __d = day(__tm.tm_mday);
3540 }
3541 else [[unlikely]]
3542 __err |= ios_base::failbit;
3543 __parts |= _ChronoParts::_Day;
3544 break;
3545
3546 case 'D': // %m/%d/%y
3547 if (__mod || __num) [[unlikely]]
3548 __err |= ios_base::failbit;
3549 else
3550 {
3551 auto __month = __read_unsigned(2); // %m
3552 __read_chr('/');
3553 auto __day = __read_unsigned(2); // %d
3554 __read_chr('/');
3555 auto __year = __read_unsigned(2); // %y
3556 if (__is_failed(__err))
3557 break;
3558 __y = year(__year + 1900 + 100 * int(__year < 69));
3559 __m = month(__month);
3560 __d = day(__day);
3561 if (!year_month_day(__y, __m, __d).ok())
3562 {
3563 __y = __yy = __iso_y = __iso_yy = __bad_y;
3564 __m = __bad_mon;
3565 __d = __bad_day;
3566 break;
3567 }
3568 }
3569 __parts |= _ChronoParts::_Date;
3570 break;
3571
3572 case 'F': // %Y-%m-%d - any N modifier only applies to %Y.
3573 if (__mod) [[unlikely]]
3574 __err |= ios_base::failbit;
3575 else
3576 {
3577 auto __year = __read_signed(__num ? __num : 4); // %Y
3578 __read_chr('-');
3579 auto __month = __read_unsigned(2); // %m
3580 __read_chr('-');
3581 auto __day = __read_unsigned(2); // %d
3582 if (__is_failed(__err))
3583 break;
3584 __y = year(__year);
3585 __m = month(__month);
3586 __d = day(__day);
3587 if (!year_month_day(__y, __m, __d).ok())
3588 {
3589 __y = __yy = __iso_y = __iso_yy = __bad_y;
3590 __m = __bad_mon;
3591 __d = __bad_day;
3592 break;
3593 }
3594 }
3595 __parts |= _ChronoParts::_Date;
3596 break;
3597
3598 case 'g': // Last two digits of ISO week-based year.
3599 if (__mod) [[unlikely]]
3600 __err |= ios_base::failbit;
3601 else
3602 {
3603 auto __val = __read_unsigned(__num ? __num : 2);
3604 if (__val >= 0 && __val <= 99)
3605 {
3606 __iso_yy = year(__val);
3607 if (__century == -1) // No %C has been parsed yet.
3608 __century = 2000;
3609 }
3610 else
3611 __iso_yy = __iso_y = __y = __yy = __bad_y;
3612 }
3613 __parts |= _ChronoParts::_Year;
3614 break;
3615
3616 case 'G': // ISO week-based year.
3617 if (__mod) [[unlikely]]
3618 __err |= ios_base::failbit;
3619 else
3620 __iso_y = year(__read_unsigned(__num ? __num : 4));
3621 __parts |= _ChronoParts::_Year;
3622 break;
3623
3624 case 'H': // 24-hour (00-23)
3625 case 'I': // 12-hour (1-12)
3626 if (__mod == 'E') [[unlikely]]
3627 __err |= ios_base::failbit;
3628 else if (__mod == 'O')
3629 {
3630#if 0
3631 struct tm __tm{};
3632 __tm.tm_ampm = 1;
3633 __tmget.get(__is, {}, __is, __err, &__tm,
3634 __fmt - 3, __fmt);
3635 if (!__is_failed(__err))
3636 {
3637 if (__c == 'I')
3638 {
3639 __h12 = hours(__tm.tm_hour);
3640 __h = __bad_h;
3641 }
3642 else
3643 __h = hours(__tm.tm_hour);
3644 }
3645#else
3646 // XXX %OI seems to be unimplementable.
3647 __err |= ios_base::failbit;
3648#endif
3649 }
3650 else
3651 {
3652 auto __val = __read_unsigned(__num ? __num : 2);
3653 if (__c == 'I' && __val >= 1 && __val <= 12)
3654 {
3655 __h12 = hours(__val);
3656 __h = __bad_h;
3657 }
3658 else if (__c == 'H' && __val >= 0 && __val <= 23)
3659 {
3660 __h = hours(__val);
3661 __h12 = __bad_h;
3662 }
3663 else
3664 {
3665 if (_M_need & _ChronoParts::_TimeOfDay)
3666 __err |= ios_base::failbit;
3667 break;
3668 }
3669 }
3670 __parts |= _ChronoParts::_TimeOfDay;
3671 break;
3672
3673 case 'j': // For duration, count of days, otherwise day of year
3674 if (__mod) [[unlikely]]
3675 __err |= ios_base::failbit;
3676 else if (_M_need == _ChronoParts::_TimeOfDay) // duration
3677 {
3678 auto __val = __read_signed(__num ? __num : 3);
3679 if (!__is_failed(__err))
3680 {
3681 __h = days(__val); // __h will get added to _M_time
3682 __parts |= _ChronoParts::_TimeOfDay;
3683 }
3684 }
3685 else
3686 {
3687 __dayofyear = __read_unsigned(__num ? __num : 3);
3688 // N.B. do not alter __parts here, done after loop.
3689 // No need for range checking here either.
3690 }
3691 break;
3692
3693 case 'm': // Month (1-12)
3694 if (__mod == 'E') [[unlikely]]
3695 __err |= ios_base::failbit;
3696 else if (__mod == 'O')
3697 {
3698 struct tm __tm{};
3699 __tmget.get(__is, {}, __is, __err, &__tm,
3700 __fmt - 2, __fmt);
3701 if (!__is_failed(__err))
3702 __m = month(__tm.tm_mon + 1);
3703 }
3704 else
3705 {
3706 auto __val = __read_unsigned(__num ? __num : 2);
3707 if (__val >= 1 && __val <= 12)
3708 __m = month(__val);
3709 else
3710 __m = __bad_mon;
3711 }
3712 __parts |= _ChronoParts::_Month;
3713 break;
3714
3715 case 'M': // Minutes
3716 if (__mod == 'E') [[unlikely]]
3717 __err |= ios_base::failbit;
3718 else if (__mod == 'O')
3719 {
3720 struct tm __tm{};
3721 __tmget.get(__is, {}, __is, __err, &__tm,
3722 __fmt - 2, __fmt);
3723 if (!__is_failed(__err))
3724 __min = minutes(__tm.tm_min);
3725 }
3726 else
3727 {
3728 auto __val = __read_unsigned(__num ? __num : 2);
3729 if (0 <= __val && __val < 60)
3730 __min = minutes(__val);
3731 else
3732 {
3733 if (_M_need & _ChronoParts::_TimeOfDay)
3734 __err |= ios_base::failbit;
3735 break;
3736 }
3737 }
3738 __parts |= _ChronoParts::_TimeOfDay;
3739 break;
3740
3741 case 'p': // Locale's AM/PM designation for 12-hour clock.
3742 if (__mod || __num)
3743 __err |= ios_base::failbit;
3744 else
3745 {
3746 // Can't use std::time_get here as it can't parse %p
3747 // in isolation without %I. This might be faster anyway.
3748 const _CharT* __ampms[2];
3749 __tmpunct._M_am_pm(__ampms);
3750 int __n = 0, __which = 3;
3751 while (__which != 0)
3752 {
3753 auto __i = __is.peek();
3754 if (_Traits::eq_int_type(__i, _Traits::eof()))
3755 {
3757 break;
3758 }
3759 __i = std::toupper(_Traits::to_char_type(__i), __loc);
3760 if (__which & 1)
3761 {
3762 if (__i != std::toupper(__ampms[0][__n], __loc))
3763 __which ^= 1;
3764 else if (__ampms[0][__n + 1] == _CharT())
3765 {
3766 __which = 1;
3767 (void) __is.get();
3768 break;
3769 }
3770 }
3771 if (__which & 2)
3772 {
3773 if (__i != std::toupper(__ampms[1][__n], __loc))
3774 __which ^= 2;
3775 else if (__ampms[1][__n + 1] == _CharT())
3776 {
3777 __which = 2;
3778 (void) __is.get();
3779 break;
3780 }
3781 }
3782 if (__which)
3783 (void) __is.get();
3784 ++__n;
3785 }
3786 if (__which == 0 || __which == 3)
3787 __err |= ios_base::failbit;
3788 else
3789 __ampm = __which;
3790 }
3791 break;
3792
3793 case 'r': // Locale's 12-hour time.
3794 if (__mod || __num)
3795 __err |= ios_base::failbit;
3796 else
3797 {
3798 struct tm __tm{};
3799 __tmget.get(__is, {}, __is, __err, &__tm,
3800 __fmt - 2, __fmt);
3801 if (!__is_failed(__err))
3802 {
3803 __h = hours(__tm.tm_hour);
3804 __min = minutes(__tm.tm_min);
3805 __s = seconds(__tm.tm_sec);
3806 }
3807 }
3808 __parts |= _ChronoParts::_TimeOfDay;
3809 break;
3810
3811 case 'R': // %H:%M
3812 case 'T': // %H:%M:%S
3813 if (__mod || __num) [[unlikely]]
3814 {
3815 __err |= ios_base::failbit;
3816 break;
3817 }
3818 else
3819 {
3820 auto __val = __read_unsigned(2);
3821 if (__val == -1 || __val > 23) [[unlikely]]
3822 {
3823 if (_M_need & _ChronoParts::_TimeOfDay)
3824 __err |= ios_base::failbit;
3825 break;
3826 }
3827 if (!__read_chr(':')) [[unlikely]]
3828 break;
3829 __h = hours(__val);
3830
3831 __val = __read_unsigned(2);
3832 if (__val == -1 || __val > 60) [[unlikely]]
3833 {
3834 if (_M_need & _ChronoParts::_TimeOfDay)
3835 __err |= ios_base::failbit;
3836 break;
3837 }
3838 __min = minutes(__val);
3839
3840 if (__c == 'R')
3841 {
3842 __parts |= _ChronoParts::_TimeOfDay;
3843 break;
3844 }
3845 else if (!__read_chr(':')) [[unlikely]]
3846 break;
3847 }
3848 [[fallthrough]];
3849
3850 case 'S': // Seconds
3851 if (__mod == 'E') [[unlikely]]
3852 __err |= ios_base::failbit;
3853 else if (__mod == 'O')
3854 {
3855 struct tm __tm{};
3856 __tmget.get(__is, {}, __is, __err, &__tm,
3857 __fmt - 3, __fmt);
3858 if (!__is_failed(__err))
3859 __s = seconds(__tm.tm_sec);
3860 }
3861 else if constexpr (_Duration::period::den == 1
3862 && !__is_floating)
3863 {
3864 auto __val = __read_unsigned(__num ? __num : 2);
3865 if (0 <= __val && __val <= 59) [[likely]]
3866 __s = seconds(__val);
3867 else
3868 {
3869 if (_M_need & _ChronoParts::_TimeOfDay)
3870 __err |= ios_base::failbit;
3871 break;
3872 }
3873 }
3874 else // Read fractional seconds
3875 {
3876 basic_stringstream<_CharT> __buf;
3877 auto __digit = _S_try_read_digit(__is, __err);
3878 if (__digit != -1)
3879 {
3880 __buf.put(_CharT('0') + __digit);
3881 __digit = _S_try_read_digit(__is, __err);
3882 if (__digit != -1)
3883 __buf.put(_CharT('0') + __digit);
3884 }
3885
3886 auto __i = __is.peek();
3887 if (_Traits::eq_int_type(__i, _Traits::eof()))
3888 __err |= ios_base::eofbit;
3889 else
3890 {
3891 _CharT __dp = '.';
3892 if (__loc != locale::classic())
3893 {
3894 auto& __np = use_facet<numpunct<_CharT>>(__loc);
3895 __dp = __np.decimal_point();
3896 }
3897 _CharT __c = _Traits::to_char_type(__i);
3898 if (__c == __dp)
3899 {
3900 (void) __is.get();
3901 __buf.put('.');
3902 int __prec
3903 = hh_mm_ss<_Duration>::fractional_width;
3904 do
3905 {
3906 __digit = _S_try_read_digit(__is, __err);
3907 if (__digit != -1)
3908 __buf.put(_CharT('0') + __digit);
3909 else
3910 break;
3911 }
3912 while (--__prec);
3913 }
3914 }
3915
3916 if (!__is_failed(__err)) [[likely]]
3917 {
3918 long double __val{};
3919#if __cpp_lib_to_chars
3920 string __str = std::move(__buf).str();
3921 auto __first = __str.data();
3922 auto __last = __first + __str.size();
3923 using enum chars_format;
3924 auto [ptr, ec] = std::from_chars(__first, __last,
3925 __val, fixed);
3926 if ((bool)ec || ptr != __last) [[unlikely]]
3927 __err |= ios_base::failbit;
3928 else
3929#else
3930 if (__buf >> __val)
3931#endif
3932 {
3933 duration<long double> __fs(__val);
3934 if constexpr (__is_floating)
3935 __s = __fs;
3936 else
3937 __s = chrono::round<_Duration>(__fs);
3938 }
3939 }
3940 }
3941 __parts |= _ChronoParts::_TimeOfDay;
3942 break;
3943
3944 case 'u': // ISO weekday (1-7)
3945 case 'w': // Weekday (0-6)
3946 if (__mod == 'E') [[unlikely]]
3947 __err |= ios_base::failbit;
3948 else if (__mod == 'O')
3949 {
3950 if (__c == 'w')
3951 {
3952 struct tm __tm{};
3953 __tmget.get(__is, {}, __is, __err, &__tm,
3954 __fmt - 3, __fmt);
3955 if (!__is_failed(__err))
3956 __wday = weekday(__tm.tm_wday);
3957 }
3958 else
3959 __err |= ios_base::failbit;
3960 }
3961 else
3962 {
3963 const int __lo = __c == 'u' ? 1 : 0;
3964 const int __hi = __lo + 6;
3965 auto __val = __read_unsigned(__num ? __num : 1);
3966 if (__lo <= __val && __val <= __hi)
3967 __wday = weekday(__val);
3968 else
3969 {
3970 __wday = __bad_wday;
3971 break;
3972 }
3973 }
3974 __parts |= _ChronoParts::_Weekday;
3975 break;
3976
3977 case 'U': // Week number of the year (from first Sunday).
3978 case 'V': // ISO week-based week number.
3979 case 'W': // Week number of the year (from first Monday).
3980 if (__mod == 'E') [[unlikely]]
3981 __err |= ios_base::failbit;
3982 else if (__mod == 'O')
3983 {
3984 if (__c == 'V') [[unlikely]]
3985 __err |= ios_base::failbit;
3986 else
3987 {
3988 // TODO nl_langinfo_l(ALT_DIGITS) ?
3989 // Not implementable using std::time_get.
3990 }
3991 }
3992 else
3993 {
3994 const int __lo = __c == 'V' ? 1 : 0;
3995 const int __hi = 53;
3996 auto __val = __read_unsigned(__num ? __num : 2);
3997 if (__lo <= __val && __val <= __hi)
3998 {
3999 switch (__c)
4000 {
4001 case 'U':
4002 __sunday_wk = __val;
4003 break;
4004 case 'V':
4005 __iso_wk = __val;
4006 break;
4007 case 'W':
4008 __monday_wk = __val;
4009 break;
4010 }
4011 }
4012 else
4013 __iso_wk = __sunday_wk = __monday_wk = -1;
4014 }
4015 // N.B. do not alter __parts here, done after loop.
4016 break;
4017
4018 case 'x': // Locale's date representation.
4019 if (__mod == 'O' || __num) [[unlikely]]
4020 __err |= ios_base::failbit;
4021 else
4022 {
4023 struct tm __tm{};
4024 __tmget.get(__is, {}, __is, __err, &__tm,
4025 __fmt - 2 - (__mod == 'E'), __fmt);
4026 if (!__is_failed(__err))
4027 {
4028 __y = year(__tm.tm_year + 1900);
4029 __m = month(__tm.tm_mon + 1);
4030 __d = day(__tm.tm_mday);
4031 }
4032 }
4033 __parts |= _ChronoParts::_Date;
4034 break;
4035
4036 case 'X': // Locale's time representation.
4037 if (__mod == 'O' || __num) [[unlikely]]
4038 __err |= ios_base::failbit;
4039 else
4040 {
4041 struct tm __tm{};
4042 __tmget.get(__is, {}, __is, __err, &__tm,
4043 __fmt - 2 - (__mod == 'E'), __fmt);
4044 if (!__is_failed(__err))
4045 {
4046 __h = hours(__tm.tm_hour);
4047 __min = minutes(__tm.tm_min);
4048 __s = seconds(__tm.tm_sec);
4049 }
4050 }
4051 __parts |= _ChronoParts::_TimeOfDay;
4052 break;
4053
4054 case 'y': // Last two digits of year.
4055 if (__mod) [[unlikely]]
4056 {
4057 struct tm __tm{};
4058 __tmget.get(__is, {}, __is, __err, &__tm,
4059 __fmt - 3, __fmt);
4060 if (!__is_failed(__err))
4061 {
4062 int __cent = __tm.tm_year < 2000 ? 1900 : 2000;
4063 __yy = year(__tm.tm_year - __cent);
4064 if (__century == -1) // No %C has been parsed yet.
4065 __century = __cent;
4066 }
4067 }
4068 else
4069 {
4070 auto __val = __read_unsigned(__num ? __num : 2);
4071 if (__val >= 0 && __val <= 99)
4072 {
4073 __yy = year(__val);
4074 if (__century == -1) // No %C has been parsed yet.
4075 __century = __val < 69 ? 2000 : 1900;
4076 }
4077 else
4078 __y = __yy = __iso_yy = __iso_y = __bad_y;
4079 }
4080 __parts |= _ChronoParts::_Year;
4081 break;
4082
4083 case 'Y': // Year
4084 if (__mod == 'O') [[unlikely]]
4085 __err |= ios_base::failbit;
4086 else if (__mod == 'E')
4087 {
4088 struct tm __tm{};
4089 __tmget.get(__is, {}, __is, __err, &__tm,
4090 __fmt - 3, __fmt);
4091 if (!__is_failed(__err))
4092 __y = year(__tm.tm_year);
4093 }
4094 else
4095 {
4096 auto __val = __read_unsigned(__num ? __num : 4);
4097 if (!__is_failed(__err))
4098 __y = year(__val);
4099 }
4100 __parts |= _ChronoParts::_Year;
4101 break;
4102
4103 case 'z':
4104 if (__num) [[unlikely]]
4105 __err |= ios_base::failbit;
4106 else
4107 {
4108 // For %Ez and %Oz read [+|-][h]h[:mm].
4109 // For %z read [+|-]hh[mm].
4110
4111 auto __i = __is.peek();
4112 if (_Traits::eq_int_type(__i, _Traits::eof()))
4113 {
4115 break;
4116 }
4117 _CharT __ic = _Traits::to_char_type(__i);
4118 const bool __neg = __ic == _CharT('-');
4119 if (__ic == _CharT('-') || __ic == _CharT('+'))
4120 (void) __is.get();
4121
4122 int_least32_t __hh;
4123 if (__mod)
4124 {
4125 // Read h[h]
4126 __hh = __read_unsigned(2);
4127 }
4128 else
4129 {
4130 // Read hh
4131 __hh = 10 * _S_try_read_digit(__is, __err);
4132 __hh += _S_try_read_digit(__is, __err);
4133 }
4134
4135 if (__is_failed(__err))
4136 break;
4137
4138 __i = __is.peek();
4139 if (_Traits::eq_int_type(__i, _Traits::eof()))
4140 {
4141 __err |= ios_base::eofbit;
4142 __tz_offset = minutes(__hh * (__neg ? -60 : 60));
4143 break;
4144 }
4145 __ic = _Traits::to_char_type(__i);
4146
4147 bool __read_mm = false;
4148 if (__mod)
4149 {
4150 if (__ic == _GLIBCXX_WIDEN(":")[0])
4151 {
4152 // Read [:mm] part.
4153 (void) __is.get();
4154 __read_mm = true;
4155 }
4156 }
4157 else if (_CharT('0') <= __ic && __ic <= _CharT('9'))
4158 {
4159 // Read [mm] part.
4160 __read_mm = true;
4161 }
4162
4163 int_least32_t __mm = 0;
4164 if (__read_mm)
4165 {
4166 __mm = 10 * _S_try_read_digit(__is, __err);
4167 __mm += _S_try_read_digit(__is, __err);
4168 }
4169
4170 if (!__is_failed(__err))
4171 {
4172 auto __z = __hh * 60 + __mm;
4173 __tz_offset = minutes(__neg ? -__z : __z);
4174 }
4175 }
4176 break;
4177
4178 case 'Z':
4179 if (__mod || __num) [[unlikely]]
4180 __err |= ios_base::failbit;
4181 else
4182 {
4183 basic_string_view<_CharT> __x = _GLIBCXX_WIDEN("_/-+");
4184 __tz_abbr.clear();
4185 while (true)
4186 {
4187 auto __i = __is.peek();
4188 if (!_Traits::eq_int_type(__i, _Traits::eof()))
4189 {
4190 _CharT __a = _Traits::to_char_type(__i);
4191 if (std::isalnum(__a, __loc)
4192 || __x.find(__a) != __x.npos)
4193 {
4194 __tz_abbr.push_back(__a);
4195 (void) __is.get();
4196 continue;
4197 }
4198 }
4199 else
4200 __err |= ios_base::eofbit;
4201 break;
4202 }
4203 if (__tz_abbr.empty())
4204 __err |= ios_base::failbit;
4205 }
4206 break;
4207
4208 case 'n': // Exactly one whitespace character.
4209 if (__mod || __num) [[unlikely]]
4210 __err |= ios_base::failbit;
4211 else
4212 {
4213 _CharT __i = __is.peek();
4214 if (_Traits::eq_int_type(__i, _Traits::eof()))
4216 else if (std::isspace(_Traits::to_char_type(__i), __loc))
4217 (void) __is.get();
4218 else
4219 __err |= ios_base::failbit;
4220 }
4221 break;
4222
4223 case 't': // Zero or one whitespace characters.
4224 if (__mod || __num) [[unlikely]]
4225 __err |= ios_base::failbit;
4226 else
4227 {
4228 _CharT __i = __is.peek();
4229 if (_Traits::eq_int_type(__i, _Traits::eof()))
4230 __err |= ios_base::eofbit;
4231 else if (std::isspace(_Traits::to_char_type(__i), __loc))
4232 (void) __is.get();
4233 }
4234 break;
4235
4236 case '%': // A % character.
4237 if (__mod || __num) [[unlikely]]
4238 __err |= ios_base::failbit;
4239 else
4240 __read_chr('%');
4241 break;
4242
4243 case 'O': // Modifiers
4244 case 'E':
4245 if (__mod || __num) [[unlikely]]
4246 {
4247 __err |= ios_base::failbit;
4248 break;
4249 }
4250 __mod = __c;
4251 continue;
4252
4253 default:
4254 if (_CharT('1') <= __c && __c <= _CharT('9'))
4255 {
4256 if (!__mod) [[likely]]
4257 {
4258 // %Nx - extract positive decimal integer N
4259 auto __end = __fmt + _Traits::length(__fmt);
4260 auto [__v, __ptr]
4261 = __format::__parse_integer(__fmt - 1, __end);
4262 if (__ptr) [[likely]]
4263 {
4264 __num = __v;
4265 __fmt = __ptr;
4266 continue;
4267 }
4268 }
4269 }
4270 __err |= ios_base::failbit;
4271 }
4272
4273 if (__is_failed(__err)) [[unlikely]]
4274 break;
4275
4276 __is_flag = false;
4277 __num = 0;
4278 __mod = _CharT();
4279 }
4280
4281 if (__century >= 0)
4282 {
4283 if (__yy != __bad_y && __y == __bad_y)
4284 __y = years(__century) + __yy; // Use %y instead of %Y
4285 if (__iso_yy != __bad_y && __iso_y == __bad_y)
4286 __iso_y = years(__century) + __iso_yy; // Use %g instead of %G
4287 }
4288
4289 bool __can_use_doy = false;
4290 bool __can_use_iso_wk = false;
4291 bool __can_use_sun_wk = false;
4292 bool __can_use_mon_wk = false;
4293
4294 // A year + day-of-year can be converted to a full date.
4295 if (__y != __bad_y && __dayofyear >= 0)
4296 {
4297 __can_use_doy = true;
4298 __parts |= _ChronoParts::_Date;
4299 }
4300 else if (__y != __bad_y && __wday != __bad_wday && __sunday_wk >= 0)
4301 {
4302 __can_use_sun_wk = true;
4303 __parts |= _ChronoParts::_Date;
4304 }
4305 else if (__y != __bad_y && __wday != __bad_wday && __monday_wk >= 0)
4306 {
4307 __can_use_mon_wk = true;
4308 __parts |= _ChronoParts::_Date;
4309 }
4310 else if (__iso_y != __bad_y && __wday != __bad_wday && __iso_wk > 0)
4311 {
4312 // An ISO week date can be converted to a full date.
4313 __can_use_iso_wk = true;
4314 __parts |= _ChronoParts::_Date;
4315 }
4316
4317 if (__is_failed(__err)) [[unlikely]]
4318 ; // Don't bother doing any more work.
4319 else if (__is_flag) [[unlikely]] // incomplete format flag
4320 __err |= ios_base::failbit;
4321 else if ((_M_need & __parts) == _M_need) [[likely]]
4322 {
4323 // We try to avoid calculating _M_sys_days and _M_ymd unless
4324 // necessary, because converting sys_days to year_month_day
4325 // (or vice versa) requires non-trivial calculations.
4326 // If we have y/m/d values then use them to populate _M_ymd
4327 // and only convert it to _M_sys_days if the caller needs that.
4328 // But if we don't have y/m/d and need to calculate the date
4329 // from the day-of-year or a week+weekday then we set _M_sys_days
4330 // and only convert it to _M_ymd if the caller needs that.
4331
4332 // We do more error checking here, but only for the fields that
4333 // we actually need to use. For example, we will not diagnose
4334 // an invalid dayofyear==366 for non-leap years unless actually
4335 // using __dayofyear. This should mean we never produce invalid
4336 // results, but it means not all invalid inputs are diagnosed,
4337 // e.g. "2023-01-01 366" >> "%F %j" ignores the invalid 366.
4338 // We also do not diagnose inconsistent values for the same
4339 // field, e.g. "2021 2022 2023" >> "%C%y %Y %Y" just uses 2023.
4340
4341 // Whether the caller wants _M_wd.
4342 // The _Weekday bit is only set for chrono::weekday.
4343 const bool __need_wday = _M_need & _ChronoParts::_Weekday;
4344
4345 // Whether the caller wants _M_sys_days and _M_time.
4346 // Only true for durations and time_points.
4347 const bool __need_time = _M_need & _ChronoParts::_TimeOfDay;
4348
4349 if (__need_wday && __wday != __bad_wday)
4350 _M_wd = __wday; // Caller only wants a weekday and we have one.
4351 else if (_M_need & _ChronoParts::_Date) // subsumes __need_wday
4352 {
4353 // Whether the caller wants _M_ymd.
4354 // True for chrono::year etc., false for time_points.
4355 const bool __need_ymd = !__need_wday && !__need_time;
4356
4357 if ((_M_need & _ChronoParts::_Year && __y == __bad_y)
4358 || (_M_need & _ChronoParts::_Month && __m == __bad_mon)
4359 || (_M_need & _ChronoParts::_Day && __d == __bad_day))
4360 {
4361 // Missing at least one of y/m/d so calculate sys_days
4362 // from the other data we have available.
4363
4364 if (__can_use_doy)
4365 {
4366 if ((0 < __dayofyear && __dayofyear <= 365)
4367 || (__dayofyear == 366 && __y.is_leap()))
4368 [[likely]]
4369 {
4370 _M_sys_days = sys_days(__y/January/1)
4371 + days(__dayofyear - 1);
4372 if (__need_ymd)
4373 _M_ymd = year_month_day(_M_sys_days);
4374 }
4375 else
4376 __err |= ios_base::failbit;
4377 }
4378 else if (__can_use_iso_wk)
4379 {
4380 // Calculate y/m/d from ISO week date.
4381
4382 if (__iso_wk == 53)
4383 {
4384 // A year has 53 weeks iff Jan 1st is a Thursday
4385 // or Jan 1 is a Wednesday and it's a leap year.
4386 const sys_days __jan4(__iso_y/January/4);
4387 weekday __wd1(__jan4 - days(3));
4388 if (__wd1 != Thursday)
4389 if (__wd1 != Wednesday || !__iso_y.is_leap())
4390 __err |= ios_base::failbit;
4391 }
4392
4393 if (!__is_failed(__err)) [[likely]]
4394 {
4395 // First Thursday is always in week one:
4396 sys_days __w(Thursday[1]/January/__iso_y);
4397 // First day of week-based year:
4398 __w -= Thursday - Monday;
4399 __w += days(weeks(__iso_wk - 1));
4400 __w += __wday - Monday;
4401 _M_sys_days = __w;
4402
4403 if (__need_ymd)
4404 _M_ymd = year_month_day(_M_sys_days);
4405 }
4406 }
4407 else if (__can_use_sun_wk)
4408 {
4409 // Calculate y/m/d from week number + weekday.
4410 sys_days __wk1(__y/January/Sunday[1]);
4411 _M_sys_days = __wk1 + weeks(__sunday_wk - 1)
4412 + days(__wday.c_encoding());
4413 _M_ymd = year_month_day(_M_sys_days);
4414 if (_M_ymd.year() != __y) [[unlikely]]
4415 __err |= ios_base::failbit;
4416 }
4417 else if (__can_use_mon_wk)
4418 {
4419 // Calculate y/m/d from week number + weekday.
4420 sys_days __wk1(__y/January/Monday[1]);
4421 _M_sys_days = __wk1 + weeks(__monday_wk - 1)
4422 + days(__wday.c_encoding() - 1);
4423 _M_ymd = year_month_day(_M_sys_days);
4424 if (_M_ymd.year() != __y) [[unlikely]]
4425 __err |= ios_base::failbit;
4426 }
4427 else // Should not be able to get here.
4428 __err |= ios_base::failbit;
4429 }
4430 else
4431 {
4432 // We know that all fields the caller needs are present,
4433 // but check that their values are in range.
4434 // Make unwanted fields valid so that _M_ymd.ok() is true.
4435
4436 if (_M_need & _ChronoParts::_Year)
4437 {
4438 if (!__y.ok()) [[unlikely]]
4439 __err |= ios_base::failbit;
4440 }
4441 else if (__y == __bad_y)
4442 __y = 1972y; // Leap year so that Feb 29 is valid.
4443
4444 if (_M_need & _ChronoParts::_Month)
4445 {
4446 if (!__m.ok()) [[unlikely]]
4447 __err |= ios_base::failbit;
4448 }
4449 else if (__m == __bad_mon)
4450 __m = January;
4451
4452 if (_M_need & _ChronoParts::_Day)
4453 {
4454 if (__d < day(1) || __d > (__y/__m/last).day())
4455 __err |= ios_base::failbit;
4456 }
4457 else if (__d == __bad_day)
4458 __d = 1d;
4459
4460 if (year_month_day __ymd(__y, __m, __d); __ymd.ok())
4461 {
4462 _M_ymd = __ymd;
4463 if (__need_wday || __need_time)
4464 _M_sys_days = sys_days(_M_ymd);
4465 }
4466 else [[unlikely]]
4467 __err |= ios_base::failbit;
4468 }
4469
4470 if (__need_wday)
4471 _M_wd = weekday(_M_sys_days);
4472 }
4473
4474 // Need to set _M_time for both durations and time_points.
4475 if (__need_time)
4476 {
4477 if (__h == __bad_h && __h12 != __bad_h)
4478 {
4479 if (__ampm == 1)
4480 __h = __h12 == hours(12) ? hours(0) : __h12;
4481 else if (__ampm == 2)
4482 __h = __h12 == hours(12) ? __h12 : __h12 + hours(12);
4483 else [[unlikely]]
4484 __err |= ios_base::failbit;
4485 }
4486
4487 auto __t = _M_time.zero();
4488 bool __ok = false;
4489
4490 if (__h != __bad_h)
4491 {
4492 __ok = true;
4493 __t += __h;
4494 }
4495
4496 if (__min != __bad_min)
4497 {
4498 __ok = true;
4499 __t += __min;
4500 }
4501
4502 if (__s != __bad_sec)
4503 {
4504 __ok = true;
4505 __t += __s;
4506 _M_is_leap_second = __s >= seconds(60);
4507 }
4508
4509 if (__ok)
4510 _M_time = __t;
4511 else
4512 __err |= ios_base::failbit;
4513 }
4514
4515 if (!__is_failed(__err)) [[likely]]
4516 {
4517 if (__offset && __tz_offset != __bad_min)
4518 *__offset = __tz_offset;
4519 if (__abbrev && !__tz_abbr.empty())
4520 *__abbrev = std::move(__tz_abbr);
4521 }
4522 }
4523 else
4524 __err |= ios_base::failbit;
4525 }
4526 if (__err)
4527 __is.setstate(__err);
4528 return __is;
4529 }
4530 /// @endcond
4531#undef _GLIBCXX_WIDEN
4532
4533 /// @} group chrono
4534} // namespace chrono
4535
4536_GLIBCXX_END_NAMESPACE_VERSION
4537} // namespace std
4538
4539#endif // C++20
4540
4541#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.