testparse_time.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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 "boost/date_time/posix_time/posix_time.hpp"
  8. #include "../testfrmwk.hpp"
  9. int
  10. main()
  11. {
  12. using namespace boost::gregorian;
  13. using namespace boost::posix_time;
  14. #ifdef BOOST_DATE_TIME_HAS_NANOSECONDS
  15. std::string s1("12:11:10.123456789");
  16. time_duration td1= duration_from_string(s1);
  17. check("parse time duration: " + s1,
  18. td1 == time_duration(12,11,10,123456789));
  19. std::string s2("12:11:10,123456789");
  20. time_duration td2= boost::date_time::parse_delimited_time_duration<time_duration>(s2);
  21. check("parse time duration: " + s2,
  22. td2 == time_duration(12,11,10,123456789));
  23. std::string s3("12:11:10");
  24. time_duration td3= boost::date_time::parse_delimited_time_duration<time_duration>(s3);
  25. check("parse time duration: " + s3,
  26. td3 == time_duration(12,11,10,0));
  27. std::string s4("23:59:59.000000001");
  28. time_duration td4= boost::date_time::parse_delimited_time_duration<time_duration>(s4);
  29. check("parse time duration: " + s4,
  30. td4 == time_duration(23,59,59)+nanosec(1));
  31. std::string s5("23:59:59.999999999");
  32. time_duration td5= boost::date_time::parse_delimited_time_duration<time_duration>(s5);
  33. check("parse time duration: " + s5,
  34. td5 == time_duration(23,59,59)+nanosec(999999999));
  35. std::string s5b("-23:59:59.999999999");
  36. time_duration td5b= boost::date_time::parse_delimited_time_duration<time_duration>(s5b);
  37. check("parse time duration: " + s5b,
  38. td5b == time_duration(-23,59,59)-nanosec(999999999));
  39. std::string s6b("1:00:00.1"); // we want 1/10th
  40. time_duration td6b= boost::date_time::parse_delimited_time_duration<time_duration>(s6b);
  41. check("parse time duration: " + s6b,
  42. td6b == time_duration(1,0,0)+nanosec(100000000)); // we want 1/10th
  43. std::string s7b("-1:00:00.0010"); // we want 1/1000th
  44. time_duration td7b= boost::date_time::parse_delimited_time_duration<time_duration>(s7b);
  45. check("parse time duration: " + s7b,
  46. td7b == time_duration(-1,0,0)-nanosec(1000000)); // we want 1/1000th
  47. std::string s8b("1:22:33.123456789321"); // too many digits
  48. time_duration td8b= boost::date_time::parse_delimited_time_duration<time_duration>(s8b);
  49. check("parse time duration: " + s8b,
  50. td8b == time_duration(1,22,33,123456789)); // excess digits should be dropped
  51. #endif
  52. #if defined(BOOST_DATE_TIME_HAS_MICROSECONDS) && (!defined(BOOST_DATE_TIME_HAS_NANOSECONDS))
  53. {
  54. std::string s1("12:11:10.123456");
  55. time_duration td1= duration_from_string(s1);
  56. check("parse time duration: " + s1,
  57. td1 == time_duration(12,11,10,123456));
  58. std::string s2("12:11:10,123456");
  59. time_duration td2= boost::date_time::parse_delimited_time_duration<time_duration>(s2);
  60. check("parse time duration: " + s2,
  61. td2 == time_duration(12,11,10,123456));
  62. std::string s3("12:11:10");
  63. time_duration td3= boost::date_time::parse_delimited_time_duration<time_duration>(s3);
  64. check("parse time duration: " + s3,
  65. td3 == time_duration(12,11,10,0));
  66. std::string s4("23:59:59.000001");
  67. time_duration td4= boost::date_time::parse_delimited_time_duration<time_duration>(s4);
  68. check("parse time duration: " + s4,
  69. td4 == time_duration(23,59,59)+microsec(1));
  70. std::string s5("23:59:59.999999");
  71. time_duration td5= boost::date_time::parse_delimited_time_duration<time_duration>(s5);
  72. check("parse time duration: " + s5,
  73. td5 == time_duration(23,59,59)+microsec(999999));
  74. std::string s5b("-23:59:59.999999");
  75. time_duration td5b= boost::date_time::parse_delimited_time_duration<time_duration>(s5b);
  76. check("parse time duration: " + s5b,
  77. td5b == time_duration(-23,59,59)-microsec(999999));
  78. std::string s6b("1:00:00.1"); // we want 1/10th
  79. time_duration td6b= boost::date_time::parse_delimited_time_duration<time_duration>(s6b);
  80. check("parse time duration: " + s6b,
  81. td6b == time_duration(1,0,0)+microsec(100000)); // we want 1/10th
  82. std::string s7b("-1:00:00.0010"); // we want 1/1000th
  83. time_duration td7b= boost::date_time::parse_delimited_time_duration<time_duration>(s7b);
  84. check("parse time duration: " + s7b,
  85. td7b == time_duration(-1,0,0)-microsec(1000)); // we want 1/1000th
  86. std::string s8b("1:22:33.123456321"); // too many digits
  87. time_duration td8b= boost::date_time::parse_delimited_time_duration<time_duration>(s8b);
  88. check("parse time duration: " + s8b,
  89. td8b == time_duration(1,22,33,123456)); // excess digits should be dropped
  90. }
  91. #endif
  92. #ifdef BOOST_DATE_TIME_HAS_NANOSECONDS
  93. {
  94. std::string ts2("2002-12-31 00:00:00.999999999");
  95. ptime t2 = time_from_string(ts2);
  96. check("parse time: " + ts2,
  97. t2 == ptime(date(2002,12,31),time_duration(0,0,0)+nanosec(999999999)));
  98. }
  99. {
  100. std::string ts2("2002-12-31 00:00:00.");
  101. ptime t2 = time_from_string(ts2);
  102. check("parse time (decimal but no digits): " + ts2,
  103. t2 == ptime(date(2002,12,31),time_duration(0,0,0)));
  104. }
  105. #endif
  106. std::string date_1, tod_1;
  107. std::string ts1("2002-01-20 23:59:59.000");
  108. boost::date_time::split(ts1, ' ', date_1, tod_1);
  109. check("split function date part of " + ts1,
  110. date_1 == std::string("2002-01-20"));
  111. check("time part of " + ts1,
  112. tod_1 == std::string("23:59:59.000"));
  113. // std::cout << date_1 << "|" << std::endl;
  114. // std::cout << tod_1 << "|" << std::endl;
  115. {
  116. ptime t1 = time_from_string(ts1);
  117. check("parse time: " + ts1,
  118. t1 == ptime(date(2002,1,20),time_duration(23,59,59)));
  119. }
  120. {
  121. std::string ts1x("2002-01-20 23:59:59.");
  122. ptime t1 = time_from_string(ts1x);
  123. check("parse time (decimal but no digits): " + ts1x,
  124. t1 == ptime(date(2002,1,20),time_duration(23,59,59)));
  125. }
  126. std::string s6("235859");
  127. time_duration td6= boost::date_time::parse_undelimited_time_duration<time_duration>(s6);
  128. check("parse time duration: " + s6,
  129. td6 == time_duration(23,58,59));
  130. s6 = "-235859";
  131. td6= boost::date_time::parse_undelimited_time_duration<time_duration>(s6);
  132. check("parse negative time duration: " + s6,
  133. td6 == time_duration(-23,58,59));
  134. {
  135. std::string ts3("20020120T235859");
  136. ptime t20 = from_iso_string(ts3);
  137. check("parse iso time: " + ts3,
  138. t20 == ptime(date(2002,1,20),time_duration(23,58,59)));
  139. }
  140. {
  141. std::string ts4("19001231T000000");
  142. ptime t21 = from_iso_string(ts4);
  143. check("parse iso time: " + ts4,
  144. t21 == ptime(date(1900,12,31),time_duration(0,0,0)));
  145. }
  146. {
  147. std::string ts5("19001231T23");
  148. ptime t22 = from_iso_string(ts5);
  149. check("parse iso time: " + ts5,
  150. t22 == ptime(date(1900,12,31),time_duration(23,0,0)));
  151. }
  152. {
  153. #if defined(BOOST_DATE_TIME_POSIX_TIME_STD_CONFIG)
  154. std::string ts3("20020120T235859.123456789");
  155. ptime t20 = from_iso_string(ts3);
  156. check("parse iso time w/ frac sec: " + ts3,
  157. t20 == ptime(date(2002,1,20),time_duration(23,58,59,123456789)));
  158. std::string ts4("19001231T000000.123");
  159. ptime t21 = from_iso_string(ts4);
  160. check("parse iso time w/ frac sec (too short): " + ts4,
  161. t21 == ptime(date(1900,12,31),time_duration(0,0,0,123000000)));
  162. std::string ts5("19001231T230000.123456789876");
  163. ptime t22 = from_iso_string(ts5);
  164. check("parse iso time w/ frac sec (too long): " + ts5,
  165. t22 == ptime(date(1900,12,31),time_duration(23,0,0,123456789)));
  166. std::string ts6("19001231T230000.");
  167. ptime t23 = from_iso_string(ts6);
  168. check("parse iso time w/ frac sec (dec only): " + ts6,
  169. t23 == ptime(date(1900,12,31),time_duration(23,0,0,0)));
  170. std::string ts7("2002-01-20T23:58:59.123456789");
  171. ptime t24 = from_iso_extended_string(ts7);
  172. check("parse iso extended time w/ frac sec: " + ts7,
  173. t24 == ptime(date(2002,1,20),time_duration(23,58,59,123456789)));
  174. std::string ts8("1900-12-31T00:00:00.123");
  175. ptime t25 = from_iso_extended_string(ts8);
  176. check("parse iso extended time w/ frac sec (too short): " + ts8,
  177. t25 == ptime(date(1900,12,31),time_duration(0,0,0,123000000)));
  178. std::string ts9("1900-12-31T23:00:00.123456789876");
  179. ptime t26 = from_iso_extended_string(ts9);
  180. check("parse iso extended time w/ frac sec (too long): " + ts9,
  181. t26 == ptime(date(1900,12,31),time_duration(23,0,0,123456789)));
  182. std::string ts10("1900-12-31T23:00:00.");
  183. ptime t27 = from_iso_extended_string(ts10);
  184. check("parse iso extended time w/ frac sec (dec only): " + ts10,
  185. t27 == ptime(date(1900,12,31),time_duration(23,0,0,0)));
  186. #else
  187. std::string ts3("20020120T235859.123456");
  188. ptime t20 = from_iso_string(ts3);
  189. check("parse iso time w/ frac sec: " + ts3,
  190. t20 == ptime(date(2002,1,20),time_duration(23,58,59,123456)));
  191. std::string ts4("19001231T000000.123");
  192. ptime t21 = from_iso_string(ts4);
  193. check("parse iso time w/ frac sec (too short): " + ts4,
  194. t21 == ptime(date(1900,12,31),time_duration(0,0,0,123000)));
  195. std::string ts5("19001231T230000.123456789876");
  196. ptime t22 = from_iso_string(ts5);
  197. check("parse iso time w/ frac sec (too long): " + ts5,
  198. t22 == ptime(date(1900,12,31),time_duration(23,0,0,123456)));
  199. std::string ts6("19001231T230000.");
  200. ptime t23 = from_iso_string(ts6);
  201. check("parse iso time w/ frac sec (dec only): " + ts6,
  202. t23 == ptime(date(1900,12,31),time_duration(23,0,0,0)));
  203. std::string ts7("2002-01-20T23:58:59.123456");
  204. ptime t24 = from_iso_extended_string(ts7);
  205. check("parse iso extended time w/ frac sec: " + ts7,
  206. t24 == ptime(date(2002,1,20),time_duration(23,58,59,123456)));
  207. std::string ts8("1900-12-31T00:00:00.123");
  208. ptime t25 = from_iso_extended_string(ts8);
  209. check("parse iso extended time w/ frac sec (too short): " + ts8,
  210. t25 == ptime(date(1900,12,31),time_duration(0,0,0,123000)));
  211. std::string ts9("1900-12-31T23:00:00.123456789876");
  212. ptime t26 = from_iso_extended_string(ts9);
  213. check("parse iso extended time w/ frac sec (too long): " + ts9,
  214. t26 == ptime(date(1900,12,31),time_duration(23,0,0,123456)));
  215. std::string ts10("1900-12-31T23:00:00.");
  216. ptime t27 = from_iso_extended_string(ts10);
  217. check("parse iso extended time w/ frac sec (dec only): " + ts10,
  218. t27 == ptime(date(1900,12,31),time_duration(23,0,0,0)));
  219. #endif // BOOST_DATE_TIME_POSIX_TIME_STD_CONFIG
  220. }
  221. std::string s7("-01:25:00"), s8("-00:40:00"), s9("0:45"), s10("0:-40");
  222. time_duration tds1 = duration_from_string(s7);
  223. time_duration tds2 = duration_from_string(s8);
  224. time_duration tds3 = duration_from_string(s9);
  225. time_duration tds4 = duration_from_string(s10);
  226. check("from string construct", tds1 == time_duration(-1,25,0));
  227. check("from string construct", tds2 == minutes(-40));
  228. check("from string construct", tds3 == minutes(45));
  229. // '-' in middle of string s10 should be ignored resulting in pos duration
  230. check("from string construct", tds4 == minutes(40));
  231. return printTestStats();
  232. }