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