testfacet.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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 <sstream>
  8. #include <iostream>
  9. #include <fstream>
  10. #include "boost/date_time/gregorian/greg_month.hpp"
  11. #include "boost/date_time/gregorian/greg_facet.hpp"
  12. #include "boost/date_time/date_format_simple.hpp"
  13. #include "boost/date_time/gregorian/gregorian.hpp"
  14. #include "../testfrmwk.hpp"
  15. #ifndef BOOST_DATE_TIME_NO_LOCALE
  16. const char* const de_short_month_names[]={"Jan","Feb","Mar","Apr","Mai","Jun","Jul","Aug","Sep","Okt","Nov","Dez", "NAM"};
  17. const char* const de_long_month_names[]={"Januar","Februar","Marz","April","Mai","Juni","Juli","August","September","Oktober","November","Dezember","NichtDerMonat"};
  18. const char* const de_special_value_names[]={"NichtDatumzeit","-unbegrenztheit", "+unbegrenztheit"};
  19. const char* const de_short_weekday_names[]={"Son", "Mon", "Die","Mit", "Don", "Fre", "Sam"};
  20. const char* const de_long_weekday_names[]={"Sonntag", "Montag", "Dienstag","Mittwoch", "Donnerstag", "Freitag", "Samstag"};
  21. #endif
  22. /** Not used for now
  23. const char* const es_short_month_names[]={"Ene","Feb","Mar","Abr","Pue","Jun","Jul","Ago","Sep","Oct","Nov","Dic", "NAM"};
  24. const char* const es_long_month_names[]={"Enero","Febrero","Marcha","Abril","Pueda","Junio","Julio","Agosto","Septiembre","Octubre","Noviembre","Diciembre","NoAMes"};
  25. const char* const es_special_value_names[]={"NoUnRatoDeLaFacha","-infinito", "+infinito"};
  26. **/
  27. int
  28. main()
  29. {
  30. // std::locale native("");
  31. // std::cout << "native: " << native.name() << std::endl;
  32. //#ifndef BOOST_NO_STD_LOCALE
  33. #ifndef BOOST_DATE_TIME_NO_LOCALE
  34. using namespace boost::gregorian;
  35. typedef greg_facet_config facet_config;
  36. typedef boost::date_time::all_date_names_put<facet_config> date_facet;
  37. typedef boost::date_time::date_names_put<facet_config> date_facet_base;
  38. typedef boost::date_time::ostream_month_formatter<date_facet_base> month_formatter;
  39. {
  40. // special_values tests
  41. std::stringstream ss;
  42. date_facet_base* f = new date_facet_base();
  43. std::locale loc(std::locale::classic(), f);
  44. ss.imbue(loc);
  45. date d(not_a_date_time);
  46. ss << d;
  47. check("Special value, stream out nadt" , ss.str() == std::string("not-a-date-time"));
  48. ss.str("");
  49. d = date(neg_infin);
  50. ss << d;
  51. check("Special value, stream out neg_infin" , ss.str() == std::string("-infinity"));
  52. ss.str("");
  53. d = date(pos_infin);
  54. ss << d;
  55. check("Special value, stream out pos_infin" , ss.str() == std::string("+infinity"));
  56. }
  57. date_facet gdnp(de_short_month_names, de_long_month_names,
  58. de_special_value_names, de_long_weekday_names,
  59. de_long_weekday_names,
  60. '.',
  61. boost::date_time::ymd_order_dmy);
  62. std::stringstream ss;
  63. std::ostreambuf_iterator<char> coi(ss);
  64. gdnp.put_month_short(coi, Oct);
  65. check("check german short month: " + ss.str(),
  66. ss.str() == std::string("Okt"));
  67. ss.str(""); //reset string stream
  68. greg_month mo(Oct);
  69. month_formatter::format_month(mo, ss, gdnp);
  70. check("check german short month: " + ss.str(),
  71. ss.str() == std::string("Okt"));
  72. ss.str(""); //reset string stream
  73. // month_formatter::format_month(mo, ss, gdnp);
  74. // check("check german long month: " + ss.str(),
  75. // ss.str() == std::string("Oktober"));
  76. greg_year_month_day ymd(2002,Oct,1);
  77. typedef boost::date_time::ostream_ymd_formatter<greg_year_month_day, date_facet_base> ymd_formatter;
  78. ss.str(""); //reset string stream
  79. ymd_formatter::ymd_put(ymd, ss, gdnp);
  80. check("check ymd: " + ss.str(),
  81. ss.str() == std::string("01.Okt.2002"));
  82. typedef boost::date_time::ostream_date_formatter<date, date_facet_base> datef;
  83. std::stringstream os;
  84. date d1(2002, Oct, 1);
  85. datef::date_put(d1, os, gdnp);
  86. check("ostream low level check string:"+os.str(),
  87. os.str() == std::string("01.Okt.2002"));
  88. // //Locale tests
  89. std::locale global;
  90. std::cout << "global: " << global.name() << std::endl;
  91. // put a facet into a locale
  92. //check for a facet p319
  93. check("no registered facet here",
  94. !std::has_facet<date_facet>(global));
  95. std::locale global2(global,
  96. new date_facet(de_short_month_names,
  97. de_long_month_names,
  98. de_special_value_names,
  99. de_long_weekday_names,
  100. de_long_weekday_names));
  101. check("facet registered here",
  102. std::has_facet<boost::date_time::date_names_put<facet_config> >(global2));
  103. std::stringstream os2;
  104. os2.imbue(global2);
  105. datef::date_put(d1, os2);
  106. check("check string imbued ostream: "+os2.str(),
  107. os2.str() == std::string("2002-Okt-01"));
  108. date infin(pos_infin);
  109. os2.str(""); //clear stream
  110. datef::date_put(infin, os2);
  111. check("check string imbued ostream: "+os2.str(),
  112. os2.str() == std::string("+unbegrenztheit"));
  113. os2.str(""); //clear stream
  114. os2 << infin;
  115. check("check string imbued ostream: "+os2.str(),
  116. os2.str() == std::string("+unbegrenztheit"));
  117. date nadt(not_a_date_time);
  118. os2.str(""); //clear stream
  119. datef::date_put(nadt, os2);
  120. check("check string imbued ostream: "+os2.str(),
  121. os2.str() == std::string("NichtDatumzeit"));
  122. std::stringstream os3;
  123. os3 << d1;
  124. check("check any old ostream: "+os3.str(),
  125. os3.str() == std::string("2002-Oct-01"));
  126. std::ofstream f("test_facet_file.out");
  127. f << d1 << std::endl;
  128. // // date formatter that takes locale and gets facet from locale
  129. std::locale german_dates1(global,
  130. new date_facet(de_short_month_names,
  131. de_long_month_names,
  132. de_special_value_names,
  133. de_short_weekday_names,
  134. de_long_weekday_names,
  135. '.',
  136. boost::date_time::ymd_order_dmy,
  137. boost::date_time::month_as_integer));
  138. os3.imbue(german_dates1);
  139. os3.str("");
  140. os3 << d1;
  141. check("check date order: "+os3.str(),
  142. os3.str() == std::string("01.10.2002"));
  143. std::locale german_dates2(global,
  144. new date_facet(de_short_month_names,
  145. de_long_month_names,
  146. de_special_value_names,
  147. de_short_weekday_names,
  148. de_long_weekday_names,
  149. ' ',
  150. boost::date_time::ymd_order_iso,
  151. boost::date_time::month_as_short_string));
  152. os3.imbue(german_dates2);
  153. os3.str("");
  154. os3 << d1;
  155. check("check date order: "+os3.str(),
  156. os3.str() == std::string("2002 Okt 01"));
  157. std::locale german_dates3(global,
  158. new date_facet(de_short_month_names,
  159. de_long_month_names,
  160. de_special_value_names,
  161. de_short_weekday_names,
  162. de_long_weekday_names,
  163. ' ',
  164. boost::date_time::ymd_order_us,
  165. boost::date_time::month_as_long_string));
  166. os3.imbue(german_dates3);
  167. os3.str("");
  168. os3 << d1;
  169. check("check date order: "+os3.str(),
  170. os3.str() == std::string("Oktober 01 2002"));
  171. date_period dp(d1, date_duration(3));
  172. os3.str("");
  173. os3 << dp;
  174. check("check date period: "+os3.str(),
  175. os3.str() == std::string("[Oktober 01 2002/Oktober 03 2002]"));
  176. /*******************************************************************/
  177. /* Streaming operations for date durations */
  178. /*******************************************************************/
  179. date_duration dur(26);
  180. std::stringstream ss2;
  181. ss2 << dur;
  182. check("date_duration stream out", ss2.str() == std::string("26"));
  183. dur = date_duration(boost::date_time::pos_infin);
  184. ss2.str("");
  185. ss2 << dur;
  186. check("date_duration stream out", ss2.str() == std::string("+infinity"));
  187. /*******************************************************************/
  188. /* Streaming operations for date generator functions */
  189. /*******************************************************************/
  190. partial_date pd(26, Jun);
  191. //std::stringstream ss2;
  192. ss2.str("");
  193. ss2 << pd;
  194. check("partial date stream out", ss2.str() == std::string("26 Jun"));
  195. ss2.str("");
  196. nth_kday_of_month nkm(nth_kday_of_month::second, Friday, Sep);
  197. ss2 << nkm;
  198. check("nth kday of month", ss2.str() == std::string("second Fri of Sep"));
  199. ss2.str("");
  200. first_kday_of_month fkm(Saturday, May);
  201. ss2 << fkm;
  202. check("first kday of month", ss2.str() == std::string("first Sat of May"));
  203. ss2.str("");
  204. last_kday_of_month lkm(Monday, Aug);
  205. ss2 << lkm;
  206. check("last kday of month", ss2.str() == std::string("last Mon of Aug"));
  207. ss2.str("");
  208. first_kday_after fka(Thursday);//fkb.get_date(d)
  209. ss2 << fka;
  210. check("first kday after", ss2.str() == std::string("Thu after"));
  211. ss2.str("");
  212. first_kday_before fkb(Tuesday); // same ^
  213. ss2 << fkb;
  214. check("first kday after", ss2.str() == std::string("Tue before"));
  215. std::cout << pd << '\n'
  216. << nkm << '\n'
  217. << fkm << '\n'
  218. << lkm << '\n'
  219. << fka << '\n'
  220. << fkb << '\n'
  221. << std::endl;
  222. /*******************************************************************/
  223. /* Input Streaming for greg_month */
  224. /*******************************************************************/
  225. {
  226. std::stringstream ss1("January");
  227. std::stringstream ss2m("dec"); // misspelled
  228. std::stringstream german("Okt");
  229. german.imbue(global2);
  230. greg_month m(3);
  231. ss1 >> m;
  232. check("Stream in month", m == greg_month(Jan));
  233. #ifndef BOOST_NO_STD_WSTRING
  234. std::wstringstream ws1(L"Dec");
  235. ws1 >> m;
  236. check("Wide Stream in month", m == greg_month(Dec));
  237. #else
  238. check("Wide Stream in not supported by this compiler", false);
  239. #endif // BOOST_NO_STD_WSTRING
  240. german >> m;
  241. check("Stream in German month", m == greg_month(Oct));
  242. try{
  243. ss2m >> m; // misspelled
  244. check("Bad month exception NOT thrown (misspelled name)", false);
  245. }catch(bad_month&){
  246. check("Bad month exception caught (misspelled name)", true);
  247. }catch(...){
  248. check("Bad month exception NOT caught (misspelled name)", false);
  249. }
  250. }
  251. /*******************************************************************/
  252. /* Input Streaming for greg_weekday */
  253. /*******************************************************************/
  254. {
  255. std::stringstream ss1("Sun");
  256. std::stringstream ss2w("Wensday"); // misspelled
  257. std::stringstream german("Mittwoch"); // Wednesday
  258. german.imbue(global2);
  259. greg_weekday wd(Friday); //something not Sunday...
  260. ss1 >> wd;
  261. check("Stream in weekday", wd == greg_weekday(Sunday));
  262. #ifndef BOOST_NO_STD_WSTRING
  263. std::wstringstream ws1(L"Saturday");
  264. ws1 >> wd;
  265. check("Wide Stream in weekday", wd == greg_weekday(Saturday));
  266. #else
  267. check("Wide Stream in not supported by this compiler", false);
  268. #endif // BOOST_NO_STD_WSTRING
  269. german >> wd;
  270. check("Stream in German weekday", wd == greg_weekday(Wednesday));
  271. try{
  272. ss2w >> wd;
  273. check("Bad weekday exception NOT thrown (misspelled name)", false);
  274. }catch(bad_weekday&){
  275. check("Bad weekday exception caught (misspelled name)", true);
  276. }catch(...){
  277. check("Bad weekday exception NOT caught (misspelled name)", false);
  278. }
  279. }
  280. #else
  281. check("No tests executed - Locales not supported by this compiler", false);
  282. #endif //BOOST_DATE_TIME_NO_LOCALE
  283. return printTestStats();
  284. }