Debian Codegraph @

libstdc++-v3/testsuite/std/time/zoned_time/io.cc

raw
1 // { dg-do run { target c++20 } }
2 // { dg-require-effective-target cxx11_abi }
3 // { dg-timeout-factor 2 }
4
5 #include <chrono>
6 #include <sstream>
7 #include <testsuite_hooks.h>
8
9 void
10 test_ostream()
11 {
12 using namespace std::chrono;
13 std::stringstream ss;
14 zoned_time<seconds> zt("America/New_York", sys_seconds{946'706'523s});
15 ss << zt;
16 VERIFY( ss.str() == "2000-01-01 01:02:03 EST" );
17 }
18
19 void
20 test_format()
21 {
22 using namespace std::chrono;
23 sys_time<milliseconds> t(1671470785708ms);
24 auto zone = "America/New_York";
25 zoned_time<milliseconds> zt(zone, t);
26
27 // Every conversion specifier is valid for a sys_time except %q and %Q.
28
29 std::string s = std::format("{:%a | %A | %b | %B | %c"
30 " | %C | %d | %D | %e | %F | %g | %G | %h"
31 " | %H | %I | %j | %m | %M | %p | %r | %R"
32 " | %S | %T | %u | %U | %V | %w | %W | %x"
33 " | %X | %y | %Y | %z | %Z}", zt);
34 VERIFY( s == "Mon | Monday | Dec | December | Mon Dec 19 12:26:25 2022"
35 " | 20 | 19 | 12/19/22 | 19 | 2022-12-19 | 22 | 2022 | Dec"
36 " | 12 | 12 | 353 | 12 | 26 | PM | 12:26:25 PM | 12:26"
37 " | 25.708 | 12:26:25.708 | 1 | 51 | 51 | 1 | 51 | 12/19/22"
38 " | 12:26:25 | 22 | 2022 | -0500 | EST" );
39
40 #ifdef _GLIBCXX_USE_WCHAR_T
41 std::wstring ws = std::format(L"{:%a | %A | %b | %B | %c"
42 " | %C | %d | %D | %e | %F | %g | %G | %h"
43 " | %H | %I | %j | %m | %M | %p | %r | %R"
44 " | %S | %T | %u | %U | %V | %w | %W | %x"
45 " | %X | %y | %Y | %z | %Z}", zt);
46 VERIFY( ws == L"Mon | Monday | Dec | December | Mon Dec 19 12:26:25 2022"
47 " | 20 | 19 | 12/19/22 | 19 | 2022-12-19 | 22 | 2022 | Dec"
48 " | 12 | 12 | 353 | 12 | 26 | PM | 12:26:25 PM | 12:26"
49 " | 25.708 | 12:26:25.708 | 1 | 51 | 51 | 1 | 51 | 12/19/22"
50 " | 12:26:25 | 22 | 2022 | -0500 | EST" );
51 #endif
52
53 auto loc = std::locale::classic();
54 auto smod = std::format(loc, "{:%Ec %EC %Od %Oe %OH %OI %Om %OM %OS %Ou %OU"
55 " %Ow %OW %Ex %EX %Oy %Ey %EY %Ez %Oz}", zt);
56 s = std::format("{:%c %C %d %e %H %I %m %M %S %u %U"
57 " %w %W %x %X %y %y %Y -05:00 -05:00}",
58 zoned_time<seconds>(zone, time_point_cast<seconds>(t)));
59 VERIFY( smod == s );
60
61 s = std::format("{}", zt);
62 VERIFY( s == "2022-12-19 12:26:25.708 EST" );
63 s = std::format("{1:=>30}", 1, zt);
64 VERIFY( s == "===2022-12-19 12:26:25.708 EST" );
65 #ifdef _GLIBCXX_USE_WCHAR_T
66 ws = std::format(L"{:+^34}", zoned_time<microseconds>(zone, t));
67 VERIFY( ws == L"++2022-12-19 12:26:25.708000 EST++" );
68 #endif
69
70 // LWG 4124. Cannot format zoned_time with resolution coarser than seconds
71 s = std::format("{}", zoned_time<minutes>(zone, time_point_cast<minutes>(t)));
72 VERIFY( s == "2022-12-19 12:26:00 EST" );
73 }
74
75 int main()
76 {
77 test_ostream();
78 test_format();
79 }