libstdc++
chrono_io.h
Go to the documentation of this file.
1// <chrono> Formatting -*- C++ -*-
2
3// Copyright The GNU Toolchain Authors.
4//
5// This file is part of the GNU ISO C++ Library. This library is free
6// software; you can redistribute it and/or modify it under the
7// terms of the GNU General Public License as published by the
8// Free Software Foundation; either version 3, or (at your option)
9// any later version.
10
11// This library is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
16// Under Section 7 of GPL version 3, you are granted additional
17// permissions described in the GCC Runtime Library Exception, version
18// 3.1, as published by the Free Software Foundation.
19
20// You should have received a copy of the GNU General Public License and
21// a copy of the GCC Runtime Library Exception along with this program;
22// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23// <http://www.gnu.org/licenses/>.
24
25/** @file include/bits/chrono_io.h
26 * This is an internal header file, included by other library headers.
27 * Do not attempt to use it directly. @headername{chrono}
28 */
29
30#ifndef _GLIBCXX_CHRONO_IO_H
31#define _GLIBCXX_CHRONO_IO_H 1
32
33#ifdef _GLIBCXX_SYSHDR
34#pragma GCC system_header
35#endif
36
37#if __cplusplus >= 202002L
38
39#include <sstream> // ostringstream
40#include <format>
41#include <charconv> // from_chars
42#include <stdexcept> // __sso_string
43
45#include <bits/unique_ptr.h>
46
47namespace std _GLIBCXX_VISIBILITY(default)
48{
49_GLIBCXX_BEGIN_NAMESPACE_VERSION
50
51namespace chrono
52{
53/// @addtogroup chrono
54/// @{
55
56/// @cond undocumented
57namespace __detail
58{
59#define _GLIBCXX_WIDEN_(C, S) ::std::__format::_Widen<C>(S, L##S)
60#define _GLIBCXX_WIDEN(S) _GLIBCXX_WIDEN_(_CharT, S)
61
62 template<typename _Period, typename _CharT>
64 __units_suffix() noexcept
65 {
66 // The standard say these are all narrow strings, which would need to
67 // be widened at run-time when inserted into a wide stream. We use
68 // STATICALLY-WIDEN to widen at compile-time.
69#define _GLIBCXX_UNITS_SUFFIX(period, suffix) \
70 if constexpr (is_same_v<_Period, period>) \
71 return _GLIBCXX_WIDEN(suffix); \
72 else
73
74 _GLIBCXX_UNITS_SUFFIX(atto, "as")
75 _GLIBCXX_UNITS_SUFFIX(femto, "fs")
76 _GLIBCXX_UNITS_SUFFIX(pico, "ps")
77 _GLIBCXX_UNITS_SUFFIX(nano, "ns")
78 _GLIBCXX_UNITS_SUFFIX(milli, "ms")
79#if _GLIBCXX_USE_ALT_MICROSECONDS_SUFFIX
80 // Deciding this at compile-time is wrong, maybe use nl_langinfo(CODESET)
81 // to check runtime environment and return u8"\u00b5s", "\xb5s", or "us".
82 _GLIBCXX_UNITS_SUFFIX(micro, "\u00b5s")
83#else
84 _GLIBCXX_UNITS_SUFFIX(micro, "us")
85#endif
86 _GLIBCXX_UNITS_SUFFIX(centi, "cs")
87 _GLIBCXX_UNITS_SUFFIX(deci, "ds")
88 _GLIBCXX_UNITS_SUFFIX(ratio<1>, "s")
89 _GLIBCXX_UNITS_SUFFIX(deca, "das")
90 _GLIBCXX_UNITS_SUFFIX(hecto, "hs")
91 _GLIBCXX_UNITS_SUFFIX(kilo, "ks")
92 _GLIBCXX_UNITS_SUFFIX(mega, "Ms")
93 _GLIBCXX_UNITS_SUFFIX(giga, "Gs")
94 _GLIBCXX_UNITS_SUFFIX(tera, "Ts")
95 _GLIBCXX_UNITS_SUFFIX(tera, "Ts")
96 _GLIBCXX_UNITS_SUFFIX(peta, "Ps")
97 _GLIBCXX_UNITS_SUFFIX(exa, "Es")
98 _GLIBCXX_UNITS_SUFFIX(ratio<60>, "min")
99 _GLIBCXX_UNITS_SUFFIX(ratio<3600>, "h")
100 _GLIBCXX_UNITS_SUFFIX(ratio<86400>, "d")
101#undef _GLIBCXX_UNITS_SUFFIX
102 return {};
103 }
104
105 template<typename _Period, typename _CharT, typename _Out>
106 inline _Out
107 __fmt_units_suffix(_Out __out) noexcept
108 {
109 if (auto __s = __detail::__units_suffix<_Period, _CharT>(); __s.size())
110 return __format::__write(std::move(__out), __s);
111 else if constexpr (_Period::den == 1)
112 return std::format_to(std::move(__out), _GLIBCXX_WIDEN("[{}]s"),
113 (uintmax_t)_Period::num);
114 else
115 return std::format_to(std::move(__out), _GLIBCXX_WIDEN("[{}/{}]s"),
116 (uintmax_t)_Period::num,
117 (uintmax_t)_Period::den);
118 }
119} // namespace __detail
120/// @endcond
121
122 /** Write a `chrono::duration` to an ostream.
123 *
124 * @since C++20
125 */
126 template<typename _CharT, typename _Traits,
127 typename _Rep, typename _Period>
130 const duration<_Rep, _Period>& __d)
131 {
133 using period = typename _Period::type;
135 __s.flags(__os.flags());
136 __s.imbue(__os.getloc());
137 __s.precision(__os.precision());
138 // _GLIBCXX_RESOLVE_LIB_DEFECTS
139 // 4118. How should duration formatters format custom rep types?
140 __s << +__d.count();
141 __detail::__fmt_units_suffix<period, _CharT>(_Out(__s));
142 __os << std::move(__s).str();
143 return __os;
144 }
145
146/// @cond undocumented
147namespace __detail
148{
149 // An unspecified type returned by `chrono::local_time_format`.
150 // This is called `local-time-format-t` in the standard.
151 template<typename _Duration>
152 struct __local_time_fmt
153 {
154 local_time<_Duration> _M_time;
155 const string* _M_abbrev;
156 const seconds* _M_offset_sec;
157 };
158
159 // _GLIBCXX_RESOLVE_LIB_DEFECTS
160 // 4124. Cannot format zoned_time with resolution coarser than seconds
161 template<typename _Duration>
162 using __local_time_fmt_for
163 = __local_time_fmt<common_type_t<_Duration, seconds>>;
164}
165/// @endcond
166
167 /** Return an object that asssociates timezone info with a local time.
168 *
169 * A `chrono::local_time` object has no timezone associated with it. This
170 * function creates an object that allows formatting a `local_time` as
171 * though it refers to a timezone with the given abbreviated name and
172 * offset from UTC.
173 *
174 * @since C++20
175 */
176 template<typename _Duration>
177 inline __detail::__local_time_fmt<_Duration>
178 local_time_format(local_time<_Duration> __time,
179 const string* __abbrev = nullptr,
180 const seconds* __offset_sec = nullptr)
181 { return {__time, __abbrev, __offset_sec}; }
182
183 /// @}
184} // namespace chrono
185
186/// @cond undocumented
187namespace __format
188{
189 [[noreturn,__gnu__::__always_inline__]]
190 inline void
191 __not_valid_for_duration()
192 { __throw_format_error("format error: chrono-format-spec not valid for "
193 "chrono::duration"); }
194
195 [[noreturn,__gnu__::__always_inline__]]
196 inline void
197 __invalid_chrono_spec()
198 { __throw_format_error("format error: chrono-format-spec not valid for "
199 "argument type"); }
200
201 // Represents the information provided by a chrono type.
202 // e.g. month_weekday has month and weekday but no year or time of day,
203 // hh_mm_ss has time of day but no date, sys_time is time_point+timezone.
204 enum class _ChronoParts : unsigned short {
205 _None = 0, _TotalSeconds = 1u, _Subseconds = 1u << 2,
206
207 // time since epoch
208 _EpochUnits = 1u << 3, _UnitSuffix = 1u << 4,
209 _EpochSeconds = _EpochUnits | _TotalSeconds,
210
211 // local (wall) time
212 _LocalDays = 1u << 5,
213 _LocalSeconds = _LocalDays | _TotalSeconds,
214
215 _Year = 1u << 6, _Month = 1u << 7, _Day = 1u << 8,
216 _Weekday = 1u << 9, _WeekdayIndex = 1u << 10, _DayOfYear = 1u << 11,
217 _IndexedWeekday = _Weekday | _WeekdayIndex,
218 _YearMonthDay = _Year | _Month | _Day,
219 _Date = _LocalDays | _YearMonthDay | _IndexedWeekday | _DayOfYear,
220
221 _HoursMinutesSeconds = 1u << 12,
222 _TimeOfDay = _HoursMinutesSeconds | _Subseconds,
223 _Time = _TimeOfDay | _TotalSeconds,
224 _EpochTime = _Time | _EpochUnits | _UnitSuffix,
225 _DateTime = _Date | _Time,
226
227 _ZoneAbbrev = 1u << 13, _ZoneOffset = 1u << 14,
228 _TimeZone = _ZoneAbbrev | _ZoneOffset,
229 _ZonedDateTime = _DateTime | _TimeZone,
230 };
231
232 [[__gnu__::__always_inline__]]
233 constexpr _ChronoParts
234 operator&(_ChronoParts __x, _ChronoParts __y) noexcept
235 { return static_cast<_ChronoParts>((unsigned)__x & (unsigned)__y); }
236
237 [[__gnu__::__always_inline__]]
238 constexpr _ChronoParts&
239 operator&=(_ChronoParts& __x, _ChronoParts __y) noexcept
240 { return __x = __x & __y; }
241
242 [[__gnu__::__always_inline__]]
243 constexpr _ChronoParts
244 operator|(_ChronoParts __x, _ChronoParts __y) noexcept
245 { return static_cast<_ChronoParts>((unsigned short)__x | (unsigned short)__y); }
246
247 [[__gnu__::__always_inline__]]
248 constexpr _ChronoParts&
249 operator|=(_ChronoParts& __x, _ChronoParts __y) noexcept
250 { return __x = __x | __y; }
251
252 // returns copy of x with all bits from y unset.
253 [[__gnu__::__always_inline__]]
254 constexpr _ChronoParts
255 operator-(_ChronoParts __x, _ChronoParts __y) noexcept
256 { return static_cast<_ChronoParts>((unsigned short)__x & ~(unsigned short)__y); }
257
258 // unsets all bits of x that are set in y
259 [[__gnu__::__always_inline__]]
260 constexpr _ChronoParts&
261 operator-=(_ChronoParts& __x, _ChronoParts __y) noexcept
262 { return __x = __x - __y; }
263
264 [[__gnu__::__always_inline__]]
265 constexpr bool
266 operator==(_ChronoParts __x, decltype(nullptr)) noexcept
267 { return (unsigned short)__x == 0; }
268
269 template<typename _CharT>
270 struct _ChronoSpec : _Spec<_CharT>
271 {
272 // When _M_prec_kind is _WP_none, the _M_prec contains the default
273 // value of fraction digits to be used for time '%S'.
274
275 // Placed in tail-padding of __format::_Spec<C>.
276 // This indicates that a locale-dependent conversion specifier such as
277 // %a is used in the chrono-specs. This is not the same as the
278 // _Spec<C>::_M_localized member which indicates that "L" was present
279 // in the format-spec, e.g. "{:L%a}" is localized and locale-specific,
280 // but "{:L}" is only localized and "{:%a}" is only locale-specific.
281 unsigned _M_locale_specific : 1;
282 // Indicates if parts that are checked for ok come directly from the
283 // input, instead of being computed.
284 unsigned _M_needs_ok_check : 1;
285 // Indicates that duration should be treated as floating point.
286 unsigned _M_floating_point_rep : 1;
287 // Indicate that duration uses user-defined representation.
288 unsigned _M_custom_rep : 1;
289 unsigned _M_unused : 4;
290
291 // Chrono parts required by format specs
292 _ChronoParts _M_needed;
293 basic_string_view<_CharT> _M_chrono_specs;
294
295 [[__gnu__::__always_inline__]]
296 constexpr bool
297 _M_needs(_ChronoParts __parts) const
298 { return (_M_needed & __parts) != 0; }
299 };
300
301 template<typename _CharT>
302 struct _ChronoFormats
303 {
304 using _String_view = basic_string_view<_CharT>;
305
306 static consteval
307 _String_view
308 _S_ftz() noexcept
309 { return _GLIBCXX_WIDEN("%F %T %Z"); }
310
311 static consteval
312 _String_view
313 _S_ft() noexcept
314 { return _S_ftz().substr(0, 5); }
315
316 static consteval
317 _String_view
318 _S_f() noexcept
319 { return _S_ftz().substr(0, 2); }
320
321 static consteval
322 _String_view
323 _S_t() noexcept
324 { return _S_ftz().substr(3, 2); }
325
326 static consteval
327 _String_view
328 _S_ymd() noexcept
329 { return _GLIBCXX_WIDEN("%Y/%b/%d"); }
330
331 static consteval
332 _String_view
333 _S_ym() noexcept
334 { return _S_ymd().substr(0, 5); }
335
336 static consteval
337 _String_view
338 _S_md() noexcept
339 { return _S_ymd().substr(3); }
340
341 static consteval
342 _String_view
343 _S_y() noexcept
344 { return _S_ymd().substr(0, 2); }
345
346 static consteval
347 _String_view
348 _S_m() noexcept
349 { return _S_ymd().substr(3, 2); }
350
351 static consteval
352 _String_view
353 _S_d() noexcept
354 { return _S_ymd().substr(6, 2); }
355
356 static consteval
357 _String_view
358 _S_ymwi() noexcept
359 // %\0 is extension for handling weekday index
360 { return _String_view(_GLIBCXX_WIDEN("%Y/%b/%a[%\0]"), 12); }
361
362 static consteval
363 _String_view
364 _S_mwi() noexcept
365 { return _S_ymwi().substr(3); }
366
367 static consteval
368 _String_view
369 _S_wi() noexcept
370 { return _S_ymwi().substr(6); }
371
372 static consteval
373 _String_view
374 _S_w() noexcept
375 { return _S_ymwi().substr(6, 2); }
376
377 static consteval
378 _String_view
379 _S_ymwl() noexcept
380 { return _GLIBCXX_WIDEN("%Y/%b/%a[last]"); }
381
382 static consteval
383 _String_view
384 _S_mwl() noexcept
385 { return _S_ymwl().substr(3); }
386
387 static consteval
388 _String_view
389 _S_wl() noexcept
390 { return _S_ymwl().substr(6); }
391
392 static consteval
393 _String_view
394 _S_yml() noexcept
395 { return _GLIBCXX_WIDEN("%Y/%b/last"); }
396
397 static consteval
398 _String_view
399 _S_ml() noexcept
400 { return _S_yml().substr(3); }
401 };
402
403 template<typename _CharT>
404 struct _ChronoData
405 {
406 static constexpr unsigned _S_max_prec = 18;
407 using _Attoseconds = chrono::duration<__UINT_LEAST64_TYPE__, atto>;
408
409 using _FormatContext
410 = basic_format_context<_Sink_iter<_CharT>, _CharT>;
411 using _FormatArgs = basic_format_args<_FormatContext>;
412 static inline auto _S_args = std::make_format_args<_FormatContext>();
413
414 _ChronoData() = default;
415 _ChronoData(_ChronoData&&) = delete;
416
417 // time since epoch
418 chrono::seconds _M_eseconds;
419 // n.b. due offset being seconds or coarser, local and epoch subseconds
420 // has the same value
421 _Attoseconds _M_subseconds;
422 // _M_ereps.get(0) stores duration units
423 // _M_ereps.get(1) stores subseconds units
424 // _M_ereps.get(2) stores precision
425 _FormatArgs _M_ereps = _S_args;
426 basic_string_view<_CharT> _M_unit_suffix;
427
428 // local (wall) time
429 chrono::local_seconds _M_lseconds;
430 chrono::local_days _M_ldays;
431
432 chrono::year _M_year;
433 chrono::month _M_month;
434 chrono::day _M_day;
435 chrono::weekday _M_weekday;
436 unsigned char _M_weekday_index;
437 chrono::days _M_day_of_year;
438
439 bool _M_is_neg;
440 chrono::hours _M_hours;
441 chrono::minutes _M_minutes;
442 chrono::seconds _M_seconds;
443
444 chrono::seconds _M_zone_offset;
445 basic_string_view<_CharT> _M_zone_abbrev;
446 const char* _M_zone_cstr = "";
447
448 template<typename _YearMonth>
449 [[__gnu__::__always_inline__]]
450 _ChronoParts
451 _M_fill_year_month(const _YearMonth& __ym, _ChronoParts __parts)
452 {
453 _M_year = __ym.year();
454 __parts -= _ChronoParts::_Year;
455 _M_month = __ym.month();
456 __parts -= _ChronoParts::_Month;
457 return __parts;
458 }
459
460 [[__gnu__::__always_inline__]]
461 _ChronoParts
462 _M_fill_day(chrono::day __d, _ChronoParts __parts)
463 {
464 _M_day = __d;
465 __parts -= _ChronoParts::_Day;
466 _M_weekday_index = ((unsigned)__d + 6u) / 7u;
467 __parts -= _ChronoParts::_WeekdayIndex;
468 return __parts;
469 }
470
471 [[__gnu__::__always_inline__]]
472 _ChronoParts
473 _M_fill_weekday(chrono::weekday_indexed __wi, _ChronoParts __parts)
474 {
475 _M_weekday = __wi.weekday();
476 __parts -= _ChronoParts::_Weekday;
477 _M_weekday_index = __wi.index();
478 __parts -= _ChronoParts::_WeekdayIndex;
479 return __parts;
480 }
481
482 // pre: _M_year is set
483 [[__gnu__::__always_inline__]]
484 _ChronoParts
485 _M_fill_aux(chrono::local_days __ld, _ChronoParts __parts)
486 {
487 using namespace chrono;
488 if ((__parts & _ChronoParts::_Weekday) != 0)
489 _M_weekday = weekday(__ld);
490 __parts -= _ChronoParts::_Weekday;
491 if ((__parts & _ChronoParts::_DayOfYear) != 0)
492 // See "Calculating Ordinal Dates" at
493 // https://github.com/HowardHinnant/date/wiki/Examples-and-Recipes
494 _M_day_of_year = __ld - local_days(_M_year/January/0);
495 __parts -= _ChronoParts::_DayOfYear;
496 return __parts;
497 }
498
499 // pre: _M_year is set
500 [[__gnu__::__always_inline__]]
501 _ChronoParts
502 _M_fill_ldays(chrono::local_days __ld, _ChronoParts __parts)
503 {
504 _M_ldays = __ld;
505 __parts -= _ChronoParts::_LocalDays;
506 return _M_fill_aux(__ld, __parts);
507 }
508
509 void
510 _M_fill_time(chrono::seconds __d)
511 {
512 chrono::hh_mm_ss<chrono::seconds> __hms(__d);
513 _M_hours = __hms.hours();
514 _M_minutes = __hms.minutes();
515 _M_seconds = __hms.seconds();
516 }
517
518 void
519 _M_fill_date_time(chrono::local_seconds __ls, _ChronoParts __parts)
520 {
521 _M_ldays = chrono::floor<chrono::days>(__ls);
522 __parts -= _ChronoParts::_LocalDays;
523 if ((__parts & _ChronoParts::_HoursMinutesSeconds) != 0)
524 _M_fill_time(_M_lseconds - _M_ldays);
525
526 if ((__parts & _ChronoParts::_Date) != 0)
527 {
528 const chrono::year_month_day __ymd(_M_ldays);
529 _M_fill_year_month(__ymd, __parts);
530 _M_fill_day(__ymd.day(), __parts);
531 _M_fill_aux(_M_ldays, __parts);
532 }
533 }
534
535 void
536 _M_fill_zone(const char* __abbrev, const wchar_t* __wabbrev)
537 {
538 if constexpr (is_same_v<_CharT, char>)
539 _M_zone_abbrev = __abbrev;
540 else
541 _M_zone_abbrev = __wabbrev;
542 _M_zone_cstr = __abbrev;
543 }
544
545 [[__gnu__::__always_inline__]]
546 void
547 _M_fill_utc_zone()
548 { _M_fill_zone("UTC", L"UTC"); }
549 };
550
551 // TODO rename this to chrono::__formatter? or chrono::__detail::__formatter?
552 template<typename _CharT>
553 struct __formatter_chrono
554 {
555 using __string_view = basic_string_view<_CharT>;
556 using __string = basic_string<_CharT>;
557
558 __formatter_chrono() = default;
559
560 constexpr explicit
561 __formatter_chrono(_ChronoSpec<_CharT> __spec) noexcept
562 : _M_spec(__spec)
563 { }
564
565 constexpr typename basic_format_parse_context<_CharT>::iterator
566 _M_parse(basic_format_parse_context<_CharT>& __pc, _ChronoParts __parts,
567 const _ChronoSpec<_CharT>& __def)
568 {
569 auto __first = __pc.begin();
570 auto __last = __pc.end();
571
572 _ChronoSpec<_CharT> __spec = __def;
573
574 auto __finalize = [this, &__spec, &__def] {
575 using enum _ChronoParts;
576 _ChronoParts __checked
577 = __spec._M_debug ? _YearMonthDay|_IndexedWeekday
578 : _Month|_Weekday;
579 // n.b. for calendar types __def._M_needed contains only parts
580 // copied from the input, remaining ones are computed, and thus ok
581 __spec._M_needs_ok_check
582 = __spec._M_needs(__def._M_needed & __checked);
583 _M_spec = __spec;
584 };
585
586 auto __finished = [&] {
587 if (__first == __last || *__first == '}')
588 {
589 __finalize();
590 return true;
591 }
592 return false;
593 };
594
595 if (__finished())
596 return __first;
597
598 __first = __spec._M_parse_fill_and_align(__first, __last);
599 if (__finished())
600 return __first;
601
602 __first = __spec._M_parse_width(__first, __last, __pc);
603 if (__finished())
604 return __first;
605
606 if (*__first == '.')
607 {
608 if ((__parts & _ChronoParts::_EpochUnits) == 0
609 || !__spec._M_floating_point_rep)
610 __throw_format_error("format error: invalid precision for duration");
611
612 // Precision is allowed, but value is ignored.
613 __first = _Spec<_CharT>()._M_parse_precision(__first, __last, __pc);
614 // Still inditate that there was user supplied precision.
615 __spec._M_prec_kind = _WP_value;
616 if (__finished())
617 return __first;
618 }
619
620 __spec._M_localized = false;
621 __first = __spec._M_parse_locale(__first, __last);
622 if (__finished())
623 return __first;
624
625 // Everything up to the end of the string or the first '}' is a
626 // chrono-specs string. Check it is valid.
627 {
628 __string_view __str(__first, __last - __first);
629 auto __end = __str.find('}');
630 if (__end != __str.npos)
631 {
632 __str.remove_suffix(__str.length() - __end);
633 __last = __first + __end;
634 }
635 if (__str.find('{') != __str.npos)
636 __throw_format_error("chrono format error: '{' in chrono-specs");
637 }
638
639 // Parse chrono-specs in [first,last), checking each conversion-spec
640 // against __parts (so fail for %Y if no year in parts).
641 // Save range in __spec._M_chrono_specs.
642 __spec._M_debug = false;
643 __spec._M_locale_specific = false;
644 __spec._M_needed = _ChronoParts::_None;
645 __spec._M_chrono_specs = __string_view();
646
647 const auto __chrono_specs = __first++; // Skip leading '%'
648 if (*__chrono_specs != '%')
649 __throw_format_error("chrono format error: no '%' at start of "
650 "chrono-specs");
651
652 _CharT __mod{};
653 bool __conv = true;
654 while (__first != __last)
655 {
656 enum _Mods { _Mod_none, _Mod_E, _Mod_O, _Mod_E_O };
657 _Mods __allowed_mods = _Mod_none;
658
659 _ChronoParts __needed = _ChronoParts::_None;
660 bool __locale_specific = false;
661
662 _CharT __c = *__first++;
663 switch (__c)
664 {
665 using enum _ChronoParts;
666 case 'a':
667 case 'A':
668 __needed = _Weekday;
669 __locale_specific = true;
670 break;
671 case 'b':
672 case 'h':
673 case 'B':
674 __needed = _Month;
675 __locale_specific = true;
676 break;
677 case 'c':
678 __needed = _Date|_HoursMinutesSeconds;
679 __allowed_mods = _Mod_E;
680 __locale_specific = true;
681 break;
682 case 'C':
683 __needed = _Year;
684 __allowed_mods = _Mod_E;
685 break;
686 case 'd':
687 case 'e':
688 __needed = _Day;
689 __allowed_mods = _Mod_O;
690 break;
691 case 'D':
692 case 'F':
693 __needed = _YearMonthDay;
694 break;
695 case 'g':
696 case 'G':
697 case 'V':
698 __needed = _LocalDays|_Year|_DayOfYear|_Weekday;
699 break;
700 case 'H':
701 case 'I':
702 __needed = _HoursMinutesSeconds;
703 __allowed_mods = _Mod_O;
704 break;
705 case 'j':
706 __needed = __parts & _DayOfYear;
707 // If we do not know day-of-year then we must have a duration,
708 // which is to be formatted as decimal number of days.
709 if (__needed == _None)
710 __needed = _HoursMinutesSeconds;
711 break;
712 case 'm':
713 __needed = _Month;
714 __allowed_mods = _Mod_O;
715 break;
716 case 'M':
717 __needed = _HoursMinutesSeconds;
718 __allowed_mods = _Mod_O;
719 break;
720 case 'p':
721 case 'r':
722 __locale_specific = true;
723 [[fallthrough]];
724 case 'R':
725 __needed = _HoursMinutesSeconds;
726 break;
727 case 'T':
728 __needed = _TimeOfDay;
729 break;
730 case 'q':
731 __needed = _UnitSuffix;
732 break;
733 case 'Q':
734 __needed = _EpochUnits;
735 break;
736 case 'S':
737 __needed = _TimeOfDay;
738 __allowed_mods = _Mod_O;
739 break;
740 case 'u':
741 case 'w':
742 __needed = _Weekday;
743 __allowed_mods = _Mod_O;
744 break;
745 case 'U':
746 case 'W':
747 __needed = _DayOfYear|_Weekday;
748 __allowed_mods = _Mod_O;
749 break;
750 case 'x':
751 __needed = _Date;
752 __locale_specific = true;
753 __allowed_mods = _Mod_E;
754 break;
755 case 'X':
756 __needed = _HoursMinutesSeconds;
757 __locale_specific = true;
758 __allowed_mods = _Mod_E;
759 break;
760 case 'y':
761 __needed = _Year;
762 __allowed_mods = _Mod_E_O;
763 break;
764 case 'Y':
765 __needed = _Year;
766 __allowed_mods = _Mod_E;
767 break;
768 case 'z':
769 __needed = _ZoneOffset;
770 __allowed_mods = _Mod_E_O;
771 break;
772 case 'Z':
773 __needed = _ZoneAbbrev;
774 break;
775 case 'n':
776 case 't':
777 case '%':
778 break;
779 case 'O':
780 case 'E':
781 if (__mod) [[unlikely]]
782 {
783 __allowed_mods = _Mod_none;
784 break;
785 }
786 __mod = __c;
787 continue;
788 default:
789 __throw_format_error("chrono format error: invalid specifier "
790 "in chrono-specs");
791 }
792
793 if ((__mod == 'E' && !(__allowed_mods & _Mod_E))
794 || (__mod == 'O' && !(__allowed_mods & _Mod_O)))
795 __throw_format_error("chrono format error: invalid modifier "
796 "in chrono-specs");
797 if (__mod && __c != 'z')
798 __locale_specific = true;
799 __mod = _CharT();
800
801 // localized formats do not include subseconds
802 if (__locale_specific)
803 __needed -= _ChronoParts::_Subseconds;
804
805 if ((__parts & __needed) != __needed)
806 __throw_format_error("chrono format error: format argument does "
807 "not contain the information required by the "
808 "chrono-specs");
809 __spec._M_needed |= __needed;
810 __spec._M_locale_specific |= __locale_specific;
811
812 // Scan for next '%', ignoring literal-chars before it.
813 size_t __pos = __string_view(__first, __last - __first).find('%');
814 if (__pos == 0)
815 ++__first;
816 else
817 {
818 if (__pos == __string_view::npos)
819 {
820 __first = __last;
821 __conv = false;
822 }
823 else
824 __first += __pos + 1;
825 }
826 }
827
828 // Check for a '%' conversion-spec without a type.
829 if (__conv || __mod != _CharT())
830 __throw_format_error("chrono format error: unescaped '%' in "
831 "chrono-specs");
832
833 __spec._M_chrono_specs
834 = __string_view(__chrono_specs, __first - __chrono_specs);
835
836 __finalize();
837 return __first;
838 }
839
840 // pre: !_M_spec._M_chrono_specs.empty()
841 template<typename _FormatContext>
842 typename _FormatContext::iterator
843 _M_format(const _ChronoData<_CharT>& __t, _FormatContext& __fc) const
844 {
845#if defined _GLIBCXX_USE_NL_LANGINFO_L && __CHAR_BIT__ == 8
846 // _GLIBCXX_RESOLVE_LIB_DEFECTS
847 // 3565. Handling of encodings in localized formatting
848 // of chrono types is underspecified
849 if constexpr (is_same_v<_CharT, char>)
850 if constexpr (__unicode::__literal_encoding_is_utf8())
851 if (_M_spec._M_localized && _M_spec._M_locale_specific)
852 {
853 extern locale __with_encoding_conversion(const locale&);
854
855 // Allocate and cache the necessary state to convert strings
856 // in the locale's encoding to UTF-8.
857 locale __loc = __fc.locale();
858 if (__loc != locale::classic())
859 __fc._M_loc = __with_encoding_conversion(__loc);
860 }
861#endif
862
863 const size_t __padwidth = _M_spec._M_get_width(__fc);
864 if (__padwidth == 0)
865 return _M_format_to(__t, __fc.out(), __fc);
866
867 using _Out = typename _FormatContext::iterator;
868 _Padding_sink<_Out, _CharT> __sink(__fc.out(), __padwidth);
869 _M_format_to(__t, __sink.out(), __fc);
870 return __sink._M_finish(_M_spec._M_align, _M_spec._M_fill);
871 }
872
873 _ChronoSpec<_CharT> _M_spec;
874
875 protected:
876 static constexpr const _CharT* _S_chars
877 = _GLIBCXX_WIDEN("0123456789.Lf:/ +-{}");
878 static constexpr _CharT _S_dot = _S_chars[10];
879 static constexpr _CharT _S_colon = _S_chars[13];
880 static constexpr _CharT _S_slash = _S_chars[14];
881 static constexpr _CharT _S_space = _S_chars[15];
882 static constexpr const _CharT* _S_fp_fmt = _S_chars + 11;
883 static constexpr const _CharT* _S_plus_minus = _S_chars + 16;
884 static constexpr const _CharT* _S_minus_empty_spec = _S_chars + 17;
885 static constexpr const _CharT* _S_empty_spec = _S_chars + 18;
886
887 [[__gnu__::__always_inline__]]
888 static _Runtime_format_string<_CharT>
889 _S_empty_fs()
890 { return _Runtime_format_string<_CharT>(_S_empty_spec); }
891
892 static constexpr const _CharT* _S_weekdays[]
893 {
894 _GLIBCXX_WIDEN("Sunday"),
895 _GLIBCXX_WIDEN("Monday"),
896 _GLIBCXX_WIDEN("Tuesday"),
897 _GLIBCXX_WIDEN("Wednesday"),
898 _GLIBCXX_WIDEN("Thursday"),
899 _GLIBCXX_WIDEN("Friday"),
900 _GLIBCXX_WIDEN("Saturday"),
901 };
902
903 static constexpr const _CharT* _S_months[]
904 {
905 _GLIBCXX_WIDEN("January"),
906 _GLIBCXX_WIDEN("February"),
907 _GLIBCXX_WIDEN("March"),
908 _GLIBCXX_WIDEN("April"),
909 _GLIBCXX_WIDEN("May"),
910 _GLIBCXX_WIDEN("June"),
911 _GLIBCXX_WIDEN("July"),
912 _GLIBCXX_WIDEN("August"),
913 _GLIBCXX_WIDEN("September"),
914 _GLIBCXX_WIDEN("October"),
915 _GLIBCXX_WIDEN("November"),
916 _GLIBCXX_WIDEN("December"),
917 };
918
919 private:
920 template<typename _OutIter>
921 _OutIter
922 _M_write(_OutIter __out, const locale& __loc, __string_view __s) const
923 {
924#if defined _GLIBCXX_USE_NL_LANGINFO_L && __CHAR_BIT__ == 8
925 __sso_string __buf;
926 // _GLIBCXX_RESOLVE_LIB_DEFECTS
927 // 3565. Handling of encodings in localized formatting
928 // of chrono types is underspecified
929 if constexpr (is_same_v<_CharT, char>)
930 if constexpr (__unicode::__literal_encoding_is_utf8())
931 if (_M_spec._M_localized && _M_spec._M_locale_specific
932 && __loc != locale::classic())
933 {
934 extern string_view
935 __locale_encoding_to_utf8(const locale&, string_view, void*);
936
937 __s = __locale_encoding_to_utf8(__loc, __s, &__buf);
938 }
939#endif
940 return __format::__write(std::move(__out), __s);
941 }
942
943 [[__gnu__::__always_inline__]]
944 static bool
945 _S_localized_spec(_CharT __conv, _CharT __mod)
946 {
947 switch (__conv)
948 {
949 case 'a':
950 case 'A':
951 case 'b':
952 case 'B':
953 case 'c':
954 case 'h':
955 case 'p':
956 case 'r':
957 case 'x':
958 case 'X':
959 return true;
960 case 'z':
961 return false;
962 default:
963 return (bool)__mod;
964 };
965 }
966
967 // Use the formatting locale's std::time_put facet to produce
968 // a locale-specific representation.
969 template<typename _Iter>
970 _Iter
971 _M_locale_fmt(_Iter __out, const locale& __loc, const struct tm& __tm,
972 char __fmt, char __mod) const
973 {
974 basic_ostringstream<_CharT> __os;
975 __os.imbue(__loc);
976 const auto& __tp = use_facet<time_put<_CharT>>(__loc);
977 __tp.put(__os, __os, _S_space, &__tm, __fmt, __mod);
978 if (__os)
979 __out = _M_write(std::move(__out), __loc, __os.view());
980 return __out;
981 }
982
983 __string_view
984 _M_check_ok(const _ChronoData<_CharT>& __t, _CharT& __conv) const
985 {
986 if (!_M_spec._M_debug)
987 {
988 switch (__conv)
989 {
990 case 'a':
991 case 'A':
992 if (!__t._M_weekday.ok()) [[unlikely]]
993 __throw_format_error("format error: invalid weekday");
994 break;
995 case 'b':
996 case 'h':
997 case 'B':
998 if (!__t._M_month.ok()) [[unlikely]]
999 __throw_format_error("format error: invalid month");
1000 break;
1001 default:
1002 break;
1003 }
1004 return __string_view();
1005 }
1006
1007 switch (__conv)
1008 {
1009 // %\0 is extension for handling weekday index
1010 case '\0':
1011 if (__t._M_weekday_index < 1 || __t._M_weekday_index > 5) [[unlikely]]
1012 return _GLIBCXX_WIDEN("index");
1013 break;
1014 case 'a':
1015 case 'A':
1016 if (!__t._M_weekday.ok()) [[unlikely]]
1017 {
1018 __conv = 'w'; // print as decimal number
1019 return _GLIBCXX_WIDEN("weekday");
1020 }
1021 break;
1022 case 'b':
1023 case 'h':
1024 case 'B':
1025 if (!__t._M_month.ok()) [[unlikely]]
1026 {
1027 __conv = 'm'; // print as decimal number
1028 return _GLIBCXX_WIDEN("month");
1029 }
1030 break;
1031 case 'd':
1032 case 'e':
1033 if (!__t._M_day.ok()) [[unlikely]]
1034 return _GLIBCXX_WIDEN("day");
1035 break;
1036 case 'F':
1037 if (!(__t._M_year/__t._M_month/__t._M_day).ok()) [[unlikely]]
1038 return _GLIBCXX_WIDEN("date");
1039 break;
1040 case 'Y':
1041 if (!__t._M_year.ok()) [[unlikely]]
1042 return _GLIBCXX_WIDEN("year");
1043 break;
1044 default:
1045 break;
1046 }
1047 return __string_view();
1048 }
1049
1050 template<typename _OutIter, typename _FormatContext>
1051 _OutIter
1052 _M_format_to(const _ChronoData<_CharT>& __t, _OutIter __out,
1053 _FormatContext& __fc) const
1054 {
1055 auto __first = _M_spec._M_chrono_specs.begin();
1056 const auto __last = _M_spec._M_chrono_specs.end();
1057
1058 auto __print_sign = [__is_neg = __t._M_is_neg, &__out] () mutable {
1059 if (__is_neg)
1060 {
1061 *__out++ = _S_plus_minus[1];
1062 __is_neg = false;
1063 }
1064 return std::move(__out);
1065 };
1066
1067 struct tm __tm{};
1068 bool __use_locale_fmt = false;
1069 if (_M_spec._M_localized && _M_spec._M_locale_specific)
1070 if (__fc.locale() != locale::classic())
1071 {
1072 __use_locale_fmt = true;
1073
1074 __tm.tm_year = (int)__t._M_year - 1900;
1075 __tm.tm_yday = __t._M_day_of_year.count();
1076 __tm.tm_mon = (unsigned)__t._M_month - 1;
1077 __tm.tm_mday = (unsigned)__t._M_day;
1078 __tm.tm_wday = __t._M_weekday.c_encoding();
1079 __tm.tm_hour = __t._M_hours.count();
1080 __tm.tm_min = __t._M_minutes.count();
1081 __tm.tm_sec = __t._M_seconds.count();
1082
1083 // Some locales use %Z in their %c format but we don't want strftime
1084 // to use the system's local time zone (from /etc/localtime or $TZ)
1085 // as the output for %Z. Setting tm_isdst to -1 says there is no
1086 // time zone info available for the time in __tm.
1087 __tm.tm_isdst = -1;
1088
1089#ifdef _GLIBCXX_USE_STRUCT_TM_TM_ZONE
1090 // POSIX.1-2024 adds tm.tm_zone which will be used for %Z.
1091 // BSD has had tm_zone since 1987 but as char* so cast away const.
1092 if (__t._M_zone_cstr)
1093 __tm.tm_zone = const_cast<char*>(__t._M_zone_cstr);
1094#endif
1095 }
1096
1097 // Characters to output for "%n", "%t" and "%%" specifiers.
1098 constexpr const _CharT* __literals = _GLIBCXX_WIDEN("\n\t%");
1099
1100 ++__first; // Skip leading '%' at start of chrono-specs.
1101
1102 _CharT __mod{};
1103 do
1104 {
1105 _CharT __c = *__first++;
1106 __string_view __invalid;
1107 if (_M_spec._M_needs_ok_check)
1108 __invalid = _M_check_ok(__t, __c);
1109
1110 if (__invalid.empty() &&__use_locale_fmt
1111 && _S_localized_spec(__c, __mod)) [[unlikely]]
1112 __out = _M_locale_fmt(std::move(__out), __fc.locale(),
1113 __tm, __c, __mod);
1114 else switch (__c)
1115 {
1116 // %\0 is extension for handling weekday index
1117 case '\0':
1118 __out = _M_wi(__t._M_weekday_index, std::move(__out));
1119 break;
1120 case 'a':
1121 case 'A':
1122 __out = _M_a_A(__t._M_weekday, std::move(__out), __c == 'A');
1123 break;
1124 case 'b':
1125 case 'h':
1126 case 'B':
1127 __out = _M_b_B(__t._M_month, std::move(__out), __c == 'B');
1128 break;
1129 case 'c':
1130 __out = _M_c(__t, std::move(__out));
1131 break;
1132 case 'C':
1133 case 'y':
1134 case 'Y':
1135 __out = _M_C_y_Y(__t._M_year, std::move(__out), __c);
1136 break;
1137 case 'd':
1138 case 'e':
1139 __out = _M_d_e(__t._M_day, std::move(__out), __c);
1140 break;
1141 case 'D':
1142 case 'x':
1143 __out = _M_D_x(__t, std::move(__out));
1144 break;
1145 case 'F':
1146 __out = _M_F(__t, std::move(__out));
1147 break;
1148 case 'g':
1149 case 'G':
1150 case 'V':
1151 __out = _M_g_G_V(__t, std::move(__out), __c);
1152 break;
1153 case 'H':
1154 case 'I':
1155 __out = _M_H_I(__t._M_hours, __print_sign(), __c);
1156 break;
1157 case 'j':
1158 __out = _M_j(__t, __print_sign());
1159 break;
1160 case 'm':
1161 __out = _M_m(__t._M_month, std::move(__out));
1162 break;
1163 case 'M':
1164 __out = _M_M(__t._M_minutes, __print_sign());
1165 break;
1166 case 'p':
1167 __out = _M_p(__t._M_hours, std::move(__out));
1168 break;
1169 case 'q':
1170 __out = _M_q(__t._M_unit_suffix, std::move(__out));
1171 break;
1172 case 'Q':
1173 __out = _M_Q(__t, __print_sign(), __fc);
1174 break;
1175 case 'r':
1176 __out = _M_r(__t, __print_sign());
1177 break;
1178 case 'R':
1179 case 'X':
1180 __out = _M_R_X(__t, __print_sign(), __c != 'R');
1181 break;
1182 case 'T':
1183 __out = _M_T(__t, __print_sign(), __fc);
1184 break;
1185 case 'S':
1186 __out = _M_S(__t, __print_sign(), __fc, __mod != 'O');
1187 break;
1188 case 'u':
1189 case 'w':
1190 __out = _M_u_w(__t._M_weekday, std::move(__out), __c);
1191 break;
1192 case 'U':
1193 case 'W':
1194 __out = _M_U_W(__t, std::move(__out), __c);
1195 break;
1196 case 'z':
1197 __out = _M_z(__t._M_zone_offset, std::move(__out), (bool)__mod);
1198 break;
1199 case 'Z':
1200 __out = _M_Z(__t._M_zone_abbrev, std::move(__out));
1201 break;
1202 case 'n':
1203 *__out++ = __literals[0];
1204 break;
1205 case 't':
1206 *__out++ = __literals[1];
1207 break;
1208 case '%':
1209 *__out++ = __literals[2];
1210 break;
1211 case 'O':
1212 case 'E':
1213 __mod = __c;
1214 continue;
1215 case '}':
1216 __first = __last;
1217 break;
1218 }
1219
1220 if (!__invalid.empty())
1221 {
1222 constexpr __string_view __pref = _GLIBCXX_WIDEN(" is not a valid ");
1223 __out = __format::__write(std::move(__out), __pref);
1224 __out = __format::__write(std::move(__out), __invalid);
1225 }
1226
1227 __mod = _CharT();
1228 // Scan for next '%' and write out everything before it.
1229 __string_view __str(__first, __last - __first);
1230 size_t __pos = __str.find('%');
1231 if (__pos == 0)
1232 ++__first;
1233 else
1234 {
1235 if (__pos == __str.npos)
1236 __first = __last;
1237 else
1238 {
1239 __str.remove_suffix(__str.length() - __pos);
1240 __first += __pos + 1;
1241 }
1242 __out = __format::__write(std::move(__out), __str);
1243 }
1244 }
1245 while (__first != __last);
1246 return std::move(__out);
1247 }
1248
1249 template<typename _OutIter>
1250 _OutIter
1251 _M_wi(unsigned __wi, _OutIter __out) const
1252 {
1253 // %\0 Extension to format weekday index, used only by empty format spec
1254 _CharT __buf[3];
1255 __out = __format::__write(std::move(__out), _S_str_d1(__buf, __wi));
1256 return std::move(__out);
1257 }
1258
1259 template<typename _OutIter>
1260 _OutIter
1261 _M_a_A(chrono::weekday __wd, _OutIter __out, bool __full) const
1262 {
1263 // %a Locale's abbreviated weekday name.
1264 // %A Locale's full weekday name.
1265 __string_view __str = _S_weekdays[__wd.c_encoding()];
1266 if (!__full)
1267 __str = __str.substr(0, 3);
1268 return __format::__write(std::move(__out), __str);
1269 }
1270
1271 template<typename _OutIter>
1272 _OutIter
1273 _M_b_B(chrono::month __m, _OutIter __out, bool __full) const
1274 {
1275 // %b Locale's abbreviated month name.
1276 // %B Locale's full month name.
1277 __string_view __str = _S_months[(unsigned)__m - 1];
1278 if (!__full)
1279 __str = __str.substr(0, 3);
1280 return __format::__write(std::move(__out), __str);
1281 }
1282
1283 template<typename _OutIter>
1284 _OutIter
1285 _M_c(const _ChronoData<_CharT>& __t, _OutIter __out) const
1286 {
1287 // %c Locale's date and time representation, for C-locale: %a %b %e %T %Y
1288 // %Ec Locale's alternate date and time representation, for C-locale same as above
1289
1290 __out = _M_a_A(__t._M_weekday, std::move(__out), false);
1291 *__out = _S_space;
1292 __out = _M_b_B(__t._M_month, std::move(++__out), false);
1293 *__out = _S_space;
1294 __out = _M_d_e(__t._M_day, std::move(++__out), 'e');
1295 *__out = _S_space;
1296 __out = _M_R_X(__t, std::move(++__out), true);
1297 *__out = _S_space;
1298 return _M_C_y_Y(__t._M_year, std::move(++__out), 'Y');
1299 }
1300
1301 template<typename _OutIter>
1302 _OutIter
1303 _M_C_y_Y(chrono::year __y, _OutIter __out, _CharT __conv) const
1304 {
1305 // %C Year divided by 100 using floored division.
1306 // %EC Locale's alternative preresentation of the century (era name).
1307 // %y Last two decimal digits of the year.
1308 // %Oy Locale's alternative representation.
1309 // %Ey Locale's alternative representation of offset from %EC.
1310 // %Y Year as a decimal number.
1311 // %EY Locale's alternative full year representation.
1312
1313 int __yi = (int)__y;
1314 const bool __is_neg = __yi < 0;
1315 __yi = __builtin_abs(__yi);
1316 int __ci = __yi / 100;
1317 // For floored division -123//100 is -2 and -100//100 is -1
1318 if (__conv == 'C' && __is_neg && (__ci * 100) != __yi) [[unlikely]]
1319 ++__ci;
1320
1321 if (__conv != 'y' && __ci >= 100) [[unlikely]]
1322 {
1323 using _FmtStr = _Runtime_format_string<_CharT>;
1324 __string_view __fs = _S_minus_empty_spec + !__is_neg;
1325 __out = std::format_to(std::move(__out), _FmtStr(__fs),
1326 __conv == 'C' ? __ci : __yi);
1327 }
1328 else
1329 {
1330 _CharT __buf[5];
1331 __buf[0] = _S_plus_minus[1];
1332 __string_view __sv(__buf + 3, __buf + 3);
1333 if (__conv != 'y')
1334 {
1335 _S_fill_two_digits(__buf + 1, __ci);
1336 __sv = __string_view(__buf + !__is_neg, __buf + 3);
1337 }
1338 if (__conv != 'C')
1339 {
1340 _S_fill_two_digits(__buf + 3, __yi % 100);
1341 __sv = __string_view(__sv.data(), __buf + 5);
1342 }
1343 __out = __format::__write(std::move(__out), __sv);
1344 }
1345 return __out;
1346 }
1347
1348 template<typename _OutIter>
1349 _OutIter
1350 _M_D_x(const _ChronoData<_CharT>& __t, _OutIter __out) const
1351 {
1352 // %D Equivalent to %m/%d/%y
1353 // %x Locale's date rep, for C-locale: %m/%d/%y
1354 // %Ex Locale's alternative date representation, for C-locale same as above
1355
1356 auto __di = (unsigned)__t._M_day;
1357 auto __mi = (unsigned)__t._M_month;
1358 auto __yi = __builtin_abs((int)__t._M_year) % 100;
1359
1360 if (__mi >= 100 || __di >= 100) [[unlikely]]
1361 {
1362 using _FmtStr = _Runtime_format_string<_CharT>;
1363 __string_view __fs = _GLIBCXX_WIDEN("{:02d}/{:02d}/{:02d}");
1364 __out = std::format_to(std::move(__out), _FmtStr(__fs),
1365 __mi, __di, __yi);
1366 }
1367 else
1368 {
1369 _CharT __buf[8];
1370 __buf[2] = _S_slash;
1371 __buf[5] = _S_slash;
1372 __string_view __sv(__buf, __buf + 8);
1373
1374 _S_fill_two_digits(__buf, __mi);
1375 _S_fill_two_digits(__buf + 3, __di);
1376 _S_fill_two_digits(__buf + 6, __yi);
1377 __out = __format::__write(std::move(__out), __sv);
1378 }
1379 return std::move(__out);
1380 }
1381
1382 template<typename _OutIter>
1383 _OutIter
1384 _M_d_e(chrono::day __d, _OutIter __out, _CharT __conv) const
1385 {
1386 // %d The day of month as a decimal number.
1387 // %Od Locale's alternative representation.
1388 // %e Day of month as decimal number, padded with space.
1389 // %Oe Locale's alternative digits.
1390
1391 unsigned __i = (unsigned)__d;
1392
1393 _CharT __buf[3];
1394 auto __sv = _S_str_d2(__buf, __i);
1395 if (__conv == _CharT('e') && __i < 10)
1396 {
1397 __buf[1] = __sv[1];
1398 __buf[0] = _S_space;
1399 __sv = {__buf, 2};
1400 }
1401
1402 __out = __format::__write(std::move(__out), __sv);
1403 return std::move(__out);
1404 }
1405
1406 template<typename _OutIter>
1407 _OutIter
1408 _M_F(const _ChronoData<_CharT>& __t, _OutIter __out) const
1409 {
1410 auto __di = (unsigned)__t._M_day;
1411 auto __mi = (unsigned)__t._M_month;
1412 auto __yi = (int)__t._M_year;
1413 const bool __is_neg = __yi < 0;
1414 __yi = __builtin_abs(__yi);
1415
1416 if (__yi >= 10000 || __mi >= 100 || __di >= 100) [[unlikely]]
1417 {
1418 using _FmtStr = _Runtime_format_string<_CharT>;
1419 __string_view __fs
1420 = _GLIBCXX_WIDEN("-{:04d}-{:02d}-{:02d}") + !__is_neg;
1421 __out = std::format_to(std::move(__out), _FmtStr(__fs),
1422 __yi, __mi, __di);
1423 }
1424 else
1425 {
1426 _CharT __buf[11];
1427 __buf[0] = _S_plus_minus[1];
1428 __buf[5] = _S_plus_minus[1];
1429 __buf[8] = _S_plus_minus[1];
1430 __string_view __sv(__buf + !__is_neg, __buf + 11);
1431
1432 _S_fill_two_digits(__buf + 1, __yi / 100);
1433 _S_fill_two_digits(__buf + 3, __yi % 100);
1434 _S_fill_two_digits(__buf + 6, __mi);
1435 _S_fill_two_digits(__buf + 9, __di);
1436 __out = __format::__write(std::move(__out), __sv);
1437 }
1438
1439 return std::move(__out);
1440 }
1441
1442 template<typename _OutIter>
1443 _OutIter
1444 _M_g_G_V(const _ChronoData<_CharT>& __t, _OutIter __out,
1445 _CharT __conv) const
1446 {
1447 // %g last two decimal digits of the ISO week-based year.
1448 // %G ISO week-based year.
1449 // %V ISO week-based week number as a decimal number.
1450 // %OV Locale's alternative numeric rep.
1451
1452 // ISO week-based year of __t is the year that contains the nearest
1453 // Thursday. The ISO week of __t is the number of weeks since
1454 // January 1 of that year.
1455
1456 using namespace chrono;
1457 // Offset of the nearest Thursday:
1458 const days __offset = (__t._M_weekday - Monday) - days(3);
1459 // Nearest Thursday as local days:
1460 const local_days __ild = __t._M_ldays - __offset;
1461 // Day of year of nearest Thursday:
1462 days __idoy = __t._M_day_of_year - __offset;
1463
1464 // Year of nearest Thursday:
1465 year __iyear;
1466 if (__idoy <= days(0))
1467 __iyear = __t._M_year - years(1);
1468 else if (__idoy <= days(365))
1469 __iyear = __t._M_year;
1470 else if (__idoy == days(366) && __t._M_year.is_leap())
1471 __iyear = __t._M_year;
1472 else if (__idoy <= days(730))
1473 __iyear = __t._M_year + years(1);
1474 else [[unlikely]]
1475 __iyear = year_month_day(__ild).year();
1476
1477 if (__conv != 'V')
1478 return _M_C_y_Y(__iyear, std::move(__out), "yY"[__conv == 'G']);
1479
1480 if (__iyear != __t._M_year)
1481 __idoy = __ild - local_days(__iyear/January/0);
1482
1483 const auto __wi = chrono::floor<weeks>(__idoy - days(1)).count() + 1;
1484 return __format::__write(std::move(__out), _S_two_digits(__wi));
1485 }
1486
1487 template<typename _OutIter>
1488 _OutIter
1489 _M_H_I(chrono::hours __h, _OutIter __out, _CharT __conv) const
1490 {
1491 // %H The hour (24-hour clock) as a decimal number.
1492 // %OH Locale's alternative representation.
1493 // %I The hour (12-hour clock) as a decimal number.
1494 // %OI Locale's alternative representation.
1495
1496 int __i = __h.count();
1497
1498 if (__conv == _CharT('I'))
1499 {
1500 __i %= 12;
1501 if (__i == 0)
1502 __i = 12;
1503 }
1504 else if (__i >= 100) [[unlikely]]
1505 return std::format_to(std::move(__out), _S_empty_fs(), __i);
1506
1507 return __format::__write(std::move(__out), _S_two_digits(__i));
1508 }
1509
1510 template<typename _OutIter>
1511 _OutIter
1512 _M_j(const _ChronoData<_CharT>& __t, _OutIter __out) const
1513 {
1514 if (!_M_spec._M_needs(_ChronoParts::_DayOfYear))
1515 {
1516 // Decimal number of days, without padding.
1517 auto __d = chrono::floor<chrono::days>(__t._M_hours).count();
1518 return std::format_to(std::move(__out), _S_empty_fs(), __d);
1519 }
1520
1521 auto __d = __t._M_day_of_year.count();
1522 if (__d >= 1000) [[unlikely]]
1523 return std::format_to(std::move(__out), _S_empty_fs(), __d);
1524
1525 _CharT __buf[3];
1526 return __format::__write(std::move(__out), _S_str_d3(__buf, __d));
1527 }
1528
1529 template<typename _OutIter>
1530 _OutIter
1531 _M_m(chrono::month __m, _OutIter __out) const
1532 {
1533 // %m month as a decimal number.
1534 // %Om Locale's alternative representation.
1535 auto __i = (unsigned)__m;
1536 if (__i == 0 && _M_spec._M_debug) [[unlikely]]
1537 // 0 should not be padded to two digits
1538 return __format::__write(std::move(__out), _S_digit(0));
1539
1540 _CharT __buf[3];
1541 return __format::__write(std::move(__out), _S_str_d2(__buf, __i));
1542 }
1543
1544 template<typename _OutIter>
1545 _OutIter
1546 _M_M(chrono::minutes __m, _OutIter __out) const
1547 {
1548 // %M The minute as a decimal number.
1549 // %OM Locale's alternative representation.
1550
1551 auto __i = __m.count();
1552 return __format::__write(std::move(__out), _S_two_digits(__i));
1553 }
1554
1555 template<typename _OutIter>
1556 _OutIter
1557 _M_p(chrono::hours __h, _OutIter __out) const
1558 {
1559 // %p The locale's equivalent of the AM/PM designations.
1560
1561 _CharT __buf[2];
1562 _S_fill_ampm(__buf, __h);
1563 return __format::__write(std::move(__out), __string_view(__buf, 2));
1564 }
1565
1566 template<typename _OutIter>
1567 _OutIter
1568 _M_q(__string_view __us, _OutIter __out) const
1569 {
1570 // %q The duration's unit suffix
1571 return __format::__write(std::move(__out), __us);
1572 }
1573
1574 template<typename _OutIter, typename _FormatContext>
1575 _OutIter
1576 _M_Q(const _ChronoData<_CharT>& __t, _OutIter __out,
1577 _FormatContext&) const
1578 {
1579 // %Q The duration's numeric value.
1580 return std::vformat_to(std::move(__out), _S_empty_spec, __t._M_ereps);
1581 }
1582
1583 template<typename _OutIter>
1584 _OutIter
1585 _M_r(const _ChronoData<_CharT>& __t, _OutIter __out) const
1586 {
1587 // %r Locale's 12-hour clock time, for C-locale: %I:%M:%S %p
1588 auto __hi = __t._M_hours.count() % 12;
1589 if (__hi == 0)
1590 __hi = 12;
1591
1592 _CharT __buf[11];
1593 __buf[2] = _S_colon;
1594 __buf[5] = _S_colon;
1595 __buf[8] = _S_space;
1596 _S_fill_two_digits(__buf, __hi);
1597 _S_fill_two_digits(__buf + 3, __t._M_minutes.count());
1598 _S_fill_two_digits(__buf + 6, __t._M_seconds.count());
1599 _S_fill_ampm(__buf + 9, __t._M_hours);
1600
1601 return __format::__write(std::move(__out), __string_view(__buf, 11));
1602 }
1603
1604 template<typename _OutIter>
1605 _OutIter
1606 _M_R_X(const _ChronoData<_CharT>& __t, _OutIter __out,
1607 bool __secs) const
1608 {
1609 // %R Equivalent to %H:%M
1610 // %X Locale's time rep, for C-locale: %H:%M:%S (without subseconds)
1611 // %EX Locale's alternative time representation, for C-locale same as above
1612
1613 auto __hi = __t._M_hours.count();
1614
1615 _CharT __buf[8];
1616 __buf[2] = _S_colon;
1617 __buf[5] = _S_colon;
1618 __string_view __sv(__buf, 8);
1619
1620 if (__hi >= 100) [[unlikely]]
1621 {
1622 __out = std::format_to(std::move(__out), _S_empty_fs(), __hi);
1623 __sv.remove_prefix(2);
1624 }
1625 else
1626 _S_fill_two_digits(__buf, __hi);
1627
1628 _S_fill_two_digits(__buf + 3, __t._M_minutes.count());
1629 if (__secs)
1630 _S_fill_two_digits(__buf + 6, __t._M_seconds.count());
1631 else
1632 __sv.remove_suffix(3);
1633
1634 return __format::__write(std::move(__out), __sv);
1635 }
1636
1637 template<typename _OutIter, typename _FormatContext>
1638 _OutIter
1639 _M_S(const _ChronoData<_CharT>& __t, _OutIter __out,
1640 _FormatContext& __ctx, bool __subs = true) const
1641 {
1642 // %S Seconds as a decimal number.
1643 // %OS The locale's alternative representation.
1644 auto __s = __t._M_seconds;
1645
1646 __out = __format::__write(std::move(__out),
1647 _S_two_digits(__s.count()));
1648 if (__subs)
1649 __out = _M_subsecs(__t, std::move(__out), __ctx);
1650 return __out;
1651 }
1652
1653 template<typename _OutIter, typename _FormatContext>
1654 _OutIter
1655 _M_subsecs(const _ChronoData<_CharT>& __t, _OutIter __out,
1656 _FormatContext& __ctx) const
1657 {
1658 unsigned __prec = _M_spec._M_prec_kind != _WP_none
1659 ? _M_spec._M_get_precision(__ctx)
1660 : _M_spec._M_prec;
1661 if (__prec == 0)
1662 return __out;
1663
1664 _CharT __dot = _S_dot;
1665 if (_M_spec._M_localized) [[unlikely]]
1666 {
1667 auto __loc = __ctx.locale();
1668 const auto& __np = use_facet<numpunct<_CharT>>(__loc);
1669 __dot = __np.decimal_point();
1670 }
1671 *__out = __dot;
1672 ++__out;
1673
1674 if (_M_spec._M_floating_point_rep)
1675 {
1676 _Str_sink<_CharT> __sink;
1677 if (_M_spec._M_localized && _M_spec._M_custom_rep) [[unlikely]]
1678 std::vformat_to(__sink.out(), __ctx.locale(),
1679 _GLIBCXX_WIDEN("{1:0.{2}Lf}"), __t._M_ereps);
1680 else
1681 std::vformat_to(__sink.out(),
1682 _GLIBCXX_WIDEN("{1:0.{2}f}"), __t._M_ereps);
1683
1684 auto __sv = __sink.view();
1685 // Skip leading zero and dot
1686 __sv.remove_prefix(2);
1687 return __format::__write(std::move(__out), __sv);
1688 }
1689
1690 constexpr unsigned __max_prec = _ChronoData<_CharT>::_S_max_prec;
1691 constexpr typename _ChronoData<_CharT>::_Attoseconds::rep __pow10t[]
1692 {
1693 1u,
1694 10u, 100u, 1000u,
1695 10'000u, 100'000u, 1000'000u,
1696 10'000'000u, 100'000'000u, 1000'000'000u,
1697 10'000'000'000u, 100'000'000'000u, 1000'000'000'000u,
1698 10'000'000'000'000u, 100'000'000'000'000u, 1000'000'000'000'000u,
1699 10'000'000'000'000'000u, 100'000'000'000'000'000u, 1000'000'000'000'000'000u,
1700 };
1701
1702 auto __subs = __t._M_subseconds.count();
1703 if (__prec < __max_prec)
1704 __subs /= __pow10t[__max_prec - __prec];
1705 else if (__prec > __max_prec)
1706 __prec = __max_prec;
1707
1708 using _FmtStr = _Runtime_format_string<_CharT>;
1709 return std::format_to(__out, _FmtStr(_GLIBCXX_WIDEN("{0:0{1}}")),
1710 __subs, __prec);
1711 }
1712
1713 // %t handled in _M_format
1714
1715 template<typename _OutIter, typename _FormatContext>
1716 _OutIter
1717 _M_T(const _ChronoData<_CharT>& __t, _OutIter __out,
1718 _FormatContext& __ctx) const
1719 {
1720 // %T Equivalent to %H:%M:%S, with subseconds
1721 __out = _M_R_X(__t, std::move(__out), true);
1722 return _M_subsecs(__t, std::move(__out), __ctx);
1723 }
1724
1725 template<typename _OutIter>
1726 _OutIter
1727 _M_u_w(chrono::weekday __wd, _OutIter __out, _CharT __conv) const
1728 {
1729 // %u ISO weekday as a decimal number (1-7), where Monday is 1.
1730 // %Ou Locale's alternative numeric rep.
1731 // %w Weekday as a decimal number (0-6), where Sunday is 0.
1732 // %Ow Locale's alternative numeric rep.
1733 unsigned __wdi = __conv == 'u' ? __wd.iso_encoding()
1734 : __wd.c_encoding();
1735 _CharT __buf[3];
1736 return __format::__write(std::move(__out), _S_str_d1(__buf, __wdi));
1737 }
1738
1739 template<typename _OutIter>
1740 _OutIter
1741 _M_U_W(const _ChronoData<_CharT>& __t, _OutIter __out,
1742 _CharT __conv) const
1743 {
1744 // %U Week number of the year as a decimal number, from first Sunday.
1745 // %OU Locale's alternative numeric rep.
1746 // %W Week number of the year as a decimal number, from first Monday.
1747 // %OW Locale's alternative numeric rep.
1748
1749 using namespace chrono;
1750 const weekday __weekstart = __conv == 'U' ? Sunday : Monday;
1751 const days __offset = __t._M_weekday - __weekstart;
1752 auto __weeks = chrono::floor<weeks>(__t._M_day_of_year - __offset - days(1));
1753 return __format::__write(std::move(__out), _S_two_digits(__weeks.count() + 1));
1754 }
1755
1756 template<typename _OutIter>
1757 _OutIter
1758 _M_z(chrono::seconds __ts, _OutIter __out, bool __mod = false) const
1759 {
1760 if (__ts == 0s)
1761 {
1762 __string_view __zero
1763 = __mod ? _GLIBCXX_WIDEN("+00:00") : _GLIBCXX_WIDEN("+0000");
1764 return __format::__write(std::move(__out), __zero);
1765 }
1766
1767 chrono::hh_mm_ss<chrono::seconds> __hms(__ts);
1768 unsigned __mo = 3 + __mod;
1769
1770 _CharT __buf[6];
1771 __buf[0] = _S_plus_minus[__hms.is_negative()];
1772 __buf[3] = _S_colon;
1773 _S_fill_two_digits(__buf + 1, __hms.hours().count());
1774 _S_fill_two_digits(__buf + __mo, __hms.minutes().count());
1775
1776 __string_view __sv(__buf, __mo + 2);
1777 return __format::__write(std::move(__out), __sv);
1778 }
1779
1780 template<typename _OutIter>
1781 _OutIter
1782 _M_Z(__string_view __abbrev, _OutIter __out) const
1783 { return __format::__write(std::move(__out), __abbrev); }
1784
1785 // %% handled in _M_format
1786
1787 // A string view of single digit character, "0".."9".
1788 static basic_string_view<_CharT>
1789 _S_digit(int __n) noexcept
1790 {
1791 // Extra 9s avoid past-the-end read on bad input.
1792 return { _GLIBCXX_WIDEN("0123456789999999") + (__n & 0xf), 1 };
1793 }
1794
1795 // A string view of two digit characters, "00".."99".
1796 static basic_string_view<_CharT>
1797 _S_two_digits(int __n) noexcept
1798 {
1799 return {
1800 _GLIBCXX_WIDEN("0001020304050607080910111213141516171819"
1801 "2021222324252627282930313233343536373839"
1802 "4041424344454647484950515253545556575859"
1803 "6061626364656667686970717273747576777879"
1804 "8081828384858687888990919293949596979899"
1805 "9999999999999999999999999999999999999999"
1806 "9999999999999999") + 2 * (__n & 0x7f),
1807 2
1808 };
1809 }
1810
1811 // Fills __buf[0] and __buf[1] with 2 digit value of __n.
1812 [[__gnu__::__always_inline__]]
1813 static void
1814 _S_fill_two_digits(_CharT* __buf, unsigned __n)
1815 {
1816 auto __sv = _S_two_digits(__n);
1817 __buf[0] = __sv[0];
1818 __buf[1] = __sv[1];
1819 }
1820
1821 // Fills __buf[0] and __buf[1] with "AM", "PM" depending on __h.
1822 [[__gnu__::__always_inline__]]
1823 static void
1824 _S_fill_ampm(_CharT* __buf, chrono::hours __h)
1825 {
1826 auto __hi = __h.count();
1827 if (__hi >= 24) [[unlikely]]
1828 __hi %= 24;
1829
1830 constexpr const _CharT* __apm = _GLIBCXX_WIDEN("APM");
1831 __buf[0] = __apm[__hi >= 12];
1832 __buf[1] = __apm[2];
1833 }
1834
1835 // Returns decimal representation of __n.
1836 // Returned string_view may point to __buf.
1837 [[__gnu__::__always_inline__]]
1838 static basic_string_view<_CharT>
1839 _S_str_d1(span<_CharT, 3> __buf, unsigned __n)
1840 {
1841 if (__n < 10) [[likely]]
1842 return _S_digit(__n);
1843 return _S_str_d2(__buf, __n);
1844 }
1845
1846 // Returns decimal representation of __n, padded to 2 digits.
1847 // Returned string_view may point to __buf.
1848 [[__gnu__::__always_inline__]]
1849 static basic_string_view<_CharT>
1850 _S_str_d2(span<_CharT, 3> __buf, unsigned __n)
1851 {
1852 if (__n < 100) [[likely]]
1853 return _S_two_digits(__n);
1854 return _S_str_d3(__buf, __n);
1855 }
1856
1857 // Returns decimal representation of __n, padded to 3 digits.
1858 // Returned string_view points to __buf.
1859 [[__gnu__::__always_inline__]]
1860 static basic_string_view<_CharT>
1861 _S_str_d3(span<_CharT, 3> __buf, unsigned __n)
1862 {
1863 _S_fill_two_digits(__buf.data(), __n / 10);
1864 __buf[2] = _S_chars[__n % 10];
1865 return __string_view(__buf.data(), 3);
1866 }
1867 };
1868
1869 template<typename _CharT>
1870 struct __formatter_duration : private __formatter_chrono<_CharT>
1871 {
1872 template<typename _Rep, typename _Period>
1873 constexpr static auto
1874 _S_subseconds(const chrono::duration<_Rep, _Period>& __d)
1875 {
1876 if constexpr (chrono::treat_as_floating_point_v<_Rep>)
1877 return chrono::duration<_Rep>(__d);
1878 else if constexpr (_Period::den == 1)
1879 return chrono::seconds(0);
1880 else
1881 {
1882 using _Attoseconds = _ChronoData<_CharT>::_Attoseconds;
1883 using _CRep = common_type_t<_Rep, typename _Attoseconds::rep>;
1884 chrono::duration<_CRep, _Period> subs(__d.count());
1885 return chrono::duration_cast<_Attoseconds>(subs);
1886 }
1887 }
1888
1889 public:
1890 template<typename _Duration>
1891 static consteval
1892 _ChronoSpec<_CharT>
1893 _S_spec_for(_ChronoParts __parts)
1894 {
1895 using _Rep = typename _Duration::rep;
1896 using enum _ChronoParts;
1897
1898 _ChronoSpec<_CharT> __res{};
1899 __res._M_floating_point_rep = chrono::treat_as_floating_point_v<_Rep>;
1900 __res._M_custom_rep = !is_arithmetic_v<_Rep>;
1901 __res._M_prec = chrono::hh_mm_ss<_Duration>::fractional_width;
1902 if ((__parts & _TimeOfDay) != 0)
1903 __res._M_localized = __res._M_prec > 0 || __res._M_floating_point_rep;
1904
1905 if ((__parts & _TimeOfDay) != 0)
1906 __res._M_needed |= _TimeOfDay;
1907 if ((__parts & _Date) != 0)
1908 __res._M_needed |= _YearMonthDay;
1909 if ((__parts & _ZoneAbbrev) != 0)
1910 __res._M_needed |= _ZoneAbbrev;
1911
1912 switch (__parts)
1913 {
1914 case _ZonedDateTime:
1915 __res._M_chrono_specs = _ChronoFormats<_CharT>::_S_ftz();
1916 break;
1917 case _DateTime:
1918 __res._M_chrono_specs = _ChronoFormats<_CharT>::_S_ft();
1919 break;
1920 case _Date:
1921 __res._M_chrono_specs = _ChronoFormats<_CharT>::_S_f();
1922 break;
1923 case _Time:
1924 __res._M_chrono_specs = _ChronoFormats<_CharT>::_S_t();
1925 break;
1926 case _None:
1927 break;
1928 default:
1929 __builtin_unreachable();
1930 }
1931 return __res;
1932 };
1933
1934 template<typename _Duration>
1935 static consteval
1936 _ChronoSpec<_CharT>
1937 _S_spec_for_tp()
1938 {
1939 using enum _ChronoParts;
1940 // streaming of local_time is defined in terms of sys_time
1941 constexpr bool __stream_insertable =
1942 requires (basic_ostream<_CharT>& __os, chrono::sys_time<_Duration> __t)
1943 { __os << __t; };
1944 if constexpr (!__stream_insertable)
1945 return _S_spec_for<_Duration>(_None);
1946 else if constexpr (is_convertible_v<_Duration, chrono::days>)
1947 return _S_spec_for<_Duration>(_Date);
1948 else
1949 return _S_spec_for<_Duration>(_DateTime);
1950 }
1951
1952 using __formatter_chrono<_CharT>::__formatter_chrono;
1953 using __formatter_chrono<_CharT>::_M_spec;
1954
1955 template<typename _Duration>
1956 constexpr typename basic_format_parse_context<_CharT>::iterator
1957 _M_parse(basic_format_parse_context<_CharT>& __pc, _ChronoParts __parts,
1958 const _ChronoSpec<_CharT>& __def)
1959 {
1960 using _Rep = typename _Duration::rep;
1961 using enum _ChronoParts;
1962
1963 auto __res
1964 = __formatter_chrono<_CharT>::_M_parse(__pc, __parts, __def);
1965 // n.b. durations do not contain date parts, and for time point all
1966 // date parts are computed, and they are always ok.
1967 _M_spec._M_needs_ok_check = false;
1968
1969 // check for custom floating point durations, if digits of output
1970 // will contain subseconds, then formatters must support specifying
1971 // precision.
1972 if constexpr (!is_floating_point_v<_Rep>)
1973 if constexpr (chrono::treat_as_floating_point_v<_Rep>)
1974 if (_M_spec._M_needs(_Subseconds|_EpochUnits)
1975 || _M_spec._M_prec_kind != _WP_none
1976 || _M_spec._M_prec_value > 0)
1977 {
1978 constexpr const _CharT* __fs = _GLIBCXX_WIDEN("#02.5Lf");
1979 basic_format_parse_context<_CharT> __npc(__fs);
1980 formatter<_Rep, _CharT> __fmtter;
1981 __fmtter.parse(__npc);
1982 }
1983 return __res;
1984 }
1985
1986 // Return the formatting locale.
1987 template<typename _FormatContext>
1988 std::locale
1989 _M_locale(_FormatContext& __fc) const
1990 {
1991 if (!_M_spec._M_localized)
1992 return std::locale::classic();
1993 else
1994 return __fc.locale();
1995 }
1996
1997 // Format duration for empty chrono-specs, e.g. "{}" (C++20 [time.format] p6).
1998 template<typename _Rep, typename _Period, typename _FormatContext>
1999 typename _FormatContext::iterator
2000 _M_format_to_ostream(const chrono::duration<_Rep, _Period>& __d,
2001 bool __is_neg,
2002 _FormatContext& __fc) const
2003 {
2004 basic_ostringstream<_CharT> __os;
2005 __os.imbue(this->_M_locale(__fc));
2006
2007 if (__is_neg) [[unlikely]]
2008 __os << this->_S_plus_minus[1];
2009 __os << __d;
2010
2011 auto __str = std::move(__os).str();
2012 return __format::__write_padded_as_spec(__str, __str.size(),
2013 __fc, _M_spec);
2014 }
2015
2016 template<typename _Rep1, typename _Period1,
2017 typename _Rep2, typename _Period2,
2018 typename _FormatContext>
2019 typename _FormatContext::iterator
2020 _M_format_units(_ChronoData<_CharT>& __cd,
2021 const chrono::duration<_Rep1, _Period1>& __ed,
2022 const chrono::duration<_Rep2, _Period2>& __ss,
2023 _FormatContext& __fc) const
2024 {
2025 __format::_Str_sink<_CharT> __suffix_store;
2026 constexpr auto _S_unit_suffix
2027 = chrono::__detail::__units_suffix<_Period1, _CharT>();
2028 if constexpr (!_S_unit_suffix.empty())
2029 __cd._M_unit_suffix = _S_unit_suffix;
2030 else if (_M_spec._M_needs(_ChronoParts::_UnitSuffix))
2031 {
2032 chrono::__detail::
2033 __fmt_units_suffix<_Period1, _CharT>(__suffix_store.out());
2034 __cd._M_unit_suffix = __suffix_store.view();
2035 }
2036
2037 const auto __prec = _M_spec._M_prec_kind != _WP_none
2038 ? _M_spec._M_get_precision(__fc)
2039 : _M_spec._M_prec;
2040
2041 using _ErasedContext = typename _ChronoData<_CharT>::_FormatContext;
2042 // _GLIBCXX_RESOLVE_LIB_DEFECTS
2043 // 4118. How should duration formatters format custom rep?
2044 auto __ereps = +__ed.count();
2045 if (!_M_spec._M_needs(_ChronoParts::_Subseconds))
2046 {
2047 auto __ssreps = 0u;
2048 auto __args_store
2049 = std::make_format_args<_ErasedContext>(__ereps, __ssreps, __prec);
2050 __cd._M_ereps = __args_store;
2051 return this->_M_format(__cd, __fc);
2052 }
2053
2054 using _Attoseconds = _ChronoData<_CharT>::_Attoseconds;
2055 auto __nss = _S_subseconds(__ss);
2056 __cd._M_subseconds = chrono::duration_cast<_Attoseconds>(__nss);
2057
2058 auto __ssreps = __nss.count();
2059 auto __args_store
2060 = std::make_format_args<_ErasedContext>(__ereps, __ssreps, __prec);
2061 __cd._M_ereps = __args_store;
2062
2063 return this->_M_format(__cd, __fc);
2064 }
2065
2066 // pre: __cd._M_lseconds and __cd._M_eseconds are set.
2067 template<typename _Rep1, typename _Period1, typename _FormatContext>
2068 typename _FormatContext::iterator
2069 _M_format_time_point(_ChronoData<_CharT>& __cd,
2070 const chrono::duration<_Rep1, _Period1>& __ed,
2071 _FormatContext& __fc) const
2072 {
2073 auto __parts = _M_spec._M_needed - _ChronoParts::_TotalSeconds;
2074 if ((__parts & _ChronoParts::_DateTime) != 0)
2075 __cd._M_fill_date_time(__cd._M_lseconds, __parts);
2076 return _M_format_units(__cd, __ed, __ed - __cd._M_eseconds, __fc);
2077 }
2078 };
2079
2080#if _GLIBCXX_USE_CXX11_ABI || ! _GLIBCXX_USE_DUAL_ABI
2081 template<typename _CharT>
2082 struct __formatter_chrono_info
2083 {
2084 constexpr typename basic_format_parse_context<_CharT>::iterator
2085 parse(basic_format_parse_context<_CharT>& __pc)
2086 { return _M_f._M_parse(__pc, _ChronoParts(), {}); }
2087
2088 template<typename _Info, typename _Out>
2089 typename basic_format_context<_Out, _CharT>::iterator
2090 format(const _Info& __i,
2091 basic_format_context<_Out, _CharT>& __fc) const
2092 {
2093 // n.b. only acceptable chrono-spec for info is one containing
2094 // only whitespaces and %%, that do not depend on formatted object.
2095 if (!_M_f._M_spec._M_chrono_specs.empty()) [[unlikely]]
2096 return _M_f._M_format(_ChronoData<_CharT>{}, __fc);
2097
2098 const size_t __padwidth = _M_f._M_spec._M_get_width(__fc);
2099 if (__padwidth == 0)
2100 return _M_format_to(__fc.out(), __i);
2101
2102 _Padding_sink<_Out, _CharT> __sink(__fc.out(), __padwidth);
2103 _M_format_to(__sink.out(), __i);
2104 return __sink._M_finish(_M_f._M_spec._M_align, _M_f._M_spec._M_fill);
2105 }
2106
2107 private:
2108 template<typename _Out>
2109 _Out
2110 _M_format_to(_Out __out, const chrono::sys_info& __si) const
2111 {
2112 using _FmtStr = _Runtime_format_string<_CharT>;
2113 // n.b. only decimal separator is locale dependent for specifiers
2114 // used below, as sys_info uses seconds and minutes duration, the
2115 // output is locale-independent.
2116 constexpr auto* __fs
2117 = _GLIBCXX_WIDEN("[{0:%F %T},{1:%F %T},{2:%T},{3:%Q%q},{0:%Z}]");
2118 const chrono::local_seconds __lb(__si.begin.time_since_epoch());
2119 return std::format_to(std::move(__out), _FmtStr(__fs),
2120 chrono::local_time_format(__lb, &__si.abbrev),
2121 __si.end, __si.offset, __si.save);
2122 }
2123
2124 template<typename _Out>
2125 _Out
2126 _M_format_to(_Out __out, const chrono::local_info& __li) const
2127 {
2128 *__out = _Separators<_CharT>::_S_squares()[0];
2129 ++__out;
2130 if (__li.result == chrono::local_info::unique)
2131 __out = _M_format_to(std::move(__out), __li.first);
2132 else
2133 {
2134 basic_string_view<_CharT> __sv;
2135 if (__li.result == chrono::local_info::nonexistent)
2136 __sv =_GLIBCXX_WIDEN("nonexistent");
2137 else
2138 __sv = _GLIBCXX_WIDEN("ambiguous");
2139 __out = __format::__write(std::move(__out), __sv);
2140
2141 __sv = _GLIBCXX_WIDEN(" local time between ");
2142 __out = __format::__write(std::move(__out), __sv);
2143 __out = _M_format_to(std::move(__out), __li.first);
2144
2145 __sv = _GLIBCXX_WIDEN(" and ");
2146 __out = __format::__write(std::move(__out), __sv);
2147 __out = _M_format_to(std::move(__out), __li.second);
2148 }
2149 *__out = _Separators<_CharT>::_S_squares()[1];
2150 ++__out;
2151 return std::move(__out);
2152 }
2153
2154 __formatter_chrono<_CharT> _M_f;
2155 };
2156#endif
2157
2158} // namespace __format
2159/// @endcond
2160
2161 template<typename _Rep, typename _Period, typename _CharT>
2162 requires __format::__formattable_impl<_Rep, _CharT>
2163 struct formatter<chrono::duration<_Rep, _Period>, _CharT>
2164 {
2165 constexpr typename basic_format_parse_context<_CharT>::iterator
2166 parse(basic_format_parse_context<_CharT>& __pc)
2167 {
2168 using enum __format::_ChronoParts;
2169 return _M_f.template _M_parse<_Duration>(__pc, _EpochTime, __defSpec);
2170 }
2171
2172 template<typename _Out>
2173 typename basic_format_context<_Out, _CharT>::iterator
2174 format(const chrono::duration<_Rep, _Period>& __d,
2175 basic_format_context<_Out, _CharT>& __fc) const
2176 {
2177 if constexpr (numeric_limits<_Rep>::is_signed)
2178 if (__d < __d.zero()) [[unlikely]]
2179 {
2180 if constexpr (is_integral_v<_Rep>)
2181 {
2182 // -d is undefined for the most negative integer.
2183 // Convert duration to corresponding unsigned rep.
2184 using _URep = make_unsigned_t<_Rep>;
2185 auto __ucnt = -static_cast<_URep>(__d.count());
2186 auto __ud = chrono::duration<_URep, _Period>(__ucnt);
2187 return _M_format(__ud, true, __fc);
2188 }
2189 else
2190 return _M_format(-__d, true, __fc);
2191 }
2192 return _M_format(__d, false, __fc);
2193 }
2194
2195 private:
2196 using _Duration = chrono::duration<_Rep, _Period>;
2197
2198 static constexpr __format::_ChronoSpec<_CharT> __defSpec = []
2199 {
2200 using enum __format::_ChronoParts;
2201 auto __res = __format::__formatter_duration<_CharT>::
2202 template _S_spec_for<_Duration>(_None);
2203 __res._M_localized = !is_integral_v<_Rep>;
2204 // n.b. for integral format output is the same as ostream output
2205 if constexpr (is_integral_v<_Rep>)
2206 {
2207 __res._M_needed = _EpochUnits|_UnitSuffix;
2208 __res._M_chrono_specs = _GLIBCXX_WIDEN("%Q%q");
2209 }
2210 return __res;
2211 }();
2212
2213 template<typename _Rep2, typename _Out>
2214 typename basic_format_context<_Out, _CharT>::iterator
2215 _M_format(const chrono::duration<_Rep2, _Period>& __d,
2216 bool __is_neg,
2217 basic_format_context<_Out, _CharT>& __fc) const
2218 {
2219 using namespace chrono;
2220 using enum __format::_ChronoParts;
2221 if constexpr (!is_integral_v<_Rep>)
2222 if (_M_f._M_spec._M_chrono_specs.empty())
2223 return _M_f._M_format_to_ostream(__d, __is_neg, __fc);
2224
2225 __format::_ChronoData<_CharT> __cd;
2226 __cd._M_is_neg = __is_neg;
2227 auto __ts = chrono::floor<chrono::seconds>(__d);
2228 __cd._M_eseconds = __ts;
2229 if (_M_f._M_spec._M_needs(_HoursMinutesSeconds))
2230 __cd._M_fill_time(__ts);
2231 return _M_f._M_format_units(__cd, __d, __d - __ts, __fc);
2232 }
2233
2234 __format::__formatter_duration<_CharT> _M_f{__defSpec};
2235 };
2236
2237#if __glibcxx_print >= 202406L
2238 // _GLIBCXX_RESOLVE_LIB_DEFECTS
2239 // 4400. enable_nonlocking_formatter_optimization for durations with custom rep
2240 template<typename _Rep, typename _Period>
2241 constexpr bool
2242 enable_nonlocking_formatter_optimization<chrono::duration<_Rep, _Period>>
2243 = is_arithmetic_v<_Rep>;
2244#endif
2245
2246 template<__format::__char _CharT>
2247 struct formatter<chrono::day, _CharT>
2248 {
2249 constexpr typename basic_format_parse_context<_CharT>::iterator
2250 parse(basic_format_parse_context<_CharT>& __pc)
2251 {
2252 using enum __format::_ChronoParts;
2253 return _M_f._M_parse(__pc, _Day|_WeekdayIndex, __defSpec);
2254 }
2255
2256 template<typename _Out>
2257 typename basic_format_context<_Out, _CharT>::iterator
2258 format(const chrono::day& __t,
2259 basic_format_context<_Out, _CharT>& __fc) const
2260 {
2261 __format::_ChronoData<_CharT> __cd{};
2262 __cd._M_fill_day(__t, __defSpec._M_needed);
2263 return _M_f._M_format(__cd, __fc);
2264 }
2265
2266 private:
2267 static constexpr __format::_ChronoSpec<_CharT> __defSpec = []
2268 {
2269 using __format::_ChronoFormats;
2270 using enum __format::_ChronoParts;
2271
2272 __format::_ChronoSpec<_CharT> __res{};
2273 __res._M_debug = true;
2274 __res._M_needed = _Day;
2275 __res._M_chrono_specs = _ChronoFormats<_CharT>::_S_d();
2276 return __res;
2277 }();
2278
2279 __format::__formatter_chrono<_CharT> _M_f{__defSpec};
2280 };
2281
2282#if __glibcxx_print >= 202406L
2283 template<>
2284 inline constexpr bool
2285 enable_nonlocking_formatter_optimization<chrono::day> = true;
2286#endif
2287
2288 template<__format::__char _CharT>
2289 struct formatter<chrono::month, _CharT>
2290 {
2291 constexpr typename basic_format_parse_context<_CharT>::iterator
2292 parse(basic_format_parse_context<_CharT>& __pc)
2293 {
2294 using enum __format::_ChronoParts;
2295 return _M_f._M_parse(__pc, _Month, __defSpec);
2296 }
2297
2298 template<typename _Out>
2299 typename basic_format_context<_Out, _CharT>::iterator
2300 format(const chrono::month& __t,
2301 basic_format_context<_Out, _CharT>& __fc) const
2302 {
2303 __format::_ChronoData<_CharT> __cd{};
2304 __cd._M_month = __t;
2305 return _M_f._M_format(__cd, __fc);
2306 }
2307
2308 private:
2309 static constexpr __format::_ChronoSpec<_CharT> __defSpec = []
2310 {
2311 using __format::_ChronoFormats;
2312 using enum __format::_ChronoParts;
2313
2314 __format::_ChronoSpec<_CharT> __res{};
2315 __res._M_debug = true;
2316 __res._M_localized = true;
2317 __res._M_locale_specific = true;
2318 __res._M_needed = _Month;
2319 __res._M_chrono_specs = _ChronoFormats<_CharT>::_S_m();
2320 return __res;
2321 }();
2322
2323 __format::__formatter_chrono<_CharT> _M_f{__defSpec};
2324 };
2325
2326#if __glibcxx_print >= 202406L
2327 template<>
2328 inline constexpr bool
2329 enable_nonlocking_formatter_optimization<chrono::month> = true;
2330#endif
2331
2332 template<__format::__char _CharT>
2333 struct formatter<chrono::year, _CharT>
2334 {
2335 constexpr typename basic_format_parse_context<_CharT>::iterator
2336 parse(basic_format_parse_context<_CharT>& __pc)
2337 {
2338 using enum __format::_ChronoParts;
2339 return _M_f._M_parse(__pc, _Year, __defSpec);
2340 }
2341
2342 template<typename _Out>
2343 typename basic_format_context<_Out, _CharT>::iterator
2344 format(const chrono::year& __t,
2345 basic_format_context<_Out, _CharT>& __fc) const
2346 {
2347 __format::_ChronoData<_CharT> __cd{};
2348 __cd._M_year = __t;
2349 return _M_f._M_format(__cd, __fc);
2350 }
2351
2352 private:
2353 static constexpr __format::_ChronoSpec<_CharT> __defSpec = []
2354 {
2355 using __format::_ChronoFormats;
2356 using enum __format::_ChronoParts;
2357
2358 __format::_ChronoSpec<_CharT> __res{};
2359 __res._M_debug = true;
2360 __res._M_needed = _Year;
2361 __res._M_chrono_specs = _ChronoFormats<_CharT>::_S_y();
2362 return __res;
2363 }();
2364
2365 __format::__formatter_chrono<_CharT> _M_f{__defSpec};
2366 };
2367
2368#if __glibcxx_print >= 202406L
2369 template<>
2370 inline constexpr bool
2371 enable_nonlocking_formatter_optimization<chrono::year> = true;
2372#endif
2373
2374 template<__format::__char _CharT>
2375 struct formatter<chrono::weekday, _CharT>
2376 {
2377 constexpr typename basic_format_parse_context<_CharT>::iterator
2378 parse(basic_format_parse_context<_CharT>& __pc)
2379 {
2380 using enum __format::_ChronoParts;
2381 return _M_f._M_parse(__pc, _Weekday, __defSpec);
2382 }
2383
2384 template<typename _Out>
2385 typename basic_format_context<_Out, _CharT>::iterator
2386 format(const chrono::weekday& __t,
2387 basic_format_context<_Out, _CharT>& __fc) const
2388 {
2389 __format::_ChronoData<_CharT> __cd{};
2390 __cd._M_weekday = __t;
2391 return _M_f._M_format(__cd, __fc);
2392 }
2393
2394 private:
2395 static constexpr __format::_ChronoSpec<_CharT> __defSpec = []
2396 {
2397 using __format::_ChronoFormats;
2398 using enum __format::_ChronoParts;
2399
2400 __format::_ChronoSpec<_CharT> __res{};
2401 __res._M_debug = true;
2402 __res._M_localized = true;
2403 __res._M_locale_specific = true;
2404 __res._M_needed = _Weekday;
2405 __res._M_chrono_specs = _ChronoFormats<_CharT>::_S_w();
2406 return __res;
2407 }();
2408
2409 __format::__formatter_chrono<_CharT> _M_f{__defSpec};
2410 };
2411
2412#if __glibcxx_print >= 202406L
2413 template<>
2414 inline constexpr bool
2415 enable_nonlocking_formatter_optimization<chrono::weekday> = true;
2416#endif
2417
2418 template<__format::__char _CharT>
2419 struct formatter<chrono::weekday_indexed, _CharT>
2420 {
2421 constexpr typename basic_format_parse_context<_CharT>::iterator
2422 parse(basic_format_parse_context<_CharT>& __pc)
2423 {
2424 using enum __format::_ChronoParts;
2425 return _M_f._M_parse(__pc, _IndexedWeekday, __defSpec);
2426 }
2427
2428 template<typename _Out>
2429 typename basic_format_context<_Out, _CharT>::iterator
2430 format(const chrono::weekday_indexed& __t,
2431 basic_format_context<_Out, _CharT>& __fc) const
2432 {
2433 __format::_ChronoData<_CharT> __cd{};
2434 __cd._M_fill_weekday(__t, __defSpec._M_needed);
2435 return _M_f._M_format(__cd, __fc);
2436 }
2437
2438 private:
2439 static constexpr __format::_ChronoSpec<_CharT> __defSpec = []
2440 {
2441 using __format::_ChronoFormats;
2442 using enum __format::_ChronoParts;
2443
2444 __format::_ChronoSpec<_CharT> __res{};
2445 __res._M_debug = true;
2446 __res._M_localized = true;
2447 __res._M_locale_specific = true;
2448 __res._M_needed = _IndexedWeekday;
2449 __res._M_chrono_specs = _ChronoFormats<_CharT>::_S_wi();
2450 return __res;
2451 }();
2452
2453 __format::__formatter_chrono<_CharT> _M_f{__defSpec};
2454 };
2455
2456#if __glibcxx_print >= 202406L
2457 template<>
2458 inline constexpr bool
2459 enable_nonlocking_formatter_optimization<chrono::weekday_indexed> = true;
2460#endif
2461
2462 template<__format::__char _CharT>
2463 struct formatter<chrono::weekday_last, _CharT>
2464 {
2465 constexpr typename basic_format_parse_context<_CharT>::iterator
2466 parse(basic_format_parse_context<_CharT>& __pc)
2467 {
2468 using enum __format::_ChronoParts;
2469 return _M_f._M_parse(__pc, _Weekday, __defSpec);
2470 }
2471
2472 template<typename _Out>
2473 typename basic_format_context<_Out, _CharT>::iterator
2474 format(const chrono::weekday_last& __t,
2475 basic_format_context<_Out, _CharT>& __fc) const
2476 {
2477 __format::_ChronoData<_CharT> __cd{};
2478 __cd._M_weekday = __t.weekday();
2479 return _M_f._M_format(__cd, __fc);
2480 }
2481
2482 private:
2483 static constexpr __format::_ChronoSpec<_CharT> __defSpec = []
2484 {
2485 using __format::_ChronoFormats;
2486 using enum __format::_ChronoParts;
2487
2488 __format::_ChronoSpec<_CharT> __res{};
2489 __res._M_debug = true;
2490 __res._M_localized = true;
2491 __res._M_locale_specific = true;
2492 __res._M_needed = _Weekday;
2493 __res._M_chrono_specs = _ChronoFormats<_CharT>::_S_wl();
2494 return __res;
2495 }();
2496
2497 __format::__formatter_chrono<_CharT> _M_f{__defSpec};
2498 };
2499
2500#if __glibcxx_print >= 202406L
2501 template<>
2502 inline constexpr bool
2503 enable_nonlocking_formatter_optimization<chrono::weekday_last> = true;
2504#endif
2505
2506 template<__format::__char _CharT>
2507 struct formatter<chrono::month_day, _CharT>
2508 {
2509 constexpr typename basic_format_parse_context<_CharT>::iterator
2510 parse(basic_format_parse_context<_CharT>& __pc)
2511 {
2512 using enum __format::_ChronoParts;
2513 return _M_f._M_parse(__pc, _Month|_Day|_WeekdayIndex, __defSpec);
2514 }
2515
2516 template<typename _Out>
2517 typename basic_format_context<_Out, _CharT>::iterator
2518 format(const chrono::month_day& __t,
2519 basic_format_context<_Out, _CharT>& __fc) const
2520 {
2521 __format::_ChronoData<_CharT> __cd{};
2522 __cd._M_month = __t.month();
2523 __cd._M_fill_day(__t.day(), __defSpec._M_needed);
2524 return _M_f._M_format(__cd, __fc);
2525 }
2526
2527 private:
2528 static constexpr __format::_ChronoSpec<_CharT> __defSpec = []
2529 {
2530 using __format::_ChronoFormats;
2531 using enum __format::_ChronoParts;
2532
2533 __format::_ChronoSpec<_CharT> __res{};
2534 __res._M_debug = true;
2535 __res._M_localized = true;
2536 __res._M_locale_specific = true;
2537 __res._M_needed = _Month|_Day;
2538 __res._M_chrono_specs = _ChronoFormats<_CharT>::_S_md();
2539 return __res;
2540 }();
2541
2542 __format::__formatter_chrono<_CharT> _M_f{__defSpec};
2543 };
2544
2545#if __glibcxx_print >= 202406L
2546 template<>
2547 inline constexpr bool
2548 enable_nonlocking_formatter_optimization<chrono::month_day> = true;
2549#endif
2550
2551 template<__format::__char _CharT>
2552 struct formatter<chrono::month_day_last, _CharT>
2553 {
2554 constexpr typename basic_format_parse_context<_CharT>::iterator
2555 parse(basic_format_parse_context<_CharT>& __pc)
2556 {
2557 using enum __format::_ChronoParts;
2558 return _M_f._M_parse(__pc, _Month, __defSpec);
2559 }
2560
2561 template<typename _Out>
2562 typename basic_format_context<_Out, _CharT>::iterator
2563 format(const chrono::month_day_last& __t,
2564 basic_format_context<_Out, _CharT>& __fc) const
2565 {
2566 __format::_ChronoData<_CharT> __cd{};
2567 __cd._M_month = __t.month();
2568 return _M_f._M_format(__cd, __fc);
2569 }
2570
2571 private:
2572 static constexpr __format::_ChronoSpec<_CharT> __defSpec = []
2573 {
2574 using __format::_ChronoFormats;
2575 using enum __format::_ChronoParts;
2576
2577 __format::_ChronoSpec<_CharT> __res{};
2578 __res._M_debug = true;
2579 __res._M_localized = true;
2580 __res._M_locale_specific = true;
2581 __res._M_needed = _Month;
2582 __res._M_chrono_specs = _ChronoFormats<_CharT>::_S_ml();
2583 return __res;
2584 }();
2585
2586 __format::__formatter_chrono<_CharT> _M_f{__defSpec};
2587 };
2588
2589#if __glibcxx_print >= 202406L
2590 template<>
2591 inline constexpr bool
2592 enable_nonlocking_formatter_optimization<chrono::month_day_last> = true;
2593#endif
2594
2595 template<__format::__char _CharT>
2596 struct formatter<chrono::month_weekday, _CharT>
2597 {
2598 constexpr typename basic_format_parse_context<_CharT>::iterator
2599 parse(basic_format_parse_context<_CharT>& __pc)
2600 {
2601 using enum __format::_ChronoParts;
2602 return _M_f._M_parse(__pc, _Month|_IndexedWeekday, __defSpec);
2603 }
2604
2605 template<typename _Out>
2606 typename basic_format_context<_Out, _CharT>::iterator
2607 format(const chrono::month_weekday& __t,
2608 basic_format_context<_Out, _CharT>& __fc) const
2609 {
2610 __format::_ChronoData<_CharT> __cd{};
2611 __cd._M_month = __t.month();
2612 __cd._M_fill_weekday(__t.weekday_indexed(), __defSpec._M_needed);
2613 return _M_f._M_format(__cd, __fc);
2614 }
2615
2616 private:
2617 static constexpr __format::_ChronoSpec<_CharT> __defSpec = []
2618 {
2619 using __format::_ChronoFormats;
2620 using enum __format::_ChronoParts;
2621
2622 __format::_ChronoSpec<_CharT> __res{};
2623 __res._M_debug = true;
2624 __res._M_localized = true;
2625 __res._M_locale_specific = true;
2626 __res._M_needed = _Month|_IndexedWeekday;
2627 __res._M_chrono_specs = _ChronoFormats<_CharT>::_S_mwi();
2628 return __res;
2629 }();
2630
2631 __format::__formatter_chrono<_CharT> _M_f{__defSpec};
2632 };
2633
2634#if __glibcxx_print >= 202406L
2635 template<>
2636 inline constexpr bool
2637 enable_nonlocking_formatter_optimization<chrono::month_weekday> = true;
2638#endif
2639
2640 template<__format::__char _CharT>
2641 struct formatter<chrono::month_weekday_last, _CharT>
2642 {
2643 constexpr typename basic_format_parse_context<_CharT>::iterator
2644 parse(basic_format_parse_context<_CharT>& __pc)
2645 {
2646 using enum __format::_ChronoParts;
2647 return _M_f._M_parse(__pc, _Month|_Weekday, __defSpec);
2648 }
2649
2650 template<typename _Out>
2651 typename basic_format_context<_Out, _CharT>::iterator
2652 format(const chrono::month_weekday_last& __t,
2653 basic_format_context<_Out, _CharT>& __fc) const
2654 {
2655 __format::_ChronoData<_CharT> __cd{};
2656 __cd._M_month = __t.month();
2657 __cd._M_weekday = __t.weekday_last().weekday();
2658 return _M_f._M_format(__cd, __fc);
2659 }
2660
2661 private:
2662 static constexpr __format::_ChronoSpec<_CharT> __defSpec = []
2663 {
2664 using __format::_ChronoFormats;
2665 using enum __format::_ChronoParts;
2666
2667 __format::_ChronoSpec<_CharT> __res{};
2668 __res._M_debug = true;
2669 __res._M_localized = true;
2670 __res._M_locale_specific = true;
2671 __res._M_needed = _Month|_Weekday;
2672 __res._M_chrono_specs = _ChronoFormats<_CharT>::_S_mwl();
2673 return __res;
2674 }();
2675
2676 __format::__formatter_chrono<_CharT> _M_f{__defSpec};
2677 };
2678
2679#if __glibcxx_print >= 202406L
2680 template<>
2681 inline constexpr bool
2682 enable_nonlocking_formatter_optimization<chrono::month_weekday_last> = true;
2683#endif
2684
2685 template<__format::__char _CharT>
2686 struct formatter<chrono::year_month, _CharT>
2687 {
2688 constexpr typename basic_format_parse_context<_CharT>::iterator
2689 parse(basic_format_parse_context<_CharT>& __pc)
2690 {
2691 using enum __format::_ChronoParts;
2692 return _M_f._M_parse(__pc, _Year|_Month, __defSpec);
2693 }
2694
2695 template<typename _Out>
2696 typename basic_format_context<_Out, _CharT>::iterator
2697 format(const chrono::year_month& __t,
2698 basic_format_context<_Out, _CharT>& __fc) const
2699 {
2700 __format::_ChronoData<_CharT> __cd{};
2701 __cd._M_fill_year_month(__t, __defSpec._M_needed);
2702 return _M_f._M_format(__cd, __fc);
2703 }
2704
2705 private:
2706 static constexpr __format::_ChronoSpec<_CharT> __defSpec = []
2707 {
2708 using __format::_ChronoFormats;
2709 using enum __format::_ChronoParts;
2710
2711 __format::_ChronoSpec<_CharT> __res{};
2712 __res._M_debug = true;
2713 __res._M_localized = true;
2714 __res._M_locale_specific = true;
2715 __res._M_needed = _Year|_Month;
2716 __res._M_chrono_specs = _ChronoFormats<_CharT>::_S_ym();
2717 return __res;
2718 }();
2719
2720 __format::__formatter_chrono<_CharT> _M_f{__defSpec};
2721 };
2722
2723#if __glibcxx_print >= 202406L
2724 template<>
2725 inline constexpr bool
2726 enable_nonlocking_formatter_optimization<chrono::year_month> = true;
2727#endif
2728
2729 template<__format::__char _CharT>
2730 struct formatter<chrono::year_month_day, _CharT>
2731 {
2732 constexpr typename basic_format_parse_context<_CharT>::iterator
2733 parse(basic_format_parse_context<_CharT>& __pc)
2734 {
2735 using enum __format::_ChronoParts;
2736 return _M_f._M_parse(__pc, _Date, __defSpec);
2737 }
2738
2739 template<typename _Out>
2740 typename basic_format_context<_Out, _CharT>::iterator
2741 format(const chrono::year_month_day& __t,
2742 basic_format_context<_Out, _CharT>& __fc) const
2743 {
2744 __format::_ChronoData<_CharT> __cd{};
2745 auto __parts = _M_f._M_spec._M_needed;
2746 __parts = __cd._M_fill_year_month(__t, __parts);
2747 __parts = __cd._M_fill_day(__t.day(), __parts);
2748 if (__parts == 0)
2749 return _M_f._M_format(__cd, __fc);
2750
2751 __cd._M_fill_ldays(chrono::local_days(__t), __parts);
2752 return _M_f._M_format(__cd, __fc);
2753 }
2754
2755 private:
2756 static constexpr __format::_ChronoSpec<_CharT> __defSpec = []
2757 {
2758 using __format::_ChronoFormats;
2759 using enum __format::_ChronoParts;
2760
2761 __format::_ChronoSpec<_CharT> __res{};
2762 __res._M_debug = true;
2763 __res._M_needed = _YearMonthDay;
2764 __res._M_chrono_specs = _ChronoFormats<_CharT>::_S_f();
2765 return __res;
2766 }();
2767
2768 __format::__formatter_chrono<_CharT> _M_f{__defSpec};
2769 };
2770
2771#if __glibcxx_print >= 202406L
2772 template<>
2773 inline constexpr bool
2774 enable_nonlocking_formatter_optimization<chrono::year_month_day> = true;
2775#endif
2776
2777 template<__format::__char _CharT>
2778 struct formatter<chrono::year_month_day_last, _CharT>
2779 {
2780 constexpr typename basic_format_parse_context<_CharT>::iterator
2781 parse(basic_format_parse_context<_CharT>& __pc)
2782 {
2783 using enum __format::_ChronoParts;
2784 return _M_f._M_parse(__pc, _Date, __defSpec);
2785 }
2786
2787 template<typename _Out>
2788 typename basic_format_context<_Out, _CharT>::iterator
2789 format(const chrono::year_month_day_last& __t,
2790 basic_format_context<_Out, _CharT>& __fc) const
2791 {
2792 using enum __format::_ChronoParts;
2793
2794 __format::_ChronoData<_CharT> __cd{};
2795 auto __parts = _M_f._M_spec._M_needed;
2796 __parts = __cd._M_fill_year_month(__t, __parts);
2797 if (_M_f._M_spec._M_needs(_Day|_WeekdayIndex))
2798 __parts = __cd._M_fill_day(__t.day(), __parts);
2799 if (__parts == 0)
2800 return _M_f._M_format(__cd, __fc);
2801
2802 __cd._M_fill_ldays(chrono::local_days(__t), __parts);
2803 return _M_f._M_format(__cd, __fc);
2804 }
2805
2806 private:
2807 static constexpr __format::_ChronoSpec<_CharT> __defSpec = []
2808 {
2809 using __format::_ChronoFormats;
2810 using enum __format::_ChronoParts;
2811
2812 __format::_ChronoSpec<_CharT> __res{};
2813 __res._M_debug = true;
2814 __res._M_localized = true;
2815 __res._M_locale_specific = true;
2816 __res._M_needed = _Year|_Month;
2817 __res._M_chrono_specs = _ChronoFormats<_CharT>::_S_yml();
2818 return __res;
2819 }();
2820
2821 __format::__formatter_chrono<_CharT> _M_f{__defSpec};
2822 };
2823
2824#if __glibcxx_print >= 202406L
2825 template<>
2826 inline constexpr bool
2827 enable_nonlocking_formatter_optimization<chrono::year_month_day_last> = true;
2828#endif
2829
2830 template<__format::__char _CharT>
2831 struct formatter<chrono::year_month_weekday, _CharT>
2832 {
2833 constexpr typename basic_format_parse_context<_CharT>::iterator
2834 parse(basic_format_parse_context<_CharT>& __pc)
2835 {
2836 using enum __format::_ChronoParts;
2837 return _M_f._M_parse(__pc, _Date, __defSpec);
2838 }
2839
2840 template<typename _Out>
2841 typename basic_format_context<_Out, _CharT>::iterator
2842 format(const chrono::year_month_weekday& __t,
2843 basic_format_context<_Out, _CharT>& __fc) const
2844 {
2845 __format::_ChronoData<_CharT> __cd{};
2846 auto __parts = _M_f._M_spec._M_needed;
2847 __parts = __cd._M_fill_year_month(__t, __parts);
2848 __parts = __cd._M_fill_weekday(__t.weekday_indexed(), __parts);
2849 if (__t.index() == 0) [[unlikely]]
2850 // n.b. day cannot be negative, so any 0th weekday uses
2851 // value-initialized (0) day of month
2852 __parts -= __format::_ChronoParts::_Day;
2853 if (__parts == 0)
2854 return _M_f._M_format(__cd, __fc);
2855
2856 chrono::local_days __ld(__t);
2857 __parts = __cd._M_fill_ldays(__ld, __parts);
2858 if (__parts == 0)
2859 return _M_f._M_format(__cd, __fc);
2860
2861 auto __dom = __ld - chrono::local_days(__t.year()/__t.month()/0);
2862 // n.b. weekday index is supplied by input, do not override it
2863 __cd._M_day = chrono::day(__dom.count());
2864 return _M_f._M_format(__cd, __fc);
2865 }
2866
2867 private:
2868 static constexpr __format::_ChronoSpec<_CharT> __defSpec = []
2869 {
2870 using __format::_ChronoFormats;
2871 using enum __format::_ChronoParts;
2872
2873 __format::_ChronoSpec<_CharT> __res{};
2874 __res._M_debug = true;
2875 __res._M_localized = true;
2876 __res._M_locale_specific = true;
2877 __res._M_needed = _Year|_Month|_IndexedWeekday;
2878 __res._M_chrono_specs = _ChronoFormats<_CharT>::_S_ymwi();
2879 return __res;
2880 }();
2881
2882 __format::__formatter_chrono<_CharT> _M_f{__defSpec};
2883 };
2884
2885#if __glibcxx_print >= 202406L
2886 template<>
2887 inline constexpr bool
2888 enable_nonlocking_formatter_optimization<chrono::year_month_weekday> = true;
2889#endif
2890
2891 template<__format::__char _CharT>
2892 struct formatter<chrono::year_month_weekday_last, _CharT>
2893 {
2894 constexpr typename basic_format_parse_context<_CharT>::iterator
2895 parse(basic_format_parse_context<_CharT>& __pc)
2896 {
2897 using enum __format::_ChronoParts;
2898 return _M_f._M_parse(__pc, _Date, __defSpec);
2899 }
2900
2901 template<typename _Out>
2902 typename basic_format_context<_Out, _CharT>::iterator
2903 format(const chrono::year_month_weekday_last& __t,
2904 basic_format_context<_Out, _CharT>& __fc) const
2905 {
2906 __format::_ChronoData<_CharT> __cd{};
2907 auto __parts = _M_f._M_spec._M_needed;
2908 __parts = __cd._M_fill_year_month(__t, __parts);
2909 __cd._M_weekday = __t.weekday_last().weekday();
2910 __parts -= __format::_ChronoParts::_Weekday;
2911 if (__parts == 0)
2912 return _M_f._M_format(__cd, __fc);
2913
2914 chrono::local_days __ld(__t);
2915 __parts = __cd._M_fill_ldays(__ld, __parts);
2916 if (__parts == 0)
2917 return _M_f._M_format(__cd, __fc);
2918
2919 auto __dom = __ld - chrono::local_days(__t.year()/__t.month()/0);
2920 __cd._M_fill_day(chrono::day(__dom.count()), __parts);
2921 return _M_f._M_format(__cd, __fc);
2922 }
2923
2924 private:
2925 static constexpr __format::_ChronoSpec<_CharT> __defSpec = []
2926 {
2927 using __format::_ChronoFormats;
2928 using enum __format::_ChronoParts;
2929
2930 __format::_ChronoSpec<_CharT> __res{};
2931 __res._M_debug = true;
2932 __res._M_localized = true;
2933 __res._M_locale_specific = true;
2934 __res._M_needed = _Year|_Month|_Weekday;
2935 __res._M_chrono_specs = _ChronoFormats<_CharT>::_S_ymwl();
2936 return __res;
2937 }();
2938
2939 __format::__formatter_chrono<_CharT> _M_f{__defSpec};
2940 };
2941
2942#if __glibcxx_print >= 202406L
2943 template<>
2944 inline constexpr bool
2945 enable_nonlocking_formatter_optimization<chrono::year_month_weekday_last> = true;
2946#endif
2947
2948 template<typename _Rep, typename _Period, __format::__char _CharT>
2949 struct formatter<chrono::hh_mm_ss<chrono::duration<_Rep, _Period>>, _CharT>
2950 {
2951 constexpr typename basic_format_parse_context<_CharT>::iterator
2952 parse(basic_format_parse_context<_CharT>& __pc)
2953 {
2954 using enum __format::_ChronoParts;
2955 return _M_f.template _M_parse<_Precision>(__pc, _Time, __defSpec);
2956 }
2957
2958 template<typename _Out>
2959 typename basic_format_context<_Out, _CharT>::iterator
2960 format(const chrono::hh_mm_ss<chrono::duration<_Rep, _Period>>& __t,
2961 basic_format_context<_Out, _CharT>& __fc) const
2962 {
2963 using enum __format::_ChronoParts;
2964
2965 __format::_ChronoData<_CharT> __cd;
2966 __cd._M_is_neg = __t.is_negative();
2967 __cd._M_hours = __t.hours();
2968 __cd._M_minutes = __t.minutes();
2969 __cd._M_seconds = __t.seconds();
2970
2971 _Precision __d(0);
2972 // n.b. computing total duration or total seconds may overflow,
2973 // do not compute them if not requested.
2974 if (_M_f._M_spec._M_needs(_EpochUnits))
2975 __d = __t.to_duration();
2976 if (_M_f._M_spec._M_needs(_TotalSeconds))
2977 __cd._M_eseconds
2978 = __cd._M_hours + __cd._M_minutes + __cd._M_seconds;
2979 return _M_f._M_format_units(__cd, __d, __t.subseconds(), __fc);
2980 }
2981
2982 private:
2983 using _Precision
2984 = typename chrono::hh_mm_ss<chrono::duration<_Rep, _Period>>::precision;
2985 static constexpr __format::_ChronoSpec<_CharT> __defSpec =
2986 __format::__formatter_duration<_CharT>::
2987 template _S_spec_for<_Precision>(__format::_ChronoParts::_Time);
2988
2989 __format::__formatter_duration<_CharT> _M_f{__defSpec};
2990 };
2991
2992#if __glibcxx_print >= 202406L
2993 // _GLIBCXX_RESOLVE_LIB_DEFECTS
2994 // 4400. enable_nonlocking_formatter_optimization for durations with custom rep
2995 template<typename _Duration>
2996 constexpr bool
2997 enable_nonlocking_formatter_optimization<chrono::hh_mm_ss<_Duration>>
2998 = enable_nonlocking_formatter_optimization<_Duration>;
2999#endif
3000
3001#if _GLIBCXX_USE_CXX11_ABI || ! _GLIBCXX_USE_DUAL_ABI
3002 template<__format::__char _CharT>
3003 struct formatter<chrono::sys_info, _CharT>
3004 {
3005 constexpr typename basic_format_parse_context<_CharT>::iterator
3006 parse(basic_format_parse_context<_CharT>& __pc)
3007 { return _M_f.parse(__pc); }
3008
3009 template<typename _Out>
3010 typename basic_format_context<_Out, _CharT>::iterator
3011 format(const chrono::sys_info& __i,
3012 basic_format_context<_Out, _CharT>& __fc) const
3013 { return _M_f.format(__i, __fc); }
3014
3015 private:
3016 __format::__formatter_chrono_info<_CharT> _M_f;
3017 };
3018
3019#if __glibcxx_print >= 202406L
3020 template<>
3021 inline constexpr bool
3022 enable_nonlocking_formatter_optimization<chrono::sys_info> = true;
3023#endif
3024
3025 template<__format::__char _CharT>
3026 struct formatter<chrono::local_info, _CharT>
3027 {
3028 constexpr typename basic_format_parse_context<_CharT>::iterator
3029 parse(basic_format_parse_context<_CharT>& __pc)
3030 { return _M_f.parse(__pc); }
3031
3032 template<typename _Out>
3033 typename basic_format_context<_Out, _CharT>::iterator
3034 format(const chrono::local_info& __i,
3035 basic_format_context<_Out, _CharT>& __fc) const
3036 { return _M_f.format(__i, __fc); }
3037
3038 private:
3039 __format::__formatter_chrono_info<_CharT> _M_f;
3040 };
3041
3042#if __glibcxx_print >= 202406L
3043 template<>
3044 inline constexpr bool
3045 enable_nonlocking_formatter_optimization<chrono::local_info> = true;
3046#endif
3047#endif
3048
3049 template<typename _Duration, __format::__char _CharT>
3050 struct formatter<chrono::sys_time<_Duration>, _CharT>
3051 {
3052 constexpr typename basic_format_parse_context<_CharT>::iterator
3053 parse(basic_format_parse_context<_CharT>& __pc)
3054 {
3055 using enum __format::_ChronoParts;
3056 auto __res
3057 = _M_f.template _M_parse<_Duration>(__pc, _ZonedDateTime, __defSpec);
3058 if constexpr (__defSpec._M_chrono_specs.empty())
3059 if (_M_f._M_spec._M_chrono_specs.empty())
3060 __format::__invalid_chrono_spec(); // chrono-specs can't be empty
3061 return __res;
3062 }
3063
3064 template<typename _Out>
3065 typename basic_format_context<_Out, _CharT>::iterator
3066 format(const chrono::sys_time<_Duration>& __t,
3067 basic_format_context<_Out, _CharT>& __fc) const
3068 {
3069 __format::_ChronoData<_CharT> __cd{};
3070 __cd._M_fill_utc_zone();
3071
3072 _Duration __ed = __t.time_since_epoch();
3073 __cd._M_eseconds = chrono::floor<chrono::seconds>(__ed);
3074 __cd._M_lseconds = chrono::local_seconds(__cd._M_eseconds);
3075 return _M_f._M_format_time_point(__cd, __ed, __fc);
3076 }
3077
3078 private:
3079 static constexpr __format::_ChronoSpec<_CharT> __defSpec =
3080 __format::__formatter_duration<_CharT>::template _S_spec_for_tp<_Duration>();
3081
3082 __format::__formatter_duration<_CharT> _M_f{__defSpec};
3083 };
3084
3085#if __glibcxx_print >= 202406L
3086 // _GLIBCXX_RESOLVE_LIB_DEFECTS
3087 // 4400. enable_nonlocking_formatter_optimization for durations with custom rep
3088 template<typename _Duration>
3089 constexpr bool
3090 enable_nonlocking_formatter_optimization<chrono::sys_time<_Duration>>
3091 = enable_nonlocking_formatter_optimization<_Duration>;
3092#endif
3093
3094 template<typename _Duration, __format::__char _CharT>
3095 struct formatter<chrono::utc_time<_Duration>, _CharT>
3096 {
3097 constexpr typename basic_format_parse_context<_CharT>::iterator
3098 parse(basic_format_parse_context<_CharT>& __pc)
3099 {
3100 using enum __format::_ChronoParts;
3101 return _M_f.template _M_parse<_Duration>(__pc, _ZonedDateTime, __defSpec);
3102 }
3103
3104 template<typename _Out>
3105 typename basic_format_context<_Out, _CharT>::iterator
3106 format(const chrono::utc_time<_Duration>& __t,
3107 basic_format_context<_Out, _CharT>& __fc) const
3108 {
3109 using __format::_ChronoParts;
3110 using namespace chrono;
3111 __format::_ChronoData<_CharT> __cd{};
3112 __cd._M_fill_utc_zone();
3113
3114 _Duration __ed = __t.time_since_epoch();
3115 __cd._M_eseconds = chrono::floor<seconds>(__ed);
3116 // Adjust by removing leap seconds to get equivalent sys_time.
3117 // We can't just use clock_cast because we want to know if the time
3118 // falls within a leap second insertion, and format seconds as "60".
3119 const auto __li = chrono::get_leap_second_info(__t);
3120 __cd._M_lseconds = local_seconds(__cd._M_eseconds - __li.elapsed);
3121 auto __parts = _M_f._M_spec._M_needed - _ChronoParts::_TotalSeconds;
3122 if ((__parts & _ChronoParts::_DateTime) != 0)
3123 {
3124 __cd._M_fill_date_time(__cd._M_lseconds, __parts);
3125 __cd._M_seconds += seconds(__li.is_leap_second);
3126 }
3127 return _M_f._M_format_units(__cd, __ed, __ed - __cd._M_eseconds, __fc);
3128 }
3129
3130 private:
3131 static constexpr __format::_ChronoSpec<_CharT> __defSpec =
3132 __format::__formatter_duration<_CharT>::
3133 template _S_spec_for<_Duration>(__format::_ChronoParts::_DateTime);
3134
3135 __format::__formatter_duration<_CharT> _M_f{__defSpec};
3136 };
3137
3138#if __glibcxx_print >= 202406L
3139 // _GLIBCXX_RESOLVE_LIB_DEFECTS
3140 // 4400. enable_nonlocking_formatter_optimization for durations with custom rep
3141 template<typename _Duration>
3142 constexpr bool
3143 enable_nonlocking_formatter_optimization<chrono::utc_time<_Duration>>
3144 = enable_nonlocking_formatter_optimization<_Duration>;
3145#endif
3146
3147 template<typename _Duration, __format::__char _CharT>
3148 struct formatter<chrono::tai_time<_Duration>, _CharT>
3149 {
3150 constexpr typename basic_format_parse_context<_CharT>::iterator
3151 parse(basic_format_parse_context<_CharT>& __pc)
3152 {
3153 using enum __format::_ChronoParts;
3154 return _M_f.template _M_parse<_Duration>(__pc, _ZonedDateTime, __defSpec);
3155 }
3156
3157 template<typename _Out>
3158 typename basic_format_context<_Out, _CharT>::iterator
3159 format(const chrono::tai_time<_Duration>& __t,
3160 basic_format_context<_Out, _CharT>& __fc) const
3161 {
3162 using namespace chrono;
3163 __format::_ChronoData<_CharT> __cd{};
3164 __cd._M_fill_zone("TAI", L"TAI");
3165
3166 _Duration __ed = __t.time_since_epoch();
3167 __cd._M_eseconds = chrono::floor<seconds>(__ed);
3168 // Offset is 1970y/January/1 - 1958y/January/1
3169 constexpr chrono::days __tai_offset = chrono::days(4383);
3170 __cd._M_lseconds = local_seconds(__cd._M_eseconds - __tai_offset);
3171 return _M_f._M_format_time_point(__cd, __ed, __fc);
3172 }
3173
3174 private:
3175 static constexpr __format::_ChronoSpec<_CharT> __defSpec =
3176 __format::__formatter_duration<_CharT>::
3177 template _S_spec_for<_Duration>(__format::_ChronoParts::_DateTime);
3178
3179 __format::__formatter_duration<_CharT> _M_f{__defSpec};
3180 };
3181
3182#if __glibcxx_print >= 202406L
3183 // _GLIBCXX_RESOLVE_LIB_DEFECTS
3184 // 4400. enable_nonlocking_formatter_optimization for durations with custom rep
3185 template<typename _Duration>
3186 constexpr bool
3187 enable_nonlocking_formatter_optimization<chrono::tai_time<_Duration>>
3188 = enable_nonlocking_formatter_optimization<_Duration>;
3189#endif
3190
3191 template<typename _Duration, __format::__char _CharT>
3192 struct formatter<chrono::gps_time<_Duration>, _CharT>
3193 {
3194 constexpr typename basic_format_parse_context<_CharT>::iterator
3195 parse(basic_format_parse_context<_CharT>& __pc)
3196 {
3197 using enum __format::_ChronoParts;
3198 return _M_f.template _M_parse<_Duration>(__pc, _ZonedDateTime, __defSpec);
3199 }
3200
3201 template<typename _Out>
3202 typename basic_format_context<_Out, _CharT>::iterator
3203 format(const chrono::gps_time<_Duration>& __t,
3204 basic_format_context<_Out, _CharT>& __fc) const
3205 {
3206 using namespace chrono;
3207 __format::_ChronoData<_CharT> __cd{};
3208 __cd._M_fill_zone("GPS", L"GPS");
3209
3210 _Duration __ed = __t.time_since_epoch();
3211 __cd._M_eseconds = chrono::floor<seconds>(__ed);
3212 // Offset is 1980y/January/Sunday[1] - 1970y/January/1
3213 constexpr chrono::days __gps_offset = chrono::days(3657);
3214 __cd._M_lseconds = local_seconds(__cd._M_eseconds + __gps_offset);
3215 return _M_f._M_format_time_point(__cd, __ed, __fc);
3216 }
3217
3218 private:
3219 static constexpr __format::_ChronoSpec<_CharT> __defSpec =
3220 __format::__formatter_duration<_CharT>::
3221 template _S_spec_for<_Duration>(__format::_ChronoParts::_DateTime);
3222
3223 __format::__formatter_duration<_CharT> _M_f{__defSpec};
3224 };
3225
3226#if __glibcxx_print >= 202406L
3227 // _GLIBCXX_RESOLVE_LIB_DEFECTS
3228 // 4400. enable_nonlocking_formatter_optimization for durations with custom rep
3229 template<typename _Duration>
3230 constexpr bool
3231 enable_nonlocking_formatter_optimization<chrono::gps_time<_Duration>>
3232 = enable_nonlocking_formatter_optimization<_Duration>;
3233#endif
3234
3235 template<typename _Duration, __format::__char _CharT>
3236 struct formatter<chrono::file_time<_Duration>, _CharT>
3237 {
3238 constexpr typename basic_format_parse_context<_CharT>::iterator
3239 parse(basic_format_parse_context<_CharT>& __pc)
3240 {
3241 using enum __format::_ChronoParts;
3242 return _M_f.template _M_parse<_Duration>(__pc, _ZonedDateTime, __defSpec);
3243 }
3244
3245 template<typename _Out>
3246 typename basic_format_context<_Out, _CharT>::iterator
3247 format(const chrono::file_time<_Duration>& __t,
3248 basic_format_context<_Out, _CharT>& __fc) const
3249 {
3250 using namespace chrono;
3251 __format::_ChronoData<_CharT> __cd{};
3252 __cd._M_fill_utc_zone();
3253
3254 _Duration __ed = __t.time_since_epoch();
3255 __cd._M_eseconds = chrono::floor<seconds>(__ed);
3256 auto __st = chrono::clock_cast<system_clock>(__t);
3257 __cd._M_lseconds
3258 = local_seconds(chrono::floor<seconds>(__st.time_since_epoch()));
3259 return _M_f._M_format_time_point(__cd, __ed, __fc);
3260 }
3261
3262 private:
3263 static constexpr __format::_ChronoSpec<_CharT> __defSpec =
3264 __format::__formatter_duration<_CharT>::
3265 template _S_spec_for<_Duration>(__format::_ChronoParts::_DateTime);
3266
3267 __format::__formatter_duration<_CharT> _M_f{__defSpec};
3268 };
3269
3270#if __glibcxx_print >= 202406L
3271 // _GLIBCXX_RESOLVE_LIB_DEFECTS
3272 // 4400. enable_nonlocking_formatter_optimization for durations with custom rep
3273 template<typename _Duration>
3274 constexpr bool
3275 enable_nonlocking_formatter_optimization<chrono::file_time<_Duration>>
3276 = enable_nonlocking_formatter_optimization<_Duration>;
3277#endif
3278
3279 template<typename _Duration, __format::__char _CharT>
3280 struct formatter<chrono::local_time<_Duration>, _CharT>
3281 {
3282 constexpr typename basic_format_parse_context<_CharT>::iterator
3283 parse(basic_format_parse_context<_CharT>& __pc)
3284 {
3285 using enum __format::_ChronoParts;
3286 auto __res
3287 = _M_f.template _M_parse<_Duration>(__pc, _DateTime, __defSpec);
3288 if constexpr (__defSpec._M_chrono_specs.empty())
3289 if (_M_f._M_spec._M_chrono_specs.empty())
3290 __format::__invalid_chrono_spec(); // chrono-specs can't be empty
3291 return __res;
3292 }
3293
3294 template<typename _Out>
3295 typename basic_format_context<_Out, _CharT>::iterator
3296 format(const chrono::local_time<_Duration>& __lt,
3297 basic_format_context<_Out, _CharT>& __fc) const
3298 {
3299 __format::_ChronoData<_CharT> __cd{};
3300 _Duration __ed = __lt.time_since_epoch();
3301 __cd._M_lseconds = chrono::floor<chrono::seconds>(__lt);
3302 __cd._M_eseconds = __cd._M_lseconds.time_since_epoch();
3303 return _M_f._M_format_time_point(__cd, __ed, __fc);
3304 }
3305
3306 private:
3307 static constexpr __format::_ChronoSpec<_CharT> __defSpec =
3308 __format::__formatter_duration<_CharT>::template _S_spec_for_tp<_Duration>();
3309
3310 __format::__formatter_duration<_CharT> _M_f{__defSpec};
3311 };
3312
3313#if __glibcxx_print >= 202406L
3314 // _GLIBCXX_RESOLVE_LIB_DEFECTS
3315 // 4400. enable_nonlocking_formatter_optimization for durations with custom rep
3316 template<typename _Duration>
3317 constexpr bool
3318 enable_nonlocking_formatter_optimization<chrono::local_time<_Duration>>
3319 = enable_nonlocking_formatter_optimization<_Duration>;
3320#endif
3321
3322 template<typename _Duration, __format::__char _CharT>
3323 struct formatter<chrono::__detail::__local_time_fmt<_Duration>, _CharT>
3324 {
3325 constexpr typename basic_format_parse_context<_CharT>::iterator
3326 parse(basic_format_parse_context<_CharT>& __pc)
3327 {
3328 using enum __format::_ChronoParts;
3329 return _M_f.template _M_parse<_Duration>(__pc, _ZonedDateTime, __defSpec);
3330 }
3331
3332 template<typename _Out>
3333 typename basic_format_context<_Out, _CharT>::iterator
3334 format(const chrono::__detail::__local_time_fmt<_Duration>& __zt,
3335 basic_format_context<_Out, _CharT>& __fc) const
3336 {
3337 using enum __format::_ChronoParts;
3338 __format::_ChronoData<_CharT> __cd{};
3339
3340 if (_M_f._M_spec._M_needs(_ZoneOffset))
3341 {
3342 if (!__zt._M_offset_sec)
3343 std::__throw_format_error("format error: no timezone available for %z");
3344 __cd._M_zone_offset = *__zt._M_offset_sec;
3345 }
3346
3347 basic_string<_CharT> __zone_store;
3348 if (_M_f._M_spec._M_needs(_ZoneAbbrev))
3349 {
3350 if (!__zt._M_abbrev)
3351 std::__throw_format_error("format error: no timezone available for %Z");
3352
3353 __cd._M_zone_cstr = __zt._M_abbrev->data();
3354 if constexpr (is_same_v<_CharT, char>)
3355 __cd._M_zone_abbrev = *__zt._M_abbrev;
3356 else
3357 {
3358 // TODO: use resize_for_override
3359 __zone_store.resize(__zt._M_abbrev->size());
3360 auto& __ct = use_facet<ctype<_CharT>>(_M_f._M_locale(__fc));
3361 __ct.widen(__zt._M_abbrev->data(),
3362 __zt._M_abbrev->data() + __zt._M_abbrev->size(),
3363 __zone_store.data());
3364 __cd._M_zone_abbrev = __zone_store;
3365 }
3366 }
3367
3368 _Duration __ed = __zt._M_time.time_since_epoch();
3369 __cd._M_lseconds = chrono::floor<chrono::seconds>(__zt._M_time);
3370 __cd._M_eseconds = __cd._M_lseconds.time_since_epoch();
3371 return _M_f._M_format_time_point(__cd, __ed, __fc);
3372 }
3373
3374 private:
3375 static constexpr __format::_ChronoSpec<_CharT> __defSpec =
3376 __format::__formatter_duration<_CharT>::
3377 template _S_spec_for<_Duration>(__format::_ChronoParts::_ZonedDateTime);
3378
3379 __format::__formatter_duration<_CharT> _M_f{__defSpec};
3380 };
3381
3382#if __glibcxx_print >= 202406L
3383 // _GLIBCXX_RESOLVE_LIB_DEFECTS
3384 // 4400. enable_nonlocking_formatter_optimization for durations with custom rep
3385 template<typename _Duration>
3386 constexpr bool
3387 enable_nonlocking_formatter_optimization<
3388 chrono::__detail::__local_time_fmt<_Duration>>
3389 = enable_nonlocking_formatter_optimization<_Duration>;
3390#endif
3391
3392#if _GLIBCXX_USE_CXX11_ABI || ! _GLIBCXX_USE_DUAL_ABI
3393 template<typename _Duration, typename _TimeZonePtr, __format::__char _CharT>
3394 struct formatter<chrono::zoned_time<_Duration, _TimeZonePtr>, _CharT>
3395 : formatter<chrono::__detail::__local_time_fmt_for<_Duration>, _CharT>
3396 {
3397 template<typename _Out>
3398 typename basic_format_context<_Out, _CharT>::iterator
3399 format(const chrono::zoned_time<_Duration, _TimeZonePtr>& __tp,
3400 basic_format_context<_Out, _CharT>& __fc) const
3401 {
3402 using _Ltf = chrono::__detail::__local_time_fmt_for<_Duration>;
3403 using _Base = formatter<_Ltf, _CharT>;
3404 const chrono::sys_info __info = __tp.get_info();
3405 const auto __lf = chrono::local_time_format(__tp.get_local_time(),
3406 &__info.abbrev,
3407 &__info.offset);
3408 return _Base::format(__lf, __fc);
3409 }
3410 };
3411
3412#if __glibcxx_print >= 202406L
3413 // _GLIBCXX_RESOLVE_LIB_DEFECTS
3414 // 4400. enable_nonlocking_formatter_optimization for durations with custom rep
3415 template<typename _Duration>
3416 constexpr bool
3417 enable_nonlocking_formatter_optimization<
3418 chrono::zoned_time<_Duration, const chrono::time_zone*>>
3419 = enable_nonlocking_formatter_optimization<_Duration>;
3420#endif
3421#endif
3422
3423namespace chrono
3424{
3425/// @addtogroup chrono
3426/// @{
3427
3428/// @cond undocumented
3429namespace __detail
3430{
3431 template<typename _Duration = seconds>
3432 struct _Parser
3433 {
3434 static_assert(is_same_v<common_type_t<_Duration, seconds>, _Duration>);
3435
3436 explicit
3437 _Parser(__format::_ChronoParts __need) : _M_need(__need) { }
3438
3439 _Parser(_Parser&&) = delete;
3440 void operator=(_Parser&&) = delete;
3441
3442 _Duration _M_time{}; // since midnight
3443 sys_days _M_sys_days{};
3444 year_month_day _M_ymd{};
3445 weekday _M_wd{};
3446 __format::_ChronoParts _M_need;
3447 unsigned _M_is_leap_second : 1 {};
3448 unsigned _M_reserved : 15 {};
3449
3450 template<typename _CharT, typename _Traits, typename _Alloc>
3451 basic_istream<_CharT, _Traits>&
3452 operator()(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt,
3453 basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr,
3454 minutes* __offset = nullptr);
3455
3456 private:
3457 // Read an unsigned integer from the stream and return it.
3458 // Extract no more than __n digits. Set failbit if an integer isn't read.
3459 template<typename _CharT, typename _Traits>
3460 static int_least32_t
3461 _S_read_unsigned(basic_istream<_CharT, _Traits>& __is,
3462 ios_base::iostate& __err, int __n)
3463 {
3464 int_least32_t __val = _S_try_read_digit(__is, __err);
3465 if (__val == -1) [[unlikely]]
3466 __err |= ios_base::failbit;
3467 else
3468 {
3469 int __n1 = (std::min)(__n, 9);
3470 // Cannot overflow __val unless we read more than 9 digits
3471 for (int __i = 1; __i < __n1; ++__i)
3472 if (auto __dig = _S_try_read_digit(__is, __err); __dig != -1)
3473 {
3474 __val *= 10;
3475 __val += __dig;
3476 }
3477
3478 while (__n1++ < __n) [[unlikely]]
3479 if (auto __dig = _S_try_read_digit(__is, __err); __dig != -1)
3480 {
3481 if (__builtin_mul_overflow(__val, 10, &__val)
3482 || __builtin_add_overflow(__val, __dig, &__val))
3483 {
3484 __err |= ios_base::failbit;
3485 return -1;
3486 }
3487 }
3488 }
3489 return __val;
3490 }
3491
3492 // Read an unsigned integer from the stream and return it.
3493 // Extract no more than __n digits. Set failbit if an integer isn't read.
3494 template<typename _CharT, typename _Traits>
3495 static int_least32_t
3496 _S_read_signed(basic_istream<_CharT, _Traits>& __is,
3497 ios_base::iostate& __err, int __n)
3498 {
3499 auto __sign = __is.peek();
3500 if (__sign == '-' || __sign == '+')
3501 (void) __is.get();
3502 int_least32_t __val = _S_read_unsigned(__is, __err, __n);
3503 if (__err & ios_base::failbit)
3504 {
3505 if (__sign == '-') [[unlikely]]
3506 __val *= -1;
3507 }
3508 return __val;
3509 }
3510
3511 // Read a digit from the stream and return it, or return -1.
3512 // If no digit is read eofbit will be set (but not failbit).
3513 template<typename _CharT, typename _Traits>
3514 static int_least32_t
3515 _S_try_read_digit(basic_istream<_CharT, _Traits>& __is,
3516 ios_base::iostate& __err)
3517 {
3518 int_least32_t __val = -1;
3519 auto __i = __is.peek();
3520 if (!_Traits::eq_int_type(__i, _Traits::eof())) [[likely]]
3521 {
3522 _CharT __c = _Traits::to_char_type(__i);
3523 if (_CharT('0') <= __c && __c <= _CharT('9')) [[likely]]
3524 {
3525 (void) __is.get();
3526 __val = __c - _CharT('0');
3527 }
3528 }
3529 else
3530 __err |= ios_base::eofbit;
3531 return __val;
3532 }
3533
3534 // Read the specified character and return true.
3535 // If the character is not found, set failbit and return false.
3536 template<typename _CharT, typename _Traits>
3537 static bool
3538 _S_read_chr(basic_istream<_CharT, _Traits>& __is,
3539 ios_base::iostate& __err, _CharT __c)
3540 {
3541 auto __i = __is.peek();
3542 if (_Traits::eq_int_type(__i, _Traits::eof()))
3543 __err |= ios_base::eofbit;
3544 else if (_Traits::to_char_type(__i) == __c) [[likely]]
3545 {
3546 (void) __is.get();
3547 return true;
3548 }
3549 __err |= ios_base::failbit;
3550 return false;
3551 }
3552 };
3553
3554 template<typename _Duration>
3555 using _Parser_t = _Parser<common_type_t<_Duration, seconds>>;
3556
3557 template<typename _Duration>
3558 consteval bool
3559 __use_floor()
3560 {
3561 if constexpr (_Duration::period::den == 1)
3562 {
3563 switch (_Duration::period::num)
3564 {
3565 case minutes::period::num:
3566 case hours::period::num:
3567 case days::period::num:
3568 case weeks::period::num:
3569 case years::period::num:
3570 return true;
3571 }
3572 }
3573 return false;
3574 }
3575
3576 // A "do the right thing" rounding function for duration and time_point
3577 // values extracted by from_stream. When treat_as_floating_point is true
3578 // we don't want to do anything, just a straightforward conversion.
3579 // When the destination type has a period of minutes, hours, days, weeks,
3580 // or years, we use chrono::floor to truncate towards negative infinity.
3581 // This ensures that an extracted timestamp such as 2024-09-05 13:00:00
3582 // will produce 2024-09-05 when rounded to days, rather than rounding up
3583 // to 2024-09-06 (a different day).
3584 // Otherwise, use chrono::round to get the nearest value representable
3585 // in the destination type.
3586 template<typename _ToDur, typename _Tp>
3587 constexpr auto
3588 __round(const _Tp& __t)
3589 {
3590 if constexpr (__is_duration_v<_Tp>)
3591 {
3592 if constexpr (treat_as_floating_point_v<typename _Tp::rep>)
3594 else if constexpr (__detail::__use_floor<_ToDur>())
3595 return chrono::floor<_ToDur>(__t);
3596 else
3597 return chrono::round<_ToDur>(__t);
3598 }
3599 else
3600 {
3601 static_assert(__is_time_point_v<_Tp>);
3602 using _Tpt = time_point<typename _Tp::clock, _ToDur>;
3603 return _Tpt(__detail::__round<_ToDur>(__t.time_since_epoch()));
3604 }
3605 }
3606
3607} // namespace __detail
3608/// @endcond
3609
3610 template<typename _CharT, typename _Traits, typename _Rep, typename _Period,
3611 typename _Alloc = allocator<_CharT>>
3612 inline basic_istream<_CharT, _Traits>&
3613 from_stream(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt,
3615 basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr,
3616 minutes* __offset = nullptr)
3617 {
3618 auto __need = __format::_ChronoParts::_TimeOfDay;
3619 __detail::_Parser_t<duration<_Rep, _Period>> __p(__need);
3620 if (__p(__is, __fmt, __abbrev, __offset))
3621 __d = __detail::__round<duration<_Rep, _Period>>(__p._M_time);
3622 return __is;
3623 }
3624
3625 template<typename _CharT, typename _Traits>
3626 inline basic_ostream<_CharT, _Traits>&
3627 operator<<(basic_ostream<_CharT, _Traits>& __os, const day& __d)
3628 {
3629 using _Ctx = __format::__format_context<_CharT>;
3630 using _Str = basic_string_view<_CharT>;
3631 _Str __s = _GLIBCXX_WIDEN("{:02d} is not a valid day");
3632 if (__d.ok())
3633 __s = __s.substr(0, 6);
3634 auto __u = (unsigned)__d;
3635 __os << std::vformat(__s, make_format_args<_Ctx>(__u));
3636 return __os;
3637 }
3638
3639 template<typename _CharT, typename _Traits,
3640 typename _Alloc = allocator<_CharT>>
3641 inline basic_istream<_CharT, _Traits>&
3642 from_stream(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt,
3643 day& __d,
3644 basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr,
3645 minutes* __offset = nullptr)
3646 {
3647 __detail::_Parser<> __p(__format::_ChronoParts::_Day);
3648 if (__p(__is, __fmt, __abbrev, __offset))
3649 __d = __p._M_ymd.day();
3650 return __is;
3651 }
3652
3653 template<typename _CharT, typename _Traits>
3654 inline basic_ostream<_CharT, _Traits>&
3655 operator<<(basic_ostream<_CharT, _Traits>& __os, const month& __m)
3656 {
3657 using _Ctx = __format::__format_context<_CharT>;
3658 using _Str = basic_string_view<_CharT>;
3659 _Str __s = _GLIBCXX_WIDEN("{:L%b}{} is not a valid month");
3660 if (__m.ok())
3661 __os << std::vformat(__os.getloc(), __s.substr(0, 6),
3662 make_format_args<_Ctx>(__m));
3663 else
3664 {
3665 auto __u = (unsigned)__m;
3666 __os << std::vformat(__s.substr(6), make_format_args<_Ctx>(__u));
3667 }
3668 return __os;
3669 }
3670
3671 template<typename _CharT, typename _Traits,
3672 typename _Alloc = allocator<_CharT>>
3673 inline basic_istream<_CharT, _Traits>&
3674 from_stream(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt,
3675 month& __m,
3676 basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr,
3677 minutes* __offset = nullptr)
3678 {
3679 __detail::_Parser<> __p(__format::_ChronoParts::_Month);
3680 if (__p(__is, __fmt, __abbrev, __offset))
3681 __m = __p._M_ymd.month();
3682 return __is;
3683 }
3684
3685 template<typename _CharT, typename _Traits>
3686 inline basic_ostream<_CharT, _Traits>&
3687 operator<<(basic_ostream<_CharT, _Traits>& __os, const year& __y)
3688 {
3689 using _Ctx = __format::__format_context<_CharT>;
3690 using _Str = basic_string_view<_CharT>;
3691 _Str __s = _GLIBCXX_WIDEN("-{:04d} is not a valid year");
3692 if (__y.ok())
3693 __s = __s.substr(0, 7);
3694 int __i = (int)__y;
3695 if (__i >= 0) [[likely]]
3696 __s.remove_prefix(1);
3697 else
3698 __i = -__i;
3699 __os << std::vformat(__s, make_format_args<_Ctx>(__i));
3700 return __os;
3701 }
3702
3703 template<typename _CharT, typename _Traits,
3704 typename _Alloc = allocator<_CharT>>
3705 inline basic_istream<_CharT, _Traits>&
3706 from_stream(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt,
3707 year& __y,
3708 basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr,
3709 minutes* __offset = nullptr)
3710 {
3711 __detail::_Parser<> __p(__format::_ChronoParts::_Year);
3712 if (__p(__is, __fmt, __abbrev, __offset))
3713 __y = __p._M_ymd.year();
3714 return __is;
3715 }
3716
3717 template<typename _CharT, typename _Traits>
3718 inline basic_ostream<_CharT, _Traits>&
3719 operator<<(basic_ostream<_CharT, _Traits>& __os, const weekday& __wd)
3720 {
3721 using _Ctx = __format::__format_context<_CharT>;
3722 using _Str = basic_string_view<_CharT>;
3723 _Str __s = _GLIBCXX_WIDEN("{:L%a}{} is not a valid weekday");
3724 if (__wd.ok())
3725 __os << std::vformat(__os.getloc(), __s.substr(0, 6),
3726 make_format_args<_Ctx>(__wd));
3727 else
3728 {
3729 auto __c = __wd.c_encoding();
3730 __os << std::vformat(__s.substr(6), make_format_args<_Ctx>(__c));
3731 }
3732 return __os;
3733 }
3734
3735 template<typename _CharT, typename _Traits,
3736 typename _Alloc = allocator<_CharT>>
3737 inline basic_istream<_CharT, _Traits>&
3738 from_stream(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt,
3739 weekday& __wd,
3740 basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr,
3741 minutes* __offset = nullptr)
3742 {
3743 __detail::_Parser<> __p(__format::_ChronoParts::_Weekday);
3744 if (__p(__is, __fmt, __abbrev, __offset))
3745 __wd = __p._M_wd;
3746 return __is;
3747 }
3748
3749 template<typename _CharT, typename _Traits>
3750 inline basic_ostream<_CharT, _Traits>&
3751 operator<<(basic_ostream<_CharT, _Traits>& __os,
3752 const weekday_indexed& __wdi)
3753 {
3754 // The standard says to format wdi.weekday() and wdi.index() using
3755 // either "{:L}[{}]" or "{:L}[{} is not a valid index]". The {:L} spec
3756 // means to format the weekday using ostringstream, so just do that.
3757 basic_stringstream<_CharT> __os2;
3758 __os2.imbue(__os.getloc());
3759 __os2 << __wdi.weekday();
3760 const auto __i = __wdi.index();
3761 basic_string_view<_CharT> __s
3762 = _GLIBCXX_WIDEN("[ is not a valid index]");
3763 __os2 << __s[0];
3764 __os2 << std::format(_GLIBCXX_WIDEN("{}"), __i);
3765 if (__i >= 1 && __i <= 5)
3766 __os2 << __s.back();
3767 else
3768 __os2 << __s.substr(1);
3769 __os << __os2.view();
3770 return __os;
3771 }
3772
3773 template<typename _CharT, typename _Traits>
3774 inline basic_ostream<_CharT, _Traits>&
3775 operator<<(basic_ostream<_CharT, _Traits>& __os,
3776 const weekday_last& __wdl)
3777 {
3778 // As above, just write straight to a stringstream, as if by "{:L}[last]"
3779 basic_stringstream<_CharT> __os2;
3780 __os2.imbue(__os.getloc());
3781 __os2 << __wdl.weekday() << _GLIBCXX_WIDEN("[last]");
3782 __os << __os2.view();
3783 return __os;
3784 }
3785
3786 template<typename _CharT, typename _Traits>
3787 inline basic_ostream<_CharT, _Traits>&
3788 operator<<(basic_ostream<_CharT, _Traits>& __os, const month_day& __md)
3789 {
3790 // As above, just write straight to a stringstream, as if by "{:L}/{}"
3791 basic_stringstream<_CharT> __os2;
3792 __os2.imbue(__os.getloc());
3793 __os2 << __md.month();
3794 if constexpr (is_same_v<_CharT, char>)
3795 __os2 << '/';
3796 else
3797 __os2 << L'/';
3798 __os2 << __md.day();
3799 __os << __os2.view();
3800 return __os;
3801 }
3802
3803 template<typename _CharT, typename _Traits,
3804 typename _Alloc = allocator<_CharT>>
3805 inline basic_istream<_CharT, _Traits>&
3806 from_stream(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt,
3807 month_day& __md,
3808 basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr,
3809 minutes* __offset = nullptr)
3810 {
3811 using __format::_ChronoParts;
3812 auto __need = _ChronoParts::_Month | _ChronoParts::_Day;
3813 __detail::_Parser<> __p(__need);
3814 if (__p(__is, __fmt, __abbrev, __offset))
3815 __md = month_day(__p._M_ymd.month(), __p._M_ymd.day());
3816 return __is;
3817 }
3818
3819 template<typename _CharT, typename _Traits>
3820 inline basic_ostream<_CharT, _Traits>&
3821 operator<<(basic_ostream<_CharT, _Traits>& __os,
3822 const month_day_last& __mdl)
3823 {
3824 // As above, just write straight to a stringstream, as if by "{:L}/last"
3825 basic_stringstream<_CharT> __os2;
3826 __os2.imbue(__os.getloc());
3827 __os2 << __mdl.month() << _GLIBCXX_WIDEN("/last");
3828 __os << __os2.view();
3829 return __os;
3830 }
3831
3832 template<typename _CharT, typename _Traits>
3833 inline basic_ostream<_CharT, _Traits>&
3834 operator<<(basic_ostream<_CharT, _Traits>& __os,
3835 const month_weekday& __mwd)
3836 {
3837 // As above, just write straight to a stringstream, as if by "{:L}/{:L}"
3838 basic_stringstream<_CharT> __os2;
3839 __os2.imbue(__os.getloc());
3840 __os2 << __mwd.month();
3841 if constexpr (is_same_v<_CharT, char>)
3842 __os2 << '/';
3843 else
3844 __os2 << L'/';
3845 __os2 << __mwd.weekday_indexed();
3846 __os << __os2.view();
3847 return __os;
3848 }
3849
3850 template<typename _CharT, typename _Traits>
3851 inline basic_ostream<_CharT, _Traits>&
3852 operator<<(basic_ostream<_CharT, _Traits>& __os,
3853 const month_weekday_last& __mwdl)
3854 {
3855 // As above, just write straight to a stringstream, as if by "{:L}/{:L}"
3856 basic_stringstream<_CharT> __os2;
3857 __os2.imbue(__os.getloc());
3858 __os2 << __mwdl.month();
3859 if constexpr (is_same_v<_CharT, char>)
3860 __os2 << '/';
3861 else
3862 __os2 << L'/';
3863 __os2 << __mwdl.weekday_last();
3864 __os << __os2.view();
3865 return __os;
3866 }
3867
3868 template<typename _CharT, typename _Traits>
3869 inline basic_ostream<_CharT, _Traits>&
3870 operator<<(basic_ostream<_CharT, _Traits>& __os, const year_month& __ym)
3871 {
3872 // As above, just write straight to a stringstream, as if by "{}/{:L}"
3873 basic_stringstream<_CharT> __os2;
3874 __os2.imbue(__os.getloc());
3875 __os2 << __ym.year();
3876 if constexpr (is_same_v<_CharT, char>)
3877 __os2 << '/';
3878 else
3879 __os2 << L'/';
3880 __os2 << __ym.month();
3881 __os << __os2.view();
3882 return __os;
3883 }
3884
3885 template<typename _CharT, typename _Traits,
3886 typename _Alloc = allocator<_CharT>>
3887 inline basic_istream<_CharT, _Traits>&
3888 from_stream(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt,
3889 year_month& __ym,
3890 basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr,
3891 minutes* __offset = nullptr)
3892 {
3893 using __format::_ChronoParts;
3894 auto __need = _ChronoParts::_Year | _ChronoParts::_Month;
3895 __detail::_Parser<> __p(__need);
3896 if (__p(__is, __fmt, __abbrev, __offset))
3897 __ym = year_month(__p._M_ymd.year(), __p._M_ymd.month());
3898 return __is;
3899 }
3900
3901 template<typename _CharT, typename _Traits>
3902 inline basic_ostream<_CharT, _Traits>&
3903 operator<<(basic_ostream<_CharT, _Traits>& __os,
3904 const year_month_day& __ymd)
3905 {
3906 using _Ctx = __format::__format_context<_CharT>;
3907 using _Str = basic_string_view<_CharT>;
3908 _Str __s = _GLIBCXX_WIDEN("{:%F} is not a valid date");
3909 __os << std::vformat(__ymd.ok() ? __s.substr(0, 5) : __s,
3910 make_format_args<_Ctx>(__ymd));
3911 return __os;
3912 }
3913
3914 template<typename _CharT, typename _Traits,
3915 typename _Alloc = allocator<_CharT>>
3916 inline basic_istream<_CharT, _Traits>&
3917 from_stream(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt,
3918 year_month_day& __ymd,
3919 basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr,
3920 minutes* __offset = nullptr)
3921 {
3922 using __format::_ChronoParts;
3923 auto __need = _ChronoParts::_Year | _ChronoParts::_Month
3924 | _ChronoParts::_Day;
3925 __detail::_Parser<> __p(__need);
3926 if (__p(__is, __fmt, __abbrev, __offset))
3927 __ymd = __p._M_ymd;
3928 return __is;
3929 }
3930
3931 template<typename _CharT, typename _Traits>
3934 const year_month_day_last& __ymdl)
3935 {
3936 // As above, just write straight to a stringstream, as if by "{}/{:L}"
3938 __os2.imbue(__os.getloc());
3939 __os2 << __ymdl.year();
3940 if constexpr (is_same_v<_CharT, char>)
3941 __os2 << '/';
3942 else
3943 __os2 << L'/';
3944 __os2 << __ymdl.month_day_last();
3945 __os << __os2.view();
3946 return __os;
3947 }
3948
3949 template<typename _CharT, typename _Traits>
3950 inline basic_ostream<_CharT, _Traits>&
3951 operator<<(basic_ostream<_CharT, _Traits>& __os,
3952 const year_month_weekday& __ymwd)
3953 {
3954 // As above, just write straight to a stringstream, as if by
3955 // "{}/{:L}/{:L}"
3956 basic_stringstream<_CharT> __os2;
3957 __os2.imbue(__os.getloc());
3958 _CharT __slash;
3959 if constexpr (is_same_v<_CharT, char>)
3960 __slash = '/';
3961 else
3962 __slash = L'/';
3963 __os2 << __ymwd.year() << __slash << __ymwd.month() << __slash
3964 << __ymwd.weekday_indexed();
3965 __os << __os2.view();
3966 return __os;
3967 }
3968
3969 template<typename _CharT, typename _Traits>
3970 inline basic_ostream<_CharT, _Traits>&
3971 operator<<(basic_ostream<_CharT, _Traits>& __os,
3972 const year_month_weekday_last& __ymwdl)
3973 {
3974 // As above, just write straight to a stringstream, as if by
3975 // "{}/{:L}/{:L}"
3976 basic_stringstream<_CharT> __os2;
3977 __os2.imbue(__os.getloc());
3978 _CharT __slash;
3979 if constexpr (is_same_v<_CharT, char>)
3980 __slash = '/';
3981 else
3982 __slash = L'/';
3983 __os2 << __ymwdl.year() << __slash << __ymwdl.month() << __slash
3984 << __ymwdl.weekday_last();
3985 __os << __os2.view();
3986 return __os;
3987 }
3988
3989 template<typename _CharT, typename _Traits, typename _Duration>
3990 inline basic_ostream<_CharT, _Traits>&
3991 operator<<(basic_ostream<_CharT, _Traits>& __os,
3992 const hh_mm_ss<_Duration>& __hms)
3993 {
3994 return __os << format(__os.getloc(), _GLIBCXX_WIDEN("{:L%T}"), __hms);
3995 }
3996
3997#if _GLIBCXX_USE_CXX11_ABI || ! _GLIBCXX_USE_DUAL_ABI
3998 /// Writes a sys_info object to an ostream in an unspecified format.
3999 template<typename _CharT, typename _Traits>
4000 basic_ostream<_CharT, _Traits>&
4001 operator<<(basic_ostream<_CharT, _Traits>& __os, const sys_info& __i)
4002 {
4003 return __os << std::format(__os.getloc(), _GLIBCXX_WIDEN("{}"), __i);
4004 }
4005
4006 /// Writes a local_info object to an ostream in an unspecified format.
4007 template<typename _CharT, typename _Traits>
4008 basic_ostream<_CharT, _Traits>&
4009 operator<<(basic_ostream<_CharT, _Traits>& __os, const local_info& __li)
4010 {
4011 __os << __format::_Separators<_CharT>::_S_squares()[0];
4012 if (__li.result == local_info::unique)
4013 __os << __li.first;
4014 else
4015 {
4016 if (__li.result == local_info::nonexistent)
4017 __os << _GLIBCXX_WIDEN("nonexistent");
4018 else
4019 __os << _GLIBCXX_WIDEN("ambiguous");
4020 __os << _GLIBCXX_WIDEN(" local time between ") << __li.first;
4021 __os << _GLIBCXX_WIDEN(" and ") << __li.second;
4022 }
4023 __os << __format::_Separators<_CharT>::_S_squares()[1];
4024 return __os;
4025 }
4026
4027 template<typename _CharT, typename _Traits, typename _Duration,
4028 typename _TimeZonePtr>
4029 inline basic_ostream<_CharT, _Traits>&
4030 operator<<(basic_ostream<_CharT, _Traits>& __os,
4031 const zoned_time<_Duration, _TimeZonePtr>& __t)
4032 {
4033 __os << format(__os.getloc(), _GLIBCXX_WIDEN("{:L%F %T %Z}"), __t);
4034 return __os;
4035 }
4036#endif
4037
4038 template<typename _CharT, typename _Traits, typename _Duration>
4039 requires (!treat_as_floating_point_v<typename _Duration::rep>)
4040 && ratio_less_v<typename _Duration::period, days::period>
4041 inline basic_ostream<_CharT, _Traits>&
4042 operator<<(basic_ostream<_CharT, _Traits>& __os,
4043 const sys_time<_Duration>& __tp)
4044 {
4045 __os << std::format(__os.getloc(), _GLIBCXX_WIDEN("{:L%F %T}"), __tp);
4046 return __os;
4047 }
4048
4049 template<typename _CharT, typename _Traits>
4050 inline basic_ostream<_CharT, _Traits>&
4051 operator<<(basic_ostream<_CharT, _Traits>& __os, const sys_days& __dp)
4052 {
4053 __os << year_month_day{__dp};
4054 return __os;
4055 }
4056
4057 template<typename _CharT, typename _Traits, typename _Duration,
4058 typename _Alloc = allocator<_CharT>>
4059 basic_istream<_CharT, _Traits>&
4060 from_stream(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt,
4061 sys_time<_Duration>& __tp,
4062 basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr,
4063 minutes* __offset = nullptr)
4064 {
4065 minutes __off{};
4066 if (!__offset)
4067 __offset = &__off;
4068 using __format::_ChronoParts;
4069 auto __need = _ChronoParts::_Year | _ChronoParts::_Month
4070 | _ChronoParts::_Day | _ChronoParts::_TimeOfDay;
4071 __detail::_Parser_t<_Duration> __p(__need);
4072 if (__p(__is, __fmt, __abbrev, __offset))
4073 {
4074 if (__p._M_is_leap_second)
4075 __is.setstate(ios_base::failbit);
4076 else
4077 {
4078 auto __st = __p._M_sys_days + __p._M_time - *__offset;
4079 __tp = __detail::__round<_Duration>(__st);
4080 }
4081 }
4082 return __is;
4083 }
4084
4085 template<typename _CharT, typename _Traits, typename _Duration>
4086 inline basic_ostream<_CharT, _Traits>&
4087 operator<<(basic_ostream<_CharT, _Traits>& __os,
4088 const utc_time<_Duration>& __t)
4089 {
4090 __os << std::format(__os.getloc(), _GLIBCXX_WIDEN("{:L%F %T}"), __t);
4091 return __os;
4092 }
4093
4094 template<typename _CharT, typename _Traits, typename _Duration,
4095 typename _Alloc = allocator<_CharT>>
4096 inline basic_istream<_CharT, _Traits>&
4097 from_stream(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt,
4098 utc_time<_Duration>& __tp,
4099 basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr,
4100 minutes* __offset = nullptr)
4101 {
4102 minutes __off{};
4103 if (!__offset)
4104 __offset = &__off;
4105 using __format::_ChronoParts;
4106 auto __need = _ChronoParts::_Year | _ChronoParts::_Month
4107 | _ChronoParts::_Day | _ChronoParts::_TimeOfDay;
4108 __detail::_Parser_t<_Duration> __p(__need);
4109 if (__p(__is, __fmt, __abbrev, __offset))
4110 {
4111 // Converting to utc_time before adding _M_time is necessary for
4112 // "23:59:60" to correctly produce a time within a leap second.
4113 auto __ut = utc_clock::from_sys(__p._M_sys_days) + __p._M_time
4114 - *__offset;
4115 __tp = __detail::__round<_Duration>(__ut);
4116 }
4117 return __is;
4118 }
4119
4120 template<typename _CharT, typename _Traits, typename _Duration>
4121 inline basic_ostream<_CharT, _Traits>&
4122 operator<<(basic_ostream<_CharT, _Traits>& __os,
4123 const tai_time<_Duration>& __t)
4124 {
4125 __os << std::format(__os.getloc(), _GLIBCXX_WIDEN("{:L%F %T}"), __t);
4126 return __os;
4127 }
4128
4129 template<typename _CharT, typename _Traits, typename _Duration,
4130 typename _Alloc = allocator<_CharT>>
4131 inline basic_istream<_CharT, _Traits>&
4132 from_stream(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt,
4133 tai_time<_Duration>& __tp,
4134 basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr,
4135 minutes* __offset = nullptr)
4136 {
4137 minutes __off{};
4138 if (!__offset)
4139 __offset = &__off;
4140 using __format::_ChronoParts;
4141 auto __need = _ChronoParts::_Year | _ChronoParts::_Month
4142 | _ChronoParts::_Day | _ChronoParts::_TimeOfDay;
4143 __detail::_Parser_t<_Duration> __p(__need);
4144 if (__p(__is, __fmt, __abbrev, __offset))
4145 {
4146 if (__p._M_is_leap_second)
4147 __is.setstate(ios_base::failbit);
4148 else
4149 {
4150 constexpr sys_days __epoch(-days(4383)); // 1958y/1/1
4151 auto __d = __p._M_sys_days - __epoch + __p._M_time - *__offset;
4152 tai_time<common_type_t<_Duration, seconds>> __tt(__d);
4153 __tp = __detail::__round<_Duration>(__tt);
4154 }
4155 }
4156 return __is;
4157 }
4158
4159 template<typename _CharT, typename _Traits, typename _Duration>
4160 inline basic_ostream<_CharT, _Traits>&
4161 operator<<(basic_ostream<_CharT, _Traits>& __os,
4162 const gps_time<_Duration>& __t)
4163 {
4164 __os << std::format(__os.getloc(), _GLIBCXX_WIDEN("{:L%F %T}"), __t);
4165 return __os;
4166 }
4167
4168 template<typename _CharT, typename _Traits, typename _Duration,
4169 typename _Alloc = allocator<_CharT>>
4170 inline basic_istream<_CharT, _Traits>&
4171 from_stream(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt,
4172 gps_time<_Duration>& __tp,
4173 basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr,
4174 minutes* __offset = nullptr)
4175 {
4176 minutes __off{};
4177 if (!__offset)
4178 __offset = &__off;
4179 using __format::_ChronoParts;
4180 auto __need = _ChronoParts::_YearMonthDay | _ChronoParts::_TimeOfDay;
4181 __detail::_Parser_t<_Duration> __p(__need);
4182 if (__p(__is, __fmt, __abbrev, __offset))
4183 {
4184 if (__p._M_is_leap_second)
4185 __is.setstate(ios_base::failbit);
4186 else
4187 {
4188 constexpr sys_days __epoch(days(3657)); // 1980y/1/Sunday[1]
4189 auto __d = __p._M_sys_days - __epoch + __p._M_time - *__offset;
4190 gps_time<common_type_t<_Duration, seconds>> __gt(__d);
4191 __tp = __detail::__round<_Duration>(__gt);
4192 }
4193 }
4194 return __is;
4195 }
4196
4197 template<typename _CharT, typename _Traits, typename _Duration>
4198 inline basic_ostream<_CharT, _Traits>&
4199 operator<<(basic_ostream<_CharT, _Traits>& __os,
4200 const file_time<_Duration>& __t)
4201 {
4202 __os << std::format(__os.getloc(), _GLIBCXX_WIDEN("{:L%F %T}"), __t);
4203 return __os;
4204 }
4205
4206 template<typename _CharT, typename _Traits, typename _Duration,
4207 typename _Alloc = allocator<_CharT>>
4208 inline basic_istream<_CharT, _Traits>&
4209 from_stream(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt,
4210 file_time<_Duration>& __tp,
4211 basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr,
4212 minutes* __offset = nullptr)
4213 {
4214 sys_time<_Duration> __st;
4215 if (chrono::from_stream(__is, __fmt, __st, __abbrev, __offset))
4216 __tp = __detail::__round<_Duration>(file_clock::from_sys(__st));
4217 return __is;
4218 }
4219
4220 template<typename _CharT, typename _Traits, typename _Duration>
4221 inline basic_ostream<_CharT, _Traits>&
4222 operator<<(basic_ostream<_CharT, _Traits>& __os,
4223 const local_time<_Duration>& __lt)
4224 // _GLIBCXX_RESOLVE_LIB_DEFECTS
4225 // 4257. Stream insertion for chrono::local_time should be constrained
4226 requires requires(const sys_time<_Duration>& __st) { __os << __st; }
4227 {
4228 __os << sys_time<_Duration>{__lt.time_since_epoch()};
4229 return __os;
4230 }
4231
4232 template<typename _CharT, typename _Traits, typename _Duration,
4233 typename _Alloc = allocator<_CharT>>
4234 basic_istream<_CharT, _Traits>&
4235 from_stream(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt,
4236 local_time<_Duration>& __tp,
4237 basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr,
4238 minutes* __offset = nullptr)
4239 {
4240 using __format::_ChronoParts;
4241 auto __need = _ChronoParts::_YearMonthDay | _ChronoParts::_TimeOfDay;
4242 __detail::_Parser_t<_Duration> __p(__need);
4243 if (__p(__is, __fmt, __abbrev, __offset))
4244 {
4245 days __d = __p._M_sys_days.time_since_epoch();
4246 auto __t = local_days(__d) + __p._M_time; // ignore offset
4247 __tp = __detail::__round<_Duration>(__t);
4248 }
4249 return __is;
4250 }
4251
4252 // [time.parse] parsing
4253
4254namespace __detail
4255{
4256 // _GLIBCXX_RESOLVE_LIB_DEFECTS
4257 // 3956. chrono::parse uses from_stream as a customization point
4258 void from_stream() = delete;
4259
4260 template<typename _Parsable, typename _CharT,
4261 typename _Traits = std::char_traits<_CharT>,
4262 typename... _OptArgs>
4263 concept __parsable = requires (basic_istream<_CharT, _Traits>& __is,
4264 const _CharT* __fmt, _Parsable& __tp,
4265 _OptArgs*... __args)
4266 { from_stream(__is, __fmt, __tp, __args...); };
4267
4268 template<typename _Parsable, typename _CharT,
4269 typename _Traits = char_traits<_CharT>,
4270 typename _Alloc = allocator<_CharT>>
4271 struct _Parse
4272 {
4273 private:
4274 using __string_type = basic_string<_CharT, _Traits, _Alloc>;
4275
4276 public:
4277 _Parse(const _CharT* __fmt, _Parsable& __tp,
4278 basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr,
4279 minutes* __offset = nullptr)
4280 : _M_fmt(__fmt), _M_tp(std::__addressof(__tp)),
4281 _M_abbrev(__abbrev), _M_offset(__offset)
4282 { }
4283
4284 _Parse(_Parse&&) = delete;
4285 _Parse& operator=(_Parse&&) = delete;
4286
4287 private:
4288 using __stream_type = basic_istream<_CharT, _Traits>;
4289
4290 const _CharT* const _M_fmt;
4291 _Parsable* const _M_tp;
4292 __string_type* const _M_abbrev;
4293 minutes* const _M_offset;
4294
4295 friend __stream_type&
4296 operator>>(__stream_type& __is, _Parse&& __p)
4297 {
4298 if (__p._M_offset)
4299 from_stream(__is, __p._M_fmt, *__p._M_tp, __p._M_abbrev,
4300 __p._M_offset);
4301 else if (__p._M_abbrev)
4302 from_stream(__is, __p._M_fmt, *__p._M_tp, __p._M_abbrev);
4303 else
4304 from_stream(__is, __p._M_fmt, *__p._M_tp);
4305 return __is;
4306 }
4307
4308 friend void operator>>(__stream_type&, _Parse&) = delete;
4309 friend void operator>>(__stream_type&, const _Parse&) = delete;
4310 };
4311} // namespace __detail
4312
4313 template<typename _CharT, __detail::__parsable<_CharT> _Parsable>
4314 [[nodiscard, __gnu__::__access__(__read_only__, 1)]]
4315 inline auto
4316 parse(const _CharT* __fmt, _Parsable& __tp)
4317 { return __detail::_Parse<_Parsable, _CharT>(__fmt, __tp); }
4318
4319 template<typename _CharT, typename _Traits, typename _Alloc,
4320 __detail::__parsable<_CharT, _Traits> _Parsable>
4321 [[nodiscard]]
4322 inline auto
4323 parse(const basic_string<_CharT, _Traits, _Alloc>& __fmt, _Parsable& __tp)
4324 {
4325 return __detail::_Parse<_Parsable, _CharT, _Traits>(__fmt.c_str(), __tp);
4326 }
4327
4328 template<typename _CharT, typename _Traits, typename _Alloc,
4329 typename _StrT = basic_string<_CharT, _Traits, _Alloc>,
4330 __detail::__parsable<_CharT, _Traits, _StrT> _Parsable>
4331 [[nodiscard, __gnu__::__access__(__read_only__, 1)]]
4332 inline auto
4333 parse(const _CharT* __fmt, _Parsable& __tp,
4334 basic_string<_CharT, _Traits, _Alloc>& __abbrev)
4335 {
4336 auto __pa = std::__addressof(__abbrev);
4337 return __detail::_Parse<_Parsable, _CharT, _Traits, _Alloc>(__fmt, __tp,
4338 __pa);
4339 }
4340
4341 template<typename _CharT, typename _Traits, typename _Alloc,
4342 typename _StrT = basic_string<_CharT, _Traits, _Alloc>,
4343 __detail::__parsable<_CharT, _Traits, _StrT> _Parsable>
4344 [[nodiscard]]
4345 inline auto
4346 parse(const basic_string<_CharT, _Traits, _Alloc>& __fmt, _Parsable& __tp,
4347 basic_string<_CharT, _Traits, _Alloc>& __abbrev)
4348 {
4349 auto __pa = std::__addressof(__abbrev);
4350 return __detail::_Parse<_Parsable, _CharT, _Traits, _Alloc>(__fmt.c_str(),
4351 __tp, __pa);
4352 }
4353
4354 template<typename _CharT, typename _Traits = char_traits<_CharT>,
4355 typename _StrT = basic_string<_CharT, _Traits>,
4356 __detail::__parsable<_CharT, _Traits, _StrT, minutes> _Parsable>
4357 [[nodiscard, __gnu__::__access__(__read_only__, 1)]]
4358 inline auto
4359 parse(const _CharT* __fmt, _Parsable& __tp, minutes& __offset)
4360 {
4361 return __detail::_Parse<_Parsable, _CharT>(__fmt, __tp, nullptr,
4362 &__offset);
4363 }
4364
4365 template<typename _CharT, typename _Traits, typename _Alloc,
4366 typename _StrT = basic_string<_CharT, _Traits>,
4367 __detail::__parsable<_CharT, _Traits, _StrT, minutes> _Parsable>
4368 [[nodiscard]]
4369 inline auto
4370 parse(const basic_string<_CharT, _Traits, _Alloc>& __fmt, _Parsable& __tp,
4371 minutes& __offset)
4372 {
4373 return __detail::_Parse<_Parsable, _CharT, _Traits, _Alloc>(__fmt.c_str(),
4374 __tp, nullptr,
4375 &__offset);
4376 }
4377
4378 template<typename _CharT, typename _Traits, typename _Alloc,
4379 typename _StrT = basic_string<_CharT, _Traits, _Alloc>,
4380 __detail::__parsable<_CharT, _Traits, _StrT, minutes> _Parsable>
4381 [[nodiscard, __gnu__::__access__(__read_only__, 1)]]
4382 inline auto
4383 parse(const _CharT* __fmt, _Parsable& __tp,
4384 basic_string<_CharT, _Traits, _Alloc>& __abbrev, minutes& __offset)
4385 {
4386 auto __pa = std::__addressof(__abbrev);
4387 return __detail::_Parse<_Parsable, _CharT, _Traits, _Alloc>(__fmt, __tp,
4388 __pa,
4389 &__offset);
4390 }
4391
4392 template<typename _CharT, typename _Traits, typename _Alloc,
4393 typename _StrT = basic_string<_CharT, _Traits, _Alloc>,
4394 __detail::__parsable<_CharT, _Traits, _StrT, minutes> _Parsable>
4395 [[nodiscard]]
4396 inline auto
4397 parse(const basic_string<_CharT, _Traits, _Alloc>& __fmt, _Parsable& __tp,
4398 basic_string<_CharT, _Traits, _Alloc>& __abbrev, minutes& __offset)
4399 {
4400 auto __pa = std::__addressof(__abbrev);
4401 return __detail::_Parse<_Parsable, _CharT, _Traits, _Alloc>(__fmt.c_str(),
4402 __tp, __pa,
4403 &__offset);
4404 }
4405
4406 /// @cond undocumented
4407 template<typename _Duration>
4408 template<typename _CharT, typename _Traits, typename _Alloc>
4409 basic_istream<_CharT, _Traits>&
4410 __detail::_Parser<_Duration>::
4411 operator()(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt,
4412 basic_string<_CharT, _Traits, _Alloc>* __abbrev,
4413 minutes* __offset)
4414 {
4415 using sentry = typename basic_istream<_CharT, _Traits>::sentry;
4417 if (sentry __cerb(__is, true); __cerb)
4418 {
4419 locale __loc = __is.getloc();
4420 auto& __tmget = std::use_facet<std::time_get<_CharT>>(__loc);
4421 auto& __tmpunct = std::use_facet<std::__timepunct<_CharT>>(__loc);
4422
4423 // RAII type to save and restore stream state.
4424 struct _Stream_state
4425 {
4426 explicit
4427 _Stream_state(basic_istream<_CharT, _Traits>& __i)
4428 : _M_is(__i),
4429 _M_flags(__i.flags(ios_base::skipws | ios_base::dec)),
4430 _M_w(__i.width(0))
4431 { }
4432
4433 ~_Stream_state()
4434 {
4435 _M_is.flags(_M_flags);
4436 _M_is.width(_M_w);
4437 }
4438
4439 _Stream_state(_Stream_state&&) = delete;
4440
4441 basic_istream<_CharT, _Traits>& _M_is;
4442 ios_base::fmtflags _M_flags;
4443 streamsize _M_w;
4444 };
4445
4446 auto __is_failed = [](ios_base::iostate __e) {
4447 return static_cast<bool>(__e & ios_base::failbit);
4448 };
4449
4450 // Read an unsigned integer from the stream and return it.
4451 // Extract no more than __n digits. Set __err on error.
4452 auto __read_unsigned = [&] (int __n) {
4453 return _S_read_unsigned(__is, __err, __n);
4454 };
4455
4456 // Read a signed integer from the stream and return it.
4457 // Extract no more than __n digits. Set __err on error.
4458 auto __read_signed = [&] (int __n) {
4459 return _S_read_signed(__is, __err, __n);
4460 };
4461
4462 // Read an expected character from the stream.
4463 auto __read_chr = [&__is, &__err] (_CharT __c) {
4464 return _S_read_chr(__is, __err, __c);
4465 };
4466
4467 using __format::_ChronoParts;
4468 _ChronoParts __parts{};
4469
4470 const year __bad_y = --year::min(); // SHRT_MIN
4471 const month __bad_mon(255);
4472 const day __bad_day(255);
4473 const weekday __bad_wday(255);
4474 const hours __bad_h(-1);
4475 const minutes __bad_min(-9999);
4476 const seconds __bad_sec(-1);
4477
4478 year __y = __bad_y, __yy = __bad_y; // %Y, %yy
4479 year __iso_y = __bad_y, __iso_yy = __bad_y; // %G, %g
4480 month __m = __bad_mon; // %m
4481 day __d = __bad_day; // %d
4482 weekday __wday = __bad_wday; // %a %A %u %w
4483 hours __h = __bad_h, __h12 = __bad_h; // %H, %I
4484 minutes __min = __bad_min; // %M
4485 _Duration __s = __bad_sec; // %S
4486 int __ampm = 0; // %p
4487 int __iso_wk = -1, __sunday_wk = -1, __monday_wk = -1; // %V, %U, %W
4488 int __century = -1; // %C
4489 int __dayofyear = -1; // %j (for non-duration)
4490
4491 minutes __tz_offset = __bad_min;
4492 basic_string<_CharT, _Traits> __tz_abbr;
4493
4494 if ((_M_need & _ChronoParts::_TimeOfDay) != 0
4495 && (_M_need & _ChronoParts::_Year) != 0)
4496 {
4497 // For time_points assume "00:00:00" is implicitly present,
4498 // so we don't fail to parse if it's not (PR libstdc++/114240).
4499 // We will still fail to parse if there's no year+month+day.
4500 __h = hours(0);
4501 __parts = _ChronoParts::_TimeOfDay;
4502 }
4503
4504 // bool __is_neg = false; // TODO: how is this handled for parsing?
4505
4506 _CharT __mod{}; // One of 'E' or 'O' or nul.
4507 unsigned __num = 0; // Non-zero for N modifier.
4508 bool __is_flag = false; // True if we're processing a % flag.
4509
4510 constexpr bool __is_floating
4511 = treat_as_floating_point_v<typename _Duration::rep>;
4512
4513 // If an out-of-range value is extracted (e.g. 61min for %M),
4514 // do not set failbit immediately because we might not need it
4515 // (e.g. parsing chrono::year doesn't care about invalid %M values).
4516 // Instead set the variable back to its initial 'bad' state,
4517 // and also set related variables corresponding to the same field
4518 // (e.g. a bad %M value for __min should also reset __h and __s).
4519 // If a valid value is needed later the bad value will cause failure.
4520
4521 // For some fields we don't know the correct range when parsing and
4522 // we have to be liberal in what we accept, e.g. we allow 366 for
4523 // day-of-year because that's valid in leap years, and we allow 31
4524 // for day-of-month. If those values are needed to determine the
4525 // result then we can do a correct range check at the end when we
4526 // know the how many days the relevant year or month actually has.
4527
4528 while (*__fmt)
4529 {
4530 _CharT __c = *__fmt++;
4531 if (!__is_flag)
4532 {
4533 if (__c == '%')
4534 __is_flag = true; // This is the start of a flag.
4535 else if (std::isspace(__c, __loc))
4536 std::ws(__is); // Match zero or more whitespace characters.
4537 else if (!__read_chr(__c)) [[unlikely]]
4538 break; // Failed to match the expected character.
4539
4540 continue; // Process next character in the format string.
4541 }
4542
4543 // Now processing a flag.
4544 switch (__c)
4545 {
4546 case 'a': // Locale's weekday name
4547 case 'A': // (full or abbreviated, matched case-insensitively).
4548 if (__mod || __num) [[unlikely]]
4549 __err = ios_base::failbit;
4550 else
4551 {
4552 struct tm __tm{};
4553 __tmget.get(__is, {}, __is, __err, &__tm,
4554 __fmt - 2, __fmt);
4555 if (!__is_failed(__err))
4556 __wday = weekday(__tm.tm_wday);
4557 }
4558 __parts |= _ChronoParts::_Weekday;
4559 break;
4560
4561 case 'b': // Locale's month name
4562 case 'h': // (full or abbreviated, matched case-insensitively).
4563 case 'B':
4564 if (__mod || __num) [[unlikely]]
4565 __err = ios_base::failbit;
4566 else
4567 {
4568 // strptime behaves differently for %b and %B,
4569 // but chrono::parse says they're equivalent.
4570 // Luckily libstdc++ std::time_get works as needed.
4571 struct tm __tm{};
4572 __tmget.get(__is, {}, __is, __err, &__tm,
4573 __fmt - 2, __fmt);
4574 if (!__is_failed(__err))
4575 __m = month(__tm.tm_mon + 1);
4576 }
4577 __parts |= _ChronoParts::_Month;
4578 break;
4579
4580 case 'c': // Locale's date and time representation.
4581 if (__mod == 'O' || __num) [[unlikely]]
4582 __err |= ios_base::failbit;
4583 else
4584 {
4585 struct tm __tm{};
4586 __tmget.get(__is, {}, __is, __err, &__tm,
4587 __fmt - 2 - (__mod == 'E'), __fmt);
4588 if (!__is_failed(__err))
4589 {
4590 __y = year(__tm.tm_year + 1900);
4591 __m = month(__tm.tm_mon + 1);
4592 __d = day(__tm.tm_mday);
4593 __h = hours(__tm.tm_hour);
4594 __min = minutes(__tm.tm_min);
4595 __s = seconds(__tm.tm_sec);
4596 }
4597 }
4598 __parts |= _ChronoParts::_DateTime;
4599 break;
4600
4601 case 'C': // Century
4602 if (!__mod) [[likely]]
4603 {
4604 auto __v = __read_signed(__num ? __num : 2);
4605 if (!__is_failed(__err))
4606 {
4607 int __cmin = (int)year::min() / 100;
4608 int __cmax = (int)year::max() / 100;
4609 if (__cmin <= __v && __v <= __cmax)
4610 __century = __v * 100;
4611 else
4612 __century = -2; // This prevents guessing century.
4613 }
4614 }
4615 else if (__mod == 'E')
4616 {
4617 struct tm __tm{};
4618 __tmget.get(__is, {}, __is, __err, &__tm,
4619 __fmt - 3, __fmt);
4620 if (!__is_failed(__err))
4621 __century = __tm.tm_year;
4622 }
4623 else [[unlikely]]
4624 __err |= ios_base::failbit;
4625 // N.B. don't set this here: __parts |= _ChronoParts::_Year;
4626 break;
4627
4628 case 'd': // Day of month (1-31)
4629 case 'e':
4630 if (!__mod) [[likely]]
4631 {
4632 auto __v = __read_unsigned(__num ? __num : 2);
4633 if (!__is_failed(__err))
4634 __d = day(__v);
4635 }
4636 else if (__mod == 'O')
4637 {
4638 struct tm __tm{};
4639 __tmget.get(__is, {}, __is, __err, &__tm,
4640 __fmt - 3, __fmt);
4641 if (!__is_failed(__err))
4642 __d = day(__tm.tm_mday);
4643 }
4644 else [[unlikely]]
4645 __err |= ios_base::failbit;
4646 __parts |= _ChronoParts::_Day;
4647 break;
4648
4649 case 'D': // %m/%d/%y
4650 if (__mod || __num) [[unlikely]]
4651 __err |= ios_base::failbit;
4652 else
4653 {
4654 auto __month = __read_unsigned(2); // %m
4655 __read_chr('/');
4656 auto __day = __read_unsigned(2); // %d
4657 __read_chr('/');
4658 auto __year = __read_unsigned(2); // %y
4659 if (__is_failed(__err))
4660 break;
4661 __y = year(__year + 1900 + 100 * int(__year < 69));
4662 __m = month(__month);
4663 __d = day(__day);
4664 if (!year_month_day(__y, __m, __d).ok())
4665 {
4666 __y = __yy = __iso_y = __iso_yy = __bad_y;
4667 __m = __bad_mon;
4668 __d = __bad_day;
4669 break;
4670 }
4671 }
4672 __parts |= _ChronoParts::_Date;
4673 break;
4674
4675 case 'F': // %Y-%m-%d - any N modifier only applies to %Y.
4676 if (__mod) [[unlikely]]
4677 __err |= ios_base::failbit;
4678 else
4679 {
4680 auto __year = __read_signed(__num ? __num : 4); // %Y
4681 __read_chr('-');
4682 auto __month = __read_unsigned(2); // %m
4683 __read_chr('-');
4684 auto __day = __read_unsigned(2); // %d
4685 if (__is_failed(__err))
4686 break;
4687 __y = year(__year);
4688 __m = month(__month);
4689 __d = day(__day);
4690 if (!year_month_day(__y, __m, __d).ok())
4691 {
4692 __y = __yy = __iso_y = __iso_yy = __bad_y;
4693 __m = __bad_mon;
4694 __d = __bad_day;
4695 break;
4696 }
4697 }
4698 __parts |= _ChronoParts::_Date;
4699 break;
4700
4701 case 'g': // Last two digits of ISO week-based year.
4702 if (__mod) [[unlikely]]
4703 __err |= ios_base::failbit;
4704 else
4705 {
4706 auto __val = __read_unsigned(__num ? __num : 2);
4707 if (__val >= 0 && __val <= 99)
4708 {
4709 __iso_yy = year(__val);
4710 if (__century == -1) // No %C has been parsed yet.
4711 __century = 2000;
4712 }
4713 else
4714 __iso_yy = __iso_y = __y = __yy = __bad_y;
4715 }
4716 __parts |= _ChronoParts::_Year;
4717 break;
4718
4719 case 'G': // ISO week-based year.
4720 if (__mod) [[unlikely]]
4721 __err |= ios_base::failbit;
4722 else
4723 __iso_y = year(__read_unsigned(__num ? __num : 4));
4724 __parts |= _ChronoParts::_Year;
4725 break;
4726
4727 case 'H': // 24-hour (00-23)
4728 case 'I': // 12-hour (1-12)
4729 if (__mod == 'E') [[unlikely]]
4730 __err |= ios_base::failbit;
4731 else if (__mod == 'O')
4732 {
4733#if 0
4734 struct tm __tm{};
4735 __tm.tm_ampm = 1;
4736 __tmget.get(__is, {}, __is, __err, &__tm,
4737 __fmt - 3, __fmt);
4738 if (!__is_failed(__err))
4739 {
4740 if (__c == 'I')
4741 {
4742 __h12 = hours(__tm.tm_hour);
4743 __h = __bad_h;
4744 }
4745 else
4746 __h = hours(__tm.tm_hour);
4747 }
4748#else
4749 // XXX %OI seems to be unimplementable.
4750 __err |= ios_base::failbit;
4751#endif
4752 }
4753 else
4754 {
4755 auto __val = __read_unsigned(__num ? __num : 2);
4756 if (__c == 'I' && __val >= 1 && __val <= 12)
4757 {
4758 __h12 = hours(__val);
4759 __h = __bad_h;
4760 }
4761 else if (__c == 'H' && __val >= 0 && __val <= 23)
4762 {
4763 __h = hours(__val);
4764 __h12 = __bad_h;
4765 }
4766 else
4767 {
4768 if ((_M_need & _ChronoParts::_TimeOfDay) != 0)
4769 __err |= ios_base::failbit;
4770 break;
4771 }
4772 }
4773 __parts |= _ChronoParts::_TimeOfDay;
4774 break;
4775
4776 case 'j': // For duration, count of days, otherwise day of year
4777 if (__mod) [[unlikely]]
4778 __err |= ios_base::failbit;
4779 else if (_M_need == _ChronoParts::_TimeOfDay) // duration
4780 {
4781 auto __val = __read_signed(__num ? __num : 3);
4782 if (!__is_failed(__err))
4783 {
4784 __h = days(__val); // __h will get added to _M_time
4785 __parts |= _ChronoParts::_TimeOfDay;
4786 }
4787 }
4788 else
4789 {
4790 __dayofyear = __read_unsigned(__num ? __num : 3);
4791 // N.B. do not alter __parts here, done after loop.
4792 // No need for range checking here either.
4793 }
4794 break;
4795
4796 case 'm': // Month (1-12)
4797 if (__mod == 'E') [[unlikely]]
4798 __err |= ios_base::failbit;
4799 else if (__mod == 'O')
4800 {
4801 struct tm __tm{};
4802 __tmget.get(__is, {}, __is, __err, &__tm,
4803 __fmt - 2, __fmt);
4804 if (!__is_failed(__err))
4805 __m = month(__tm.tm_mon + 1);
4806 }
4807 else
4808 {
4809 auto __val = __read_unsigned(__num ? __num : 2);
4810 if (__val >= 1 && __val <= 12)
4811 __m = month(__val);
4812 else
4813 __m = __bad_mon;
4814 }
4815 __parts |= _ChronoParts::_Month;
4816 break;
4817
4818 case 'M': // Minutes
4819 if (__mod == 'E') [[unlikely]]
4820 __err |= ios_base::failbit;
4821 else if (__mod == 'O')
4822 {
4823 struct tm __tm{};
4824 __tmget.get(__is, {}, __is, __err, &__tm,
4825 __fmt - 2, __fmt);
4826 if (!__is_failed(__err))
4827 __min = minutes(__tm.tm_min);
4828 }
4829 else
4830 {
4831 auto __val = __read_unsigned(__num ? __num : 2);
4832 if (0 <= __val && __val < 60)
4833 __min = minutes(__val);
4834 else
4835 {
4836 if ((_M_need & _ChronoParts::_TimeOfDay) != 0)
4837 __err |= ios_base::failbit;
4838 break;
4839 }
4840 }
4841 __parts |= _ChronoParts::_TimeOfDay;
4842 break;
4843
4844 case 'p': // Locale's AM/PM designation for 12-hour clock.
4845 if (__mod || __num)
4846 __err |= ios_base::failbit;
4847 else
4848 {
4849 // Can't use std::time_get here as it can't parse %p
4850 // in isolation without %I. This might be faster anyway.
4851 const _CharT* __ampms[2];
4852 __tmpunct._M_am_pm(__ampms);
4853 int __n = 0, __which = 3;
4854 while (__which != 0)
4855 {
4856 auto __i = __is.peek();
4857 if (_Traits::eq_int_type(__i, _Traits::eof()))
4858 {
4860 break;
4861 }
4862 __i = std::toupper(_Traits::to_char_type(__i), __loc);
4863 if (__which & 1)
4864 {
4865 if (__i != std::toupper(__ampms[0][__n], __loc))
4866 __which ^= 1;
4867 else if (__ampms[0][__n + 1] == _CharT())
4868 {
4869 __which = 1;
4870 (void) __is.get();
4871 break;
4872 }
4873 }
4874 if (__which & 2)
4875 {
4876 if (__i != std::toupper(__ampms[1][__n], __loc))
4877 __which ^= 2;
4878 else if (__ampms[1][__n + 1] == _CharT())
4879 {
4880 __which = 2;
4881 (void) __is.get();
4882 break;
4883 }
4884 }
4885 if (__which)
4886 (void) __is.get();
4887 ++__n;
4888 }
4889 if (__which == 0 || __which == 3)
4890 __err |= ios_base::failbit;
4891 else
4892 __ampm = __which;
4893 }
4894 break;
4895
4896 case 'r': // Locale's 12-hour time.
4897 if (__mod || __num)
4898 __err |= ios_base::failbit;
4899 else
4900 {
4901 struct tm __tm{};
4902 __tmget.get(__is, {}, __is, __err, &__tm,
4903 __fmt - 2, __fmt);
4904 if (!__is_failed(__err))
4905 {
4906 __h = hours(__tm.tm_hour);
4907 __min = minutes(__tm.tm_min);
4908 __s = seconds(__tm.tm_sec);
4909 }
4910 }
4911 __parts |= _ChronoParts::_TimeOfDay;
4912 break;
4913
4914 case 'R': // %H:%M
4915 case 'T': // %H:%M:%S
4916 if (__mod || __num) [[unlikely]]
4917 {
4918 __err |= ios_base::failbit;
4919 break;
4920 }
4921 else
4922 {
4923 auto __val = __read_unsigned(2);
4924 if (__val == -1 || __val > 23) [[unlikely]]
4925 {
4926 if ((_M_need & _ChronoParts::_TimeOfDay) != 0)
4927 __err |= ios_base::failbit;
4928 break;
4929 }
4930 if (!__read_chr(':')) [[unlikely]]
4931 break;
4932 __h = hours(__val);
4933
4934 __val = __read_unsigned(2);
4935 if (__val == -1 || __val > 60) [[unlikely]]
4936 {
4937 if ((_M_need & _ChronoParts::_TimeOfDay) != 0)
4938 __err |= ios_base::failbit;
4939 break;
4940 }
4941 __min = minutes(__val);
4942
4943 if (__c == 'R')
4944 {
4945 __parts |= _ChronoParts::_TimeOfDay;
4946 break;
4947 }
4948 else if (!__read_chr(':')) [[unlikely]]
4949 break;
4950 }
4951 [[fallthrough]];
4952
4953 case 'S': // Seconds
4954 if (__mod == 'E') [[unlikely]]
4955 __err |= ios_base::failbit;
4956 else if (__mod == 'O')
4957 {
4958 struct tm __tm{};
4959 __tmget.get(__is, {}, __is, __err, &__tm,
4960 __fmt - 3, __fmt);
4961 if (!__is_failed(__err))
4962 __s = seconds(__tm.tm_sec);
4963 }
4964 else if constexpr (_Duration::period::den == 1
4965 && !__is_floating)
4966 {
4967 auto __val = __read_unsigned(__num ? __num : 2);
4968 if (0 <= __val && __val <= 59) [[likely]]
4969 __s = seconds(__val);
4970 else
4971 {
4972 if ((_M_need & _ChronoParts::_TimeOfDay) != 0)
4973 __err |= ios_base::failbit;
4974 break;
4975 }
4976 }
4977 else // Read fractional seconds
4978 {
4979 basic_stringstream<_CharT> __buf;
4980 auto __digit = _S_try_read_digit(__is, __err);
4981 if (__digit != -1)
4982 {
4983 __buf.put(_CharT('0') + __digit);
4984 __digit = _S_try_read_digit(__is, __err);
4985 if (__digit != -1)
4986 __buf.put(_CharT('0') + __digit);
4987 }
4988
4989 auto __i = __is.peek();
4990 if (_Traits::eq_int_type(__i, _Traits::eof()))
4991 __err |= ios_base::eofbit;
4992 else
4993 {
4994 _CharT __dp = '.';
4995 if (__loc != locale::classic())
4996 {
4997 auto& __np = use_facet<numpunct<_CharT>>(__loc);
4998 __dp = __np.decimal_point();
4999 }
5000 _CharT __c = _Traits::to_char_type(__i);
5001 if (__c == __dp)
5002 {
5003 (void) __is.get();
5004 __buf.put('.');
5005 int __prec
5006 = hh_mm_ss<_Duration>::fractional_width;
5007 do
5008 {
5009 __digit = _S_try_read_digit(__is, __err);
5010 if (__digit != -1)
5011 __buf.put(_CharT('0') + __digit);
5012 else
5013 break;
5014 }
5015 while (--__prec);
5016 }
5017 }
5018
5019 if (!__is_failed(__err)) [[likely]]
5020 {
5021 long double __val{};
5022#if __cpp_lib_to_chars
5023 string __str = std::move(__buf).str();
5024 auto __first = __str.data();
5025 auto __last = __first + __str.size();
5026 using enum chars_format;
5027 auto [ptr, ec] = std::from_chars(__first, __last,
5028 __val, fixed);
5029 if ((bool)ec || ptr != __last) [[unlikely]]
5030 __err |= ios_base::failbit;
5031 else
5032#else
5033 if (__buf >> __val)
5034#endif
5035 {
5036 duration<long double> __fs(__val);
5037 if constexpr (__is_floating)
5038 __s = __fs;
5039 else
5040 __s = chrono::round<_Duration>(__fs);
5041 }
5042 }
5043 }
5044 __parts |= _ChronoParts::_TimeOfDay;
5045 break;
5046
5047 case 'u': // ISO weekday (1-7)
5048 case 'w': // Weekday (0-6)
5049 if (__mod == 'E') [[unlikely]]
5050 __err |= ios_base::failbit;
5051 else if (__mod == 'O')
5052 {
5053 if (__c == 'w')
5054 {
5055 struct tm __tm{};
5056 __tmget.get(__is, {}, __is, __err, &__tm,
5057 __fmt - 3, __fmt);
5058 if (!__is_failed(__err))
5059 __wday = weekday(__tm.tm_wday);
5060 }
5061 else
5062 __err |= ios_base::failbit;
5063 }
5064 else
5065 {
5066 const int __lo = __c == 'u' ? 1 : 0;
5067 const int __hi = __lo + 6;
5068 auto __val = __read_unsigned(__num ? __num : 1);
5069 if (__lo <= __val && __val <= __hi)
5070 __wday = weekday(__val);
5071 else
5072 {
5073 __wday = __bad_wday;
5074 break;
5075 }
5076 }
5077 __parts |= _ChronoParts::_Weekday;
5078 break;
5079
5080 case 'U': // Week number of the year (from first Sunday).
5081 case 'V': // ISO week-based week number.
5082 case 'W': // Week number of the year (from first Monday).
5083 if (__mod == 'E') [[unlikely]]
5084 __err |= ios_base::failbit;
5085 else if (__mod == 'O')
5086 {
5087 if (__c == 'V') [[unlikely]]
5088 __err |= ios_base::failbit;
5089 else
5090 {
5091 // TODO nl_langinfo_l(ALT_DIGITS) ?
5092 // Not implementable using std::time_get.
5093 }
5094 }
5095 else
5096 {
5097 const int __lo = __c == 'V' ? 1 : 0;
5098 const int __hi = 53;
5099 auto __val = __read_unsigned(__num ? __num : 2);
5100 if (__lo <= __val && __val <= __hi)
5101 {
5102 switch (__c)
5103 {
5104 case 'U':
5105 __sunday_wk = __val;
5106 break;
5107 case 'V':
5108 __iso_wk = __val;
5109 break;
5110 case 'W':
5111 __monday_wk = __val;
5112 break;
5113 }
5114 }
5115 else
5116 __iso_wk = __sunday_wk = __monday_wk = -1;
5117 }
5118 // N.B. do not alter __parts here, done after loop.
5119 break;
5120
5121 case 'x': // Locale's date representation.
5122 if (__mod == 'O' || __num) [[unlikely]]
5123 __err |= ios_base::failbit;
5124 else
5125 {
5126 struct tm __tm{};
5127 __tmget.get(__is, {}, __is, __err, &__tm,
5128 __fmt - 2 - (__mod == 'E'), __fmt);
5129 if (!__is_failed(__err))
5130 {
5131 __y = year(__tm.tm_year + 1900);
5132 __m = month(__tm.tm_mon + 1);
5133 __d = day(__tm.tm_mday);
5134 }
5135 }
5136 __parts |= _ChronoParts::_Date;
5137 break;
5138
5139 case 'X': // Locale's time representation.
5140 if (__mod == 'O' || __num) [[unlikely]]
5141 __err |= ios_base::failbit;
5142 else
5143 {
5144 struct tm __tm{};
5145 __tmget.get(__is, {}, __is, __err, &__tm,
5146 __fmt - 2 - (__mod == 'E'), __fmt);
5147 if (!__is_failed(__err))
5148 {
5149 __h = hours(__tm.tm_hour);
5150 __min = minutes(__tm.tm_min);
5151 __s = seconds(__tm.tm_sec);
5152 }
5153 }
5154 __parts |= _ChronoParts::_TimeOfDay;
5155 break;
5156
5157 case 'y': // Last two digits of year.
5158 if (__mod) [[unlikely]]
5159 {
5160 struct tm __tm{};
5161 __tmget.get(__is, {}, __is, __err, &__tm,
5162 __fmt - 3, __fmt);
5163 if (!__is_failed(__err))
5164 {
5165 int __cent = __tm.tm_year < 2000 ? 1900 : 2000;
5166 __yy = year(__tm.tm_year - __cent);
5167 if (__century == -1) // No %C has been parsed yet.
5168 __century = __cent;
5169 }
5170 }
5171 else
5172 {
5173 auto __val = __read_unsigned(__num ? __num : 2);
5174 if (__val >= 0 && __val <= 99)
5175 {
5176 __yy = year(__val);
5177 if (__century == -1) // No %C has been parsed yet.
5178 __century = __val < 69 ? 2000 : 1900;
5179 }
5180 else
5181 __y = __yy = __iso_yy = __iso_y = __bad_y;
5182 }
5183 __parts |= _ChronoParts::_Year;
5184 break;
5185
5186 case 'Y': // Year
5187 if (__mod == 'O') [[unlikely]]
5188 __err |= ios_base::failbit;
5189 else if (__mod == 'E')
5190 {
5191 struct tm __tm{};
5192 __tmget.get(__is, {}, __is, __err, &__tm,
5193 __fmt - 3, __fmt);
5194 if (!__is_failed(__err))
5195 __y = year(__tm.tm_year);
5196 }
5197 else
5198 {
5199 auto __val = __read_unsigned(__num ? __num : 4);
5200 if (!__is_failed(__err))
5201 __y = year(__val);
5202 }
5203 __parts |= _ChronoParts::_Year;
5204 break;
5205
5206 case 'z':
5207 if (__num) [[unlikely]]
5208 __err |= ios_base::failbit;
5209 else
5210 {
5211 // For %Ez and %Oz read [+|-][h]h[:mm].
5212 // For %z read [+|-]hh[mm].
5213
5214 auto __i = __is.peek();
5215 if (_Traits::eq_int_type(__i, _Traits::eof()))
5216 {
5218 break;
5219 }
5220 _CharT __ic = _Traits::to_char_type(__i);
5221 const bool __neg = __ic == _CharT('-');
5222 if (__ic == _CharT('-') || __ic == _CharT('+'))
5223 (void) __is.get();
5224
5225 int_least32_t __hh;
5226 if (__mod)
5227 {
5228 // Read h[h]
5229 __hh = __read_unsigned(2);
5230 }
5231 else
5232 {
5233 // Read hh
5234 __hh = 10 * _S_try_read_digit(__is, __err);
5235 __hh += _S_try_read_digit(__is, __err);
5236 }
5237
5238 if (__is_failed(__err))
5239 break;
5240
5241 __i = __is.peek();
5242 if (_Traits::eq_int_type(__i, _Traits::eof()))
5243 {
5244 __err |= ios_base::eofbit;
5245 __tz_offset = minutes(__hh * (__neg ? -60 : 60));
5246 break;
5247 }
5248 __ic = _Traits::to_char_type(__i);
5249
5250 bool __read_mm = false;
5251 if (__mod)
5252 {
5253 if (__ic == _GLIBCXX_WIDEN(":")[0])
5254 {
5255 // Read [:mm] part.
5256 (void) __is.get();
5257 __read_mm = true;
5258 }
5259 }
5260 else if (_CharT('0') <= __ic && __ic <= _CharT('9'))
5261 {
5262 // Read [mm] part.
5263 __read_mm = true;
5264 }
5265
5266 int_least32_t __mm = 0;
5267 if (__read_mm)
5268 {
5269 __mm = 10 * _S_try_read_digit(__is, __err);
5270 __mm += _S_try_read_digit(__is, __err);
5271 }
5272
5273 if (!__is_failed(__err))
5274 {
5275 auto __z = __hh * 60 + __mm;
5276 __tz_offset = minutes(__neg ? -__z : __z);
5277 }
5278 }
5279 break;
5280
5281 case 'Z':
5282 if (__mod || __num) [[unlikely]]
5283 __err |= ios_base::failbit;
5284 else
5285 {
5286 basic_string_view<_CharT> __x = _GLIBCXX_WIDEN("_/-+");
5287 __tz_abbr.clear();
5288 while (true)
5289 {
5290 auto __i = __is.peek();
5291 if (!_Traits::eq_int_type(__i, _Traits::eof()))
5292 {
5293 _CharT __a = _Traits::to_char_type(__i);
5294 if (std::isalnum(__a, __loc)
5295 || __x.find(__a) != __x.npos)
5296 {
5297 __tz_abbr.push_back(__a);
5298 (void) __is.get();
5299 continue;
5300 }
5301 }
5302 else
5303 __err |= ios_base::eofbit;
5304 break;
5305 }
5306 if (__tz_abbr.empty())
5307 __err |= ios_base::failbit;
5308 }
5309 break;
5310
5311 case 'n': // Exactly one whitespace character.
5312 if (__mod || __num) [[unlikely]]
5313 __err |= ios_base::failbit;
5314 else
5315 {
5316 _CharT __i = __is.peek();
5317 if (_Traits::eq_int_type(__i, _Traits::eof()))
5319 else if (std::isspace(_Traits::to_char_type(__i), __loc))
5320 (void) __is.get();
5321 else
5322 __err |= ios_base::failbit;
5323 }
5324 break;
5325
5326 case 't': // Zero or one whitespace characters.
5327 if (__mod || __num) [[unlikely]]
5328 __err |= ios_base::failbit;
5329 else
5330 {
5331 _CharT __i = __is.peek();
5332 if (_Traits::eq_int_type(__i, _Traits::eof()))
5333 __err |= ios_base::eofbit;
5334 else if (std::isspace(_Traits::to_char_type(__i), __loc))
5335 (void) __is.get();
5336 }
5337 break;
5338
5339 case '%': // A % character.
5340 if (__mod || __num) [[unlikely]]
5341 __err |= ios_base::failbit;
5342 else
5343 __read_chr('%');
5344 break;
5345
5346 case 'O': // Modifiers
5347 case 'E':
5348 if (__mod || __num) [[unlikely]]
5349 {
5350 __err |= ios_base::failbit;
5351 break;
5352 }
5353 __mod = __c;
5354 continue;
5355
5356 default:
5357 if (_CharT('1') <= __c && __c <= _CharT('9'))
5358 {
5359 if (!__mod) [[likely]]
5360 {
5361 // %Nx - extract positive decimal integer N
5362 auto __end = __fmt + _Traits::length(__fmt);
5363 auto [__v, __ptr]
5364 = __format::__parse_integer(__fmt - 1, __end);
5365 if (__ptr) [[likely]]
5366 {
5367 __num = __v;
5368 __fmt = __ptr;
5369 continue;
5370 }
5371 }
5372 }
5373 __err |= ios_base::failbit;
5374 }
5375
5376 if (__is_failed(__err)) [[unlikely]]
5377 break;
5378
5379 __is_flag = false;
5380 __num = 0;
5381 __mod = _CharT();
5382 }
5383
5384 if (__century >= 0)
5385 {
5386 if (__yy != __bad_y && __y == __bad_y)
5387 __y = years(__century) + __yy; // Use %y instead of %Y
5388 if (__iso_yy != __bad_y && __iso_y == __bad_y)
5389 __iso_y = years(__century) + __iso_yy; // Use %g instead of %G
5390 }
5391
5392 bool __can_use_doy = false;
5393 bool __can_use_iso_wk = false;
5394 bool __can_use_sun_wk = false;
5395 bool __can_use_mon_wk = false;
5396
5397 // A year + day-of-year can be converted to a full date.
5398 if (__y != __bad_y && __dayofyear >= 0)
5399 {
5400 __can_use_doy = true;
5401 __parts |= _ChronoParts::_Date;
5402 }
5403 else if (__y != __bad_y && __wday != __bad_wday && __sunday_wk >= 0)
5404 {
5405 __can_use_sun_wk = true;
5406 __parts |= _ChronoParts::_Date;
5407 }
5408 else if (__y != __bad_y && __wday != __bad_wday && __monday_wk >= 0)
5409 {
5410 __can_use_mon_wk = true;
5411 __parts |= _ChronoParts::_Date;
5412 }
5413 else if (__iso_y != __bad_y && __wday != __bad_wday && __iso_wk > 0)
5414 {
5415 // An ISO week date can be converted to a full date.
5416 __can_use_iso_wk = true;
5417 __parts |= _ChronoParts::_Date;
5418 }
5419
5420 if (__is_failed(__err)) [[unlikely]]
5421 ; // Don't bother doing any more work.
5422 else if (__is_flag) [[unlikely]] // incomplete format flag
5423 __err |= ios_base::failbit;
5424 else if ((_M_need & __parts) == _M_need) [[likely]]
5425 {
5426 // We try to avoid calculating _M_sys_days and _M_ymd unless
5427 // necessary, because converting sys_days to year_month_day
5428 // (or vice versa) requires non-trivial calculations.
5429 // If we have y/m/d values then use them to populate _M_ymd
5430 // and only convert it to _M_sys_days if the caller needs that.
5431 // But if we don't have y/m/d and need to calculate the date
5432 // from the day-of-year or a week+weekday then we set _M_sys_days
5433 // and only convert it to _M_ymd if the caller needs that.
5434
5435 // We do more error checking here, but only for the fields that
5436 // we actually need to use. For example, we will not diagnose
5437 // an invalid dayofyear==366 for non-leap years unless actually
5438 // using __dayofyear. This should mean we never produce invalid
5439 // results, but it means not all invalid inputs are diagnosed,
5440 // e.g. "2023-01-01 366" >> "%F %j" ignores the invalid 366.
5441 // We also do not diagnose inconsistent values for the same
5442 // field, e.g. "2021 2022 2023" >> "%C%y %Y %Y" just uses 2023.
5443
5444 // Whether the caller wants _M_wd.
5445 // The _Weekday bit is only set for chrono::weekday.
5446 const bool __need_wday = (_M_need & _ChronoParts::_Weekday) != 0;
5447
5448 // Whether the caller wants _M_sys_days and _M_time.
5449 // Only true for durations and time_points.
5450 const bool __need_time = (_M_need & _ChronoParts::_TimeOfDay) != 0;
5451
5452 if (__need_wday && __wday != __bad_wday)
5453 _M_wd = __wday; // Caller only wants a weekday and we have one.
5454 else if ((_M_need & _ChronoParts::_Date) != 0) // subsumes __need_wday
5455 {
5456 // Whether the caller wants _M_ymd.
5457 // True for chrono::year etc., false for time_points.
5458 const bool __need_ymd = !__need_wday && !__need_time;
5459
5460 if (((_M_need & _ChronoParts::_Year) != 0 && __y == __bad_y)
5461 || ((_M_need & _ChronoParts::_Month) != 0 && __m == __bad_mon)
5462 || ((_M_need & _ChronoParts::_Day) != 0 && __d == __bad_day))
5463 {
5464 // Missing at least one of y/m/d so calculate sys_days
5465 // from the other data we have available.
5466
5467 if (__can_use_doy)
5468 {
5469 if ((0 < __dayofyear && __dayofyear <= 365)
5470 || (__dayofyear == 366 && __y.is_leap()))
5471 [[likely]]
5472 {
5473 _M_sys_days = sys_days(__y/January/1)
5474 + days(__dayofyear - 1);
5475 if (__need_ymd)
5476 _M_ymd = year_month_day(_M_sys_days);
5477 }
5478 else
5479 __err |= ios_base::failbit;
5480 }
5481 else if (__can_use_iso_wk)
5482 {
5483 // Calculate y/m/d from ISO week date.
5484
5485 if (__iso_wk == 53)
5486 {
5487 // A year has 53 weeks iff Jan 1st is a Thursday
5488 // or Jan 1 is a Wednesday and it's a leap year.
5489 const sys_days __jan4(__iso_y/January/4);
5490 weekday __wd1(__jan4 - days(3));
5491 if (__wd1 != Thursday)
5492 if (__wd1 != Wednesday || !__iso_y.is_leap())
5493 __err |= ios_base::failbit;
5494 }
5495
5496 if (!__is_failed(__err)) [[likely]]
5497 {
5498 // First Thursday is always in week one:
5499 sys_days __w(Thursday[1]/January/__iso_y);
5500 // First day of week-based year:
5501 __w -= Thursday - Monday;
5502 __w += days(weeks(__iso_wk - 1));
5503 __w += __wday - Monday;
5504 _M_sys_days = __w;
5505
5506 if (__need_ymd)
5507 _M_ymd = year_month_day(_M_sys_days);
5508 }
5509 }
5510 else if (__can_use_sun_wk)
5511 {
5512 // Calculate y/m/d from week number + weekday.
5513 sys_days __wk1(__y/January/Sunday[1]);
5514 _M_sys_days = __wk1 + weeks(__sunday_wk - 1)
5515 + days(__wday.c_encoding());
5516 _M_ymd = year_month_day(_M_sys_days);
5517 if (_M_ymd.year() != __y) [[unlikely]]
5518 __err |= ios_base::failbit;
5519 }
5520 else if (__can_use_mon_wk)
5521 {
5522 // Calculate y/m/d from week number + weekday.
5523 sys_days __wk1(__y/January/Monday[1]);
5524 _M_sys_days = __wk1 + weeks(__monday_wk - 1)
5525 + days(__wday.c_encoding() - 1);
5526 _M_ymd = year_month_day(_M_sys_days);
5527 if (_M_ymd.year() != __y) [[unlikely]]
5528 __err |= ios_base::failbit;
5529 }
5530 else // Should not be able to get here.
5531 __err |= ios_base::failbit;
5532 }
5533 else
5534 {
5535 // We know that all fields the caller needs are present,
5536 // but check that their values are in range.
5537 // Make unwanted fields valid so that _M_ymd.ok() is true.
5538
5539 if ((_M_need & _ChronoParts::_Year) != 0)
5540 {
5541 if (!__y.ok()) [[unlikely]]
5542 __err |= ios_base::failbit;
5543 }
5544 else if (__y == __bad_y)
5545 __y = 1972y; // Leap year so that Feb 29 is valid.
5546
5547 if ((_M_need & _ChronoParts::_Month) != 0)
5548 {
5549 if (!__m.ok()) [[unlikely]]
5550 __err |= ios_base::failbit;
5551 }
5552 else if (__m == __bad_mon)
5553 __m = January;
5554
5555 if ((_M_need & _ChronoParts::_Day) != 0)
5556 {
5557 if (__d < day(1) || __d > (__y/__m/last).day())
5558 __err |= ios_base::failbit;
5559 }
5560 else if (__d == __bad_day)
5561 __d = 1d;
5562
5563 if (year_month_day __ymd(__y, __m, __d); __ymd.ok())
5564 {
5565 _M_ymd = __ymd;
5566 if (__need_wday || __need_time)
5567 _M_sys_days = sys_days(_M_ymd);
5568 }
5569 else [[unlikely]]
5570 __err |= ios_base::failbit;
5571 }
5572
5573 if (__need_wday)
5574 _M_wd = weekday(_M_sys_days);
5575 }
5576
5577 // Need to set _M_time for both durations and time_points.
5578 if (__need_time)
5579 {
5580 if (__h == __bad_h && __h12 != __bad_h)
5581 {
5582 if (__ampm == 1)
5583 __h = __h12 == hours(12) ? hours(0) : __h12;
5584 else if (__ampm == 2)
5585 __h = __h12 == hours(12) ? __h12 : __h12 + hours(12);
5586 else [[unlikely]]
5587 __err |= ios_base::failbit;
5588 }
5589
5590 auto __t = _M_time.zero();
5591 bool __ok = false;
5592
5593 if (__h != __bad_h)
5594 {
5595 __ok = true;
5596 __t += __h;
5597 }
5598
5599 if (__min != __bad_min)
5600 {
5601 __ok = true;
5602 __t += __min;
5603 }
5604
5605 if (__s != __bad_sec)
5606 {
5607 __ok = true;
5608 __t += __s;
5609 _M_is_leap_second = __s >= seconds(60);
5610 }
5611
5612 if (__ok)
5613 _M_time = __t;
5614 else
5615 __err |= ios_base::failbit;
5616 }
5617
5618 if (!__is_failed(__err)) [[likely]]
5619 {
5620 if (__offset && __tz_offset != __bad_min)
5621 *__offset = __tz_offset;
5622 if (__abbrev && !__tz_abbr.empty())
5623 *__abbrev = std::move(__tz_abbr);
5624 }
5625 }
5626 else
5627 __err |= ios_base::failbit;
5628 }
5629 if (__err)
5630 __is.setstate(__err);
5631 return __is;
5632 }
5633 /// @endcond
5634#undef _GLIBCXX_WIDEN
5635
5636 /// @} group chrono
5637} // namespace chrono
5638
5639_GLIBCXX_END_NAMESPACE_VERSION
5640} // namespace std
5641
5642#endif // C++20
5643
5644#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:178
duration< int64_t, ratio< 604800 > > weeks
weeks
Definition chrono.h:914
constexpr __enable_if_is_duration< _ToDur > floor(const duration< _Rep, _Period > &__d)
Definition chrono.h:392
constexpr enable_if_t< __and_< __is_duration< _ToDur >, __not_< treat_as_floating_point< typename _ToDur::rep > > >::value, _ToDur > round(const duration< _Rep, _Period > &__d)
Definition chrono.h:437
duration< int64_t, ratio< 86400 > > days
days
Definition chrono.h:911
duration< int64_t, ratio< 31556952 > > years
years
Definition chrono.h:917
duration< int64_t, ratio< 3600 > > hours
hours
Definition chrono.h:907
duration< int64_t, ratio< 60 > > minutes
minutes
Definition chrono.h:904
basic_ostream< _CharT, _Traits > & operator<<(std::basic_ostream< _CharT, _Traits > &__os, const duration< _Rep, _Period > &__d)
Definition chrono_io.h:129
duration< int64_t > seconds
seconds
Definition chrono.h:901
constexpr __enable_if_is_duration< _ToDur > duration_cast(const duration< _Rep, _Period > &__d)
Definition chrono.h:279
constexpr complex< _Tp > operator-(const complex< _Tp > &__x, const complex< _Tp > &__y)
Return new complex value x minus y.
Definition complex:404
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
Definition move.h:138
constexpr _Tp * __addressof(_Tp &__r) noexcept
Same as C++11 std::addressof.
Definition move.h:52
constexpr const _Tp & min(const _Tp &, const _Tp &)
This does what you think it does.
const _Facet & use_facet(const locale &__loc)
Return a facet.
ISO C++ entities toplevel namespace is std.
ptrdiff_t streamsize
Integral type for I/O operation counts and buffer sizes.
Definition postypes.h:73
chars_format
floating-point format for primitive numerical conversion
Definition charconv:631
bool isspace(_CharT __c, const locale &__loc)
Convenience interface to ctype.is(ctype_base::space, __c).
_CharT toupper(_CharT __c, const locale &__loc)
Convenience interface to ctype.toupper(__c).
bool isalnum(_CharT __c, const locale &__loc)
Convenience interface to ctype.is(ctype_base::alnum, __c).
ios_base & dec(ios_base &__base)
Calls base.setf(ios_base::dec, ios_base::basefield).
Definition ios_base.h:1094
ios_base & skipws(ios_base &__base)
Calls base.setf(ios_base::skipws).
Definition ios_base.h:1020
constexpr bitset< _Nb > operator|(const bitset< _Nb > &__x, const bitset< _Nb > &__y) noexcept
Global bitwise operations on bitsets.
Definition bitset:1607
basic_istream< _CharT, _Traits > & ws(basic_istream< _CharT, _Traits > &__is)
Quick and easy way to eat whitespace.
Definition istream.tcc:1078
constexpr bitset< _Nb > operator&(const bitset< _Nb > &__x, const bitset< _Nb > &__y) noexcept
Global bitwise operations on bitsets.
Definition bitset:1597
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:562
ISO C++ 2011 namespace for date and time utilities.
locale imbue(const locale &__loc)
Moves to a new locale.
Template class basic_istream.
Definition istream:63
Template class basic_ostream.
Definition ostream.h:67
Controlling output for std::string.
Definition sstream:872
Controlling input and output for std::string.
Definition sstream:1138
Provides output iterator semantics for streambufs.
Provides compile-time rational arithmetic.
Definition ratio:272
A non-owning reference to a string.
Definition string_view:109
Managing sequences of characters and character-like objects.
constexpr iterator begin() noexcept
chrono::duration represents a distance between two points in time
Definition chrono.h:516
_Ios_Iostate iostate
This is a bitmask type.
Definition ios_base.h:453
streamsize precision() const
Flags access.
Definition ios_base.h:765
fmtflags flags() const
Access to format flags.
Definition ios_base.h:694
static const iostate eofbit
Indicates that an input operation reached the end of an input sequence.
Definition ios_base.h:460
static const iostate goodbit
Indicates all is well.
Definition ios_base.h:468
locale getloc() const
Locale access.
Definition ios_base.h:841
static const iostate failbit
Indicates that an input operation failed to read the expected characters, or that an output operation...
Definition ios_base.h:465
static const locale & classic()
Return reference to the C locale.