Debian Codegraph @

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

raw
1 // { dg-do run { target c++20 } }
2 // { dg-timeout-factor 2 }
3
4 #include <chrono>
5 #include <sstream>
6 #include <testsuite_hooks.h>
7
8 void
9 test_ostream()
10 {
11 using std::ostringstream;
12 using namespace std::chrono;
13
14 auto t = sys_days{July/1/2015} - 500ms;
15 auto u = clock_cast<utc_clock>(t);
16
17 std::string_view results[] = {
18 "2015-06-30 23:59:59.500 UTC",
19 "2015-06-30 23:59:59.750 UTC",
20 "2015-06-30 23:59:60.000 UTC",
21 "2015-06-30 23:59:60.250 UTC",
22 "2015-06-30 23:59:60.500 UTC",
23 "2015-06-30 23:59:60.750 UTC",
24 "2015-07-01 00:00:00.000 UTC",
25 "2015-07-01 00:00:00.250 UTC",
26 };
27
28 for (auto result : results)
29 {
30 ostringstream out;
31 out << u << " UTC";
32 VERIFY( out.str() == result );
33 u += 250ms;
34 }
35 }
36
37 void
38 test_format()
39 {
40 using namespace std::chrono_literals;
41 std::chrono::utc_time<std::chrono::milliseconds> t(1671470812708ms);
42
43 // Every conversion specifier is valid for a utc_time except %q and %Q.
44 std::string_view specs = "aAbBcCdDeFgGhHIjmMpqQrRSTuUVwWxXyYzZ";
45 std::string_view badspecs = "qQ";
46
47 std::ostringstream ss;
48 std::wostringstream wss;
49 const auto& ct = std::use_facet<std::ctype<wchar_t>>(wss.getloc());
50
51 for (char c : specs)
52 {
53 char fmt[] = { '{', ':', '%', c, '}' };
54 try
55 {
56 ss << std::vformat(std::string_view(fmt, 5),
57 std::make_format_args(t));
58 ss << " | ";
59 // The call above should throw for any conversion-spec in badspecs:
60 VERIFY(badspecs.find(c) == badspecs.npos);
61
62 }
63 catch (const std::format_error& e)
64 {
65 VERIFY(badspecs.find(c) != badspecs.npos);
66 std::string_view s = e.what();
67 // Libstdc++-specific message:
68 VERIFY(s.find("format argument does not contain the information "
69 "required by the chrono-specs") != s.npos);
70 }
71
72 wchar_t wfmt[] = { L'{', L':', L'%', ct.widen(c), L'}' };
73 try
74 {
75 wss << std::vformat(std::wstring_view(wfmt, 5),
76 std::make_wformat_args(t));
77 wss << L" | ";
78 // The call above should throw for any conversion-spec in badspecs:
79 VERIFY(badspecs.find(c) == badspecs.npos);
80 }
81 catch (const std::format_error& e)
82 {
83 VERIFY(badspecs.find(c) != badspecs.npos);
84 std::string_view s = e.what();
85 // Libstdc++-specific message:
86 VERIFY(s.find("format argument does not contain the information "
87 "required by the chrono-specs") != s.npos);
88 }
89 }
90
91 std::string s = ss.str();
92 VERIFY( s == "Mon | Monday | Dec | December | Mon Dec 19 17:26:25 2022"
93 " | 20 | 19 | 12/19/22 | 19 | 2022-12-19 | 22 | 2022 | Dec"
94 " | 17 | 05 | 353 | 12 | 26 | PM | 05:26:25 PM | 17:26"
95 " | 25.708 | 17:26:25.708 | 1 | 51 | 51 | 1 | 51 | 12/19/22"
96 " | 17:26:25 | 22 | 2022 | +0000 | UTC | " );
97
98 std::wstring ws = wss.str();
99 VERIFY( ws == L"Mon | Monday | Dec | December | Mon Dec 19 17:26:25 2022"
100 " | 20 | 19 | 12/19/22 | 19 | 2022-12-19 | 22 | 2022 | Dec"
101 " | 17 | 05 | 353 | 12 | 26 | PM | 05:26:25 PM | 17:26"
102 " | 25.708 | 17:26:25.708 | 1 | 51 | 51 | 1 | 51 | 12/19/22"
103 " | 17:26:25 | 22 | 2022 | +0000 | UTC | " );
104
105 std::chrono::utc_seconds leap(1483228800s + 26s); // 1 Jan 2017
106 s = std::format("{:%T}", leap - 1s);
107 VERIFY( s == "23:59:59" );
108 s = std::format("{:%T}", leap);
109 VERIFY( s == "23:59:60" );
110 s = std::format("{:%T}", leap + 10ms);
111 VERIFY( s == "23:59:60.010" );
112
113 s = std::format("{:%T}", leap + 1s);
114 VERIFY( s == "00:00:00" );
115
116 // PR libstdc++/113500
117 s = std::format("{}", leap + 100ms + 2.5s);
118 VERIFY( s == "2017-01-01 00:00:01.600");
119
120 std::chrono::utc_seconds t0{};
121 s = std::format("{:%Z %z %Ez %Oz}", t0);
122 VERIFY( s == "UTC +0000 +00:00 +00:00" );
123
124 s = std::format("{}", t0);
125 VERIFY( s == "1970-01-01 00:00:00" );
126 }
127
128 void
129 test_parse()
130 {
131 using namespace std::chrono;
132 const sys_seconds expected = sys_days(2023y/August/9) + 20h + 44min + 3s;
133 utc_seconds tp;
134
135 minutes offset;
136 std::string abbrev;
137 std::istringstream is("23 2210 21:44:3 +1 BST#");
138 VERIFY( is >> parse("%y %j0 %4H:%5M:%6S %Oz %Z", tp, abbrev, offset) );
139 VERIFY( ! is.eof() );
140 VERIFY( tp == clock_cast<utc_clock>(expected) );
141 VERIFY( abbrev == "BST" );
142 VERIFY( offset == 60min );
143
144 tp = {};
145 is.clear();
146 is.str("20230809214403 0100 BST:");
147 VERIFY( is >> parse("%Y%m%d%H%M%S %z %Z:", tp) );
148 VERIFY( ! is.eof() );
149 VERIFY( tp == clock_cast<utc_clock>(expected) );
150
151 is.clear();
152 is.str("2023-W32-3 20:44:03");
153 VERIFY( is >> parse("%G-W%V-%u %T", tp) );
154 VERIFY( ! is.eof() );
155 VERIFY( tp == clock_cast<utc_clock>(expected) );
156
157 // Test round trip
158 std::stringstream ss;
159 ss << clock_cast<utc_clock>(expected) << " 0012 UTC";
160 VERIFY( ss >> parse("%F %T %z %Z", tp, abbrev, offset) );
161 VERIFY( ss.eof() );
162 VERIFY( (tp + offset) == clock_cast<utc_clock>(expected) );
163 VERIFY( abbrev == "UTC" );
164 VERIFY( offset == 12min );
165
166 ss.str("");
167 ss.clear();
168 ss << utc_seconds{};
169 VERIFY( ss >> parse("%F %T", tp) );
170 VERIFY( tp.time_since_epoch() == 0s );
171 }
172
173 int main()
174 {
175 test_ostream();
176 test_format();
177 test_parse();
178 }