testdate_facet_new.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. /* Copyright (c) 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: Jeff Garland, Bart Garst
  6. * $Date$
  7. */
  8. #include "boost/date_time/gregorian/gregorian.hpp"
  9. #include "boost/date_time/date_facet.hpp"
  10. #include "../testfrmwk.hpp"
  11. #include <iostream>
  12. #include <sstream>
  13. template<class temporal_type, typename charT>
  14. inline
  15. void
  16. teststreaming(std::string testname,
  17. temporal_type value,
  18. std::basic_string<charT> expected_result,
  19. const std::locale& locale = std::locale::classic())
  20. {
  21. std::basic_stringstream<charT> ss;
  22. ss.imbue(locale);
  23. ss << value;
  24. check_equal(testname, ss.str(), expected_result);
  25. }
  26. // collections for adding to facet
  27. const char* const month_short_names[]={"*jan*","*feb*","*mar*",
  28. "*apr*","*may*","*jun*",
  29. "*jul*","*aug*","*sep*",
  30. "*oct*","*nov*","*dec*"};
  31. const char* const month_long_names[]={"**January**","**February**","**March**",
  32. "**April**","**May**","**June**",
  33. "**July**","**August**","**September**",
  34. "**October**","**November**","**December**"};
  35. const char* const weekday_short_names[]={"day1", "day2","day3","day4",
  36. "day5","day6","day7"};
  37. const char* const weekday_long_names[]= {"Sun-0", "Mon-1", "Tue-2",
  38. "Wed-3", "Thu-4",
  39. "Fri-5", "Sat-6"};
  40. std::vector<std::basic_string<char> > short_weekday_names;
  41. std::vector<std::basic_string<char> > long_weekday_names;
  42. std::vector<std::basic_string<char> > short_month_names;
  43. std::vector<std::basic_string<char> > long_month_names;
  44. #if !defined(BOOST_NO_STD_WSTRING)
  45. // collections of test results
  46. const std::wstring full_months[]={L"January",L"February",L"March",
  47. L"April",L"May",L"June",
  48. L"July",L"August",L"September",
  49. L"October",L"November",L"December"};
  50. const std::wstring short_months[]={L"Jan",L"Feb",L"Mar",
  51. L"Apr",L"May",L"Jun",
  52. L"Jul",L"Aug",L"Sep",
  53. L"Oct",L"Nov",L"Dec"};
  54. const std::wstring full_weekdays[]= {L"Sunday", L"Monday",L"Tuesday",
  55. L"Wednesday", L"Thursday",
  56. L"Friday", L"Saturday"};
  57. const std::wstring short_weekdays[]= {L"Sun", L"Mon",L"Tue",
  58. L"Wed", L"Thu",
  59. L"Fri", L"Sat"};
  60. //const whcar_t const
  61. #endif // BOOST_NO_STD_WSTRING
  62. int main() {
  63. using namespace boost::gregorian;
  64. std::copy(&month_short_names[0],
  65. &month_short_names[12],
  66. std::back_inserter(short_month_names));
  67. std::copy(&month_long_names[0],
  68. &month_long_names[12],
  69. std::back_inserter(long_month_names));
  70. std::copy(&weekday_short_names[0],
  71. &weekday_short_names[7],
  72. std::back_inserter(short_weekday_names));
  73. std::copy(&weekday_long_names[0],
  74. &weekday_long_names[7],
  75. std::back_inserter(long_weekday_names));
  76. {
  77. std::stringstream ss;
  78. date d(2004,Oct,31);
  79. date_period dp(d, d + days(7));
  80. ss << d;
  81. check("to_string & default formats match",
  82. to_simple_string(d) == ss.str());
  83. ss.str("");
  84. ss << dp;
  85. check("to_string & default formats match",
  86. to_simple_string(dp) == ss.str());
  87. }
  88. {
  89. date d(2004,Oct, 13);
  90. date_period dp(d, d + days(7));
  91. {
  92. date_facet* datefacet = new date_facet();
  93. datefacet->format(date_facet::standard_format_specifier);
  94. std::cout.imbue(std::locale(std::locale::classic(), datefacet));
  95. teststreaming("default classic date", d, std::string("10/13/04"),
  96. std::locale(std::locale::classic(), datefacet));
  97. std::cout << "default classic date output: " << d << std::endl;
  98. }
  99. {
  100. date_facet* datefacet = new date_facet();
  101. datefacet->format(date_facet::standard_format_specifier);
  102. teststreaming("default classic date period", dp,
  103. std::string("[10/13/04/10/19/04]"),
  104. std::locale(std::locale::classic(), datefacet));
  105. }
  106. {
  107. date_facet* datefacet = new date_facet();
  108. datefacet->format("%Y-%d-%b %a");
  109. teststreaming("custom date facet date period", dp,
  110. std::string("[2004-13-Oct Wed/2004-19-Oct Tue]"),
  111. std::locale(std::locale::classic(), datefacet));
  112. }
  113. {
  114. date_facet* datefacet = new date_facet();
  115. datefacet->set_iso_format();
  116. teststreaming("custom date facet date", d,
  117. std::string("20041013"),
  118. std::locale(std::locale::classic(), datefacet));
  119. }
  120. {
  121. date_facet* datefacet = new date_facet();
  122. datefacet->set_iso_format();
  123. teststreaming("custom date facet date period", dp,
  124. std::string("[20041013/20041019]"),
  125. std::locale(std::locale::classic(), datefacet));
  126. }
  127. {
  128. date_facet* datefacet = new date_facet();
  129. datefacet->set_iso_extended_format();
  130. teststreaming("custom date facet date", d,
  131. std::string("2004-10-13"),
  132. std::locale(std::locale::classic(), datefacet));
  133. }
  134. {
  135. date_facet* datefacet = new date_facet();
  136. datefacet->set_iso_extended_format();
  137. teststreaming("custom date facet date period", dp,
  138. std::string("[2004-10-13/2004-10-19]"),
  139. std::locale(std::locale::classic(), datefacet));
  140. }
  141. {
  142. date_facet* datefacet = new date_facet();
  143. datefacet->set_iso_extended_format();
  144. period_formatter pf(period_formatter::AS_OPEN_RANGE, " / ", "[ ", " )", " ]");
  145. datefacet->period_formatter(pf);
  146. teststreaming("custom date facet date period - open range custom delimeters", dp,
  147. std::string("[ 2004-10-13 / 2004-10-20 )"),
  148. std::locale(std::locale::classic(), datefacet));
  149. }
  150. // trac-11142: actually test delimiter_strings(...)
  151. {
  152. date_facet* datefacet = new date_facet();
  153. datefacet->set_iso_extended_format();
  154. period_formatter pf(period_formatter::AS_OPEN_RANGE, " / ", "[ ", " )", " ]");
  155. pf.delimiter_strings(" to ", "from ", " inclusive", " exclusive");
  156. datefacet->period_formatter(pf);
  157. teststreaming("custom date facet date period - delimiter_strings", dp,
  158. std::string("from 2004-10-13 to 2004-10-20 inclusive"),
  159. std::locale(std::locale::classic(), datefacet));
  160. }
  161. {
  162. date_facet* datefacet = new date_facet("%A %b %d, %Y");
  163. datefacet->short_month_names(short_month_names);
  164. teststreaming("custom date facet -- custom short month names", d,
  165. std::string("Wednesday *oct* 13, 2004"),
  166. std::locale(std::locale::classic(), datefacet));
  167. }
  168. {
  169. date_facet* datefacet = new date_facet("%B %A %d, %Y");
  170. datefacet->long_month_names(long_month_names);
  171. teststreaming("custom date facet -- custom long month names", d,
  172. std::string("**October** Wednesday 13, 2004"),
  173. std::locale(std::locale::classic(), datefacet));
  174. }
  175. {
  176. date_facet* datefacet = new date_facet("%a - %b %d, %Y");
  177. datefacet->short_weekday_names(short_weekday_names);
  178. std::cout.imbue(std::locale(std::locale::classic(), datefacet));
  179. std::cout << d << std::endl;
  180. teststreaming("custom date facet -- custom short weekday names", d,
  181. std::string("day4 - Oct 13, 2004"),
  182. std::locale(std::locale::classic(), datefacet));
  183. }
  184. {
  185. date_facet* datefacet = new date_facet("%b %d, %Y ++ %A");
  186. datefacet->long_weekday_names(long_weekday_names);
  187. teststreaming("custom date facet -- custom short weekday names", d,
  188. std::string("Oct 13, 2004 ++ Wed-3"),
  189. std::locale(std::locale::classic(), datefacet));
  190. }
  191. {//date
  192. date_facet* datefacet = new date_facet("%Y-%b-%d %%d");
  193. teststreaming("Literal '%' in date format", d,
  194. std::string("2004-Oct-13 %d"),
  195. std::locale(std::locale::classic(), datefacet));
  196. }
  197. {
  198. date_facet* datefacet = new date_facet("%Y-%b-%d %%%d");
  199. teststreaming("Multiple literal '%'s in date format", d,
  200. std::string("2004-Oct-13 %13"),
  201. std::locale(std::locale::classic(), datefacet));
  202. }
  203. {
  204. date d1(2004,Oct, 13);
  205. date_facet* datefacet = new date_facet("%d%m%y");
  206. teststreaming("Single digit year and %y", d1,
  207. std::string("131004"),
  208. std::locale(std::locale::classic(), datefacet));
  209. }
  210. {//month
  211. date_facet* datefacet = new date_facet();
  212. datefacet->month_format("%b %%b");
  213. teststreaming("Literal '%' in month format", d.month(),
  214. std::string("Oct %b"),
  215. std::locale(std::locale::classic(), datefacet));
  216. }
  217. {
  218. date_facet* datefacet = new date_facet();
  219. datefacet->month_format("%b %%%b");
  220. teststreaming("Multiple literal '%'s in month format", d.month(),
  221. std::string("Oct %Oct"),
  222. std::locale(std::locale::classic(), datefacet));
  223. }
  224. {//weekday
  225. date_facet* datefacet = new date_facet();
  226. datefacet->weekday_format("%a %%a");
  227. teststreaming("Literal '%' in weekday format", d.day_of_week(),
  228. std::string("Wed %a"),
  229. std::locale(std::locale::classic(), datefacet));
  230. }
  231. {
  232. date_facet* datefacet = new date_facet();
  233. datefacet->weekday_format("%a %%%a");
  234. teststreaming("Multiple literal '%'s in weekday format", d.day_of_week(),
  235. std::string("Wed %Wed"),
  236. std::locale(std::locale::classic(), datefacet));
  237. }
  238. date d_not_date(not_a_date_time);
  239. teststreaming("special value, no special facet", d_not_date, std::string("not-a-date-time"));
  240. // std::cout.imbue(std::locale(std::locale::classic(), datefacet));
  241. // std::cout << d << std::endl;
  242. }
  243. // date_generator tests
  244. {
  245. partial_date pd(31,Oct);
  246. teststreaming("partial date", pd, std::string("31 Oct"));
  247. first_kday_of_month fkd(Tuesday, Sep);
  248. teststreaming("first kday", fkd, std::string("first Tue of Sep"));
  249. nth_kday_of_month nkd2(nth_kday_of_month::second, Tuesday, Sep);
  250. teststreaming("nth kday", nkd2, std::string("second Tue of Sep"));
  251. nth_kday_of_month nkd3(nth_kday_of_month::third, Tuesday, Sep);
  252. teststreaming("nth kday", nkd3, std::string("third Tue of Sep"));
  253. nth_kday_of_month nkd4(nth_kday_of_month::fourth, Tuesday, Sep);
  254. teststreaming("nth kday", nkd4, std::string("fourth Tue of Sep"));
  255. nth_kday_of_month nkd5(nth_kday_of_month::fifth, Tuesday, Sep);
  256. teststreaming("nth kday", nkd5, std::string("fifth Tue of Sep"));
  257. last_kday_of_month lkd(Tuesday, Sep);
  258. teststreaming("last kday", lkd, std::string("last Tue of Sep"));
  259. first_kday_before fkb(Wednesday);
  260. teststreaming("First before", fkb, std::string("Wed before"));
  261. first_kday_after fka(Thursday);
  262. teststreaming("First after", fka, std::string("Thu after"));
  263. }
  264. #if !defined(BOOST_NO_STD_WSTRING)
  265. date d(2004,Oct, 13);
  266. date_period dp(d, d + days(7));
  267. date d_not_date(not_a_date_time);
  268. teststreaming("special value, no special facet wide", d_not_date,
  269. std::wstring(L"not-a-date-time"));
  270. {
  271. wdate_facet* wdatefacet = new wdate_facet();
  272. wdatefacet->format(wdate_facet::standard_format_specifier);
  273. teststreaming("widestream default classic date", d,
  274. std::wstring(L"10/13/04"),
  275. std::locale(std::locale::classic(), wdatefacet));
  276. }
  277. {
  278. wdate_facet* wdatefacet = new wdate_facet();
  279. wdatefacet->format(wdate_facet::standard_format_specifier);
  280. teststreaming("widestream default classic date period", dp,
  281. std::wstring(L"[10/13/04/10/19/04]"),
  282. std::locale(std::locale::classic(), wdatefacet));
  283. }
  284. {
  285. wdate_facet* wdatefacet = new wdate_facet();
  286. wdatefacet->format(L"%Y-%d-%b %a");
  287. teststreaming("widestream custom date facet", d,
  288. std::wstring(L"2004-13-Oct Wed"),
  289. std::locale(std::locale::classic(), wdatefacet));
  290. }
  291. {
  292. wdate_facet* wdatefacet = new wdate_facet();
  293. wdatefacet->format(L"%Y-%d-%b %a");
  294. teststreaming("widestream custom date facet date period", dp,
  295. std::wstring(L"[2004-13-Oct Wed/2004-19-Oct Tue]"),
  296. std::locale(std::locale::classic(), wdatefacet));
  297. }
  298. {
  299. wdate_facet* wdatefacet = new wdate_facet();
  300. wdatefacet->set_iso_extended_format();
  301. wperiod_formatter pf(wperiod_formatter::AS_OPEN_RANGE, L" / ", L"[ ", L" )", L" ]");
  302. wdatefacet->period_formatter(pf);
  303. teststreaming("custom date facet date period - open range custom delimeters", dp,
  304. std::wstring(L"[ 2004-10-13 / 2004-10-20 )"),
  305. std::locale(std::locale::classic(), wdatefacet));
  306. }
  307. /************* small gregorian types tests *************/
  308. wdate_facet* small_types_facet = new wdate_facet();
  309. std::locale loc = std::locale(std::locale::classic(), small_types_facet);
  310. // greg_year test
  311. greg_year gy(2004);
  312. teststreaming("greg_year", gy, std::string("2004"));
  313. // greg_month tests
  314. {
  315. for(greg_month::value_type i = 0; i < 12; ++i) {
  316. greg_month m(i+1); // month numbers 1-12
  317. teststreaming("greg_month short", m, short_months[i], loc);
  318. }
  319. small_types_facet->month_format(L"%B"); // full name
  320. for(greg_month::value_type i = 0; i < 12; ++i) {
  321. greg_month m(i+1); // month numbers 1-12
  322. teststreaming("greg_month full", m, full_months[i], loc);
  323. }
  324. }
  325. // greg_weekday tests
  326. {
  327. for(greg_weekday::value_type i = 0; i < 7; ++i) {
  328. greg_weekday gw(i); // weekday numbers 0-6
  329. teststreaming("greg_weekday short", gw, short_weekdays[i], loc);
  330. }
  331. small_types_facet->weekday_format(L"%A"); // full name
  332. for(greg_weekday::value_type i = 0; i < 7; ++i) {
  333. greg_weekday gw(i); // weekday numbers 0-6
  334. teststreaming("greg_weekday full", gw, full_weekdays[i], loc);
  335. }
  336. }
  337. #endif // BOOST_NO_STD_WSTRING
  338. return printTestStats();
  339. }