]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/date_time/test/posix_time/testtime.cpp
import quincy beta 17.1.0
[ceph.git] / ceph / src / boost / libs / date_time / test / posix_time / testtime.cpp
1 /* Copyright (c) 2002,2003,2005 CrystalClear Software, Inc.
2 * Use, modification and distribution is subject to the
3 * Boost Software License, Version 1.0. (See accompanying
4 * file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
5 * Author: Jeff Garland, Bart Garst
6 */
7
8 #include <iostream>
9 #include "boost/date_time/gregorian/gregorian.hpp"
10 #include "boost/date_time/posix_time/posix_time.hpp"
11 #include "../testfrmwk.hpp"
12
13 void special_values_tests()
14 {
15 using namespace boost::posix_time;
16 using namespace boost::gregorian;
17
18 time_duration td_pi(pos_infin), td_ni(neg_infin), td_ndt(not_a_date_time);
19 date_duration dd_pi(pos_infin), dd_ni(neg_infin), dd_ndt(not_a_date_time);
20 date d_pi(pos_infin), d_ni(neg_infin), d_ndt(not_a_date_time);
21 time_duration td(1,2,3,4);
22 date_duration dd(1234);
23 date d(2003,Oct,31);
24
25 #if !defined(DATE_TIME_NO_DEFAULT_CONSTRUCTOR)
26 { // default constructor
27 ptime def;
28 check("Default constructor", def == ptime(not_a_date_time));
29 }
30 #endif // DATE_TIME_NO_DEFAULT_CONSTRUCTOR
31
32 #ifdef BOOST_NO_CXX14_CONSTEXPR
33 check("constexpr not configured", true);
34 #else
35 //check constexpr case
36 {
37 constexpr date d1(1970,1,1);
38 constexpr ptime t1(d1);
39 static_assert(t1.date().day() == 1, "constexpr construction day()");
40 static_assert(t1.date().month() == 1, "constexpr construction month()");
41 static_assert(t1.date().year() == 1970, "constexpr construction year()");
42 static_assert(t1.time_of_day().hours() == 0, "constexpr construction hour()");
43 static_assert(t1.time_of_day().minutes() == 0, "constexpr construction minute()");
44 static_assert(t1.time_of_day().seconds() == 0, "constexpr construction second()");
45 constexpr ptime t2(d1, time_duration(1,2,3));
46 static_assert(t2.time_of_day().hours() == 1, "constexpr contruct with duration hour()");
47 static_assert(t2.time_of_day().minutes() == 2, "constexpr contruct with duration minute()");
48 static_assert(t2.time_of_day().seconds() == 3, "constexpr contruct with duration second()");
49 check("constexpr tests compiled", true);
50 }
51 #endif
52
53
54 { // special values construction tests
55 ptime p_sv1(pos_infin);
56 std::string s("+infinity");
57 check("from special value +infinity", to_simple_string(p_sv1) == s);
58 ptime result = p_sv1 + dd;
59 check("Special value (pos infin) + date_duration = +infinity", to_iso_extended_string(result) == s);
60 check("is_special function - pos infin", result.is_special());
61 result = p_sv1 - dd;
62 check("Special value (pos infin) - date_duration = +infinity", to_iso_extended_string(result) == s);
63 result = p_sv1 - dd_ni;
64 check("Special value (pos infin) - date_duration (neg infin) = +infinity", to_iso_extended_string(result) == s);
65 ptime p_sv2(neg_infin);
66 s = "-infinity";
67 check("from special value -infinity", to_iso_string(p_sv2) == s);
68 result = p_sv2 - td_pi;
69 check("Special value (neg infin) - special time_duration (pos infin) = -infinity", to_iso_extended_string(result) == s);
70 ptime p_sv3(not_a_date_time);
71 check("is_special function - not_a_date_time", p_sv3.is_special());
72 s = "not-a-date-time";
73 check("from special value NADT", to_iso_extended_string(p_sv3) == s);
74 result = p_sv3 + td;
75 check("Special value (NADT) + time_duration = NADT", to_iso_extended_string(result) == s);
76 result = p_sv3 - td;
77 check("Special value (NADT) - time_duration = NADT", to_iso_extended_string(result) == s);
78 result = p_sv2 + td_pi;
79 check("Special value (neg infin) + special time_duration (pos infin) = NADT", to_iso_extended_string(result) == s);
80 result = p_sv1 + dd_ni;
81 check("Special value (pos infin) + date_duration (neg infin) = NADT", to_iso_extended_string(result) == s);
82 result = p_sv1 + dd_ndt;
83 check("Special value (pos infin) - date_duration (NADT) = NADT", to_iso_extended_string(result) == s);
84 }
85 { // special values construction tests
86 ptime p_sv1(d_pi, td);
87 std::string s("+infinity");
88 check("duration & special_date", to_simple_string(p_sv1) == s);
89 ptime p_sv2(d_ni, td);
90 s = "-infinity";
91 check("duration & special_date", to_iso_string(p_sv2) == s);
92 ptime p_sv3(d_ndt, td);
93 s = "not-a-date-time";
94 check("duration & special_date", to_iso_extended_string(p_sv3) == s);
95 }
96 { // special values construction tests
97 ptime p_sv1(d_ndt, td);
98 std::string s("not-a-date-time");
99 check("NADT & duration", to_simple_string(p_sv1) == s);
100 ptime p_sv2(d, td_ndt);
101 check("date & NADT", to_iso_string(p_sv2) == s);
102 ptime p_sv3(d_pi, td_ni);
103 check("+infinity_date & -infinity_duration",
104 to_iso_extended_string(p_sv3) == s);
105
106 }
107 { // special values tests
108 ptime p_sv1(d, td_pi), pt(d,td);
109 std::string s("+infinity");
110 check("special_duration & date", to_simple_string(p_sv1) == s);
111 check("ptime::date() +infinity", to_simple_string(p_sv1.date()) == s);
112 ptime p_sv2(d, td_ni);
113 s = "-infinity";
114 check("special_duration & date", to_iso_string(p_sv2) == s);
115 check("ptime::time_of_day() -infinity",
116 to_simple_string(p_sv2.time_of_day()) == s);
117 ptime p_sv3(d, td_ndt);
118 s = "not-a-date-time";
119 check("special_duration & date", to_iso_extended_string(p_sv3) == s);
120 check("ptime::date() - NADT", to_simple_string(p_sv3.date()) == s);
121 check("ptime::time_of_day() - NADT",
122 to_simple_string(p_sv3.time_of_day()) == s);
123 check("-infinity less than ...", p_sv2 < p_sv1);
124 check("-infinity less than ...", p_sv2 < pt);
125 check("+infinity greater than ...", pt < p_sv1);
126 check("-infinity less than equal to ...", p_sv2 <= p_sv2);
127 check("-infinity less than equal to ...", p_sv2 <= pt);
128 check("+infinity greater than equal to ...", p_sv1 >= pt);
129 check("not equal", p_sv1 != p_sv2);
130 check("not equal", p_sv3 != p_sv2);
131 check("not equal", pt != p_sv1);
132
133 check("is_pos_infinity", p_sv1.is_infinity() && p_sv1.is_pos_infinity());
134 check("is_neg_infinity", p_sv2.is_infinity() && p_sv2.is_neg_infinity());
135 check("is_not_a_date_time", !p_sv3.is_infinity() && p_sv3.is_not_a_date_time());
136
137 check("special_ptime + date_duration", p_sv1 + dd == p_sv1);
138 check("ptime - special_date_duration", pt - dd_pi == p_sv2);
139 check("ptime - special_date_duration", pt - dd_ndt == p_sv3);
140
141 check("special_ptime + time_duration", p_sv2 + td == p_sv2);
142 check("special_ptime - time_duration", pt - td_ni == p_sv1);
143 check("ptime + special_time_duration", pt + td_ndt == p_sv3);
144 check("ptime - special_ptime", pt - p_sv1 == td_ni);
145 check("ptime - special_ptime", pt - p_sv2 == td_pi);
146 check("ptime - special_ptime", pt - p_sv3 == td_ndt);
147 check("special_ptime - special_ptime", p_sv2 - p_sv2 == td_ndt);
148 }
149 }
150
151 int
152 main()
153 {
154 using namespace boost::posix_time;
155 using namespace boost::gregorian;
156
157 date d(2001,Dec,1);
158 time_duration td(5,4,3);
159 ptime t1(d, td); //2001-Dec-1 05:04:03
160 check("date part check", t1.date() == d);
161 check("time part check", t1.time_of_day() == td);
162 check("ptime with more than 24 hours", ptime(date(2005,10,30), hours(25)) == ptime(date(2005,10,31),hours(1)));
163 ptime t2(t1); //copy constructor
164 ptime t3 = t2; //assignment
165 check("date part check", t3.date() == d);
166 check("time part check", t3.time_of_day() == td);
167 check("equality", t1 == t3);
168 date d2(2001,Jan,1);
169 ptime t4(d2, td); //2001-Jan-1 05:04:03
170 check("equality - not equal", !(t1 == t4));
171 time_duration td1(5,4,0);
172 ptime t5(d, td1); //2001-Dec-1 05:04:00
173 check("equality - not equal", !(t1 == t5));
174 check("not equal - not equal", t1 != t5);
175
176 check("less - not less", !(t1 < t1));
177 check("less - less", t4 < t1);
178 check("less - less", t5 < t1);
179 check("less equal - equal", t1 <= t1);
180 check("greater equal - equal", t1 >= t1);
181
182 date_duration twodays(2);
183 ptime t6 = t1 + twodays;
184 date d3(2001,Dec,3);
185 check("operator+(date_duration)", t6 == ptime(d3,td));
186 ptime t7 = t1 - twodays;
187 check("operator-(date_duration)", t7 == ptime(date(2001,Nov,29),td));
188 {
189 ptime t6b(date(2003,Oct,31),time_duration(10,0,0,0));
190 t6b += date_duration(55);
191 check("operator +=(date_duration)", t6b ==
192 ptime(date(2003,Dec,25), time_duration(10,0,0,0)));
193 t6b += hours(6);
194 check("operator +=(time_duration)", t6b ==
195 ptime(date(2003,Dec,25), time_duration(16,0,0,0)));
196 t6b -= date_duration(55);
197 check("operator -=(date_duration)", t6b ==
198 ptime(date(2003,Oct,31), time_duration(16,0,0,0)));
199 t6b -= hours(6);
200 check("operator -=(time_duration)", t6b ==
201 ptime(date(2003,Oct,31), time_duration(10,0,0,0)));
202 t6b += hours(25);
203 check("operator +=(time_duration, more than 24 hours)", t6b ==
204 ptime(date(2003,Nov,1), time_duration(11,0,0,0)));
205 t6b -= hours(49);
206 check("operator -=(time_duration, more than 48 hours)", t6b ==
207 ptime(date(2003,Oct,30), time_duration(10,0,0,0)));
208 }
209 time_duration td2(1,2,3);
210 ptime t8(date(2001,Dec,1)); //midnight
211 ptime t9 = t8 + td2; //Dec 2 at 01:02:03
212 ptime t10(date(2001,Dec,1),time_duration(1,2,3));
213 std::cout << to_simple_string(t9) << std::endl;
214 std::cout << to_simple_string(t10) << std::endl;
215 std::cout << to_simple_string(td2) << std::endl;
216 check("add 2001-Dec-01 0:0:0 + 01:02:03", t9 == t10);
217 {
218 ptime t9x(date(2001,Dec,1), time_duration(12,0,0)); //Dec 1 at Noon
219 time_duration td3(-4,0,0);
220 check("add 2001-Dec-01 12:00:00 + (-04:00:00)",
221 t9x+td3 == ptime(date(2001,Dec,1), time_duration(8,0,0)) );
222 std::cout << to_simple_string(t9x-td3) << std::endl;
223 }
224 time_duration td3(24,0,0); // a day
225 check("add 2001-Dec-01 0:0:0 + 24:00:00", t8+td3 == ptime(date(2001,Dec,2)));
226 time_duration td4(24,0,1); // a day, 1 second
227 check("add 2001-Dec-01 0:0:0 + 24:00:01", t8+td4
228 == ptime(date(2001,Dec,2), time_duration(0,0,1)));
229 //looks like this fails b/c limits are exceeded now that we have subseconds..
230 time_duration td5(168,0,1); //one week 24X7
231 check("add 2001-Dec-01 0:0:0 + 168:00:01", t8+td5
232 == ptime(date(2001,Dec,8), time_duration(0,0,1)));
233
234 // ptime t10a = t8+td5;
235 // std::cout << to_simple_string(t10a) << std::endl;
236
237 //Subtraction of time duration -- add more!!
238 ptime t11(date(2001,Dec,1), time_duration(12,0,0)); //noon
239 time_duration td6(12,0,1);
240 ptime t12 = t11-td6;
241 check("sub 2001-Dec-01 12:0:0 - 12:00:01",
242 t12 == ptime(date(2001,Nov,30), time_duration(23,59,59)));
243
244 check("sub 2001-Dec-01 12:0:0 - 13:00:00",
245 (t11-time_duration(13,0,0))== ptime(date(2001,Nov,30),
246 time_duration(23,0,0)));
247 check("sub 2001-Dec-01 12:0:0 - (-13:00:00)",
248 (t11-time_duration(-13,0,0))== ptime(date(2001,Dec,2),
249 time_duration(1,0,0)));
250 // std::cout << to_simple_string(t12.date()) << std::endl;
251
252 ptime t13(d, hours(3));
253 ptime t14(d, hours(4));
254 ptime t14a(d+date_duration(1), hours(4));
255 //Subtract 2 times
256 std::cout << to_simple_string(t14-t13) << std::endl;
257 // time_duration td7 =
258 check("time subtraction positive result",
259 t14-t13 == hours(1));
260 std::cout << to_simple_string(t13-t14) << std::endl;
261 check("time subtraction negative result",
262 t13-t14 == hours(-1));
263 check("time subtraction positive result",
264 t14a-t14 == hours(24));
265
266
267 ptime t15(d, time_duration(0,0,0,1));
268 ptime t16(d, time_duration(0,0,0,2));
269 check("time subsecond add test",
270 t15 + time_duration::unit() == t16);
271 check("time subsecond sub test",
272 t16 - time_duration::unit() == t15);
273
274 ptime t17 = ptime(d) - time_duration::unit();
275 std::cout << to_simple_string(t17) << std::endl;
276
277 ptime t18(d, hours(25));
278 std::cout << to_simple_string(t18) << std::endl;
279
280 //time_t conversions:
281 t18 = from_time_t(0); //1970-1-1 0:0:0
282 check("time_t conversion of 0", t18 == ptime(date(1970,1,1)));
283 check("time_t conversion from 0", to_time_t(t18) == 0);
284
285 std::time_t tt(500000000);
286 t18 = from_time_t(tt); //1985-11-5 0:53:20
287 check("time_t conversion of 500000000",
288 t18 == ptime(date(1985,11,5), time_duration(0,53,20)));
289 check("time_t conversion from 500000000", to_time_t(t18) == tt);
290
291 std::time_t tt1(1060483634);
292 t18 = from_time_t(tt1); //2003-08-10 2:47:14
293 check("time_t conversion of 1060483634",
294 t18 == ptime(date(2003,8,10), time_duration(2,47,14)));
295 check("time_t conversion from 1060483634", to_time_t(t18) == tt1);
296
297 std::time_t tt2(1760483634);
298 t18 = from_time_t(tt2); //2025-10-14 23:13:54
299 check("time_t conversion of 1760483634",
300 t18 == ptime(date(2025,10,14), time_duration(23,13,54)));
301 check("time_t conversion from 1760483634", to_time_t(t18) == tt2);
302
303 std::time_t tt3(1960483634);
304 t18 = from_time_t(tt3); //2032-2-15 18:47:14
305 check("time_t conversion of 1960483634",
306 t18 == ptime(date(2032,2,15), time_duration(18,47,14)));
307 check("time_t conversion from 1960483634", to_time_t(t18) == tt3);
308
309 special_values_tests();
310
311 //min and max constructors
312 ptime min_ptime(min_date_time);
313 check("check min time constructor", min_ptime == ptime(date(1400,1,1), time_duration(0,0,0,0)));
314 // std::cout << min_ptime << std::endl;
315 ptime max_ptime(max_date_time);
316 check("check max time constructor", max_ptime == ptime(date(9999,12,31),
317 hours(24)-time_duration::unit()));
318 // std::cout << max_ptime << std::endl;
319
320 //tm conversion checks
321 //converts to date and back -- should get same result -- note no fractional seconds in these times
322 check("tm conversion functions 2001-12-1 05:04:03", ptime_from_tm(to_tm(t1)) == t1);
323 check("tm conversion functions min date 1400-1-1 ", ptime_from_tm(to_tm(min_ptime)) == min_ptime);
324 //this conversion will drop fractional seconds
325 check("tm conversion functions max date 9999-12-31 23:59:59.9999 - truncated frac seconds",
326 ptime_from_tm(to_tm(max_ptime)) == ptime(date(max_date_time), time_duration(23,59,59)));
327
328 try{
329 ptime pt(pos_infin);
330 tm pt_tm = to_tm(pt);
331 check("Exception not thrown (special_value to_tm)", false);
332 //following code does nothing useful but stops compiler from complaining about unused pt_tm
333 std::cout << pt_tm.tm_sec << std::endl;
334 }catch(std::out_of_range&){
335 check("Caught expected exception (special_value to_tm)", true);
336 }catch(...){
337 check("Caught un-expected exception (special_value to_tm)", false);
338 }
339 try{
340 // exception is only thrown from gregorian::to_tm. Needed to
341 // be sure it always gets thrown.
342 ptime pt(date(2002,Oct,31), hours(1));
343 pt += time_duration(pos_infin);
344 tm pt_tm = to_tm(pt);
345 check("Exception not thrown (special_value to_tm)", false);
346 //following code does nothing useful but stops compiler from complaining about unused pt_tm
347 std::cout << pt_tm.tm_sec << std::endl;
348 }catch(std::out_of_range&){
349 check("Caught expected exception (special_value to_tm)", true);
350 }catch(...){
351 check("Caught un-expected exception (special_value to_tm)", false);
352 }
353
354
355 return printTestStats();
356
357 }
358