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