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