libstdc++
chrono_io.h
Go to the documentation of this file.
1// <chrono> Formatting -*- C++ -*-
2
3// Copyright The GNU Toolchain Authors.
4//
5// This file is part of the GNU ISO C++ Library. This library is free
6// software; you can redistribute it and/or modify it under the
7// terms of the GNU General Public License as published by the
8// Free Software Foundation; either version 3, or (at your option)
9// any later version.
10
11// This library is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
16// Under Section 7 of GPL version 3, you are granted additional
17// permissions described in the GCC Runtime Library Exception, version
18// 3.1, as published by the Free Software Foundation.
19
20// You should have received a copy of the GNU General Public License and
21// a copy of the GCC Runtime Library Exception along with this program;
22// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23// <http://www.gnu.org/licenses/>.
24
25/** @file include/bits/chrono_io.h
26 * This is an internal header file, included by other library headers.
27 * Do not attempt to use it directly. @headername{chrono}
28 */
29
30#ifndef _GLIBCXX_CHRONO_IO_H
31#define _GLIBCXX_CHRONO_IO_H 1
32
33#pragma GCC system_header
34
35#if __cplusplus >= 202002L
36
37#include <sstream> // ostringstream
38#include <iomanip> // setw, setfill
39#include <format>
40
42
43namespace std _GLIBCXX_VISIBILITY(default)
44{
45_GLIBCXX_BEGIN_NAMESPACE_VERSION
46
47namespace chrono
48{
49/// @addtogroup chrono
50/// @{
51
52/// @cond undocumented
53namespace __detail
54{
55 // STATICALLY-WIDEN, see C++20 [time.general]
56 // It doesn't matter for format strings (which can only be char or wchar_t)
57 // but this returns the narrow string for anything that isn't wchar_t. This
58 // is done because const char* can be inserted into any ostream type, and
59 // will be widened at runtime if necessary.
60 template<typename _CharT>
61 consteval auto
62 _Widen(const char* __narrow, const wchar_t* __wide)
63 {
64 if constexpr (is_same_v<_CharT, wchar_t>)
65 return __wide;
66 else
67 return __narrow;
68 }
69#define _GLIBCXX_WIDEN_(C, S) ::std::chrono::__detail::_Widen<C>(S, L##S)
70#define _GLIBCXX_WIDEN(S) _GLIBCXX_WIDEN_(_CharT, S)
71
72 template<typename _Period, typename _CharT>
74 __units_suffix() noexcept
75 {
76 // The standard say these are all narrow strings, which would need to
77 // be widened at run-time when inserted into a wide stream. We use
78 // STATICALLY-WIDEN to widen at compile-time.
79#define _GLIBCXX_UNITS_SUFFIX(period, suffix) \
80 if constexpr (is_same_v<_Period, period>) \
81 return _GLIBCXX_WIDEN(suffix); \
82 else
83
89#if _GLIBCXX_USE_ALT_MICROSECONDS_SUFFIX
90 // Deciding this at compile-time is wrong, maybe use nl_langinfo(CODESET)
91 // to check runtime environment and return u8"\u00b5s", "\xb5s", or "us".
93#else
95#endif
111#undef _GLIBCXX_UNITS_SUFFIX
112 return {};
113 }
114
115 template<typename _Period, typename _CharT, typename _Out>
116 inline _Out
117 __fmt_units_suffix(_Out __out) noexcept
118 {
119 if (auto __s = __detail::__units_suffix<_Period, _CharT>(); __s.size())
120 return __format::__write(std::move(__out), __s);
121 else if constexpr (_Period::den == 1)
123 (uintmax_t)_Period::num);
124 else
125 return std::format_to(std::move(__out), _GLIBCXX_WIDEN("[{}/{}]s"),
126 (uintmax_t)_Period::num,
127 (uintmax_t)_Period::den);
128 }
129} // namespace __detail
130/// @endcond
131
132 /** Write a `chrono::duration` to an ostream.
133 *
134 * @since C++20
135 */
136 template<typename _CharT, typename _Traits,
137 typename _Rep, typename _Period>
140 const duration<_Rep, _Period>& __d)
141 {
143 using period = typename _Period::type;
145 __s.flags(__os.flags());
146 __s.imbue(__os.getloc());
147 __s.precision(__os.precision());
148 __s << __d.count();
149 __detail::__fmt_units_suffix<period, _CharT>(_Out(__s));
150 __os << std::move(__s).str();
151 return __os;
152 }
153
154/// @cond undocumented
155namespace __detail
156{
157 // An unspecified type returned by `chrono::local_time_format`.
158 template<typename _Duration>
159 struct __local_time_fmt
160 {
162 const string* _M_abbrev;
163 const seconds* _M_offset_sec;
164 };
165
166 struct __local_fmt_t;
167}
168/// @endcond
169
170 /** Return an object that asssociates timezone info with a local time.
171 *
172 * A `chrono::local_time` object has no timezone associated with it. This
173 * function creates an object that allows formatting a `local_time` as
174 * though it refers to a timezone with the given abbreviated name and
175 * offset from UTC.
176 *
177 * @since C++20
178 */
179 template<typename _Duration>
180 inline __detail::__local_time_fmt<_Duration>
182 const string* __abbrev = nullptr,
183 const seconds* __offset_sec = nullptr)
184 { return {__time, __abbrev, __offset_sec}; }
185
186 /// @}
187} // namespace chrono
188
189/// @cond undocumented
190namespace __format
191{
192 [[noreturn,__gnu__::__always_inline__]]
193 inline void
194 __no_timezone_available()
195 { __throw_format_error("format error: no timezone available for %Z or %z"); }
196
197 [[noreturn,__gnu__::__always_inline__]]
198 inline void
199 __not_valid_for_duration()
200 { __throw_format_error("format error: chrono-format-spec not valid for "
201 "chrono::duration"); }
202
203 [[noreturn,__gnu__::__always_inline__]]
204 inline void
205 __invalid_chrono_spec()
206 { __throw_format_error("format error: chrono-format-spec not valid for "
207 "argument type"); }
208
209 template<typename _CharT>
210 struct _ChronoSpec : _Spec<_CharT>
211 {
212 basic_string_view<_CharT> _M_chrono_specs;
213 };
214
215 // Represents the information provided by a chrono type.
216 // e.g. month_weekday has month and weekday but no year or time of day,
217 // hh_mm_ss has time of day but no date, sys_time is time_point+timezone.
218 enum _ChronoParts {
219 _Year = 1, _Month = 2, _Day = 4, _Weekday = 8, _TimeOfDay = 16,
220 _TimeZone = 32,
221 _Date = _Year | _Month | _Day | _Weekday,
222 _DateTime = _Date | _TimeOfDay,
223 _ZonedDateTime = _DateTime | _TimeZone,
224 _Duration = 128 // special case
225 };
226
227 constexpr _ChronoParts
228 operator|(_ChronoParts __x, _ChronoParts __y)
229 { return static_cast<_ChronoParts>((int)__x | (int)__y); }
230
231 // TODO rename this to chrono::__formatter? or chrono::__detail::__formatter?
232 template<typename _CharT>
233 struct __formatter_chrono
234 {
235 using __string_view = basic_string_view<_CharT>;
236 using __string = basic_string<_CharT>;
237
238 template<typename _ParseContext>
239 constexpr typename _ParseContext::iterator
240 _M_parse(_ParseContext& __pc, _ChronoParts __parts)
241 {
242 auto __first = __pc.begin();
243 auto __last = __pc.end();
244
245 _ChronoSpec<_CharT> __spec{};
246
247 auto __finalize = [this, &__spec] {
248 _M_spec = __spec;
249 };
250
251 auto __finished = [&] {
252 if (__first == __last || *__first == '}')
253 {
254 __finalize();
255 return true;
256 }
257 return false;
258 };
259
260 if (__finished())
261 return __first;
262
263 __first = __spec._M_parse_fill_and_align(__first, __last);
264 if (__finished())
265 return __first;
266
267 __first = __spec._M_parse_width(__first, __last, __pc);
268 if (__finished())
269 return __first;
270
271 if (__parts & _ChronoParts::_Duration)
272 {
273 __first = __spec._M_parse_precision(__first, __last, __pc);
274 if (__finished())
275 return __first;
276 }
277
278 __first = __spec._M_parse_locale(__first, __last);
279 if (__finished())
280 return __first;
281
282 // Everything up to the end of the string or the first '}' is a
283 // chrono-specs string. Check it is valid.
284 {
285 __string_view __str(__first, __last - __first);
286 auto __end = __str.find('}');
287 if (__end != __str.npos)
288 {
289 __str.remove_suffix(__str.length() - __end);
290 __last = __first + __end;
291 }
292 if (__str.find('{') != __str.npos)
293 __throw_format_error("chrono format error: '{' in chrono-specs");
294 }
295
296 // Parse chrono-specs in [first,last), checking each conversion-spec
297 // against __parts (so fail for %Y if no year in parts).
298 // Save range in __spec._M_chrono_specs.
299
300 const auto __chrono_specs = __first++; // Skip leading '%'
301 if (*__chrono_specs != '%')
302 __throw_format_error("chrono format error: no '%' at start of "
303 "chrono-specs");
304
305 _CharT __mod{};
306 bool __conv = true;
307 int __needed = 0;
308
309 while (__first != __last)
310 {
311 enum _Mods { _Mod_none, _Mod_E, _Mod_O, _Mod_E_O };
312 _Mods __allowed_mods = _Mod_none;
313
314 _CharT __c = *__first++;
315 switch (__c)
316 {
317 case 'a':
318 case 'A':
319 __needed = _Weekday;
320 break;
321 case 'b':
322 case 'h':
323 case 'B':
324 __needed = _Month;
325 break;
326 case 'c':
327 __needed = _DateTime;
328 __allowed_mods = _Mod_E;
329 break;
330 case 'C':
331 __needed = _Year;
332 __allowed_mods = _Mod_E;
333 break;
334 case 'd':
335 case 'e':
336 __needed = _Day;
337 __allowed_mods = _Mod_O;
338 break;
339 case 'D':
340 case 'F':
341 __needed = _Date;
342 break;
343 case 'g':
344 case 'G':
345 __needed = _Date;
346 break;
347 case 'H':
348 case 'I':
349 __needed = _TimeOfDay;
350 __allowed_mods = _Mod_O;
351 break;
352 case 'j':
353 if (!(__parts & _Duration))
354 __needed = _Date;
355 break;
356 case 'm':
357 __needed = _Month;
358 __allowed_mods = _Mod_O;
359 break;
360 case 'M':
361 __needed = _TimeOfDay;
362 __allowed_mods = _Mod_O;
363 break;
364 case 'p':
365 case 'r':
366 case 'R':
367 case 'T':
368 __needed = _TimeOfDay;
369 break;
370 case 'q':
371 case 'Q':
372 __needed = _Duration;
373 break;
374 case 'S':
375 __needed = _TimeOfDay;
376 __allowed_mods = _Mod_O;
377 break;
378 case 'u':
379 case 'w':
380 __needed = _Weekday;
381 __allowed_mods = _Mod_O;
382 break;
383 case 'U':
384 case 'V':
385 case 'W':
386 __needed = _Date;
387 __allowed_mods = _Mod_O;
388 break;
389 case 'x':
390 __needed = _Date;
391 __allowed_mods = _Mod_E;
392 break;
393 case 'X':
394 __needed = _TimeOfDay;
395 __allowed_mods = _Mod_E;
396 break;
397 case 'y':
398 __needed = _Year;
399 __allowed_mods = _Mod_E_O;
400 break;
401 case 'Y':
402 __needed = _Year;
403 __allowed_mods = _Mod_E;
404 break;
405 case 'z':
406 __needed = _TimeZone;
407 __allowed_mods = _Mod_E_O;
408 break;
409 case 'Z':
410 __needed = _TimeZone;
411 break;
412 case 'n':
413 case 't':
414 case '%':
415 break;
416 case 'O':
417 case 'E':
418 if (__mod) [[unlikely]]
419 {
420 __allowed_mods = _Mod_none;
421 break;
422 }
423 __mod = __c;
424 continue;
425 default:
426 __throw_format_error("chrono format error: invalid "
427 " specifier in chrono-specs");
428 }
429
430 if ((__mod == 'E' && !(__allowed_mods & _Mod_E))
431 || (__mod == 'O' && !(__allowed_mods & _Mod_O)))
432 __throw_format_error("chrono format error: invalid "
433 " modifier in chrono-specs");
434 __mod = _CharT();
435
436 if ((__parts & __needed) != __needed)
437 __throw_format_error("chrono format error: format argument "
438 "does not contain the information "
439 "required by the chrono-specs");
440
441 // Scan for next '%', ignoring literal-chars before it.
442 size_t __pos = __string_view(__first, __last - __first).find('%');
443 if (__pos == 0)
444 ++__first;
445 else
446 {
447 if (__pos == __string_view::npos)
448 {
449 __first = __last;
450 __conv = false;
451 }
452 else
453 __first += __pos + 1;
454 }
455 }
456
457 // Check for a '%' conversion-spec without a type.
458 if (__conv || __mod != _CharT())
459 __throw_format_error("chrono format error: unescaped '%' in "
460 "chrono-specs");
461
462 _M_spec = __spec;
463 _M_spec._M_chrono_specs
464 = __string_view(__chrono_specs, __first - __chrono_specs);
465
466 return __first;
467 }
468
469 // TODO this function template is instantiated for every different _Tp.
470 // Consider creating a polymorphic interface for calendar types so
471 // that we instantiate fewer different specializations. Similar to
472 // _Sink_iter for std::format. Replace each _S_year, _S_day etc. with
473 // member functions of that type.
474 template<typename _Tp, typename _FormatContext>
475 typename _FormatContext::iterator
476 _M_format(const _Tp& __t, _FormatContext& __fc,
477 bool __is_neg = false) const
478 {
479 auto __first = _M_spec._M_chrono_specs.begin();
480 const auto __last = _M_spec._M_chrono_specs.end();
481 if (__first == __last)
482 return _M_format_to_ostream(__t, __fc, __is_neg);
483
484 _Sink_iter<_CharT> __out;
485 __format::_Str_sink<_CharT> __sink;
486 bool __write_direct = false;
487 if constexpr (is_same_v<typename _FormatContext::iterator,
488 _Sink_iter<_CharT>>)
489 {
490 if (_M_spec._M_width_kind == __format::_WP_none)
491 {
492 __out = __fc.out();
493 __write_direct = true;
494 }
495 else
496 __out = __sink.out();
497 }
498 else
499 __out = __sink.out();
500
501 // formatter<duration> passes the correct value of __is_neg
502 // for durations but for hh_mm_ss we decide it here.
503 if constexpr (__is_specialization_of<_Tp, chrono::hh_mm_ss>)
504 __is_neg = __t.is_negative();
505
506 auto __print_sign = [&__is_neg, &__out] {
507 if constexpr (chrono::__is_duration_v<_Tp>
508 || __is_specialization_of<_Tp, chrono::hh_mm_ss>)
509 if (__is_neg)
510 {
511 *__out++ = _S_plus_minus[1];
512 __is_neg = false;
513 }
514 return std::move(__out);
515 };
516
517 // Characters to output for "%n", "%t" and "%%" specifiers.
518 constexpr const _CharT* __literals = _GLIBCXX_WIDEN("\n\t%");
519
520 ++__first; // Skip leading '%' at start of chrono-specs.
521
522 _CharT __mod{};
523 do
524 {
525 _CharT __c = *__first++;
526 switch (__c)
527 {
528 case 'a':
529 case 'A':
530 __out = _M_a_A(__t, std::move(__out), __fc, __c == 'A');
531 break;
532 case 'b':
533 case 'h':
534 case 'B':
535 __out = _M_b_B(__t, std::move(__out), __fc, __c == 'B');
536 break;
537 case 'c':
538 __out = _M_c(__t, std::move(__out), __fc, __mod == 'E');
539 break;
540 case 'C':
541 case 'y':
542 case 'Y':
543 __out = _M_C_y_Y(__t, std::move(__out), __fc, __c, __mod);
544 break;
545 case 'd':
546 case 'e':
547 __out = _M_d_e(__t, std::move(__out), __fc, __c, __mod == 'O');
548 break;
549 case 'D':
550 __out = _M_D(__t, std::move(__out), __fc);
551 break;
552 case 'F':
553 __out = _M_F(__t, std::move(__out), __fc);
554 break;
555 case 'g':
556 case 'G':
557 __out = _M_g_G(__t, std::move(__out), __fc, __c == 'G');
558 break;
559 case 'H':
560 case 'I':
561 __out = _M_H_I(__t, __print_sign(), __fc, __c, __mod == 'O');
562 break;
563 case 'j':
564 __out = _M_j(__t, __print_sign(), __fc);
565 break;
566 case 'm':
567 __out = _M_m(__t, std::move(__out), __fc, __mod == 'O');
568 break;
569 case 'M':
570 __out = _M_M(__t, __print_sign(), __fc, __mod == 'O');
571 break;
572 case 'p':
573 __out = _M_p(__t, std::move(__out), __fc);
574 break;
575 case 'q':
576 __out = _M_q(__t, std::move(__out), __fc);
577 break;
578 case 'Q':
579 // %Q The duration's numeric value.
580 if constexpr (chrono::__is_duration_v<_Tp>)
581 __out = std::format_to(__print_sign(), _S_empty_spec,
582 __t.count());
583 else
584 __throw_format_error("chrono format error: argument is "
585 "not a duration");
586 break;
587 case 'r':
588 __out = _M_r(__t, __print_sign(), __fc);
589 break;
590 case 'R':
591 case 'T':
592 __out = _M_R_T(__t, __print_sign(), __fc, __c == 'T');
593 break;
594 case 'S':
595 __out = _M_S(__t, __print_sign(), __fc, __mod == 'O');
596 break;
597 case 'u':
598 case 'w':
599 __out = _M_u_w(__t, std::move(__out), __fc, __c, __mod == 'O');
600 break;
601 case 'U':
602 case 'V':
603 case 'W':
604 __out = _M_U_V_W(__t, std::move(__out), __fc, __c,
605 __mod == 'O');
606 break;
607 case 'x':
608 __out = _M_x(__t, std::move(__out), __fc, __mod == 'E');
609 break;
610 case 'X':
611 __out = _M_X(__t, __print_sign(), __fc, __mod == 'E');
612 break;
613 case 'z':
614 __out = _M_z(__t, std::move(__out), __fc, (bool)__mod);
615 break;
616 case 'Z':
617 __out = _M_Z(__t, std::move(__out), __fc);
618 break;
619 case 'n':
620 *__out++ = __literals[0];
621 break;
622 case 't':
623 *__out++ = __literals[1];
624 break;
625 case '%':
626 *__out++ = __literals[2];
627 break;
628 case 'O':
629 case 'E':
630 __mod = __c;
631 continue;
632 case '}':
633 __first = __last;
634 break;
635 }
636 __mod = _CharT();
637 // Scan for next '%' and write out everything before it.
638 __string_view __str(__first, __last - __first);
639 size_t __pos = __str.find('%');
640 if (__pos == 0)
641 ++__first;
642 else
643 {
644 if (__pos == __str.npos)
645 __first = __last;
646 else
647 {
648 __str.remove_suffix(__str.length() - __pos);
649 __first += __pos + 1;
650 }
651 __out = __format::__write(std::move(__out), __str);
652 }
653 }
654 while (__first != __last);
655
656 if constexpr (is_same_v<typename _FormatContext::iterator,
657 _Sink_iter<_CharT>>)
658 if (__write_direct)
659 return __out;
660
661 auto __str = std::move(__sink).get();
662 return __format::__write_padded_as_spec(__str, __str.size(),
663 __fc, _M_spec);
664 }
665
666 _ChronoSpec<_CharT> _M_spec;
667
668 private:
669 // Return the formatting locale.
670 template<typename _FormatContext>
672 _M_locale(_FormatContext& __fc) const
673 {
674 if (!_M_spec._M_localized)
675 return std::locale::classic();
676 else
677 return __fc.locale();
678 }
679
680 // Format for empty chrono-specs, e.g. "{}" (C++20 [time.format] p6).
681 // TODO: consider moving body of every operator<< into this function
682 // and use std::format("{}", t) to implement those operators. That
683 // would avoid std::format("{}", t) calling operator<< which calls
684 // std::format again.
685 template<typename _Tp, typename _FormatContext>
686 typename _FormatContext::iterator
687 _M_format_to_ostream(const _Tp& __t, _FormatContext& __fc,
688 bool __is_neg) const
689 {
690 using ::std::chrono::__detail::__utc_leap_second;
691 using ::std::chrono::__detail::__local_time_fmt;
692
693 if constexpr (__is_specialization_of<_Tp, __local_time_fmt>)
694 return _M_format_to_ostream(__t._M_time, __fc, false);
695 else
696 {
697 basic_ostringstream<_CharT> __os;
698 __os.imbue(_M_locale(__fc));
699
700 if constexpr (__is_specialization_of<_Tp, __utc_leap_second>)
701 __os << __t._M_date << ' ' << __t._M_time;
702 else if constexpr (chrono::__is_time_point_v<_Tp>)
703 {
704 // Need to be careful here because not all specializations
705 // of chrono::sys_time can be written to an ostream.
706 // For the specializations of time_point that can be
707 // formatted with an empty chrono-specs, either it's a
708 // sys_time with period greater or equal to days:
709 if constexpr (is_convertible_v<_Tp, chrono::sys_days>)
710 __os << _S_date(__t);
711 else // Or it's formatted as "{:L%F %T}":
712 {
713 auto __days = chrono::floor<chrono::days>(__t);
714 __os << chrono::year_month_day(__days) << ' '
715 << chrono::hh_mm_ss(__t - __days);
716 }
717 }
718 else
719 {
720 if constexpr (chrono::__is_duration_v<_Tp>)
721 if (__is_neg) [[unlikely]]
722 __os << _S_plus_minus[1];
723 __os << __t;
724 }
725
726 auto __str = std::move(__os).str();
727 return __format::__write_padded_as_spec(__str, __str.size(),
728 __fc, _M_spec);
729 }
730 }
731
732 static constexpr const _CharT* _S_chars
733 = _GLIBCXX_WIDEN("0123456789+-:/ {}");
734 static constexpr const _CharT* _S_plus_minus = _S_chars + 10;
735 static constexpr _CharT _S_colon = _S_chars[12];
736 static constexpr _CharT _S_slash = _S_chars[13];
737 static constexpr _CharT _S_space = _S_chars[14];
738 static constexpr const _CharT* _S_empty_spec = _S_chars + 15;
739
740 template<typename _Tp, typename _FormatContext>
741 typename _FormatContext::iterator
742 _M_a_A(const _Tp& __t, typename _FormatContext::iterator __out,
743 _FormatContext& __ctx, bool __full) const
744 {
745 // %a Locale's abbreviated weekday name.
746 // %A Locale's full weekday name.
747 chrono::weekday __wd = _S_weekday(__t);
748 if (!__wd.ok())
749 __throw_format_error("format error: invalid weekday");
750
751 locale __loc = _M_locale(__ctx);
752 const auto& __tp = use_facet<__timepunct<_CharT>>(__loc);
753 const _CharT* __days[7];
754 if (__full)
755 __tp._M_days(__days);
756 else
757 __tp._M_days_abbreviated(__days);
758 __string_view __str(__days[__wd.c_encoding()]);
759 return __format::__write(std::move(__out), __str);
760 }
761
762 template<typename _Tp, typename _FormatContext>
763 typename _FormatContext::iterator
764 _M_b_B(const _Tp& __t, typename _FormatContext::iterator __out,
765 _FormatContext& __ctx, bool __full) const
766 {
767 // %b Locale's abbreviated month name.
768 // %B Locale's full month name.
769 chrono::month __m = _S_month(__t);
770 if (!__m.ok())
771 __throw_format_error("format error: invalid month");
772 locale __loc = _M_locale(__ctx);
773 const auto& __tp = use_facet<__timepunct<_CharT>>(__loc);
774 const _CharT* __months[12];
775 if (__full)
776 __tp._M_months(__months);
777 else
778 __tp._M_months_abbreviated(__months);
779 __string_view __str(__months[(unsigned)__m - 1]);
780 return __format::__write(std::move(__out), __str);
781 }
782
783 template<typename _Tp, typename _FormatContext>
784 typename _FormatContext::iterator
785 _M_c(const _Tp& __tt, typename _FormatContext::iterator __out,
786 _FormatContext& __ctx, bool __mod = false) const
787 {
788 // %c Locale's date and time representation.
789 // %Ec Locale's alternate date and time representation.
790
791 auto __t = _S_floor_seconds(__tt);
792 locale __loc = _M_locale(__ctx);
793 const auto& __tp = use_facet<__timepunct<_CharT>>(__loc);
794 const _CharT* __formats[2];
795 __tp._M_date_time_formats(__formats);
796 const _CharT* __rep = __formats[__mod];
797 if (!*__rep)
798 __rep = _GLIBCXX_WIDEN("%a %b %e %H:%M:%S %Y");
799 basic_string<_CharT> __fmt(_S_empty_spec);
800 __fmt.insert(1u, 1u, _S_colon);
801 __fmt.insert(2u, __rep);
802 return std::vformat_to(std::move(__out), __loc, __fmt,
804 }
805
806 template<typename _Tp, typename _FormatContext>
807 typename _FormatContext::iterator
808 _M_C_y_Y(const _Tp& __t, typename _FormatContext::iterator __out,
809 _FormatContext& __ctx, _CharT __conv, _CharT __mod = 0) const
810 {
811 // %C Year divided by 100 using floored division.
812 // %EC Locale's alternative preresentation of the century (era name).
813 // %y Last two decimal digits of the year.
814 // %Oy Locale's alternative representation.
815 // %Ey Locale's alternative representation of offset from %EC.
816 // %Y Year as a decimal number.
817 // %EY Locale's alternative full year representation.
818
819 chrono::year __y = _S_year(__t);
820
821 if (__mod) [[unlikely]]
822 {
823 struct tm __tm{};
824 __tm.tm_year = (int)__y - 1900;
825 return _M_locale_fmt(std::move(__out), _M_locale(__ctx), __tm,
826 __conv, __mod);
827 }
828
829 basic_string<_CharT> __s;
830 int __yi = (int)__y;
831 const bool __is_neg = __yi < 0;
832 __yi = __builtin_abs(__yi);
833
834 if (__conv == 'Y' || __conv == 'C')
835 {
836 int __ci = __yi / 100;
837 if (__is_neg) [[unlikely]]
838 {
839 __s.assign(1, _S_plus_minus[1]);
840 // For floored division -123//100 is -2 and -100//100 is -1
841 if (__conv == 'C' && (__ci * 100) != __yi)
842 ++__ci;
843 }
844 if (__ci >= 100) [[unlikely]]
845 {
846 __s += std::format(_S_empty_spec, __ci / 100);
847 __ci %= 100;
848 }
849 __s += _S_two_digits(__ci);
850 }
851
852 if (__conv == 'Y' || __conv == 'y')
853 __s += _S_two_digits(__yi % 100);
854
855 return __format::__write(std::move(__out), __string_view(__s));
856 }
857
858 template<typename _Tp, typename _FormatContext>
859 typename _FormatContext::iterator
860 _M_D(const _Tp& __t, typename _FormatContext::iterator __out,
861 _FormatContext&) const
862 {
863 auto __ymd = _S_date(__t);
864 basic_string<_CharT> __s;
865#if ! _GLIBCXX_USE_CXX11_ABI
866 __s.reserve(8);
867#endif
868 __s = _S_two_digits((unsigned)__ymd.month());
869 __s += _S_slash;
870 __s += _S_two_digits((unsigned)__ymd.day());
871 __s += _S_slash;
872 __s += _S_two_digits(__builtin_abs((int)__ymd.year()) % 100);
873 return __format::__write(std::move(__out), __string_view(__s));
874 }
875
876 template<typename _Tp, typename _FormatContext>
877 typename _FormatContext::iterator
878 _M_d_e(const _Tp& __t, typename _FormatContext::iterator __out,
879 _FormatContext& __ctx, _CharT __conv, bool __mod = false) const
880 {
881 // %d The day of month as a decimal number.
882 // %Od Locale's alternative representation.
883 // %e Day of month as decimal number, padded with space.
884 // %Oe Locale's alternative digits.
885
886 chrono::day __d = _S_day(__t);
887 unsigned __i = (unsigned)__d;
888
889 if (__mod) [[unlikely]]
890 {
891 struct tm __tm{};
892 __tm.tm_mday = __i;
893 return _M_locale_fmt(std::move(__out), _M_locale(__ctx), __tm,
894 (char)__conv, 'O');
895 }
896
897 auto __sv = _S_two_digits(__i);
898 _CharT __buf[2];
899 if (__conv == _CharT('e') && __i < 10)
900 {
901 __buf[0] = _S_space;
902 __buf[1] = __sv[1];
903 __sv = {__buf, 2};
904 }
905 return __format::__write(std::move(__out), __sv);
906 }
907
908 template<typename _Tp, typename _FormatContext>
909 typename _FormatContext::iterator
910 _M_F(const _Tp& __t, typename _FormatContext::iterator __out,
911 _FormatContext&) const
912 {
913 auto __ymd = _S_date(__t);
914 basic_string<_CharT> __s;
915#if ! _GLIBCXX_USE_CXX11_ABI
916 __s.reserve(11);
917#endif
918 __s += std::format(_GLIBCXX_WIDEN("{:04d}- - "), (int)__ymd.year());
919 auto __sv = _S_two_digits((unsigned)__ymd.month());
920 __s[__s.size() - 5] = __sv[0];
921 __s[__s.size() - 4] = __sv[1];
922 __sv = _S_two_digits((unsigned)__ymd.day());
923 __s[__s.size() - 2] = __sv[0];
924 __s[__s.size() - 1] = __sv[1];
925 __sv = __s;
926 return __format::__write(std::move(__out), __sv);
927 }
928
929 template<typename _Tp, typename _FormatContext>
930 typename _FormatContext::iterator
931 _M_g_G(const _Tp& __t, typename _FormatContext::iterator __out,
932 _FormatContext& __ctx, bool __full) const
933 {
934 // %g last two decimal digits of the ISO week-based year.
935 // %G ISO week-based year.
936 using namespace chrono;
937 auto __d = _S_days(__t);
938 // Move to nearest Thursday:
939 __d -= (weekday(__d) - Monday) - days(3);
940 // ISO week-based year is the year that contains that Thursday:
941 year __y = year_month_day(__d).year();
942 return _M_C_y_Y(__y, std::move(__out), __ctx, "yY"[__full]);
943 }
944
945 template<typename _Tp, typename _FormatContext>
946 typename _FormatContext::iterator
947 _M_H_I(const _Tp& __t, typename _FormatContext::iterator __out,
948 _FormatContext& __ctx, _CharT __conv, bool __mod = false) const
949 {
950 // %H The hour (24-hour clock) as a decimal number.
951 // %OH Locale's alternative representation.
952 // %I The hour (12-hour clock) as a decimal number.
953 // %OI Locale's alternative representation.
954
955 const auto __hms = _S_hms(__t);
956 int __i = __hms.hours().count();
957
958 if (__mod) [[unlikely]]
959 {
960 struct tm __tm{};
961 __tm.tm_hour = __i;
962 return _M_locale_fmt(std::move(__out), _M_locale(__ctx), __tm,
963 (char)__conv, 'O');
964 }
965
966 if (__conv == _CharT('I'))
967 {
968 if (__i == 0)
969 __i = 12;
970 else if (__i > 12)
971 __i -= 12;
972 }
973 return __format::__write(std::move(__out), _S_two_digits(__i));
974 }
975
976 template<typename _Tp, typename _FormatContext>
977 typename _FormatContext::iterator
978 _M_j(const _Tp& __t, typename _FormatContext::iterator __out,
979 _FormatContext&) const
980 {
981 if constexpr (chrono::__is_duration_v<_Tp>)
982 {
983 // Decimal number of days, without padding.
984 unsigned __d = chrono::duration_cast<chrono::days>(__t).count();
985 return std::format_to(std::move(__out), _S_empty_spec, __d);
986 }
987 else
988 {
989 // Day of the year as a decimal number, padding with zero.
990 using namespace chrono;
991 auto __day = _S_days(__t);
992 auto __ymd = _S_date(__t);
993 days __d;
994 // See "Calculating Ordinal Dates" at
995 // https://github.com/HowardHinnant/date/wiki/Examples-and-Recipes
996 if constexpr (is_same_v<typename decltype(__day)::clock, local_t>)
997 __d = __day - local_days(__ymd.year()/January/0);
998 else
999 __d = __day - sys_days(__ymd.year()/January/0);
1000 return std::format_to(std::move(__out), _GLIBCXX_WIDEN("{:03d}"),
1001 __d.count());
1002 }
1003 }
1004
1005 template<typename _Tp, typename _FormatContext>
1006 typename _FormatContext::iterator
1007 _M_m(const _Tp& __t, typename _FormatContext::iterator __out,
1008 _FormatContext& __ctx, bool __mod) const
1009 {
1010 // %m month as a decimal number.
1011 // %Om Locale's alternative representation.
1012
1013 auto __m = _S_month(__t);
1014 auto __i = (unsigned)__m;
1015
1016 if (__mod) [[unlikely]] // %Om
1017 {
1018 struct tm __tm{};
1019 __tm.tm_mon = __i - 1;
1020 return _M_locale_fmt(std::move(__out), _M_locale(__ctx), __tm,
1021 'm', 'O');
1022 }
1023
1024 return __format::__write(std::move(__out), _S_two_digits(__i));
1025 }
1026
1027 template<typename _Tp, typename _FormatContext>
1028 typename _FormatContext::iterator
1029 _M_M(const _Tp& __t, typename _FormatContext::iterator __out,
1030 _FormatContext& __ctx, bool __mod) const
1031 {
1032 // %M The minute as a decimal number.
1033 // %OM Locale's alternative representation.
1034
1035 auto __m = _S_hms(__t).minutes();
1036 auto __i = __m.count();
1037
1038 if (__mod) [[unlikely]] // %OM
1039 {
1040 struct tm __tm{};
1041 __tm.tm_min = __i;
1042 return _M_locale_fmt(std::move(__out), _M_locale(__ctx), __tm,
1043 'M', 'O');
1044 }
1045
1046 return __format::__write(std::move(__out), _S_two_digits(__i));
1047 }
1048
1049 template<typename _Tp, typename _FormatContext>
1050 typename _FormatContext::iterator
1051 _M_p(const _Tp& __t, typename _FormatContext::iterator __out,
1052 _FormatContext& __ctx) const
1053 {
1054 // %p The locale's equivalent of the AM/PM designations.
1055 auto __hms = _S_hms(__t);
1056 locale __loc = _M_locale(__ctx);
1057 const auto& __tp = use_facet<__timepunct<_CharT>>(__loc);
1058 const _CharT* __ampm[2];
1059 __tp._M_am_pm(__ampm);
1060 return std::format_to(std::move(__out), _S_empty_spec,
1061 __ampm[__hms.hours().count() >= 12]);
1062 }
1063
1064 template<typename _Tp, typename _FormatContext>
1065 typename _FormatContext::iterator
1066 _M_q(const _Tp&, typename _FormatContext::iterator __out,
1067 _FormatContext&) const
1068 {
1069 // %q The duration's unit suffix
1070 if constexpr (!chrono::__is_duration_v<_Tp>)
1071 __throw_format_error("format error: argument is not a duration");
1072 else
1073 {
1074 namespace __d = chrono::__detail;
1075 using period = typename _Tp::period;
1076 return __d::__fmt_units_suffix<period, _CharT>(std::move(__out));
1077 }
1078 }
1079
1080 // %Q handled in _M_format
1081
1082 template<typename _Tp, typename _FormatContext>
1083 typename _FormatContext::iterator
1084 _M_r(const _Tp& __tt, typename _FormatContext::iterator __out,
1085 _FormatContext& __ctx) const
1086 {
1087 // %r locale's 12-hour clock time.
1088 auto __t = _S_floor_seconds(__tt);
1089 locale __loc = _M_locale(__ctx);
1090 const auto& __tp = use_facet<__timepunct<_CharT>>(__loc);
1091 const _CharT* __ampm_fmt;
1092 __tp._M_am_pm_format(&__ampm_fmt);
1093 basic_string<_CharT> __fmt(_S_empty_spec);
1094 __fmt.insert(1u, 1u, _S_colon);
1095 __fmt.insert(2u, __ampm_fmt);
1096 return std::vformat_to(std::move(__out), __fmt,
1098 }
1099
1100 template<typename _Tp, typename _FormatContext>
1101 typename _FormatContext::iterator
1102 _M_R_T(const _Tp& __t, typename _FormatContext::iterator __out,
1103 _FormatContext& __ctx, bool __secs) const
1104 {
1105 // %R Equivalent to %H:%M
1106 // %T Equivalent to %H:%M:%S
1107 auto __hms = _S_hms(__t);
1108
1109 basic_string<_CharT> __s;
1110#if ! _GLIBCXX_USE_CXX11_ABI
1111 __s.reserve(11);
1112#endif
1113 __s = std::format(_GLIBCXX_WIDEN("{:02d}:00"), __hms.hours().count());
1114 auto __sv = _S_two_digits(__hms.minutes().count());
1115 __s[__s.size() - 2] = __sv[0];
1116 __s[__s.size() - 1] = __sv[1];
1117 __sv = __s;
1118 __out = __format::__write(std::move(__out), __sv);
1119 if (__secs)
1120 {
1121 *__out++ = _S_colon;
1122 __out = _M_S(__hms, std::move(__out), __ctx);
1123 }
1124 return __out;
1125 }
1126
1127 template<typename _Tp, typename _FormatContext>
1128 typename _FormatContext::iterator
1129 _M_S(const _Tp& __t, typename _FormatContext::iterator __out,
1130 _FormatContext& __ctx, bool __mod = false) const
1131 {
1132 // %S Seconds as a decimal number.
1133 // %OS The locale's alternative representation.
1134 auto __hms = _S_hms(__t);
1135
1136 if (__mod) [[unlikely]] // %OS
1137 {
1138 struct tm __tm{};
1139 __tm.tm_sec = (int)__hms.seconds().count();
1140 return _M_locale_fmt(std::move(__out), _M_locale(__ctx), __tm,
1141 'S', 'O');
1142 }
1143
1144 if constexpr (__hms.fractional_width == 0)
1145 __out = __format::__write(std::move(__out),
1146 _S_two_digits(__hms.seconds().count()));
1147 else
1148 {
1149 locale __loc = _M_locale(__ctx);
1150 auto __s = __hms.seconds();
1151 auto __ss = __hms.subseconds();
1152 using rep = typename decltype(__ss)::rep;
1153 if constexpr (is_floating_point_v<rep>)
1154 {
1155 chrono::duration<rep> __fs = __s + __ss;
1156 __out = std::format_to(std::move(__out), __loc,
1157 _GLIBCXX_WIDEN("{:#0{}.{}Lf}"),
1158 __fs.count(),
1159 3 + __hms.fractional_width,
1160 __hms.fractional_width);
1161 }
1162 else
1163 {
1164 const auto& __np
1165 = use_facet<numpunct<_CharT>>(__loc);
1166 __out = __format::__write(std::move(__out),
1167 _S_two_digits(__s.count()));
1168 *__out++ = __np.decimal_point();
1169 if constexpr (is_integral_v<rep>)
1170 __out = std::format_to(std::move(__out),
1171 _GLIBCXX_WIDEN("{:0{}}"),
1172 __ss.count(),
1173 __hms.fractional_width);
1174 else
1175 {
1176 auto __str = std::format(_S_empty_spec, __ss.count());
1177 __out = std::format_to(_GLIBCXX_WIDEN("{:0>{}s}"),
1178 __str,
1179 __hms.fractional_width);
1180 }
1181 }
1182 }
1183 return __out;
1184 }
1185
1186 // %t handled in _M_format
1187
1188 template<typename _Tp, typename _FormatContext>
1189 typename _FormatContext::iterator
1190 _M_u_w(const _Tp& __t, typename _FormatContext::iterator __out,
1191 _FormatContext& __ctx, _CharT __conv, bool __mod = false) const
1192 {
1193 // %u ISO weekday as a decimal number (1-7), where Monday is 1.
1194 // %Ou Locale's alternative numeric rep.
1195 // %w Weekday as a decimal number (0-6), where Sunday is 0.
1196 // %Ow Locale's alternative numeric rep.
1197
1198 chrono::weekday __wd = _S_weekday(__t);
1199
1200 if (__mod) [[unlikely]]
1201 {
1202 struct tm __tm{};
1203 __tm.tm_wday = __wd.c_encoding();
1204 return _M_locale_fmt(std::move(__out), _M_locale(__ctx), __tm,
1205 (char)__conv, 'O');
1206 }
1207
1208 unsigned __wdi = __conv == 'u' ? __wd.iso_encoding()
1209 : __wd.c_encoding();
1210 const _CharT __d = _S_digit(__wdi);
1211 return __format::__write(std::move(__out), __string_view(&__d, 1));
1212 }
1213
1214 template<typename _Tp, typename _FormatContext>
1215 typename _FormatContext::iterator
1216 _M_U_V_W(const _Tp& __t, typename _FormatContext::iterator __out,
1217 _FormatContext& __ctx, _CharT __conv, bool __mod = false) const
1218 {
1219 // %U Week number of the year as a decimal number, from first Sunday.
1220 // %OU Locale's alternative numeric rep.
1221 // %V ISO week-based week number as a decimal number.
1222 // %OV Locale's alternative numeric rep.
1223 // %W Week number of the year as a decimal number, from first Monday.
1224 // %OW Locale's alternative numeric rep.
1225 using namespace chrono;
1226 auto __d = _S_days(__t);
1227 using _TDays = decltype(__d); // Either sys_days or local_days.
1228
1229 if (__mod) [[unlikely]]
1230 {
1231 const year_month_day __ymd(__d);
1232 const year __y = __ymd.year();
1233 struct tm __tm{};
1234 __tm.tm_year = (int)__y - 1900;
1235 __tm.tm_yday = (__d - _TDays(__y/January/1)).count();
1236 __tm.tm_wday = weekday(__d).c_encoding();
1237 return _M_locale_fmt(std::move(__out), _M_locale(__ctx), __tm,
1238 (char)__conv, 'O');
1239 }
1240
1241 _TDays __first; // First day of week 1.
1242 if (__conv == 'V') // W01 begins on Monday before first Thursday.
1243 {
1244 // Move to nearest Thursday:
1245 __d -= (weekday(__d) - Monday) - days(3);
1246 // ISO week of __t is number of weeks since January 1 of the
1247 // same year as that nearest Thursday.
1248 __first = _TDays(year_month_day(__d).year()/January/1);
1249 }
1250 else
1251 {
1252 year __y;
1253 if constexpr (requires { __t.year(); })
1254 __y = __t.year();
1255 else
1256 __y = year_month_day(__d).year();
1257 const weekday __weekstart = __conv == 'U' ? Sunday : Monday;
1258 __first = _TDays(__y/January/__weekstart[1]);
1259 }
1260 auto __weeks = chrono::floor<weeks>(__d - __first);
1261 __string_view __sv = _S_two_digits(__weeks.count() + 1);
1262 return __format::__write(std::move(__out), __sv);
1263 }
1264
1265 template<typename _Tp, typename _FormatContext>
1266 typename _FormatContext::iterator
1267 _M_x(const _Tp& __t, typename _FormatContext::iterator __out,
1268 _FormatContext& __ctx, bool __mod = false) const
1269 {
1270 // %x Locale's date rep
1271 // %Ex Locale's alternative date representation.
1272 locale __loc = _M_locale(__ctx);
1273 const auto& __tp = use_facet<__timepunct<_CharT>>(__loc);
1274 const _CharT* __date_reps[2];
1275 __tp._M_date_formats(__date_reps);
1276 const _CharT* __rep = __date_reps[__mod];
1277 if (!*__rep)
1278 return _M_D(__t, std::move(__out), __ctx);
1279
1280 basic_string<_CharT> __fmt(_S_empty_spec);
1281 __fmt.insert(1u, 1u, _S_colon);
1282 __fmt.insert(2u, __rep);
1283 return std::vformat_to(std::move(__out), __fmt,
1285 }
1286
1287 template<typename _Tp, typename _FormatContext>
1288 typename _FormatContext::iterator
1289 _M_X(const _Tp& __tt, typename _FormatContext::iterator __out,
1290 _FormatContext& __ctx, bool __mod = false) const
1291 {
1292 // %X Locale's time rep
1293 // %EX Locale's alternative time representation.
1294 auto __t = _S_floor_seconds(__tt);
1295 locale __loc = _M_locale(__ctx);
1296 const auto& __tp = use_facet<__timepunct<_CharT>>(__loc);
1297 const _CharT* __time_reps[2];
1298 __tp._M_time_formats(__time_reps);
1299 const _CharT* __rep = __time_reps[__mod];
1300 if (!*__rep)
1301 return _M_R_T(__t, std::move(__out), __ctx, true);
1302
1303 basic_string<_CharT> __fmt(_S_empty_spec);
1304 __fmt.insert(1u, 1u, _S_colon);
1305 __fmt.insert(2u, __rep);
1306 return std::vformat_to(std::move(__out), __fmt,
1308 }
1309
1310 template<typename _Tp, typename _FormatContext>
1311 typename _FormatContext::iterator
1312 _M_z(const _Tp& __t, typename _FormatContext::iterator __out,
1313 _FormatContext&, bool __mod = false) const
1314 {
1315 using ::std::chrono::__detail::__utc_leap_second;
1316 using ::std::chrono::__detail::__local_time_fmt;
1317
1318 auto __utc = __mod ? __string_view(_GLIBCXX_WIDEN("+00:00"), 6)
1319 : __string_view(_GLIBCXX_WIDEN("+0000"), 5);
1320
1321 if constexpr (chrono::__is_time_point_v<_Tp>)
1322 {
1323 if constexpr (is_same_v<typename _Tp::clock,
1324 chrono::system_clock>)
1325 return __format::__write(std::move(__out), __utc);
1326 }
1327 else if constexpr (__is_specialization_of<_Tp, __local_time_fmt>)
1328 {
1329 if (__t._M_offset_sec)
1330 {
1331 auto __sv = __utc;
1332 basic_string<_CharT> __s;
1333 if (*__t._M_offset_sec != 0s)
1334 {
1335 chrono:: hh_mm_ss __hms(*__t._M_offset_sec);
1336 __s = _S_plus_minus[__hms.is_negative()];
1337 __s += _S_two_digits(__hms.hours().count());
1338 if (__mod)
1339 __s += _S_colon;
1340 __s += _S_two_digits(__hms.minutes().count());
1341 __sv = __s;
1342 }
1343 return __format::__write(std::move(__out), __sv);
1344 }
1345 }
1346 else if constexpr (__is_specialization_of<_Tp, __utc_leap_second>)
1347 return __format::__write(std::move(__out), __utc);
1348
1349 __no_timezone_available();
1350 }
1351
1352 template<typename _Tp, typename _FormatContext>
1353 typename _FormatContext::iterator
1354 _M_Z(const _Tp& __t, typename _FormatContext::iterator __out,
1355 _FormatContext& __ctx) const
1356 {
1357 using ::std::chrono::__detail::__utc_leap_second;
1358 using ::std::chrono::__detail::__local_time_fmt;
1359
1360 __string_view __utc(_GLIBCXX_WIDEN("UTC"), 3);
1361 if constexpr (chrono::__is_time_point_v<_Tp>)
1362 {
1363 if constexpr (is_same_v<typename _Tp::clock,
1364 chrono::system_clock>)
1365 return __format::__write(std::move(__out), __utc);
1366 }
1367 else if constexpr (__is_specialization_of<_Tp, __local_time_fmt>)
1368 {
1369 if (__t._M_abbrev)
1370 {
1371 string_view __sv = *__t._M_abbrev;
1372 if constexpr (is_same_v<_CharT, char>)
1373 return __format::__write(std::move(__out), __sv);
1374 else
1375 {
1376 // TODO use resize_and_overwrite
1377 basic_string<_CharT> __ws(__sv.size(), _CharT());
1378 auto& __ct = use_facet<ctype<_CharT>>(_M_locale(__ctx));
1379 __ct.widen(__sv.begin(), __sv.end(), __ws.data());
1380 __string_view __wsv = __ws;
1381 return __format::__write(std::move(__out), __wsv);
1382 }
1383 }
1384 }
1385 else if constexpr (__is_specialization_of<_Tp, __utc_leap_second>)
1386 return __format::__write(std::move(__out), __utc);
1387
1388 __no_timezone_available();
1389 }
1390
1391 // %% handled in _M_format
1392
1393 // A single digit character in the range '0'..'9'.
1394 static _CharT
1395 _S_digit(int __n) noexcept
1396 {
1397 // Extra 9s avoid past-the-end read on bad input.
1398 return _GLIBCXX_WIDEN("0123456789999999")[__n & 0xf];
1399 }
1400
1401 // A string view of two digit characters, "00".."99".
1402 static basic_string_view<_CharT>
1403 _S_two_digits(int __n) noexcept
1404 {
1405 return {
1406 _GLIBCXX_WIDEN("0001020304050607080910111213141516171819"
1407 "2021222324252627282930313233343536373839"
1408 "4041424344454647484950515253545556575859"
1409 "6061626364656667686970717273747576777879"
1410 "8081828384858687888990919293949596979899"
1411 "9999999999999999999999999999999999999999"
1412 "9999999999999999") + 2 * (__n & 0x7f),
1413 2
1414 };
1415 }
1416
1417 // Accessors for the components of chrono types:
1418
1419 // Returns a hh_mm_ss.
1420 template<typename _Tp>
1421 static decltype(auto)
1422 _S_hms(const _Tp& __t)
1423 {
1424 using ::std::chrono::__detail::__utc_leap_second;
1425 using ::std::chrono::__detail::__local_time_fmt;
1426
1427 if constexpr (__is_specialization_of<_Tp, chrono::hh_mm_ss>)
1428 return __t;
1429 else if constexpr (__is_specialization_of<_Tp, __utc_leap_second>)
1430 return __t._M_time;
1431 else if constexpr (chrono::__is_duration_v<_Tp>)
1432 return chrono::hh_mm_ss<_Tp>(__t);
1433 else if constexpr (chrono::__is_time_point_v<_Tp>)
1434 return chrono::hh_mm_ss(__t - chrono::floor<chrono::days>(__t));
1435 else if constexpr (__is_specialization_of<_Tp, __local_time_fmt>)
1436 return _S_hms(__t._M_time);
1437 else
1438 {
1439 __invalid_chrono_spec();
1440 return chrono::hh_mm_ss<chrono::seconds>();
1441 }
1442 }
1443
1444 // Returns a sys_days or local_days.
1445 template<typename _Tp>
1446 static auto
1447 _S_days(const _Tp& __t)
1448 {
1449 using namespace chrono;
1450 using ::std::chrono::__detail::__utc_leap_second;
1451 using ::std::chrono::__detail::__local_time_fmt;
1452
1453 if constexpr (__is_time_point_v<_Tp>)
1454 return chrono::floor<days>(__t);
1455 else if constexpr (__is_specialization_of<_Tp, __utc_leap_second>)
1456 return __t._M_date;
1457 else if constexpr (__is_specialization_of<_Tp, __local_time_fmt>)
1458 return chrono::floor<days>(__t._M_time);
1459 else if constexpr (is_same_v<_Tp, year_month_day>
1460 || is_same_v<_Tp, year_month_day_last>
1461 || is_same_v<_Tp, year_month_weekday>
1462 || is_same_v<_Tp, year_month_weekday_last>)
1463 return sys_days(__t);
1464 else
1465 {
1466 if constexpr (__is_duration_v<_Tp>)
1467 __not_valid_for_duration();
1468 else
1469 __invalid_chrono_spec();
1470 return chrono::sys_days();
1471 }
1472 }
1473
1474 // Returns a year_month_day.
1475 template<typename _Tp>
1476 static chrono::year_month_day
1477 _S_date(const _Tp& __t)
1478 {
1479 if constexpr (is_same_v<_Tp, chrono::year_month_day>)
1480 return __t;
1481 else
1482 return chrono::year_month_day(_S_days(__t));
1483 }
1484
1485 template<typename _Tp>
1486 static chrono::day
1487 _S_day(const _Tp& __t)
1488 {
1489 using namespace chrono;
1490
1491 if constexpr (is_same_v<_Tp, day>)
1492 return __t;
1493 else if constexpr (requires { __t.day(); })
1494 return __t.day();
1495 else
1496 return _S_date(__t).day();
1497 }
1498
1499 template<typename _Tp>
1500 static chrono::month
1501 _S_month(const _Tp& __t)
1502 {
1503 using namespace chrono;
1504
1505 if constexpr (is_same_v<_Tp, month>)
1506 return __t;
1507 else if constexpr (requires { __t.month(); })
1508 return __t.month();
1509 else
1510 return _S_date(__t).month();
1511 }
1512
1513 template<typename _Tp>
1514 static chrono::year
1515 _S_year(const _Tp& __t)
1516 {
1517 using namespace chrono;
1518
1519 if constexpr (is_same_v<_Tp, year>)
1520 return __t;
1521 else if constexpr (requires { __t.year(); })
1522 return __t.year();
1523 else
1524 return _S_date(__t).year();
1525 }
1526
1527 template<typename _Tp>
1528 static chrono::weekday
1529 _S_weekday(const _Tp& __t)
1530 {
1531 using namespace ::std::chrono;
1532 using ::std::chrono::__detail::__local_time_fmt;
1533
1534 if constexpr (is_same_v<_Tp, weekday>)
1535 return __t;
1536 else if constexpr (requires { __t.weekday(); })
1537 return __t.weekday();
1538 else if constexpr (is_same_v<_Tp, month_weekday>)
1539 return __t.weekday_indexed().weekday();
1540 else if constexpr (is_same_v<_Tp, month_weekday_last>)
1541 return __t.weekday_last().weekday();
1542 else
1543 return weekday(_S_days(__t));
1544 }
1545
1546 // Remove subsecond precision from a time_point.
1547 template<typename _Tp>
1548 static auto
1549 _S_floor_seconds(const _Tp& __t)
1550 {
1551 using chrono::__detail::__local_time_fmt;
1552 if constexpr (chrono::__is_time_point_v<_Tp>
1553 || chrono::__is_duration_v<_Tp>)
1554 {
1555 if constexpr (_Tp::period::den != 1)
1556 return chrono::floor<chrono::seconds>(__t);
1557 else
1558 return __t;
1559 }
1560 else if constexpr (__is_specialization_of<_Tp, chrono::hh_mm_ss>)
1561 {
1562 if constexpr (_Tp::fractional_width != 0)
1563 return chrono::floor<chrono::seconds>(__t.to_duration());
1564 else
1565 return __t;
1566 }
1567 else if constexpr (__is_specialization_of<_Tp, __local_time_fmt>)
1568 return _S_floor_seconds(__t._M_time);
1569 else
1570 return __t;
1571 }
1572
1573 // Use the formatting locale's std::time_put facet to produce
1574 // a locale-specific representation.
1575 template<typename _Iter>
1576 _Iter
1577 _M_locale_fmt(_Iter __out, const locale& __loc, const struct tm& __tm,
1578 char __fmt, char __mod) const
1579 {
1580 basic_ostringstream<_CharT> __os;
1581 const auto& __tp = use_facet<time_put<_CharT>>(__loc);
1582 __tp.put(__os, __os, _S_space, &__tm, __fmt, __mod);
1583 if (__os)
1584 __out = __format::__write(std::move(__out), __os.view());
1585 return __out;
1586 }
1587 };
1588
1589} // namespace __format
1590/// @endcond
1591
1592 template<typename _Rep, typename _Period, typename _CharT>
1593 struct formatter<chrono::duration<_Rep, _Period>, _CharT>
1594 {
1595 constexpr typename basic_format_parse_context<_CharT>::iterator
1596 parse(basic_format_parse_context<_CharT>& __pc)
1597 {
1598 using namespace __format;
1599 auto __it = _M_f._M_parse(__pc, _Duration|_TimeOfDay);
1600 if constexpr (!is_floating_point_v<_Rep>)
1601 if (_M_f._M_spec._M_prec_kind != __format::_WP_none)
1602 __throw_format_error("format error: invalid precision for duration");
1603 return __it;
1604 }
1605
1606 template<typename _Out>
1607 typename basic_format_context<_Out, _CharT>::iterator
1608 format(const chrono::duration<_Rep, _Period>& __d,
1609 basic_format_context<_Out, _CharT>& __fc) const
1610 {
1611 if constexpr (numeric_limits<_Rep>::is_signed)
1612 if (__d < __d.zero()) [[unlikely]]
1613 {
1614 if constexpr (is_integral_v<_Rep>)
1615 {
1616 // -d is undefined for the most negative integer.
1617 // Convert duration to corresponding unsigned rep.
1618 using _URep = make_unsigned_t<_Rep>;
1619 auto __ucnt = -static_cast<_URep>(__d.count());
1620 auto __ud = chrono::duration<_URep, _Period>(__ucnt);
1621 return _M_f._M_format(__ud, __fc, true);
1622 }
1623 else
1624 return _M_f._M_format(-__d, __fc, true);
1625 }
1626 return _M_f._M_format(__d, __fc, false);
1627 }
1628
1629 private:
1630 __format::__formatter_chrono<_CharT> _M_f;
1631 };
1632
1633 template<typename _CharT>
1634 struct formatter<chrono::day, _CharT>
1635 {
1636 template<typename _ParseContext>
1637 constexpr typename _ParseContext::iterator
1638 parse(_ParseContext& __pc)
1639 { return _M_f._M_parse(__pc, __format::_Day); }
1640
1641 template<typename _FormatContext>
1642 typename _FormatContext::iterator
1643 format(const chrono::day& __t, _FormatContext& __fc) const
1644 { return _M_f._M_format(__t, __fc); }
1645
1646 private:
1647 __format::__formatter_chrono<_CharT> _M_f;
1648 };
1649
1650 template<typename _CharT>
1651 struct formatter<chrono::month, _CharT>
1652 {
1653 template<typename _ParseContext>
1654 constexpr typename _ParseContext::iterator
1655 parse(_ParseContext& __pc)
1656 { return _M_f._M_parse(__pc, __format::_Month); }
1657
1658 template<typename _FormatContext>
1659 typename _FormatContext::iterator
1660 format(const chrono::month& __t, _FormatContext& __fc) const
1661 { return _M_f._M_format(__t, __fc); }
1662
1663 private:
1664 __format::__formatter_chrono<_CharT> _M_f;
1665 };
1666
1667 template<typename _CharT>
1668 struct formatter<chrono::year, _CharT>
1669 {
1670 template<typename _ParseContext>
1671 constexpr typename _ParseContext::iterator
1672 parse(_ParseContext& __pc)
1673 { return _M_f._M_parse(__pc, __format::_Year); }
1674
1675 template<typename _FormatContext>
1676 typename _FormatContext::iterator
1677 format(const chrono::year& __t, _FormatContext& __fc) const
1678 { return _M_f._M_format(__t, __fc); }
1679
1680 private:
1681 __format::__formatter_chrono<_CharT> _M_f;
1682 };
1683
1684 template<typename _CharT>
1685 struct formatter<chrono::weekday, _CharT>
1686 {
1687 template<typename _ParseContext>
1688 constexpr typename _ParseContext::iterator
1689 parse(_ParseContext& __pc)
1690 { return _M_f._M_parse(__pc, __format::_Weekday); }
1691
1692 template<typename _FormatContext>
1693 typename _FormatContext::iterator
1694 format(const chrono::weekday& __t, _FormatContext& __fc) const
1695 { return _M_f._M_format(__t, __fc); }
1696
1697 private:
1698 __format::__formatter_chrono<_CharT> _M_f;
1699 };
1700
1701 template<typename _CharT>
1702 struct formatter<chrono::weekday_indexed, _CharT>
1703 {
1704 template<typename _ParseContext>
1705 constexpr typename _ParseContext::iterator
1706 parse(_ParseContext& __pc)
1707 { return _M_f._M_parse(__pc, __format::_Weekday); }
1708
1709 template<typename _FormatContext>
1710 typename _FormatContext::iterator
1711 format(const chrono::weekday_indexed& __t, _FormatContext& __fc) const
1712 { return _M_f._M_format(__t, __fc); }
1713
1714 private:
1715 __format::__formatter_chrono<_CharT> _M_f;
1716 };
1717
1718 template<typename _CharT>
1719 struct formatter<chrono::weekday_last, _CharT>
1720 {
1721 template<typename _ParseContext>
1722 constexpr typename _ParseContext::iterator
1723 parse(_ParseContext& __pc)
1724 { return _M_f._M_parse(__pc, __format::_Weekday); }
1725
1726 template<typename _FormatContext>
1727 typename _FormatContext::iterator
1728 format(const chrono::weekday_last& __t, _FormatContext& __fc) const
1729 { return _M_f._M_format(__t, __fc); }
1730
1731 private:
1732 __format::__formatter_chrono<_CharT> _M_f;
1733 };
1734
1735 template<typename _CharT>
1736 struct formatter<chrono::month_day, _CharT>
1737 {
1738 template<typename _ParseContext>
1739 constexpr typename _ParseContext::iterator
1740 parse(_ParseContext& __pc)
1741 { return _M_f._M_parse(__pc, __format::_Month|__format::_Day); }
1742
1743 template<typename _FormatContext>
1744 typename _FormatContext::iterator
1745 format(const chrono::month_day& __t, _FormatContext& __fc) const
1746 { return _M_f._M_format(__t, __fc); }
1747
1748 private:
1749 __format::__formatter_chrono<_CharT> _M_f;
1750 };
1751
1752 template<typename _CharT>
1753 struct formatter<chrono::month_day_last, _CharT>
1754 {
1755 template<typename _ParseContext>
1756 constexpr typename _ParseContext::iterator
1757 parse(_ParseContext& __pc)
1758 { return _M_f._M_parse(__pc, __format::_Month|__format::_Day); }
1759
1760 template<typename _FormatContext>
1761 typename _FormatContext::iterator
1762 format(const chrono::month_day_last& __t, _FormatContext& __fc) const
1763 { return _M_f._M_format(__t, __fc); }
1764
1765 private:
1766 __format::__formatter_chrono<_CharT> _M_f;
1767 };
1768
1769 template<typename _CharT>
1770 struct formatter<chrono::month_weekday, _CharT>
1771 {
1772 template<typename _ParseContext>
1773 constexpr typename _ParseContext::iterator
1774 parse(_ParseContext& __pc)
1775 { return _M_f._M_parse(__pc, __format::_Month|__format::_Weekday); }
1776
1777 template<typename _FormatContext>
1778 typename _FormatContext::iterator
1779 format(const chrono::month_weekday& __t, _FormatContext& __fc) const
1780 { return _M_f._M_format(__t, __fc); }
1781
1782 private:
1783 __format::__formatter_chrono<_CharT> _M_f;
1784 };
1785
1786 template<typename _CharT>
1787 struct formatter<chrono::month_weekday_last, _CharT>
1788 {
1789 template<typename _ParseContext>
1790 constexpr typename _ParseContext::iterator
1791 parse(_ParseContext& __pc)
1792 { return _M_f._M_parse(__pc, __format::_Month|__format::_Weekday); }
1793
1794 template<typename _FormatContext>
1795 typename _FormatContext::iterator
1796 format(const chrono::month_weekday_last& __t,
1797 _FormatContext& __fc) const
1798 { return _M_f._M_format(__t, __fc); }
1799
1800 private:
1801 __format::__formatter_chrono<_CharT> _M_f;
1802 };
1803
1804 template<typename _CharT>
1805 struct formatter<chrono::year_month, _CharT>
1806 {
1807 template<typename _ParseContext>
1808 constexpr typename _ParseContext::iterator
1809 parse(_ParseContext& __pc)
1810 { return _M_f._M_parse(__pc, __format::_Year|__format::_Month); }
1811
1812 template<typename _FormatContext>
1813 typename _FormatContext::iterator
1814 format(const chrono::year_month& __t, _FormatContext& __fc) const
1815 { return _M_f._M_format(__t, __fc); }
1816
1817 private:
1818 __format::__formatter_chrono<_CharT> _M_f;
1819 };
1820
1821 template<typename _CharT>
1822 struct formatter<chrono::year_month_day, _CharT>
1823 {
1824 template<typename _ParseContext>
1825 constexpr typename _ParseContext::iterator
1826 parse(_ParseContext& __pc)
1827 { return _M_f._M_parse(__pc, __format::_Date); }
1828
1829 template<typename _FormatContext>
1830 typename _FormatContext::iterator
1831 format(const chrono::year_month_day& __t, _FormatContext& __fc) const
1832 { return _M_f._M_format(__t, __fc); }
1833
1834 private:
1835 __format::__formatter_chrono<_CharT> _M_f;
1836 };
1837
1838 template<typename _CharT>
1839 struct formatter<chrono::year_month_day_last, _CharT>
1840 {
1841 template<typename _ParseContext>
1842 constexpr typename _ParseContext::iterator
1843 parse(_ParseContext& __pc)
1844 { return _M_f._M_parse(__pc, __format::_Date); }
1845
1846 template<typename _FormatContext>
1847 typename _FormatContext::iterator
1848 format(const chrono::year_month_day_last& __t,
1849 _FormatContext& __fc) const
1850 { return _M_f._M_format(__t, __fc); }
1851
1852 private:
1853 __format::__formatter_chrono<_CharT> _M_f;
1854 };
1855
1856 template<typename _CharT>
1857 struct formatter<chrono::year_month_weekday, _CharT>
1858 {
1859 template<typename _ParseContext>
1860 constexpr typename _ParseContext::iterator
1861 parse(_ParseContext& __pc)
1862 { return _M_f._M_parse(__pc, __format::_Date); }
1863
1864 template<typename _FormatContext>
1865 typename _FormatContext::iterator
1866 format(const chrono::year_month_weekday& __t,
1867 _FormatContext& __fc) const
1868 { return _M_f._M_format(__t, __fc); }
1869
1870 private:
1871 __format::__formatter_chrono<_CharT> _M_f;
1872 };
1873
1874 template<typename _CharT>
1875 struct formatter<chrono::year_month_weekday_last, _CharT>
1876 {
1877 template<typename _ParseContext>
1878 constexpr typename _ParseContext::iterator
1879 parse(_ParseContext& __pc)
1880 { return _M_f._M_parse(__pc, __format::_Date); }
1881
1882 template<typename _FormatContext>
1883 typename _FormatContext::iterator
1884 format(const chrono::year_month_weekday_last& __t,
1885 _FormatContext& __fc) const
1886 { return _M_f._M_format(__t, __fc); }
1887
1888 private:
1889 __format::__formatter_chrono<_CharT> _M_f;
1890 };
1891
1892 template<typename _Rep, typename _Period, typename _CharT>
1893 struct formatter<chrono::hh_mm_ss<chrono::duration<_Rep, _Period>>, _CharT>
1894 {
1895 template<typename _ParseContext>
1896 constexpr typename _ParseContext::iterator
1897 parse(_ParseContext& __pc)
1898 { return _M_f._M_parse(__pc, __format::_TimeOfDay); }
1899
1900 template<typename _FormatContext>
1901 typename _FormatContext::iterator
1902 format(const chrono::hh_mm_ss<chrono::duration<_Rep, _Period>>& __t,
1903 _FormatContext& __fc) const
1904 { return _M_f._M_format(__t, __fc); }
1905
1906 private:
1907 __format::__formatter_chrono<_CharT> _M_f;
1908 };
1909
1910#if _GLIBCXX_USE_CXX11_ABI || ! _GLIBCXX_USE_DUAL_ABI
1911 template<typename _CharT>
1912 struct formatter<chrono::sys_info, _CharT>
1913 {
1914 template<typename _ParseContext>
1915 constexpr typename _ParseContext::iterator
1916 parse(_ParseContext& __pc)
1917 { return _M_f._M_parse(__pc, __format::_ChronoParts{}); }
1918
1919 template<typename _FormatContext>
1920 typename _FormatContext::iterator
1921 format(const chrono::sys_info& __i, _FormatContext& __fc) const
1922 { return _M_f._M_format(__i, __fc); }
1923
1924 private:
1925 __format::__formatter_chrono<_CharT> _M_f;
1926 };
1927
1928 template<typename _CharT>
1929 struct formatter<chrono::local_info, _CharT>
1930 {
1931 template<typename _ParseContext>
1932 constexpr typename _ParseContext::iterator
1933 parse(_ParseContext& __pc)
1934 { return _M_f._M_parse(__pc, __format::_ChronoParts{}); }
1935
1936 template<typename _FormatContext>
1937 typename _FormatContext::iterator
1938 format(const chrono::local_info& __i, _FormatContext& __fc) const
1939 { return _M_f._M_format(__i, __fc); }
1940
1941 private:
1942 __format::__formatter_chrono<_CharT> _M_f;
1943 };
1944#endif
1945
1946 template<typename _Duration, typename _CharT>
1947 struct formatter<chrono::sys_time<_Duration>, _CharT>
1948 {
1949 template<typename _ParseContext>
1950 constexpr typename _ParseContext::iterator
1951 parse(_ParseContext& __pc)
1952 {
1953 auto __next = _M_f._M_parse(__pc, __format::_ZonedDateTime);
1954 if constexpr (!__stream_insertable)
1955 if (_M_f._M_spec._M_chrono_specs.empty())
1956 __format::__invalid_chrono_spec(); // chrono-specs can't be empty
1957 return __next;
1958 }
1959
1960 template<typename _FormatContext>
1961 typename _FormatContext::iterator
1962 format(const chrono::sys_time<_Duration>& __t,
1963 _FormatContext& __fc) const
1964 { return _M_f._M_format(__t, __fc); }
1965
1966 private:
1967 static constexpr bool __stream_insertable
1968 = requires (basic_ostream<_CharT>& __os,
1969 chrono::sys_time<_Duration> __t) { __os << __t; };
1970
1971 __format::__formatter_chrono<_CharT> _M_f;
1972 };
1973
1974 template<typename _Duration, typename _CharT>
1975 struct formatter<chrono::utc_time<_Duration>, _CharT>
1976 : __format::__formatter_chrono<_CharT>
1977 {
1978 template<typename _ParseContext>
1979 constexpr typename _ParseContext::iterator
1980 parse(_ParseContext& __pc)
1981 { return _M_f._M_parse(__pc, __format::_ZonedDateTime); }
1982
1983 template<typename _FormatContext>
1984 typename _FormatContext::iterator
1985 format(const chrono::utc_time<_Duration>& __t,
1986 _FormatContext& __fc) const
1987 {
1988 // Adjust by removing leap seconds to get equivalent sys_time.
1989 // We can't just use clock_cast because we want to know if the time
1990 // falls within a leap second insertion, and format seconds as "60".
1991 using chrono::__detail::__utc_leap_second;
1992 using chrono::seconds;
1993 using chrono::sys_time;
1994 using _CDur = common_type_t<_Duration, seconds>;
1995 const auto __li = chrono::get_leap_second_info(__t);
1996 sys_time<_CDur> __s{__t.time_since_epoch() - __li.elapsed};
1997 if (!__li.is_leap_second) [[likely]]
1998 return _M_f._M_format(__s, __fc);
1999 else
2000 return _M_f._M_format(__utc_leap_second(__s), __fc);
2001 }
2002
2003 private:
2004 friend formatter<chrono::__detail::__utc_leap_second<_Duration>, _CharT>;
2005
2006 __format::__formatter_chrono<_CharT> _M_f;
2007 };
2008
2009 template<typename _Duration, typename _CharT>
2010 struct formatter<chrono::tai_time<_Duration>, _CharT>
2011 : __format::__formatter_chrono<_CharT>
2012 {
2013 template<typename _ParseContext>
2014 constexpr typename _ParseContext::iterator
2015 parse(_ParseContext& __pc)
2016 { return _M_f._M_parse(__pc, __format::_ZonedDateTime); }
2017
2018 template<typename _FormatContext>
2019 typename _FormatContext::iterator
2020 format(const chrono::tai_time<_Duration>& __t,
2021 _FormatContext& __fc) const
2022 {
2023 // Convert to __local_time_fmt with abbrev "TAI" and offset 0s.
2024
2025 // Offset is 1970y/January/1 - 1958y/January/1
2026 constexpr chrono::days __tai_offset = chrono::days(4383);
2027 using _CDur = common_type_t<_Duration, chrono::days>;
2028 chrono::local_time<_CDur> __lt(__t.time_since_epoch() - __tai_offset);
2029 const string __abbrev("TAI", 3);
2030 const chrono::seconds __off = 0s;
2031 const auto __lf = chrono::local_time_format(__lt, &__abbrev, &__off);
2032 return _M_f._M_format(__lf, __fc);
2033 }
2034
2035 private:
2036 __format::__formatter_chrono<_CharT> _M_f;
2037 };
2038
2039 template<typename _Duration, typename _CharT>
2040 struct formatter<chrono::gps_time<_Duration>, _CharT>
2041 : __format::__formatter_chrono<_CharT>
2042 {
2043 template<typename _ParseContext>
2044 constexpr typename _ParseContext::iterator
2045 parse(_ParseContext& __pc)
2046 { return _M_f._M_parse(__pc, __format::_ZonedDateTime); }
2047
2048 template<typename _FormatContext>
2049 typename _FormatContext::iterator
2050 format(const chrono::gps_time<_Duration>& __t,
2051 _FormatContext& __fc) const
2052 {
2053 // Convert to __local_time_fmt with abbrev "GPS" and offset 0s.
2054
2055 // Offset is 1980y/January/Sunday[1] - 1970y/January/1
2056 constexpr chrono::days __gps_offset = chrono::days(3657);
2057 using _CDur = common_type_t<_Duration, chrono::days>;
2058 chrono::local_time<_CDur> __lt(__t.time_since_epoch() + __gps_offset);
2059 const string __abbrev("GPS", 3);
2060 const chrono::seconds __off = 0s;
2061 const auto __lf = chrono::local_time_format(__lt, &__abbrev, &__off);
2062 return _M_f._M_format(__lf, __fc);
2063 }
2064
2065 private:
2066 __format::__formatter_chrono<_CharT> _M_f;
2067 };
2068
2069 template<typename _Duration, typename _CharT>
2070 struct formatter<chrono::file_time<_Duration>, _CharT>
2071 {
2072 template<typename _ParseContext>
2073 constexpr typename _ParseContext::iterator
2074 parse(_ParseContext& __pc)
2075 { return _M_f._M_parse(__pc, __format::_ZonedDateTime); }
2076
2077 template<typename _FormatContext>
2078 typename _FormatContext::iterator
2079 format(const chrono::file_time<_Duration>& __t,
2080 _FormatContext& __ctx) const
2081 {
2082 using namespace chrono;
2083 return _M_f._M_format(chrono::clock_cast<system_clock>(__t), __ctx);
2084 }
2085
2086 private:
2087 __format::__formatter_chrono<_CharT> _M_f;
2088 };
2089
2090 template<typename _Duration, typename _CharT>
2091 struct formatter<chrono::local_time<_Duration>, _CharT>
2092 {
2093 template<typename _ParseContext>
2094 constexpr typename _ParseContext::iterator
2095 parse(_ParseContext& __pc)
2096 { return _M_f._M_parse(__pc, __format::_DateTime); }
2097
2098 template<typename _FormatContext>
2099 typename _FormatContext::iterator
2100 format(const chrono::local_time<_Duration>& __t,
2101 _FormatContext& __ctx) const
2102 { return _M_f._M_format(__t, __ctx); }
2103
2104 private:
2105 __format::__formatter_chrono<_CharT> _M_f;
2106 };
2107
2108 template<typename _Duration, typename _CharT>
2109 struct formatter<chrono::__detail::__local_time_fmt<_Duration>, _CharT>
2110 {
2111 template<typename _ParseContext>
2112 constexpr typename _ParseContext::iterator
2113 parse(_ParseContext& __pc)
2114 { return _M_f._M_parse(__pc, __format::_ZonedDateTime); }
2115
2116 template<typename _FormatContext>
2117 typename _FormatContext::iterator
2118 format(const chrono::__detail::__local_time_fmt<_Duration>& __t,
2119 _FormatContext& __ctx) const
2120 { return _M_f._M_format(__t, __ctx); }
2121
2122 private:
2123 __format::__formatter_chrono<_CharT> _M_f;
2124 };
2125
2126#if _GLIBCXX_USE_CXX11_ABI || ! _GLIBCXX_USE_DUAL_ABI
2127 template<typename _Duration, typename _TimeZonePtr, typename _CharT>
2128 struct formatter<chrono::zoned_time<_Duration, _TimeZonePtr>, _CharT>
2129 : formatter<chrono::__detail::__local_time_fmt<_Duration>, _CharT>
2130 {
2131 template<typename _FormatContext>
2132 typename _FormatContext::iterator
2133 format(const chrono::zoned_time<_Duration, _TimeZonePtr>& __tp,
2134 _FormatContext& __ctx) const
2135 {
2136 using chrono::__detail::__local_time_fmt;
2137 using _Base = formatter<__local_time_fmt<_Duration>, _CharT>;
2138 const chrono::sys_info __info = __tp.get_info();
2139 const auto __lf = chrono::local_time_format(__tp.get_local_time(),
2140 &__info.abbrev,
2141 &__info.offset);
2142 return _Base::format(__lf, __ctx);
2143 }
2144 };
2145#endif
2146
2147 // Partial specialization needed for %c formatting of __utc_leap_second.
2148 template<typename _Duration, typename _CharT>
2149 struct formatter<chrono::__detail::__utc_leap_second<_Duration>, _CharT>
2150 : formatter<chrono::utc_time<_Duration>, _CharT>
2151 {
2152 template<typename _FormatContext>
2153 typename _FormatContext::iterator
2154 format(const chrono::__detail::__utc_leap_second<_Duration>& __t,
2155 _FormatContext& __fc) const
2156 { return this->_M_f._M_format(__t, __fc); }
2157 };
2158
2159namespace chrono
2160{
2161/// @addtogroup chrono
2162/// @{
2163
2164 // TODO: from_stream for duration
2165#if 0
2166 template<typename _CharT, typename _Traits, typename _Rep, typename _Period,
2167 typename _Alloc = allocator<_CharT>>
2168 basic_istream<_CharT, _Traits>&
2169 from_stream(basic_istream<_CharT, _Traits>& __is, const _CharT* __fmt,
2170 duration<_Rep, _Period>& __d,
2171 basic_string<_CharT, _Traits, _Alloc>* __abbrev = nullptr,
2172 minutes* __offset = nullptr)
2173 {
2174 }
2175#endif
2176
2177 template<typename _CharT, typename _Traits>
2178 inline basic_ostream<_CharT, _Traits>&
2179 operator<<(basic_ostream<_CharT, _Traits>& __os, const day& __d)
2180 {
2181 using _Ctx = __conditional_t<is_same_v<_CharT, char>,
2182 format_context, wformat_context>;
2183 using _Str = basic_string_view<_CharT>;
2184 _Str __s = _GLIBCXX_WIDEN("{:02d} is not a valid day");
2185 if (__d.ok())
2186 __s = __s.substr(0, 6);
2187 auto __u = (unsigned)__d;
2188 __os << std::vformat(__s, make_format_args<_Ctx>(__u));
2189 return __os;
2190 }
2191
2192 // TODO from_stream for day
2193
2194 template<typename _CharT, typename _Traits>
2195 inline basic_ostream<_CharT, _Traits>&
2196 operator<<(basic_ostream<_CharT, _Traits>& __os, const month& __m)
2197 {
2198 using _Ctx = __conditional_t<is_same_v<_CharT, char>,
2199 format_context, wformat_context>;
2200 using _Str = basic_string_view<_CharT>;
2201 _Str __s = _GLIBCXX_WIDEN("{:L%b}{} is not a valid month");
2202 if (__m.ok())
2203 __os << std::vformat(__os.getloc(), __s.substr(0, 6),
2204 make_format_args<_Ctx>(__m));
2205 else
2206 {
2207 auto __u = (unsigned)__m;
2208 __os << std::vformat(__s.substr(6), make_format_args<_Ctx>(__u));
2209 }
2210 return __os;
2211 }
2212
2213 // TODO from_stream for month
2214
2215 template<typename _CharT, typename _Traits>
2216 inline basic_ostream<_CharT, _Traits>&
2217 operator<<(basic_ostream<_CharT, _Traits>& __os, const year& __y)
2218 {
2219 using _Ctx = __conditional_t<is_same_v<_CharT, char>,
2220 format_context, wformat_context>;
2221 using _Str = basic_string_view<_CharT>;
2222 _Str __s = _GLIBCXX_WIDEN("-{:04d} is not a valid year");
2223 if (__y.ok())
2224 __s = __s.substr(0, 7);
2225 int __i = (int)__y;
2226 if (__i >= 0) [[likely]]
2227 __s.remove_prefix(1);
2228 else
2229 __i = -__i;
2230 __os << std::vformat(__s, make_format_args<_Ctx>(__i));
2231 return __os;
2232 }
2233
2234 // TODO from_stream for year
2235
2236 template<typename _CharT, typename _Traits>
2237 inline basic_ostream<_CharT, _Traits>&
2238 operator<<(basic_ostream<_CharT, _Traits>& __os, const weekday& __wd)
2239 {
2240 using _Ctx = __conditional_t<is_same_v<_CharT, char>,
2241 format_context, wformat_context>;
2242 using _Str = basic_string_view<_CharT>;
2243 _Str __s = _GLIBCXX_WIDEN("{:L%a}{} is not a valid weekday");
2244 if (__wd.ok())
2245 __os << std::vformat(__os.getloc(), __s.substr(0, 6),
2246 make_format_args<_Ctx>(__wd));
2247 else
2248 {
2249 auto __c = __wd.c_encoding();
2250 __os << std::vformat(__s.substr(6), make_format_args<_Ctx>(__c));
2251 }
2252 return __os;
2253 }
2254
2255 // TODO from_stream for weekday
2256
2257 template<typename _CharT, typename _Traits>
2258 inline basic_ostream<_CharT, _Traits>&
2259 operator<<(basic_ostream<_CharT, _Traits>& __os,
2260 const weekday_indexed& __wdi)
2261 {
2262 // The standard says to format wdi.weekday() and wdi.index() using
2263 // either "{:L}[{}]" or "{:L}[{} is not a valid index]". The {:L} spec
2264 // means to format the weekday using ostringstream, so just do that.
2265 basic_stringstream<_CharT> __os2;
2266 __os2.imbue(__os.getloc());
2267 __os2 << __wdi.weekday();
2268 const auto __i = __wdi.index();
2269 if constexpr (is_same_v<_CharT, char>)
2270 __os2 << std::format("[{}", __i);
2271 else
2272 __os2 << std::format(L"[{}", __i);
2273 basic_string_view<_CharT> __s = _GLIBCXX_WIDEN(" is not a valid index]");
2274 if (__i >= 1 && __i <= 5)
2275 __os2 << __s.back();
2276 else
2277 __os2 << __s;
2278 __os << __os2.view();
2279 return __os;
2280 }
2281
2282 template<typename _CharT, typename _Traits>
2283 inline basic_ostream<_CharT, _Traits>&
2284 operator<<(basic_ostream<_CharT, _Traits>& __os,
2285 const weekday_last& __wdl)
2286 {
2287 // As above, just write straight to a stringstream, as if by "{:L}[last]"
2288 basic_stringstream<_CharT> __os2;
2289 __os2.imbue(__os.getloc());
2290 __os2 << __wdl.weekday() << _GLIBCXX_WIDEN("[last]");
2291 __os << __os2.view();
2292 return __os;
2293 }
2294
2295 template<typename _CharT, typename _Traits>
2296 inline basic_ostream<_CharT, _Traits>&
2297 operator<<(basic_ostream<_CharT, _Traits>& __os, const month_day& __md)
2298 {
2299 // As above, just write straight to a stringstream, as if by "{:L}/{}"
2300 basic_stringstream<_CharT> __os2;
2301 __os2.imbue(__os.getloc());
2302 __os2 << __md.month();
2303 if constexpr (is_same_v<_CharT, char>)
2304 __os2 << '/';
2305 else
2306 __os2 << L'/';
2307 __os2 << __md.day();
2308 __os << __os2.view();
2309 return __os;
2310 }
2311
2312 // TODO from_stream for month_day
2313
2314 template<typename _CharT, typename _Traits>
2315 inline basic_ostream<_CharT, _Traits>&
2316 operator<<(basic_ostream<_CharT, _Traits>& __os,
2317 const month_day_last& __mdl)
2318 {
2319 // As above, just write straight to a stringstream, as if by "{:L}/last"
2320 basic_stringstream<_CharT> __os2;
2321 __os2.imbue(__os.getloc());
2322 __os2 << __mdl.month();
2323 if constexpr (is_same_v<_CharT, char>)
2324 __os2 << "/last";
2325 else
2326 __os2 << L"/last";
2327 __os << __os2.view();
2328 return __os;
2329 }
2330
2331 template<typename _CharT, typename _Traits>
2332 inline basic_ostream<_CharT, _Traits>&
2333 operator<<(basic_ostream<_CharT, _Traits>& __os,
2334 const month_weekday& __mwd)
2335 {
2336 // As above, just write straight to a stringstream, as if by "{:L}/{:L}"
2337 basic_stringstream<_CharT> __os2;
2338 __os2.imbue(__os.getloc());
2339 __os2 << __mwd.month();
2340 if constexpr (is_same_v<_CharT, char>)
2341 __os2 << '/';
2342 else
2343 __os2 << L'/';
2344 __os2 << __mwd.weekday_indexed();
2345 __os << __os2.view();
2346 return __os;
2347 }
2348
2349 template<typename _CharT, typename _Traits>
2350 inline basic_ostream<_CharT, _Traits>&
2351 operator<<(basic_ostream<_CharT, _Traits>& __os,
2352 const month_weekday_last& __mwdl)
2353 {
2354 // As above, just write straight to a stringstream, as if by "{:L}/{:L}"
2355 basic_stringstream<_CharT> __os2;
2356 __os2.imbue(__os.getloc());
2357 __os2 << __mwdl.month();
2358 if constexpr (is_same_v<_CharT, char>)
2359 __os2 << '/';
2360 else
2361 __os2 << L'/';
2362 __os2 << __mwdl.weekday_last();
2363 __os << __os2.view();
2364 return __os;
2365 }
2366
2367 template<typename _CharT, typename _Traits>
2368 inline basic_ostream<_CharT, _Traits>&
2369 operator<<(basic_ostream<_CharT, _Traits>& __os, const year_month& __ym)
2370 {
2371 // As above, just write straight to a stringstream, as if by "{}/{:L}"
2372 basic_stringstream<_CharT> __os2;
2373 __os2.imbue(__os.getloc());
2374 __os2 << __ym.year();
2375 if constexpr (is_same_v<_CharT, char>)
2376 __os2 << '/';
2377 else
2378 __os2 << L'/';
2379 __os2 << __ym.month();
2380 __os << __os2.view();
2381 return __os;
2382 }
2383
2384 // TODO from_stream for year_month
2385
2386 template<typename _CharT, typename _Traits>
2387 inline basic_ostream<_CharT, _Traits>&
2388 operator<<(basic_ostream<_CharT, _Traits>& __os,
2389 const year_month_day& __ymd)
2390 {
2391 using _Ctx = __conditional_t<is_same_v<_CharT, char>,
2392 format_context, wformat_context>;
2393 using _Str = basic_string_view<_CharT>;
2394 _Str __s = _GLIBCXX_WIDEN("{:%F} is not a valid date");
2395 __os << std::vformat(__ymd.ok() ? __s.substr(0, 5) : __s,
2396 make_format_args<_Ctx>(__ymd));
2397 return __os;
2398 }
2399
2400 // TODO from_stream for year_month_day
2401
2402 template<typename _CharT, typename _Traits>
2403 inline basic_ostream<_CharT, _Traits>&
2404 operator<<(basic_ostream<_CharT, _Traits>& __os,
2405 const year_month_day_last& __ymdl)
2406 {
2407 // As above, just write straight to a stringstream, as if by "{}/{:L}"
2408 basic_stringstream<_CharT> __os2;
2409 __os2.imbue(__os.getloc());
2410 __os2 << __ymdl.year();
2411 if constexpr (is_same_v<_CharT, char>)
2412 __os2 << '/';
2413 else
2414 __os2 << L'/';
2415 __os2 << __ymdl.month_day_last();
2416 __os << __os2.view();
2417 return __os;
2418 }
2419
2420 template<typename _CharT, typename _Traits>
2421 inline basic_ostream<_CharT, _Traits>&
2422 operator<<(basic_ostream<_CharT, _Traits>& __os,
2423 const year_month_weekday& __ymwd)
2424 {
2425 // As above, just write straight to a stringstream, as if by
2426 // "{}/{:L}/{:L}"
2427 basic_stringstream<_CharT> __os2;
2428 __os2.imbue(__os.getloc());
2429 _CharT __slash;
2430 if constexpr (is_same_v<_CharT, char>)
2431 __slash = '/';
2432 else
2433 __slash = L'/';
2434 __os2 << __ymwd.year() << __slash << __ymwd.month() << __slash
2435 << __ymwd.weekday_indexed();
2436 __os << __os2.view();
2437 return __os;
2438 }
2439
2440 template<typename _CharT, typename _Traits>
2441 inline basic_ostream<_CharT, _Traits>&
2442 operator<<(basic_ostream<_CharT, _Traits>& __os,
2443 const year_month_weekday_last& __ymwdl)
2444 {
2445 // As above, just write straight to a stringstream, as if by
2446 // "{}/{:L}/{:L}"
2447 basic_stringstream<_CharT> __os2;
2448 __os2.imbue(__os.getloc());
2449 _CharT __slash;
2450 if constexpr (is_same_v<_CharT, char>)
2451 __slash = '/';
2452 else
2453 __slash = L'/';
2454 __os2 << __ymwdl.year() << __slash << __ymwdl.month() << __slash
2455 << __ymwdl.weekday_last();
2456 __os << __os2.view();
2457 return __os;
2458 }
2459
2460 template<typename _CharT, typename _Traits, typename _Duration>
2461 inline basic_ostream<_CharT, _Traits>&
2462 operator<<(basic_ostream<_CharT, _Traits>& __os,
2463 const hh_mm_ss<_Duration>& __hms)
2464 {
2465 return __os << format(__os.getloc(), _GLIBCXX_WIDEN("{:L%T}"), __hms);
2466 }
2467
2468#if _GLIBCXX_USE_CXX11_ABI || ! _GLIBCXX_USE_DUAL_ABI
2469 /// Writes a sys_info object to an ostream in an unspecified format.
2470 template<typename _CharT, typename _Traits>
2471 basic_ostream<_CharT, _Traits>&
2472 operator<<(basic_ostream<_CharT, _Traits>& __os, const sys_info& __i)
2473 {
2474 __os << '[' << __i.begin << ',' << __i.end
2475 << ',' << hh_mm_ss(__i.offset) << ',' << __i.save
2476 << ',' << __i.abbrev << ']';
2477 return __os;
2478 }
2479
2480 /// Writes a local_info object to an ostream in an unspecified format.
2481 template<typename _CharT, typename _Traits>
2482 basic_ostream<_CharT, _Traits>&
2483 operator<<(basic_ostream<_CharT, _Traits>& __os, const local_info& __li)
2484 {
2485 __os << '[';
2486 if (__li.result == local_info::unique)
2487 __os << __li.first;
2488 else
2489 {
2490 if (__li.result == local_info::nonexistent)
2491 __os << "nonexistent";
2492 else
2493 __os << "ambiguous";
2494 __os << " local time between " << __li.first;
2495 __os << " and " << __li.second;
2496 }
2497 __os << ']';
2498 return __os;
2499 }
2500
2501 template<typename _CharT, typename _Traits, typename _Duration,
2502 typename _TimeZonePtr>
2503 inline basic_ostream<_CharT, _Traits>&
2504 operator<<(basic_ostream<_CharT, _Traits>& __os,
2505 const zoned_time<_Duration, _TimeZonePtr>& __t)
2506 {
2507 __os << format(__os.getloc(), _GLIBCXX_WIDEN("{:L%F %T %Z}"), __t);
2508 return __os;
2509 }
2510#endif
2511
2512 template<typename _CharT, typename _Traits, typename _Duration>
2513 requires (!treat_as_floating_point_v<typename _Duration::rep>)
2514 && ratio_less_v<typename _Duration::period, days::period>
2515 inline basic_ostream<_CharT, _Traits>&
2516 operator<<(basic_ostream<_CharT, _Traits>& __os,
2517 const sys_time<_Duration>& __tp)
2518 {
2519 __os << std::format(__os.getloc(), _GLIBCXX_WIDEN("{:L%F %T}"), __tp);
2520 return __os;
2521 }
2522
2523 template<typename _CharT, typename _Traits>
2524 inline basic_ostream<_CharT, _Traits>&
2525 operator<<(basic_ostream<_CharT, _Traits>& __os, const sys_days& __dp)
2526 {
2527 __os << year_month_day{__dp};
2528 return __os;
2529 }
2530
2531 // TODO: from_stream for sys_time
2532
2533 template<typename _CharT, typename _Traits, typename _Duration>
2534 inline basic_ostream<_CharT, _Traits>&
2535 operator<<(basic_ostream<_CharT, _Traits>& __os,
2536 const utc_time<_Duration>& __t)
2537 {
2538 __os << std::format(__os.getloc(), _GLIBCXX_WIDEN("{:L%F %T}"), __t);
2539 return __os;
2540 }
2541
2542 // TODO: from_stream for utc_time
2543
2544 template<typename _CharT, typename _Traits, typename _Duration>
2545 inline basic_ostream<_CharT, _Traits>&
2546 operator<<(basic_ostream<_CharT, _Traits>& __os,
2547 const tai_time<_Duration>& __t)
2548 {
2549 __os << std::format(__os.getloc(), _GLIBCXX_WIDEN("{:L%F %T}"), __t);
2550 return __os;
2551 }
2552
2553 // TODO: from_stream for tai_time
2554
2555 template<typename _CharT, typename _Traits, typename _Duration>
2556 inline basic_ostream<_CharT, _Traits>&
2557 operator<<(basic_ostream<_CharT, _Traits>& __os,
2558 const gps_time<_Duration>& __t)
2559 {
2560 __os << std::format(__os.getloc(), _GLIBCXX_WIDEN("{:L%F %T}"), __t);
2561 return __os;
2562 }
2563
2564 // TODO: from_stream for gps_time
2565
2566
2567 template<typename _CharT, typename _Traits, typename _Duration>
2568 inline basic_ostream<_CharT, _Traits>&
2569 operator<<(basic_ostream<_CharT, _Traits>& __os,
2570 const file_time<_Duration>& __t)
2571 {
2572 __os << std::format(__os.getloc(), _GLIBCXX_WIDEN("{:L%F %T}"), __t);
2573 return __os;
2574 }
2575
2576 // TODO: from_stream for file_time
2577
2578 template<typename _CharT, typename _Traits, typename _Duration>
2579 inline basic_ostream<_CharT, _Traits>&
2580 operator<<(basic_ostream<_CharT, _Traits>& __os,
2581 const local_time<_Duration>& __lt)
2582 {
2583 __os << sys_time<_Duration>{__lt.time_since_epoch()};
2584 return __os;
2585 }
2586
2587 // TODO: from_stream for local_time
2588#undef _GLIBCXX_WIDEN
2589
2590 /// @} group chrono
2591} // namespace chrono
2592
2593_GLIBCXX_END_NAMESPACE_VERSION
2594} // namespace std
2595
2596#endif // C++20
2597
2598#endif //_GLIBCXX_CHRONO_IO_H
__detail::__local_time_fmt< _Duration > local_time_format(local_time< _Duration > __time, const string *__abbrev=nullptr, const seconds *__offset_sec=nullptr)
Definition chrono_io.h:181
duration< int64_t > seconds
seconds
Definition chrono.h:897
duration< int64_t, ratio< 86400 > > days
days
Definition chrono.h:907
basic_ostream< _CharT, _Traits > & operator<<(std::basic_ostream< _CharT, _Traits > &__os, const duration< _Rep, _Period > &__d)
Definition chrono_io.h:139
duration< int64_t, ratio< 60 > > minutes
minutes
Definition chrono.h:900
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
Definition move.h:97
ISO C++ entities toplevel namespace is std.
constexpr bitset< _Nb > operator|(const bitset< _Nb > &__x, const bitset< _Nb > &__y) noexcept
Global bitwise operations on bitsets.
Definition bitset:1563
ISO C++ 2011 namespace for date and time utilities.
static constexpr bool is_signed
Definition limits:223
iterator begin()
Definition cow_string.h:805
Container class for localization functionality.
static const locale & classic()
Return reference to the C locale.