1 // -*- C++ -*-
2 //===----------------------------------------------------------------------===//
3 //
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //
8 //===----------------------------------------------------------------------===//
9
10 #ifndef _LIBCPP___CHRONO_FORMATTER_H
11 #define _LIBCPP___CHRONO_FORMATTER_H
12
13 #include <__chrono/calendar.h>
14 #include <__chrono/concepts.h>
15 #include <__chrono/convert_to_tm.h>
16 #include <__chrono/day.h>
17 #include <__chrono/duration.h>
18 #include <__chrono/hh_mm_ss.h>
19 #include <__chrono/month.h>
20 #include <__chrono/month_weekday.h>
21 #include <__chrono/monthday.h>
22 #include <__chrono/ostream.h>
23 #include <__chrono/parser_std_format_spec.h>
24 #include <__chrono/statically_widen.h>
25 #include <__chrono/system_clock.h>
26 #include <__chrono/time_point.h>
27 #include <__chrono/weekday.h>
28 #include <__chrono/year.h>
29 #include <__chrono/year_month.h>
30 #include <__chrono/year_month_day.h>
31 #include <__chrono/year_month_weekday.h>
32 #include <__concepts/arithmetic.h>
33 #include <__concepts/same_as.h>
34 #include <__config>
35 #include <__format/concepts.h>
36 #include <__format/format_error.h>
37 #include <__format/format_functions.h>
38 #include <__format/format_parse_context.h>
39 #include <__format/formatter.h>
40 #include <__format/formatter_output.h>
41 #include <__format/parser_std_format_spec.h>
42 #include <__memory/addressof.h>
43 #include <cmath>
44 #include <ctime>
45 #include <sstream>
46 #include <string>
47 #include <string_view>
48
49 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
50 # pragma GCC system_header
51 #endif
52
53 _LIBCPP_BEGIN_NAMESPACE_STD
54
55 #if _LIBCPP_STD_VER >= 20 && !defined(_LIBCPP_HAS_NO_INCOMPLETE_FORMAT)
56
57 namespace __formatter {
58
59 /// Formats a time based on a tm struct.
60 ///
61 /// This formatter passes the formatting to time_put which uses strftime. When
62 /// the value is outside the valid range it's unspecified what strftime will
63 /// output. For example weekday 8 can print 1 when the day is processed modulo
64 /// 7 since that handles the Sunday for 0-based weekday. It can also print 8 if
65 /// 7 is handled as a special case.
66 ///
67 /// The Standard doesn't specify what to do in this case so the result depends
68 /// on the result of the underlying code.
69 ///
70 /// \pre When the (abbreviated) weekday or month name are used, the caller
71 /// validates whether the value is valid. So the caller handles that
72 /// requirement of Table 97: Meaning of conversion specifiers
73 /// [tab:time.format.spec].
74 ///
75 /// When no chrono-specs are provided it uses the stream formatter.
76
77 // For tiny ratios it's not possible to convert a duration to a hh_mm_ss. This
78 // fails compile-time due to the limited precision of the ratio (64-bit is too
79 // small). Therefore a duration uses its own conversion.
80 template <class _CharT, class _Rep, class _Period>
81 _LIBCPP_HIDE_FROM_ABI void
__format_sub_seconds(const chrono::duration<_Rep,_Period> & __value,basic_stringstream<_CharT> & __sstr)82 __format_sub_seconds(const chrono::duration<_Rep, _Period>& __value, basic_stringstream<_CharT>& __sstr) {
83 __sstr << std::use_facet<numpunct<_CharT>>(__sstr.getloc()).decimal_point();
84
85 using __duration = chrono::duration<_Rep, _Period>;
86
87 auto __fraction = __value - chrono::duration_cast<chrono::seconds>(__value);
88 if constexpr (chrono::treat_as_floating_point_v<_Rep>)
89 // When the floating-point value has digits itself they are ignored based
90 // on the wording in [tab:time.format.spec]
91 // If the precision of the input cannot be exactly represented with
92 // seconds, then the format is a decimal floating-point number with a
93 // fixed format and a precision matching that of the precision of the
94 // input (or to a microseconds precision if the conversion to
95 // floating-point decimal seconds cannot be made within 18 fractional
96 // digits).
97 //
98 // This matches the behaviour of MSVC STL, fmtlib interprets this
99 // differently and uses 3 decimals.
100 // https://godbolt.org/z/6dsbnW8ba
101 std::format_to(std::ostreambuf_iterator<_CharT>{__sstr},
102 _LIBCPP_STATICALLY_WIDEN(_CharT, "{:0{}.0f}"),
103 __fraction.count(),
104 chrono::hh_mm_ss<__duration>::fractional_width);
105 else
106 std::format_to(std::ostreambuf_iterator<_CharT>{__sstr},
107 _LIBCPP_STATICALLY_WIDEN(_CharT, "{:0{}}"),
108 __fraction.count(),
109 chrono::hh_mm_ss<__duration>::fractional_width);
110 }
111
112 template <class _CharT, __is_time_point _Tp>
__format_sub_seconds(const _Tp & __value,basic_stringstream<_CharT> & __sstr)113 _LIBCPP_HIDE_FROM_ABI void __format_sub_seconds(const _Tp& __value, basic_stringstream<_CharT>& __sstr) {
114 __formatter::__format_sub_seconds(__value.time_since_epoch(), __sstr);
115 }
116
117 template <class _CharT, class _Duration>
118 _LIBCPP_HIDE_FROM_ABI void
__format_sub_seconds(const chrono::hh_mm_ss<_Duration> & __value,basic_stringstream<_CharT> & __sstr)119 __format_sub_seconds(const chrono::hh_mm_ss<_Duration>& __value, basic_stringstream<_CharT>& __sstr) {
120 __sstr << std::use_facet<numpunct<_CharT>>(__sstr.getloc()).decimal_point();
121 if constexpr (chrono::treat_as_floating_point_v<typename _Duration::rep>)
122 std::format_to(std::ostreambuf_iterator<_CharT>{__sstr},
123 _LIBCPP_STATICALLY_WIDEN(_CharT, "{:0{}.0f}"),
124 __value.subseconds().count(),
125 __value.fractional_width);
126 else
127 std::format_to(std::ostreambuf_iterator<_CharT>{__sstr},
128 _LIBCPP_STATICALLY_WIDEN(_CharT, "{:0{}}"),
129 __value.subseconds().count(),
130 __value.fractional_width);
131 }
132
133 template <class _Tp>
__use_fraction()134 consteval bool __use_fraction() {
135 if constexpr (__is_time_point<_Tp>)
136 return chrono::hh_mm_ss<typename _Tp::duration>::fractional_width;
137 else if constexpr (chrono::__is_duration<_Tp>::value)
138 return chrono::hh_mm_ss<_Tp>::fractional_width;
139 else if constexpr (__is_hh_mm_ss<_Tp>)
140 return _Tp::fractional_width;
141 else
142 return false;
143 }
144
145 template <class _CharT>
__format_year(int __year,basic_stringstream<_CharT> & __sstr)146 _LIBCPP_HIDE_FROM_ABI void __format_year(int __year, basic_stringstream<_CharT>& __sstr) {
147 if (__year < 0) {
148 __sstr << _CharT('-');
149 __year = -__year;
150 }
151
152 // TODO FMT Write an issue
153 // If the result has less than four digits it is zero-padded with 0 to two digits.
154 // is less -> has less
155 // left-padded -> zero-padded, otherwise the proper value would be 000-0.
156
157 // Note according to the wording it should be left padded, which is odd.
158 __sstr << std::format(_LIBCPP_STATICALLY_WIDEN(_CharT, "{:04}"), __year);
159 }
160
161 template <class _CharT>
__format_century(int __year,basic_stringstream<_CharT> & __sstr)162 _LIBCPP_HIDE_FROM_ABI void __format_century(int __year, basic_stringstream<_CharT>& __sstr) {
163 // TODO FMT Write an issue
164 // [tab:time.format.spec]
165 // %C The year divided by 100 using floored division. If the result is a
166 // single decimal digit, it is prefixed with 0.
167
168 bool __negative = __year < 0;
169 int __century = (__year - (99 * __negative)) / 100; // floored division
170 __sstr << std::format(_LIBCPP_STATICALLY_WIDEN(_CharT, "{:02}"), __century);
171 }
172
173 template <class _CharT, class _Tp>
__format_chrono_using_chrono_specs(const _Tp & __value,basic_stringstream<_CharT> & __sstr,basic_string_view<_CharT> __chrono_specs)174 _LIBCPP_HIDE_FROM_ABI void __format_chrono_using_chrono_specs(
175 const _Tp& __value, basic_stringstream<_CharT>& __sstr, basic_string_view<_CharT> __chrono_specs) {
176 tm __t = std::__convert_to_tm<tm>(__value);
177 const auto& __facet = std::use_facet<time_put<_CharT>>(__sstr.getloc());
178 for (auto __it = __chrono_specs.begin(); __it != __chrono_specs.end(); ++__it) {
179 if (*__it == _CharT('%')) {
180 auto __s = __it;
181 ++__it;
182 // We only handle the types that can't be directly handled by time_put.
183 // (as an optimization n, t, and % are also handled directly.)
184 switch (*__it) {
185 case _CharT('n'):
186 __sstr << _CharT('\n');
187 break;
188 case _CharT('t'):
189 __sstr << _CharT('\t');
190 break;
191 case _CharT('%'):
192 __sstr << *__it;
193 break;
194
195 case _CharT('C'): {
196 // strftime's output is only defined in the range [00, 99].
197 int __year = __t.tm_year + 1900;
198 if (__year < 1000 || __year > 9999)
199 __formatter::__format_century(__year, __sstr);
200 else
201 __facet.put({__sstr}, __sstr, _CharT(' '), std::addressof(__t), std::to_address(__s), std::to_address(__it + 1));
202 } break;
203
204 case _CharT('j'):
205 if constexpr (chrono::__is_duration<_Tp>::value)
206 // Converting a duration where the period has a small ratio to days
207 // may fail to compile. This due to loss of precision in the
208 // conversion. In order to avoid that issue convert to seconds as
209 // an intemediate step.
210 __sstr << chrono::duration_cast<chrono::days>(chrono::duration_cast<chrono::seconds>(__value)).count();
211 else
212 __facet.put({__sstr}, __sstr, _CharT(' '), std::addressof(__t), std::to_address(__s), std::to_address(__it + 1));
213 break;
214
215 case _CharT('q'):
216 if constexpr (chrono::__is_duration<_Tp>::value) {
217 __sstr << chrono::__units_suffix<_CharT, typename _Tp::period>();
218 break;
219 }
220 __builtin_unreachable();
221
222 case _CharT('Q'):
223 // TODO FMT Determine the proper ideas
224 // - Should it honour the precision?
225 // - Shoult it honour the locale setting for the separators?
226 // The wording for Q doesn't use the word locale and the effect of
227 // precision is unspecified.
228 //
229 // MSVC STL ignores precision but uses separator
230 // FMT honours precision and has a bug for separator
231 // https://godbolt.org/z/78b7sMxns
232 if constexpr (chrono::__is_duration<_Tp>::value) {
233 __sstr << std::format(_LIBCPP_STATICALLY_WIDEN(_CharT, "{}"), __value.count());
234 break;
235 }
236 __builtin_unreachable();
237
238 case _CharT('S'):
239 case _CharT('T'):
240 __facet.put({__sstr}, __sstr, _CharT(' '), std::addressof(__t), std::to_address(__s), std::to_address(__it + 1));
241 if constexpr (__use_fraction<_Tp>())
242 __formatter::__format_sub_seconds(__value, __sstr);
243 break;
244
245 // Unlike time_put and strftime the formatting library requires %Y
246 //
247 // [tab:time.format.spec]
248 // The year as a decimal number. If the result is less than four digits
249 // it is left-padded with 0 to four digits.
250 //
251 // This means years in the range (-1000, 1000) need manual formatting.
252 // It's unclear whether %EY needs the same treatment. For example the
253 // Japanese EY contains the era name and year. This is zero-padded to 2
254 // digits in time_put (note that older glibc versions didn't do
255 // padding.) However most eras won't reach 100 years, let alone 1000.
256 // So padding to 4 digits seems unwanted for Japanese.
257 //
258 // The same applies to %Ex since that too depends on the era.
259 //
260 // %x the locale's date representation is currently doesn't handle the
261 // zero-padding too.
262 //
263 // The 4 digits can be implemented better at a later time. On POSIX
264 // systems the required information can be extracted by nl_langinfo
265 // https://man7.org/linux/man-pages/man3/nl_langinfo.3.html
266 //
267 // Note since year < -1000 is expected to be rare it uses the more
268 // expensive year routine.
269 //
270 // TODO FMT evaluate the comment above.
271
272 # if defined(__GLIBC__) || defined(_AIX)
273 case _CharT('y'):
274 // Glibc fails for negative values, AIX for positive values too.
275 __sstr << std::format(_LIBCPP_STATICALLY_WIDEN(_CharT, "{:02}"), (std::abs(__t.tm_year + 1900)) % 100);
276 break;
277 # endif // defined(__GLIBC__) || defined(_AIX)
278
279 case _CharT('Y'): {
280 int __year = __t.tm_year + 1900;
281 if (__year < 1000)
282 __formatter::__format_year(__year, __sstr);
283 else
284 __facet.put({__sstr}, __sstr, _CharT(' '), std::addressof(__t), std::to_address(__s), std::to_address(__it + 1));
285 } break;
286
287 case _CharT('F'): {
288 int __year = __t.tm_year + 1900;
289 if (__year < 1000) {
290 __formatter::__format_year(__year, __sstr);
291 __sstr << std::format(_LIBCPP_STATICALLY_WIDEN(_CharT, "-{:02}-{:02}"), __t.tm_mon + 1, __t.tm_mday);
292 } else
293 __facet.put({__sstr}, __sstr, _CharT(' '), std::addressof(__t), std::to_address(__s), std::to_address(__it + 1));
294 } break;
295
296 case _CharT('Z'):
297 // TODO FMT Add proper timezone support.
298 __sstr << _LIBCPP_STATICALLY_WIDEN(_CharT, "UTC");
299 break;
300
301 case _CharT('O'):
302 if constexpr (__use_fraction<_Tp>()) {
303 // Handle OS using the normal representation for the non-fractional
304 // part. There seems to be no locale information regarding how the
305 // fractional part should be formatted.
306 if (*(__it + 1) == 'S') {
307 ++__it;
308 __facet.put({__sstr}, __sstr, _CharT(' '), std::addressof(__t), std::to_address(__s), std::to_address(__it + 1));
309 __formatter::__format_sub_seconds(__value, __sstr);
310 break;
311 }
312 }
313 [[fallthrough]];
314 case _CharT('E'):
315 ++__it;
316 [[fallthrough]];
317 default:
318 __facet.put({__sstr}, __sstr, _CharT(' '), std::addressof(__t), std::to_address(__s), std::to_address(__it + 1));
319 break;
320 }
321 } else {
322 __sstr << *__it;
323 }
324 }
325 }
326
327 template <class _Tp>
__weekday_ok(const _Tp & __value)328 _LIBCPP_HIDE_FROM_ABI constexpr bool __weekday_ok(const _Tp& __value) {
329 if constexpr (__is_time_point<_Tp>)
330 return true;
331 else if constexpr (same_as<_Tp, chrono::day>)
332 return true;
333 else if constexpr (same_as<_Tp, chrono::month>)
334 return __value.ok();
335 else if constexpr (same_as<_Tp, chrono::year>)
336 return true;
337 else if constexpr (same_as<_Tp, chrono::weekday>)
338 return true;
339 else if constexpr (same_as<_Tp, chrono::weekday_indexed>)
340 return true;
341 else if constexpr (same_as<_Tp, chrono::weekday_last>)
342 return true;
343 else if constexpr (same_as<_Tp, chrono::month_day>)
344 return true;
345 else if constexpr (same_as<_Tp, chrono::month_day_last>)
346 return true;
347 else if constexpr (same_as<_Tp, chrono::month_weekday>)
348 return true;
349 else if constexpr (same_as<_Tp, chrono::month_weekday_last>)
350 return true;
351 else if constexpr (same_as<_Tp, chrono::year_month>)
352 return true;
353 else if constexpr (same_as<_Tp, chrono::year_month_day>)
354 return __value.ok();
355 else if constexpr (same_as<_Tp, chrono::year_month_day_last>)
356 return __value.ok();
357 else if constexpr (same_as<_Tp, chrono::year_month_weekday>)
358 return __value.weekday().ok();
359 else if constexpr (same_as<_Tp, chrono::year_month_weekday_last>)
360 return __value.weekday().ok();
361 else if constexpr (__is_hh_mm_ss<_Tp>)
362 return true;
363 else
364 static_assert(sizeof(_Tp) == 0, "Add the missing type specialization");
365 }
366
367 template <class _Tp>
__weekday_name_ok(const _Tp & __value)368 _LIBCPP_HIDE_FROM_ABI constexpr bool __weekday_name_ok(const _Tp& __value) {
369 if constexpr (__is_time_point<_Tp>)
370 return true;
371 else if constexpr (same_as<_Tp, chrono::day>)
372 return true;
373 else if constexpr (same_as<_Tp, chrono::month>)
374 return __value.ok();
375 else if constexpr (same_as<_Tp, chrono::year>)
376 return true;
377 else if constexpr (same_as<_Tp, chrono::weekday>)
378 return __value.ok();
379 else if constexpr (same_as<_Tp, chrono::weekday_indexed>)
380 return __value.weekday().ok();
381 else if constexpr (same_as<_Tp, chrono::weekday_last>)
382 return __value.weekday().ok();
383 else if constexpr (same_as<_Tp, chrono::month_day>)
384 return true;
385 else if constexpr (same_as<_Tp, chrono::month_day_last>)
386 return true;
387 else if constexpr (same_as<_Tp, chrono::month_weekday>)
388 return __value.weekday_indexed().ok();
389 else if constexpr (same_as<_Tp, chrono::month_weekday_last>)
390 return __value.weekday_indexed().ok();
391 else if constexpr (same_as<_Tp, chrono::year_month>)
392 return true;
393 else if constexpr (same_as<_Tp, chrono::year_month_day>)
394 return __value.ok();
395 else if constexpr (same_as<_Tp, chrono::year_month_day_last>)
396 return __value.ok();
397 else if constexpr (same_as<_Tp, chrono::year_month_weekday>)
398 return __value.weekday().ok();
399 else if constexpr (same_as<_Tp, chrono::year_month_weekday_last>)
400 return __value.weekday().ok();
401 else if constexpr (__is_hh_mm_ss<_Tp>)
402 return true;
403 else
404 static_assert(sizeof(_Tp) == 0, "Add the missing type specialization");
405 }
406
407 template <class _Tp>
__date_ok(const _Tp & __value)408 _LIBCPP_HIDE_FROM_ABI constexpr bool __date_ok(const _Tp& __value) {
409 if constexpr (__is_time_point<_Tp>)
410 return true;
411 else if constexpr (same_as<_Tp, chrono::day>)
412 return true;
413 else if constexpr (same_as<_Tp, chrono::month>)
414 return __value.ok();
415 else if constexpr (same_as<_Tp, chrono::year>)
416 return true;
417 else if constexpr (same_as<_Tp, chrono::weekday>)
418 return true;
419 else if constexpr (same_as<_Tp, chrono::weekday_indexed>)
420 return true;
421 else if constexpr (same_as<_Tp, chrono::weekday_last>)
422 return true;
423 else if constexpr (same_as<_Tp, chrono::month_day>)
424 return true;
425 else if constexpr (same_as<_Tp, chrono::month_day_last>)
426 return true;
427 else if constexpr (same_as<_Tp, chrono::month_weekday>)
428 return true;
429 else if constexpr (same_as<_Tp, chrono::month_weekday_last>)
430 return true;
431 else if constexpr (same_as<_Tp, chrono::year_month>)
432 return true;
433 else if constexpr (same_as<_Tp, chrono::year_month_day>)
434 return __value.ok();
435 else if constexpr (same_as<_Tp, chrono::year_month_day_last>)
436 return __value.ok();
437 else if constexpr (same_as<_Tp, chrono::year_month_weekday>)
438 return __value.ok();
439 else if constexpr (same_as<_Tp, chrono::year_month_weekday_last>)
440 return __value.ok();
441 else if constexpr (__is_hh_mm_ss<_Tp>)
442 return true;
443 else
444 static_assert(sizeof(_Tp) == 0, "Add the missing type specialization");
445 }
446
447 template <class _Tp>
__month_name_ok(const _Tp & __value)448 _LIBCPP_HIDE_FROM_ABI constexpr bool __month_name_ok(const _Tp& __value) {
449 if constexpr (__is_time_point<_Tp>)
450 return true;
451 else if constexpr (same_as<_Tp, chrono::day>)
452 return true;
453 else if constexpr (same_as<_Tp, chrono::month>)
454 return __value.ok();
455 else if constexpr (same_as<_Tp, chrono::year>)
456 return true;
457 else if constexpr (same_as<_Tp, chrono::weekday>)
458 return true;
459 else if constexpr (same_as<_Tp, chrono::weekday_indexed>)
460 return true;
461 else if constexpr (same_as<_Tp, chrono::weekday_last>)
462 return true;
463 else if constexpr (same_as<_Tp, chrono::month_day>)
464 return __value.month().ok();
465 else if constexpr (same_as<_Tp, chrono::month_day_last>)
466 return __value.month().ok();
467 else if constexpr (same_as<_Tp, chrono::month_weekday>)
468 return __value.month().ok();
469 else if constexpr (same_as<_Tp, chrono::month_weekday_last>)
470 return __value.month().ok();
471 else if constexpr (same_as<_Tp, chrono::year_month>)
472 return __value.month().ok();
473 else if constexpr (same_as<_Tp, chrono::year_month_day>)
474 return __value.month().ok();
475 else if constexpr (same_as<_Tp, chrono::year_month_day_last>)
476 return __value.month().ok();
477 else if constexpr (same_as<_Tp, chrono::year_month_weekday>)
478 return __value.month().ok();
479 else if constexpr (same_as<_Tp, chrono::year_month_weekday_last>)
480 return __value.month().ok();
481 else if constexpr (__is_hh_mm_ss<_Tp>)
482 return true;
483 else
484 static_assert(sizeof(_Tp) == 0, "Add the missing type specialization");
485 }
486
487 template <class _CharT, class _Tp>
488 _LIBCPP_HIDE_FROM_ABI auto
489 __format_chrono(const _Tp& __value,
490 auto& __ctx,
491 __format_spec::__parsed_specifications<_CharT> __specs,
492 basic_string_view<_CharT> __chrono_specs) -> decltype(__ctx.out()) {
493 basic_stringstream<_CharT> __sstr;
494 // [time.format]/2
495 // 2.1 - the "C" locale if the L option is not present in chrono-format-spec, otherwise
496 // 2.2 - the locale passed to the formatting function if any, otherwise
497 // 2.3 - the global locale.
498 // Note that the __ctx's locale() call does 2.2 and 2.3.
499 if (__specs.__chrono_.__locale_specific_form_)
500 __sstr.imbue(__ctx.locale());
501 else
502 __sstr.imbue(locale::classic());
503
504 if (__chrono_specs.empty())
505 __sstr << __value;
506 else {
507 if constexpr (chrono::__is_duration<_Tp>::value) {
508 if (__value < __value.zero())
509 __sstr << _CharT('-');
510 __formatter::__format_chrono_using_chrono_specs(chrono::abs(__value), __sstr, __chrono_specs);
511 // TODO FMT When keeping the precision it will truncate the string.
512 // Note that the behaviour what the precision does isn't specified.
513 __specs.__precision_ = -1;
514 } else {
515 // Test __weekday_name_ before __weekday_ to give a better error.
516 if (__specs.__chrono_.__weekday_name_ && !__formatter::__weekday_name_ok(__value))
517 std::__throw_format_error("formatting a weekday name needs a valid weekday");
518
519 if (__specs.__chrono_.__weekday_ && !__formatter::__weekday_ok(__value))
520 std::__throw_format_error("formatting a weekday needs a valid weekday");
521
522 if (__specs.__chrono_.__day_of_year_ && !__formatter::__date_ok(__value))
523 std::__throw_format_error("formatting a day of year needs a valid date");
524
525 if (__specs.__chrono_.__week_of_year_ && !__formatter::__date_ok(__value))
526 std::__throw_format_error("formatting a week of year needs a valid date");
527
528 if (__specs.__chrono_.__month_name_ && !__formatter::__month_name_ok(__value))
529 std::__throw_format_error("formatting a month name from an invalid month number");
530
531 if constexpr (__is_hh_mm_ss<_Tp>) {
532 // Note this is a pedantic intepretation of the Standard. A hh_mm_ss
533 // is no longer a time_of_day and can store an arbitrary number of
534 // hours. A number of hours in a 12 or 24 hour clock can't represent
535 // 24 hours or more. The functions std::chrono::make12 and
536 // std::chrono::make24 reaffirm this view point.
537 //
538 // Interestingly this will be the only output stream function that
539 // throws.
540 //
541 // TODO FMT The wording probably needs to be adapted to
542 // - The displayed hours is hh_mm_ss.hours() % 24
543 // - It should probably allow %j in the same fashion as duration.
544 // - The stream formatter should change its output when hours >= 24
545 // - Write it as not valid,
546 // - or write the number of days.
547 if (__specs.__chrono_.__hour_ && __value.hours().count() > 23)
548 std::__throw_format_error("formatting a hour needs a valid value");
549
550 if (__value.is_negative())
551 __sstr << _CharT('-');
552 }
553
554 __formatter::__format_chrono_using_chrono_specs(__value, __sstr, __chrono_specs);
555 }
556 }
557
558 // TODO FMT Use the stringstream's view after P0408R7 has been implemented.
559 basic_string<_CharT> __str = __sstr.str();
560 return __formatter::__write_string(basic_string_view<_CharT>{__str}, __ctx.out(), __specs);
561 }
562
563 } // namespace __formatter
564
565 template <__fmt_char_type _CharT>
566 struct _LIBCPP_TEMPLATE_VIS __formatter_chrono {
567 public:
568 _LIBCPP_HIDE_FROM_ABI constexpr auto __parse(
569 basic_format_parse_context<_CharT>& __parse_ctx, __format_spec::__fields __fields, __format_spec::__flags __flags)
570 -> decltype(__parse_ctx.begin()) {
571 return __parser_.__parse(__parse_ctx, __fields, __flags);
572 }
573
574 template <class _Tp>
575 _LIBCPP_HIDE_FROM_ABI auto format(const _Tp& __value, auto& __ctx) const -> decltype(__ctx.out()) const {
576 return __formatter::__format_chrono(
577 __value, __ctx, __parser_.__parser_.__get_parsed_chrono_specifications(__ctx), __parser_.__chrono_specs_);
578 }
579
580 __format_spec::__parser_chrono<_CharT> __parser_;
581 };
582
583 template <class _Duration, __fmt_char_type _CharT>
584 struct _LIBCPP_TEMPLATE_VIS formatter<chrono::sys_time<_Duration>, _CharT> : public __formatter_chrono<_CharT> {
585 public:
586 using _Base = __formatter_chrono<_CharT>;
587
588 _LIBCPP_HIDE_FROM_ABI constexpr auto parse(basic_format_parse_context<_CharT>& __parse_ctx)
589 -> decltype(__parse_ctx.begin()) {
590 return _Base::__parse(__parse_ctx, __format_spec::__fields_chrono, __format_spec::__flags::__clock);
591 }
592 };
593
594 template <class _Rep, class _Period, __fmt_char_type _CharT>
595 struct formatter<chrono::duration<_Rep, _Period>, _CharT> : public __formatter_chrono<_CharT> {
596 public:
597 using _Base = __formatter_chrono<_CharT>;
598
599 _LIBCPP_HIDE_FROM_ABI constexpr auto parse(basic_format_parse_context<_CharT>& __parse_ctx)
600 -> decltype(__parse_ctx.begin()) {
601 // [time.format]/1
602 // Giving a precision specification in the chrono-format-spec is valid only
603 // for std::chrono::duration types where the representation type Rep is a
604 // floating-point type. For all other Rep types, an exception of type
605 // format_error is thrown if the chrono-format-spec contains a precision
606 // specification.
607 //
608 // Note this doesn't refer to chrono::treat_as_floating_point_v<_Rep>.
609 if constexpr (std::floating_point<_Rep>)
610 return _Base::__parse(__parse_ctx, __format_spec::__fields_chrono_fractional, __format_spec::__flags::__duration);
611 else
612 return _Base::__parse(__parse_ctx, __format_spec::__fields_chrono, __format_spec::__flags::__duration);
613 }
614 };
615
616 template <__fmt_char_type _CharT>
617 struct _LIBCPP_TEMPLATE_VIS formatter<chrono::day, _CharT>
618 : public __formatter_chrono<_CharT> {
619 public:
620 using _Base = __formatter_chrono<_CharT>;
621
622 _LIBCPP_HIDE_FROM_ABI constexpr auto parse(basic_format_parse_context<_CharT>& __parse_ctx)
623 -> decltype(__parse_ctx.begin()) {
624 return _Base::__parse(__parse_ctx, __format_spec::__fields_chrono, __format_spec::__flags::__day);
625 }
626 };
627
628 template <__fmt_char_type _CharT>
629 struct _LIBCPP_TEMPLATE_VIS formatter<chrono::month, _CharT>
630 : public __formatter_chrono<_CharT> {
631 public:
632 using _Base = __formatter_chrono<_CharT>;
633
634 _LIBCPP_HIDE_FROM_ABI constexpr auto parse(basic_format_parse_context<_CharT>& __parse_ctx)
635 -> decltype(__parse_ctx.begin()) {
636 return _Base::__parse(__parse_ctx, __format_spec::__fields_chrono, __format_spec::__flags::__month);
637 }
638 };
639
640 template <__fmt_char_type _CharT>
641 struct _LIBCPP_TEMPLATE_VIS formatter<chrono::year, _CharT>
642 : public __formatter_chrono<_CharT> {
643 public:
644 using _Base = __formatter_chrono<_CharT>;
645
646 _LIBCPP_HIDE_FROM_ABI constexpr auto parse(basic_format_parse_context<_CharT>& __parse_ctx)
647 -> decltype(__parse_ctx.begin()) {
648 return _Base::__parse(__parse_ctx, __format_spec::__fields_chrono, __format_spec::__flags::__year);
649 }
650 };
651
652 template <__fmt_char_type _CharT>
653 struct _LIBCPP_TEMPLATE_VIS formatter<chrono::weekday, _CharT>
654 : public __formatter_chrono<_CharT> {
655 public:
656 using _Base = __formatter_chrono<_CharT>;
657
658 _LIBCPP_HIDE_FROM_ABI constexpr auto parse(basic_format_parse_context<_CharT>& __parse_ctx)
659 -> decltype(__parse_ctx.begin()) {
660 return _Base::__parse(__parse_ctx, __format_spec::__fields_chrono, __format_spec::__flags::__weekday);
661 }
662 };
663
664 template <__fmt_char_type _CharT>
665 struct _LIBCPP_TEMPLATE_VIS formatter<chrono::weekday_indexed, _CharT>
666 : public __formatter_chrono<_CharT> {
667 public:
668 using _Base = __formatter_chrono<_CharT>;
669
670 _LIBCPP_HIDE_FROM_ABI constexpr auto parse(basic_format_parse_context<_CharT>& __parse_ctx)
671 -> decltype(__parse_ctx.begin()) {
672 return _Base::__parse(__parse_ctx, __format_spec::__fields_chrono, __format_spec::__flags::__weekday);
673 }
674 };
675
676 template <__fmt_char_type _CharT>
677 struct _LIBCPP_TEMPLATE_VIS formatter<chrono::weekday_last, _CharT>
678 : public __formatter_chrono<_CharT> {
679 public:
680 using _Base = __formatter_chrono<_CharT>;
681
682 _LIBCPP_HIDE_FROM_ABI constexpr auto parse(basic_format_parse_context<_CharT>& __parse_ctx)
683 -> decltype(__parse_ctx.begin()) {
684 return _Base::__parse(__parse_ctx, __format_spec::__fields_chrono, __format_spec::__flags::__weekday);
685 }
686 };
687
688 template <__fmt_char_type _CharT>
689 struct _LIBCPP_TEMPLATE_VIS formatter<chrono::month_day, _CharT>
690 : public __formatter_chrono<_CharT> {
691 public:
692 using _Base = __formatter_chrono<_CharT>;
693
694 _LIBCPP_HIDE_FROM_ABI constexpr auto parse(basic_format_parse_context<_CharT>& __parse_ctx)
695 -> decltype(__parse_ctx.begin()) {
696 return _Base::__parse(__parse_ctx, __format_spec::__fields_chrono, __format_spec::__flags::__month_day);
697 }
698 };
699
700 template <__fmt_char_type _CharT>
701 struct _LIBCPP_TEMPLATE_VIS formatter<chrono::month_day_last, _CharT>
702 : public __formatter_chrono<_CharT> {
703 public:
704 using _Base = __formatter_chrono<_CharT>;
705
706 _LIBCPP_HIDE_FROM_ABI constexpr auto parse(basic_format_parse_context<_CharT>& __parse_ctx)
707 -> decltype(__parse_ctx.begin()) {
708 return _Base::__parse(__parse_ctx, __format_spec::__fields_chrono, __format_spec::__flags::__month);
709 }
710 };
711
712 template <__fmt_char_type _CharT>
713 struct _LIBCPP_TEMPLATE_VIS formatter<chrono::month_weekday, _CharT>
714 : public __formatter_chrono<_CharT> {
715 public:
716 using _Base = __formatter_chrono<_CharT>;
717
718 _LIBCPP_HIDE_FROM_ABI constexpr auto parse(basic_format_parse_context<_CharT>& __parse_ctx)
719 -> decltype(__parse_ctx.begin()) {
720 return _Base::__parse(__parse_ctx, __format_spec::__fields_chrono, __format_spec::__flags::__month_weekday);
721 }
722 };
723
724 template <__fmt_char_type _CharT>
725 struct _LIBCPP_TEMPLATE_VIS formatter<chrono::month_weekday_last, _CharT>
726 : public __formatter_chrono<_CharT> {
727 public:
728 using _Base = __formatter_chrono<_CharT>;
729
730 _LIBCPP_HIDE_FROM_ABI constexpr auto parse(basic_format_parse_context<_CharT>& __parse_ctx)
731 -> decltype(__parse_ctx.begin()) {
732 return _Base::__parse(__parse_ctx, __format_spec::__fields_chrono, __format_spec::__flags::__month_weekday);
733 }
734 };
735
736 template <__fmt_char_type _CharT>
737 struct _LIBCPP_TEMPLATE_VIS formatter<chrono::year_month, _CharT>
738 : public __formatter_chrono<_CharT> {
739 public:
740 using _Base = __formatter_chrono<_CharT>;
741
742 _LIBCPP_HIDE_FROM_ABI constexpr auto parse(basic_format_parse_context<_CharT>& __parse_ctx)
743 -> decltype(__parse_ctx.begin()) {
744 return _Base::__parse(__parse_ctx, __format_spec::__fields_chrono, __format_spec::__flags::__year_month);
745 }
746 };
747
748 template <__fmt_char_type _CharT>
749 struct _LIBCPP_TEMPLATE_VIS formatter<chrono::year_month_day, _CharT>
750 : public __formatter_chrono<_CharT> {
751 public:
752 using _Base = __formatter_chrono<_CharT>;
753
754 _LIBCPP_HIDE_FROM_ABI constexpr auto parse(basic_format_parse_context<_CharT>& __parse_ctx)
755 -> decltype(__parse_ctx.begin()) {
756 return _Base::__parse(__parse_ctx, __format_spec::__fields_chrono, __format_spec::__flags::__date);
757 }
758 };
759
760 template <__fmt_char_type _CharT>
761 struct _LIBCPP_TEMPLATE_VIS formatter<chrono::year_month_day_last, _CharT>
762 : public __formatter_chrono<_CharT> {
763 public:
764 using _Base = __formatter_chrono<_CharT>;
765
766 _LIBCPP_HIDE_FROM_ABI constexpr auto parse(basic_format_parse_context<_CharT>& __parse_ctx)
767 -> decltype(__parse_ctx.begin()) {
768 return _Base::__parse(__parse_ctx, __format_spec::__fields_chrono, __format_spec::__flags::__date);
769 }
770 };
771
772 template <__fmt_char_type _CharT>
773 struct _LIBCPP_TEMPLATE_VIS formatter<chrono::year_month_weekday, _CharT>
774 : public __formatter_chrono<_CharT> {
775 public:
776 using _Base = __formatter_chrono<_CharT>;
777
778 _LIBCPP_HIDE_FROM_ABI constexpr auto parse(basic_format_parse_context<_CharT>& __parse_ctx)
779 -> decltype(__parse_ctx.begin()) {
780 return _Base::__parse(__parse_ctx, __format_spec::__fields_chrono, __format_spec::__flags::__date);
781 }
782 };
783
784 template <__fmt_char_type _CharT>
785 struct _LIBCPP_TEMPLATE_VIS formatter<chrono::year_month_weekday_last, _CharT>
786 : public __formatter_chrono<_CharT> {
787 public:
788 using _Base = __formatter_chrono<_CharT>;
789
790 _LIBCPP_HIDE_FROM_ABI constexpr auto parse(basic_format_parse_context<_CharT>& __parse_ctx)
791 -> decltype(__parse_ctx.begin()) {
792 return _Base::__parse(__parse_ctx, __format_spec::__fields_chrono, __format_spec::__flags::__date);
793 }
794 };
795
796 template <class _Duration, __fmt_char_type _CharT>
797 struct formatter<chrono::hh_mm_ss<_Duration>, _CharT> : public __formatter_chrono<_CharT> {
798 public:
799 using _Base = __formatter_chrono<_CharT>;
800
801 _LIBCPP_HIDE_FROM_ABI constexpr auto parse(basic_format_parse_context<_CharT>& __parse_ctx)
802 -> decltype(__parse_ctx.begin()) {
803 return _Base::__parse(__parse_ctx, __format_spec::__fields_chrono, __format_spec::__flags::__time);
804 }
805 };
806 #endif // if _LIBCPP_STD_VER >= 20 && !defined(_LIBCPP_HAS_NO_INCOMPLETE_FORMAT)
807
808 _LIBCPP_END_NAMESPACE_STD
809
810 #endif // _LIBCPP___CHRONO_FORMATTER_H
811