Debian Codegraph @

libstdc++-v3/testsuite/std/time/time_zone/get_info_local.cc

raw
1 // { dg-do run { target c++20 } }
2 // { dg-require-effective-target tzdb }
3
4 #include <chrono>
5 #include <testsuite_hooks.h>
6
7 using namespace std::chrono;
8
9 struct empty_tag { } empty;
10
11 bool operator==(const sys_info& info, empty_tag)
12 {
13 return info.begin == sys_seconds() && info.end == info.begin
14 && info.offset == 0s && info.save == 0min && info.abbrev.empty();
15 }
16
17 void
18 test_utc()
19 {
20 auto tz = locate_zone("UTC");
21 auto now = time_point_cast<seconds>(system_clock::now());
22 local_info info = tz->get_info(local_seconds(now.time_since_epoch()));
23 VERIFY( info.result == local_info::unique );
24 VERIFY( info.first.begin < now );
25 VERIFY( info.first.end > now );
26 VERIFY( info.first.offset == 0h );
27 VERIFY( info.first.save == 0h );
28 VERIFY( info.first.abbrev == "UTC" );
29 VERIFY( info.second == empty );
30 }
31
32 auto dst_start = March/Sunday[last];
33 auto dst_end = October/Sunday[last];
34
35 void
36 test_unique()
37 {
38 auto tz = locate_zone("Europe/London");
39 local_days feb1(sys_days(2022y/February/1).time_since_epoch());
40 local_info info;
41
42 info = tz->get_info(feb1);
43 VERIFY( info.result == local_info::unique );
44 VERIFY( info.first.begin == sys_days(2021y/dst_end) + 1h );
45 VERIFY( info.first.end == sys_days(2022y/dst_start) + 1h );
46 VERIFY( info.first.offset == 0h );
47 VERIFY( info.first.save == 0h );
48 VERIFY( info.first.abbrev == "GMT" );
49 VERIFY( info.second == empty );
50
51 info = tz->get_info(feb1 + months(4));
52 VERIFY( info.result == local_info::unique );
53 VERIFY( info.first.begin == sys_days(2022y/dst_start) + 1h );
54 VERIFY( info.first.end == sys_days(2022y/dst_end) + 1h );
55 VERIFY( info.first.offset == 1h );
56 VERIFY( info.first.save == 1h );
57 VERIFY( info.first.abbrev == "BST" );
58 VERIFY( info.second == empty );
59 }
60
61 void
62 test_nonexistent()
63 {
64 auto tz = locate_zone("Europe/Helsinki");
65 sys_time<hours> change = sys_days(2022y/dst_start) + 1h;
66 local_seconds nonesuch(change.time_since_epoch() + 2h + 30min);
67 local_info info;
68
69 info = tz->get_info(nonesuch);
70 VERIFY( info.result == local_info::nonexistent );
71 VERIFY( info.first.end == change );
72 VERIFY( info.first.offset == 2h );
73 VERIFY( info.first.save == 0h );
74 VERIFY( info.first.abbrev == "EET" );
75 VERIFY( info.second.begin == info.first.end );
76 VERIFY( info.second.offset == 3h );
77 VERIFY( info.second.save == 1h );
78 VERIFY( info.second.abbrev == "EEST" );
79
80 tz = locate_zone("America/New_York");
81 nonesuch = local_days(Sunday[2]/March/2016) + 2h + 30min;
82 info = tz->get_info(nonesuch);
83 VERIFY( info.result == local_info::nonexistent );
84 VERIFY( info.first.end == sys_days(Sunday[2]/March/2016) + 5h + 2h );
85 VERIFY( info.first.offset == -5h );
86 VERIFY( info.first.save == 0h );
87 VERIFY( info.first.abbrev == "EST" );
88 VERIFY( info.second.begin == info.first.end );
89 VERIFY( info.second.offset == -4h );
90 VERIFY( info.second.save == 1h );
91 VERIFY( info.second.abbrev == "EDT" );
92 }
93
94 void
95 test_ambiguous()
96 {
97 auto tz = locate_zone("Europe/Helsinki");
98 sys_time<hours> change = sys_days(2022y/dst_end) + 1h;
99 local_seconds twix(change.time_since_epoch() + 2h + 30min);
100 local_info info;
101
102 info = tz->get_info(twix);
103 VERIFY( info.result == local_info::ambiguous );
104 VERIFY( info.first.end == change );
105 VERIFY( info.first.offset == 3h );
106 VERIFY( info.first.save == 1h );
107 VERIFY( info.first.abbrev == "EEST" );
108 VERIFY( info.second.begin == info.first.end );
109 VERIFY( info.second.offset == 2h );
110 VERIFY( info.second.save == 0h );
111 VERIFY( info.second.abbrev == "EET" );
112
113 tz = locate_zone("America/New_York");
114 twix = local_days(Sunday[2]/March/2016) + 2h + 30min;
115 info = tz->get_info(twix);
116 VERIFY( info.result == local_info::nonexistent );
117 VERIFY( info.first.end == sys_days(Sunday[2]/March/2016) + 5h + 2h );
118 VERIFY( info.first.offset == -5h );
119 VERIFY( info.first.save == 0h );
120 VERIFY( info.first.abbrev == "EST" );
121 VERIFY( info.second.begin == info.first.end );
122 VERIFY( info.second.offset == -4h );
123 VERIFY( info.second.save == 1h );
124 VERIFY( info.second.abbrev == "EDT" );
125 }
126
127 void
128 test_egypt()
129 {
130 local_days d(2010y/May/1);
131 auto tz = locate_zone("Egypt");
132 local_info info = tz->get_info(d);
133 VERIFY( info.result == local_info::unique );
134 VERIFY( info.first.begin == sys_days(2010y/April/29) + 22h );
135 VERIFY( info.first.offset == 3h );
136 VERIFY( info.first.save == 1h );
137 VERIFY( info.first.abbrev == "EEST" );
138
139 info = tz->get_info(d - 24h);
140 VERIFY( info.result == local_info::nonexistent );
141 VERIFY( info.first.begin == sys_days(2009y/August/20) + 21h );
142 VERIFY( info.first.offset == 2h );
143 VERIFY( info.first.save == 0h );
144 VERIFY( info.first.abbrev == "EET" );
145 VERIFY( info.second.begin == sys_days(2010y/April/29) + 22h );
146 VERIFY( info.second.offset == 3h );
147 VERIFY( info.second.save == 1h );
148 VERIFY( info.second.abbrev == "EEST" );
149
150 std::ostringstream out;
151 local_seconds lt(local_days(2001y/January/1));
152 const local_days end(2014y/September/1);
153
154 while (lt < end)
155 {
156 local_info i = tz->get_info(lt);
157
158 out << '\n' << i;
159
160 auto next = i.first.end;
161 if (i.result != local_info::unique)
162 next = i.second.begin + 24h;
163 lt = zoned_time(tz, next).get_local_time();
164 }
165 out << '\n';
166
167 std::string expected = R"(
168 [[2000-09-28 21:00:00,2001-04-26 22:00:00,02:00:00,0min,EET]]
169 [[2001-04-26 22:00:00,2001-09-27 21:00:00,03:00:00,60min,EEST]]
170 [ambiguous local time between [2001-04-26 22:00:00,2001-09-27 21:00:00,03:00:00,60min,EEST] and [2001-09-27 21:00:00,2002-04-25 22:00:00,02:00:00,0min,EET]]
171 [[2001-09-27 21:00:00,2002-04-25 22:00:00,02:00:00,0min,EET]]
172 [[2002-04-25 22:00:00,2002-09-26 21:00:00,03:00:00,60min,EEST]]
173 [ambiguous local time between [2002-04-25 22:00:00,2002-09-26 21:00:00,03:00:00,60min,EEST] and [2002-09-26 21:00:00,2003-04-24 22:00:00,02:00:00,0min,EET]]
174 [[2002-09-26 21:00:00,2003-04-24 22:00:00,02:00:00,0min,EET]]
175 [[2003-04-24 22:00:00,2003-09-25 21:00:00,03:00:00,60min,EEST]]
176 [ambiguous local time between [2003-04-24 22:00:00,2003-09-25 21:00:00,03:00:00,60min,EEST] and [2003-09-25 21:00:00,2004-04-29 22:00:00,02:00:00,0min,EET]]
177 [[2003-09-25 21:00:00,2004-04-29 22:00:00,02:00:00,0min,EET]]
178 [[2004-04-29 22:00:00,2004-09-30 21:00:00,03:00:00,60min,EEST]]
179 [ambiguous local time between [2004-04-29 22:00:00,2004-09-30 21:00:00,03:00:00,60min,EEST] and [2004-09-30 21:00:00,2005-04-28 22:00:00,02:00:00,0min,EET]]
180 [[2004-09-30 21:00:00,2005-04-28 22:00:00,02:00:00,0min,EET]]
181 [[2005-04-28 22:00:00,2005-09-29 21:00:00,03:00:00,60min,EEST]]
182 [ambiguous local time between [2005-04-28 22:00:00,2005-09-29 21:00:00,03:00:00,60min,EEST] and [2005-09-29 21:00:00,2006-04-27 22:00:00,02:00:00,0min,EET]]
183 [[2005-09-29 21:00:00,2006-04-27 22:00:00,02:00:00,0min,EET]]
184 [[2006-04-27 22:00:00,2006-09-21 21:00:00,03:00:00,60min,EEST]]
185 [ambiguous local time between [2006-04-27 22:00:00,2006-09-21 21:00:00,03:00:00,60min,EEST] and [2006-09-21 21:00:00,2007-04-26 22:00:00,02:00:00,0min,EET]]
186 [[2006-09-21 21:00:00,2007-04-26 22:00:00,02:00:00,0min,EET]]
187 [[2007-04-26 22:00:00,2007-09-06 21:00:00,03:00:00,60min,EEST]]
188 [ambiguous local time between [2007-04-26 22:00:00,2007-09-06 21:00:00,03:00:00,60min,EEST] and [2007-09-06 21:00:00,2008-04-24 22:00:00,02:00:00,0min,EET]]
189 [[2007-09-06 21:00:00,2008-04-24 22:00:00,02:00:00,0min,EET]]
190 [[2008-04-24 22:00:00,2008-08-28 21:00:00,03:00:00,60min,EEST]]
191 [ambiguous local time between [2008-04-24 22:00:00,2008-08-28 21:00:00,03:00:00,60min,EEST] and [2008-08-28 21:00:00,2009-04-23 22:00:00,02:00:00,0min,EET]]
192 [[2008-08-28 21:00:00,2009-04-23 22:00:00,02:00:00,0min,EET]]
193 [[2009-04-23 22:00:00,2009-08-20 21:00:00,03:00:00,60min,EEST]]
194 [ambiguous local time between [2009-04-23 22:00:00,2009-08-20 21:00:00,03:00:00,60min,EEST] and [2009-08-20 21:00:00,2010-04-29 22:00:00,02:00:00,0min,EET]]
195 [[2009-08-20 21:00:00,2010-04-29 22:00:00,02:00:00,0min,EET]]
196 [[2010-04-29 22:00:00,2010-08-10 21:00:00,03:00:00,60min,EEST]]
197 [ambiguous local time between [2010-04-29 22:00:00,2010-08-10 21:00:00,03:00:00,60min,EEST] and [2010-08-10 21:00:00,2010-09-09 22:00:00,02:00:00,0min,EET]]
198 [[2010-08-10 21:00:00,2010-09-09 22:00:00,02:00:00,0min,EET]]
199 [[2010-09-09 22:00:00,2010-09-30 21:00:00,03:00:00,60min,EEST]]
200 [ambiguous local time between [2010-09-09 22:00:00,2010-09-30 21:00:00,03:00:00,60min,EEST] and [2010-09-30 21:00:00,2014-05-15 22:00:00,02:00:00,0min,EET]]
201 [[2010-09-30 21:00:00,2014-05-15 22:00:00,02:00:00,0min,EET]]
202 [[2014-05-15 22:00:00,2014-06-26 21:00:00,03:00:00,60min,EEST]]
203 [ambiguous local time between [2014-05-15 22:00:00,2014-06-26 21:00:00,03:00:00,60min,EEST] and [2014-06-26 21:00:00,2014-07-31 22:00:00,02:00:00,0min,EET]]
204 [[2014-06-26 21:00:00,2014-07-31 22:00:00,02:00:00,0min,EET]]
205 [[2014-07-31 22:00:00,2014-09-25 21:00:00,03:00:00,60min,EEST]]
206 )";
207 VERIFY( out.str() == expected );
208 }
209
210 int main()
211 {
212 test_utc();
213 test_unique();
214 test_nonexistent();
215 test_ambiguous();
216 test_egypt();
217 }