testdate.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /* Copyright (c) 2002,2003 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 <boost/cstdint.hpp>
  8. #include "boost/date_time/gregorian/gregorian.hpp"
  9. #include "../testfrmwk.hpp"
  10. #include <iostream>
  11. #include <sstream>
  12. void test_yearlimit(int yr, bool allowed)
  13. {
  14. std::stringstream sdesc;
  15. sdesc << "should" << (allowed ? "" : " not") << " be able to make a date in year " << yr;
  16. try {
  17. boost::gregorian::date chkyr(yr, 1, 1);
  18. check(sdesc.str(), allowed);
  19. }
  20. catch (std::out_of_range&) { check(sdesc.str(), !allowed); }
  21. }
  22. int
  23. main()
  24. {
  25. using namespace boost::gregorian;
  26. //various constructors
  27. #if !defined(DATE_TIME_NO_DEFAULT_CONSTRUCTOR)
  28. date def;
  29. check("Default constructor", def == date(not_a_date_time));
  30. #endif
  31. date d1(1900,1,1);
  32. date d2 = date(2000,1,1);
  33. date d3(d2);
  34. check("Copy constructor", d3 == d2);
  35. date d4(2000,12,31);
  36. date d4a(2000,Dec,31);
  37. //d4a.print(std::cout); std::cout << std::endl;
  38. check("month_rep constructor", d4 == d4a);
  39. //std::cout << d3 << std::endl;
  40. //retrieval functions
  41. check_equal("1900-01-01 day is 01", d1.day(), 1);
  42. check_equal("1900-01-01 month is 01", d1.month(), 1);
  43. check_equal("1900-01-01 year is 1900", d1.year(), 1900);
  44. check_equal("2000-12-31 day is 31", d4.day(), 31);
  45. check_equal("2000-12-31 month is 12", d4.month(), 12);
  46. check_equal("2000-12-31 year is 2000", d4.year(), 2000);
  47. //operator<
  48. check("1900-01-01 is less than 2000-01-01", d1 < d2);
  49. check("2000-01-01 is NOT less than 2000-01-01", !(d1 < d1));
  50. //operator<=
  51. check("2000-01-01 is less equal than 2000-01-01", d1 <= d1);
  52. //operator>
  53. check("2000-01-01 is greater than 1900-01-01", d2 > d1);
  54. check("2000-01-01 is NOT greater than 2000-01-01", !(d1 < d1));
  55. //operator>=
  56. check("2000-01-01 is greater equal than 2000-01-01", d1 >= d1);
  57. //operator!=
  58. check("2000-01-01 is NOT equal to 1900-01-01", d2 != d1);
  59. //operator==
  60. check_equal("2000-01-01 is equal 2000-01-01", d3, d2);
  61. check("2000-01-01 is greater equal 2000-01-01", d3 >= d2);
  62. check("2000-01-01 is greater equal 2000-01-01", d3 <= d2);
  63. date::ymd_type ymd = d1.year_month_day();
  64. check_equal("ymd year", ymd.year, 1900);
  65. check_equal("ymd month", ymd.month, 1);
  66. check_equal("ymd day", ymd.day, 1);
  67. //The max function will not compile with Borland 5.5
  68. //Complains about must specialize basic_data<limits> ???
  69. // std::cout << "Max date is " << (date::max)() << std::endl;
  70. // //std::cout << "Max date is " << (basic_date< date_limits<unsigned int,1900> >::max)() << std::endl;
  71. // //std::cout << "Max date is " << (date_limits<unsigned int, 1900>::max)() << std::endl;
  72. const date answers[] = {date(1900,Jan,1),date(1900,Jan,4),date(1900,Jan,7),
  73. date(1900,Jan,10),date(1900,Jan,13)};
  74. date_duration off(3);
  75. date d5(1900,1,1);
  76. for (int i=0; i < 5; ++i) {
  77. //std::cout << d5 << " ";
  78. check(" addition ", d5 == answers[i]);
  79. d5 = d5 + off;
  80. }
  81. std::cout << std::endl;
  82. const date answers1[] = {date(2000,2,26),date(2000,2,28),date(2000,Mar,1)};
  83. date d8(2000,Feb,26);
  84. for (int j=0; j < 3; ++j) {
  85. //std::cout << d8 << " ";
  86. check(" more addition ", d8 == answers1[j]);
  87. d8 = d8 + days(2);
  88. }
  89. // std::cout << std::endl;
  90. date d6(2000,2,28);
  91. date d7(2000,3,1);
  92. date_duration twoDays(2);
  93. date_duration negtwoDays(-2);
  94. date_duration zeroDays(0);
  95. check_equal("2000-03-01 - 2000-02-28 == 2 days", twoDays, (d7-d6));
  96. check_equal("2000-02-28 - 2000-03-01 == - 2 days", negtwoDays, (d6-d7));
  97. check_equal("2000-02-28 - 2000-02-28 == 0 days", zeroDays, (d6-d6));
  98. check_equal("2000-02-28 + 2 days == 2000-03-01 ", d6 + twoDays, d7);
  99. check_equal("2000-03-01 - 2 days == 2000-02-28 ", d7 - twoDays, d6);
  100. check_equal("Add duration to date", date(1999,1,1) + date_duration(365), date(2000,1,1));
  101. check_equal("Add zero days", date(1999,1,1) + zeroDays, date(1999,1,1));
  102. //can't do this...
  103. //check("Add date to duration", date_duration(365) + date(1999,1,1) == date(2000,1,1));
  104. {
  105. date d(2003,Oct,31);
  106. date_duration dd(55);
  107. d += dd;
  108. check("date += date_duration", d == date(2003,Dec,25));
  109. d -= dd;
  110. check("date -= date_duration", d == date(2003,Oct,31));
  111. /* special_values is more thoroughly tested later,
  112. * this is just a test of += & -= with special values */
  113. d += date_duration(pos_infin);
  114. check("date += inf_dur", d == date(pos_infin));
  115. d -= dd;
  116. check("inf_date -= dur", d == date(pos_infin));
  117. }
  118. {
  119. date d(2003,Oct,31);
  120. date_duration dd1(pos_infin), dd2(neg_infin), dd3(not_a_date_time);
  121. check_equal("date + inf_dur", d + dd1, date(pos_infin));
  122. check_equal("date + inf_dur", d + dd2, date(neg_infin));
  123. check_equal("date + nan_dur", d + dd3, date(not_a_date_time));
  124. check_equal("date - inf_dur", d - dd1, date(neg_infin));
  125. check_equal("date - inf_dur", d - dd2, date(pos_infin));
  126. check_equal("date - nan_dur", d - dd3, date(not_a_date_time));
  127. check_equal("inf_date + inf_dur", date(pos_infin) + dd1, date(pos_infin));
  128. check_equal("inf_date - inf_dur", date(pos_infin) - dd1, date(not_a_date_time));
  129. check_equal("inf_date + inf_dur", date(neg_infin) + dd1, date(not_a_date_time));
  130. check_equal("inf_date - inf_dur", date(neg_infin) - dd1, date(neg_infin));
  131. }
  132. try {
  133. date d9(2000, Jan, 32);
  134. check("day out of range", false);
  135. //never reached if working -- but stops compiler warnings :-)
  136. std::cout << "Oops: " << to_iso_string(d9) << std::endl;
  137. }
  138. catch (bad_day_of_month&) {
  139. check("day out of range", true);
  140. }
  141. try {
  142. date d9(2000, Jan, 0);
  143. check("day out of range", false);
  144. //never reached if working -- but stops compiler warnings :-)
  145. std::cout << "Oops: " << to_iso_string(d9) << std::endl;
  146. }
  147. catch (bad_day_of_month&) {
  148. check("day out of range", true);
  149. }
  150. try {
  151. date d20(2000, Feb, 31);
  152. check("day out of range", false);
  153. //never reached if working -- but stops compiler warnings :-)
  154. std::cout << "Oops: " << to_iso_string(d20) << std::endl;
  155. }
  156. catch (bad_day_of_month&) {
  157. check("day out of range", true);
  158. }
  159. //more subtle -- one day past in a leap year
  160. try {
  161. date d21(2000, Feb, 30);
  162. check("day out of range", false);
  163. //never reached if working -- but stops compiler warnings :-)
  164. std::cout << "Oops: " << to_iso_string(d21) << std::endl;
  165. }
  166. catch (bad_day_of_month&) {
  167. check("day out of range", true);
  168. }
  169. //more subtle -- one day past in a leap year
  170. try {
  171. date d22(2000, Feb, 29);
  172. check("last day of month ok", true);
  173. std::cout << to_iso_string(d22) << std::endl; //stop compiler warning
  174. }
  175. catch (bad_day_of_month&) {
  176. check("last day of month -- oops bad exception", false);
  177. }
  178. //Not a leap year -- now Feb 29 is bad
  179. try {
  180. date d23(1999, Feb, 29);
  181. check("day out of range", false);
  182. //never reached if working -- but stops compiler warnings :-)
  183. std::cout << "Oops: " << to_iso_string(d23) << std::endl;
  184. }
  185. catch (bad_day_of_month&) {
  186. check("day out of range", true);
  187. }
  188. //check out some special values
  189. check("check not a date - false", !d7.is_not_a_date());
  190. check("check positive infinity - false", !d7.is_pos_infinity());
  191. check("check negative infinity - false", !d7.is_neg_infinity());
  192. date d10(neg_infin);
  193. check("check negative infinity - true", d10.is_infinity());
  194. d10 = d10 + twoDays; //still neg infinity
  195. check("check negative infinity - true", d10.is_neg_infinity());
  196. date d11(pos_infin);
  197. check("check positive infinity - true", d11.is_infinity());
  198. d11 = d11 + twoDays;
  199. check("check positive infinity add - true", d11.is_pos_infinity());
  200. date d12(not_a_date_time);
  201. check("check not a date", d12.is_not_a_date());
  202. check("check infinity compare ", d10 != d11);
  203. check("check infinity compare ", d10 < d11);
  204. check("check infinity nad compare ", d12 != d11);
  205. date d13(max_date_time);
  206. check("check infinity - max compare ", d13 < d11);
  207. check_equal("max date_time value ", d13, date(9999,Dec, 31));
  208. std::cout << to_simple_string(d13) << std::endl;
  209. date d14(min_date_time);
  210. check("check infinity - min compare ", d14 > d10);
  211. std::cout << to_simple_string(d14) << std::endl;
  212. check_equal("min date_time value ", d14, date(1400,Jan, 1));
  213. date d15(1400,1,1);
  214. std::cout << d15.day_of_week().as_long_string() << std::endl;
  215. check("check infinity - min compare ", d10 < d15);
  216. // most of this testing is in the gregorian_calendar tests
  217. std::cout << d15.julian_day() << std::endl;
  218. check_equal("check julian day ", d15.julian_day(),
  219. static_cast<boost::uint32_t>(2232400));
  220. check_equal("check modjulian day ", d15.modjulian_day(), -167601);
  221. date d16(2004,2,29);
  222. check_equal("check julian day ", d16.julian_day(),
  223. static_cast<boost::uint32_t>(2453065));
  224. check_equal("check modjulian day ", d16.modjulian_day(),
  225. static_cast<boost::uint32_t>(53064));
  226. // most of this testing is in the gregorian_calendar tests
  227. date d31(2000, Jun, 1);
  228. check_equal("check iso week number ", d31.week_number(), 22);
  229. date d32(2000, Aug, 1);
  230. check_equal("check iso week number ", d32.week_number(), 31);
  231. date d33(2000, Oct, 1);
  232. check_equal("check iso week number ", d33.week_number(), 39);
  233. date d34(2000, Dec, 1);
  234. check_equal("check iso week number ", d34.week_number(), 48);
  235. date d35(2000, Dec, 24);
  236. check_equal("check iso week number ", d35.week_number(), 51);
  237. date d36(2000, Dec, 25);
  238. check_equal("check iso week number ", d36.week_number(), 52);
  239. date d37(2000, Dec, 31);
  240. check_equal("check iso week number ", d37.week_number(), 52);
  241. date d38(2001, Jan, 1);
  242. check_equal("check iso week number ", d38.week_number(), 1);
  243. try {
  244. int dayofyear1 = d38.day_of_year();
  245. check_equal("check day of year number", dayofyear1, 1);
  246. check_equal("check day of year number", d37.day_of_year(), 366);
  247. date d39(2001,Dec,31);
  248. check_equal("check day of year number", d39.day_of_year(), 365);
  249. date d40(2000,Feb,29);
  250. check_equal("check day of year number", d40.day_of_year(), 60);
  251. date d41(1400,Jan,1);
  252. check_equal("check day of year number", d41.day_of_year(), 1);
  253. date d42(1400,Jan,1);
  254. check_equal("check day of year number", d42.day_of_year(), 1);
  255. date d43(2002,Nov,17);
  256. check_equal("check day of year number", d43.day_of_year(), 321);
  257. }
  258. catch(std::exception& e) {
  259. std::cout << e.what() << std::endl;
  260. check("check day of year number", false);
  261. }
  262. //converts to date and back -- should get same result
  263. check_equal("tm conversion functions 2000-1-1", date_from_tm(to_tm(d2)), d2);
  264. check_equal("tm conversion functions 1900-1-1", date_from_tm(to_tm(d1)), d1);
  265. check_equal("tm conversion functions min date 1400-1-1 ", date_from_tm(to_tm(d14)), d14);
  266. check_equal("tm conversion functions max date 9999-12-31", date_from_tm(to_tm(d13)), d13);
  267. try{
  268. date d(neg_infin);
  269. tm d_tm = to_tm(d);
  270. check("Exception not thrown (special_value to_tm)", false);
  271. std::cout << d_tm.tm_sec << std::endl; //does nothing useful but stops compiler from complaining about unused d_tm
  272. }catch(std::out_of_range&){
  273. check("Caught expected exception (special_value to_tm)", true);
  274. }catch(...){
  275. check("Caught un-expected exception (special_value to_tm)", false);
  276. }
  277. // trac-13159
  278. test_yearlimit( 0, false);
  279. test_yearlimit( 1399, false);
  280. test_yearlimit( 1400, true);
  281. test_yearlimit( 1401, true);
  282. test_yearlimit( 9999, true);
  283. test_yearlimit(10000, false);
  284. test_yearlimit(10001, false);
  285. return printTestStats();
  286. }