testtime_facet.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  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/posix_time/posix_time.hpp"
  9. #include "../testfrmwk.hpp"
  10. #include <fstream>
  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. #if !defined(BOOST_NO_STD_WSTRING)
  27. static const wchar_t* long_month_names[] =
  28. {L"Januar",L"Februar",L"Marz",L"April",L"Mai",L"Juni",L"Juli",L"August",
  29. L"September",L"Oktober",L"November",L"Dezember"};
  30. static const wchar_t* short_month_names[]=
  31. {L"Jan",L"Feb",L"Mar",L"Apr",L"Mai",L"Jun",L"Jul",L"Aug",
  32. L"Sep",L"Okt",L"Nov",L"Dez"};
  33. std::vector<std::basic_string<wchar_t> > de_short_month_names;
  34. std::vector<std::basic_string<wchar_t> > de_long_month_names;
  35. #endif //
  36. int main() {
  37. using namespace boost::gregorian;
  38. using namespace boost::posix_time;
  39. try {
  40. date d(2004,Oct,13);
  41. date min_date(min_date_time);
  42. date max_date(max_date_time);
  43. date_period dp(d, d + date_duration(7));
  44. ptime t(d, time_duration(18,01,56));
  45. ptime tf = t + microseconds(3);
  46. time_period tp(t, tf + days(7) + time_duration(1,1,1));
  47. time_duration td = hours(3) + minutes(2) + seconds(1) + milliseconds(9);
  48. time_duration longer_td = hours(10) + minutes(22) + seconds(15) + milliseconds(980); // two characters in hours
  49. time_duration long_td = hours(300) + minutes(2) + seconds(1) + milliseconds(9); // more than two characters in hours
  50. {
  51. std::stringstream ss;
  52. ss << t;
  53. check("Stream and to_string formats match (ptime)",
  54. to_simple_string(t) == ss.str());
  55. std::cout << t << ' ' << td << std::endl;
  56. ss.str("");
  57. ss << tf;
  58. check("Stream and to_string formats match (ptime w/ fracsec)",
  59. to_simple_string(tf) == ss.str());
  60. ss.str("");
  61. ss << tp;
  62. check("Stream and to_string formats match (time_period)",
  63. to_simple_string(tp) == ss.str());
  64. ss.str("");
  65. ss << td;
  66. check("Stream and to_string formats match (time_duration)",
  67. to_simple_string(td) == ss.str());
  68. std::cout << ss.str() << std::endl;
  69. time_facet* f = new time_facet();
  70. ss.imbue(std::locale(ss.getloc(), f));
  71. ss.str("");
  72. f->format("%Y-%b-%d %H:%M:%S %%d");
  73. f->time_duration_format("%H:%M:%S %%S");
  74. ss << t;
  75. check("Literal '%' in format", ss.str() == std::string("2004-Oct-13 18:01:56 %d"));
  76. ss.str("");
  77. ss << td;
  78. check("Literal '%' in time_duration format", ss.str() == std::string("03:02:01 %S"));
  79. ss.str("");
  80. f->format("%Y-%b-%d %H:%M:%S %%%d");
  81. f->time_duration_format("%H:%M:%S %%%S");
  82. ss << t;
  83. check("Multiple literal '%'s in format", ss.str() == std::string("2004-Oct-13 18:01:56 %13"));
  84. ss.str("");
  85. ss << td;
  86. check("Multiple literal '%'s in time_duration format", ss.str() == std::string("03:02:01 %01"));
  87. ss.str("");
  88. // Longer time durations
  89. f->time_duration_format("%H:%M:%S");
  90. ss << longer_td;
  91. check("Longer time durations", ss.str() == std::string("10:22:15"));
  92. ss.str("");
  93. // Long time durations
  94. f->time_duration_format("%O:%M:%S");
  95. ss << long_td;
  96. check("Long time durations", ss.str() == std::string("300:02:01"));
  97. ss.str("");
  98. // Short-hand format specifiers
  99. f->format("%T");
  100. f->time_duration_format("%T");
  101. ss << t;
  102. check("Short-hand '%T' in time format", ss.str() == std::string("18:01:56"));
  103. ss.str("");
  104. ss << td;
  105. check("Short-hand '%T' in time_duration format", ss.str() == std::string("03:02:01"));
  106. ss.str("");
  107. f->format("%R");
  108. f->time_duration_format("%R");
  109. ss << t;
  110. check("Short-hand '%R' in time format", ss.str() == std::string("18:01"));
  111. ss.str("");
  112. ss << td;
  113. check("Short-hand '%R' in time_duration format", ss.str() == std::string("03:02"));
  114. ss.str("");
  115. }
  116. { // negative time_duration tests
  117. std::string result;
  118. std::stringstream ss;
  119. time_duration td1(2,0,0);
  120. time_duration td2(1,0,0);
  121. ss << td2 - td1;
  122. result = "-01:00:00";
  123. check("Negative time_duration", result == ss.str());
  124. ss.str("");
  125. time_duration td3(0,2,0);
  126. time_duration td4(0,1,0);
  127. ss << td4 - td3;
  128. result = "-00:01:00";
  129. check("Negative time_duration", result == ss.str());
  130. ss.str("");
  131. time_duration td5(0,0,2);
  132. time_duration td6(0,0,1);
  133. ss << td6 - td5;
  134. result = "-00:00:01";
  135. check("Negative time_duration", result == ss.str());
  136. ss.str("");
  137. #if defined(BOOST_DATE_TIME_POSIX_TIME_STD_CONFIG)
  138. result = "-00:00:00.000000001";
  139. #else
  140. result = "-00:00:00.000001";
  141. #endif
  142. time_duration td7(0,0,0,123);
  143. time_duration td8(0,0,0,122);
  144. ss << td8 - td7;
  145. check("Negative time_duration: " + ss.str(), result == ss.str());
  146. //reset the sign to always print
  147. time_facet* f = new time_facet();
  148. ss.imbue(std::locale(ss.getloc(), f));
  149. f->time_duration_format("%+%H:%M:%S""%F");
  150. ss.str("");
  151. ss << td4 - td3;
  152. result = "-00:01:00";
  153. check("Negative time_duration sign always: " + ss.str(), result == ss.str());
  154. ss.str("");
  155. ss << td3 - td4;
  156. result = "+00:01:00";
  157. check("time_duration sign always: " + ss.str(), result == ss.str());
  158. #if defined(BOOST_DATE_TIME_POSIX_TIME_STD_CONFIG)
  159. result = "-00:00:00.000000001";
  160. #else
  161. result = "-00:00:00.000001";
  162. #endif
  163. ss.str("");
  164. ss << td8 - td7;
  165. check("Negative time_duration: " + ss.str(), result == ss.str());
  166. #if defined(BOOST_DATE_TIME_POSIX_TIME_STD_CONFIG)
  167. result = "+00:00:00.000000001";
  168. #else
  169. result = "+00:00:00.000001";
  170. #endif
  171. ss.str("");
  172. ss << td7 - td8;
  173. check("time_duration sign bit always: " + ss.str(), result == ss.str());
  174. f->time_duration_format("%-%H hours and %-%M minutes");
  175. ss.str("");
  176. ss << td4 - td3;
  177. result = "-00 hours and -01 minutes";
  178. check("Negative time_duration two sign flags" + ss.str(), result == ss.str());
  179. ss.str("");
  180. // Longer time durations
  181. f->time_duration_format("%-%H:%M:%S");
  182. ss << -longer_td;
  183. check("Longer negative time durations", ss.str() == std::string("-10:22:15"));
  184. ss.str("");
  185. // Long time durations
  186. f->time_duration_format("%-%O:%M:%S");
  187. ss << -long_td;
  188. check("Long negative time durations", ss.str() == std::string("-300:02:01"));
  189. ss.str("");
  190. }
  191. // The test verifies that #2698 is fixed. That is, the time and date facet should
  192. // not dereference end() iterator for the format string in do_put_tm.
  193. {
  194. boost::gregorian::date date(2009, 1, 1);
  195. boost::posix_time::time_duration tdx(0, 0, 0, 0);
  196. boost::posix_time::ptime boost_time(date, tdx);
  197. std::stringstream sstr;
  198. boost::posix_time::time_facet* pFacet = new boost::posix_time::time_facet("");
  199. sstr.imbue(std::locale(std::locale::classic(), pFacet));
  200. sstr << boost_time;
  201. }
  202. #if !defined(BOOST_NO_STD_WSTRING)
  203. std::copy(&short_month_names[0],
  204. &short_month_names[12],
  205. std::back_inserter(de_short_month_names));
  206. std::copy(&long_month_names[0],
  207. &long_month_names[12],
  208. std::back_inserter(de_long_month_names));
  209. {
  210. wtime_facet *timefacet = new wtime_facet(wtime_facet::standard_format);
  211. teststreaming("widestream default classic time", t,
  212. //std::wstring(L"Wed Oct 13 18:01:56 2004"),
  213. std::wstring(L"10/13/04 18:01:56"),
  214. std::locale(std::locale::classic(), timefacet));
  215. }
  216. {
  217. wtime_facet *timefacet = new wtime_facet(wtime_facet::standard_format);
  218. teststreaming("widestream default classic time with fractional seconds truncated", t,
  219. //std::wstring(L"Wed Oct 13 18:01:56 2004"),
  220. std::wstring(L"10/13/04 18:01:56"),
  221. std::locale(std::locale::classic(), timefacet));
  222. }
  223. {
  224. wtime_facet *timefacet = new wtime_facet(wtime_facet::standard_format);
  225. teststreaming("widestream default time period with fractional seconds truncated", tp,
  226. //std::wstring(L"[Wed Oct 13 18:01:56 2004/Wed Oct 20 19:02:57 2004]"),
  227. std::wstring(L"[10/13/04 18:01:56/10/20/04 19:02:57]"),
  228. std::locale(std::locale::classic(), timefacet));
  229. }
  230. {
  231. wtime_facet *timefacet = new wtime_facet(L"%Y-%b-%d %I:%M:%S %p");
  232. teststreaming("widestream time in 12 hours format w/ (AM/PM)", tp.begin(),
  233. std::wstring(L"2004-Oct-13 06:01:56 PM"),
  234. std::locale(std::locale::classic(), timefacet));
  235. }
  236. {
  237. wtime_facet *timefacet = new wtime_facet(wtime_facet::standard_format);
  238. #ifdef BOOST_DATE_TIME_HAS_NANOSECONDS
  239. teststreaming("widestream time duration", td,
  240. std::wstring(L"03:02:01.009000000"),
  241. std::locale(std::locale::classic(), timefacet));
  242. #else
  243. teststreaming("widestream time duration", td,
  244. std::wstring(L"03:02:01.009000"),
  245. std::locale(std::locale::classic(), timefacet));
  246. #endif // BOOST_DATE_TIME_HAS_NANOSECONDS
  247. }
  248. #ifdef BOOST_DATE_TIME_HAS_NANOSECONDS
  249. teststreaming("widestream time duration", td,
  250. std::wstring(L"03:02:01.009000000"));
  251. #else
  252. teststreaming("widestream time duration", td,
  253. std::wstring(L"03:02:01.009000"));
  254. #endif // BOOST_DATE_TIME_HAS_NANOSECONDS
  255. //wtime_facet *timefacet = new wtime_facet();
  256. //std::locale cloc = std::locale(std::locale::classic(), timefacet);
  257. //ss.imbue(cloc);
  258. // ss << L"classic date: " << d << std::endl;
  259. // ss << L"classic dateperiod: " << dp << std::endl;
  260. //ss << L"classic time: " << t << std::endl;
  261. //ss << L"classic timefrac: " << tf << std::endl;
  262. //ss << L"classic timeperiod: " << tp << std::endl;
  263. {
  264. wtime_facet* wtimefacet = new wtime_facet(L"day: %j date: %Y-%b-%d weekday: %A time: %H:%M:%s");
  265. #ifdef BOOST_DATE_TIME_HAS_NANOSECONDS
  266. teststreaming("widestream custom time facet narly format", t,
  267. std::wstring(L"day: 287 date: 2004-Oct-13 weekday: Wednesday time: 18:01:56.000000000"),
  268. std::locale(std::locale::classic(), wtimefacet));
  269. #else
  270. teststreaming("widestream custom time facet narly format", t,
  271. std::wstring(L"day: 287 date: 2004-Oct-13 weekday: Wednesday time: 18:01:56.000000"),
  272. std::locale(std::locale::classic(), wtimefacet));
  273. #endif
  274. }
  275. {
  276. wtime_facet* wtimefacet = new wtime_facet(L"%Y-%b-%d %H:%M:%S,%f");
  277. #ifdef BOOST_DATE_TIME_HAS_NANOSECONDS
  278. teststreaming("widestream custom time fractional seconds: %Y-%b-%d %H:%M:%S,%f", t,
  279. std::wstring(L"2004-Oct-13 18:01:56,000000000"),
  280. std::locale(std::locale::classic(), wtimefacet));
  281. #else
  282. teststreaming("widestream custom time fractional seconds: %Y-%b-%d %H:%M:%S,%f", t,
  283. std::wstring(L"2004-Oct-13 18:01:56,000000"),
  284. std::locale(std::locale::classic(), wtimefacet));
  285. #endif
  286. }
  287. {
  288. wtime_facet* wtimefacet = new wtime_facet(L"%Y-%b-%d %H:%M:%S");
  289. teststreaming("widestream custom time no frac seconds: %Y-%b-%d %H:%M:%S", t,
  290. std::wstring(L"2004-Oct-13 18:01:56"),
  291. std::locale(std::locale::classic(), wtimefacet));
  292. }
  293. {
  294. wtime_facet* wtimefacet = new wtime_facet(L"%Y-%b-%d %H:%M:%S");
  295. wtimefacet->short_month_names(de_short_month_names);
  296. teststreaming("widestream custom time no frac seconds, german months: %Y-%b-%d %H:%M:%S", t,
  297. std::wstring(L"2004-Okt-13 18:01:56"),
  298. std::locale(std::locale::classic(), wtimefacet));
  299. }
  300. {
  301. wtime_facet* wtimefacet = new wtime_facet();
  302. wtimefacet->format(L"%B %b %Y");
  303. wtimefacet->short_month_names(de_short_month_names);
  304. wtimefacet->long_month_names(de_long_month_names);
  305. teststreaming("widestream custom time no frac seconds, german months: %B %b %Y", t,
  306. std::wstring(L"Oktober Okt 2004"),
  307. std::locale(std::locale::classic(), wtimefacet));
  308. }
  309. {
  310. wtime_facet* wtimefacet = new wtime_facet(L"%Y-%b-%d %H:%M:%S" L"%F");
  311. teststreaming("widestream custom time no frac seconds %F operator: %Y-%b-%d %H:%M:%S""%F", t,
  312. std::wstring(L"2004-Oct-13 18:01:56"),
  313. std::locale(std::locale::classic(), wtimefacet));
  314. }
  315. {
  316. wtime_facet* wtimefacet = new wtime_facet(L"%Y-%b-%d %H:%M:%S" L"%F");
  317. #ifdef BOOST_DATE_TIME_HAS_NANOSECONDS
  318. teststreaming("widestream custom time with frac seconds %F operator: %Y-%b-%d %H:%M:%S""%F", tf,
  319. std::wstring(L"2004-Oct-13 18:01:56.000003000"),
  320. std::locale(std::locale::classic(), wtimefacet));
  321. #else
  322. teststreaming("widestream custom time with frac seconds %F operator: %Y-%b-%d %H:%M:%S""%F", tf,
  323. std::wstring(L"2004-Oct-13 18:01:56.000003"),
  324. std::locale(std::locale::classic(), wtimefacet));
  325. #endif // BOOST_DATE_TIME_HAS_NANOSECONDS
  326. }
  327. {
  328. wtime_facet* wtimefacet = new wtime_facet();
  329. wtimefacet->set_iso_format();
  330. #ifdef BOOST_DATE_TIME_HAS_NANOSECONDS
  331. teststreaming("widestream custom time iso format", tf,
  332. std::wstring(L"20041013T180156.000003000"),
  333. std::locale(std::locale::classic(), wtimefacet));
  334. #else
  335. teststreaming("widestream custom time iso format", tf,
  336. std::wstring(L"20041013T180156.000003"),
  337. std::locale(std::locale::classic(), wtimefacet));
  338. #endif // BOOST_DATE_TIME_HAS_NANOSECONDS
  339. }
  340. {
  341. wtime_facet* wtimefacet = new wtime_facet();
  342. wtimefacet->set_iso_extended_format();
  343. #ifdef BOOST_DATE_TIME_HAS_NANOSECONDS
  344. teststreaming("widestream custom time iso extended format", tf,
  345. std::wstring(L"2004-10-13 18:01:56.000003000"),
  346. std::locale(std::locale::classic(), wtimefacet));
  347. #else
  348. teststreaming("widestream custom time iso extended format", tf,
  349. std::wstring(L"2004-10-13 18:01:56.000003"),
  350. std::locale(std::locale::classic(), wtimefacet));
  351. #endif // BOOST_DATE_TIME_HAS_NANOSECONDS
  352. }
  353. {
  354. wtime_facet* wtimefacet = new wtime_facet(L"%Y-%b-%d %H:%M:%S" L"%F");
  355. #ifdef BOOST_DATE_TIME_HAS_NANOSECONDS
  356. teststreaming("widestream time period frac seconds %F operator: %Y-%b-%d %H:%M:%S""%F", tp,
  357. std::wstring(L"[2004-Oct-13 18:01:56/2004-Oct-20 19:02:57.000002999]"),
  358. std::locale(std::locale::classic(), wtimefacet));
  359. #else
  360. teststreaming("widestream time period frac seconds %F operator: %Y-%b-%d %H:%M:%S""%F", tp,
  361. std::wstring(L"[2004-Oct-13 18:01:56/2004-Oct-20 19:02:57.000002]"),
  362. std::locale(std::locale::classic(), wtimefacet));
  363. #endif // BOOST_DATE_TIME_HAS_NANOSECONDS
  364. }
  365. {
  366. wtime_facet* wtimefacet = new wtime_facet(L"%Y-%b-%d %H:%M:%s");
  367. wperiod_formatter pf(wperiod_formatter::AS_OPEN_RANGE, L" / ", L"[ ", L" )", L" ]");
  368. wtimefacet->period_formatter(pf);
  369. #ifdef BOOST_DATE_TIME_HAS_NANOSECONDS
  370. teststreaming("widestream custom time : %Y-%b-%d %H:%M:%s", tp,
  371. std::wstring(L"[ 2004-Oct-13 18:01:56.000000000 / 2004-Oct-20 19:02:57.000003000 )"),
  372. std::locale(std::locale::classic(), wtimefacet));
  373. #else
  374. teststreaming("widestream custom time : %Y-%b-%d %H:%M:%s", tp,
  375. std::wstring(L"[ 2004-Oct-13 18:01:56.000000 / 2004-Oct-20 19:02:57.000003 )"),
  376. std::locale(std::locale::classic(), wtimefacet));
  377. #endif // BOOST_DATE_TIME_HAS_NANOSECONDS
  378. }
  379. {
  380. ptime nt(not_a_date_time);
  381. teststreaming("widestream custom time : not a datetime", nt,
  382. std::wstring(L"not-a-date-time"));
  383. }
  384. // //Denmark English has iso extended default format...
  385. // std::locale gloc("en_DK");
  386. // ss.imbue(gloc);
  387. // ss << L"default english-denmark date: " << d << std::endl;
  388. // ss << L"default english-denmark dateperiod: " << dp << std::endl;
  389. // ss << L"default english-denmark time: " << t << std::endl;
  390. // ss << L"default english-denmark timefrac: " << tf << std::endl;
  391. // ss << L"default english-denmark timeperiod: " << tp << std::endl;
  392. #endif
  393. }
  394. catch(std::exception& e) {
  395. std::cout << "Caught std::exception: " << e.what() << std::endl;
  396. }
  397. catch(...) {
  398. std::cout << "bad exception" << std::endl;
  399. }
  400. return printTestStats();
  401. }