1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9// TODO(mordante) Investigate
10// UNSUPPORTED: apple-clang
11
12// UNSUPPORTED: c++03, c++11, c++14, c++17
13// UNSUPPORTED: no-localization
14// UNSUPPORTED: GCC-ALWAYS_INLINE-FIXME
15
16// XFAIL: availability-fp_to_chars-missing
17
18// REQUIRES: locale.fr_FR.UTF-8
19// REQUIRES: locale.ja_JP.UTF-8
20
21// <chrono>
22
23// template<class Rep, class Period, class charT>
24// struct formatter<chrono::duration<Rep, Period>, charT>;
25
26#include <chrono>
27#include <format>
28
29#include <cassert>
30#include <concepts>
31#include <locale>
32#include <iostream>
33#include <ratio>
34#include <type_traits>
35
36#include "formatter_tests.h"
37#include "make_string.h"
38#include "platform_support.h" // locale name macros
39#include "test_macros.h"
40
41template <class CharT>
42static void test_no_chrono_specs() {
43 using namespace std::literals::chrono_literals;
44
45 check(SV("1as"), SV("{}"), std::chrono::duration<int, std::atto>(1));
46 check(SV("1fs"), SV("{}"), std::chrono::duration<int, std::femto>(1));
47 check(SV("1ps"), SV("{}"), std::chrono::duration<int, std::pico>(1));
48 check(SV("1ns"), SV("{}"), 1ns);
49#ifndef TEST_HAS_NO_UNICODE
50 check(SV("1\u00b5s"), SV("{}"), 1us);
51#else
52 check(SV("1us"), SV("{}"), 1us);
53#endif
54 check(SV("1ms"), SV("{}"), 1ms);
55 check(SV("1cs"), SV("{}"), std::chrono::duration<int, std::centi>(1));
56 check(SV("1ds"), SV("{}"), std::chrono::duration<int, std::deci>(1));
57
58 check(SV("1s"), SV("{}"), 1s);
59
60 check(SV("1das"), SV("{}"), std::chrono::duration<int, std::deca>(1));
61 check(SV("1hs"), SV("{}"), std::chrono::duration<int, std::hecto>(1));
62 check(SV("1ks"), SV("{}"), std::chrono::duration<int, std::kilo>(1));
63 check(SV("1Ms"), SV("{}"), std::chrono::duration<int, std::mega>(1));
64 check(SV("1Gs"), SV("{}"), std::chrono::duration<int, std::giga>(1));
65 check(SV("1Ts"), SV("{}"), std::chrono::duration<int, std::tera>(1));
66 check(SV("1Ps"), SV("{}"), std::chrono::duration<int, std::peta>(1));
67 check(SV("1Es"), SV("{}"), std::chrono::duration<int, std::exa>(1));
68
69 check(SV("1min"), SV("{}"), 1min);
70 check(SV("1h"), SV("{}"), 1h);
71 check(SV("1d"), SV("{}"), std::chrono::duration<int, std::ratio<86400>>(1));
72
73 check(SV("1[42]s"), SV("{}"), std::chrono::duration<int, std::ratio<42>>(1));
74 check(SV("1[11]s"), SV("{}"), std::chrono::duration<int, std::ratio<33, 3>>(1));
75 check(SV("1[11/9]s"), SV("{}"), std::chrono::duration<int, std::ratio<11, 9>>(1));
76}
77
78template <class CharT>
79static void test_valid_positive_integral_values() {
80 using namespace std::literals::chrono_literals;
81
82 constexpr std::basic_string_view<CharT> fmt = SV(
83 "{:"
84 "%%H='%H'%t"
85 "%%OH='%OH'%t"
86 "%%I='%I'%t"
87 "%%OI='%OI'%t"
88 "%%M='%M'%t"
89 "%%OM='%OM'%t"
90 "%%S='%S'%t"
91 "%%OS='%OS'%t"
92 "%%p='%p'%t"
93 "%%R='%R'%t"
94 "%%T='%T'%t"
95 "%%r='%r'%t"
96 "%%X='%X'%t"
97 "%%EX='%EX'%t"
98 "%%j='%j'%t"
99 "%%Q='%Q'%t"
100 "%%q='%q'%t"
101 "%n}");
102 constexpr std::basic_string_view<CharT> lfmt = SV(
103 "{:L"
104 "%%H='%H'%t"
105 "%%OH='%OH'%t"
106 "%%I='%I'%t"
107 "%%OI='%OI'%t"
108 "%%M='%M'%t"
109 "%%OM='%OM'%t"
110 "%%S='%S'%t"
111 "%%OS='%OS'%t"
112 "%%p='%p'%t"
113 "%%R='%R'%t"
114 "%%T='%T'%t"
115 "%%r='%r'%t"
116 "%%X='%X'%t"
117 "%%EX='%EX'%t"
118 "%%j='%j'%t"
119 "%%Q='%Q'%t"
120 "%%q='%q'%t"
121 "%n}");
122
123 const std::locale loc(LOCALE_ja_JP_UTF_8);
124 std::locale::global(std::locale(LOCALE_fr_FR_UTF_8));
125
126 // Non localized output using C-locale
127 check(SV("%H='00'\t"
128 "%OH='00'\t"
129 "%I='12'\t"
130 "%OI='12'\t"
131 "%M='00'\t"
132 "%OM='00'\t"
133 "%S='00'\t"
134 "%OS='00'\t"
135 "%p='AM'\t"
136 "%R='00:00'\t"
137 "%T='00:00:00'\t"
138 "%r='12:00:00 AM'\t"
139 "%X='00:00:00'\t"
140 "%EX='00:00:00'\t"
141 "%j='0'\t"
142 "%Q='0'\t"
143 "%q='s'\t"
144 "\n"),
145 fmt,
146 0s);
147
148 check(SV("%H='11'\t"
149 "%OH='11'\t"
150 "%I='11'\t"
151 "%OI='11'\t"
152 "%M='59'\t"
153 "%OM='59'\t"
154 "%S='59'\t"
155 "%OS='59'\t"
156 "%p='AM'\t"
157 "%R='11:59'\t"
158 "%T='11:59:59'\t"
159 "%r='11:59:59 AM'\t"
160 "%X='11:59:59'\t"
161 "%EX='11:59:59'\t"
162 "%j='0'\t"
163 "%Q='43199'\t"
164 "%q='s'\t"
165 "\n"),
166 fmt,
167 11h + 59min + 59s);
168
169 check(SV("%H='12'\t"
170 "%OH='12'\t"
171 "%I='12'\t"
172 "%OI='12'\t"
173 "%M='00'\t"
174 "%OM='00'\t"
175 "%S='00'\t"
176 "%OS='00'\t"
177 "%p='PM'\t"
178 "%R='12:00'\t"
179 "%T='12:00:00'\t"
180 "%r='12:00:00 PM'\t"
181 "%X='12:00:00'\t"
182 "%EX='12:00:00'\t"
183 "%j='0'\t"
184 "%Q='12'\t"
185 "%q='h'\t"
186 "\n"),
187 fmt,
188 12h);
189
190 check(SV("%H='23'\t"
191 "%OH='23'\t"
192 "%I='11'\t"
193 "%OI='11'\t"
194 "%M='59'\t"
195 "%OM='59'\t"
196 "%S='59'\t"
197 "%OS='59'\t"
198 "%p='PM'\t"
199 "%R='23:59'\t"
200 "%T='23:59:59'\t"
201 "%r='11:59:59 PM'\t"
202 "%X='23:59:59'\t"
203 "%EX='23:59:59'\t"
204 "%j='0'\t"
205 "%Q='86399'\t"
206 "%q='s'\t"
207 "\n"),
208 fmt,
209 23h + 59min + 59s);
210
211 check(SV("%H='00'\t"
212 "%OH='00'\t"
213 "%I='12'\t"
214 "%OI='12'\t"
215 "%M='00'\t"
216 "%OM='00'\t"
217 "%S='00'\t"
218 "%OS='00'\t"
219 "%p='AM'\t"
220 "%R='00:00'\t"
221 "%T='00:00:00'\t"
222 "%r='12:00:00 AM'\t"
223 "%X='00:00:00'\t"
224 "%EX='00:00:00'\t"
225 "%j='7'\t"
226 "%Q='7'\t"
227 "%q='d'\t"
228 "\n"),
229 fmt,
230 std::chrono::duration<int, std::ratio<86400>>(7));
231
232 // Use the global locale (fr_FR)
233 check(SV("%H='00'\t"
234 "%OH='00'\t"
235 "%I='12'\t"
236 "%OI='12'\t"
237 "%M='00'\t"
238 "%OM='00'\t"
239 "%S='00'\t"
240 "%OS='00'\t"
241#if defined(_AIX)
242 "%p='AM'\t"
243#else
244 "%p=''\t"
245#endif
246 "%R='00:00'\t"
247 "%T='00:00:00'\t"
248#ifdef _WIN32
249 "%r='00:00:00'\t"
250#elif defined(_AIX)
251 "%r='12:00:00 AM'\t"
252#elif defined(__APPLE__) || defined(__FreeBSD__)
253 "%r=''\t"
254#else
255 "%r='12:00:00 '\t"
256#endif
257 "%X='00:00:00'\t"
258 "%EX='00:00:00'\t"
259 "%j='0'\t"
260 "%Q='0'\t"
261 "%q='s'\t"
262 "\n"),
263 lfmt,
264 0s);
265
266 check(SV("%H='11'\t"
267 "%OH='11'\t"
268 "%I='11'\t"
269 "%OI='11'\t"
270 "%M='59'\t"
271 "%OM='59'\t"
272 "%S='59'\t"
273 "%OS='59'\t"
274#if defined(_AIX)
275 "%p='AM'\t"
276#else
277 "%p=''\t"
278#endif
279 "%R='11:59'\t"
280 "%T='11:59:59'\t"
281#ifdef _WIN32
282 "%r='11:59:59'\t"
283#elif defined(_AIX)
284 "%r='11:59:59 AM'\t"
285#elif defined(__APPLE__) || defined(__FreeBSD__)
286 "%r=''\t"
287#else
288 "%r='11:59:59 '\t"
289#endif
290 "%X='11:59:59'\t"
291 "%EX='11:59:59'\t"
292 "%j='0'\t"
293 "%Q='43199'\t"
294 "%q='s'\t"
295 "\n"),
296 lfmt,
297 11h + 59min + 59s);
298
299 check(SV("%H='12'\t"
300 "%OH='12'\t"
301 "%I='12'\t"
302 "%OI='12'\t"
303 "%M='00'\t"
304 "%OM='00'\t"
305 "%S='00'\t"
306 "%OS='00'\t"
307#if defined(_AIX)
308 "%p='PM'\t"
309#else
310 "%p=''\t"
311#endif
312 "%R='12:00'\t"
313 "%T='12:00:00'\t"
314#ifdef _WIN32
315 "%r='12:00:00'\t"
316#elif defined(_AIX)
317 "%r='12:00:00 PM'\t"
318#elif defined(__APPLE__) || defined(__FreeBSD__)
319 "%r=''\t"
320#else
321 "%r='12:00:00 '\t"
322#endif
323 "%X='12:00:00'\t"
324 "%EX='12:00:00'\t"
325 "%j='0'\t"
326 "%Q='12'\t"
327 "%q='h'\t"
328 "\n"),
329 lfmt,
330 12h);
331
332 check(SV("%H='23'\t"
333 "%OH='23'\t"
334 "%I='11'\t"
335 "%OI='11'\t"
336 "%M='59'\t"
337 "%OM='59'\t"
338 "%S='59'\t"
339 "%OS='59'\t"
340#if defined(_AIX)
341 "%p='PM'\t"
342#else
343 "%p=''\t"
344#endif
345 "%R='23:59'\t"
346 "%T='23:59:59'\t"
347#if defined(_AIX)
348 "%r='11:59:59 PM'\t"
349#elif defined(__APPLE__) || defined(__FreeBSD__)
350 "%r=''\t"
351#elif defined(_WIN32)
352 "%r='23:59:59'\t"
353#else
354 "%r='11:59:59 '\t"
355#endif
356 "%X='23:59:59'\t"
357 "%EX='23:59:59'\t"
358 "%j='0'\t"
359 "%Q='86399'\t"
360 "%q='s'\t"
361 "\n"),
362 lfmt,
363 23h + 59min + 59s);
364
365 check(SV("%H='00'\t"
366 "%OH='00'\t"
367 "%I='12'\t"
368 "%OI='12'\t"
369 "%M='00'\t"
370 "%OM='00'\t"
371 "%S='00'\t"
372 "%OS='00'\t"
373#if defined(_AIX)
374 "%p='AM'\t"
375#else
376 "%p=''\t"
377#endif
378 "%R='00:00'\t"
379 "%T='00:00:00'\t"
380#ifdef _WIN32
381 "%r='00:00:00'\t"
382#elif defined(_AIX)
383 "%r='12:00:00 AM'\t"
384#elif defined(__APPLE__) || defined(__FreeBSD__)
385 "%r=''\t"
386#elif defined(_WIN32)
387 "%r='12:00:00'\t"
388#else
389 "%r='12:00:00 '\t"
390#endif
391 "%X='00:00:00'\t"
392 "%EX='00:00:00'\t"
393 "%j='7'\t"
394 "%Q='7'\t"
395 "%q='d'\t"
396 "\n"),
397 lfmt,
398 std::chrono::duration<int, std::ratio<86400>>(7));
399
400 // Use supplied locale (ja_JP). This locale has a different alternate.
401#if defined(__APPLE__) || defined(_AIX) || defined(_WIN32) || defined(__FreeBSD__)
402 check(loc,
403 SV("%H='00'\t"
404 "%OH='00'\t"
405 "%I='12'\t"
406 "%OI='12'\t"
407 "%M='00'\t"
408 "%OM='00'\t"
409 "%S='00'\t"
410 "%OS='00'\t"
411# if defined(__APPLE__)
412 "%p='AM'\t"
413# else
414 "%p='午前'\t"
415# endif
416 "%R='00:00'\t"
417 "%T='00:00:00'\t"
418# if defined(__APPLE__) || defined(__FreeBSD__)
419# if defined(__APPLE__)
420 "%r='12:00:00 AM'\t"
421# else
422 "%r='12:00:00 午前'\t"
423# endif
424 "%X='00時00分00秒'\t"
425 "%EX='00時00分00秒'\t"
426# elif defined(_WIN32)
427 "%r='0:00:00'\t"
428 "%X='0:00:00'\t"
429 "%EX='0:00:00'\t"
430# else
431 "%r='午前12:00:00'\t"
432 "%X='00:00:00'\t"
433 "%EX='00:00:00'\t"
434# endif
435 "%j='0'\t"
436 "%Q='0'\t"
437 "%q='s'\t"
438 "\n"),
439 lfmt,
440 0s);
441
442 check(loc,
443 SV("%H='11'\t"
444 "%OH='11'\t"
445 "%I='11'\t"
446 "%OI='11'\t"
447 "%M='59'\t"
448 "%OM='59'\t"
449 "%S='59'\t"
450 "%OS='59'\t"
451# if defined(__APPLE__)
452 "%p='AM'\t"
453# else
454 "%p='午前'\t"
455# endif
456 "%R='11:59'\t"
457 "%T='11:59:59'\t"
458# if defined(__APPLE__) || defined(__FreeBSD__)
459# if defined(__APPLE__)
460 "%r='11:59:59 AM'\t"
461# else
462 "%r='11:59:59 午前'\t"
463# endif
464 "%X='11時59分59秒'\t"
465 "%EX='11時59分59秒'\t"
466# elif defined(_WIN32)
467 "%r='11:59:59'\t"
468 "%X='11:59:59'\t"
469 "%EX='11:59:59'\t"
470# else
471 "%r='午前11:59:59'\t"
472 "%X='11:59:59'\t"
473 "%EX='11:59:59'\t"
474# endif
475 "%j='0'\t"
476 "%Q='43199'\t"
477 "%q='s'\t"
478 "\n"),
479 lfmt,
480 11h + 59min + 59s);
481
482 check(loc,
483 SV("%H='12'\t"
484 "%OH='12'\t"
485 "%I='12'\t"
486 "%OI='12'\t"
487 "%M='00'\t"
488 "%OM='00'\t"
489 "%S='00'\t"
490 "%OS='00'\t"
491# if defined(__APPLE__)
492 "%p='PM'\t"
493# else
494 "%p='午後'\t"
495# endif
496 "%R='12:00'\t"
497 "%T='12:00:00'\t"
498# if defined(__APPLE__) || defined(__FreeBSD__)
499# if defined(__APPLE__)
500 "%r='12:00:00 PM'\t"
501# else
502 "%r='12:00:00 午後'\t"
503# endif
504 "%X='12時00分00秒'\t"
505 "%EX='12時00分00秒'\t"
506# else
507# ifdef _WIN32
508 "%r='12:00:00'\t"
509# else
510 "%r='午後12:00:00'\t"
511# endif
512 "%X='12:00:00'\t"
513 "%EX='12:00:00'\t"
514# endif
515 "%j='0'\t"
516 "%Q='12'\t"
517 "%q='h'\t"
518 "\n"),
519 lfmt,
520 12h);
521
522 check(loc,
523 SV("%H='23'\t"
524 "%OH='23'\t"
525 "%I='11'\t"
526 "%OI='11'\t"
527 "%M='59'\t"
528 "%OM='59'\t"
529 "%S='59'\t"
530 "%OS='59'\t"
531# if defined(__APPLE__)
532 "%p='PM'\t"
533# else
534 "%p='午後'\t"
535# endif
536 "%R='23:59'\t"
537 "%T='23:59:59'\t"
538# if defined(__APPLE__) || defined(__FreeBSD__)
539# if defined(__APPLE__)
540 "%r='11:59:59 PM'\t"
541# else
542 "%r='11:59:59 午後'\t"
543# endif
544 "%X='23時59分59秒'\t"
545 "%EX='23時59分59秒'\t"
546# else
547# ifdef _WIN32
548 "%r='23:59:59'\t"
549# else
550 "%r='午後11:59:59'\t"
551# endif
552 "%X='23:59:59'\t"
553 "%EX='23:59:59'\t"
554# endif
555 "%j='0'\t"
556 "%Q='86399'\t"
557 "%q='s'\t"
558 "\n"),
559 lfmt,
560 23h + 59min + 59s);
561
562 check(loc,
563 SV("%H='00'\t"
564 "%OH='00'\t"
565 "%I='12'\t"
566 "%OI='12'\t"
567 "%M='00'\t"
568 "%OM='00'\t"
569 "%S='00'\t"
570 "%OS='00'\t"
571# if defined(__APPLE__)
572 "%p='AM'\t"
573# else
574 "%p='午前'\t"
575# endif
576 "%R='00:00'\t"
577 "%T='00:00:00'\t"
578# if defined(__APPLE__) || defined(__FreeBSD__)
579# if defined(__APPLE__)
580 "%r='12:00:00 AM'\t"
581# else
582 "%r='12:00:00 午前'\t"
583# endif
584 "%X='00時00分00秒'\t"
585 "%EX='00時00分00秒'\t"
586# elif defined(_WIN32)
587 "%r='0:00:00'\t"
588 "%X='0:00:00'\t"
589 "%EX='0:00:00'\t"
590# else
591 "%r='午前12:00:00'\t"
592 "%X='00:00:00'\t"
593 "%EX='00:00:00'\t"
594# endif
595 "%j='7'\t"
596 "%Q='7'\t"
597 "%q='d'\t"
598 "\n"),
599 lfmt,
600 std::chrono::duration<int, std::ratio<86400>>(7));
601#else // defined(__APPLE__) || defined(_AIX) || defined(_WIN32)
602 check(loc,
603 SV("%H='00'\t"
604 "%OH='〇'\t"
605 "%I='12'\t"
606 "%OI='十二'\t"
607 "%M='00'\t"
608 "%OM='〇'\t"
609 "%S='00'\t"
610 "%OS='〇'\t"
611 "%p='午前'\t"
612 "%R='00:00'\t"
613 "%T='00:00:00'\t"
614 "%r='午前12時00分00秒'\t"
615 "%X='00時00分00秒'\t"
616 "%EX='00時00分00秒'\t"
617 "%j='0'\t"
618 "%Q='0'\t"
619 "%q='s'\t"
620 "\n"),
621 lfmt,
622 0s);
623
624 check(loc,
625 SV("%H='11'\t"
626 "%OH='十一'\t"
627 "%I='11'\t"
628 "%OI='十一'\t"
629 "%M='59'\t"
630 "%OM='五十九'\t"
631 "%S='59'\t"
632 "%OS='五十九'\t"
633 "%p='午前'\t"
634 "%R='11:59'\t"
635 "%T='11:59:59'\t"
636 "%r='午前11時59分59秒'\t"
637 "%X='11時59分59秒'\t"
638 "%EX='11時59分59秒'\t"
639 "%j='0'\t"
640 "%Q='43199'\t"
641 "%q='s'\t"
642 "\n"),
643 lfmt,
644 11h + 59min + 59s);
645
646 check(loc,
647 SV("%H='12'\t"
648 "%OH='十二'\t"
649 "%I='12'\t"
650 "%OI='十二'\t"
651 "%M='00'\t"
652 "%OM='〇'\t"
653 "%S='00'\t"
654 "%OS='〇'\t"
655 "%p='午後'\t"
656 "%R='12:00'\t"
657 "%T='12:00:00'\t"
658 "%r='午後12時00分00秒'\t"
659 "%X='12時00分00秒'\t"
660 "%EX='12時00分00秒'\t"
661 "%j='0'\t"
662 "%Q='12'\t"
663 "%q='h'\t"
664 "\n"),
665 lfmt,
666 12h);
667
668 check(loc,
669 SV("%H='23'\t"
670 "%OH='二十三'\t"
671 "%I='11'\t"
672 "%OI='十一'\t"
673 "%M='59'\t"
674 "%OM='五十九'\t"
675 "%S='59'\t"
676 "%OS='五十九'\t"
677 "%p='午後'\t"
678 "%R='23:59'\t"
679 "%T='23:59:59'\t"
680 "%r='午後11時59分59秒'\t"
681 "%X='23時59分59秒'\t"
682 "%EX='23時59分59秒'\t"
683 "%j='0'\t"
684 "%Q='86399'\t"
685 "%q='s'\t"
686 "\n"),
687 lfmt,
688 23h + 59min + 59s);
689
690 check(loc,
691 SV("%H='00'\t"
692 "%OH='〇'\t"
693 "%I='12'\t"
694 "%OI='十二'\t"
695 "%M='00'\t"
696 "%OM='〇'\t"
697 "%S='00'\t"
698 "%OS='〇'\t"
699 "%p='午前'\t"
700 "%R='00:00'\t"
701 "%T='00:00:00'\t"
702 "%r='午前12時00分00秒'\t"
703 "%X='00時00分00秒'\t"
704 "%EX='00時00分00秒'\t"
705 "%j='7'\t"
706 "%Q='7'\t"
707 "%q='d'\t"
708 "\n"),
709 lfmt,
710 std::chrono::duration<int, std::ratio<86400>>(7));
711#endif // defined(__APPLE__) || defined(_AIX) || defined(_WIN32)
712
713 std::locale::global(loc: std::locale::classic());
714}
715
716template <class CharT>
717static void test_valid_negative_integral_values() {
718 // [time.format]/4 The result of formatting a std::chrono::duration instance
719 // holding a negative value, or an hh_mm_ss object h for which
720 // h.is_negative() is true, is equivalent to the output of the corresponding
721 // positive value, with a STATICALLY-WIDEN<charT>("-") character sequence
722 // placed before the replacement of the initial conversion specifier.
723 //
724 // Note in this case %% is the initial conversion specifier.
725 using namespace std::literals::chrono_literals;
726
727 constexpr std::basic_string_view<CharT> fmt = SV(
728 "{:"
729 "%%H='%H'%t"
730 "%%OH='%OH'%t"
731 "%%I='%I'%t"
732 "%%OI='%OI'%t"
733 "%%M='%M'%t"
734 "%%OM='%OM'%t"
735 "%%S='%S'%t"
736 "%%OS='%OS'%t"
737 "%%p='%p'%t"
738 "%%R='%R'%t"
739 "%%T='%T'%t"
740 "%%r='%r'%t"
741 "%%X='%X'%t"
742 "%%EX='%EX'%t"
743 "%%j='%j'%t"
744 "%%Q='%Q'%t"
745 "%%q='%q'%t"
746 "%n}");
747 constexpr std::basic_string_view<CharT> lfmt = SV(
748 "{:L"
749 "%%H='%H'%t"
750 "%%OH='%OH'%t"
751 "%%I='%I'%t"
752 "%%OI='%OI'%t"
753 "%%M='%M'%t"
754 "%%OM='%OM'%t"
755 "%%S='%S'%t"
756 "%%OS='%OS'%t"
757 "%%p='%p'%t"
758 "%%R='%R'%t"
759 "%%T='%T'%t"
760 "%%r='%r'%t"
761 "%%X='%X'%t"
762 "%%EX='%EX'%t"
763 "%%j='%j'%t"
764 "%%Q='%Q'%t"
765 "%%q='%q'%t"
766 "%n}");
767
768 const std::locale loc(LOCALE_ja_JP_UTF_8);
769 std::locale::global(std::locale(LOCALE_fr_FR_UTF_8));
770
771 // Non localized output using C-locale
772 check(SV("-%H='23'\t"
773 "%OH='23'\t"
774 "%I='11'\t"
775 "%OI='11'\t"
776 "%M='59'\t"
777 "%OM='59'\t"
778 "%S='59'\t"
779 "%OS='59'\t"
780 "%p='PM'\t"
781 "%R='23:59'\t"
782 "%T='23:59:59'\t"
783 "%r='11:59:59 PM'\t"
784 "%X='23:59:59'\t"
785 "%EX='23:59:59'\t"
786 "%j='0'\t"
787 "%Q='86399'\t"
788 "%q='s'\t"
789 "\n"),
790 fmt,
791 -(23h + 59min + 59s));
792
793 // Use the global locale (fr_FR)
794 check(SV("-%H='23'\t"
795 "%OH='23'\t"
796 "%I='11'\t"
797 "%OI='11'\t"
798 "%M='59'\t"
799 "%OM='59'\t"
800 "%S='59'\t"
801 "%OS='59'\t"
802#if defined(_AIX)
803 "%p='PM'\t"
804#else
805 "%p=''\t"
806#endif
807 "%R='23:59'\t"
808 "%T='23:59:59'\t"
809#if defined(_AIX)
810 "%r='11:59:59 PM'\t"
811#elif defined(__APPLE__) || defined(__FreeBSD__)
812 "%r=''\t"
813#elif defined(_WIN32)
814 "%r='23:59:59'\t"
815#else
816 "%r='11:59:59 '\t"
817#endif
818 "%X='23:59:59'\t"
819 "%EX='23:59:59'\t"
820 "%j='0'\t"
821 "%Q='86399'\t"
822 "%q='s'\t"
823 "\n"),
824 lfmt,
825 -(23h + 59min + 59s));
826
827 // Use supplied locale (ja_JP). This locale has a different alternate.
828#if defined(__APPLE__) || defined(_AIX) || defined(_WIN32) || defined(__FreeBSD__)
829 check(loc,
830 SV("-%H='23'\t"
831 "%OH='23'\t"
832 "%I='11'\t"
833 "%OI='11'\t"
834 "%M='59'\t"
835 "%OM='59'\t"
836 "%S='59'\t"
837 "%OS='59'\t"
838# if defined(__APPLE__)
839 "%p='PM'\t"
840# else
841 "%p='午後'\t"
842# endif
843 "%R='23:59'\t"
844 "%T='23:59:59'\t"
845# if defined(__APPLE__) || defined(__FreeBSD__)
846# if defined(__APPLE__)
847 "%r='11:59:59 PM'\t"
848# else
849 "%r='11:59:59 午後'\t"
850# endif
851 "%X='23時59分59秒'\t"
852 "%EX='23時59分59秒'\t"
853# elif defined(_WIN32)
854 "%r='23:59:59'\t"
855 "%X='23:59:59'\t"
856 "%EX='23:59:59'\t"
857# else
858 "%r='午後11:59:59'\t"
859 "%X='23:59:59'\t"
860 "%EX='23:59:59'\t"
861# endif
862 "%j='0'\t"
863 "%Q='86399'\t"
864 "%q='s'\t"
865 "\n"),
866 lfmt,
867 -(23h + 59min + 59s));
868#else // defined(__APPLE__) || defined(_AIX) || defined(_WIN32)|| defined(__FreeBSD__)
869 check(loc,
870 SV("-%H='23'\t"
871 "%OH='二十三'\t"
872 "%I='11'\t"
873 "%OI='十一'\t"
874 "%M='59'\t"
875 "%OM='五十九'\t"
876 "%S='59'\t"
877 "%OS='五十九'\t"
878 "%p='午後'\t"
879 "%R='23:59'\t"
880 "%T='23:59:59'\t"
881 "%r='午後11時59分59秒'\t"
882 "%X='23時59分59秒'\t"
883 "%EX='23時59分59秒'\t"
884 "%j='0'\t"
885 "%Q='86399'\t"
886 "%q='s'\t"
887 "\n"),
888 lfmt,
889 -(23h + 59min + 59s));
890#endif // defined(__APPLE__) || defined(_AIX) || defined(_WIN32)|| defined(__FreeBSD__)
891 std::locale::global(loc: std::locale::classic());
892}
893
894template <class CharT>
895static void test_valid_fractional_values() {
896 using namespace std::literals::chrono_literals;
897
898 const std::locale loc(LOCALE_ja_JP_UTF_8);
899 std::locale::global(std::locale(LOCALE_fr_FR_UTF_8));
900
901 // Non localized output using C-locale
902 check(SV("00.000000001"), SV("{:%S}"), 1ns);
903 check(SV("00.000000501"), SV("{:%S}"), 501ns);
904 check(SV("00.000001000"), SV("{:%S}"), 1000ns);
905 check(SV("00.000000000001"), SV("{:%S}"), std::chrono::duration<int, std::pico>(1));
906 check(SV("00.000000000000001"), SV("{:%S}"), std::chrono::duration<int, std::femto>(1));
907 check(SV("00.000000000000000001"), SV("{:%S}"), std::chrono::duration<int, std::atto>(1));
908
909 check(SV("00.001"), SV("{:%S}"), 1ms);
910 check(SV("00.01"), SV("{:%S}"), std::chrono::duration<int, std::centi>(1));
911 check(SV("00.1"), SV("{:%S}"), std::chrono::duration<int, std::deci>(1));
912 check(SV("01.1"), SV("{:%S}"), std::chrono::duration<int, std::deci>(11));
913
914 check(SV("00.001"), SV("{:%S}"), std::chrono::duration<float, std::milli>(1.123456789));
915 check(SV("00.011"), SV("{:%S}"), std::chrono::duration<double, std::milli>(11.123456789));
916 check(SV("01"), SV("{:%S}"), std::chrono::duration<long double>(61.123456789));
917
918 check(SV("00.000000001"), SV("{:%OS}"), 1ns);
919 check(SV("00.000000501"), SV("{:%OS}"), 501ns);
920 check(SV("00.000001000"), SV("{:%OS}"), 1000ns);
921 check(SV("00.000000000001"), SV("{:%OS}"), std::chrono::duration<int, std::pico>(1));
922 check(SV("00.000000000000001"), SV("{:%OS}"), std::chrono::duration<int, std::femto>(1));
923 check(SV("00.000000000000000001"), SV("{:%OS}"), std::chrono::duration<int, std::atto>(1));
924
925 check(SV("00.001"), SV("{:%OS}"), 1ms);
926 check(SV("00.01"), SV("{:%OS}"), std::chrono::duration<int, std::centi>(1));
927 check(SV("00.1"), SV("{:%OS}"), std::chrono::duration<int, std::deci>(1));
928 check(SV("01.1"), SV("{:%OS}"), std::chrono::duration<int, std::deci>(11));
929
930 check(SV("00.001"), SV("{:%OS}"), std::chrono::duration<float, std::milli>(1.123456789));
931 check(SV("00.011"), SV("{:%OS}"), std::chrono::duration<double, std::milli>(11.123456789));
932 check(SV("01"), SV("{:%OS}"), std::chrono::duration<long double>(61.123456789));
933
934 check(SV("01:05:06.000000001"), SV("{:%T}"), 1h + 5min + 6s + 1ns);
935 check(SV("01:05:06.000000501"), SV("{:%T}"), 1h + 5min + 6s + 501ns);
936 check(SV("01:05:06.000001000"), SV("{:%T}"), 1h + 5min + 6s + 1000ns);
937
938 check(SV("01:05:06.001"), SV("{:%T}"), 1h + 5min + 6s + 1ms);
939 check(SV("01:05:06.01"), SV("{:%T}"), 1h + 5min + 6s + std::chrono::duration<int, std::centi>(1));
940 check(SV("01:05:06.1"), SV("{:%T}"), 1h + 5min + 6s + std::chrono::duration<int, std::deci>(1));
941 check(SV("01:05:07.1"), SV("{:%T}"), 1h + 5min + 6s + std::chrono::duration<int, std::deci>(11));
942
943 check(
944 SV("00:01:02"), SV("{:%T}"), std::chrono::duration<float, std::ratio<60>>(1) + std::chrono::duration<float>(2.5));
945 check(SV("01:05:11"),
946 SV("{:%T}"),
947 std::chrono::duration<double, std::ratio<3600>>(1) + std::chrono::duration<double, std::ratio<60>>(5) +
948 std::chrono::duration<double>(11.123456789));
949 check(SV("01:06:01"),
950 SV("{:%T}"),
951 std::chrono::duration<long double, std::ratio<3600>>(1) +
952 std::chrono::duration<long double, std::ratio<60>>(5) + std::chrono::duration<long double>(61.123456789));
953
954 check(SV("0"), SV("{:%j}"), std::chrono::duration<float, std::milli>(1.));
955 check(SV("1"), SV("{:%j}"), std::chrono::duration<double, std::milli>(86'400'000));
956 check(SV("1"), SV("{:%j}"), std::chrono::duration<long double, std::ratio<7 * 24 * 3600>>(0.14285714286));
957
958 check(SV("1000000"), SV("{:%Q}"), 1'000'000s);
959 check(SV("1"), SV("{:%Q}"), std::chrono::duration<float, std::milli>(1.));
960 check(SV("1.123456789"), SV("{:.6%Q}"), std::chrono::duration<double, std::milli>(1.123456789));
961 check(SV("1.123456789"), SV("{:.9%Q}"), std::chrono::duration<long double, std::milli>(1.123456789));
962
963 // Use the global locale (fr_FR)
964 check(SV("00,000000001"), SV("{:L%S}"), 1ns);
965 check(SV("00,000000501"), SV("{:L%S}"), 501ns);
966 check(SV("00,000001000"), SV("{:L%S}"), 1000ns);
967 check(SV("00,000000000001"), SV("{:L%S}"), std::chrono::duration<int, std::pico>(1));
968 check(SV("00,000000000000001"), SV("{:L%S}"), std::chrono::duration<int, std::femto>(1));
969 check(SV("00,000000000000000001"), SV("{:L%S}"), std::chrono::duration<int, std::atto>(1));
970
971 check(SV("00,001"), SV("{:L%S}"), 1ms);
972 check(SV("00,01"), SV("{:L%S}"), std::chrono::duration<int, std::centi>(1));
973 check(SV("00,1"), SV("{:L%S}"), std::chrono::duration<int, std::deci>(1));
974 check(SV("01,1"), SV("{:L%S}"), std::chrono::duration<int, std::deci>(11));
975
976 check(SV("00,001"), SV("{:L%S}"), std::chrono::duration<float, std::milli>(1.123456789));
977 check(SV("00,011"), SV("{:L%S}"), std::chrono::duration<double, std::milli>(11.123456789));
978 check(SV("01"), SV("{:L%S}"), std::chrono::duration<long double>(61.123456789));
979
980 check(SV("00,000000001"), SV("{:L%OS}"), 1ns);
981 check(SV("00,000000501"), SV("{:L%OS}"), 501ns);
982 check(SV("00,000001000"), SV("{:L%OS}"), 1000ns);
983 check(SV("00,000000000001"), SV("{:L%OS}"), std::chrono::duration<int, std::pico>(1));
984 check(SV("00,000000000000001"), SV("{:L%OS}"), std::chrono::duration<int, std::femto>(1));
985 check(SV("00,000000000000000001"), SV("{:L%OS}"), std::chrono::duration<int, std::atto>(1));
986
987 check(SV("00,001"), SV("{:L%OS}"), 1ms);
988 check(SV("00,01"), SV("{:L%OS}"), std::chrono::duration<int, std::centi>(1));
989 check(SV("00,1"), SV("{:L%OS}"), std::chrono::duration<int, std::deci>(1));
990 check(SV("01,1"), SV("{:L%OS}"), std::chrono::duration<int, std::deci>(11));
991
992 check(SV("00,001"), SV("{:L%OS}"), std::chrono::duration<float, std::milli>(1.123456789));
993 check(SV("00,011"), SV("{:L%OS}"), std::chrono::duration<double, std::milli>(11.123456789));
994 check(SV("01"), SV("{:L%OS}"), std::chrono::duration<long double>(61.123456789));
995
996 check(SV("01:05:06,000000001"), SV("{:L%T}"), 1h + 5min + 6s + 1ns);
997 check(SV("01:05:06,000000501"), SV("{:L%T}"), 1h + 5min + 6s + 501ns);
998 check(SV("01:05:06,000001000"), SV("{:L%T}"), 1h + 5min + 6s + 1000ns);
999
1000 check(SV("01:05:06,001"), SV("{:L%T}"), 1h + 5min + 6s + 1ms);
1001 check(SV("01:05:06,01"), SV("{:L%T}"), 1h + 5min + 6s + std::chrono::duration<int, std::centi>(1));
1002 check(SV("01:05:06,1"), SV("{:L%T}"), 1h + 5min + 6s + std::chrono::duration<int, std::deci>(1));
1003 check(SV("01:05:07,1"), SV("{:L%T}"), 1h + 5min + 6s + std::chrono::duration<int, std::deci>(11));
1004
1005 check(SV("00:01:02"),
1006 SV("{:L%T}"),
1007 std::chrono::duration<float, std::ratio<60>>(1) + std::chrono::duration<float>(2.5));
1008 check(SV("01:05:11"),
1009 SV("{:L%T}"),
1010 std::chrono::duration<double, std::ratio<3600>>(1) + std::chrono::duration<double, std::ratio<60>>(5) +
1011 std::chrono::duration<double>(11.123456789));
1012 check(SV("01:06:01"),
1013 SV("{:L%T}"),
1014 std::chrono::duration<long double, std::ratio<3600>>(1) +
1015 std::chrono::duration<long double, std::ratio<60>>(5) + std::chrono::duration<long double>(61.123456789));
1016
1017 check(SV("0"), SV("{:L%j}"), std::chrono::duration<float, std::milli>(1.));
1018 check(SV("1"), SV("{:L%j}"), std::chrono::duration<double, std::milli>(86'400'000));
1019 check(SV("1"), SV("{:L%j}"), std::chrono::duration<long double, std::ratio<7 * 24 * 3600>>(0.14285714286));
1020
1021 check(SV("1000000"), SV("{:L%Q}"), 1'000'000s); // The Standard mandates not localized.
1022 check(SV("1"), SV("{:L%Q}"), std::chrono::duration<float, std::milli>(1.));
1023 check(SV("1.123456789"), SV("{:.6L%Q}"), std::chrono::duration<double, std::milli>(1.123456789));
1024 check(SV("1.123456789"), SV("{:.9L%Q}"), std::chrono::duration<long double, std::milli>(1.123456789));
1025
1026 // Use supplied locale (ja_JP). This locale has a different alternate.
1027 check(loc, SV("00.000000001"), SV("{:L%S}"), 1ns);
1028 check(loc, SV("00.000000501"), SV("{:L%S}"), 501ns);
1029 check(loc, SV("00.000001000"), SV("{:L%S}"), 1000ns);
1030 check(loc, SV("00.000000000001"), SV("{:L%S}"), std::chrono::duration<int, std::pico>(1));
1031 check(loc, SV("00.000000000000001"), SV("{:L%S}"), std::chrono::duration<int, std::femto>(1));
1032 check(loc, SV("00.000000000000000001"), SV("{:L%S}"), std::chrono::duration<int, std::atto>(1));
1033
1034 check(loc, SV("00.001"), SV("{:L%S}"), 1ms);
1035 check(loc, SV("00.01"), SV("{:L%S}"), std::chrono::duration<int, std::centi>(1));
1036 check(loc, SV("00.1"), SV("{:L%S}"), std::chrono::duration<int, std::deci>(1));
1037 check(loc, SV("01.1"), SV("{:L%S}"), std::chrono::duration<int, std::deci>(11));
1038
1039 check(loc, SV("00.001"), SV("{:L%S}"), std::chrono::duration<float, std::milli>(1.123456789));
1040 check(loc, SV("00.011"), SV("{:L%S}"), std::chrono::duration<double, std::milli>(11.123456789));
1041 check(loc, SV("01"), SV("{:L%S}"), std::chrono::duration<long double>(61.123456789));
1042
1043#if defined(__APPLE__) || defined(_AIX) || defined(_WIN32) || defined(__FreeBSD__)
1044
1045 check(SV("00.000000001"), SV("{:%OS}"), 1ns);
1046 check(SV("00.000000501"), SV("{:%OS}"), 501ns);
1047 check(SV("00.000001000"), SV("{:%OS}"), 1000ns);
1048 check(SV("00.000000000001"), SV("{:%OS}"), std::chrono::duration<int, std::pico>(1));
1049 check(SV("00.000000000000001"), SV("{:%OS}"), std::chrono::duration<int, std::femto>(1));
1050 check(SV("00.000000000000000001"), SV("{:%OS}"), std::chrono::duration<int, std::atto>(1));
1051
1052 check(SV("00.001"), SV("{:%OS}"), 1ms);
1053 check(SV("00.01"), SV("{:%OS}"), std::chrono::duration<int, std::centi>(1));
1054 check(SV("00.1"), SV("{:%OS}"), std::chrono::duration<int, std::deci>(1));
1055 check(SV("01.1"), SV("{:%OS}"), std::chrono::duration<int, std::deci>(11));
1056
1057 check(SV("00.001"), SV("{:%OS}"), std::chrono::duration<float, std::milli>(1.123456789));
1058 check(SV("00.011"), SV("{:%OS}"), std::chrono::duration<double, std::milli>(11.123456789));
1059 check(SV("01"), SV("{:%OS}"), std::chrono::duration<long double>(61.123456789));
1060#else // defined(__APPLE__) || defined(_AIX) || defined(_WIN32)|| defined(__FreeBSD__)
1061
1062 check(loc, SV("〇.000000001"), SV("{:L%OS}"), 1ns);
1063 check(loc, SV("〇.000000501"), SV("{:L%OS}"), 501ns);
1064 check(loc, SV("〇.000001000"), SV("{:L%OS}"), 1000ns);
1065 check(loc, SV("〇.000000000001"), SV("{:L%OS}"), std::chrono::duration<int, std::pico>(1));
1066 check(loc, SV("〇.000000000000001"), SV("{:L%OS}"), std::chrono::duration<int, std::femto>(1));
1067 check(loc, SV("〇.000000000000000001"), SV("{:L%OS}"), std::chrono::duration<int, std::atto>(1));
1068
1069 check(loc, SV("〇.001"), SV("{:L%OS}"), 1ms);
1070 check(loc, SV("〇.01"), SV("{:L%OS}"), std::chrono::duration<int, std::centi>(1));
1071 check(loc, SV("〇.1"), SV("{:L%OS}"), std::chrono::duration<int, std::deci>(1));
1072 check(loc, SV("一.1"), SV("{:L%OS}"), std::chrono::duration<int, std::deci>(11));
1073
1074 check(loc, SV("〇.001"), SV("{:L%OS}"), std::chrono::duration<float, std::milli>(1.123456789));
1075 check(loc, SV("〇.011"), SV("{:L%OS}"), std::chrono::duration<double, std::milli>(11.123456789));
1076 check(loc, SV("一"), SV("{:L%OS}"), std::chrono::duration<long double>(61.123456789));
1077#endif // defined(__APPLE__) || defined(_AIX) || defined(_WIN32)|| defined(__FreeBSD__)
1078
1079 check(loc, SV("01:05:06.000000001"), SV("{:L%T}"), 1h + 5min + 6s + 1ns);
1080 check(loc, SV("01:05:06.000000501"), SV("{:L%T}"), 1h + 5min + 6s + 501ns);
1081 check(loc, SV("01:05:06.000001000"), SV("{:L%T}"), 1h + 5min + 6s + 1000ns);
1082
1083 check(loc, SV("01:05:06.001"), SV("{:L%T}"), 1h + 5min + 6s + 1ms);
1084 check(loc, SV("01:05:06.01"), SV("{:L%T}"), 1h + 5min + 6s + std::chrono::duration<int, std::centi>(1));
1085 check(loc, SV("01:05:06.1"), SV("{:L%T}"), 1h + 5min + 6s + std::chrono::duration<int, std::deci>(1));
1086 check(loc, SV("01:05:07.1"), SV("{:L%T}"), 1h + 5min + 6s + std::chrono::duration<int, std::deci>(11));
1087
1088 check(loc,
1089 SV("00:01:02"),
1090 SV("{:L%T}"),
1091 std::chrono::duration<float, std::ratio<60>>(1) + std::chrono::duration<float>(2.5));
1092 check(loc,
1093 SV("01:05:11"),
1094 SV("{:L%T}"),
1095 std::chrono::duration<double, std::ratio<3600>>(1) + std::chrono::duration<double, std::ratio<60>>(5) +
1096 std::chrono::duration<double>(11.123456789));
1097 check(loc,
1098 SV("01:06:01"),
1099 SV("{:L%T}"),
1100 std::chrono::duration<long double, std::ratio<3600>>(1) +
1101 std::chrono::duration<long double, std::ratio<60>>(5) + std::chrono::duration<long double>(61.123456789));
1102
1103 check(loc, SV("0"), SV("{:L%j}"), std::chrono::duration<float, std::milli>(1.));
1104 check(loc, SV("1"), SV("{:L%j}"), std::chrono::duration<double, std::milli>(86'400'000));
1105 check(loc, SV("1"), SV("{:L%j}"), std::chrono::duration<long double, std::ratio<7 * 24 * 3600>>(0.14285714286));
1106
1107 check(loc, SV("1000000"), SV("{:L%Q}"), 1'000'000s); // The Standard mandates not localized.
1108 check(loc, SV("1"), SV("{:L%Q}"), std::chrono::duration<float, std::milli>(1.));
1109 check(loc, SV("1.123456789"), SV("{:.6L%Q}"), std::chrono::duration<double, std::milli>(1.123456789));
1110 check(loc, SV("1.123456789"), SV("{:.9L%Q}"), std::chrono::duration<long double, std::milli>(1.123456789));
1111
1112 std::locale::global(loc: std::locale::classic());
1113}
1114
1115template <class CharT>
1116static void test_valid_values() {
1117 test_valid_positive_integral_values<CharT>();
1118 test_valid_negative_integral_values<CharT>();
1119 test_valid_fractional_values<CharT>();
1120}
1121
1122template <class CharT>
1123static void test_pr62082() {
1124 // Examples in https://llvm.org/PR62082
1125 check(SV("39.223300"), SV("{:%S}"), std::chrono::duration<int, std::ratio<101, 103>>{40});
1126 check(SV("01.4755859375"), SV("{:%S}"), std::chrono::duration<int, std::ratio<1, 1024>>{1511});
1127
1128 // Test with all possible number of decimals [0, 18]. When it does not
1129 // fit in 18 decimals it uses 6.
1130 check(SV("05"), SV("{:%S}"), std::chrono::duration<float, std::ratio<1, 1>>{5}); // 0
1131
1132 check(SV("05.0"), SV("{:%S}"), std::chrono::duration<float, std::ratio<1, 2>>{10}); // 1
1133 check(SV("05.5"), SV("{:%S}"), std::chrono::duration<int, std::ratio<1, 2>>{11}); // 1
1134
1135 check(SV("01.00"), SV("{:%S}"), std::chrono::duration<int, std::ratio<1, 4>>{4}); // 2
1136 check(SV("01.50"), SV("{:%S}"), std::chrono::duration<double, std::ratio<1, 4>>{6}); // 2
1137 check(SV("01.75"), SV("{:%S}"), std::chrono::duration<long double, std::ratio<1, 4>>{7}); // 2
1138
1139 check(SV("01.000"), SV("{:%S}"), std::chrono::duration<int, std::ratio<1, 8>>{8}); // 3
1140 check(SV("01.0000"), SV("{:%S}"), std::chrono::duration<int, std::ratio<1, 16>>{16}); // 4
1141 check(SV("01.00000"), SV("{:%S}"), std::chrono::duration<int, std::ratio<1, 32>>{32}); // 5
1142 check(SV("01.000000"), SV("{:%S}"), std::chrono::duration<int, std::ratio<1, 64>>{64}); // 6
1143 check(SV("01.0000000"), SV("{:%S}"), std::chrono::duration<int, std::ratio<1, 128>>{128}); // 7
1144 check(SV("01.00000000"), SV("{:%S}"), std::chrono::duration<int, std::ratio<1, 256>>{256}); // 8
1145 check(SV("01.000000000"), SV("{:%S}"), std::chrono::duration<int, std::ratio<1, 512>>{512}); // 9
1146 check(SV("01.0000000000"), SV("{:%S}"), std::chrono::duration<int, std::ratio<1, 1024>>{1024}); // 10
1147 check(SV("01.00000000000"), SV("{:%S}"), std::chrono::duration<int, std::ratio<1, 2048>>{2048}); // 11
1148 check(SV("01.000000000000"), SV("{:%S}"), std::chrono::duration<int, std::ratio<1, 4096>>{4096}); // 12
1149 check(SV("01.0000000000000"), SV("{:%S}"), std::chrono::duration<int, std::ratio<1, 8192>>{8192}); // 13
1150 check(SV("01.00000000000000"), SV("{:%S}"), std::chrono::duration<int, std::ratio<1, 16384>>{16384}); // 14
1151 check(SV("01.000000000000000"), SV("{:%S}"), std::chrono::duration<int, std::ratio<1, 32768>>{32768}); // 15
1152 check(SV("01.0000000000000000"), SV("{:%S}"), std::chrono::duration<int, std::ratio<1, 65536>>{65536}); // 16
1153 check(SV("01.00000000000000000"), SV("{:%S}"), std::chrono::duration<int, std::ratio<1, 131072>>{131072}); // 17
1154 check(SV("01.000000000000000000"), SV("{:%S}"), std::chrono::duration<int, std::ratio<1, 262144>>{262144}); // 18
1155 check(SV("01.000000"), SV("{:%S}"), std::chrono::duration<int, std::ratio<1, 524288>>{524288}); // 19 -> 6
1156
1157 // Infinite number of decimals will use 6 decimals.
1158 check(SV("00.111111"), SV("{:%S}"), std::chrono::duration<int, std::ratio<1, 9>>{1});
1159 check(SV("00.111111"), SV("{:%S}"), std::chrono::duration<float, std::ratio<1, 9>>{1});
1160 check(SV("00.111111"), SV("{:%S}"), std::chrono::duration<double, std::ratio<1, 9>>{1});
1161 check(SV("00.111111"), SV("{:%S}"), std::chrono::duration<long double, std::ratio<1, 9>>{1});
1162}
1163
1164template <class CharT>
1165static void test_unsigned_duration() {
1166 // Reported in https://github.com/llvm/llvm-project/issues/96820
1167 using namespace std::literals::chrono_literals;
1168
1169 check(SV("1as"), SV("{}"), std::chrono::duration<unsigned short, std::atto>(1));
1170 check(SV("1fs"), SV("{}"), std::chrono::duration<unsigned, std::femto>(1));
1171 check(SV("1ps"), SV("{}"), std::chrono::duration<unsigned long, std::pico>(1));
1172 check(SV("1ns"), SV("{}"), std::chrono::duration<unsigned long long, std::nano>(1));
1173}
1174
1175template <class CharT>
1176static void test() {
1177 using namespace std::literals::chrono_literals;
1178
1179 test_no_chrono_specs<CharT>();
1180 test_valid_values<CharT>();
1181 test_unsigned_duration<CharT>();
1182 test_pr62082<CharT>();
1183
1184 check_invalid_types<CharT>(
1185 {SV("H"), SV("I"), SV("j"), SV("M"), SV("n"), SV("O"), SV("p"), SV("q"), SV("Q"), SV("r"),
1186 SV("R"), SV("S"), SV("t"), SV("T"), SV("X"), SV("EX"), SV("OH"), SV("OI"), SV("OM"), SV("OS")},
1187 0ms);
1188
1189 check_exception("The format specifier expects a '%' or a '}'", SV("{:A"), 0ms);
1190 check_exception("The chrono specifiers contain a '{'", SV("{:%%{"), 0ms);
1191 check_exception("End of input while parsing a conversion specifier", SV("{:%"), 0ms);
1192 check_exception("End of input while parsing the modifier E", SV("{:%E"), 0ms);
1193 check_exception("End of input while parsing the modifier O", SV("{:%O"), 0ms);
1194
1195 // Make sure the required values work, based on their minimum number of required bits per [time.syn].
1196 check(SV("23:47:16.854775807"),
1197 SV("{:%T}"),
1198 std::chrono::nanoseconds{0x7fff'ffff'ffff'ffffll}); // 64 bit signed value max
1199 check(SV("23:35:09.481983"),
1200 SV("{:%T}"),
1201 std::chrono::microseconds{0x003f'ffff'ffff'ffffll}); // 55 bit signed value max
1202 check(SV("06:20:44.415"), SV("{:%T}"), std::chrono::milliseconds{0x0000'fff'ffff'ffffll}); // 45 bit signed value max
1203 check(SV("01:53:03"), SV("{:%T}"), std::chrono::seconds{0x0000'0003'ffff'ffffll}); // 35 bit signed value max
1204 check(SV("12:15:00"), SV("{:%T}"), std::chrono::minutes{0x0fff'ffff}); // 29 bit signed value max
1205 check(SV("15:00:00"), SV("{:%T}"), std::chrono::hours{0x003f'ffff}); // 23 bit signed value max
1206 check(SV("00:00:00"), SV("{:%T}"), std::chrono::days{0x0ff'ffff}); // 25 bit signed value max
1207 check(SV("00:00:00"), SV("{:%T}"), std::chrono::weeks{0x003f'ffff}); // 22 bit signed value max
1208 check(SV("21:11:42"), SV("{:%T}"), std::chrono::months{0x0007'ffff}); // 20 bit signed value max
1209 check(SV("05:42:00"), SV("{:%T}"), std::chrono::years{0xffff}); // 17 bit signed value max
1210
1211 // Precision not allowed
1212 check_exception("The format specifier expects a '%' or a '}'", SV("{:.3}"), 0ms);
1213}
1214
1215int main(int, char**) {
1216 test<char>();
1217
1218#ifndef TEST_HAS_NO_WIDE_CHARACTERS
1219 test<wchar_t>();
1220#endif
1221
1222 return 0;
1223}
1224

source code of libcxx/test/std/time/time.syn/formatter.duration.pass.cpp