Debian Codegraph @

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

raw
1 // { dg-do run { target c++20 } }
2 // { dg-require-namedlocale "fr_FR.ISO8859-15" }
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 std::ostringstream;
13 using namespace std::chrono;
14
15 ostringstream ss;
16 ss << 2022y/December/19 << ' ' << 2022y/November/31;
17 VERIFY( ss.str() == "2022-12-19 2022-11-31 is not a valid date" );
18
19 ss.str("");
20 ss.imbue(std::locale(ISO_8859(15,fr_FR)));
21 ss << 1789y/July/14;
22 VERIFY( ss.str() == "1789-07-14" );
23 }
24
25 void
26 test_format()
27 {
28 using std::chrono::year_month_day;
29 using std::chrono::December;
30 using std::chrono::January;
31 using namespace std::chrono_literals;
32
33 auto s = std::format("{:%y%%%Y%t%C%n%j %a %b}", 2022y/December/19);
34 VERIFY( s == "22%2022\t20\n353 Mon Dec" );
35 auto ws = std::format(L"{:%y%%%Y%t%C%n%d}", 2023y/January/32);
36 VERIFY( ws == L"23%2023\t20\n32" );
37
38 s = std::format("{:%F} {}", 2023y/January/32, 2023y/January/32);
39 VERIFY( s == "2023-01-32 2023-01-32 is not a valid date" );
40
41 s = std::format("{:%C%g-W%V-%u}", 2022y/January/1);
42 VERIFY( s == "2021-W52-6" );
43 s = std::format("{:%G-W%V-%u}", 2022y/January/3);
44 VERIFY( s == "2022-W01-1" );
45
46 // %U: Week number for weeks starting on Sunday
47 s = std::format("Day {:%w (%a) of Week %U of %Y}", 2022y/January/1);
48 VERIFY( s == "Day 6 (Sat) of Week 00 of 2022" );
49 s = std::format("Day {:%w (%a) of Week %U of %Y}", 2022y/January/2);
50 VERIFY( s == "Day 0 (Sun) of Week 01 of 2022" );
51 // %W: Week number for weeks starting on Monday
52 s = std::format("Day {:%u (%a) of Week %W of %Y}", 2022y/January/2);
53 VERIFY( s == "Day 7 (Sun) of Week 00 of 2022" );
54 s = std::format("Day {:%u (%a) of Week %W of %Y}", 2022y/January/3);
55 VERIFY( s == "Day 1 (Mon) of Week 01 of 2022" );
56
57 // %V: ISO week number (ISO 8601).
58 s = std::format("W{:%V}", 1977y/1/1);
59 VERIFY( s == "W53" );
60 s = std::format("W{:%V}", 1977y/1/2);
61 VERIFY( s == "W53" );
62 s = std::format("W{:%V}", 1977y/12/31);
63 VERIFY( s == "W52" );
64 s = std::format("W{:%V}", 1978y/1/1);
65 VERIFY( s == "W52" );
66 s = std::format("W{:%V}", 1978y/1/2);
67 VERIFY( s == "W01" );
68 s = std::format("W{:%V}", 1978y/12/31);
69 VERIFY( s == "W52" );
70 s = std::format("W{:%V}", 1979y/1/1);
71 VERIFY( s == "W01" );
72 s = std::format("W{:%V}", 1979y/12/30);
73 VERIFY( s == "W52" );
74 s = std::format("W{:%V}", 1979y/12/31);
75 VERIFY( s == "W01" );
76 s = std::format("W{:%V}", 1980y/1/1);
77 VERIFY( s == "W01" );
78
79 s = std::format("{:%x}", 2022y/December/19);
80 VERIFY( s == "12/19/22" );
81 s = std::format("{:L%x}", 2022y/December/19);
82 VERIFY( s == "12/19/22" );
83 std::locale loc_fr(ISO_8859(15,fr_FR));
84 s = std::format(loc_fr, "{:%x}", 2022y/December/19);
85 VERIFY( s == "12/19/22" );
86 s = std::format(loc_fr, "{:L%x}", 2022y/December/19);
87 VERIFY( s == "19/12/2022" || s == "19.12.2022" ); // depends on locale defs
88 s = std::format(loc_fr, "{}", 2022y/December/19);
89 VERIFY( s == "2022-12-19" );
90 s = std::format(loc_fr, "{:L%F}", 2022y/December/19);
91 VERIFY( s == "2022-12-19" );
92
93 std::string_view specs = "aAbBcCdDeFgGhHIjmMpqQrRSTuUVwWxXyYzZ";
94 std::string_view my_specs = "aAbBCdDeFgGhjmuUVwWxyY";
95 for (char c : specs)
96 {
97 char fmt[] = { '{', ':', '%', c, '}' };
98 try
99 {
100 year_month_day ymd = 2022y/December/19;
101 (void) std::vformat(std::string_view(fmt, 5), std::make_format_args(ymd));
102 // The call above should throw for any conversion-spec not in my_specs:
103 VERIFY(my_specs.find(c) != my_specs.npos);
104 }
105 catch (const std::format_error& e)
106 {
107 VERIFY(my_specs.find(c) == my_specs.npos);
108 std::string_view s = e.what();
109 // Libstdc++-specific message:
110 VERIFY(s.find("format argument does not contain the information "
111 "required by the chrono-specs") != s.npos);
112 }
113 }
114 }
115
116 void
117 test_parse()
118 {
119 using namespace std::chrono;
120 const year_month_day expected = 2023y/August/10;
121 year_month_day ymd;
122
123 minutes offset;
124 std::string abbrev;
125 std::istringstream is("23 2220 21:44:3 +1 'BST'");
126 VERIFY( is >> parse("%y %j0 %4H:%5M:%6S %Oz '%Z'", ymd, abbrev, offset) );
127 VERIFY( ! is.eof() );
128 VERIFY( ymd == expected );
129 VERIFY( abbrev == "BST" );
130 VERIFY( offset == 60min );
131
132 is.clear();
133 is.str("2023 365");
134 VERIFY( is >> parse("%Y %j", ymd) );
135 VERIFY( ymd == 2023y/December/31 );
136
137 ymd = 1970y/January/1;
138 is.clear();
139 is.str("2023 366");
140 VERIFY( ! (is >> parse("%Y %j", ymd)) ); // Not a leap year, no 366th day.
141 VERIFY( ymd == 1970y/January/1 );
142
143 is.clear();
144 is.str("2020 366");
145 VERIFY( is >> parse("%Y %j", ymd) );
146 VERIFY( ! is.eof() );
147 VERIFY( ymd == 2020y/December/31 );
148
149 ymd = 1970y/January/1;
150 is.clear();
151 is.str("2020 0");
152 VERIFY( ! (is >> parse("%Y %j", ymd)) ); // zero is invalid for day-of-year
153 VERIFY( is.eof() );
154 VERIFY( ymd == 1970y/January/1 );
155
156 is.clear();
157 is.str("2023-01-01 00:30 0100");
158 VERIFY( is >> parse("%F %R %z", ymd) );
159 VERIFY( ! is.eof() );
160 VERIFY( ymd == 2023y/January/1 ); // Date not adjusted by TZ offset.
161
162 ymd = {};
163 is.clear();
164 is.str("2022-W52-6");
165 VERIFY( is >> parse("%G-W%V-%u", ymd) );
166 VERIFY( ymd == 2022y/December/31 );
167
168 is.clear();
169 is.str("2022-W52-8");
170 VERIFY( ! (is >> parse("%G-W%V-%u", ymd)) ); // 8 is not a valid weekday
171 is.clear();
172 is.str("2022-W52-0");
173 VERIFY( ! (is >> parse("%G-W%V-%u", ymd)) ); // 0 is not a valid weekday
174 is.clear();
175 is.str("2022-W53-1");
176 VERIFY( ! (is >> parse("%G-W%V-%u", ymd)) ); // W53 is not valid for 2022
177 }
178
179 int main()
180 {
181 test_ostream();
182 test_format();
183 test_parse();
184 }