testduration.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /* Copyright (c) 2002-2004 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. * $Date$
  7. */
  8. #include "boost/date_time/posix_time/posix_time_duration.hpp"
  9. #include "boost/date_time/compiler_config.hpp"
  10. #if defined(BOOST_DATE_TIME_INCLUDE_LIMITED_HEADERS)
  11. #include "boost/date_time/posix_time/time_formatters_limited.hpp"
  12. #else
  13. #include "boost/date_time/posix_time/time_formatters.hpp"
  14. #endif
  15. #include "../testfrmwk.hpp"
  16. int
  17. main()
  18. {
  19. using namespace boost::posix_time;
  20. // std::cout << "Default limits: SECOND " << std::endl;
  21. {
  22. time_duration td;
  23. check("default construction -- 0 ticks", td.ticks() == 0);
  24. // check("default construction -- 0 secs", td.seconds() == 0);
  25. // check("default construction -- 0 min", td.minutes() == 0);
  26. }
  27. // construct from components
  28. time_duration td1(1,25,0);
  29. time_duration td3(td1.hours(),td1.minutes(),td1.seconds());
  30. check("total up elements", td1 == td3);
  31. td1 = -td1; // td1 == "-1:25:00"
  32. check("invert_sign",td3 == td1.invert_sign());
  33. check("invert_sign",td1 == td3.invert_sign());
  34. check("abs",td3 == td1.abs());
  35. check("abs",td3 == td3.abs());
  36. check("is_positive",td3.is_positive());
  37. check("is_positive",!td1.is_positive());
  38. check("is_negative",td1.is_negative());
  39. check("is_negative",!td3.is_negative());
  40. check("is_zero",!td1.is_zero());
  41. check("is_zero",(td1 - td1).is_zero());
  42. td3 = time_duration(td1.hours(),td1.minutes(),td1.seconds());
  43. check("total up elements-inverted sign", td1 == td3);
  44. time_duration t_1(0,1,40);
  45. time_duration t_2(0,1,41);
  46. check("less test", !(t_2 < t_2));
  47. check("equal test", t_1 == t_1);
  48. check("greater equal - equal", t_1 >= t_1);
  49. check("greater equal - greater", t_2 >= t_1);
  50. check("less equal - equal ", t_2 <= t_2);
  51. check("greater ", t_2 > t_1);
  52. check("greater - not ", !(t_1 > t_2));
  53. time_duration t_3(t_2);
  54. check("copy constructor ", t_2 == t_3);
  55. time_duration t_4 = t_3;
  56. check("assignment operator ", t_2 == t_4);
  57. time_duration t_5(1,30,20,10); // 1hr, 30min, 20sec, 10 frac sec
  58. t_5 /= 2;
  59. check("divide equal", (t_5.hours() == 0 &&
  60. t_5.minutes() == 45 &&
  61. t_5.seconds() == 10 &&
  62. t_5.fractional_seconds() == 5));
  63. t_5 = time_duration(3,15,8,0) / 2;
  64. check("divide int", t_5 == time_duration(1,37,34,0));
  65. {
  66. time_duration td = hours(5);
  67. td *= 5;
  68. check("mult-equals int", time_duration(25,0,0,0) == td);
  69. }
  70. t_5 = t_2 + t_1;
  71. //VC6 goes ambiguous on the next line...
  72. #if (defined(BOOST_MSVC) && (_MSC_VER < 1300))
  73. //sorry ticks() doesn't work on VC6
  74. #else
  75. std::cout << t_5.ticks() << std::endl;
  76. #endif
  77. check("add", t_5 == time_duration(0,3,21));
  78. time_duration td_a(5,5,5,5);
  79. time_duration td_b(4,4,4,4);
  80. time_duration td_c(2,2,2,2);
  81. td_a += td_b;
  82. check("add equal", td_a == time_duration(9,9,9,9));
  83. time_duration td_d = td_b - td_c;
  84. check("subtract", td_d == time_duration(2,2,2,2));
  85. td_d -= td_b;
  86. check("subtract equal (neg result)", td_d == td_c - td_b);
  87. time_duration utd(1,2,3,4);
  88. time_duration utd2 = -utd;
  89. //std::cout << td_d << '\n' << utd2 << std::endl;
  90. check("unary-", ((utd2.hours() == -1) &&
  91. (utd2.minutes() == -2) &&
  92. (utd2.seconds() == -3) &&
  93. (utd2.fractional_seconds() == -4)) );
  94. utd2 = -hours(5);
  95. check("unary-", utd2.hours() == -5);
  96. utd2 = -utd2;
  97. check("unary-", utd2.hours() == 5);
  98. time_duration t_6(5,4,3); //05:04:03
  99. check("h-m-s 5-4-3 hours", t_6.hours() == 5);
  100. check("h-m-s 5-4-3 minutes", t_6.minutes() == 4);
  101. check("h-m-s 5-4-3 seconds", t_6.seconds() == 3);
  102. std::cout << t_6.total_seconds() << std::endl;
  103. check("h-m-s 5-4-3 total_seconds", t_6.total_seconds() == 18243);
  104. hours tenhours(10);
  105. minutes fivemin(5);
  106. time_duration t7 = time_duration(1,2,3) + tenhours + fivemin;
  107. check("short hand durations add", t7 == time_duration(11,7,3));
  108. time_duration t8 = tenhours + time_duration(1,2,3) + fivemin;
  109. check("short hand durations add", t8 == time_duration(11,7,3));
  110. if (time_duration::resolution() >= boost::date_time::micro) {
  111. time_duration t_9(5,4,3,9876); //05:04:03.09876
  112. check("h-m-s 5-4-3.21 hours", t_9.hours() == 5);
  113. check("h-m-s 5-4-3.21 min ", t_9.minutes() == 4);
  114. check("h-m-s 5-4-3.21 sec ", t_9.seconds() == 3);
  115. check("h-m-s 5-4-3.21 fs ", t_9.fractional_seconds() == 9876);
  116. check("h-m-s 5-4-3.21 total_seconds", t_9.total_seconds() == 18243);
  117. // check("h-m-s 5-4-3.21 fs ", t_9.fs_as_double() == 0.9876);
  118. //std::cout << t_9.fs_as_double() << std::endl;
  119. std::cout << to_simple_string(t_9) << std::endl;
  120. }
  121. if (time_duration::resolution() >= boost::date_time::tenth) {
  122. time_duration t_10(5,4,3,9); //05:04:03.00001
  123. check("h-m-s 5-4-3.9 hours", t_10.hours() == 5);
  124. check("h-m-s 5-4-3.9 min ", t_10.minutes() == 4);
  125. check("h-m-s 5-4-3.9 sec ", t_10.seconds() == 3);
  126. check("h-m-s 5-4-3.9 fs ", t_10.fractional_seconds() == 9);
  127. check("h-m-s 5-4-3.9 total_seconds", t_10.total_seconds() == 18243);
  128. std::cout << to_simple_string(t_10) << std::endl;
  129. }
  130. if (time_duration::resolution() >= boost::date_time::milli) {
  131. millisec ms(9);
  132. // time_duration t_10(0,0,0,); //00:00:00.009
  133. std::cout << "time_resolution: " << time_duration::resolution() << std::endl;
  134. #if (defined(BOOST_MSVC) && (_MSC_VER < 1300))
  135. //sorry res_adjust() doesn't work on VC6
  136. #else
  137. std::cout << "res_adjust " << time_res_traits::res_adjust() << std::endl;
  138. #endif
  139. if (time_duration::resolution() == boost::date_time::nano) {
  140. check("millisec", ms.fractional_seconds() == 9000000);
  141. check("total_seconds - nofrac", ms.total_seconds() == 0);
  142. check("total_millisec", ms.total_milliseconds() == 9);
  143. check("ticks per second", time_duration::ticks_per_second() == 1000000000);
  144. }
  145. else {
  146. check("millisec 9000", ms.fractional_seconds() == 9000);
  147. check("total_seconds - nofrac", ms.total_seconds() == 0);
  148. check("total_millisec", ms.total_milliseconds() == 9);
  149. }
  150. }
  151. #ifdef BOOST_DATE_TIME_HAS_NANOSECONDS
  152. if (time_duration::resolution() >= boost::date_time::nano) {
  153. nanosec ns(9);
  154. // time_duration t_10(0,0,0,); //00:00:00.00009
  155. check("nanosec", ns.fractional_seconds() == 9);
  156. check("total nanosec", ns.total_nanoseconds() == 9);
  157. check("total microsec - truncated", ns.total_microseconds() == 0);
  158. std::cout << to_simple_string(ns) << std::endl;
  159. time_duration ns18 = ns + ns;
  160. check("nanosec", ns18.fractional_seconds() == 18);
  161. std::cout << to_simple_string(ns18) << std::endl;
  162. nanosec ns2(1000000000); //one second
  163. check("nano to second compare", ns2 == seconds(1));
  164. check("check total seconds", ns2.total_seconds() == 1);
  165. std::cout << to_simple_string(ns2) << std::endl;
  166. check("division of nanoseconds", (nanosec(3)/2) == nanosec(1));
  167. check("multiplication of nanosec", nanosec(3)*1000 == microsec(3));
  168. }
  169. #endif
  170. // Test for overflows (ticket #3471)
  171. {
  172. ptime start(boost::gregorian::date(2000, 1, 1));
  173. ptime end(boost::gregorian::date(2000, 5, 1));
  174. time_duration td = end - start;
  175. ptime end2 = start + microseconds(td.total_microseconds());
  176. check("microseconds constructor overflow", end == end2);
  177. }
  178. time_duration t_11(3600,0,0);
  179. check("3600 hours ", t_11.hours() == 3600);
  180. check("total seconds 3600 hours", t_11.total_seconds() == 12960000);
  181. time_duration td_12(1,2,3,10);
  182. std::cout << td_12.total_seconds() << std::endl;
  183. check("total seconds 3723 hours", td_12.total_seconds() == 3723);
  184. // time_duration t_11a = t_11/time_duration(0,3,0);
  185. check("division", (hours(2)/2) == hours(1));
  186. check("division", (hours(3)/2) == time_duration(1,30,0));
  187. check("division", (hours(3)/3) == hours(1));
  188. check("multiplication", time_duration(3,0,0)*2 == hours(6));
  189. check("multiplication", hours(360)*1000 == hours(360000));
  190. // special_values operations
  191. time_duration pi_dur(pos_infin), ni_dur(neg_infin), ndt_dur(not_a_date_time);
  192. check("+infin + -infin", pi_dur + ni_dur == ndt_dur);
  193. check("infin / int", pi_dur / 3 == pi_dur);
  194. check("infin + duration", pi_dur + td_12 == pi_dur);
  195. check("infin - duration", pi_dur - td_12 == pi_dur);
  196. check("unary-", -pi_dur == ni_dur);
  197. check("-infin less than +infin", ni_dur < pi_dur);
  198. check("-infin less than duration", ni_dur < td_12);
  199. check("+infin greater than duration", pi_dur > td_12);
  200. std::string result(""), answer("+infinity");
  201. result = to_simple_string(pi_dur);
  202. check("to string +infin", result==answer);
  203. result = to_simple_string(ni_dur);
  204. answer = "-infinity";
  205. check("to string +infin", result==answer);
  206. result = to_simple_string(ndt_dur);
  207. //answer = "not-a-number";
  208. answer = "not-a-date-time";
  209. check("to string +infin", result==answer);
  210. using namespace boost::gregorian;
  211. ptime t1(date(2001,7,14));
  212. ptime t2(date(2002,7,14));
  213. check("One year of hours: 365*24=8760", 365*24 == ((t2-t1).hours()));
  214. check("Total seconds in a year", 365*24*3600 == ((t2-t1).total_seconds()));
  215. std::cout << to_simple_string(time_duration(20000 * 24, 0, 0, 0)) << std::endl;
  216. std::cout << to_simple_string(time_duration(24855 * 24, 0, 0, 0)) << std::endl;
  217. std::cout << to_simple_string(time_duration(24856 * 24, 0, 0, 0)) << std::endl;
  218. std::cout << to_simple_string(time_duration(25000 * 24, 0, 0, 0)) << std::endl;
  219. time_duration tdl1(25000*24, 0, 0, 0);
  220. check("600000 hours", tdl1.hours() == 600000);
  221. time_duration tdl2(2000000, 0, 0, 0);
  222. check("2000000 hours", tdl2.hours() == 2000000);
  223. check("total milliseconds", seconds(1).total_milliseconds() == 1000);
  224. check("total microseconds", seconds(1).total_microseconds() == 1000000);
  225. check("total nanoseconds", seconds(1).total_nanoseconds() == 1000000000);
  226. check("total milliseconds", hours(1).total_milliseconds() == 3600*1000);
  227. boost::int64_t tms = static_cast<boost::int64_t>(3600)*1000000*1001; //ms per sec
  228. check("total microseconds 1000 hours", hours(1001).total_microseconds() == tms);
  229. tms = static_cast<boost::int64_t>(3600)*365*24*1000;
  230. check("total milliseconds - one year", (t2-t1).total_milliseconds() == tms);
  231. tms = 3600*365*24*static_cast<boost::int64_t>(1000000000);
  232. check("total nanoseconds - one year", (t2-t1).total_nanoseconds() == tms);
  233. #if (defined(BOOST_MSVC) && (_MSC_VER < 1300))
  234. #else
  235. std::cout << "tms: " << (t2-t1).total_milliseconds() << std::endl;
  236. std::cout << "nano per year: " << (t2-t1).total_nanoseconds() << std::endl;
  237. #endif
  238. // make it into a double
  239. double d1 = microseconds(25).ticks()/(double)time_duration::ticks_per_second();
  240. std::cout << d1 << std::endl;
  241. //Following causes errors on several compilers about value to large for
  242. //type. So it is commented out for now. Strangely works on gcc 3.3
  243. //eg: integer constant out of range
  244. // long sec_in_200k_hours(7200000000);
  245. // check("total sec in 2000000 hours",
  246. // tdl2.total_seconds() == sec_in_200k_hours);
  247. // std::cout << to_simple_string(time_duration(2000000, 0, 0, 0)) << std::endl;
  248. return printTestStats();
  249. }