Debian Codegraph @

libstdc++-v3/testsuite/std/time/clock/local/io.cc

raw
1 // { dg-do run { target c++20 } }
2 // { dg-timeout-factor 2 }
3
4 #include <chrono>
5 #include <format>
6 #include <testsuite_hooks.h>
7
8 void
9 test_ostream()
10 {
11 using namespace std::chrono;
12
13 auto lt = local_time<seconds>(1722198122s);
14
15 std::ostringstream ss;
16 ss << lt;
17 auto s = ss.str();
18 ss.str("");
19 ss.clear();
20 ss << sys_time<seconds>{lt.time_since_epoch()};
21 auto s2 = ss.str();
22 VERIFY( s == s2 );
23 }
24
25 void
26 test_format()
27 {
28 using std::format;
29 using namespace std::chrono;
30
31 auto lt = local_seconds(1722198122s);
32
33 #if __cpp_exceptions
34 auto args = std::make_format_args(lt);
35 try
36 {
37 (void) std::vformat("{:%Z}", args);
38 VERIFY(false);
39 }
40 catch (const std::format_error&)
41 {
42 }
43 try
44 {
45 (void) std::vformat("{:%z}", args);
46 VERIFY(false);
47 }
48 catch (const std::format_error&)
49 {
50 }
51 #endif
52
53 auto s = format("{0:%F %T %a}", lt);
54 VERIFY( s == "2024-07-28 20:22:02 Sun" );
55
56 s = format("{}", lt);
57 VERIFY( s == "2024-07-28 20:22:02" );
58
59 // Test formatting for chrono::local_time_format and local-time-format-t too:
60 auto ltf = local_time_format(lt);
61 s = std::format("{:%F %T %a %b}", ltf);
62 VERIFY( s == "2024-07-28 20:22:02 Sun Jul" );
63 #if __cpp_exceptions
64 try
65 {
66 (void) std::format("{:%Z}", ltf);
67 VERIFY(false);
68 }
69 catch (const std::format_error&)
70 {
71 }
72 try
73 {
74 (void) std::format("{:%z}", ltf);
75 VERIFY(false);
76 }
77 catch (const std::format_error&)
78 {
79 }
80 #endif
81 std::string abbrev = "FOO";
82 seconds off = -3600s;
83 ltf = local_time_format(lt, &abbrev, &off);
84 s = std::format("{}", ltf);
85 VERIFY( s == "2024-07-28 20:22:02 FOO" );
86 s = std::format("{:%Z %T %F %z %Ez}", ltf);
87 __builtin_puts(s.c_str());
88 VERIFY( s == "FOO 20:22:02 2024-07-28 -0100 -01:00" );
89
90 s = std::format("{}", local_seconds{});
91 VERIFY( s == "1970-01-01 00:00:00" );
92
93 s = std::format("{}", local_days{}); // PR libstdc++/120293
94 VERIFY( s == "1970-01-01" );
95 }
96
97 void
98 test_parse()
99 {
100 using namespace std::chrono;
101 const sys_seconds expected = sys_days(2023y/August/9) + 21h + 44min;
102 local_seconds tp;
103
104 minutes offset;
105 std::string abbrev;
106 std::istringstream is("2023-8-9 21:44 +1 BST#"); // Not adjusted for offset.
107 VERIFY( is >> parse("%F %R %Oz %Z", tp, abbrev, offset) );
108 VERIFY( ! is.eof() );
109 VERIFY( tp == local_seconds(expected.time_since_epoch()) );
110 VERIFY( abbrev == "BST" );
111 VERIFY( offset == 60min );
112
113 // Test round trip
114 std::stringstream ss;
115 ss << local_seconds{expected.time_since_epoch()} << " X 0123";
116 VERIFY( ss >> parse("%F %T %Z %z", tp, abbrev, offset) );
117 VERIFY( ! ss.eof() );
118 VERIFY( tp == local_seconds{expected.time_since_epoch()} );
119 VERIFY( abbrev == "X" );
120 VERIFY( offset == (1h + 23min) );
121
122 ss.str("");
123 ss << local_seconds{};
124 VERIFY( ss >> parse("%F %T", tp) );
125 VERIFY( tp.time_since_epoch() == 0s );
126 }
127
128 int main()
129 {
130 test_ostream();
131 test_format();
132 test_parse();
133 }