teststreams.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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/date_time/posix_time/posix_time.hpp"
  8. #include "boost/date_time/gregorian/gregorian.hpp"
  9. #include "../testfrmwk.hpp"
  10. #ifndef BOOST_DATE_TIME_NO_LOCALE
  11. const char* const de_short_month_names[]={"Jan","Feb","Mar","Apr","Mai","Jun","Jul","Aug","Sep","Okt","Nov","Dez", "NAM"};
  12. const char* const de_long_month_names[]={"Januar","Februar","Marz","April","Mai","Juni","Juli","August","September","Oktober","November","Dezember","NichtDerMonat"};
  13. const char* const de_special_value_names[]={"NichtDatumzeit","-unbegrenztheit", "+unbegrenztheit"};
  14. const char* const de_short_weekday_names[]={"Son", "Mon", "Die","Mit", "Don", "Fre", "Sam"};
  15. const char* const de_long_weekday_names[]={"Sonntag", "Montag", "Dienstag","Mittwoch", "Donnerstag", "Freitag", "Samstag"};
  16. #endif
  17. int
  18. main()
  19. {
  20. #ifndef BOOST_DATE_TIME_NO_LOCALE
  21. using namespace boost::gregorian;
  22. using namespace boost::posix_time;
  23. std::stringstream ss;
  24. date d1(2002,May,1);
  25. ptime t1(d1, hours(12)+minutes(10)+seconds(5));
  26. time_duration td0(12,10,5,123);
  27. ptime t0(d1, td0);
  28. ss << t0;
  29. #ifdef BOOST_DATE_TIME_HAS_NANOSECONDS
  30. check("check time output: "+ss.str(),
  31. ss.str() == std::string("2002-May-01 12:10:05.000000123"));
  32. #else
  33. check("check time output: "+ss.str(),
  34. ss.str() == std::string("2002-May-01 12:10:05.000123"));
  35. #endif // BOOST_DATE_TIME_HAS_NANOSECONDS
  36. // ss.imbue(global);
  37. time_period tp(t1, ptime(d1, hours(23)+time_duration::unit()));
  38. ss.str("");
  39. ss << tp;
  40. check("check time period output: "+ ss.str(),
  41. ss.str() == std::string("[2002-May-01 12:10:05/2002-May-01 23:00:00]"));
  42. //Send out the same time with german
  43. typedef boost::date_time::all_date_names_put<greg_facet_config> date_facet;
  44. ss.imbue(std::locale(std::locale::classic(),
  45. new date_facet(de_short_month_names,
  46. de_long_month_names,
  47. de_special_value_names,
  48. de_short_weekday_names,
  49. de_long_weekday_names,
  50. '.',
  51. boost::date_time::ymd_order_dmy,
  52. boost::date_time::month_as_short_string)));
  53. ss.str("");
  54. ss << t1;
  55. check("check time output: "+ ss.str(),
  56. ss.str() == std::string("01.Mai.2002 12:10:05"));
  57. time_duration td(5,4,3);
  58. time_duration td1(-1,25,0), td2(0,-40,0);
  59. ss.str("");
  60. ss << td;
  61. check("check time period output: "+ ss.str(),
  62. ss.str() == std::string("05:04:03"));
  63. ss.str("");
  64. ss << td1;
  65. check("check time period output: "+ ss.str(),
  66. ss.str() == std::string("-01:25:00"));
  67. ss.str("");
  68. ss << td2;
  69. check("check time period output: "+ ss.str(),
  70. ss.str() == std::string("-00:40:00"));
  71. ss.str("");
  72. ss << tp;
  73. check("check time period output - german: "+ ss.str(),
  74. ss.str() == std::string("[01.Mai.2002 12:10:05/01.Mai.2002 23:00:00]"));
  75. /* Input streaming is only available for compilers that
  76. * do not have BOOST_DATE_TIME_INCLUDE_LIMITED_HEADERS defined */
  77. #ifndef BOOST_DATE_TIME_INCLUDE_LIMITED_HEADERS
  78. /****** test streaming in for time classes ******/
  79. {
  80. std::istringstream iss("01:02:03.000004 garbage");
  81. iss >> td;
  82. #ifdef BOOST_DATE_TIME_HAS_NANOSECONDS
  83. check("Stream in time_duration", td == time_duration(1,2,3,4000));
  84. #else
  85. check("Stream in time_duration", td == time_duration(1,2,3,4));
  86. #endif
  87. }
  88. #if !defined(BOOST_NO_STD_ITERATOR_TRAITS) // vc6 & vc7
  89. {
  90. std::istringstream iss("2003-May-13 01:02:03");
  91. iss >> t1;
  92. check("Stream in ptime", t1 == ptime(date(2003,May,13), time_duration(1,2,3)));
  93. std::istringstream iss2("2003-January-13 01:02:03");
  94. iss2 >> t1;
  95. check("Stream in ptime2", t1 == ptime(date(2003,Jan,13), time_duration(1,2,3)));
  96. std::istringstream iss3("2003-feb-13 11:10:09");
  97. iss3 >> t1;
  98. check("Stream in ptime3", t1 == ptime(date(2003,Feb,13), time_duration(11,10,9)));
  99. try {
  100. std::istringstream iss4("2003-xxx-13 11:10:09");
  101. iss3 >> t1;
  102. check("Stream bad ptime", false); //never reach here, bad month exception
  103. }
  104. catch(std::exception& e) {
  105. std::cout << "Got expected exception: " << e.what() << std::endl;
  106. check("Stream bad ptime", true);
  107. }
  108. }
  109. {
  110. date d1x(2001,Aug,1), d2x(2003,May,13);
  111. #ifdef BOOST_DATE_TIME_HAS_NANOSECONDS
  112. time_duration td1x(15,32,18,20304000), td2x(1,2,3);
  113. #else
  114. time_duration td1x(15,32,18,20304), td2x(1,2,3);
  115. #endif
  116. time_period result(ptime(d1x,td1x), ptime(d2x,td2x));
  117. std::istringstream iss("[2001-Aug-01 15:32:18.020304/2003-May-13 01:02:03]");
  118. iss >> tp;
  119. check("Stream in time_period", tp == result);
  120. }
  121. #if !defined(BOOST_NO_STD_WSTRING)
  122. /*** wide streaming ***/
  123. {
  124. std::wistringstream wiss1(L"01:02:03");//.000004");
  125. wiss1 >> td;
  126. check("Wide stream in time_duration", td == time_duration(1,2,3));
  127. std::wistringstream wiss2(L"2003-May-23 03:20:10");
  128. wiss2 >> t1;
  129. check("Wide stream in ptime", t1 == ptime(date(2003,May,23), time_duration(3,20,10)));
  130. std::wistringstream wiss3(L"[2004-Jan-01 02:03:04/2004-May-13 01:00:00]");
  131. wiss3 >> tp;
  132. date d1x = date(2004,Jan,1);
  133. date d2x = date(2004,May,13);
  134. time_duration td1x = time_duration(2,3,4);
  135. time_duration td2x = time_duration(1,0,0);
  136. time_period result = time_period(ptime(d1x,td1x), ptime(d2x,td2x));
  137. check("Wide stream in time_period", tp == result);
  138. }
  139. #else
  140. check("Wide streaming not available for this compiler", false);
  141. #endif // BOOST_NO_STD_WSTRING
  142. #else // BOOST_NO_STD_ITERATOR_TRAITS
  143. check("Streaming in of alphabetic dates (Ex: 2003-Aug-21) \
  144. not supported by this compiler", false);
  145. #endif // BOOST_NO_STD_ITERATOR_TRAITS
  146. #else // BOOST_DATE_TIME_INCLUDE_LIMITED_HEADERS
  147. check("Streaming in of time classes not supported by this compiler", false);
  148. #endif // BOOST_DATE_TIME_INCLUDE_LIMITED_HEADERS
  149. #else //BOOST_DATE_TIME_NO_LOCALE
  150. check("No tests executed - Locales not supported by this compiler", false);
  151. #endif //BOOST_DATE_TIME_NO_LOCALE
  152. return printTestStats();
  153. }