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 [[__gnu__::__always_inline__]]
483 _ChronoParts
484 _M_fill_aux(chrono::local_days __ld, _ChronoParts __parts)
485 {
486 using namespace chrono;
487 if ((__parts & _ChronoParts::_Weekday) != 0)
488 _M_weekday = weekday(__ld);
489 __parts -= _ChronoParts::_Weekday;
490 if ((__parts & _ChronoParts::_DayOfYear) != 0)
491 // See "Calculating Ordinal Dates" at
492 // https://github.com/HowardHinnant/date/wiki/Examples-and-Recipes
493 _M_day_of_year = __ld - local_days(_M_year/January/0);
494 __parts -= _ChronoParts::_DayOfYear;
495 return __parts;
496 }
497
498 [[__gnu__::__always_inline__]]
499 _ChronoParts
500 _M_fill_ldays(chrono::local_days __ld, _ChronoParts __parts)
501 {
502 _M_ldays = __ld;
503 __parts -= _ChronoParts::_LocalDays;
504 return _M_fill_aux(__ld, __parts);
505 }
506
507 void
508 _M_fill_time(chrono::seconds __d)
509 {
510 chrono::hh_mm_ss<chrono::seconds> __hms(__d);
511 _M_hours = __hms.hours();
512 _M_minutes = __hms.minutes();
513 _M_seconds = __hms.seconds();
514 }
515
516 void
517 _M_fill_date_time(chrono::local_seconds __ls, _ChronoParts __parts)
518 {
519 _M_ldays = chrono::floor<chrono::days>(__ls);
520 __parts -= _ChronoParts::_LocalDays;
521 if ((__parts & _ChronoParts::_HoursMinutesSeconds) != 0)
522 _M_fill_time(_M_lseconds - _M_ldays);
523
524 if ((__parts & _ChronoParts::_Date) != 0)
525 {
526 const chrono::year_month_day __ymd(_M_ldays);
527 _M_fill_year_month(__ymd, __parts);
528 _M_fill_day(__ymd.day(), __parts);
529 _M_fill_aux(_M_ldays, __parts);
530 }
531 }
532
533 void
534 _M_fill_zone(const char* __abbrev, const wchar_t* __wabbrev)
535 {
536 if constexpr (is_same_v<_CharT, char>)
537 _M_zone_abbrev = __abbrev;
538 else
539 _M_zone_abbrev = __wabbrev;
540 _M_zone_cstr = __abbrev;
541 }
542
543 [[__gnu__::__always_inline__]]
544 void
545 _M_fill_utc_zone()
546 { _M_fill_zone("UTC", L"UTC"); }
547 };
548
549 // TODO rename this to chrono::__formatter? or chrono::__detail::__formatter?
550 template<typename _CharT>
551 struct __formatter_chrono
552 {
553 using __string_view = basic_string_view<_CharT>;
554 using __string = basic_string<_CharT>;
555
556 __formatter_chrono() = default;
557
558 constexpr explicit
559 __formatter_chrono(_ChronoSpec<_CharT> __spec) noexcept
560 : _M_spec(__spec)
561 { }
562
563 template<typename _ParseContext>
564 constexpr typename _ParseContext::iterator
565 _M_parse(_ParseContext& __pc, _ChronoParts __parts,
566 const _ChronoSpec<_CharT>& __def)
567 {
568 auto __first = __pc.begin();
569 auto __last = __pc.end();
570
571 _ChronoSpec<_CharT> __spec = __def;
572
573 auto __finalize = [this, &__spec, &__def] {
574 using enum _ChronoParts;
575 _ChronoParts __checked
576 = __spec._M_debug ? _YearMonthDay|_IndexedWeekday
577 : _Month|_Weekday;
578 // n.b. for calendar types __def._M_needed contains only parts
579 // copied from the input, remaining ones are computed, and thus ok
580 __spec._M_needs_ok_check
581 = __spec._M_needs(__def._M_needed & __checked);
582 _M_spec = __spec;
583 };
584
585 auto __finished = [&] {
586 if (__first == __last || *__first == '}')
587 {
588 __finalize();
589 return true;
590 }
591 return false;
592 };
593
594 if (__finished())
595 return __first;
596
597 __first = __spec._M_parse_fill_and_align(__first, __last);
598 if (__finished())
599 return __first;
600
601 __first = __spec._M_parse_width(__first, __last, __pc);
602 if (__finished())
603 return __first;
604
605 if (*__first == '.')
606 {
607 if ((__parts & _ChronoParts::_EpochUnits) == 0
608 || !__spec._M_floating_point_rep)
609 __throw_format_error("format error: invalid precision for duration");
610
611 // Precision is allowed, but value is ignored.
612 __first = _Spec<_CharT>()._M_parse_precision(__first, __last, __pc);
613 // Still inditate that there was user supplied precision.
614 __spec._M_prec_kind = _WP_value;
615 if (__finished())
616 return __first;
617 }
618
619 __spec._M_localized = false;
620 __first = __spec._M_parse_locale(__first, __last);
621 if (__finished())
622 return __first;
623
624 // Everything up to the end of the string or the first '}' is a
625 // chrono-specs string. Check it is valid.
626 {
627 __string_view __str(__first, __last - __first);
628 auto __end = __str.find('}');
629 if (__end != __str.npos)
630 {
631 __str.remove_suffix(__str.length() - __end);
632 __last = __first + __end;
633 }
634 if (__str.find('{') != __str.npos)
635 __throw_format_error("chrono format error: '{' in chrono-specs");
636 }
637
638 // Parse chrono-specs in [first,last), checking each conversion-spec
639 // against __parts (so fail for %Y if no year in parts).
640 // Save range in __spec._M_chrono_specs.
641 __spec._M_debug = false;
642 __spec._M_locale_specific = false;
643 __spec._M_needed = _ChronoParts::_None;
644 __spec._M_chrono_specs = __string_view();
645
646 const auto __chrono_specs = __first++; // Skip leading '%'
647 if (*__chrono_specs != '%')
648 __throw_format_error("chrono format error: no '%' at start of "
649 "chrono-specs");
650
651 _CharT __mod{};
652 bool __conv = true;
653 while (__first != __last)
654 {
655 enum _Mods { _Mod_none, _Mod_E, _Mod_O, _Mod_E_O };
656 _Mods __allowed_mods = _Mod_none;
657
658 _ChronoParts __needed = _ChronoParts::_None;
659 bool __locale_specific = false;
660
661 _CharT __c = *__first++;
662 switch (__c)
663 {
664 using enum _ChronoParts;
665 case 'a':
666 case 'A':
667 __needed = _Weekday;
668 __locale_specific = true;
669 break;
670 case 'b':
671 case 'h':
672 case 'B':
673 __needed = _Month;
674 __locale_specific = true;
675 break;
676 case 'c':
677 __needed = _Date|_HoursMinutesSeconds;
678 __allowed_mods = _Mod_E;
679 __locale_specific = true;
680 break;
681 case 'C':
682 __needed = _Year;
683 __allowed_mods = _Mod_E;
684 break;
685 case 'd':
686 case 'e':
687 __needed = _Day;
688 __allowed_mods = _Mod_O;
689 break;
690 case 'D':
691 case 'F':
692 __needed = _YearMonthDay;
693 break;
694 case 'g':
695 case 'G':
696 __needed = _LocalDays|_Weekday;
697 break;
698 case 'H':
699 case 'I':
700 __needed = _HoursMinutesSeconds;
701 __allowed_mods = _Mod_O;
702 break;
703 case 'j':
704 __needed = __parts & _DayOfYear;
705 // If we do not know day-of-year then we must have a duration,
706 // which is to be formatted as decimal number of days.
707 if (__needed == _None)
708 __needed = _HoursMinutesSeconds;
709 break;
710 case 'm':
711 __needed = _Month;
712 __allowed_mods = _Mod_O;
713 break;
714 case 'M':
715 __needed = _HoursMinutesSeconds;
716 __allowed_mods = _Mod_O;
717 break;
718 case 'p':
719 case 'r':
720 __locale_specific = true;
721 [[fallthrough]];
722 case 'R':
723 __needed = _HoursMinutesSeconds;
724 break;
725 case 'T':
726 __needed = _TimeOfDay;
727 break;
728 case 'q':
729 __needed = _UnitSuffix;
730 break;
731 case 'Q':
732 __needed = _EpochUnits;
733 break;
734 case 'S':
735 __needed = _TimeOfDay;
736 __allowed_mods = _Mod_O;
737 break;
738 case 'u':
739 case 'w':
740 __needed = _Weekday;
741 __allowed_mods = _Mod_O;
742 break;
743 case 'U':
744 case 'V':
745 case 'W':
746 __needed = _LocalDays|_Year|_DayOfYear|_Weekday;
747 __allowed_mods = _Mod_O;
748 break;
749 case 'x':
750 __needed = _Date;
751 __locale_specific = true;
752 __allowed_mods = _Mod_E;
753 break;
754 case 'X':
755 __needed = _HoursMinutesSeconds;
756 __locale_specific = true;
757 __allowed_mods = _Mod_E;
758 break;
759 case 'y':
760 __needed = _Year;
761 __allowed_mods = _Mod_E_O;
762 break;
763 case 'Y':
764 __needed = _Year;
765 __allowed_mods = _Mod_E;
766 break;
767 case 'z':
768 __needed = _ZoneOffset;
769 __allowed_mods = _Mod_E_O;
770 break;
771 case 'Z':
772 __needed = _ZoneAbbrev;
773 break;
774 case 'n':
775 case 't':
776 case '%':
777 break;
778 case 'O':
779 case 'E':
780 if (__mod) [[unlikely]]
781 {
782 __allowed_mods = _Mod_none;
783 break;
784 }
785 __mod = __c;
786 continue;
787 default:
788 __throw_format_error("chrono format error: invalid "
789 " specifier in chrono-specs");
790 }
791
792 if ((__mod == 'E' && !(__allowed_mods & _Mod_E))
793 || (__mod == 'O' && !(__allowed_mods & _Mod_O)))
794 __throw_format_error("chrono format error: invalid "
795 " modifier in chrono-specs");
796 if (__mod && __c != 'z')
797 __locale_specific = true;
798 __mod = _CharT();
799
800 // localized formats do not include subseconds
801 if (__locale_specific)
802 __needed -= _ChronoParts::_Subseconds;
803
804 if ((__parts & __needed) != __needed)
805 __throw_format_error("chrono format error: format argument "
806 "does not contain the information "
807 "required by the chrono-specs");
808 __spec._M_needed |= __needed;
809 __spec._M_locale_specific |= __locale_specific;
810
811 // Scan for next '%', ignoring literal-chars before it.
812 size_t __pos = __string_view(__first, __last - __first).find('%');
813 if (__pos == 0)
814 ++__first;
815 else
816 {
817 if (__pos == __string_view::npos)
818 {
819 __first = __last;
820 __conv = false;
821 }
822 else
823 __first += __pos + 1;
824 }
825 }
826
827 // Check for a '%' conversion-spec without a type.
828 if (__conv || __mod != _CharT())
829 __throw_format_error("chrono format error: unescaped '%' in "
830 "chrono-specs");
831
832 __spec._M_chrono_specs
833 = __string_view(__chrono_specs, __first - __chrono_specs);
834
835 __finalize();
836 return __first;
837 }
838
839 // pre: !_M_spec._M_chrono_specs.empty()
840 template<typename _FormatContext>
841 typename _FormatContext::iterator
842 _M_format(const _ChronoData<_CharT>& __t, _FormatContext& __fc) const
843 {
844#if defined _GLIBCXX_USE_NL_LANGINFO_L && __CHAR_BIT__ == 8
845 // _GLIBCXX_RESOLVE_LIB_DEFECTS
846 // 3565. Handling of encodings in localized formatting
847 // of chrono types is underspecified
848 if constexpr (is_same_v<_CharT, char>)
849 if constexpr (__unicode::__literal_encoding_is_utf8())
850 if (_M_spec._M_localized && _M_spec._M_locale_specific)
851 {
852 extern locale __with_encoding_conversion(const locale&);
853
854 // Allocate and cache the necessary state to convert strings
855 // in the locale's encoding to UTF-8.
856 locale __loc = __fc.locale();
857 if (__loc != locale::classic())
858 __fc._M_loc = __with_encoding_conversion(__loc);
859 }
860#endif
861
862 const size_t __padwidth = _M_spec._M_get_width(__fc);
863 if (__padwidth == 0)
864 return _M_format_to(__t, __fc.out(), __fc);
865
866 using _Out = typename _FormatContext::iterator;
867 _Padding_sink<_Out, _CharT> __sink(__fc.out(), __padwidth);
868 _M_format_to(__t, __sink.out(), __fc);
869 return __sink._M_finish(_M_spec._M_align, _M_spec._M_fill);
870 }
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 __out = _M_g_G(__t, std::move(__out), __c == 'G');
1151 break;
1152 case 'H':
1153 case 'I':
1154 __out = _M_H_I(__t._M_hours, __print_sign(), __c);
1155 break;
1156 case 'j':
1157 __out = _M_j(__t, __print_sign());
1158 break;
1159 case 'm':
1160 __out = _M_m(__t._M_month, std::move(__out));
1161 break;
1162 case 'M':
1163 __out = _M_M(__t._M_minutes, __print_sign());
1164 break;
1165 case 'p':
1166 __out = _M_p(__t._M_hours, std::move(__out));
1167 break;
1168 case 'q':
1169 __out = _M_q(__t._M_unit_suffix, std::move(__out));
1170 break;
1171 case 'Q':
1172 __out = _M_Q(__t, __print_sign(), __fc);
1173 break;
1174 case 'r':
1175 __out = _M_r(__t, __print_sign());
1176 break;
1177 case 'R':
1178 case 'X':
1179 __out = _M_R_X(__t, __print_sign(), __c != 'R');
1180 break;
1181 case 'T':
1182 __out = _M_T(__t, __print_sign(), __fc);
1183 break;
1184 case 'S':
1185 __out = _M_S(__t, __print_sign(), __fc, __mod != 'O');
1186 break;
1187 case 'u':
1188 case 'w':
1189 __out = _M_u_w(__t._M_weekday, std::move(__out), __c);
1190 break;
1191 case 'U':
1192 case 'V':
1193 case 'W':
1194 __out = _M_U_V_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(const _ChronoData<_CharT>& __t, _OutIter __out,
1445 bool __full) const
1446 {
1447 // %g last two decimal digits of the ISO week-based year.
1448 // %G ISO week-based year.
1449 using namespace chrono;
1450 auto __d = __t._M_ldays;
1451 // Move to nearest Thursday:
1452 __d -= (__t._M_weekday - Monday) - days(3);
1453 // ISO week-based year is the year that contains that Thursday:
1454 year __y = year_month_day(__d).year();
1455 return _M_C_y_Y(__y, std::move(__out), "yY"[__full]);
1456 }
1457
1458 template<typename _OutIter>
1459 _OutIter
1460 _M_H_I(chrono::hours __h, _OutIter __out, _CharT __conv) const
1461 {
1462 // %H The hour (24-hour clock) as a decimal number.
1463 // %OH Locale's alternative representation.
1464 // %I The hour (12-hour clock) as a decimal number.
1465 // %OI Locale's alternative representation.
1466
1467 int __i = __h.count();
1468
1469 if (__conv == _CharT('I'))
1470 {
1471 __i %= 12;
1472 if (__i == 0)
1473 __i = 12;
1474 }
1475 else if (__i >= 100) [[unlikely]]
1476 return std::format_to(std::move(__out), _S_empty_fs(), __i);
1477
1478 return __format::__write(std::move(__out), _S_two_digits(__i));
1479 }
1480
1481 template<typename _OutIter>
1482 _OutIter
1483 _M_j(const _ChronoData<_CharT>& __t, _OutIter __out) const
1484 {
1485 if (!_M_spec._M_needs(_ChronoParts::_DayOfYear))
1486 {
1487 // Decimal number of days, without padding.
1488 auto __d = chrono::floor<chrono::days>(__t._M_hours).count();
1489 return std::format_to(std::move(__out), _S_empty_fs(), __d);
1490 }
1491
1492 auto __d = __t._M_day_of_year.count();
1493 if (__d >= 1000) [[unlikely]]
1494 return std::format_to(std::move(__out), _S_empty_fs(), __d);
1495
1496 _CharT __buf[3];
1497 return __format::__write(std::move(__out), _S_str_d3(__buf, __d));
1498 }
1499
1500 template<typename _OutIter>
1501 _OutIter
1502 _M_m(chrono::month __m, _OutIter __out) const
1503 {
1504 // %m month as a decimal number.
1505 // %Om Locale's alternative representation.
1506 auto __i = (unsigned)__m;
1507 if (__i == 0 && _M_spec._M_debug) [[unlikely]]
1508 // 0 should not be padded to two digits
1509 return __format::__write(std::move(__out), _S_digit(0));
1510
1511 _CharT __buf[3];
1512 return __format::__write(std::move(__out), _S_str_d2(__buf, __i));
1513 }
1514
1515 template<typename _OutIter>
1516 _OutIter
1517 _M_M(chrono::minutes __m, _OutIter __out) const
1518 {
1519 // %M The minute as a decimal number.
1520 // %OM Locale's alternative representation.
1521
1522 auto __i = __m.count();
1523 return __format::__write(std::move(__out), _S_two_digits(__i));
1524 }
1525
1526 template<typename _OutIter>
1527 _OutIter
1528 _M_p(chrono::hours __h, _OutIter __out) const
1529 {
1530 // %p The locale's equivalent of the AM/PM designations.
1531
1532 _CharT __buf[2];
1533 _S_fill_ampm(__buf, __h);
1534 return __format::__write(std::move(__out), __string_view(__buf, 2));
1535 }
1536
1537 template<typename _OutIter>
1538 _OutIter
1539 _M_q(__string_view __us, _OutIter __out) const
1540 {
1541 // %q The duration's unit suffix
1542 return __format::__write(std::move(__out), __us);
1543 }
1544
1545 template<typename _OutIter, typename _FormatContext>
1546 _OutIter
1547 _M_Q(const _ChronoData<_CharT>& __t, _OutIter __out,
1548 _FormatContext&) const
1549 {
1550 // %Q The duration's numeric value.
1551 return std::vformat_to(std::move(__out), _S_empty_spec, __t._M_ereps);
1552 }
1553
1554 template<typename _OutIter>
1555 _OutIter
1556 _M_r(const _ChronoData<_CharT>& __t, _OutIter __out) const
1557 {
1558 // %r Locale's 12-hour clock time, for C-locale: %I:%M:%S %p
1559 auto __hi = __t._M_hours.count() % 12;
1560 if (__hi == 0)
1561 __hi = 12;
1562
1563 _CharT __buf[11];
1564 __buf[2] = _S_colon;
1565 __buf[5] = _S_colon;
1566 __buf[8] = _S_space;
1567 _S_fill_two_digits(__buf, __hi);
1568 _S_fill_two_digits(__buf + 3, __t._M_minutes.count());
1569 _S_fill_two_digits(__buf + 6, __t._M_seconds.count());
1570 _S_fill_ampm(__buf + 9, __t._M_hours);
1571
1572 return __format::__write(std::move(__out), __string_view(__buf, 11));
1573 }
1574
1575 template<typename _OutIter>
1576 _OutIter
1577 _M_R_X(const _ChronoData<_CharT>& __t, _OutIter __out,
1578 bool __secs) const
1579 {
1580 // %R Equivalent to %H:%M
1581 // %X Locale's time rep, for C-locale: %H:%M:%S (without subseconds)
1582 // %EX Locale's alternative time representation, for C-locale same as above
1583
1584 auto __hi = __t._M_hours.count();
1585
1586 _CharT __buf[8];
1587 __buf[2] = _S_colon;
1588 __buf[5] = _S_colon;
1589 __string_view __sv(__buf, 8);
1590
1591 if (__hi >= 100) [[unlikely]]
1592 {
1593 __out = std::format_to(std::move(__out), _S_empty_fs(), __hi);
1594 __sv.remove_prefix(2);
1595 }
1596 else
1597 _S_fill_two_digits(__buf, __hi);
1598
1599 _S_fill_two_digits(__buf + 3, __t._M_minutes.count());
1600 if (__secs)
1601 _S_fill_two_digits(__buf + 6, __t._M_seconds.count());
1602 else
1603 __sv.remove_suffix(3);
1604
1605 return __format::__write(std::move(__out), __sv);
1606 }
1607
1608 template<typename _OutIter, typename _FormatContext>
1609 _OutIter
1610 _M_S(const _ChronoData<_CharT>& __t, _OutIter __out,
1611 _FormatContext& __ctx, bool __subs = true) const
1612 {
1613 // %S Seconds as a decimal number.
1614 // %OS The locale's alternative representation.
1615 auto __s = __t._M_seconds;
1616
1617 __out = __format::__write(std::move(__out),
1618 _S_two_digits(__s.count()));
1619 if (__subs)
1620 __out = _M_subsecs(__t, std::move(__out), __ctx);
1621 return __out;
1622 }
1623
1624 template<typename _OutIter, typename _FormatContext>
1625 _OutIter
1626 _M_subsecs(const _ChronoData<_CharT>& __t, _OutIter __out,
1627 _FormatContext& __ctx) const
1628 {
1629 unsigned __prec = _M_spec._M_prec_kind != _WP_none
1630 ? _M_spec._M_get_precision(__ctx)
1631 : _M_spec._M_prec;
1632 if (__prec == 0)
1633 return __out;
1634
1635 _CharT __dot = _S_dot;
1636 if (_M_spec._M_localized) [[unlikely]]
1637 {
1638 auto __loc = __ctx.locale();
1639 const auto& __np = use_facet<numpunct<_CharT>>(__loc);
1640 __dot = __np.decimal_point();
1641 }
1642 *__out = __dot;
1643 ++__out;
1644
1645 if (_M_spec._M_floating_point_rep)
1646 {
1647 _Str_sink<_CharT> __sink;
1648 if (_M_spec._M_localized && _M_spec._M_custom_rep) [[unlikely]]
1649 std::vformat_to(__sink.out(), __ctx.locale(),
1650 _GLIBCXX_WIDEN("{1:0.{2}Lf}"), __t._M_ereps);
1651 else
1652 std::vformat_to(__sink.out(),
1653 _GLIBCXX_WIDEN("{1:0.{2}f}"), __t._M_ereps);
1654
1655 auto __sv = __sink.view();
1656 // Skip leading zero and dot
1657 __sv.remove_prefix(2);
1658 return __format::__write(std::move(__out), __sv);
1659 }
1660
1661 constexpr unsigned __max_prec = _ChronoData<_CharT>::_S_max_prec;
1662 constexpr typename _ChronoData<_CharT>::_Attoseconds::rep __pow10t[]
1663 {
1664 1u,
1665 10u, 100u, 1000u,
1666 10'000u, 100'000u, 1000'000u,
1667 10'000'000u, 100'000'000u, 1000'000'000u,
1668 10'000'000'000u, 100'000'000'000u, 1000'000'000'000u,
1669 10'000'000'000'000u, 100'000'000'000'000u, 1000'000'000'000'000u,
1670 10'000'000'000'000'000u, 100'000'000'000'000'000u, 1000'000'000'000'000'000u,
1671 };
1672
1673 auto __subs = __t._M_subseconds.count();
1674 if (__prec < __max_prec)
1675 __subs /= __pow10t[__max_prec - __prec];
1676 else if (__prec > __max_prec)
1677 __prec = __max_prec;
1678
1679 using _FmtStr = _Runtime_format_string<_CharT>;
1680 return std::format_to(__out, _FmtStr(_GLIBCXX_WIDEN("{0:0{1}}")),
1681 __subs, __prec);
1682 }
1683
1684 // %t handled in _M_format
1685
1686 template<typename _OutIter, typename _FormatContext>
1687 _OutIter
1688 _M_T(const _ChronoData<_CharT>& __t, _OutIter __out,
1689 _FormatContext& __ctx) const
1690 {
1691 // %T Equivalent to %H:%M:%S, with subseconds
1692 __out = _M_R_X(__t, std::move(__out), true);
1693 return _M_subsecs(__t, std::move(__out), __ctx);
1694 }
1695
1696 template<typename _OutIter>
1697 _OutIter
1698 _M_u_w(chrono::weekday __wd, _OutIter __out, _CharT __conv) const
1699 {
1700 // %u ISO weekday as a decimal number (1-7), where Monday is 1.
1701 // %Ou Locale's alternative numeric rep.
1702 // %w Weekday as a decimal number (0-6), where Sunday is 0.
1703 // %Ow Locale's alternative numeric rep.
1704 unsigned __wdi = __conv == 'u' ? __wd.iso_encoding()
1705 : __wd.c_encoding();
1706 _CharT __buf[3];
1707 return __format::__write(std::move(__out), _S_str_d1(__buf, __wdi));
1708 }
1709
1710 template<typename _OutIter>
1711 _OutIter
1712 _M_U_V_W(const _ChronoData<_CharT>& __t, _OutIter __out,
1713 _CharT __conv) const
1714 {
1715 // %U Week number of the year as a decimal number, from first Sunday.
1716 // %OU Locale's alternative numeric rep.
1717 // %V ISO week-based week number as a decimal number.
1718 // %OV Locale's alternative numeric rep.
1719 // %W Week number of the year as a decimal number, from first Monday.
1720 // %OW Locale's alternative numeric rep.
1721 using namespace chrono;
1722
1723 auto __d = __t._M_ldays;
1724 local_days __first; // First day of week 1.
1725 if (__conv == 'V') // W01 begins on Monday before first Thursday.
1726 {
1727 // Move to nearest Thursday:
1728 __d -= (__t._M_weekday - Monday) - days(3);
1729 // ISO week of __t is number of weeks since January 1 of the
1730 // same year as that nearest Thursday.
1731 __first = local_days(year_month_day(__d).year()/January/1);
1732 }
1733 else
1734 {
1735 const weekday __weekstart = __conv == 'U' ? Sunday : Monday;
1736 __first = local_days(__t._M_year/January/__weekstart[1]);
1737 }
1738 auto __weeks = chrono::floor<weeks>(__d - __first);
1739 __string_view __sv = _S_two_digits(__weeks.count() + 1);
1740 return __format::__write(std::move(__out), __sv);
1741 }
1742
1743 template<typename _OutIter>
1744 _OutIter
1745 _M_z(chrono::seconds __ts, _OutIter __out, bool __mod = false) const
1746 {
1747 if (__ts == 0s)
1748 {
1749 __string_view __zero
1750 = __mod ? _GLIBCXX_WIDEN("+00:00") : _GLIBCXX_WIDEN("+0000");
1751 return __format::__write(std::move(__out), __zero);
1752 }
1753
1754 chrono::hh_mm_ss<chrono::seconds> __hms(__ts);
1755 unsigned __mo = 3 + __mod;
1756
1757 _CharT __buf[6];
1758 __buf[0] = _S_plus_minus[__hms.is_negative()];
1759 __buf[3] = _S_colon;
1760 _S_fill_two_digits(__buf + 1, __hms.hours().count());
1761 _S_fill_two_digits(__buf + __mo, __hms.minutes().count());
1762
1763 __string_view __sv(__buf, __mo + 2);
1764 return __format::__write(std::move(__out), __sv);
1765 }
1766
1767 template<typename _OutIter>
1768 _OutIter
1769 _M_Z(__string_view __abbrev, _OutIter __out) const
1770 { return __format::__write(std::move(__out), __abbrev); }
1771
1772 // %% handled in _M_format
1773
1774 // A string view of single digit character, "0".."9".
1775 static basic_string_view<_CharT>
1776 _S_digit(int __n) noexcept
1777 {
1778 // Extra 9s avoid past-the-end read on bad input.
1779 return { _GLIBCXX_WIDEN("0123456789999999") + (__n & 0xf), 1 };
1780 }
1781
1782 // A string view of two digit characters, "00".."99".
1783 static basic_string_view<_CharT>
1784 _S_two_digits(int __n) noexcept
1785 {
1786 return {
1787 _GLIBCXX_WIDEN("0001020304050607080910111213141516171819"
1788 "2021222324252627282930313233343536373839"
1789 "4041424344454647484950515253545556575859"
1790 "6061626364656667686970717273747576777879"
1791 "8081828384858687888990919293949596979899"
1792 "9999999999999999999999999999999999999999"
1793 "9999999999999999") + 2 * (__n & 0x7f),
1794 2
1795 };
1796 }
1797
1798 // Fills __buf[0] and __buf[1] with 2 digit value of __n.
1799 [[__gnu__::__always_inline__]]
1800 static void
1801 _S_fill_two_digits(_CharT* __buf, unsigned __n)
1802 {
1803 auto __sv = _S_two_digits(__n);
1804 __buf[0] = __sv[0];
1805 __buf[1] = __sv[1];
1806 }
1807
1808 // Fills __buf[0] and __buf[1] with "AM", "PM" depending on __h.
1809 [[__gnu__::__always_inline__]]
1810 static void
1811 _S_fill_ampm(_CharT* __buf, chrono::hours __h)
1812 {
1813 auto __hi = __h.count();
1814 if (__hi >= 24) [[unlikely]]
1815 __hi %= 24;
1816
1817 constexpr const _CharT* __apm = _GLIBCXX_WIDEN("APM");
1818 __buf[0] = __apm[__hi >= 12];
1819 __buf[1] = __apm[2];
1820 }
1821
1822 // Returns decimal representation of __n.
1823 // Returned string_view may point to __buf.
1824 [[__gnu__::__always_inline__]]
1825 static basic_string_view<_CharT>
1826 _S_str_d1(span<_CharT, 3> __buf, unsigned __n)
1827 {
1828 if (__n < 10) [[likely]]
1829 return _S_digit(__n);
1830 return _S_str_d2(__buf, __n);
1831 }
1832
1833 // Returns decimal representation of __n, padded to 2 digits.
1834 // Returned string_view may point to __buf.
1835 [[__gnu__::__always_inline__]]
1836 static basic_string_view<_CharT>
1837 _S_str_d2(span<_CharT, 3> __buf, unsigned __n)
1838 {
1839 if (__n < 100) [[likely]]
1840 return _S_two_digits(__n);
1841 return _S_str_d3(__buf, __n);
1842 }
1843
1844 // Returns decimal representation of __n, padded to 3 digits.
1845 // Returned string_view points to __buf.
1846 [[__gnu__::__always_inline__]]
1847 static basic_string_view<_CharT>
1848 _S_str_d3(span<_CharT, 3> __buf, unsigned __n)
1849 {
1850 _S_fill_two_digits(__buf.data(), __n / 10);
1851 __buf[2] = _S_chars[__n % 10];
1852 return __string_view(__buf.data(), 3);
1853 }
1854 };
1855
1856 template<typename _CharT>
1857 struct __formatter_duration : private __formatter_chrono<_CharT>
1858 {
1859 template<typename _Rep, typename _Period>
1860 constexpr static auto
1861 _S_subseconds(const chrono::duration<_Rep, _Period>& __d)
1862 {
1863 if constexpr (chrono::treat_as_floating_point_v<_Rep>)
1864 return chrono::duration<_Rep>(__d);
1865 else if constexpr (_Period::den == 1)
1866 return chrono::seconds(0);
1867 else
1868 {
1869 using _Attoseconds = _ChronoData<_CharT>::_Attoseconds;
1870 using _CRep = common_type_t<_Rep, typename _Attoseconds::rep>;
1871 chrono::duration<_CRep, _Period> subs(__d.count());
1872 return chrono::duration_cast<_Attoseconds>(subs);
1873 }
1874 }
1875
1876 public:
1877 template<typename _Duration>
1878 static consteval
1879 _ChronoSpec<_CharT>
1880 _S_spec_for(_ChronoParts __parts)
1881 {
1882 using _Rep = typename _Duration::rep;
1883 using enum _ChronoParts;
1884
1885 _ChronoSpec<_CharT> __res{};
1886 __res._M_floating_point_rep = chrono::treat_as_floating_point_v<_Rep>;
1887 __res._M_custom_rep = !is_arithmetic_v<_Rep>;
1888 __res._M_prec = chrono::hh_mm_ss<_Duration>::fractional_width;
1889 if ((__parts & _TimeOfDay) != 0)
1890 __res._M_localized = __res._M_prec > 0 || __res._M_floating_point_rep;
1891
1892 if ((__parts & _TimeOfDay) != 0)
1893 __res._M_needed |= _TimeOfDay;
1894 if ((__parts & _Date) != 0)
1895 __res._M_needed |= _YearMonthDay;
1896 if ((__parts & _ZoneAbbrev) != 0)
1897 __res._M_needed |= _ZoneAbbrev;
1898
1899 switch (__parts)
1900 {
1901 case _ZonedDateTime:
1902 __res._M_chrono_specs = _ChronoFormats<_CharT>::_S_ftz();
1903 break;
1904 case _DateTime:
1905 __res._M_chrono_specs = _ChronoFormats<_CharT>::_S_ft();
1906 break;
1907 case _Date:
1908 __res._M_chrono_specs = _ChronoFormats<_CharT>::_S_f();
1909 break;
1910 case _Time:
1911 __res._M_chrono_specs = _ChronoFormats<_CharT>::_S_t();
1912 break;
1913 case _None:
1914 break;
1915 default:
1916 __builtin_unreachable();
1917 }
1918 return __res;
1919 };
1920
1921 using __formatter_chrono<_CharT>::__formatter_chrono;
1922 using __formatter_chrono<_CharT>::_M_spec;
1923
1924 template<typename _Duration, typename _ParseContext>
1925 constexpr typename _ParseContext::iterator
1926 _M_parse(_ParseContext& __pc, _ChronoParts __parts,
1927 const _ChronoSpec<_CharT>& __def = {})
1928 {
1929 using _Rep = typename _Duration::rep;
1930 using enum _ChronoParts;
1931
1932 auto __res
1933 = __formatter_chrono<_CharT>::_M_parse(__pc, __parts, __def);
1934 // n.b. durations do not contain date parts, and for time point all
1935 // date parts are computed, and they are always ok.
1936 _M_spec._M_needs_ok_check = false;
1937
1938 // check for custom floating point durations, if digits of output
1939 // will contain subseconds, then formatters must support specifying
1940 // precision.
1941 if constexpr (!is_floating_point_v<_Rep>)
1942 if constexpr (chrono::treat_as_floating_point_v<_Rep>)
1943 if (_M_spec._M_needs(_Subseconds|_EpochUnits)
1944 || _M_spec._M_prec_kind != _WP_none
1945 || _M_spec._M_prec_value > 0)
1946 {
1947 constexpr const _CharT* __fs = _GLIBCXX_WIDEN("#02.5Lf");
1948 basic_format_parse_context<_CharT> __npc(__fs);
1949 formatter<_Rep, _CharT> __fmtter;
1950 __fmtter.parse(__npc);
1951 }
1952 return __res;
1953 }
1954
1955 // Return the formatting locale.
1956 template<typename _FormatContext>
1957 std::locale
1958 _M_locale(_FormatContext& __fc) const
1959 {
1960 if (!_M_spec._M_localized)
1961 return std::locale::classic();
1962 else
1963 return __fc.locale();
1964 }
1965
1966 // Format duration for empty chrono-specs, e.g. "{}" (C++20 [time.format] p6).
1967 template<typename _Rep, typename _Period, typename _FormatContext>
1968 typename _FormatContext::iterator
1969 _M_format_to_ostream(const chrono::duration<_Rep, _Period>& __d,
1970 bool __is_neg,
1971 _FormatContext& __fc) const
1972 {
1973 basic_ostringstream<_CharT> __os;
1974 __os.imbue(this->_M_locale(__fc));
1975
1976 if (__is_neg) [[unlikely]]
1977 __os << this->_S_plus_minus[1];
1978 __os << __d;
1979
1980 auto __str = std::move(__os).str();
1981 return __format::__write_padded_as_spec(__str, __str.size(),
1982 __fc, _M_spec);
1983 }
1984
1985 template<typename _Rep1, typename _Period1,
1986 typename _Rep2, typename _Period2,
1987 typename _FormatContext>
1988 typename _FormatContext::iterator
1989 _M_format_units(_ChronoData<_CharT>& __cd,
1990 const chrono::duration<_Rep1, _Period1>& __ed,
1991 const chrono::duration<_Rep2, _Period2>& __ss,
1992 _FormatContext& __fc) const
1993 {
1994 __format::_Str_sink<_CharT> __suffix_store;
1995 constexpr auto _S_unit_suffix
1996 = chrono::__detail::__units_suffix<_Period1, _CharT>();
1997 if constexpr (!_S_unit_suffix.empty())
1998 __cd._M_unit_suffix = _S_unit_suffix;
1999 else if (_M_spec._M_needs(_ChronoParts::_UnitSuffix))
2000 {
2001 chrono::__detail::
2002 __fmt_units_suffix<_Period1, _CharT>(__suffix_store.out());
2003 __cd._M_unit_suffix = __suffix_store.view();
2004 }
2005
2006 const auto __prec = _M_spec._M_prec_kind != _WP_none
2007 ? _M_spec._M_get_precision(__fc)
2008 : _M_spec._M_prec;
2009
2010 using _ErasedContext = typename _ChronoData<_CharT>::_FormatContext;
2011 // _GLIBCXX_RESOLVE_LIB_DEFECTS
2012 // 4118. How should duration formatters format custom rep?
2013 auto __ereps = +__ed.count();
2014 if (!_M_spec._M_needs(_ChronoParts::_Subseconds))
2015 {
2016 auto __ssreps = 0u;
2017 auto __args_store
2018 = std::make_format_args<_ErasedContext>(__ereps, __ssreps, __prec);
2019 __cd._M_ereps = __args_store;
2020 return this->_M_format(__cd, __fc);
2021 }
2022
2023 using _Attoseconds = _ChronoData<_CharT>::_Attoseconds;
2024 auto __nss = _S_subseconds(__ss);
2025 __cd._M_subseconds = chrono::duration_cast<_Attoseconds>(__nss);
2026
2027 auto __ssreps = __nss.count();
2028 auto __args_store
2029 = std::make_format_args<_ErasedContext>(__ereps, __ssreps, __prec);
2030 __cd._M_ereps = __args_store;
2031
2032 return this->_M_format(__cd, __fc);
2033 }
2034
2035 // pre: __cd._M_lseconds and __cd._M_eseconds are set.
2036 template<typename _Rep1, typename _Period1, typename _FormatContext>
2037 typename _FormatContext::iterator
2038 _M_format_time_point(_ChronoData<_CharT>& __cd,
2039 const chrono::duration<_Rep1, _Period1>& __ed,
2040 _FormatContext& __fc) const
2041 {
2042 auto __parts = _M_spec._M_needed - _ChronoParts::_TotalSeconds;
2043 if ((__parts & _ChronoParts::_DateTime) != 0)
2044 __cd._M_fill_date_time(__cd._M_lseconds, __parts);
2045 return _M_format_units(__cd, __ed, __ed - __cd._M_eseconds, __fc);
2046 }
2047 };
2048
2049#if _GLIBCXX_USE_CXX11_ABI || ! _GLIBCXX_USE_DUAL_ABI
2050 template<typename _CharT>
2051 struct __formatter_chrono_info
2052 {
2053 constexpr typename basic_format_parse_context<_CharT>::iterator
2054 parse(basic_format_parse_context<_CharT>& __pc)
2055 { return _M_f._M_parse(__pc, _ChronoParts(), {}); }
2056
2057 template<typename _Info, typename _Out>
2058 typename basic_format_context<_Out, _CharT>::iterator
2059 format(const _Info& __i,
2060 basic_format_context<_Out, _CharT>& __fc) const
2061 {
2062 // n.b. only acceptable chrono-spec for info is one containing
2063 // only whitespaces and %%, that do not depend on formatted object.
2064 if (!_M_f._M_spec._M_chrono_specs.empty()) [[unlikely]]
2065 return _M_f._M_format(_ChronoData<_CharT>{}, __fc);
2066
2067 const size_t __padwidth = _M_f._M_spec._M_get_width(__fc);
2068 if (__padwidth == 0)
2069 return _M_format_to(__fc.out(), __i);
2070
2071 _Padding_sink<_Out, _CharT> __sink(__fc.out(), __padwidth);
2072 _M_format_to(__sink.out(), __i);
2073 return __sink._M_finish(_M_f._M_spec._M_align, _M_f._M_spec._M_fill);
2074 }
2075
2076 private:
2077 template<typename _Out>
2078 _Out
2079 _M_format_to(_Out __out, const chrono::sys_info& __si) const
2080 {
2081 using _FmtStr = _Runtime_format_string<_CharT>;
2082 // n.b. only decimal separator is locale dependent for specifiers
2083 // used below, as sys_info uses seconds and minutes duration, the
2084 // output is locale-independent.
2085 constexpr auto* __fs
2086 = _GLIBCXX_WIDEN("[{0:%F %T},{1:%F %T},{2:%T},{3:%Q%q},{0:%Z}]");
2087 const chrono::local_seconds __lb(__si.begin.time_since_epoch());
2088 return std::format_to(std::move(__out), _FmtStr(__fs),
2089 chrono::local_time_format(__lb, &__si.abbrev),
2090 __si.end, __si.offset, __si.save);
2091 }
2092
2093 template<typename _Out>
2094 _Out
2095 _M_format_to(_Out __out, const chrono::local_info& __li) const
2096 {
2097 *__out = _Separators<_CharT>::_S_squares()[0];
2098 ++__out;
2099 if (__li.result == chrono::local_info::unique)
2100 __out = _M_format_to(std::move(__out), __li.first);
2101 else
2102 {
2103 basic_string_view<_CharT> __sv;
2104 if (__li.result == chrono::local_info::nonexistent)
2105 __sv =_GLIBCXX_WIDEN("nonexistent");
2106 else
2107 __sv = _GLIBCXX_WIDEN("ambiguous");
2108 __out = __format::__write(std::move(__out), __sv);
2109
2110 __sv = _GLIBCXX_WIDEN(" local time between ");
2111 __out = __format::__write(std::move(__out), __sv);
2112 __out = _M_format_to(std::move(__out), __li.first);
2113
2114 __sv = _GLIBCXX_WIDEN(" and ");
2115 __out = __format::__write(std::move(__out), __sv);
2116 __out = _M_format_to(std::move(__out), __li.second);
2117 }
2118 *__out = _Separators<_CharT>::_S_squares()[1];
2119 ++__out;
2120 return std::move(__out);
2121 }
2122
2123 __formatter_chrono<_CharT> _M_f;
2124 };
2125#endif
2126
2127} // namespace __format
2128/// @endcond
2129
2130 template<typename _Rep, typename _Period, typename _CharT>
2131 requires __format::__formattable_impl<_Rep, _CharT>
2132 struct formatter<chrono::duration<_Rep, _Period>, _CharT>
2133 {
2134 constexpr typename basic_format_parse_context<_CharT>::iterator
2135 parse(basic_format_parse_context<_CharT>& __pc)
2136 {
2137 using enum __format::_ChronoParts;
2138 return _M_f.template _M_parse<_Duration>(__pc, _EpochTime, __defSpec);
2139 }
2140
2141 template<typename _Out>
2142 typename basic_format_context<_Out, _CharT>::iterator
2143 format(const chrono::duration<_Rep, _Period>& __d,
2144 basic_format_context<_Out, _CharT>& __fc) const
2145 {
2146 if constexpr (numeric_limits<_Rep>::is_signed)
2147 if (__d < __d.zero()) [[unlikely]]
2148 {
2149 if constexpr (is_integral_v<_Rep>)
2150 {
2151 // -d is undefined for the most negative integer.
2152 // Convert duration to corresponding unsigned rep.
2153 using _URep = make_unsigned_t<_Rep>;
2154 auto __ucnt = -static_cast<_URep>(__d.count());
2155 auto __ud = chrono::duration<_URep, _Period>(__ucnt);
2156 return _M_format(__ud, true, __fc);
2157 }
2158 else
2159 return _M_format(-__d, true, __fc);
2160 }
2161 return _M_format(__d, false, __fc);
2162 }
2163
2164 private:
2165 using _Duration = chrono::duration<_Rep, _Period>;
2166
2167 static constexpr __format::_ChronoSpec<_CharT> __defSpec = []
2168 {
2169 using enum __format::_ChronoParts;
2170 auto __res = __format::__formatter_duration<_CharT>::
2171 template _S_spec_for<_Duration>(_None);
2172 __res._M_localized = !is_integral_v<_Rep>;
2173 // n.b. for integral format output is the same as ostream output
2174 if constexpr (is_integral_v<_Rep>)
2175 {
2176 __res._M_needed = _EpochUnits|_UnitSuffix;
2177 __res._M_chrono_specs = _GLIBCXX_WIDEN("%Q%q");
2178 }
2179 return __res;
2180 }();
2181
2182 template<typename _Rep2, typename _Out>
2183 typename basic_format_context<_Out, _CharT>::iterator
2184 _M_format(const chrono::duration<_Rep2, _Period>& __d,
2185 bool __is_neg,
2186 basic_format_context<_Out, _CharT>& __fc) const
2187 {
2188 using namespace chrono;
2189 using enum __format::_ChronoParts;
2190 if constexpr (!is_integral_v<_Rep>)
2191 if (_M_f._M_spec._M_chrono_specs.empty())
2192 return _M_f._M_format_to_ostream(__d, __is_neg, __fc);
2193
2194 __format::_ChronoData<_CharT> __cd;
2195 __cd._M_is_neg = __is_neg;
2196 auto __ts = chrono::floor<chrono::seconds>(__d);
2197 __cd._M_eseconds = __ts;
2198 if (_M_f._M_spec._M_needs(_HoursMinutesSeconds))
2199 __cd._M_fill_time(__ts);
2200 return _M_f._M_format_units(__cd, __d, __d - __ts, __fc);
2201 }
2202
2203 __format::__formatter_duration<_CharT> _M_f{__defSpec};
2204 };
2205
2206 template<__format::__char _CharT>
2207 struct formatter<chrono::day, _CharT>
2208 {
2209 constexpr typename basic_format_parse_context<_CharT>::iterator
2210 parse(basic_format_parse_context<_CharT>& __pc)
2211 {
2212 using enum __format::_ChronoParts;
2213 return _M_f._M_parse(__pc, _Day|_WeekdayIndex, __defSpec);
2214 }
2215
2216 template<typename _Out>
2217 typename basic_format_context<_Out, _CharT>::iterator
2218 format(const chrono::day& __t,
2219 basic_format_context<_Out, _CharT>& __fc) const
2220 {
2221 __format::_ChronoData<_CharT> __cd{};
2222 __cd._M_fill_day(__t, __defSpec._M_needed);
2223 return _M_f._M_format(__cd, __fc);
2224 }
2225
2226 private:
2227 static constexpr __format::_ChronoSpec<_CharT> __defSpec = []
2228 {
2229 using __format::_ChronoFormats;
2230 using enum __format::_ChronoParts;
2231
2232 __format::_ChronoSpec<_CharT> __res{};
2233 __res._M_debug = true;
2234 __res._M_needed = _Day;
2235 __res._M_chrono_specs = _ChronoFormats<_CharT>::_S_d();
2236 return __res;
2237 }();
2238
2239 __format::__formatter_chrono<_CharT> _M_f{__defSpec};
2240 };
2241
2242 template<__format::__char _CharT>
2243 struct formatter<chrono::month, _CharT>
2244 {
2245 constexpr typename basic_format_parse_context<_CharT>::iterator
2246 parse(basic_format_parse_context<_CharT>& __pc)
2247 {
2248 using enum __format::_ChronoParts;
2249 return _M_f._M_parse(__pc, _Month, __defSpec);
2250 }
2251
2252 template<typename _Out>
2253 typename basic_format_context<_Out, _CharT>::iterator
2254 format(const chrono::month& __t,
2255 basic_format_context<_Out, _CharT>& __fc) const
2256 {
2257 __format::_ChronoData<_CharT> __cd{};
2258 __cd._M_month = __t;
2259 return _M_f._M_format(__cd, __fc);
2260 }
2261
2262 private:
2263 static constexpr __format::_ChronoSpec<_CharT> __defSpec = []
2264 {
2265 using __format::_ChronoFormats;
2266 using enum __format::_ChronoParts;
2267
2268 __format::_ChronoSpec<_CharT> __res{};
2269 __res._M_debug = true;
2270 __res._M_localized = true;
2271 __res._M_locale_specific = true;
2272 __res._M_needed = _Month;
2273 __res._M_chrono_specs = _ChronoFormats<_CharT>::_S_m();
2274 return __res;
2275 }();
2276
2277 __format::__formatter_chrono<_CharT> _M_f{__defSpec};
2278 };
2279
2280 template<__format::__char _CharT>
2281 struct formatter<chrono::year, _CharT>
2282 {
2283 constexpr typename basic_format_parse_context<_CharT>::iterator
2284 parse(basic_format_parse_context<_CharT>& __pc)
2285 {
2286 using enum __format::_ChronoParts;
2287 return _M_f._M_parse(__pc, _Year, __defSpec);
2288 }
2289
2290 template<typename _Out>
2291 typename basic_format_context<_Out, _CharT>::iterator
2292 format(const chrono::year& __t,
2293 basic_format_context<_Out, _CharT>& __fc) const
2294 {
2295 __format::_ChronoData<_CharT> __cd{};
2296 __cd._M_year = __t;
2297 return _M_f._M_format(__cd, __fc);
2298 }
2299
2300 private:
2301 static constexpr __format::_ChronoSpec<_CharT> __defSpec = []
2302 {
2303 using __format::_ChronoFormats;
2304 using enum __format::_ChronoParts;
2305
2306 __format::_ChronoSpec<_CharT> __res{};
2307 __res._M_debug = true;
2308 __res._M_needed = _Year;
2309 __res._M_chrono_specs = _ChronoFormats<_CharT>::_S_y();
2310 return __res;
2311 }();
2312
2313 __format::__formatter_chrono<_CharT> _M_f{__defSpec};
2314 };
2315
2316 template<__format::__char _CharT>
2317 struct formatter<chrono::weekday, _CharT>
2318 {
2319 constexpr typename basic_format_parse_context<_CharT>::iterator
2320 parse(basic_format_parse_context<_CharT>& __pc)
2321 {
2322 using enum __format::_ChronoParts;
2323 return _M_f._M_parse(__pc, _Weekday, __defSpec);
2324 }
2325
2326 template<typename _Out>
2327 typename basic_format_context<_Out, _CharT>::iterator
2328 format(const chrono::weekday& __t,
2329 basic_format_context<_Out, _CharT>& __fc) const
2330 {
2331 __format::_ChronoData<_CharT> __cd{};
2332 __cd._M_weekday = __t;
2333 return _M_f._M_format(__cd, __fc);
2334 }
2335
2336 private:
2337 static constexpr __format::_ChronoSpec<_CharT> __defSpec = []
2338 {
2339 using __format::_ChronoFormats;
2340 using enum __format::_ChronoParts;
2341
2342 __format::_ChronoSpec<_CharT> __res{};
2343 __res._M_debug = true;
2344 __res._M_localized = true;
2345 __res._M_locale_specific = true;
2346 __res._M_needed = _Weekday;
2347 __res._M_chrono_specs = _ChronoFormats<_CharT>::_S_w();
2348 return __res;
2349 }();
2350
2351 __format::__formatter_chrono<_CharT> _M_f{__defSpec};
2352 };
2353
2354 template<__format::__char _CharT>
2355 struct formatter<chrono::weekday_indexed, _CharT>
2356 {
2357 constexpr typename basic_format_parse_context<_CharT>::iterator
2358 parse(basic_format_parse_context<_CharT>& __pc)
2359 {
2360 using enum __format::_ChronoParts;
2361 return _M_f._M_parse(__pc, _IndexedWeekday, __defSpec);
2362 }
2363
2364 template<typename _Out>
2365 typename basic_format_context<_Out, _CharT>::iterator
2366 format(const chrono::weekday_indexed& __t,
2367 basic_format_context<_Out, _CharT>& __fc) const
2368 {
2369 __format::_ChronoData<_CharT> __cd{};
2370 __cd._M_fill_weekday(__t, __defSpec._M_needed);
2371 return _M_f._M_format(__cd, __fc);
2372 }
2373
2374 private:
2375 static constexpr __format::_ChronoSpec<_CharT> __defSpec = []
2376 {
2377 using __format::_ChronoFormats;
2378 using enum __format::_ChronoParts;
2379
2380 __format::_ChronoSpec<_CharT> __res{};
2381 __res._M_debug = true;
2382 __res._M_localized = true;
2383 __res._M_locale_specific = true;
2384 __res._M_needed = _IndexedWeekday;
2385 __res._M_chrono_specs = _ChronoFormats<_CharT>::_S_wi();
2386 return __res;
2387 }();
2388
2389 __format::__formatter_chrono<_CharT> _M_f{__defSpec};
2390 };
2391
2392 template<__format::__char _CharT>
2393 struct formatter<chrono::weekday_last, _CharT>
2394 {
2395 constexpr typename basic_format_parse_context<_CharT>::iterator
2396 parse(basic_format_parse_context<_CharT>& __pc)
2397 {
2398 using enum __format::_ChronoParts;
2399 return _M_f._M_parse(__pc, _Weekday, __defSpec);
2400 }
2401
2402 template<typename _Out>
2403 typename basic_format_context<_Out, _CharT>::iterator
2404 format(const chrono::weekday_last& __t,
2405 basic_format_context<_Out, _CharT>& __fc) const
2406 {
2407 __format::_ChronoData<_CharT> __cd{};
2408 __cd._M_weekday = __t.weekday();
2409 return _M_f._M_format(__cd, __fc);
2410 }
2411
2412 private:
2413 static constexpr __format::_ChronoSpec<_CharT> __defSpec = []
2414 {
2415 using __format::_ChronoFormats;
2416 using enum __format::_ChronoParts;
2417
2418 __format::_ChronoSpec<_CharT> __res{};
2419 __res._M_debug = true;
2420 __res._M_localized = true;
2421 __res._M_locale_specific = true;
2422 __res._M_needed = _Weekday;
2423 __res._M_chrono_specs = _ChronoFormats<_CharT>::_S_wl();
2424 return __res;
2425 }();
2426
2427 __format::__formatter_chrono<_CharT> _M_f{__defSpec};
2428 };
2429
2430 template<__format::__char _CharT>
2431 struct formatter<chrono::month_day, _CharT>
2432 {
2433 constexpr typename basic_format_parse_context<_CharT>::iterator
2434 parse(basic_format_parse_context<_CharT>& __pc)
2435 {
2436 using enum __format::_ChronoParts;
2437 return _M_f._M_parse(__pc, _Month|_Day|_WeekdayIndex, __defSpec);
2438 }
2439
2440 template<typename _Out>
2441 typename basic_format_context<_Out, _CharT>::iterator
2442 format(const chrono::month_day& __t,
2443 basic_format_context<_Out, _CharT>& __fc) const
2444 {
2445 __format::_ChronoData<_CharT> __cd{};
2446 __cd._M_month = __t.month();
2447 __cd._M_fill_day(__t.day(), __defSpec._M_needed);
2448 return _M_f._M_format(__cd, __fc);
2449 }
2450
2451 private:
2452 static constexpr __format::_ChronoSpec<_CharT> __defSpec = []
2453 {
2454 using __format::_ChronoFormats;
2455 using enum __format::_ChronoParts;
2456
2457 __format::_ChronoSpec<_CharT> __res{};
2458 __res._M_debug = true;
2459 __res._M_localized = true;
2460 __res._M_locale_specific = true;
2461 __res._M_needed = _Month|_Day;
2462 __res._M_chrono_specs = _ChronoFormats<_CharT>::_S_md();
2463 return __res;
2464 }();
2465
2466 __format::__formatter_chrono<_CharT> _M_f{__defSpec};
2467 };
2468
2469 template<__format::__char _CharT>
2470 struct formatter<chrono::month_day_last, _CharT>
2471 {
2472 constexpr typename basic_format_parse_context<_CharT>::iterator
2473 parse(basic_format_parse_context<_CharT>& __pc)
2474 {
2475 using enum __format::_ChronoParts;
2476 return _M_f._M_parse(__pc, _Month, __defSpec);
2477 }
2478
2479 template<typename _Out>
2480 typename basic_format_context<_Out, _CharT>::iterator
2481 format(const chrono::month_day_last& __t,
2482 basic_format_context<_Out, _CharT>& __fc) const
2483 {
2484 __format::_ChronoData<_CharT> __cd{};
2485 __cd._M_month = __t.month();
2486 return _M_f._M_format(__cd, __fc);
2487 }
2488
2489 private:
2490 static constexpr __format::_ChronoSpec<_CharT> __defSpec = []
2491 {
2492 using __format::_ChronoFormats;
2493 using enum __format::_ChronoParts;
2494
2495 __format::_ChronoSpec<_CharT> __res{};
2496 __res._M_debug = true;
2497 __res._M_localized = true;
2498 __res._M_locale_specific = true;
2499 __res._M_needed = _Month;
2500 __res._M_chrono_specs = _ChronoFormats<_CharT>::_S_ml();
2501 return __res;
2502 }();
2503
2504 __format::__formatter_chrono<_CharT> _M_f{__defSpec};
2505 };
2506
2507 template<__format::__char _CharT>
2508 struct formatter<chrono::month_weekday, _CharT>
2509 {
2510 constexpr typename basic_format_parse_context<_CharT>::iterator
2511 parse(basic_format_parse_context<_CharT>& __pc)
2512 {
2513 using enum __format::_ChronoParts;
2514 return _M_f._M_parse(__pc, _Month|_IndexedWeekday, __defSpec);
2515 }
2516
2517 template<typename _Out>
2518 typename basic_format_context<_Out, _CharT>::iterator
2519 format(const chrono::month_weekday& __t,
2520 basic_format_context<_Out, _CharT>& __fc) const
2521 {
2522 __format::_ChronoData<_CharT> __cd{};
2523 __cd._M_month = __t.month();
2524 __cd._M_fill_weekday(__t.weekday_indexed(), __defSpec._M_needed);
2525 return _M_f._M_format(__cd, __fc);
2526 }
2527
2528 private:
2529 static constexpr __format::_ChronoSpec<_CharT> __defSpec = []
2530 {
2531 using __format::_ChronoFormats;
2532 using enum __format::_ChronoParts;
2533
2534 __format::_ChronoSpec<_CharT> __res{};
2535 __res._M_debug = true;
2536 __res._M_localized = true;
2537 __res._M_locale_specific = true;
2538 __res._M_needed = _Month|_IndexedWeekday;
2539 __res._M_chrono_specs = _ChronoFormats<_CharT>::_S_mwi();
2540 return __res;
2541 }();
2542
2543 __format::__formatter_chrono<_CharT> _M_f{__defSpec};
2544 };
2545
2546 template<__format::__char _CharT>
2547 struct formatter<chrono::month_weekday_last, _CharT>
2548 {
2549 constexpr typename basic_format_parse_context<_CharT>::iterator
2550 parse(basic_format_parse_context<_CharT>& __pc)
2551 {
2552 using enum __format::_ChronoParts;
2553 return _M_f._M_parse(__pc, _Month|_Weekday, __defSpec);
2554 }
2555
2556 template<typename _Out>
2557 typename basic_format_context<_Out, _CharT>::iterator
2558 format(const chrono::month_weekday_last& __t,
2559 basic_format_context<_Out, _CharT>& __fc) const
2560 {
2561 __format::_ChronoData<_CharT> __cd{};
2562 __cd._M_month = __t.month();
2563 __cd._M_weekday = __t.weekday_last().weekday();
2564 return _M_f._M_format(__cd, __fc);
2565 }
2566
2567 private:
2568 static constexpr __format::_ChronoSpec<_CharT> __defSpec = []
2569 {
2570 using __format::_ChronoFormats;
2571 using enum __format::_ChronoParts;
2572
2573 __format::_ChronoSpec<_CharT> __res{};
2574 __res._M_debug = true;
2575 __res._M_localized = true;
2576 __res._M_locale_specific = true;
2577 __res._M_needed = _Month|_Weekday;
2578 __res._M_chrono_specs = _ChronoFormats<_CharT>::_S_mwl();
2579 return __res;
2580 }();
2581
2582 __format::__formatter_chrono<_CharT> _M_f{__defSpec};
2583 };
2584
2585 template<__format::__char _CharT>
2586 struct formatter<chrono::year_month, _CharT>
2587 {
2588 constexpr typename basic_format_parse_context<_CharT>::iterator
2589 parse(basic_format_parse_context<_CharT>& __pc)
2590 {
2591 using enum __format::_ChronoParts;
2592 return _M_f._M_parse(__pc, _Year|_Month, __defSpec);
2593 }
2594
2595 template<typename _Out>
2596 typename basic_format_context<_Out, _CharT>::iterator
2597 format(const chrono::year_month& __t,
2598 basic_format_context<_Out, _CharT>& __fc) const
2599 {
2600 __format::_ChronoData<_CharT> __cd{};
2601 __cd._M_fill_year_month(__t, __defSpec._M_needed);
2602 return _M_f._M_format(__cd, __fc);
2603 }
2604
2605 private:
2606 static constexpr __format::_ChronoSpec<_CharT> __defSpec = []
2607 {
2608 using __format::_ChronoFormats;
2609 using enum __format::_ChronoParts;
2610
2611 __format::_ChronoSpec<_CharT> __res{};
2612 __res._M_debug = true;
2613 __res._M_localized = true;
2614 __res._M_locale_specific = true;
2615 __res._M_needed = _Year|_Month;
2616 __res._M_chrono_specs = _ChronoFormats<_CharT>::_S_ym();
2617 return __res;
2618 }();
2619
2620 __format::__formatter_chrono<_CharT> _M_f{__defSpec};
2621 };
2622
2623 template<__format::__char _CharT>
2624 struct formatter<chrono::year_month_day, _CharT>
2625 {
2626 constexpr typename basic_format_parse_context<_CharT>::iterator
2627 parse(basic_format_parse_context<_CharT>& __pc)
2628 {
2629 using enum __format::_ChronoParts;
2630 return _M_f._M_parse(__pc, _Date, __defSpec);
2631 }
2632
2633 template<typename _Out>
2634 typename basic_format_context<_Out, _CharT>::iterator
2635 format(const chrono::year_month_day& __t,
2636 basic_format_context<_Out, _CharT>& __fc) const
2637 {
2638 __format::_ChronoData<_CharT> __cd{};
2639 auto __parts = _M_f._M_spec._M_needed;
2640 __parts = __cd._M_fill_year_month(__t, __parts);
2641 __parts = __cd._M_fill_day(__t.day(), __parts);
2642 if (__parts == 0)
2643 return _M_f._M_format(__cd, __fc);
2644
2645 chrono::local_days __ld(__t);
2646 __cd._M_fill_ldays(__ld, __parts);
2647 return _M_f._M_format(__cd, __fc);
2648 }
2649
2650 private:
2651 static constexpr __format::_ChronoSpec<_CharT> __defSpec = []
2652 {
2653 using __format::_ChronoFormats;
2654 using enum __format::_ChronoParts;
2655
2656 __format::_ChronoSpec<_CharT> __res{};
2657 __res._M_debug = true;
2658 __res._M_needed = _YearMonthDay;
2659 __res._M_chrono_specs = _ChronoFormats<_CharT>::_S_f();
2660 return __res;
2661 }();
2662
2663 __format::__formatter_chrono<_CharT> _M_f{__defSpec};
2664 };
2665
2666 template<__format::__char _CharT>
2667 struct formatter<chrono::year_month_day_last, _CharT>
2668 {
2669 constexpr typename basic_format_parse_context<_CharT>::iterator
2670 parse(basic_format_parse_context<_CharT>& __pc)
2671 {
2672 using enum __format::_ChronoParts;
2673 return _M_f._M_parse(__pc, _Date, __defSpec);
2674 }
2675
2676 template<typename _Out>
2677 typename basic_format_context<_Out, _CharT>::iterator
2678 format(const chrono::year_month_day_last& __t,
2679 basic_format_context<_Out, _CharT>& __fc) const
2680 {
2681 __format::_ChronoData<_CharT> __cd{};
2682 auto __parts = _M_f._M_spec._M_needed;
2683 __parts = __cd._M_fill_year_month(__t, __parts);
2684 if (__parts == 0)
2685 return _M_f._M_format(__cd, __fc);
2686
2687 chrono::local_days __ld(__t);
2688 __parts = __cd._M_fill_ldays(__ld, __parts);
2689 if (__parts == 0)
2690 return _M_f._M_format(__cd, __fc);
2691
2692 chrono::year_month_day __ymd(__ld);
2693 __cd._M_fill_day(__ymd.day(), __parts);
2694 return _M_f._M_format(__cd, __fc);
2695 }
2696
2697 private:
2698 static constexpr __format::_ChronoSpec<_CharT> __defSpec = []
2699 {
2700 using __format::_ChronoFormats;
2701 using enum __format::_ChronoParts;
2702
2703 __format::_ChronoSpec<_CharT> __res{};
2704 __res._M_debug = true;
2705 __res._M_localized = true;
2706 __res._M_locale_specific = true;
2707 __res._M_needed = _Year|_Month;
2708 __res._M_chrono_specs = _ChronoFormats<_CharT>::_S_yml();
2709 return __res;
2710 }();
2711
2712 __format::__formatter_chrono<_CharT> _M_f{__defSpec};
2713 };
2714
2715 template<__format::__char _CharT>
2716 struct formatter<chrono::year_month_weekday, _CharT>
2717 {
2718 constexpr typename basic_format_parse_context<_CharT>::iterator
2719 parse(basic_format_parse_context<_CharT>& __pc)
2720 {
2721 using enum __format::_ChronoParts;
2722 return _M_f._M_parse(__pc, _Date, __defSpec);
2723 }
2724
2725 template<typename _Out>
2726 typename basic_format_context<_Out, _CharT>::iterator
2727 format(const chrono::year_month_weekday& __t,
2728 basic_format_context<_Out, _CharT>& __fc) const
2729 {
2730 __format::_ChronoData<_CharT> __cd{};
2731 auto __parts = _M_f._M_spec._M_needed;
2732 __parts = __cd._M_fill_year_month(__t, __parts);
2733 __parts = __cd._M_fill_weekday(__t.weekday_indexed(), __parts);
2734 if (__parts == 0)
2735 return _M_f._M_format(__cd, __fc);
2736
2737 chrono::local_days __ld(__t);
2738 __parts = __cd._M_fill_ldays(__ld, __parts);
2739 if (__parts == 0)
2740 return _M_f._M_format(__cd, __fc);
2741
2742 chrono::year_month_day __ymd(__ld);
2743 // n.b. weekday index is supplied by input, do not override it
2744 __cd._M_day = __ymd.day();
2745 return _M_f._M_format(__cd, __fc);
2746 }
2747
2748 private:
2749 static constexpr __format::_ChronoSpec<_CharT> __defSpec = []
2750 {
2751 using __format::_ChronoFormats;
2752 using enum __format::_ChronoParts;
2753
2754 __format::_ChronoSpec<_CharT> __res{};
2755 __res._M_debug = true;
2756 __res._M_localized = true;
2757 __res._M_locale_specific = true;
2758 __res._M_needed = _Year|_Month|_IndexedWeekday;
2759 __res._M_chrono_specs = _ChronoFormats<_CharT>::_S_ymwi();
2760 return __res;
2761 }();
2762
2763 __format::__formatter_chrono<_CharT> _M_f{__defSpec};
2764 };
2765
2766 template<__format::__char _CharT>
2767 struct formatter<chrono::year_month_weekday_last, _CharT>
2768 {
2769 constexpr typename basic_format_parse_context<_CharT>::iterator
2770 parse(basic_format_parse_context<_CharT>& __pc)
2771 {
2772 using enum __format::_ChronoParts;
2773 return _M_f._M_parse(__pc, _Date, __defSpec);
2774 }
2775
2776 template<typename _Out>
2777 typename basic_format_context<_Out, _CharT>::iterator
2778 format(const chrono::year_month_weekday_last& __t,
2779 basic_format_context<_Out, _CharT>& __fc) const
2780 {
2781 __format::_ChronoData<_CharT> __cd{};
2782 auto __parts = _M_f._M_spec._M_needed;
2783 __parts = __cd._M_fill_year_month(__t, __parts);
2784 __cd._M_weekday = __t.weekday_last().weekday();
2785 __parts -= __format::_ChronoParts::_Weekday;
2786 if (__parts == 0)
2787 return _M_f._M_format(__cd, __fc);
2788
2789 chrono::local_days __ld(__t);
2790 __parts = __cd._M_fill_ldays(__ld, __parts);
2791 if (__parts == 0)
2792 return _M_f._M_format(__cd, __fc);
2793
2794 chrono::year_month_day __ymd(__ld);
2795 __cd._M_fill_day(__ymd.day(), __parts);
2796 return _M_f._M_format(__cd, __fc);
2797 }
2798
2799 private:
2800 static constexpr __format::_ChronoSpec<_CharT> __defSpec = []
2801 {
2802 using __format::_ChronoFormats;
2803 using enum __format::_ChronoParts;
2804
2805 __format::_ChronoSpec<_CharT> __res{};
2806 __res._M_debug = true;
2807 __res._M_localized = true;
2808 __res._M_locale_specific = true;
2809 __res._M_needed = _Year|_Month|_Weekday;
2810 __res._M_chrono_specs = _ChronoFormats<_CharT>::_S_ymwl();
2811 return __res;
2812 }();
2813
2814 __format::__formatter_chrono<_CharT> _M_f{__defSpec};
2815 };
2816
2817 template<typename _Rep, typename _Period, __format::__char _CharT>
2818 struct formatter<chrono::hh_mm_ss<chrono::duration<_Rep, _Period>>, _CharT>
2819 {
2820 constexpr typename basic_format_parse_context<_CharT>::iterator
2821 parse(basic_format_parse_context<_CharT>& __pc)
2822 {
2823 using enum __format::_ChronoParts;
2824 return _M_f.template _M_parse<_Precision>(__pc, _Time, __defSpec);
2825 }
2826
2827 template<typename _Out>
2828 typename basic_format_context<_Out, _CharT>::iterator
2829 format(const chrono::hh_mm_ss<chrono::duration<_Rep, _Period>>& __t,
2830 basic_format_context<_Out, _CharT>& __fc) const
2831 {
2832 using enum __format::_ChronoParts;
2833
2834 __format::_ChronoData<_CharT> __cd;
2835 __cd._M_is_neg = __t.is_negative();
2836 __cd._M_hours = __t.hours();
2837 __cd._M_minutes = __t.minutes();
2838 __cd._M_seconds = __t.seconds();
2839
2840 _Precision __d(0);
2841 // n.b. computing total duration or total seconds may overflow,
2842 // do not compute them if not requested.
2843 if (_M_f._M_spec._M_needs(_EpochUnits))
2844 __d = __t.to_duration();
2845 if (_M_f._M_spec._M_needs(_TotalSeconds))
2846 __cd._M_eseconds
2847 = __cd._M_hours + __cd._M_minutes + __cd._M_seconds;
2848 return _M_f._M_format_units(__cd, __d, __t.subseconds(), __fc);
2849 }
2850
2851 private:
2852 using _Precision
2853 = typename chrono::hh_mm_ss<chrono::duration<_Rep, _Period>>::precision;
2854 static constexpr __format::_ChronoSpec<_CharT> __defSpec =
2855 __format::__formatter_duration<_CharT>::
2856 template _S_spec_for<_Precision>(__format::_ChronoParts::_Time);
2857
2858 __format::__formatter_duration<_CharT> _M_f{__defSpec};
2859 };
2860
2861#if _GLIBCXX_USE_CXX11_ABI || ! _GLIBCXX_USE_DUAL_ABI
2862 template<__format::__char _CharT>
2863 struct formatter<chrono::sys_info, _CharT>
2864 {
2865 constexpr typename basic_format_parse_context<_CharT>::iterator
2866 parse(basic_format_parse_context<_CharT>& __pc)
2867 { return _M_f.parse(__pc); }
2868
2869 template<typename _Out>
2870 typename basic_format_context<_Out, _CharT>::iterator
2871 format(const chrono::sys_info& __i,
2872 basic_format_context<_Out, _CharT>& __fc) const
2873 { return _M_f.format(__i, __fc); }
2874
2875 private:
2876 __format::__formatter_chrono_info<_CharT> _M_f;
2877 };
2878
2879 template<__format::__char _CharT>
2880 struct formatter<chrono::local_info, _CharT>
2881 {
2882 constexpr typename basic_format_parse_context<_CharT>::iterator
2883 parse(basic_format_parse_context<_CharT>& __pc)
2884 { return _M_f.parse(__pc); }
2885
2886 template<typename _Out>
2887 typename basic_format_context<_Out, _CharT>::iterator
2888 format(const chrono::local_info& __i,
2889 basic_format_context<_Out, _CharT>& __fc) const
2890 { return _M_f.format(__i, __fc); }
2891
2892 private:
2893 __format::__formatter_chrono_info<_CharT> _M_f;
2894 };
2895#endif
2896
2897 template<typename _Duration, __format::__char _CharT>
2898 struct formatter<chrono::sys_time<_Duration>, _CharT>
2899 {
2900 constexpr typename basic_format_parse_context<_CharT>::iterator
2901 parse(basic_format_parse_context<_CharT>& __pc)
2902 {
2903 using enum __format::_ChronoParts;
2904 auto __next
2905 = _M_f.template _M_parse<_Duration>(__pc, _ZonedDateTime, __defSpec);
2906 if constexpr (!__stream_insertable)
2907 if (_M_f._M_spec._M_chrono_specs.empty())
2908 __format::__invalid_chrono_spec(); // chrono-specs can't be empty
2909 return __next;
2910 }
2911
2912 template<typename _Out>
2913 typename basic_format_context<_Out, _CharT>::iterator
2914 format(const chrono::sys_time<_Duration>& __t,
2915 basic_format_context<_Out, _CharT>& __fc) const
2916 {
2917 __format::_ChronoData<_CharT> __cd{};
2918 __cd._M_fill_utc_zone();
2919
2920 _Duration __ed = __t.time_since_epoch();
2921 __cd._M_eseconds = chrono::floor<chrono::seconds>(__ed);
2922 __cd._M_lseconds = chrono::local_seconds(__cd._M_eseconds);
2923 return _M_f._M_format_time_point(__cd, __ed, __fc);
2924 }
2925
2926 private:
2927 static constexpr bool __stream_insertable
2928 = requires (basic_ostream<_CharT>& __os,
2929 chrono::sys_time<_Duration> __t) { __os << __t; };
2930
2931 static constexpr __format::_ChronoSpec<_CharT> __defSpec = []
2932 {
2933 using enum __format::_ChronoParts;
2934 __format::_ChronoParts __needed = _DateTime;
2935 if constexpr (!__stream_insertable)
2936 __needed = _None;
2937 else if constexpr (is_convertible_v<_Duration, chrono::days>)
2938 __needed = _Date;
2939 return __format::__formatter_duration<_CharT>::
2940 template _S_spec_for<_Duration>(__needed);
2941 }();
2942
2943 __format::__formatter_duration<_CharT> _M_f{__defSpec};
2944 };
2945
2946 template<typename _Duration, __format::__char _CharT>
2947 struct formatter<chrono::utc_time<_Duration>, _CharT>
2948 : __format::__formatter_chrono<_CharT>
2949 {
2950 constexpr typename basic_format_parse_context<_CharT>::iterator
2951 parse(basic_format_parse_context<_CharT>& __pc)
2952 {
2953 using enum __format::_ChronoParts;
2954 return _M_f.template _M_parse<_Duration>(__pc, _ZonedDateTime, __defSpec);
2955 }
2956
2957 template<typename _Out>
2958 typename basic_format_context<_Out, _CharT>::iterator
2959 format(const chrono::utc_time<_Duration>& __t,
2960 basic_format_context<_Out, _CharT>& __fc) const
2961 {
2962 using __format::_ChronoParts;
2963 using namespace chrono;
2964 __format::_ChronoData<_CharT> __cd{};
2965 __cd._M_fill_utc_zone();
2966
2967 _Duration __ed = __t.time_since_epoch();
2968 __cd._M_eseconds = chrono::floor<seconds>(__ed);
2969 // Adjust by removing leap seconds to get equivalent sys_time.
2970 // We can't just use clock_cast because we want to know if the time
2971 // falls within a leap second insertion, and format seconds as "60".
2972 const auto __li = chrono::get_leap_second_info(__t);
2973 __cd._M_lseconds = local_seconds(__cd._M_eseconds - __li.elapsed);
2974 auto __parts = _M_f._M_spec._M_needed - _ChronoParts::_TotalSeconds;
2975 if ((__parts & _ChronoParts::_DateTime) != 0)
2976 {
2977 __cd._M_fill_date_time(__cd._M_lseconds, __parts);
2978 __cd._M_seconds += seconds(__li.is_leap_second);
2979 }
2980 return _M_f._M_format_units(__cd, __ed, __ed - __cd._M_eseconds, __fc);
2981 }
2982
2983 private:
2984 static constexpr __format::_ChronoSpec<_CharT> __defSpec =
2985 __format::__formatter_duration<_CharT>::
2986 template _S_spec_for<_Duration>(__format::_ChronoParts::_DateTime);
2987
2988 __format::__formatter_duration<_CharT> _M_f{__defSpec};
2989 };
2990
2991 template<typename _Duration, __format::__char _CharT>
2992 struct formatter<chrono::tai_time<_Duration>, _CharT>
2993 : __format::__formatter_chrono<_CharT>
2994 {
2995 constexpr typename basic_format_parse_context<_CharT>::iterator
2996 parse(basic_format_parse_context<_CharT>& __pc)
2997 {
2998 using enum __format::_ChronoParts;
2999 return _M_f.template _M_parse<_Duration>(__pc, _ZonedDateTime, __defSpec);
3000 }
3001
3002 template<typename _Out>
3003 typename basic_format_context<_Out, _CharT>::iterator
3004 format(const chrono::tai_time<_Duration>& __t,
3005 basic_format_context<_Out, _CharT>& __fc) const
3006 {
3007 using namespace chrono;
3008 __format::_ChronoData<_CharT> __cd{};
3009 __cd._M_fill_zone("TAI", L"TAI");
3010
3011 _Duration __ed = __t.time_since_epoch();
3012 __cd._M_eseconds = chrono::floor<seconds>(__ed);
3013 // Offset is 1970y/January/1 - 1958y/January/1
3014 constexpr chrono::days __tai_offset = chrono::days(4383);
3015 __cd._M_lseconds = local_seconds(__cd._M_eseconds - __tai_offset);
3016 return _M_f._M_format_time_point(__cd, __ed, __fc);
3017 }
3018
3019 private:
3020 static constexpr __format::_ChronoSpec<_CharT> __defSpec =
3021 __format::__formatter_duration<_CharT>::
3022 template _S_spec_for<_Duration>(__format::_ChronoParts::_DateTime);
3023
3024 __format::__formatter_duration<_CharT> _M_f{__defSpec};
3025 };
3026
3027 template<typename _Duration, __format::__char _CharT>
3028 struct formatter<chrono::gps_time<_Duration>, _CharT>
3029 : __format::__formatter_chrono<_CharT>
3030 {
3031 constexpr typename basic_format_parse_context<_CharT>::iterator
3032 parse(basic_format_parse_context<_CharT>& __pc)
3033 {
3034 using enum __format::_ChronoParts;
3035 return _M_f.template _M_parse<_Duration>(__pc, _ZonedDateTime, __defSpec);
3036 }
3037
3038 template<typename _Out>
3039 typename basic_format_context<_Out, _CharT>::iterator
3040 format(const chrono::gps_time<_Duration>& __t,
3041 basic_format_context<_Out, _CharT>& __fc) const
3042 {
3043 using namespace chrono;
3044 __format::_ChronoData<_CharT> __cd{};
3045 __cd._M_fill_zone("GPS", L"GPS");
3046
3047 _Duration __ed = __t.time_since_epoch();
3048 __cd._M_eseconds = chrono::floor<seconds>(__ed);
3049 // Offset is 1980y/January/Sunday[1] - 1970y/January/1
3050 constexpr chrono::days __gps_offset = chrono::days(3657);
3051 __cd._M_lseconds = local_seconds(__cd._M_eseconds + __gps_offset);
3052 return _M_f._M_format_time_point(__cd, __ed, __fc);
3053 }
3054
3055 private:
3056 static constexpr __format::_ChronoSpec<_CharT> __defSpec =
3057 __format::__formatter_duration<_CharT>::
3058 template _S_spec_for<_Duration>(__format::_ChronoParts::_DateTime);
3059
3060 __format::__formatter_duration<_CharT> _M_f{__defSpec};
3061 };
3062
3063 template<typename _Duration, __format::__char _CharT>
3064 struct formatter<chrono::file_time<_Duration>, _CharT>
3065 {
3066 constexpr typename basic_format_parse_context<_CharT>::iterator
3067 parse(basic_format_parse_context<_CharT>& __pc)
3068 {
3069 using enum __format::_ChronoParts;
3070 return _M_f.template _M_parse<_Duration>(__pc, _ZonedDateTime, __defSpec);
3071 }
3072
3073 template<typename _Out>
3074 typename basic_format_context<_Out, _CharT>::iterator
3075 format(const chrono::file_time<_Duration>& __t,
3076 basic_format_context<_Out, _CharT>& __fc) const
3077 {
3078 using namespace chrono;
3079 __format::_ChronoData<_CharT> __cd{};
3080 __cd._M_fill_utc_zone();
3081
3082 _Duration __ed = __t.time_since_epoch();
3083 __cd._M_eseconds = chrono::floor<seconds>(__ed);
3084 auto __st = chrono::clock_cast<system_clock>(__t);
3085 __cd._M_lseconds
3086 = local_seconds(chrono::floor<seconds>(__st.time_since_epoch()));
3087 return _M_f._M_format_time_point(__cd, __ed, __fc);
3088 }
3089
3090 private:
3091 static constexpr __format::_ChronoSpec<_CharT> __defSpec =
3092 __format::__formatter_duration<_CharT>::
3093 template _S_spec_for<_Duration>(__format::_ChronoParts::_DateTime);
3094
3095 __format::__formatter_duration<_CharT> _M_f{__defSpec};
3096 };
3097
3098 template<typename _Duration, __format::__char _CharT>
3099 struct formatter<chrono::local_time<_Duration>, _CharT>
3100 {
3101 constexpr typename basic_format_parse_context<_CharT>::iterator
3102 parse(basic_format_parse_context<_CharT>& __pc)
3103 {
3104 using enum __format::_ChronoParts;
3105 return _M_f.template _M_parse<_Duration>(__pc, _DateTime, __defSpec);
3106 }
3107
3108 template<typename _Out>
3109 typename basic_format_context<_Out, _CharT>::iterator
3110 format(const chrono::local_time<_Duration>& __lt,
3111 basic_format_context<_Out, _CharT>& __fc) const
3112 {
3113 __format::_ChronoData<_CharT> __cd{};
3114 _Duration __ed = __lt.time_since_epoch();
3115 __cd._M_lseconds = chrono::floor<chrono::seconds>(__lt);
3116 __cd._M_eseconds = __cd._M_lseconds.time_since_epoch();
3117 return _M_f._M_format_time_point(__cd, __ed, __fc);
3118 }
3119
3120 private:
3121 static constexpr __format::_ChronoSpec<_CharT> __defSpec = []
3122 {
3123 using enum __format::_ChronoParts;
3124 __format::_ChronoParts __needed = _DateTime;
3125 if constexpr (is_convertible_v<_Duration, chrono::days>)
3126 __needed = _Date;
3127 return __format::__formatter_duration<_CharT>::
3128 template _S_spec_for<_Duration>(__needed);
3129 }();
3130
3131 __format::__formatter_duration<_CharT> _M_f{__defSpec};
3132 };
3133
3134 template<typename _Duration, __format::__char _CharT>
3135 struct formatter<chrono::__detail::__local_time_fmt<_Duration>, _CharT>
3136 {
3137 constexpr typename basic_format_parse_context<_CharT>::iterator
3138 parse(basic_format_parse_context<_CharT>& __pc)
3139 {
3140 using enum __format::_ChronoParts;
3141 return _M_f.template _M_parse<_Duration>(__pc, _ZonedDateTime, __defSpec);
3142 }
3143
3144 template<typename _Out>
3145 typename basic_format_context<_Out, _CharT>::iterator
3146 format(const chrono::__detail::__local_time_fmt<_Duration>& __zt,
3147 basic_format_context<_Out, _CharT>& __fc) const
3148 {
3149 using enum __format::_ChronoParts;
3150 __format::_ChronoData<_CharT> __cd{};
3151
3152 if (_M_f._M_spec._M_needs(_ZoneOffset))
3153 {
3154 if (!__zt._M_offset_sec)
3155 std::__throw_format_error("format error: no timezone available for %z");
3156 __cd._M_zone_offset = *__zt._M_offset_sec;
3157 }
3158
3159 basic_string<_CharT> __zone_store;
3160 if (_M_f._M_spec._M_needs(_ZoneAbbrev))
3161 {
3162 if (!__zt._M_abbrev)
3163 std::__throw_format_error("format error: no timezone available for %Z");
3164
3165 __cd._M_zone_cstr = __zt._M_abbrev->data();
3166 if constexpr (is_same_v<_CharT, char>)
3167 __cd._M_zone_abbrev = *__zt._M_abbrev;
3168 else
3169 {
3170 // TODO: use resize_for_override
3171 __zone_store.resize(__zt._M_abbrev->size());
3172 auto& __ct = use_facet<ctype<_CharT>>(_M_f._M_locale(__fc));
3173 __ct.widen(__zt._M_abbrev->data(),
3174 __zt._M_abbrev->data() + __zt._M_abbrev->size(),
3175 __zone_store.data());
3176 __cd._M_zone_abbrev = __zone_store;
3177 }
3178 }
3179
3180 _Duration __ed = __zt._M_time.time_since_epoch();
3181 __cd._M_lseconds = chrono::floor<chrono::seconds>(__zt._M_time);
3182 __cd._M_eseconds = __cd._M_lseconds.time_since_epoch();
3183 return _M_f._M_format_time_point(__cd, __ed, __fc);
3184 }
3185
3186 private:
3187 static constexpr __format::_ChronoSpec<_CharT> __defSpec =
3188 __format::__formatter_duration<_CharT>::
3189 template _S_spec_for<_Duration>(__format::_ChronoParts::_ZonedDateTime);
3190
3191 __format::__formatter_duration<_CharT> _M_f{__defSpec};
3192 };
3193
3194#if _GLIBCXX_USE_CXX11_ABI || ! _GLIBCXX_USE_DUAL_ABI
3195 template<typename _Duration, typename _TimeZonePtr, __format::__char _CharT>
3196 struct formatter<chrono::zoned_time<_Duration, _TimeZonePtr>, _CharT>
3197 : formatter<chrono::__detail::__local_time_fmt_for<_Duration>, _CharT>
3198 {
3199 template<typename _Out>
3200 typename basic_format_context<_Out, _CharT>::iterator
3201 format(const chrono::zoned_time<_Duration, _TimeZonePtr>& __tp,
3202 basic_format_context<_Out, _CharT>& __fc) const
3203 {
3204 using _Ltf = chrono::__detail::__local_time_fmt_for<_Duration>;
3205 using _Base = formatter<_Ltf, _CharT>;
3206 const chrono::sys_info __info = __tp.get_info();
3207 const auto __lf = chrono::local_time_format(__tp.get_local_time(),
3208 &__info.abbrev,
3209 &__info.offset);
3210 return _Base::format(__lf, __fc);
3211 }
3212 };
3213#endif
3214
3215namespace chrono
3216{
3217/// @addtogroup chrono
3218/// @{
3219
3220/// @cond undocumented
3221namespace __detail
3222{
3223 template<typename _Duration = seconds>
3224 struct _Parser
3225 {
3226 static_assert(is_same_v<common_type_t<_Duration, seconds>, _Duration>);
3227
3228 explicit
3229 _Parser(__format::_ChronoParts __need) : _M_need(__need) { }
3230
3231 _Parser(_Parser&&) = delete;
3232 void operator=(_Parser&&) = delete;
3233
3234 _Duration _M_time{}; // since midnight
3235 sys_days _M_sys_days{};
3236 year_month_day _M_ymd{};
3237 weekday _M_wd{};
3238 __format::_ChronoParts _M_need;
3239 unsigned _M_is_leap_second : 1 {};
3240 unsigned _M_reserved : 15 {};
3241
3242 template<typename _CharT, typename _Traits, typename _Alloc>
3243 basic_istream<_CharT, _Traits>&
3244 operator()(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt,
3245 basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr,
3246 minutes* __offset = nullptr);
3247
3248 private:
3249 // Read an unsigned integer from the stream and return it.
3250 // Extract no more than __n digits. Set failbit if an integer isn't read.
3251 template<typename _CharT, typename _Traits>
3252 static int_least32_t
3253 _S_read_unsigned(basic_istream<_CharT, _Traits>& __is,
3254 ios_base::iostate& __err, int __n)
3255 {
3256 int_least32_t __val = _S_try_read_digit(__is, __err);
3257 if (__val == -1) [[unlikely]]
3258 __err |= ios_base::failbit;
3259 else
3260 {
3261 int __n1 = (std::min)(__n, 9);
3262 // Cannot overflow __val unless we read more than 9 digits
3263 for (int __i = 1; __i < __n1; ++__i)
3264 if (auto __dig = _S_try_read_digit(__is, __err); __dig != -1)
3265 {
3266 __val *= 10;
3267 __val += __dig;
3268 }
3269
3270 while (__n1++ < __n) [[unlikely]]
3271 if (auto __dig = _S_try_read_digit(__is, __err); __dig != -1)
3272 {
3273 if (__builtin_mul_overflow(__val, 10, &__val)
3274 || __builtin_add_overflow(__val, __dig, &__val))
3275 {
3276 __err |= ios_base::failbit;
3277 return -1;
3278 }
3279 }
3280 }
3281 return __val;
3282 }
3283
3284 // Read an unsigned integer from the stream and return it.
3285 // Extract no more than __n digits. Set failbit if an integer isn't read.
3286 template<typename _CharT, typename _Traits>
3287 static int_least32_t
3288 _S_read_signed(basic_istream<_CharT, _Traits>& __is,
3289 ios_base::iostate& __err, int __n)
3290 {
3291 auto __sign = __is.peek();
3292 if (__sign == '-' || __sign == '+')
3293 (void) __is.get();
3294 int_least32_t __val = _S_read_unsigned(__is, __err, __n);
3295 if (__err & ios_base::failbit)
3296 {
3297 if (__sign == '-') [[unlikely]]
3298 __val *= -1;
3299 }
3300 return __val;
3301 }
3302
3303 // Read a digit from the stream and return it, or return -1.
3304 // If no digit is read eofbit will be set (but not failbit).
3305 template<typename _CharT, typename _Traits>
3306 static int_least32_t
3307 _S_try_read_digit(basic_istream<_CharT, _Traits>& __is,
3308 ios_base::iostate& __err)
3309 {
3310 int_least32_t __val = -1;
3311 auto __i = __is.peek();
3312 if (!_Traits::eq_int_type(__i, _Traits::eof())) [[likely]]
3313 {
3314 _CharT __c = _Traits::to_char_type(__i);
3315 if (_CharT('0') <= __c && __c <= _CharT('9')) [[likely]]
3316 {
3317 (void) __is.get();
3318 __val = __c - _CharT('0');
3319 }
3320 }
3321 else
3322 __err |= ios_base::eofbit;
3323 return __val;
3324 }
3325
3326 // Read the specified character and return true.
3327 // If the character is not found, set failbit and return false.
3328 template<typename _CharT, typename _Traits>
3329 static bool
3330 _S_read_chr(basic_istream<_CharT, _Traits>& __is,
3331 ios_base::iostate& __err, _CharT __c)
3332 {
3333 auto __i = __is.peek();
3334 if (_Traits::eq_int_type(__i, _Traits::eof()))
3335 __err |= ios_base::eofbit;
3336 else if (_Traits::to_char_type(__i) == __c) [[likely]]
3337 {
3338 (void) __is.get();
3339 return true;
3340 }
3341 __err |= ios_base::failbit;
3342 return false;
3343 }
3344 };
3345
3346 template<typename _Duration>
3347 using _Parser_t = _Parser<common_type_t<_Duration, seconds>>;
3348
3349 template<typename _Duration>
3350 consteval bool
3351 __use_floor()
3352 {
3353 if constexpr (_Duration::period::den == 1)
3354 {
3355 switch (_Duration::period::num)
3356 {
3357 case minutes::period::num:
3358 case hours::period::num:
3359 case days::period::num:
3360 case weeks::period::num:
3361 case years::period::num:
3362 return true;
3363 }
3364 }
3365 return false;
3366 }
3367
3368 // A "do the right thing" rounding function for duration and time_point
3369 // values extracted by from_stream. When treat_as_floating_point is true
3370 // we don't want to do anything, just a straightforward conversion.
3371 // When the destination type has a period of minutes, hours, days, weeks,
3372 // or years, we use chrono::floor to truncate towards negative infinity.
3373 // This ensures that an extracted timestamp such as 2024-09-05 13:00:00
3374 // will produce 2024-09-05 when rounded to days, rather than rounding up
3375 // to 2024-09-06 (a different day).
3376 // Otherwise, use chrono::round to get the nearest value representable
3377 // in the destination type.
3378 template<typename _ToDur, typename _Tp>
3379 constexpr auto
3380 __round(const _Tp& __t)
3381 {
3382 if constexpr (__is_duration_v<_Tp>)
3383 {
3384 if constexpr (treat_as_floating_point_v<typename _Tp::rep>)
3386 else if constexpr (__detail::__use_floor<_ToDur>())
3387 return chrono::floor<_ToDur>(__t);
3388 else
3389 return chrono::round<_ToDur>(__t);
3390 }
3391 else
3392 {
3393 static_assert(__is_time_point_v<_Tp>);
3394 using _Tpt = time_point<typename _Tp::clock, _ToDur>;
3395 return _Tpt(__detail::__round<_ToDur>(__t.time_since_epoch()));
3396 }
3397 }
3398
3399} // namespace __detail
3400/// @endcond
3401
3402 template<typename _CharT, typename _Traits, typename _Rep, typename _Period,
3403 typename _Alloc = allocator<_CharT>>
3404 inline basic_istream<_CharT, _Traits>&
3405 from_stream(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt,
3407 basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr,
3408 minutes* __offset = nullptr)
3409 {
3410 auto __need = __format::_ChronoParts::_TimeOfDay;
3411 __detail::_Parser_t<duration<_Rep, _Period>> __p(__need);
3412 if (__p(__is, __fmt, __abbrev, __offset))
3413 __d = __detail::__round<duration<_Rep, _Period>>(__p._M_time);
3414 return __is;
3415 }
3416
3417 template<typename _CharT, typename _Traits>
3418 inline basic_ostream<_CharT, _Traits>&
3419 operator<<(basic_ostream<_CharT, _Traits>& __os, const day& __d)
3420 {
3421 using _Ctx = __format::__format_context<_CharT>;
3422 using _Str = basic_string_view<_CharT>;
3423 _Str __s = _GLIBCXX_WIDEN("{:02d} is not a valid day");
3424 if (__d.ok())
3425 __s = __s.substr(0, 6);
3426 auto __u = (unsigned)__d;
3427 __os << std::vformat(__s, make_format_args<_Ctx>(__u));
3428 return __os;
3429 }
3430
3431 template<typename _CharT, typename _Traits,
3432 typename _Alloc = allocator<_CharT>>
3433 inline basic_istream<_CharT, _Traits>&
3434 from_stream(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt,
3435 day& __d,
3436 basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr,
3437 minutes* __offset = nullptr)
3438 {
3439 __detail::_Parser<> __p(__format::_ChronoParts::_Day);
3440 if (__p(__is, __fmt, __abbrev, __offset))
3441 __d = __p._M_ymd.day();
3442 return __is;
3443 }
3444
3445 template<typename _CharT, typename _Traits>
3446 inline basic_ostream<_CharT, _Traits>&
3447 operator<<(basic_ostream<_CharT, _Traits>& __os, const month& __m)
3448 {
3449 using _Ctx = __format::__format_context<_CharT>;
3450 using _Str = basic_string_view<_CharT>;
3451 _Str __s = _GLIBCXX_WIDEN("{:L%b}{} is not a valid month");
3452 if (__m.ok())
3453 __os << std::vformat(__os.getloc(), __s.substr(0, 6),
3454 make_format_args<_Ctx>(__m));
3455 else
3456 {
3457 auto __u = (unsigned)__m;
3458 __os << std::vformat(__s.substr(6), make_format_args<_Ctx>(__u));
3459 }
3460 return __os;
3461 }
3462
3463 template<typename _CharT, typename _Traits,
3464 typename _Alloc = allocator<_CharT>>
3465 inline basic_istream<_CharT, _Traits>&
3466 from_stream(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt,
3467 month& __m,
3468 basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr,
3469 minutes* __offset = nullptr)
3470 {
3471 __detail::_Parser<> __p(__format::_ChronoParts::_Month);
3472 if (__p(__is, __fmt, __abbrev, __offset))
3473 __m = __p._M_ymd.month();
3474 return __is;
3475 }
3476
3477 template<typename _CharT, typename _Traits>
3478 inline basic_ostream<_CharT, _Traits>&
3479 operator<<(basic_ostream<_CharT, _Traits>& __os, const year& __y)
3480 {
3481 using _Ctx = __format::__format_context<_CharT>;
3482 using _Str = basic_string_view<_CharT>;
3483 _Str __s = _GLIBCXX_WIDEN("-{:04d} is not a valid year");
3484 if (__y.ok())
3485 __s = __s.substr(0, 7);
3486 int __i = (int)__y;
3487 if (__i >= 0) [[likely]]
3488 __s.remove_prefix(1);
3489 else
3490 __i = -__i;
3491 __os << std::vformat(__s, make_format_args<_Ctx>(__i));
3492 return __os;
3493 }
3494
3495 template<typename _CharT, typename _Traits,
3496 typename _Alloc = allocator<_CharT>>
3497 inline basic_istream<_CharT, _Traits>&
3498 from_stream(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt,
3499 year& __y,
3500 basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr,
3501 minutes* __offset = nullptr)
3502 {
3503 __detail::_Parser<> __p(__format::_ChronoParts::_Year);
3504 if (__p(__is, __fmt, __abbrev, __offset))
3505 __y = __p._M_ymd.year();
3506 return __is;
3507 }
3508
3509 template<typename _CharT, typename _Traits>
3510 inline basic_ostream<_CharT, _Traits>&
3511 operator<<(basic_ostream<_CharT, _Traits>& __os, const weekday& __wd)
3512 {
3513 using _Ctx = __format::__format_context<_CharT>;
3514 using _Str = basic_string_view<_CharT>;
3515 _Str __s = _GLIBCXX_WIDEN("{:L%a}{} is not a valid weekday");
3516 if (__wd.ok())
3517 __os << std::vformat(__os.getloc(), __s.substr(0, 6),
3518 make_format_args<_Ctx>(__wd));
3519 else
3520 {
3521 auto __c = __wd.c_encoding();
3522 __os << std::vformat(__s.substr(6), make_format_args<_Ctx>(__c));
3523 }
3524 return __os;
3525 }
3526
3527 template<typename _CharT, typename _Traits,
3528 typename _Alloc = allocator<_CharT>>
3529 inline basic_istream<_CharT, _Traits>&
3530 from_stream(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt,
3531 weekday& __wd,
3532 basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr,
3533 minutes* __offset = nullptr)
3534 {
3535 __detail::_Parser<> __p(__format::_ChronoParts::_Weekday);
3536 if (__p(__is, __fmt, __abbrev, __offset))
3537 __wd = __p._M_wd;
3538 return __is;
3539 }
3540
3541 template<typename _CharT, typename _Traits>
3542 inline basic_ostream<_CharT, _Traits>&
3543 operator<<(basic_ostream<_CharT, _Traits>& __os,
3544 const weekday_indexed& __wdi)
3545 {
3546 // The standard says to format wdi.weekday() and wdi.index() using
3547 // either "{:L}[{}]" or "{:L}[{} is not a valid index]". The {:L} spec
3548 // means to format the weekday using ostringstream, so just do that.
3549 basic_stringstream<_CharT> __os2;
3550 __os2.imbue(__os.getloc());
3551 __os2 << __wdi.weekday();
3552 const auto __i = __wdi.index();
3553 basic_string_view<_CharT> __s
3554 = _GLIBCXX_WIDEN("[ is not a valid index]");
3555 __os2 << __s[0];
3556 __os2 << std::format(_GLIBCXX_WIDEN("{}"), __i);
3557 if (__i >= 1 && __i <= 5)
3558 __os2 << __s.back();
3559 else
3560 __os2 << __s.substr(1);
3561 __os << __os2.view();
3562 return __os;
3563 }
3564
3565 template<typename _CharT, typename _Traits>
3566 inline basic_ostream<_CharT, _Traits>&
3567 operator<<(basic_ostream<_CharT, _Traits>& __os,
3568 const weekday_last& __wdl)
3569 {
3570 // As above, just write straight to a stringstream, as if by "{:L}[last]"
3571 basic_stringstream<_CharT> __os2;
3572 __os2.imbue(__os.getloc());
3573 __os2 << __wdl.weekday() << _GLIBCXX_WIDEN("[last]");
3574 __os << __os2.view();
3575 return __os;
3576 }
3577
3578 template<typename _CharT, typename _Traits>
3579 inline basic_ostream<_CharT, _Traits>&
3580 operator<<(basic_ostream<_CharT, _Traits>& __os, const month_day& __md)
3581 {
3582 // As above, just write straight to a stringstream, as if by "{:L}/{}"
3583 basic_stringstream<_CharT> __os2;
3584 __os2.imbue(__os.getloc());
3585 __os2 << __md.month();
3586 if constexpr (is_same_v<_CharT, char>)
3587 __os2 << '/';
3588 else
3589 __os2 << L'/';
3590 __os2 << __md.day();
3591 __os << __os2.view();
3592 return __os;
3593 }
3594
3595 template<typename _CharT, typename _Traits,
3596 typename _Alloc = allocator<_CharT>>
3597 inline basic_istream<_CharT, _Traits>&
3598 from_stream(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt,
3599 month_day& __md,
3600 basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr,
3601 minutes* __offset = nullptr)
3602 {
3603 using __format::_ChronoParts;
3604 auto __need = _ChronoParts::_Month | _ChronoParts::_Day;
3605 __detail::_Parser<> __p(__need);
3606 if (__p(__is, __fmt, __abbrev, __offset))
3607 __md = month_day(__p._M_ymd.month(), __p._M_ymd.day());
3608 return __is;
3609 }
3610
3611 template<typename _CharT, typename _Traits>
3612 inline basic_ostream<_CharT, _Traits>&
3613 operator<<(basic_ostream<_CharT, _Traits>& __os,
3614 const month_day_last& __mdl)
3615 {
3616 // As above, just write straight to a stringstream, as if by "{:L}/last"
3617 basic_stringstream<_CharT> __os2;
3618 __os2.imbue(__os.getloc());
3619 __os2 << __mdl.month() << _GLIBCXX_WIDEN("/last");
3620 __os << __os2.view();
3621 return __os;
3622 }
3623
3624 template<typename _CharT, typename _Traits>
3625 inline basic_ostream<_CharT, _Traits>&
3626 operator<<(basic_ostream<_CharT, _Traits>& __os,
3627 const month_weekday& __mwd)
3628 {
3629 // As above, just write straight to a stringstream, as if by "{:L}/{:L}"
3630 basic_stringstream<_CharT> __os2;
3631 __os2.imbue(__os.getloc());
3632 __os2 << __mwd.month();
3633 if constexpr (is_same_v<_CharT, char>)
3634 __os2 << '/';
3635 else
3636 __os2 << L'/';
3637 __os2 << __mwd.weekday_indexed();
3638 __os << __os2.view();
3639 return __os;
3640 }
3641
3642 template<typename _CharT, typename _Traits>
3643 inline basic_ostream<_CharT, _Traits>&
3644 operator<<(basic_ostream<_CharT, _Traits>& __os,
3645 const month_weekday_last& __mwdl)
3646 {
3647 // As above, just write straight to a stringstream, as if by "{:L}/{:L}"
3648 basic_stringstream<_CharT> __os2;
3649 __os2.imbue(__os.getloc());
3650 __os2 << __mwdl.month();
3651 if constexpr (is_same_v<_CharT, char>)
3652 __os2 << '/';
3653 else
3654 __os2 << L'/';
3655 __os2 << __mwdl.weekday_last();
3656 __os << __os2.view();
3657 return __os;
3658 }
3659
3660 template<typename _CharT, typename _Traits>
3661 inline basic_ostream<_CharT, _Traits>&
3662 operator<<(basic_ostream<_CharT, _Traits>& __os, const year_month& __ym)
3663 {
3664 // As above, just write straight to a stringstream, as if by "{}/{:L}"
3665 basic_stringstream<_CharT> __os2;
3666 __os2.imbue(__os.getloc());
3667 __os2 << __ym.year();
3668 if constexpr (is_same_v<_CharT, char>)
3669 __os2 << '/';
3670 else
3671 __os2 << L'/';
3672 __os2 << __ym.month();
3673 __os << __os2.view();
3674 return __os;
3675 }
3676
3677 template<typename _CharT, typename _Traits,
3678 typename _Alloc = allocator<_CharT>>
3679 inline basic_istream<_CharT, _Traits>&
3680 from_stream(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt,
3681 year_month& __ym,
3682 basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr,
3683 minutes* __offset = nullptr)
3684 {
3685 using __format::_ChronoParts;
3686 auto __need = _ChronoParts::_Year | _ChronoParts::_Month;
3687 __detail::_Parser<> __p(__need);
3688 if (__p(__is, __fmt, __abbrev, __offset))
3689 __ym = year_month(__p._M_ymd.year(), __p._M_ymd.month());
3690 return __is;
3691 }
3692
3693 template<typename _CharT, typename _Traits>
3694 inline basic_ostream<_CharT, _Traits>&
3695 operator<<(basic_ostream<_CharT, _Traits>& __os,
3696 const year_month_day& __ymd)
3697 {
3698 using _Ctx = __format::__format_context<_CharT>;
3699 using _Str = basic_string_view<_CharT>;
3700 _Str __s = _GLIBCXX_WIDEN("{:%F} is not a valid date");
3701 __os << std::vformat(__ymd.ok() ? __s.substr(0, 5) : __s,
3702 make_format_args<_Ctx>(__ymd));
3703 return __os;
3704 }
3705
3706 template<typename _CharT, typename _Traits,
3707 typename _Alloc = allocator<_CharT>>
3708 inline basic_istream<_CharT, _Traits>&
3709 from_stream(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt,
3710 year_month_day& __ymd,
3711 basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr,
3712 minutes* __offset = nullptr)
3713 {
3714 using __format::_ChronoParts;
3715 auto __need = _ChronoParts::_Year | _ChronoParts::_Month
3716 | _ChronoParts::_Day;
3717 __detail::_Parser<> __p(__need);
3718 if (__p(__is, __fmt, __abbrev, __offset))
3719 __ymd = __p._M_ymd;
3720 return __is;
3721 }
3722
3723 template<typename _CharT, typename _Traits>
3726 const year_month_day_last& __ymdl)
3727 {
3728 // As above, just write straight to a stringstream, as if by "{}/{:L}"
3730 __os2.imbue(__os.getloc());
3731 __os2 << __ymdl.year();
3732 if constexpr (is_same_v<_CharT, char>)
3733 __os2 << '/';
3734 else
3735 __os2 << L'/';
3736 __os2 << __ymdl.month_day_last();
3737 __os << __os2.view();
3738 return __os;
3739 }
3740
3741 template<typename _CharT, typename _Traits>
3742 inline basic_ostream<_CharT, _Traits>&
3743 operator<<(basic_ostream<_CharT, _Traits>& __os,
3744 const year_month_weekday& __ymwd)
3745 {
3746 // As above, just write straight to a stringstream, as if by
3747 // "{}/{:L}/{:L}"
3748 basic_stringstream<_CharT> __os2;
3749 __os2.imbue(__os.getloc());
3750 _CharT __slash;
3751 if constexpr (is_same_v<_CharT, char>)
3752 __slash = '/';
3753 else
3754 __slash = L'/';
3755 __os2 << __ymwd.year() << __slash << __ymwd.month() << __slash
3756 << __ymwd.weekday_indexed();
3757 __os << __os2.view();
3758 return __os;
3759 }
3760
3761 template<typename _CharT, typename _Traits>
3762 inline basic_ostream<_CharT, _Traits>&
3763 operator<<(basic_ostream<_CharT, _Traits>& __os,
3764 const year_month_weekday_last& __ymwdl)
3765 {
3766 // As above, just write straight to a stringstream, as if by
3767 // "{}/{:L}/{:L}"
3768 basic_stringstream<_CharT> __os2;
3769 __os2.imbue(__os.getloc());
3770 _CharT __slash;
3771 if constexpr (is_same_v<_CharT, char>)
3772 __slash = '/';
3773 else
3774 __slash = L'/';
3775 __os2 << __ymwdl.year() << __slash << __ymwdl.month() << __slash
3776 << __ymwdl.weekday_last();
3777 __os << __os2.view();
3778 return __os;
3779 }
3780
3781 template<typename _CharT, typename _Traits, typename _Duration>
3782 inline basic_ostream<_CharT, _Traits>&
3783 operator<<(basic_ostream<_CharT, _Traits>& __os,
3784 const hh_mm_ss<_Duration>& __hms)
3785 {
3786 return __os << format(__os.getloc(), _GLIBCXX_WIDEN("{:L%T}"), __hms);
3787 }
3788
3789#if _GLIBCXX_USE_CXX11_ABI || ! _GLIBCXX_USE_DUAL_ABI
3790 /// Writes a sys_info object to an ostream in an unspecified format.
3791 template<typename _CharT, typename _Traits>
3792 basic_ostream<_CharT, _Traits>&
3793 operator<<(basic_ostream<_CharT, _Traits>& __os, const sys_info& __i)
3794 {
3795 return __os << std::format(__os.getloc(), _GLIBCXX_WIDEN("{}"), __i);
3796 }
3797
3798 /// Writes a local_info object to an ostream in an unspecified format.
3799 template<typename _CharT, typename _Traits>
3800 basic_ostream<_CharT, _Traits>&
3801 operator<<(basic_ostream<_CharT, _Traits>& __os, const local_info& __li)
3802 {
3803 __os << __format::_Separators<_CharT>::_S_squares()[0];
3804 if (__li.result == local_info::unique)
3805 __os << __li.first;
3806 else
3807 {
3808 if (__li.result == local_info::nonexistent)
3809 __os << _GLIBCXX_WIDEN("nonexistent");
3810 else
3811 __os << _GLIBCXX_WIDEN("ambiguous");
3812 __os << _GLIBCXX_WIDEN(" local time between ") << __li.first;
3813 __os << _GLIBCXX_WIDEN(" and ") << __li.second;
3814 }
3815 __os << __format::_Separators<_CharT>::_S_squares()[1];
3816 return __os;
3817 }
3818
3819 template<typename _CharT, typename _Traits, typename _Duration,
3820 typename _TimeZonePtr>
3821 inline basic_ostream<_CharT, _Traits>&
3822 operator<<(basic_ostream<_CharT, _Traits>& __os,
3823 const zoned_time<_Duration, _TimeZonePtr>& __t)
3824 {
3825 __os << format(__os.getloc(), _GLIBCXX_WIDEN("{:L%F %T %Z}"), __t);
3826 return __os;
3827 }
3828#endif
3829
3830 template<typename _CharT, typename _Traits, typename _Duration>
3831 requires (!treat_as_floating_point_v<typename _Duration::rep>)
3832 && ratio_less_v<typename _Duration::period, days::period>
3833 inline basic_ostream<_CharT, _Traits>&
3834 operator<<(basic_ostream<_CharT, _Traits>& __os,
3835 const sys_time<_Duration>& __tp)
3836 {
3837 __os << std::format(__os.getloc(), _GLIBCXX_WIDEN("{:L%F %T}"), __tp);
3838 return __os;
3839 }
3840
3841 template<typename _CharT, typename _Traits>
3842 inline basic_ostream<_CharT, _Traits>&
3843 operator<<(basic_ostream<_CharT, _Traits>& __os, const sys_days& __dp)
3844 {
3845 __os << year_month_day{__dp};
3846 return __os;
3847 }
3848
3849 template<typename _CharT, typename _Traits, typename _Duration,
3850 typename _Alloc = allocator<_CharT>>
3851 basic_istream<_CharT, _Traits>&
3852 from_stream(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt,
3853 sys_time<_Duration>& __tp,
3854 basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr,
3855 minutes* __offset = nullptr)
3856 {
3857 minutes __off{};
3858 if (!__offset)
3859 __offset = &__off;
3860 using __format::_ChronoParts;
3861 auto __need = _ChronoParts::_Year | _ChronoParts::_Month
3862 | _ChronoParts::_Day | _ChronoParts::_TimeOfDay;
3863 __detail::_Parser_t<_Duration> __p(__need);
3864 if (__p(__is, __fmt, __abbrev, __offset))
3865 {
3866 if (__p._M_is_leap_second)
3867 __is.setstate(ios_base::failbit);
3868 else
3869 {
3870 auto __st = __p._M_sys_days + __p._M_time - *__offset;
3871 __tp = __detail::__round<_Duration>(__st);
3872 }
3873 }
3874 return __is;
3875 }
3876
3877 template<typename _CharT, typename _Traits, typename _Duration>
3878 inline basic_ostream<_CharT, _Traits>&
3879 operator<<(basic_ostream<_CharT, _Traits>& __os,
3880 const utc_time<_Duration>& __t)
3881 {
3882 __os << std::format(__os.getloc(), _GLIBCXX_WIDEN("{:L%F %T}"), __t);
3883 return __os;
3884 }
3885
3886 template<typename _CharT, typename _Traits, typename _Duration,
3887 typename _Alloc = allocator<_CharT>>
3888 inline basic_istream<_CharT, _Traits>&
3889 from_stream(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt,
3890 utc_time<_Duration>& __tp,
3891 basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr,
3892 minutes* __offset = nullptr)
3893 {
3894 minutes __off{};
3895 if (!__offset)
3896 __offset = &__off;
3897 using __format::_ChronoParts;
3898 auto __need = _ChronoParts::_Year | _ChronoParts::_Month
3899 | _ChronoParts::_Day | _ChronoParts::_TimeOfDay;
3900 __detail::_Parser_t<_Duration> __p(__need);
3901 if (__p(__is, __fmt, __abbrev, __offset))
3902 {
3903 // Converting to utc_time before adding _M_time is necessary for
3904 // "23:59:60" to correctly produce a time within a leap second.
3905 auto __ut = utc_clock::from_sys(__p._M_sys_days) + __p._M_time
3906 - *__offset;
3907 __tp = __detail::__round<_Duration>(__ut);
3908 }
3909 return __is;
3910 }
3911
3912 template<typename _CharT, typename _Traits, typename _Duration>
3913 inline basic_ostream<_CharT, _Traits>&
3914 operator<<(basic_ostream<_CharT, _Traits>& __os,
3915 const tai_time<_Duration>& __t)
3916 {
3917 __os << std::format(__os.getloc(), _GLIBCXX_WIDEN("{:L%F %T}"), __t);
3918 return __os;
3919 }
3920
3921 template<typename _CharT, typename _Traits, typename _Duration,
3922 typename _Alloc = allocator<_CharT>>
3923 inline basic_istream<_CharT, _Traits>&
3924 from_stream(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt,
3925 tai_time<_Duration>& __tp,
3926 basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr,
3927 minutes* __offset = nullptr)
3928 {
3929 minutes __off{};
3930 if (!__offset)
3931 __offset = &__off;
3932 using __format::_ChronoParts;
3933 auto __need = _ChronoParts::_Year | _ChronoParts::_Month
3934 | _ChronoParts::_Day | _ChronoParts::_TimeOfDay;
3935 __detail::_Parser_t<_Duration> __p(__need);
3936 if (__p(__is, __fmt, __abbrev, __offset))
3937 {
3938 if (__p._M_is_leap_second)
3939 __is.setstate(ios_base::failbit);
3940 else
3941 {
3942 constexpr sys_days __epoch(-days(4383)); // 1958y/1/1
3943 auto __d = __p._M_sys_days - __epoch + __p._M_time - *__offset;
3944 tai_time<common_type_t<_Duration, seconds>> __tt(__d);
3945 __tp = __detail::__round<_Duration>(__tt);
3946 }
3947 }
3948 return __is;
3949 }
3950
3951 template<typename _CharT, typename _Traits, typename _Duration>
3952 inline basic_ostream<_CharT, _Traits>&
3953 operator<<(basic_ostream<_CharT, _Traits>& __os,
3954 const gps_time<_Duration>& __t)
3955 {
3956 __os << std::format(__os.getloc(), _GLIBCXX_WIDEN("{:L%F %T}"), __t);
3957 return __os;
3958 }
3959
3960 template<typename _CharT, typename _Traits, typename _Duration,
3961 typename _Alloc = allocator<_CharT>>
3962 inline basic_istream<_CharT, _Traits>&
3963 from_stream(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt,
3964 gps_time<_Duration>& __tp,
3965 basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr,
3966 minutes* __offset = nullptr)
3967 {
3968 minutes __off{};
3969 if (!__offset)
3970 __offset = &__off;
3971 using __format::_ChronoParts;
3972 auto __need = _ChronoParts::_YearMonthDay | _ChronoParts::_TimeOfDay;
3973 __detail::_Parser_t<_Duration> __p(__need);
3974 if (__p(__is, __fmt, __abbrev, __offset))
3975 {
3976 if (__p._M_is_leap_second)
3977 __is.setstate(ios_base::failbit);
3978 else
3979 {
3980 constexpr sys_days __epoch(days(3657)); // 1980y/1/Sunday[1]
3981 auto __d = __p._M_sys_days - __epoch + __p._M_time - *__offset;
3982 gps_time<common_type_t<_Duration, seconds>> __gt(__d);
3983 __tp = __detail::__round<_Duration>(__gt);
3984 }
3985 }
3986 return __is;
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 file_time<_Duration>& __t)
3993 {
3994 __os << std::format(__os.getloc(), _GLIBCXX_WIDEN("{:L%F %T}"), __t);
3995 return __os;
3996 }
3997
3998 template<typename _CharT, typename _Traits, typename _Duration,
3999 typename _Alloc = allocator<_CharT>>
4000 inline basic_istream<_CharT, _Traits>&
4001 from_stream(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt,
4002 file_time<_Duration>& __tp,
4003 basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr,
4004 minutes* __offset = nullptr)
4005 {
4006 sys_time<_Duration> __st;
4007 if (chrono::from_stream(__is, __fmt, __st, __abbrev, __offset))
4008 __tp = __detail::__round<_Duration>(file_clock::from_sys(__st));
4009 return __is;
4010 }
4011
4012 template<typename _CharT, typename _Traits, typename _Duration>
4013 inline basic_ostream<_CharT, _Traits>&
4014 operator<<(basic_ostream<_CharT, _Traits>& __os,
4015 const local_time<_Duration>& __lt)
4016 // _GLIBCXX_RESOLVE_LIB_DEFECTS
4017 // 4257. Stream insertion for chrono::local_time should be constrained
4018 requires requires(const sys_time<_Duration>& __st) { __os << __st; }
4019 {
4020 __os << sys_time<_Duration>{__lt.time_since_epoch()};
4021 return __os;
4022 }
4023
4024 template<typename _CharT, typename _Traits, typename _Duration,
4025 typename _Alloc = allocator<_CharT>>
4026 basic_istream<_CharT, _Traits>&
4027 from_stream(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt,
4028 local_time<_Duration>& __tp,
4029 basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr,
4030 minutes* __offset = nullptr)
4031 {
4032 using __format::_ChronoParts;
4033 auto __need = _ChronoParts::_YearMonthDay | _ChronoParts::_TimeOfDay;
4034 __detail::_Parser_t<_Duration> __p(__need);
4035 if (__p(__is, __fmt, __abbrev, __offset))
4036 {
4037 days __d = __p._M_sys_days.time_since_epoch();
4038 auto __t = local_days(__d) + __p._M_time; // ignore offset
4039 __tp = __detail::__round<_Duration>(__t);
4040 }
4041 return __is;
4042 }
4043
4044 // [time.parse] parsing
4045
4046namespace __detail
4047{
4048 // _GLIBCXX_RESOLVE_LIB_DEFECTS
4049 // 3956. chrono::parse uses from_stream as a customization point
4050 void from_stream() = delete;
4051
4052 template<typename _Parsable, typename _CharT,
4053 typename _Traits = std::char_traits<_CharT>,
4054 typename... _OptArgs>
4055 concept __parsable = requires (basic_istream<_CharT, _Traits>& __is,
4056 const _CharT* __fmt, _Parsable& __tp,
4057 _OptArgs*... __args)
4058 { from_stream(__is, __fmt, __tp, __args...); };
4059
4060 template<typename _Parsable, typename _CharT,
4061 typename _Traits = char_traits<_CharT>,
4062 typename _Alloc = allocator<_CharT>>
4063 struct _Parse
4064 {
4065 private:
4066 using __string_type = basic_string<_CharT, _Traits, _Alloc>;
4067
4068 public:
4069 _Parse(const _CharT* __fmt, _Parsable& __tp,
4070 basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr,
4071 minutes* __offset = nullptr)
4072 : _M_fmt(__fmt), _M_tp(std::__addressof(__tp)),
4073 _M_abbrev(__abbrev), _M_offset(__offset)
4074 { }
4075
4076 _Parse(_Parse&&) = delete;
4077 _Parse& operator=(_Parse&&) = delete;
4078
4079 private:
4080 using __stream_type = basic_istream<_CharT, _Traits>;
4081
4082 const _CharT* const _M_fmt;
4083 _Parsable* const _M_tp;
4084 __string_type* const _M_abbrev;
4085 minutes* const _M_offset;
4086
4087 friend __stream_type&
4088 operator>>(__stream_type& __is, _Parse&& __p)
4089 {
4090 if (__p._M_offset)
4091 from_stream(__is, __p._M_fmt, *__p._M_tp, __p._M_abbrev,
4092 __p._M_offset);
4093 else if (__p._M_abbrev)
4094 from_stream(__is, __p._M_fmt, *__p._M_tp, __p._M_abbrev);
4095 else
4096 from_stream(__is, __p._M_fmt, *__p._M_tp);
4097 return __is;
4098 }
4099
4100 friend void operator>>(__stream_type&, _Parse&) = delete;
4101 friend void operator>>(__stream_type&, const _Parse&) = delete;
4102 };
4103} // namespace __detail
4104
4105 template<typename _CharT, __detail::__parsable<_CharT> _Parsable>
4106 [[nodiscard, __gnu__::__access__(__read_only__, 1)]]
4107 inline auto
4108 parse(const _CharT* __fmt, _Parsable& __tp)
4109 { return __detail::_Parse<_Parsable, _CharT>(__fmt, __tp); }
4110
4111 template<typename _CharT, typename _Traits, typename _Alloc,
4112 __detail::__parsable<_CharT, _Traits> _Parsable>
4113 [[nodiscard]]
4114 inline auto
4115 parse(const basic_string<_CharT, _Traits, _Alloc>& __fmt, _Parsable& __tp)
4116 {
4117 return __detail::_Parse<_Parsable, _CharT, _Traits>(__fmt.c_str(), __tp);
4118 }
4119
4120 template<typename _CharT, typename _Traits, typename _Alloc,
4121 typename _StrT = basic_string<_CharT, _Traits, _Alloc>,
4122 __detail::__parsable<_CharT, _Traits, _StrT> _Parsable>
4123 [[nodiscard, __gnu__::__access__(__read_only__, 1)]]
4124 inline auto
4125 parse(const _CharT* __fmt, _Parsable& __tp,
4126 basic_string<_CharT, _Traits, _Alloc>& __abbrev)
4127 {
4128 auto __pa = std::__addressof(__abbrev);
4129 return __detail::_Parse<_Parsable, _CharT, _Traits, _Alloc>(__fmt, __tp,
4130 __pa);
4131 }
4132
4133 template<typename _CharT, typename _Traits, typename _Alloc,
4134 typename _StrT = basic_string<_CharT, _Traits, _Alloc>,
4135 __detail::__parsable<_CharT, _Traits, _StrT> _Parsable>
4136 [[nodiscard]]
4137 inline auto
4138 parse(const basic_string<_CharT, _Traits, _Alloc>& __fmt, _Parsable& __tp,
4139 basic_string<_CharT, _Traits, _Alloc>& __abbrev)
4140 {
4141 auto __pa = std::__addressof(__abbrev);
4142 return __detail::_Parse<_Parsable, _CharT, _Traits, _Alloc>(__fmt.c_str(),
4143 __tp, __pa);
4144 }
4145
4146 template<typename _CharT, typename _Traits = char_traits<_CharT>,
4147 typename _StrT = basic_string<_CharT, _Traits>,
4148 __detail::__parsable<_CharT, _Traits, _StrT, minutes> _Parsable>
4149 [[nodiscard, __gnu__::__access__(__read_only__, 1)]]
4150 inline auto
4151 parse(const _CharT* __fmt, _Parsable& __tp, minutes& __offset)
4152 {
4153 return __detail::_Parse<_Parsable, _CharT>(__fmt, __tp, nullptr,
4154 &__offset);
4155 }
4156
4157 template<typename _CharT, typename _Traits, typename _Alloc,
4158 typename _StrT = basic_string<_CharT, _Traits>,
4159 __detail::__parsable<_CharT, _Traits, _StrT, minutes> _Parsable>
4160 [[nodiscard]]
4161 inline auto
4162 parse(const basic_string<_CharT, _Traits, _Alloc>& __fmt, _Parsable& __tp,
4163 minutes& __offset)
4164 {
4165 return __detail::_Parse<_Parsable, _CharT, _Traits, _Alloc>(__fmt.c_str(),
4166 __tp, nullptr,
4167 &__offset);
4168 }
4169
4170 template<typename _CharT, typename _Traits, typename _Alloc,
4171 typename _StrT = basic_string<_CharT, _Traits, _Alloc>,
4172 __detail::__parsable<_CharT, _Traits, _StrT, minutes> _Parsable>
4173 [[nodiscard, __gnu__::__access__(__read_only__, 1)]]
4174 inline auto
4175 parse(const _CharT* __fmt, _Parsable& __tp,
4176 basic_string<_CharT, _Traits, _Alloc>& __abbrev, minutes& __offset)
4177 {
4178 auto __pa = std::__addressof(__abbrev);
4179 return __detail::_Parse<_Parsable, _CharT, _Traits, _Alloc>(__fmt, __tp,
4180 __pa,
4181 &__offset);
4182 }
4183
4184 template<typename _CharT, typename _Traits, typename _Alloc,
4185 typename _StrT = basic_string<_CharT, _Traits, _Alloc>,
4186 __detail::__parsable<_CharT, _Traits, _StrT, minutes> _Parsable>
4187 [[nodiscard]]
4188 inline auto
4189 parse(const basic_string<_CharT, _Traits, _Alloc>& __fmt, _Parsable& __tp,
4190 basic_string<_CharT, _Traits, _Alloc>& __abbrev, minutes& __offset)
4191 {
4192 auto __pa = std::__addressof(__abbrev);
4193 return __detail::_Parse<_Parsable, _CharT, _Traits, _Alloc>(__fmt.c_str(),
4194 __tp, __pa,
4195 &__offset);
4196 }
4197
4198 /// @cond undocumented
4199 template<typename _Duration>
4200 template<typename _CharT, typename _Traits, typename _Alloc>
4201 basic_istream<_CharT, _Traits>&
4202 __detail::_Parser<_Duration>::
4203 operator()(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt,
4204 basic_string<_CharT, _Traits, _Alloc>* __abbrev,
4205 minutes* __offset)
4206 {
4207 using sentry = typename basic_istream<_CharT, _Traits>::sentry;
4209 if (sentry __cerb(__is, true); __cerb)
4210 {
4211 locale __loc = __is.getloc();
4212 auto& __tmget = std::use_facet<std::time_get<_CharT>>(__loc);
4213 auto& __tmpunct = std::use_facet<std::__timepunct<_CharT>>(__loc);
4214
4215 // RAII type to save and restore stream state.
4216 struct _Stream_state
4217 {
4218 explicit
4219 _Stream_state(basic_istream<_CharT, _Traits>& __i)
4220 : _M_is(__i),
4221 _M_flags(__i.flags(ios_base::skipws | ios_base::dec)),
4222 _M_w(__i.width(0))
4223 { }
4224
4225 ~_Stream_state()
4226 {
4227 _M_is.flags(_M_flags);
4228 _M_is.width(_M_w);
4229 }
4230
4231 _Stream_state(_Stream_state&&) = delete;
4232
4233 basic_istream<_CharT, _Traits>& _M_is;
4234 ios_base::fmtflags _M_flags;
4235 streamsize _M_w;
4236 };
4237
4238 auto __is_failed = [](ios_base::iostate __e) {
4239 return static_cast<bool>(__e & ios_base::failbit);
4240 };
4241
4242 // Read an unsigned integer from the stream and return it.
4243 // Extract no more than __n digits. Set __err on error.
4244 auto __read_unsigned = [&] (int __n) {
4245 return _S_read_unsigned(__is, __err, __n);
4246 };
4247
4248 // Read a signed integer from the stream and return it.
4249 // Extract no more than __n digits. Set __err on error.
4250 auto __read_signed = [&] (int __n) {
4251 return _S_read_signed(__is, __err, __n);
4252 };
4253
4254 // Read an expected character from the stream.
4255 auto __read_chr = [&__is, &__err] (_CharT __c) {
4256 return _S_read_chr(__is, __err, __c);
4257 };
4258
4259 using __format::_ChronoParts;
4260 _ChronoParts __parts{};
4261
4262 const year __bad_y = --year::min(); // SHRT_MIN
4263 const month __bad_mon(255);
4264 const day __bad_day(255);
4265 const weekday __bad_wday(255);
4266 const hours __bad_h(-1);
4267 const minutes __bad_min(-9999);
4268 const seconds __bad_sec(-1);
4269
4270 year __y = __bad_y, __yy = __bad_y; // %Y, %yy
4271 year __iso_y = __bad_y, __iso_yy = __bad_y; // %G, %g
4272 month __m = __bad_mon; // %m
4273 day __d = __bad_day; // %d
4274 weekday __wday = __bad_wday; // %a %A %u %w
4275 hours __h = __bad_h, __h12 = __bad_h; // %H, %I
4276 minutes __min = __bad_min; // %M
4277 _Duration __s = __bad_sec; // %S
4278 int __ampm = 0; // %p
4279 int __iso_wk = -1, __sunday_wk = -1, __monday_wk = -1; // %V, %U, %W
4280 int __century = -1; // %C
4281 int __dayofyear = -1; // %j (for non-duration)
4282
4283 minutes __tz_offset = __bad_min;
4284 basic_string<_CharT, _Traits> __tz_abbr;
4285
4286 if ((_M_need & _ChronoParts::_TimeOfDay) != 0
4287 && (_M_need & _ChronoParts::_Year) != 0)
4288 {
4289 // For time_points assume "00:00:00" is implicitly present,
4290 // so we don't fail to parse if it's not (PR libstdc++/114240).
4291 // We will still fail to parse if there's no year+month+day.
4292 __h = hours(0);
4293 __parts = _ChronoParts::_TimeOfDay;
4294 }
4295
4296 // bool __is_neg = false; // TODO: how is this handled for parsing?
4297
4298 _CharT __mod{}; // One of 'E' or 'O' or nul.
4299 unsigned __num = 0; // Non-zero for N modifier.
4300 bool __is_flag = false; // True if we're processing a % flag.
4301
4302 constexpr bool __is_floating
4303 = treat_as_floating_point_v<typename _Duration::rep>;
4304
4305 // If an out-of-range value is extracted (e.g. 61min for %M),
4306 // do not set failbit immediately because we might not need it
4307 // (e.g. parsing chrono::year doesn't care about invalid %M values).
4308 // Instead set the variable back to its initial 'bad' state,
4309 // and also set related variables corresponding to the same field
4310 // (e.g. a bad %M value for __min should also reset __h and __s).
4311 // If a valid value is needed later the bad value will cause failure.
4312
4313 // For some fields we don't know the correct range when parsing and
4314 // we have to be liberal in what we accept, e.g. we allow 366 for
4315 // day-of-year because that's valid in leap years, and we allow 31
4316 // for day-of-month. If those values are needed to determine the
4317 // result then we can do a correct range check at the end when we
4318 // know the how many days the relevant year or month actually has.
4319
4320 while (*__fmt)
4321 {
4322 _CharT __c = *__fmt++;
4323 if (!__is_flag)
4324 {
4325 if (__c == '%')
4326 __is_flag = true; // This is the start of a flag.
4327 else if (std::isspace(__c, __loc))
4328 std::ws(__is); // Match zero or more whitespace characters.
4329 else if (!__read_chr(__c)) [[unlikely]]
4330 break; // Failed to match the expected character.
4331
4332 continue; // Process next character in the format string.
4333 }
4334
4335 // Now processing a flag.
4336 switch (__c)
4337 {
4338 case 'a': // Locale's weekday name
4339 case 'A': // (full or abbreviated, matched case-insensitively).
4340 if (__mod || __num) [[unlikely]]
4341 __err = ios_base::failbit;
4342 else
4343 {
4344 struct tm __tm{};
4345 __tmget.get(__is, {}, __is, __err, &__tm,
4346 __fmt - 2, __fmt);
4347 if (!__is_failed(__err))
4348 __wday = weekday(__tm.tm_wday);
4349 }
4350 __parts |= _ChronoParts::_Weekday;
4351 break;
4352
4353 case 'b': // Locale's month name
4354 case 'h': // (full or abbreviated, matched case-insensitively).
4355 case 'B':
4356 if (__mod || __num) [[unlikely]]
4357 __err = ios_base::failbit;
4358 else
4359 {
4360 // strptime behaves differently for %b and %B,
4361 // but chrono::parse says they're equivalent.
4362 // Luckily libstdc++ std::time_get works as needed.
4363 struct tm __tm{};
4364 __tmget.get(__is, {}, __is, __err, &__tm,
4365 __fmt - 2, __fmt);
4366 if (!__is_failed(__err))
4367 __m = month(__tm.tm_mon + 1);
4368 }
4369 __parts |= _ChronoParts::_Month;
4370 break;
4371
4372 case 'c': // Locale's date and time representation.
4373 if (__mod == 'O' || __num) [[unlikely]]
4374 __err |= ios_base::failbit;
4375 else
4376 {
4377 struct tm __tm{};
4378 __tmget.get(__is, {}, __is, __err, &__tm,
4379 __fmt - 2 - (__mod == 'E'), __fmt);
4380 if (!__is_failed(__err))
4381 {
4382 __y = year(__tm.tm_year + 1900);
4383 __m = month(__tm.tm_mon + 1);
4384 __d = day(__tm.tm_mday);
4385 __h = hours(__tm.tm_hour);
4386 __min = minutes(__tm.tm_min);
4387 __s = seconds(__tm.tm_sec);
4388 }
4389 }
4390 __parts |= _ChronoParts::_DateTime;
4391 break;
4392
4393 case 'C': // Century
4394 if (!__mod) [[likely]]
4395 {
4396 auto __v = __read_signed(__num ? __num : 2);
4397 if (!__is_failed(__err))
4398 {
4399 int __cmin = (int)year::min() / 100;
4400 int __cmax = (int)year::max() / 100;
4401 if (__cmin <= __v && __v <= __cmax)
4402 __century = __v * 100;
4403 else
4404 __century = -2; // This prevents guessing century.
4405 }
4406 }
4407 else if (__mod == 'E')
4408 {
4409 struct tm __tm{};
4410 __tmget.get(__is, {}, __is, __err, &__tm,
4411 __fmt - 3, __fmt);
4412 if (!__is_failed(__err))
4413 __century = __tm.tm_year;
4414 }
4415 else [[unlikely]]
4416 __err |= ios_base::failbit;
4417 // N.B. don't set this here: __parts |= _ChronoParts::_Year;
4418 break;
4419
4420 case 'd': // Day of month (1-31)
4421 case 'e':
4422 if (!__mod) [[likely]]
4423 {
4424 auto __v = __read_unsigned(__num ? __num : 2);
4425 if (!__is_failed(__err))
4426 __d = day(__v);
4427 }
4428 else if (__mod == 'O')
4429 {
4430 struct tm __tm{};
4431 __tmget.get(__is, {}, __is, __err, &__tm,
4432 __fmt - 3, __fmt);
4433 if (!__is_failed(__err))
4434 __d = day(__tm.tm_mday);
4435 }
4436 else [[unlikely]]
4437 __err |= ios_base::failbit;
4438 __parts |= _ChronoParts::_Day;
4439 break;
4440
4441 case 'D': // %m/%d/%y
4442 if (__mod || __num) [[unlikely]]
4443 __err |= ios_base::failbit;
4444 else
4445 {
4446 auto __month = __read_unsigned(2); // %m
4447 __read_chr('/');
4448 auto __day = __read_unsigned(2); // %d
4449 __read_chr('/');
4450 auto __year = __read_unsigned(2); // %y
4451 if (__is_failed(__err))
4452 break;
4453 __y = year(__year + 1900 + 100 * int(__year < 69));
4454 __m = month(__month);
4455 __d = day(__day);
4456 if (!year_month_day(__y, __m, __d).ok())
4457 {
4458 __y = __yy = __iso_y = __iso_yy = __bad_y;
4459 __m = __bad_mon;
4460 __d = __bad_day;
4461 break;
4462 }
4463 }
4464 __parts |= _ChronoParts::_Date;
4465 break;
4466
4467 case 'F': // %Y-%m-%d - any N modifier only applies to %Y.
4468 if (__mod) [[unlikely]]
4469 __err |= ios_base::failbit;
4470 else
4471 {
4472 auto __year = __read_signed(__num ? __num : 4); // %Y
4473 __read_chr('-');
4474 auto __month = __read_unsigned(2); // %m
4475 __read_chr('-');
4476 auto __day = __read_unsigned(2); // %d
4477 if (__is_failed(__err))
4478 break;
4479 __y = year(__year);
4480 __m = month(__month);
4481 __d = day(__day);
4482 if (!year_month_day(__y, __m, __d).ok())
4483 {
4484 __y = __yy = __iso_y = __iso_yy = __bad_y;
4485 __m = __bad_mon;
4486 __d = __bad_day;
4487 break;
4488 }
4489 }
4490 __parts |= _ChronoParts::_Date;
4491 break;
4492
4493 case 'g': // Last two digits of ISO week-based year.
4494 if (__mod) [[unlikely]]
4495 __err |= ios_base::failbit;
4496 else
4497 {
4498 auto __val = __read_unsigned(__num ? __num : 2);
4499 if (__val >= 0 && __val <= 99)
4500 {
4501 __iso_yy = year(__val);
4502 if (__century == -1) // No %C has been parsed yet.
4503 __century = 2000;
4504 }
4505 else
4506 __iso_yy = __iso_y = __y = __yy = __bad_y;
4507 }
4508 __parts |= _ChronoParts::_Year;
4509 break;
4510
4511 case 'G': // ISO week-based year.
4512 if (__mod) [[unlikely]]
4513 __err |= ios_base::failbit;
4514 else
4515 __iso_y = year(__read_unsigned(__num ? __num : 4));
4516 __parts |= _ChronoParts::_Year;
4517 break;
4518
4519 case 'H': // 24-hour (00-23)
4520 case 'I': // 12-hour (1-12)
4521 if (__mod == 'E') [[unlikely]]
4522 __err |= ios_base::failbit;
4523 else if (__mod == 'O')
4524 {
4525#if 0
4526 struct tm __tm{};
4527 __tm.tm_ampm = 1;
4528 __tmget.get(__is, {}, __is, __err, &__tm,
4529 __fmt - 3, __fmt);
4530 if (!__is_failed(__err))
4531 {
4532 if (__c == 'I')
4533 {
4534 __h12 = hours(__tm.tm_hour);
4535 __h = __bad_h;
4536 }
4537 else
4538 __h = hours(__tm.tm_hour);
4539 }
4540#else
4541 // XXX %OI seems to be unimplementable.
4542 __err |= ios_base::failbit;
4543#endif
4544 }
4545 else
4546 {
4547 auto __val = __read_unsigned(__num ? __num : 2);
4548 if (__c == 'I' && __val >= 1 && __val <= 12)
4549 {
4550 __h12 = hours(__val);
4551 __h = __bad_h;
4552 }
4553 else if (__c == 'H' && __val >= 0 && __val <= 23)
4554 {
4555 __h = hours(__val);
4556 __h12 = __bad_h;
4557 }
4558 else
4559 {
4560 if ((_M_need & _ChronoParts::_TimeOfDay) != 0)
4561 __err |= ios_base::failbit;
4562 break;
4563 }
4564 }
4565 __parts |= _ChronoParts::_TimeOfDay;
4566 break;
4567
4568 case 'j': // For duration, count of days, otherwise day of year
4569 if (__mod) [[unlikely]]
4570 __err |= ios_base::failbit;
4571 else if (_M_need == _ChronoParts::_TimeOfDay) // duration
4572 {
4573 auto __val = __read_signed(__num ? __num : 3);
4574 if (!__is_failed(__err))
4575 {
4576 __h = days(__val); // __h will get added to _M_time
4577 __parts |= _ChronoParts::_TimeOfDay;
4578 }
4579 }
4580 else
4581 {
4582 __dayofyear = __read_unsigned(__num ? __num : 3);
4583 // N.B. do not alter __parts here, done after loop.
4584 // No need for range checking here either.
4585 }
4586 break;
4587
4588 case 'm': // Month (1-12)
4589 if (__mod == 'E') [[unlikely]]
4590 __err |= ios_base::failbit;
4591 else if (__mod == 'O')
4592 {
4593 struct tm __tm{};
4594 __tmget.get(__is, {}, __is, __err, &__tm,
4595 __fmt - 2, __fmt);
4596 if (!__is_failed(__err))
4597 __m = month(__tm.tm_mon + 1);
4598 }
4599 else
4600 {
4601 auto __val = __read_unsigned(__num ? __num : 2);
4602 if (__val >= 1 && __val <= 12)
4603 __m = month(__val);
4604 else
4605 __m = __bad_mon;
4606 }
4607 __parts |= _ChronoParts::_Month;
4608 break;
4609
4610 case 'M': // Minutes
4611 if (__mod == 'E') [[unlikely]]
4612 __err |= ios_base::failbit;
4613 else if (__mod == 'O')
4614 {
4615 struct tm __tm{};
4616 __tmget.get(__is, {}, __is, __err, &__tm,
4617 __fmt - 2, __fmt);
4618 if (!__is_failed(__err))
4619 __min = minutes(__tm.tm_min);
4620 }
4621 else
4622 {
4623 auto __val = __read_unsigned(__num ? __num : 2);
4624 if (0 <= __val && __val < 60)
4625 __min = minutes(__val);
4626 else
4627 {
4628 if ((_M_need & _ChronoParts::_TimeOfDay) != 0)
4629 __err |= ios_base::failbit;
4630 break;
4631 }
4632 }
4633 __parts |= _ChronoParts::_TimeOfDay;
4634 break;
4635
4636 case 'p': // Locale's AM/PM designation for 12-hour clock.
4637 if (__mod || __num)
4638 __err |= ios_base::failbit;
4639 else
4640 {
4641 // Can't use std::time_get here as it can't parse %p
4642 // in isolation without %I. This might be faster anyway.
4643 const _CharT* __ampms[2];
4644 __tmpunct._M_am_pm(__ampms);
4645 int __n = 0, __which = 3;
4646 while (__which != 0)
4647 {
4648 auto __i = __is.peek();
4649 if (_Traits::eq_int_type(__i, _Traits::eof()))
4650 {
4652 break;
4653 }
4654 __i = std::toupper(_Traits::to_char_type(__i), __loc);
4655 if (__which & 1)
4656 {
4657 if (__i != std::toupper(__ampms[0][__n], __loc))
4658 __which ^= 1;
4659 else if (__ampms[0][__n + 1] == _CharT())
4660 {
4661 __which = 1;
4662 (void) __is.get();
4663 break;
4664 }
4665 }
4666 if (__which & 2)
4667 {
4668 if (__i != std::toupper(__ampms[1][__n], __loc))
4669 __which ^= 2;
4670 else if (__ampms[1][__n + 1] == _CharT())
4671 {
4672 __which = 2;
4673 (void) __is.get();
4674 break;
4675 }
4676 }
4677 if (__which)
4678 (void) __is.get();
4679 ++__n;
4680 }
4681 if (__which == 0 || __which == 3)
4682 __err |= ios_base::failbit;
4683 else
4684 __ampm = __which;
4685 }
4686 break;
4687
4688 case 'r': // Locale's 12-hour time.
4689 if (__mod || __num)
4690 __err |= ios_base::failbit;
4691 else
4692 {
4693 struct tm __tm{};
4694 __tmget.get(__is, {}, __is, __err, &__tm,
4695 __fmt - 2, __fmt);
4696 if (!__is_failed(__err))
4697 {
4698 __h = hours(__tm.tm_hour);
4699 __min = minutes(__tm.tm_min);
4700 __s = seconds(__tm.tm_sec);
4701 }
4702 }
4703 __parts |= _ChronoParts::_TimeOfDay;
4704 break;
4705
4706 case 'R': // %H:%M
4707 case 'T': // %H:%M:%S
4708 if (__mod || __num) [[unlikely]]
4709 {
4710 __err |= ios_base::failbit;
4711 break;
4712 }
4713 else
4714 {
4715 auto __val = __read_unsigned(2);
4716 if (__val == -1 || __val > 23) [[unlikely]]
4717 {
4718 if ((_M_need & _ChronoParts::_TimeOfDay) != 0)
4719 __err |= ios_base::failbit;
4720 break;
4721 }
4722 if (!__read_chr(':')) [[unlikely]]
4723 break;
4724 __h = hours(__val);
4725
4726 __val = __read_unsigned(2);
4727 if (__val == -1 || __val > 60) [[unlikely]]
4728 {
4729 if ((_M_need & _ChronoParts::_TimeOfDay) != 0)
4730 __err |= ios_base::failbit;
4731 break;
4732 }
4733 __min = minutes(__val);
4734
4735 if (__c == 'R')
4736 {
4737 __parts |= _ChronoParts::_TimeOfDay;
4738 break;
4739 }
4740 else if (!__read_chr(':')) [[unlikely]]
4741 break;
4742 }
4743 [[fallthrough]];
4744
4745 case 'S': // Seconds
4746 if (__mod == 'E') [[unlikely]]
4747 __err |= ios_base::failbit;
4748 else if (__mod == 'O')
4749 {
4750 struct tm __tm{};
4751 __tmget.get(__is, {}, __is, __err, &__tm,
4752 __fmt - 3, __fmt);
4753 if (!__is_failed(__err))
4754 __s = seconds(__tm.tm_sec);
4755 }
4756 else if constexpr (_Duration::period::den == 1
4757 && !__is_floating)
4758 {
4759 auto __val = __read_unsigned(__num ? __num : 2);
4760 if (0 <= __val && __val <= 59) [[likely]]
4761 __s = seconds(__val);
4762 else
4763 {
4764 if ((_M_need & _ChronoParts::_TimeOfDay) != 0)
4765 __err |= ios_base::failbit;
4766 break;
4767 }
4768 }
4769 else // Read fractional seconds
4770 {
4771 basic_stringstream<_CharT> __buf;
4772 auto __digit = _S_try_read_digit(__is, __err);
4773 if (__digit != -1)
4774 {
4775 __buf.put(_CharT('0') + __digit);
4776 __digit = _S_try_read_digit(__is, __err);
4777 if (__digit != -1)
4778 __buf.put(_CharT('0') + __digit);
4779 }
4780
4781 auto __i = __is.peek();
4782 if (_Traits::eq_int_type(__i, _Traits::eof()))
4783 __err |= ios_base::eofbit;
4784 else
4785 {
4786 _CharT __dp = '.';
4787 if (__loc != locale::classic())
4788 {
4789 auto& __np = use_facet<numpunct<_CharT>>(__loc);
4790 __dp = __np.decimal_point();
4791 }
4792 _CharT __c = _Traits::to_char_type(__i);
4793 if (__c == __dp)
4794 {
4795 (void) __is.get();
4796 __buf.put('.');
4797 int __prec
4798 = hh_mm_ss<_Duration>::fractional_width;
4799 do
4800 {
4801 __digit = _S_try_read_digit(__is, __err);
4802 if (__digit != -1)
4803 __buf.put(_CharT('0') + __digit);
4804 else
4805 break;
4806 }
4807 while (--__prec);
4808 }
4809 }
4810
4811 if (!__is_failed(__err)) [[likely]]
4812 {
4813 long double __val{};
4814#if __cpp_lib_to_chars
4815 string __str = std::move(__buf).str();
4816 auto __first = __str.data();
4817 auto __last = __first + __str.size();
4818 using enum chars_format;
4819 auto [ptr, ec] = std::from_chars(__first, __last,
4820 __val, fixed);
4821 if ((bool)ec || ptr != __last) [[unlikely]]
4822 __err |= ios_base::failbit;
4823 else
4824#else
4825 if (__buf >> __val)
4826#endif
4827 {
4828 duration<long double> __fs(__val);
4829 if constexpr (__is_floating)
4830 __s = __fs;
4831 else
4832 __s = chrono::round<_Duration>(__fs);
4833 }
4834 }
4835 }
4836 __parts |= _ChronoParts::_TimeOfDay;
4837 break;
4838
4839 case 'u': // ISO weekday (1-7)
4840 case 'w': // Weekday (0-6)
4841 if (__mod == 'E') [[unlikely]]
4842 __err |= ios_base::failbit;
4843 else if (__mod == 'O')
4844 {
4845 if (__c == 'w')
4846 {
4847 struct tm __tm{};
4848 __tmget.get(__is, {}, __is, __err, &__tm,
4849 __fmt - 3, __fmt);
4850 if (!__is_failed(__err))
4851 __wday = weekday(__tm.tm_wday);
4852 }
4853 else
4854 __err |= ios_base::failbit;
4855 }
4856 else
4857 {
4858 const int __lo = __c == 'u' ? 1 : 0;
4859 const int __hi = __lo + 6;
4860 auto __val = __read_unsigned(__num ? __num : 1);
4861 if (__lo <= __val && __val <= __hi)
4862 __wday = weekday(__val);
4863 else
4864 {
4865 __wday = __bad_wday;
4866 break;
4867 }
4868 }
4869 __parts |= _ChronoParts::_Weekday;
4870 break;
4871
4872 case 'U': // Week number of the year (from first Sunday).
4873 case 'V': // ISO week-based week number.
4874 case 'W': // Week number of the year (from first Monday).
4875 if (__mod == 'E') [[unlikely]]
4876 __err |= ios_base::failbit;
4877 else if (__mod == 'O')
4878 {
4879 if (__c == 'V') [[unlikely]]
4880 __err |= ios_base::failbit;
4881 else
4882 {
4883 // TODO nl_langinfo_l(ALT_DIGITS) ?
4884 // Not implementable using std::time_get.
4885 }
4886 }
4887 else
4888 {
4889 const int __lo = __c == 'V' ? 1 : 0;
4890 const int __hi = 53;
4891 auto __val = __read_unsigned(__num ? __num : 2);
4892 if (__lo <= __val && __val <= __hi)
4893 {
4894 switch (__c)
4895 {
4896 case 'U':
4897 __sunday_wk = __val;
4898 break;
4899 case 'V':
4900 __iso_wk = __val;
4901 break;
4902 case 'W':
4903 __monday_wk = __val;
4904 break;
4905 }
4906 }
4907 else
4908 __iso_wk = __sunday_wk = __monday_wk = -1;
4909 }
4910 // N.B. do not alter __parts here, done after loop.
4911 break;
4912
4913 case 'x': // Locale's date representation.
4914 if (__mod == 'O' || __num) [[unlikely]]
4915 __err |= ios_base::failbit;
4916 else
4917 {
4918 struct tm __tm{};
4919 __tmget.get(__is, {}, __is, __err, &__tm,
4920 __fmt - 2 - (__mod == 'E'), __fmt);
4921 if (!__is_failed(__err))
4922 {
4923 __y = year(__tm.tm_year + 1900);
4924 __m = month(__tm.tm_mon + 1);
4925 __d = day(__tm.tm_mday);
4926 }
4927 }
4928 __parts |= _ChronoParts::_Date;
4929 break;
4930
4931 case 'X': // Locale's time representation.
4932 if (__mod == 'O' || __num) [[unlikely]]
4933 __err |= ios_base::failbit;
4934 else
4935 {
4936 struct tm __tm{};
4937 __tmget.get(__is, {}, __is, __err, &__tm,
4938 __fmt - 2 - (__mod == 'E'), __fmt);
4939 if (!__is_failed(__err))
4940 {
4941 __h = hours(__tm.tm_hour);
4942 __min = minutes(__tm.tm_min);
4943 __s = seconds(__tm.tm_sec);
4944 }
4945 }
4946 __parts |= _ChronoParts::_TimeOfDay;
4947 break;
4948
4949 case 'y': // Last two digits of year.
4950 if (__mod) [[unlikely]]
4951 {
4952 struct tm __tm{};
4953 __tmget.get(__is, {}, __is, __err, &__tm,
4954 __fmt - 3, __fmt);
4955 if (!__is_failed(__err))
4956 {
4957 int __cent = __tm.tm_year < 2000 ? 1900 : 2000;
4958 __yy = year(__tm.tm_year - __cent);
4959 if (__century == -1) // No %C has been parsed yet.
4960 __century = __cent;
4961 }
4962 }
4963 else
4964 {
4965 auto __val = __read_unsigned(__num ? __num : 2);
4966 if (__val >= 0 && __val <= 99)
4967 {
4968 __yy = year(__val);
4969 if (__century == -1) // No %C has been parsed yet.
4970 __century = __val < 69 ? 2000 : 1900;
4971 }
4972 else
4973 __y = __yy = __iso_yy = __iso_y = __bad_y;
4974 }
4975 __parts |= _ChronoParts::_Year;
4976 break;
4977
4978 case 'Y': // Year
4979 if (__mod == 'O') [[unlikely]]
4980 __err |= ios_base::failbit;
4981 else if (__mod == 'E')
4982 {
4983 struct tm __tm{};
4984 __tmget.get(__is, {}, __is, __err, &__tm,
4985 __fmt - 3, __fmt);
4986 if (!__is_failed(__err))
4987 __y = year(__tm.tm_year);
4988 }
4989 else
4990 {
4991 auto __val = __read_unsigned(__num ? __num : 4);
4992 if (!__is_failed(__err))
4993 __y = year(__val);
4994 }
4995 __parts |= _ChronoParts::_Year;
4996 break;
4997
4998 case 'z':
4999 if (__num) [[unlikely]]
5000 __err |= ios_base::failbit;
5001 else
5002 {
5003 // For %Ez and %Oz read [+|-][h]h[:mm].
5004 // For %z read [+|-]hh[mm].
5005
5006 auto __i = __is.peek();
5007 if (_Traits::eq_int_type(__i, _Traits::eof()))
5008 {
5010 break;
5011 }
5012 _CharT __ic = _Traits::to_char_type(__i);
5013 const bool __neg = __ic == _CharT('-');
5014 if (__ic == _CharT('-') || __ic == _CharT('+'))
5015 (void) __is.get();
5016
5017 int_least32_t __hh;
5018 if (__mod)
5019 {
5020 // Read h[h]
5021 __hh = __read_unsigned(2);
5022 }
5023 else
5024 {
5025 // Read hh
5026 __hh = 10 * _S_try_read_digit(__is, __err);
5027 __hh += _S_try_read_digit(__is, __err);
5028 }
5029
5030 if (__is_failed(__err))
5031 break;
5032
5033 __i = __is.peek();
5034 if (_Traits::eq_int_type(__i, _Traits::eof()))
5035 {
5036 __err |= ios_base::eofbit;
5037 __tz_offset = minutes(__hh * (__neg ? -60 : 60));
5038 break;
5039 }
5040 __ic = _Traits::to_char_type(__i);
5041
5042 bool __read_mm = false;
5043 if (__mod)
5044 {
5045 if (__ic == _GLIBCXX_WIDEN(":")[0])
5046 {
5047 // Read [:mm] part.
5048 (void) __is.get();
5049 __read_mm = true;
5050 }
5051 }
5052 else if (_CharT('0') <= __ic && __ic <= _CharT('9'))
5053 {
5054 // Read [mm] part.
5055 __read_mm = true;
5056 }
5057
5058 int_least32_t __mm = 0;
5059 if (__read_mm)
5060 {
5061 __mm = 10 * _S_try_read_digit(__is, __err);
5062 __mm += _S_try_read_digit(__is, __err);
5063 }
5064
5065 if (!__is_failed(__err))
5066 {
5067 auto __z = __hh * 60 + __mm;
5068 __tz_offset = minutes(__neg ? -__z : __z);
5069 }
5070 }
5071 break;
5072
5073 case 'Z':
5074 if (__mod || __num) [[unlikely]]
5075 __err |= ios_base::failbit;
5076 else
5077 {
5078 basic_string_view<_CharT> __x = _GLIBCXX_WIDEN("_/-+");
5079 __tz_abbr.clear();
5080 while (true)
5081 {
5082 auto __i = __is.peek();
5083 if (!_Traits::eq_int_type(__i, _Traits::eof()))
5084 {
5085 _CharT __a = _Traits::to_char_type(__i);
5086 if (std::isalnum(__a, __loc)
5087 || __x.find(__a) != __x.npos)
5088 {
5089 __tz_abbr.push_back(__a);
5090 (void) __is.get();
5091 continue;
5092 }
5093 }
5094 else
5095 __err |= ios_base::eofbit;
5096 break;
5097 }
5098 if (__tz_abbr.empty())
5099 __err |= ios_base::failbit;
5100 }
5101 break;
5102
5103 case 'n': // Exactly one whitespace character.
5104 if (__mod || __num) [[unlikely]]
5105 __err |= ios_base::failbit;
5106 else
5107 {
5108 _CharT __i = __is.peek();
5109 if (_Traits::eq_int_type(__i, _Traits::eof()))
5111 else if (std::isspace(_Traits::to_char_type(__i), __loc))
5112 (void) __is.get();
5113 else
5114 __err |= ios_base::failbit;
5115 }
5116 break;
5117
5118 case 't': // Zero or one whitespace characters.
5119 if (__mod || __num) [[unlikely]]
5120 __err |= ios_base::failbit;
5121 else
5122 {
5123 _CharT __i = __is.peek();
5124 if (_Traits::eq_int_type(__i, _Traits::eof()))
5125 __err |= ios_base::eofbit;
5126 else if (std::isspace(_Traits::to_char_type(__i), __loc))
5127 (void) __is.get();
5128 }
5129 break;
5130
5131 case '%': // A % character.
5132 if (__mod || __num) [[unlikely]]
5133 __err |= ios_base::failbit;
5134 else
5135 __read_chr('%');
5136 break;
5137
5138 case 'O': // Modifiers
5139 case 'E':
5140 if (__mod || __num) [[unlikely]]
5141 {
5142 __err |= ios_base::failbit;
5143 break;
5144 }
5145 __mod = __c;
5146 continue;
5147
5148 default:
5149 if (_CharT('1') <= __c && __c <= _CharT('9'))
5150 {
5151 if (!__mod) [[likely]]
5152 {
5153 // %Nx - extract positive decimal integer N
5154 auto __end = __fmt + _Traits::length(__fmt);
5155 auto [__v, __ptr]
5156 = __format::__parse_integer(__fmt - 1, __end);
5157 if (__ptr) [[likely]]
5158 {
5159 __num = __v;
5160 __fmt = __ptr;
5161 continue;
5162 }
5163 }
5164 }
5165 __err |= ios_base::failbit;
5166 }
5167
5168 if (__is_failed(__err)) [[unlikely]]
5169 break;
5170
5171 __is_flag = false;
5172 __num = 0;
5173 __mod = _CharT();
5174 }
5175
5176 if (__century >= 0)
5177 {
5178 if (__yy != __bad_y && __y == __bad_y)
5179 __y = years(__century) + __yy; // Use %y instead of %Y
5180 if (__iso_yy != __bad_y && __iso_y == __bad_y)
5181 __iso_y = years(__century) + __iso_yy; // Use %g instead of %G
5182 }
5183
5184 bool __can_use_doy = false;
5185 bool __can_use_iso_wk = false;
5186 bool __can_use_sun_wk = false;
5187 bool __can_use_mon_wk = false;
5188
5189 // A year + day-of-year can be converted to a full date.
5190 if (__y != __bad_y && __dayofyear >= 0)
5191 {
5192 __can_use_doy = true;
5193 __parts |= _ChronoParts::_Date;
5194 }
5195 else if (__y != __bad_y && __wday != __bad_wday && __sunday_wk >= 0)
5196 {
5197 __can_use_sun_wk = true;
5198 __parts |= _ChronoParts::_Date;
5199 }
5200 else if (__y != __bad_y && __wday != __bad_wday && __monday_wk >= 0)
5201 {
5202 __can_use_mon_wk = true;
5203 __parts |= _ChronoParts::_Date;
5204 }
5205 else if (__iso_y != __bad_y && __wday != __bad_wday && __iso_wk > 0)
5206 {
5207 // An ISO week date can be converted to a full date.
5208 __can_use_iso_wk = true;
5209 __parts |= _ChronoParts::_Date;
5210 }
5211
5212 if (__is_failed(__err)) [[unlikely]]
5213 ; // Don't bother doing any more work.
5214 else if (__is_flag) [[unlikely]] // incomplete format flag
5215 __err |= ios_base::failbit;
5216 else if ((_M_need & __parts) == _M_need) [[likely]]
5217 {
5218 // We try to avoid calculating _M_sys_days and _M_ymd unless
5219 // necessary, because converting sys_days to year_month_day
5220 // (or vice versa) requires non-trivial calculations.
5221 // If we have y/m/d values then use them to populate _M_ymd
5222 // and only convert it to _M_sys_days if the caller needs that.
5223 // But if we don't have y/m/d and need to calculate the date
5224 // from the day-of-year or a week+weekday then we set _M_sys_days
5225 // and only convert it to _M_ymd if the caller needs that.
5226
5227 // We do more error checking here, but only for the fields that
5228 // we actually need to use. For example, we will not diagnose
5229 // an invalid dayofyear==366 for non-leap years unless actually
5230 // using __dayofyear. This should mean we never produce invalid
5231 // results, but it means not all invalid inputs are diagnosed,
5232 // e.g. "2023-01-01 366" >> "%F %j" ignores the invalid 366.
5233 // We also do not diagnose inconsistent values for the same
5234 // field, e.g. "2021 2022 2023" >> "%C%y %Y %Y" just uses 2023.
5235
5236 // Whether the caller wants _M_wd.
5237 // The _Weekday bit is only set for chrono::weekday.
5238 const bool __need_wday = (_M_need & _ChronoParts::_Weekday) != 0;
5239
5240 // Whether the caller wants _M_sys_days and _M_time.
5241 // Only true for durations and time_points.
5242 const bool __need_time = (_M_need & _ChronoParts::_TimeOfDay) != 0;
5243
5244 if (__need_wday && __wday != __bad_wday)
5245 _M_wd = __wday; // Caller only wants a weekday and we have one.
5246 else if ((_M_need & _ChronoParts::_Date) != 0) // subsumes __need_wday
5247 {
5248 // Whether the caller wants _M_ymd.
5249 // True for chrono::year etc., false for time_points.
5250 const bool __need_ymd = !__need_wday && !__need_time;
5251
5252 if (((_M_need & _ChronoParts::_Year) != 0 && __y == __bad_y)
5253 || ((_M_need & _ChronoParts::_Month) != 0 && __m == __bad_mon)
5254 || ((_M_need & _ChronoParts::_Day) != 0 && __d == __bad_day))
5255 {
5256 // Missing at least one of y/m/d so calculate sys_days
5257 // from the other data we have available.
5258
5259 if (__can_use_doy)
5260 {
5261 if ((0 < __dayofyear && __dayofyear <= 365)
5262 || (__dayofyear == 366 && __y.is_leap()))
5263 [[likely]]
5264 {
5265 _M_sys_days = sys_days(__y/January/1)
5266 + days(__dayofyear - 1);
5267 if (__need_ymd)
5268 _M_ymd = year_month_day(_M_sys_days);
5269 }
5270 else
5271 __err |= ios_base::failbit;
5272 }
5273 else if (__can_use_iso_wk)
5274 {
5275 // Calculate y/m/d from ISO week date.
5276
5277 if (__iso_wk == 53)
5278 {
5279 // A year has 53 weeks iff Jan 1st is a Thursday
5280 // or Jan 1 is a Wednesday and it's a leap year.
5281 const sys_days __jan4(__iso_y/January/4);
5282 weekday __wd1(__jan4 - days(3));
5283 if (__wd1 != Thursday)
5284 if (__wd1 != Wednesday || !__iso_y.is_leap())
5285 __err |= ios_base::failbit;
5286 }
5287
5288 if (!__is_failed(__err)) [[likely]]
5289 {
5290 // First Thursday is always in week one:
5291 sys_days __w(Thursday[1]/January/__iso_y);
5292 // First day of week-based year:
5293 __w -= Thursday - Monday;
5294 __w += days(weeks(__iso_wk - 1));
5295 __w += __wday - Monday;
5296 _M_sys_days = __w;
5297
5298 if (__need_ymd)
5299 _M_ymd = year_month_day(_M_sys_days);
5300 }
5301 }
5302 else if (__can_use_sun_wk)
5303 {
5304 // Calculate y/m/d from week number + weekday.
5305 sys_days __wk1(__y/January/Sunday[1]);
5306 _M_sys_days = __wk1 + weeks(__sunday_wk - 1)
5307 + days(__wday.c_encoding());
5308 _M_ymd = year_month_day(_M_sys_days);
5309 if (_M_ymd.year() != __y) [[unlikely]]
5310 __err |= ios_base::failbit;
5311 }
5312 else if (__can_use_mon_wk)
5313 {
5314 // Calculate y/m/d from week number + weekday.
5315 sys_days __wk1(__y/January/Monday[1]);
5316 _M_sys_days = __wk1 + weeks(__monday_wk - 1)
5317 + days(__wday.c_encoding() - 1);
5318 _M_ymd = year_month_day(_M_sys_days);
5319 if (_M_ymd.year() != __y) [[unlikely]]
5320 __err |= ios_base::failbit;
5321 }
5322 else // Should not be able to get here.
5323 __err |= ios_base::failbit;
5324 }
5325 else
5326 {
5327 // We know that all fields the caller needs are present,
5328 // but check that their values are in range.
5329 // Make unwanted fields valid so that _M_ymd.ok() is true.
5330
5331 if ((_M_need & _ChronoParts::_Year) != 0)
5332 {
5333 if (!__y.ok()) [[unlikely]]
5334 __err |= ios_base::failbit;
5335 }
5336 else if (__y == __bad_y)
5337 __y = 1972y; // Leap year so that Feb 29 is valid.
5338
5339 if ((_M_need & _ChronoParts::_Month) != 0)
5340 {
5341 if (!__m.ok()) [[unlikely]]
5342 __err |= ios_base::failbit;
5343 }
5344 else if (__m == __bad_mon)
5345 __m = January;
5346
5347 if ((_M_need & _ChronoParts::_Day) != 0)
5348 {
5349 if (__d < day(1) || __d > (__y/__m/last).day())
5350 __err |= ios_base::failbit;
5351 }
5352 else if (__d == __bad_day)
5353 __d = 1d;
5354
5355 if (year_month_day __ymd(__y, __m, __d); __ymd.ok())
5356 {
5357 _M_ymd = __ymd;
5358 if (__need_wday || __need_time)
5359 _M_sys_days = sys_days(_M_ymd);
5360 }
5361 else [[unlikely]]
5362 __err |= ios_base::failbit;
5363 }
5364
5365 if (__need_wday)
5366 _M_wd = weekday(_M_sys_days);
5367 }
5368
5369 // Need to set _M_time for both durations and time_points.
5370 if (__need_time)
5371 {
5372 if (__h == __bad_h && __h12 != __bad_h)
5373 {
5374 if (__ampm == 1)
5375 __h = __h12 == hours(12) ? hours(0) : __h12;
5376 else if (__ampm == 2)
5377 __h = __h12 == hours(12) ? __h12 : __h12 + hours(12);
5378 else [[unlikely]]
5379 __err |= ios_base::failbit;
5380 }
5381
5382 auto __t = _M_time.zero();
5383 bool __ok = false;
5384
5385 if (__h != __bad_h)
5386 {
5387 __ok = true;
5388 __t += __h;
5389 }
5390
5391 if (__min != __bad_min)
5392 {
5393 __ok = true;
5394 __t += __min;
5395 }
5396
5397 if (__s != __bad_sec)
5398 {
5399 __ok = true;
5400 __t += __s;
5401 _M_is_leap_second = __s >= seconds(60);
5402 }
5403
5404 if (__ok)
5405 _M_time = __t;
5406 else
5407 __err |= ios_base::failbit;
5408 }
5409
5410 if (!__is_failed(__err)) [[likely]]
5411 {
5412 if (__offset && __tz_offset != __bad_min)
5413 *__offset = __tz_offset;
5414 if (__abbrev && !__tz_abbr.empty())
5415 *__abbrev = std::move(__tz_abbr);
5416 }
5417 }
5418 else
5419 __err |= ios_base::failbit;
5420 }
5421 if (__err)
5422 __is.setstate(__err);
5423 return __is;
5424 }
5425 /// @endcond
5426#undef _GLIBCXX_WIDEN
5427
5428 /// @} group chrono
5429} // namespace chrono
5430
5431_GLIBCXX_END_NAMESPACE_VERSION
5432} // namespace std
5433
5434#endif // C++20
5435
5436#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.