testtime.cpp 14 KB

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