Debian Codegraph @

libcxx/test/std/time/time.syn/formatter.hh_mm_ss.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::hh_mm_ss<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 "string_literal.h"
36 #include "test_macros.h"
37
38 template <class CharT>
39 static void test_no_chrono_specs() {
40 using namespace std::literals::chrono_literals;
41
42 std::locale::global(std::locale(LOCALE_fr_FR_UTF_8));
43
44 // Non localized output
45 check(SV("00:00:00.000"), SV("{}"), std::chrono::hh_mm_ss{0ms});
46 check(SV("*00:00:00.000*"), SV("{:*^14}"), std::chrono::hh_mm_ss{0ms});
47 check(SV("*00:00:00.000"), SV("{:*>13}"), std::chrono::hh_mm_ss{0ms});
48
49 std::locale::global(std::locale::classic());
50 }
51
52 template <class CharT>
53 static void test_valid_values() {
54 using namespace std::literals::chrono_literals;
55
56 constexpr std::basic_string_view<CharT> fmt = SV(
57 "{:"
58 "%%H='%H'%t"
59 "%%OH='%OH'%t"
60 "%%I='%I'%t"
61 "%%OI='%OI'%t"
62 "%%M='%M'%t"
63 "%%OM='%OM'%t"
64 "%%S='%S'%t"
65 "%%OS='%OS'%t"
66 "%%p='%p'%t"
67 "%%R='%R'%t"
68 "%%T='%T'%t"
69 "%%r='%r'%t"
70 "%%X='%X'%t"
71 "%%EX='%EX'%t"
72 "%n}");
73 constexpr std::basic_string_view<CharT> lfmt = SV(
74 "{:L"
75 "%%H='%H'%t"
76 "%%OH='%OH'%t"
77 "%%I='%I'%t"
78 "%%OI='%OI'%t"
79 "%%M='%M'%t"
80 "%%OM='%OM'%t"
81 "%%S='%S'%t"
82 "%%OS='%OS'%t"
83 "%%p='%p'%t"
84 "%%R='%R'%t"
85 "%%T='%T'%t"
86 "%%r='%r'%t"
87 "%%X='%X'%t"
88 "%%EX='%EX'%t"
89 "%n}");
90
91 const std::locale loc(LOCALE_ja_JP_UTF_8);
92 std::locale::global(std::locale(LOCALE_fr_FR_UTF_8));
93
94 // Non localized output using C-locale
95 check(SV("%H='00'\t"
96 "%OH='00'\t"
97 "%I='12'\t"
98 "%OI='12'\t"
99 "%M='00'\t"
100 "%OM='00'\t"
101 "%S='00'\t"
102 "%OS='00'\t"
103 "%p='AM'\t"
104 "%R='00:00'\t"
105 "%T='00:00:00'\t"
106 "%r='12:00:00 AM'\t"
107 "%X='00:00:00'\t"
108 "%EX='00:00:00'\t"
109 "\n"),
110 fmt,
111 std::chrono::hh_mm_ss(0s));
112
113 check(SV("%H='23'\t"
114 "%OH='23'\t"
115 "%I='11'\t"
116 "%OI='11'\t"
117 "%M='31'\t"
118 "%OM='31'\t"
119 "%S='30.123'\t"
120 "%OS='30.123'\t"
121 "%p='PM'\t"
122 "%R='23:31'\t"
123 "%T='23:31:30.123'\t"
124 "%r='11:31:30 PM'\t"
125 "%X='23:31:30'\t"
126 "%EX='23:31:30'\t"
127 "\n"),
128 fmt,
129 std::chrono::hh_mm_ss(23h + 31min + 30s + 123ms));
130
131 check(SV("-%H='03'\t"
132 "%OH='03'\t"
133 "%I='03'\t"
134 "%OI='03'\t"
135 "%M='02'\t"
136 "%OM='02'\t"
137 "%S='01.123456789012'\t"
138 "%OS='01.123456789012'\t"
139 "%p='AM'\t"
140 "%R='03:02'\t"
141 "%T='03:02:01.123456789012'\t"
142 "%r='03:02:01 AM'\t"
143 "%X='03:02:01'\t"
144 "%EX='03:02:01'\t"
145 "\n"),
146 fmt,
147 std::chrono::hh_mm_ss(-(3h + 2min + 1s + std::chrono::duration<std::int64_t, std::pico>(123456789012))));
148
149 // The number of fractional seconds is 0 according to the Standard
150 // TODO FMT Determine what to do.
151 check(SV("%H='01'\t"
152 "%OH='01'\t"
153 "%I='01'\t"
154 "%OI='01'\t"
155 "%M='01'\t"
156 "%OM='01'\t"
157 "%S='01'\t"
158 "%OS='01'\t"
159 "%p='AM'\t"
160 "%R='01:01'\t"
161 "%T='01:01:01'\t"
162 "%r='01:01:01 AM'\t"
163 "%X='01:01:01'\t"
164 "%EX='01:01:01'\t"
165 "\n"),
166 fmt,
167 std::chrono::hh_mm_ss(std::chrono::duration<double>(3661.123456)));
168
169 // Use the global locale (fr_FR)
170 check(SV("%H='00'\t"
171 "%OH='00'\t"
172 "%I='12'\t"
173 "%OI='12'\t"
174 "%M='00'\t"
175 "%OM='00'\t"
176 "%S='00'\t"
177 "%OS='00'\t"
178 #if defined(_AIX)
179 "%p='AM'\t"
180 #else
181 "%p=''\t"
182 #endif
183 "%R='00:00'\t"
184 "%T='00:00:00'\t"
185 #ifdef _WIN32
186 "%r='00:00:00'\t"
187 #elif defined(_AIX)
188 "%r='12:00:00 AM'\t"
189 #elif defined(__APPLE__) || defined(__FreeBSD__)
190 "%r=''\t"
191 #else
192 "%r='12:00:00 '\t"
193 #endif
194 "%X='00:00:00'\t"
195 "%EX='00:00:00'\t"
196 "\n"),
197 lfmt,
198 std::chrono::hh_mm_ss(0s));
199
200 check(SV("%H='23'\t"
201 "%OH='23'\t"
202 "%I='11'\t"
203 "%OI='11'\t"
204 "%M='31'\t"
205 "%OM='31'\t"
206 "%S='30,123'\t"
207 "%OS='30,123'\t"
208 #if defined(_AIX)
209 "%p='PM'\t"
210 #else
211 "%p=''\t"
212 #endif
213 "%R='23:31'\t"
214 "%T='23:31:30,123'\t"
215 #ifdef _WIN32
216 "%r='23:31:30'\t"
217 #elif defined(_AIX)
218 "%r='11:31:30 PM'\t"
219 #elif defined(__APPLE__) || defined(__FreeBSD__)
220 "%r=''\t"
221 #else
222 "%r='11:31:30 '\t"
223 #endif
224 "%X='23:31:30'\t"
225 "%EX='23:31:30'\t"
226 "\n"),
227 lfmt,
228 std::chrono::hh_mm_ss(23h + 31min + 30s + 123ms));
229
230 check(SV("-%H='03'\t"
231 "%OH='03'\t"
232 "%I='03'\t"
233 "%OI='03'\t"
234 "%M='02'\t"
235 "%OM='02'\t"
236 "%S='01,123456789012'\t"
237 "%OS='01,123456789012'\t"
238 #if defined(_AIX)
239 "%p='AM'\t"
240 #else
241 "%p=''\t"
242 #endif
243 "%R='03:02'\t"
244 "%T='03:02:01,123456789012'\t"
245 #ifdef _WIN32
246 "%r='03:02:01'\t"
247 #elif defined(_AIX)
248 "%r='03:02:01 AM'\t"
249 #elif defined(__APPLE__) || defined(__FreeBSD__)
250 "%r=''\t"
251 #else
252 "%r='03:02:01 '\t"
253 #endif
254 "%X='03:02:01'\t"
255 "%EX='03:02:01'\t"
256 "\n"),
257 lfmt,
258 std::chrono::hh_mm_ss(-(3h + 2min + 1s + std::chrono::duration<std::int64_t, std::pico>(123456789012))));
259
260 check(SV("%H='01'\t"
261 "%OH='01'\t"
262 "%I='01'\t"
263 "%OI='01'\t"
264 "%M='01'\t"
265 "%OM='01'\t"
266 "%S='01'\t"
267 "%OS='01'\t"
268 #if defined(_AIX)
269 "%p='AM'\t"
270 #else
271 "%p=''\t"
272 #endif
273 "%R='01:01'\t"
274 "%T='01:01:01'\t"
275 #ifdef _WIN32
276 "%r='01:01:01'\t"
277 #elif defined(_AIX)
278 "%r='01:01:01 AM'\t"
279 #elif defined(__APPLE__) || defined(__FreeBSD__)
280 "%r=''\t"
281 #else
282 "%r='01:01:01 '\t"
283 #endif
284 "%X='01:01:01'\t"
285 "%EX='01:01:01'\t"
286 "\n"),
287 lfmt,
288 std::chrono::hh_mm_ss(std::chrono::duration<double>(3661.123456)));
289
290 // Use supplied locale (ja_JP). This locale has a different alternate.
291 #if defined(__APPLE__) || defined(_AIX) || defined(_WIN32) || defined(__FreeBSD__)
292 check(loc,
293 SV("%H='00'\t"
294 "%OH='00'\t"
295 "%I='12'\t"
296 "%OI='12'\t"
297 "%M='00'\t"
298 "%OM='00'\t"
299 "%S='00'\t"
300 "%OS='00'\t"
301 # if defined(__APPLE__)
302 "%p='AM'\t"
303 # else
304 "%p='午前'\t"
305 # endif
306 "%R='00:00'\t"
307 "%T='00:00:00'\t"
308 # if defined(__APPLE__) || defined(__FreeBSD__)
309 # if defined(__APPLE__)
310 "%r='12:00:00 AM'\t"
311 # else
312 "%r='12:00:00 午前'\t"
313 # endif
314 "%X='00時00分00秒'\t"
315 "%EX='00時00分00秒'\t"
316 # elif defined(_WIN32)
317 "%r='0:00:00'\t"
318 "%X='0:00:00'\t"
319 "%EX='0:00:00'\t"
320 # else
321 "%r='午前12:00:00'\t"
322 "%X='00:00:00'\t"
323 "%EX='00:00:00'\t"
324 # endif
325 "\n"),
326 lfmt,
327 std::chrono::hh_mm_ss(0s));
328
329 check(loc,
330 SV("%H='23'\t"
331 "%OH='23'\t"
332 "%I='11'\t"
333 "%OI='11'\t"
334 "%M='31'\t"
335 "%OM='31'\t"
336 "%S='30.123'\t"
337 "%OS='30.123'\t"
338 # if defined(__APPLE__)
339 "%p='PM'\t"
340 # else
341 "%p='午後'\t"
342 # endif
343 "%R='23:31'\t"
344 "%T='23:31:30.123'\t"
345 # if defined(__APPLE__) || defined(__FreeBSD__)
346 # if defined(__APPLE__)
347 "%r='11:31:30 PM'\t"
348 # else
349 "%r='11:31:30 午後'\t"
350 # endif
351 "%X='23時31分30秒'\t"
352 "%EX='23時31分30秒'\t"
353 # elif defined(_WIN32)
354 "%r='23:31:30'\t"
355 "%X='23:31:30'\t"
356 "%EX='23:31:30'\t"
357 # else
358 "%r='午後11:31:30'\t"
359 "%X='23:31:30'\t"
360 "%EX='23:31:30'\t"
361 # endif
362 "\n"),
363 lfmt,
364 std::chrono::hh_mm_ss(23h + 31min + 30s + 123ms));
365
366 check(loc,
367 SV("-%H='03'\t"
368 "%OH='03'\t"
369 "%I='03'\t"
370 "%OI='03'\t"
371 "%M='02'\t"
372 "%OM='02'\t"
373 "%S='01.123456789012'\t"
374 "%OS='01.123456789012'\t"
375 # if defined(__APPLE__)
376 "%p='AM'\t"
377 # else
378 "%p='午前'\t"
379 # endif
380 "%R='03:02'\t"
381 "%T='03:02:01.123456789012'\t"
382 # if defined(__APPLE__) || defined(__FreeBSD__)
383 # if defined(__APPLE__)
384 "%r='03:02:01 AM'\t"
385 # else
386 "%r='03:02:01 午前'\t"
387 # endif
388 "%X='03時02分01秒'\t"
389 "%EX='03時02分01秒'\t"
390 # elif defined(_WIN32)
391 "%r='3:02:01'\t"
392 "%X='3:02:01'\t"
393 "%EX='3:02:01'\t"
394 # else
395 "%r='午前03:02:01'\t"
396 "%X='03:02:01'\t"
397 "%EX='03:02:01'\t"
398 # endif
399 "\n"),
400 lfmt,
401 std::chrono::hh_mm_ss(-(3h + 2min + 1s + std::chrono::duration<std::int64_t, std::pico>(123456789012))));
402
403 check(loc,
404 SV("%H='01'\t"
405 "%OH='01'\t"
406 "%I='01'\t"
407 "%OI='01'\t"
408 "%M='01'\t"
409 "%OM='01'\t"
410 "%S='01'\t"
411 "%OS='01'\t"
412 # if defined(__APPLE__)
413 "%p='AM'\t"
414 # else
415 "%p='午前'\t"
416 # endif
417 "%R='01:01'\t"
418 "%T='01:01:01'\t"
419 # if defined(__APPLE__) || defined(__FreeBSD__)
420 # if defined(__APPLE__)
421 "%r='01:01:01 AM'\t"
422 # else
423 "%r='01:01:01 午前'\t"
424 # endif
425 "%X='01時01分01秒'\t"
426 "%EX='01時01分01秒'\t"
427 # elif defined(_WIN32)
428 "%r='1:01:01'\t"
429 "%X='1:01:01'\t"
430 "%EX='1:01:01'\t"
431 # else
432 "%r='午前01:01:01'\t"
433 "%X='01:01:01'\t"
434 "%EX='01:01:01'\t"
435 # endif
436 "\n"),
437 lfmt,
438 std::chrono::hh_mm_ss(std::chrono::duration<double>(3661.123456)));
439 #else // defined(__APPLE__) || defined(_AIX) || defined(_WIN32)
440 check(loc,
441 SV("%H='00'\t"
442 "%OH='〇'\t"
443 "%I='12'\t"
444 "%OI='十二'\t"
445 "%M='00'\t"
446 "%OM='〇'\t"
447 "%S='00'\t"
448 "%OS='〇'\t"
449 "%p='午前'\t"
450 "%R='00:00'\t"
451 "%T='00:00:00'\t"
452 "%r='午前12時00分00秒'\t"
453 "%X='00時00分00秒'\t"
454 "%EX='00時00分00秒'\t"
455 "\n"),
456 lfmt,
457 std::chrono::hh_mm_ss(0s));
458
459 // TODO FMT What should fractions be in alternate display mode?
460 check(loc,
461 SV("%H='23'\t"
462 "%OH='二十三'\t"
463 "%I='11'\t"
464 "%OI='十一'\t"
465 "%M='31'\t"
466 "%OM='三十一'\t"
467 "%S='30.123'\t"
468 "%OS='三十.123'\t"
469 "%p='午後'\t"
470 "%R='23:31'\t"
471 "%T='23:31:30.123'\t"
472 "%r='午後11時31分30秒'\t"
473 "%X='23時31分30秒'\t"
474 "%EX='23時31分30秒'\t"
475 "\n"),
476 lfmt,
477 std::chrono::hh_mm_ss(23h + 31min + 30s + 123ms));
478
479 check(loc,
480 SV("-%H='03'\t"
481 "%OH='三'\t"
482 "%I='03'\t"
483 "%OI='三'\t"
484 "%M='02'\t"
485 "%OM='二'\t"
486 "%S='01.123456789012'\t"
487 "%OS='一.123456789012'\t"
488 "%p='午前'\t"
489 "%R='03:02'\t"
490 "%T='03:02:01.123456789012'\t"
491 "%r='午前03時02分01秒'\t"
492 "%X='03時02分01秒'\t"
493 "%EX='03時02分01秒'\t"
494 "\n"),
495 lfmt,
496 std::chrono::hh_mm_ss(-(3h + 2min + 1s + std::chrono::duration<std::int64_t, std::pico>(123456789012))));
497
498 check(loc,
499 SV("%H='01'\t"
500 "%OH='一'\t"
501 "%I='01'\t"
502 "%OI='一'\t"
503 "%M='01'\t"
504 "%OM='一'\t"
505 "%S='01'\t"
506 "%OS='一'\t"
507 "%p='午前'\t"
508 "%R='01:01'\t"
509 "%T='01:01:01'\t"
510 "%r='午前01時01分01秒'\t"
511 "%X='01時01分01秒'\t"
512 "%EX='01時01分01秒'\t"
513 "\n"),
514 lfmt,
515 std::chrono::hh_mm_ss(std::chrono::duration<double>(3661.123456)));
516 #endif // defined(__APPLE__) || defined(_AIX) || defined(_WIN32)
517
518 std::locale::global(std::locale::classic());
519 }
520
521 template <class CharT>
522 static void test_invalid_values() {
523 using namespace std::literals::chrono_literals;
524
525 // This looks odd, however the 24 hours is not valid for a 24 hour clock.
526 // TODO FMT discuss what the "proper" behaviour is.
527 check_exception("Formatting a hour needs a valid value", SV("{:%H"), std::chrono::hh_mm_ss{24h});
528 check_exception("Formatting a hour needs a valid value", SV("{:%OH"), std::chrono::hh_mm_ss{24h});
529 check_exception("Formatting a hour needs a valid value", SV("{:%I"), std::chrono::hh_mm_ss{24h});
530 check_exception("Formatting a hour needs a valid value", SV("{:%OI"), std::chrono::hh_mm_ss{24h});
531 check(SV("00"), SV("{:%M}"), std::chrono::hh_mm_ss{24h});
532 check(SV("00"), SV("{:%OM}"), std::chrono::hh_mm_ss{24h});
533 check(SV("00"), SV("{:%S}"), std::chrono::hh_mm_ss{24h});
534 check(SV("00"), SV("{:%OS}"), std::chrono::hh_mm_ss{24h});
535 check_exception("Formatting a hour needs a valid value", SV("{:%p"), std::chrono::hh_mm_ss{24h});
536 check_exception("Formatting a hour needs a valid value", SV("{:%R"), std::chrono::hh_mm_ss{24h});
537 check_exception("Formatting a hour needs a valid value", SV("{:%T"), std::chrono::hh_mm_ss{24h});
538 check_exception("Formatting a hour needs a valid value", SV("{:%r"), std::chrono::hh_mm_ss{24h});
539 check_exception("Formatting a hour needs a valid value", SV("{:%X"), std::chrono::hh_mm_ss{24h});
540 check_exception("Formatting a hour needs a valid value", SV("{:%EX"), std::chrono::hh_mm_ss{24h});
541 }
542
543 template <class CharT>
544 static void test() {
545 using namespace std::literals::chrono_literals;
546
547 test_no_chrono_specs<CharT>();
548 test_valid_values<CharT>();
549 test_invalid_values<CharT>();
550 check_invalid_types<CharT>(
551 {SV("H"),
552 SV("I"),
553 SV("M"),
554 SV("S"),
555 SV("p"),
556 SV("r"),
557 SV("R"),
558 SV("T"),
559 SV("X"),
560 SV("OH"),
561 SV("OI"),
562 SV("OM"),
563 SV("OS"),
564 SV("EX")},
565 std::chrono::hh_mm_ss{0ms});
566
567 check_exception("The format specifier expects a '%' or a '}'", SV("{:A"), std::chrono::hh_mm_ss{0ms});
568 check_exception("The chrono specifiers contain a '{'", SV("{:%%{"), std::chrono::hh_mm_ss{0ms});
569 check_exception("End of input while parsing a conversion specifier", SV("{:%"), std::chrono::hh_mm_ss{0ms});
570 check_exception("End of input while parsing the modifier E", SV("{:%E"), std::chrono::hh_mm_ss{0ms});
571 check_exception("End of input while parsing the modifier O", SV("{:%O"), std::chrono::hh_mm_ss{0ms});
572
573 check_exception("The format specifier expects a '%' or a '}'", SV("{:.3}"), std::chrono::hh_mm_ss{0ms});
574 }
575
576 int main(int, char**) {
577 test<char>();
578
579 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
580 test<wchar_t>();
581 #endif
582
583 return 0;
584 }