Debian Codegraph @

libcxx/test/std/time/time.syn/formatter.duration.pass.cpp

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