Debian Codegraph @

libstdc++-v3/testsuite/std/time/clock/system/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 namespace std::chrono;
12 std::stringstream ss;
13 ss << sys_seconds{0s} << '\n'; // 1970-01-01 00:00:00
14 ss << sys_seconds{946'684'800s} << '\n'; // 2000-01-01 00:00:00
15 ss << sys_seconds{946'688'523s} << '\n'; // 2000-01-01 01:02:03
16 std::string s1, s2, s3;
17 std::getline(ss, s1);
18 std::getline(ss, s2);
19 std::getline(ss, s3);
20 VERIFY( s1 == "1970-01-01 00:00:00" );
21 VERIFY( s2 == "2000-01-01 00:00:00" );
22 VERIFY( s3 == "2000-01-01 01:02:03" );
23 }
24
25 template<typename T>
26 concept stream_insertable
27 = requires (std::ostream& out, const T& t) { out << t; };
28
29 // operator<<(ostream&, const sys_time<D>&) is constrained to not
30 // allow floating-point types or periods of days or greater.
31 using fp_sys_time = std::chrono::sys_time<std::chrono::duration<float>>;
32 static_assert( !stream_insertable<fp_sys_time> );
33
34 // But there is an overload for sys_days.
35 static_assert( stream_insertable<std::chrono::sys_days> );
36
37 void
38 test_format()
39 {
40 using namespace std::chrono_literals;
41 std::chrono::sys_time<std::chrono::milliseconds> t(1671470785708ms);
42
43 // Every conversion specifier is valid for a sys_time except %q and %Q.
44
45 std::string s = std::format("{:%a | %A | %b | %B | %c"
46 " | %C | %d | %D | %e | %F | %g | %G | %h"
47 " | %H | %I | %j | %m | %M | %p | %r | %R"
48 " | %S | %T | %u | %U | %V | %w | %W | %x"
49 " | %X | %y | %Y | %z | %Z}", t);
50 VERIFY( s == "Mon | Monday | Dec | December | Mon Dec 19 17:26:25 2022"
51 " | 20 | 19 | 12/19/22 | 19 | 2022-12-19 | 22 | 2022 | Dec"
52 " | 17 | 05 | 353 | 12 | 26 | PM | 05:26:25 PM | 17:26"
53 " | 25.708 | 17:26:25.708 | 1 | 51 | 51 | 1 | 51 | 12/19/22"
54 " | 17:26:25 | 22 | 2022 | +0000 | UTC" );
55
56 std::wstring ws = std::format(L"{:%a | %A | %b | %B | %c"
57 " | %C | %d | %D | %e | %F | %g | %G | %h"
58 " | %H | %I | %j | %m | %M | %p | %r | %R"
59 " | %S | %T | %u | %U | %V | %w | %W | %x"
60 " | %X | %y | %Y | %z | %Z}", t);
61 VERIFY( ws == L"Mon | Monday | Dec | December | Mon Dec 19 17:26:25 2022"
62 " | 20 | 19 | 12/19/22 | 19 | 2022-12-19 | 22 | 2022 | Dec"
63 " | 17 | 05 | 353 | 12 | 26 | PM | 05:26:25 PM | 17:26"
64 " | 25.708 | 17:26:25.708 | 1 | 51 | 51 | 1 | 51 | 12/19/22"
65 " | 17:26:25 | 22 | 2022 | +0000 | UTC" );
66
67 auto st = std::chrono::time_point_cast<std::chrono::seconds>(t);
68 auto loc = std::locale::classic();
69 auto smod = std::format(loc, "{:%Ec %EC %Od %Oe %OH %OI %Om %OM %OS %Ou %OU"
70 " %Ow %OW %Ex %EX %Oy %Ey %EY %Ez %Oz}", t);
71 s = std::format("{:%c %C %d %e %H %I %m %M %S %u %U"
72 " %w %W %x %X %y %y %Y +00:00 +00:00}",
73 st);
74 VERIFY( smod == s );
75
76 s = std::format("{}", t);
77 VERIFY( s == "2022-12-19 17:26:25.708" );
78 s = std::format("{0:} {0:=<21}", st);
79 VERIFY( s == "2022-12-19 17:26:25 2022-12-19 17:26:25==" );
80 }
81
82 void
83 test_parse()
84 {
85 using namespace std::chrono;
86 sys_seconds tp, expected = sys_days(2023y/July/24) + 13h + 05min;
87
88 std::istringstream is("24-hour time: 2023-07-24 13:05");
89 VERIFY( is >> parse("24-hour time: %Y-%m-%d %H:%M", tp) );
90 VERIFY( ! is.eof() );
91 VERIFY( tp == expected );
92
93 tp = {};
94 is.clear();
95 is.str("12-hour time: 2023-07-24 1.05 PM ");
96 VERIFY( is >> parse("12-hour time: %F %I.%M %p", tp) );
97 VERIFY( ! is.eof() );
98 VERIFY( tp == expected );
99
100 tp = {};
101 is.clear();
102 is.str("2023-07-24 14:05 +01");
103 VERIFY( is >> parse("%F %H:%M %z", tp) ); // %z is used even without offset
104 VERIFY( is.eof() );
105 VERIFY( tp == expected );
106
107 tp = {};
108 minutes offset{};
109 is.clear();
110 is.str("2023-07-24 15:35 0230");
111 VERIFY( is >> parse("%F %H:%M %z", tp, offset) );
112 VERIFY( ! is.eof() );
113 VERIFY( tp == expected );
114
115 tp = {};
116 std::string abbrev;
117 is.clear();
118 is.str("2023-07-24 08:05 -5:00 EST EST");
119 VERIFY( is >> parse("%F %H:%M %Ez %Z %Z", tp, abbrev) );
120 VERIFY( is.eof() );
121 VERIFY( tp == expected );
122 VERIFY( abbrev == "EST" );
123
124 tp = {};
125 abbrev = {};
126 offset = {};
127 is.clear();
128 is.str("2023-07-24 07:05 -06:00 ABC/+123/-456/_=");
129 VERIFY( is >> parse("%F %H:%M %Ez %Z", tp, abbrev, offset) );
130 VERIFY( ! is.eof() );
131 VERIFY( tp == expected );
132 VERIFY( offset == -360min );
133 VERIFY( abbrev == "ABC/+123/-456/_" );
134
135 tp = sys_seconds(99s);
136 offset = 99min;
137 is.clear();
138 is.str("-02:00 ");
139 VERIFY( ! (is >> parse("%Ez ", tp, offset)) );
140 VERIFY( is.fail() );
141 VERIFY( tp == sys_seconds(99s) ); // tp is only updated on successful parse.
142 VERIFY( offset == 99min ); // offset is only updated on successful parse.
143
144 tp = sys_seconds(99s);
145 abbrev = "99";
146 is.clear();
147 is.str("GMT ");
148 VERIFY( ! (is >> parse("%Z ", tp, abbrev)) );
149 VERIFY( is.fail() );
150 VERIFY( tp == sys_seconds(99s) ); // tp is only updated on successful parse.
151 VERIFY( abbrev == "99" ); // abbrev is only updated on successful parse.
152 }
153
154 int main()
155 {
156 test_ostream();
157 test_format();
158 test_parse();
159 }