testlocal_time_input_facet.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /* Copyright (c) 2005 CrystalClear Software, Inc.
  2. * Subject to the Boost Software License, Version 1.0.
  3. * (See accompanying file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
  4. * Author: Jeff Garland, Bart Garst
  5. * $Date$
  6. */
  7. #include "boost/date_time/local_time/local_time.hpp"
  8. #include "../testfrmwk.hpp"
  9. #include <iostream>
  10. #include <sstream>
  11. #include <string>
  12. // for tests that are expected to fail and throw exceptions
  13. template<class temporal_type, class exception_type>
  14. bool failure_test(temporal_type component,
  15. const std::string& input,
  16. exception_type const& /*except*/,
  17. boost::local_time::local_time_input_facet* facet)
  18. {
  19. using namespace boost::local_time;
  20. bool result = false;
  21. std::istringstream iss(input);
  22. iss.exceptions(std::ios_base::failbit); // turn on exceptions
  23. iss.imbue(std::locale(std::locale::classic(), facet));
  24. try {
  25. iss >> component;
  26. }
  27. catch(exception_type& e) {
  28. std::cout << "Expected exception caught: \""
  29. << e.what() << "\"" << std::endl;
  30. result = iss.fail(); // failbit must be set to pass test
  31. }
  32. catch(...) {
  33. result = false;
  34. }
  35. return result;
  36. }
  37. // for tests that are expected to fail quietly
  38. template<class temporal_type>
  39. bool failure_test(temporal_type component,
  40. const std::string& input,
  41. boost::local_time::local_time_input_facet* facet)
  42. {
  43. using namespace boost::local_time;
  44. std::istringstream iss(input);
  45. /* leave exceptions turned off
  46. * iss.exceptions(std::ios_base::failbit); */
  47. iss.imbue(std::locale(std::locale::classic(), facet));
  48. try {
  49. iss >> component;
  50. }
  51. catch(...) {
  52. std::cout << "Caught unexpected exception" << std::endl;
  53. return false;
  54. }
  55. return iss.fail(); // failbit must be set to pass test
  56. }
  57. int main() {
  58. using namespace boost::gregorian;
  59. using namespace boost::posix_time;
  60. using namespace boost::local_time;
  61. time_zone_ptr null_zone;
  62. local_date_time ldt1(not_a_date_time, null_zone);
  63. // verify wide stream works, thorough tests done in narrow stream
  64. #if !defined(BOOST_NO_STD_WSTRING)
  65. {
  66. std::wstringstream ws;
  67. ws.str(L"2005-Feb-15 12:15:00 EST-05EDT,M4.1.0,M10.5.0");
  68. ws >> ldt1;
  69. check("Wide stream, Eastern US, daylight savings, winter, minimal input",
  70. ldt1.local_time() == ptime(date(2005,2,15), time_duration(12,15,0)));
  71. check("Wide stream, Eastern US, daylight savings, winter, minimal input",
  72. ldt1.utc_time() == ptime(date(2005,2,15), time_duration(17,15,0)));
  73. check("Wide stream, Eastern US, daylight savings, winter, minimal input", !ldt1.is_dst());
  74. ws.str(L"");
  75. wlocal_time_input_facet* wfacet = new wlocal_time_input_facet(L"%m/%d/%y %ZP");
  76. std::locale loc(std::locale::classic(), wfacet);
  77. ws.imbue(loc);
  78. ws.str(L"10/31/04 PST-08PDT,M4.1.0,M10.5.0"); // midnight on end transition day, still in dst
  79. ws >> ldt1;
  80. std::wcout << ldt1.local_time() << std::endl;
  81. check("Wide stream, Eastern US, daylight savings, winter, custom format",
  82. ldt1.local_time() == ptime(date(2004,10,31), time_duration(0,0,0)));
  83. check("Wide stream, Eastern US, daylight savings, winter, custom format",
  84. ldt1.utc_time() == ptime(date(2004,10,31), time_duration(7,0,0)));
  85. check("Wide stream, Eastern US, daylight savings, winter, custom format", ldt1.is_dst());
  86. }
  87. #endif // BOOST_NO_STD_WSTRING
  88. std::stringstream ss;
  89. ss.str("2005-Feb-25 12:15:00 EST-05EDT,M4.1.0,M10.5.0");
  90. ss >> ldt1;
  91. check("Eastern US, daylight savings, winter, minimal input",
  92. ldt1.local_time() == ptime(date(2005,2,25), time_duration(12,15,0)));
  93. check("Eastern US, daylight savings, winter, minimal input",
  94. ldt1.utc_time() == ptime(date(2005,2,25), time_duration(17,15,0)));
  95. check("Eastern US, daylight savings, winter, minimal input", !ldt1.is_dst());
  96. ss.str("");
  97. ss.str("2005-Aug-25 12:15:00 EST-05EDT,M4.1.0,M10.5.0");
  98. ss >> ldt1;
  99. check("Eastern US, daylight savings, summer, minimal input",
  100. ldt1.local_time() == ptime(date(2005,8,25), time_duration(12,15,0)));
  101. check("Eastern US, daylight savings, summer, minimal input",
  102. ldt1.utc_time() == ptime(date(2005,8,25), time_duration(16,15,0)));
  103. check("Eastern US, daylight savings, summer, minimal input", ldt1.is_dst());
  104. ss.str("");
  105. ss.str("2005-Apr-03 01:15:00 EST-05EDT,M4.1.0,M10.5.0");
  106. ss >> ldt1;
  107. check("Eastern US, daylight savings, transition point", !ldt1.is_dst());
  108. ldt1 += hours(1);
  109. check("Eastern US, daylight savings, transition point", ldt1.is_dst());
  110. ss.str("");
  111. ss.str("2005-Apr-03 01:15:00 EST-05EDT,93,303");
  112. ss >> ldt1;
  113. check("Eastern US, daylight savings, transition point", !ldt1.is_dst());
  114. ldt1 += hours(1);
  115. check("Eastern US, daylight savings, transition point", ldt1.is_dst());
  116. ss.str("");
  117. ss.str("2005-Oct-30 00:15:00 EST-05EDT,M4.1.0,M10.5.0");
  118. ss >> ldt1;
  119. check("Eastern US, daylight savings, transition point", ldt1.is_dst());
  120. ldt1 += hours(1);
  121. check("Eastern US, daylight savings, transition point", ldt1.is_dst());
  122. ldt1 += hours(1);
  123. check("Eastern US, daylight savings, transition point", !ldt1.is_dst());
  124. ss.str("");
  125. ss.str("2005-Oct-30 00:15:00 EST-05EDT,93,303");
  126. ss >> ldt1;
  127. check("Eastern US, daylight savings, transition point", ldt1.is_dst());
  128. ldt1 += hours(1);
  129. check("Eastern US, daylight savings, transition point", ldt1.is_dst());
  130. ldt1 += hours(1);
  131. check("Eastern US, daylight savings, transition point", !ldt1.is_dst());
  132. ss.str("");
  133. ss.str("2005-Aug-25 12:15:00 MST-07");
  134. ss >> ldt1;
  135. check("Mountain US, no daylight savings",
  136. ldt1.local_time() == ptime(date(2005,8,25), time_duration(12,15,0)));
  137. check("Mountain US, no daylight savings",
  138. ldt1.utc_time() == ptime(date(2005,8,25), time_duration(19,15,0)));
  139. check("Mountain US, no daylight savings", !ldt1.is_dst());
  140. ss.str("");
  141. // insure input & output formats match
  142. local_time_facet* out_facet =
  143. new local_time_facet(local_time_input_facet::default_time_input_format);
  144. std::locale loc(std::locale::classic(), out_facet);
  145. ss.imbue(loc);
  146. time_zone_ptr syd_tz(new posix_time_zone("EST+10EST,M10.5.0,M3.5.0/03:00"));
  147. ptime pt(date(2005,6,12), hours(0));
  148. local_date_time ldt2(pt, syd_tz);
  149. ss << ldt2;
  150. ss >> ldt1;
  151. check("Output as input makes match", ldt1 == ldt2);
  152. check("Output as input makes match",
  153. ldt1.zone()->dst_local_start_time(2004) == ldt2.zone()->dst_local_start_time(2004));
  154. ss.str("");
  155. time_zone_ptr f_tz(new posix_time_zone("FST+03FDT,90,300"));
  156. ldt2 = local_date_time(ptime(date(2005,6,12), hours(0)), f_tz);
  157. ss << ldt2;
  158. ss >> ldt1;
  159. check("Output as input makes match", ldt1 == ldt2);
  160. check("Output as input makes match",
  161. ldt1.zone()->dst_local_start_time(2004) == ldt2.zone()->dst_local_start_time(2004));
  162. ss.str("");
  163. // missing input & wrong format tests
  164. ss.str("2005-Oct-30 00:15:00");
  165. ss >> ldt1;
  166. check("Missing time_zone spec makes UTC", ldt1.zone_as_posix_string() == std::string("UTC+00"));
  167. check("Missing time_zone spec makes UTC", ldt1.utc_time() == ldt1.local_time());
  168. ss.str("");
  169. {
  170. std::istringstream iss("2005-Aug-25 12:15:00 MST-07");
  171. local_time_input_facet* f = new local_time_input_facet("%Y-%b-%d %H:%M:%S %z");
  172. std::locale locx(std::locale::classic(), f);
  173. iss.imbue(locx);
  174. iss >> ldt1;
  175. check("Wrong format flag makes UTC", ldt1.zone_as_posix_string() == std::string("UTC+00"));
  176. check("Wrong format flag makes UTC", ldt1.utc_time() == ldt1.local_time());
  177. }
  178. // failure tests: (posix_time_zone) bad_offset, bad_adjustment,
  179. // (local_date_time) ambiguous_result, time_label_invalid,
  180. // time/date failures already tested
  181. ambiguous_result amb_ex("default");
  182. time_label_invalid inv_ex("default");
  183. check("Failure test ambiguous time label (w/exceptions)",
  184. failure_test(ldt1,
  185. "2005-Oct-30 01:15:00 EST-05EDT,M4.1.0,M10.5.0",
  186. amb_ex,
  187. new local_time_input_facet()));
  188. check("Failure test ambiguous time label (no exceptions)",
  189. failure_test(ldt1,
  190. "2005-Oct-30 01:15:00 EST-05EDT,M4.1.0,M10.5.0",
  191. new local_time_input_facet()));
  192. check("Failure test ambiguous time label (w/exceptions)",
  193. failure_test(ldt1,
  194. "2005-Oct-30 01:15:00 EST-05EDT,93,303",
  195. amb_ex,
  196. new local_time_input_facet()));
  197. check("Failure test ambiguous time label (no exceptions)",
  198. failure_test(ldt1,
  199. "2005-Oct-30 01:15:00 EST-05EDT,93,303",
  200. new local_time_input_facet()));
  201. check("Failure test invalid time label (w/exceptions)",
  202. failure_test(ldt1,
  203. "2005-Apr-03 02:15:00 EST-05EDT,M4.1.0,M10.5.0",
  204. inv_ex,
  205. new local_time_input_facet()));
  206. check("Failure test invalid time label (no exceptions)",
  207. failure_test(ldt1,
  208. "2005-Apr-03 02:15:00 EST-05EDT,M4.1.0,M10.5.0",
  209. new local_time_input_facet()));
  210. check("Failure test invalid time label (w/exceptions)",
  211. failure_test(ldt1,
  212. "2005-Apr-03 02:15:00 EST-05EDT,93,303",
  213. inv_ex,
  214. new local_time_input_facet()));
  215. check("Failure test invalid time label (no exceptions)",
  216. failure_test(ldt1,
  217. "2005-Apr-03 02:15:00 EST-05EDT,93,303",
  218. new local_time_input_facet()));
  219. return printTestStats();
  220. }