testgreg_wstream.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /* Copyright (c) 2003-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: Bart Garst
  6. * $Date$
  7. */
  8. #include <iostream>
  9. #include <sstream>
  10. #include <boost/date_time/gregorian/gregorian.hpp>
  11. #include "../testfrmwk.hpp"
  12. using namespace boost::gregorian;
  13. int main(){
  14. #if defined(BOOST_NO_STD_WSTRING) || \
  15. defined(BOOST_DATE_TIME_INCLUDE_LIMITED_HEADERS)
  16. check("No wstring/wstream support for this compiler", false);
  17. #else
  18. std::wstring res, ws;
  19. std::wstringstream wss;
  20. /* date_period is used because almost all the date-type objects
  21. * that have the operator<< can be easily accessed from it.
  22. * Those are: date, greg_month, greg_day_of_week,
  23. * date_period, date_duration (also date_generators) */
  24. date_period dp(date(2003,Aug,21), date(2004,May,27));
  25. // date
  26. wss << dp.begin();
  27. res = L"2003-Aug-21";
  28. check("date operator<<", wss.str() == res);
  29. wss.str(L"");
  30. ws = to_simple_wstring(dp.begin());
  31. check("date to_simple_string", ws == res);
  32. ws = to_iso_wstring(dp.begin());
  33. res = L"20030821";
  34. check("date to_iso_string", ws == res);
  35. ws = to_iso_extended_wstring(dp.begin());
  36. res = L"2003-08-21";
  37. check("date to_iso_extended_string", ws == res);
  38. ws = to_sql_wstring(dp.begin());
  39. check("date to_sql_string", ws == res);
  40. wss.str(L"");
  41. #ifndef BOOST_NO_STD_ITERATOR_TRAITS
  42. {
  43. res = L"2003-Aug-21";
  44. std::wstringstream wss2(L"2003-Aug-21");
  45. date testdate(not_a_date_time);
  46. wss2 >> testdate;
  47. check("date operator>>", to_simple_wstring(testdate) == res);
  48. }
  49. #else
  50. check("no date operator>> for this compiler", false);
  51. #endif
  52. // greg_month
  53. wss << dp.begin().month();
  54. res = L"08";
  55. check("greg_month", wss.str() == res);
  56. wss.str(L"");
  57. ws = dp.begin().month().as_short_wstring();
  58. res = L"Aug";
  59. check("greg_month as_short_wstring", ws == res);
  60. ws = dp.begin().month().as_long_wstring();
  61. res = L"August";
  62. check("greg_month as_long_wstring", ws == res);
  63. /*{
  64. std::wstringstream wss2(L"August");
  65. greg_month testmonth(not_a_date_time);
  66. wss2 >> testmonth;
  67. check("greg_month operator>>", to_simple_wstring(testmonth) == res);
  68. }*/
  69. // greg_day_of_week
  70. wss << dp.begin().day_of_week();
  71. res = L"Thu";
  72. check("greg_day_of_week", wss.str() == res);
  73. wss.str(L"");
  74. ws = dp.begin().day_of_week().as_short_wstring();
  75. check("greg_day_of_week as_short_wstring", ws == res);
  76. ws = dp.begin().day_of_week().as_long_wstring();
  77. res = L"Thursday";
  78. check("greg_day_of_week as_long_wstring", ws == res);
  79. /*{
  80. std::wstringstream wss2(L"Thu");
  81. greg_day_of_week testday(not_a_date_time);
  82. wss2 >> testday;
  83. check("greg_day_of_week operator>>", to_simple_wstring(testday) == res);
  84. }*/
  85. // date_period
  86. wss << dp;
  87. res = L"[2003-Aug-21/2004-May-26]";
  88. check("date_period", wss.str() == res);
  89. wss.str(L"");
  90. ws = to_simple_wstring(dp);
  91. check("date_period to_simple_string", ws == res);
  92. res = L"20030821/20040526";
  93. ws = to_iso_wstring(dp);
  94. check("date_period to_iso_string", ws == res);
  95. {
  96. std::wstringstream wss2(L"[2003-Aug-21/2004-May-27]");
  97. res = L"[2003-Aug-21/2004-May-26]";
  98. // following line gives an ambiguous overload of op>>
  99. //date_period testperiod(date(not_a_date_time),date_duration(not_a_date_time));
  100. date_period testperiod(date(2003,Aug,21), date(2004,May,27));
  101. wss2 >> testperiod;
  102. check("date_period operator>>", to_simple_wstring(testperiod) == res);
  103. }
  104. // date_duration
  105. wss << dp.length();
  106. res = L"280";
  107. check("date_duration", wss.str() == res);
  108. wss.str(L"");
  109. {
  110. std::wstringstream wss2(L"280");
  111. date_duration testduration(not_a_date_time);
  112. wss2 >> testduration;
  113. check("date_duration operator>>", testduration.days() == 280);
  114. }
  115. // special values
  116. date sv_d(neg_infin);
  117. date_duration sv_dd(pos_infin);
  118. //date_period sv_dp(sv_d,sv_dd);
  119. // sv-date
  120. wss << sv_d;
  121. res = L"-infinity";
  122. check("date operator<< special value", wss.str() == res);
  123. wss.str(L"");
  124. ws = to_simple_wstring(sv_d);
  125. check("date to_simple_string special value", ws == res);
  126. // sv-date_duration
  127. wss << sv_dd;
  128. res = L"+infinity";
  129. check("date_duration operator<< special value", wss.str() == res);
  130. wss.str(L"");
  131. // sv-date_period
  132. /*
  133. * works - just gives unexpected results:
  134. *[-infinity/not-a-date-time]
  135. wss << sv_dp;
  136. res = L"[2003-Aug-21/2004-May-26]";
  137. check("date_period", wss.str() == res);
  138. std::wcout << wss.str() << std::endl;
  139. wss.str(L"");
  140. */
  141. // date_generators
  142. first_day_of_the_week_after fka(Monday);
  143. wss << fka;
  144. res = L"Mon after";
  145. check("first_kday_after", wss.str() == res);
  146. wss.str(L"");
  147. first_day_of_the_week_before fkb(Monday);
  148. wss << fkb;
  149. res = L"Mon before";
  150. check("first_kday_before", wss.str() == res);
  151. wss.str(L"");
  152. first_day_of_the_week_in_month fkom(Monday,Jan);
  153. wss << fkom;
  154. res = L"first Mon of Jan";
  155. check("first_kday_of_month", wss.str() == res);
  156. wss.str(L"");
  157. last_day_of_the_week_in_month lkom(Monday,Jan);
  158. wss << lkom;
  159. res = L"last Mon of Jan";
  160. check("last_kday_of_month", wss.str() == res);
  161. wss.str(L"");
  162. #endif
  163. return printTestStats();
  164. }