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