test_formatting.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  1. //
  2. // Copyright (c) 2009-2011 Artyom Beilis (Tonkikh)
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See
  5. // accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. //
  8. #ifndef BOOST_LOCALE_WITH_ICU
  9. #include <iostream>
  10. int main()
  11. {
  12. std::cout << "ICU is not build... Skipping" << std::endl;
  13. }
  14. #else
  15. #ifdef _MSC_VER
  16. #define _CRT_SECURE_NO_WARNINGS
  17. // Disable this "security crap"
  18. #endif
  19. #include <boost/locale/formatting.hpp>
  20. #include <boost/locale/format.hpp>
  21. #include <boost/locale/date_time.hpp>
  22. #include <boost/locale/generator.hpp>
  23. #include "test_locale.hpp"
  24. #include "test_locale_tools.hpp"
  25. #include <sstream>
  26. #include <iostream>
  27. #include <iomanip>
  28. #include <limits>
  29. #include <unicode/uversion.h>
  30. using namespace boost::locale;
  31. //#define TEST_DEBUG
  32. #ifdef BOOST_MSVC
  33. #define _CRT_SECURE_NO_WARNINGS
  34. #endif
  35. #ifdef TEST_DEBUG
  36. #undef BOOST_LOCALE_ENABLE_CHAR16_T
  37. #undef BOOST_LOCALE_ENABLE_CHAR32_T
  38. template<typename T>
  39. void print_diff(T const &,T const &,int)
  40. {
  41. }
  42. template<>
  43. void print_diff(std::string const &l,std::string const &r,int line)
  44. {
  45. if(l!=r) {
  46. std::cerr << "----[" << l <<"]!=\n----["<<r<<"] in " << line << std::endl;
  47. }
  48. }
  49. #define TESTEQ(x,y) do { print_diff((x),(y),__LINE__); TEST((x)==(y)); } while(0)
  50. #else
  51. #define TESTEQ(x,y) TEST((x)==(y))
  52. #endif
  53. #define TEST_FMT(manip,value,expected) \
  54. do{ \
  55. std::basic_ostringstream<CharType> ss; \
  56. ss.imbue(loc); \
  57. ss << manip << value; \
  58. TESTEQ(ss.str(),to_correct_string<CharType>(expected,loc)); \
  59. }while(0)
  60. #ifndef _LIBCPP_VERSION
  61. static bool parsing_fails()
  62. {
  63. return true;
  64. }
  65. #else
  66. static bool parsing_fails()
  67. {
  68. static bool checked=false;
  69. static bool fails;
  70. if(!checked) {
  71. try {
  72. std::istringstream ss("x");
  73. ss.exceptions(std::ios_base::failbit);
  74. int x;
  75. ss>>x;
  76. fails =false;
  77. }
  78. catch(std::ios_base::failure const &) {
  79. fails=true;
  80. }
  81. catch(...) {
  82. fails=false;
  83. }
  84. checked=true;
  85. if(!fails) {
  86. std::cerr << "!!! Warning: libc++ library does not throw an exception on failbit !!!" << std::endl;
  87. }
  88. }
  89. return fails;
  90. }
  91. #endif
  92. #define TEST_NOPAR(manip,actual,type) \
  93. do{ \
  94. type v; \
  95. std::basic_string<CharType> act= \
  96. to_correct_string<CharType>(actual,loc); \
  97. { \
  98. std::basic_istringstream<CharType> ss; \
  99. ss.imbue(loc); \
  100. ss.str(act); \
  101. ss >> manip >> v ; \
  102. TEST(ss.fail()); \
  103. } \
  104. if(parsing_fails()){ \
  105. std::basic_istringstream<CharType> ss; \
  106. ss.imbue(loc); \
  107. ss.str(act); \
  108. ss.exceptions(std::ios_base::failbit); \
  109. ss >> manip; \
  110. TEST_THROWS(ss >> v,std::ios_base::failure); \
  111. } \
  112. }while(0)
  113. #define TEST_PAR(manip,type,actual,expected) \
  114. do{ \
  115. type v; \
  116. {std::basic_istringstream<CharType> ss; \
  117. ss.imbue(loc); \
  118. ss.str(to_correct_string<CharType>(actual,loc)); \
  119. ss >> manip >> v >> std::ws; \
  120. TESTEQ(v,expected); \
  121. TEST(ss.eof()); }\
  122. {std::basic_istringstream<CharType> ss; \
  123. ss.imbue(loc); \
  124. ss.str(to_correct_string<CharType>(std::string(actual)+"@",loc)); \
  125. CharType tmp_c; \
  126. ss >> manip >> v >> std::skipws >> tmp_c; \
  127. TESTEQ(v,expected); \
  128. TEST(tmp_c=='@'); } \
  129. }while(0)
  130. #define TEST_FP1(manip,value_in,str,type,value_out) \
  131. do { \
  132. TEST_FMT(manip,value_in,str); \
  133. TEST_PAR(manip,type,str,value_out); \
  134. }while(0)
  135. #define TEST_FP2(m1,m2,value_in,str,type,value_out) \
  136. do { \
  137. TEST_FMT(m1<<m2,value_in,str); \
  138. TEST_PAR(m1>>m2,type,str,value_out); \
  139. }while(0)
  140. #define TEST_FP3(m1,m2,m3,value_in,str,type,value_out) \
  141. do { \
  142. TEST_FMT(m1<<m2<<m3,value_in,str); \
  143. TEST_PAR(m1>>m2>>m3,type,str,value_out); \
  144. }while(0)
  145. #define TEST_FP4(m1,m2,m3,m4,value_in,str,type,value_out) \
  146. do { \
  147. TEST_FMT(m1<<m2<<m3<<m4,value_in,str); \
  148. TEST_PAR(m1>>m2>>m3>>m4,type,str,value_out); \
  149. }while(0)
  150. #define FORMAT(f,v,exp) \
  151. do{\
  152. std::basic_ostringstream<CharType> ss; \
  153. ss.imbue(loc); \
  154. std::basic_string<CharType> fmt = to_correct_string<CharType>(f,loc); \
  155. ss << boost::locale::basic_format<CharType>(fmt) % v; \
  156. TESTEQ(ss.str(),to_correct_string<CharType>(exp,loc)); \
  157. ss.str(to_correct_string<CharType>("",loc)); \
  158. ss << boost::locale::basic_format<CharType>(boost::locale::translate(fmt.c_str())) % v; \
  159. /*ss << boost::locale::basic_format<CharType>(fmt) % v; */ \
  160. TESTEQ(ss.str(),to_correct_string<CharType>(exp,loc)); \
  161. TESTEQ( (boost::locale::basic_format<CharType>(fmt) % v).str(loc),to_correct_string<CharType>(exp,loc)); \
  162. } while(0)
  163. #define TEST_MIN_MAX_FMT(type,minval,maxval) \
  164. do { \
  165. TEST_FMT(as::number,std::numeric_limits<type>::min(),minval); \
  166. TEST_FMT(as::number,std::numeric_limits<type>::max(),maxval); \
  167. }while(0)
  168. #define TEST_MIN_MAX_PAR(type,minval,maxval) \
  169. do {\
  170. TEST_PAR(as::number,type,minval,std::numeric_limits<type>::min()); \
  171. TEST_PAR(as::number,type,maxval,std::numeric_limits<type>::max()); \
  172. }while(0)
  173. #define TEST_MIN_MAX(type,minval,maxval) \
  174. do { \
  175. TEST_MIN_MAX_FMT(type,minval,maxval); \
  176. TEST_MIN_MAX_PAR(type,minval,maxval); \
  177. }while(0)
  178. #define BOOST_ICU_VER (U_ICU_VERSION_MAJOR_NUM*100 + U_ICU_VERSION_MINOR_NUM)
  179. #define BOOST_ICU_EXACT_VER (U_ICU_VERSION_MAJOR_NUM*10000 + U_ICU_VERSION_MINOR_NUM * 100 + U_ICU_VERSION_PATCHLEVEL_NUM)
  180. bool short_parsing_fails()
  181. {
  182. static bool fails = false;
  183. static bool get_result = false;
  184. if(get_result)
  185. return fails;
  186. std::stringstream ss("65000");
  187. ss.imbue(std::locale::classic());
  188. short v=0;
  189. ss >> v;
  190. fails = ss.fail();
  191. get_result = true;
  192. return fails;
  193. }
  194. template<typename CharType>
  195. void test_manip(std::string e_charset="UTF-8")
  196. {
  197. boost::locale::generator g;
  198. std::locale loc=g("en_US."+e_charset);
  199. TEST_FP1(as::posix,1200.1,"1200.1",double,1200.1);
  200. TEST_FP1(as::number,1200.1,"1,200.1",double,1200.1);
  201. TEST_FMT(as::number<<std::setfill(CharType('_'))<<std::setw(6),1534,"_1,534");
  202. TEST_FMT(as::number<<std::left<<std::setfill(CharType('_'))<<std::setw(6),1534,"1,534_");
  203. // Ranges
  204. if(sizeof(short) == 2) {
  205. TEST_MIN_MAX(short,"-32,768","32,767");
  206. TEST_MIN_MAX(unsigned short,"0","65,535");
  207. TEST_NOPAR(as::number,"-1",unsigned short);
  208. if(short_parsing_fails()) {
  209. TEST_NOPAR(as::number,"65,535",short);
  210. }
  211. }
  212. if(sizeof(int)==4) {
  213. TEST_MIN_MAX(int,"-2,147,483,648","2,147,483,647");
  214. TEST_MIN_MAX(unsigned int,"0","4,294,967,295");
  215. TEST_NOPAR(as::number,"-1",unsigned int);
  216. TEST_NOPAR(as::number,"4,294,967,295",int);
  217. }
  218. if(sizeof(long)==4) {
  219. TEST_MIN_MAX(long,"-2,147,483,648","2,147,483,647");
  220. TEST_MIN_MAX(unsigned long,"0","4,294,967,295");
  221. TEST_NOPAR(as::number,"-1",unsigned long);
  222. TEST_NOPAR(as::number,"4,294,967,295",long);
  223. }
  224. if(sizeof(long)==8) {
  225. TEST_MIN_MAX(long,"-9,223,372,036,854,775,808","9,223,372,036,854,775,807");
  226. TEST_MIN_MAX_FMT(unsigned long,"0","18446744073709551615"); // Unsupported range by icu - ensure fallback
  227. TEST_NOPAR(as::number,"-1",unsigned long);
  228. }
  229. #ifndef BOOST_NO_LONG_LONG
  230. if(sizeof(long long)==8) {
  231. TEST_MIN_MAX(long long,"-9,223,372,036,854,775,808","9,223,372,036,854,775,807");
  232. // we can't really parse this as ICU does not support this range, only format
  233. TEST_MIN_MAX_FMT(unsigned long long,"0","18446744073709551615"); // Unsupported range by icu - ensure fallback
  234. TEST_FMT(as::number,9223372036854775807ULL,"9,223,372,036,854,775,807");
  235. TEST_FMT(as::number,9223372036854775808ULL,"9223372036854775808"); // Unsupported range by icu - ensure fallback
  236. TEST_NOPAR(as::number,"-1",unsigned long long);
  237. }
  238. #endif
  239. TEST_FP3(as::number,std::left,std::setw(3),15,"15 ",int,15);
  240. TEST_FP3(as::number,std::right,std::setw(3),15," 15",int,15);
  241. TEST_FP3(as::number,std::setprecision(3),std::fixed,13.1,"13.100",double,13.1);
  242. #if BOOST_ICU_VER < 5601
  243. // bug #13276
  244. TEST_FP3(as::number,std::setprecision(3),std::scientific,13.1,"1.310E1",double,13.1);
  245. #endif
  246. TEST_NOPAR(as::number,"",int);
  247. TEST_NOPAR(as::number,"--3",int);
  248. TEST_NOPAR(as::number,"y",int);
  249. TEST_FP1(as::percent,0.1,"10%",double,0.1);
  250. TEST_FP3(as::percent,std::fixed,std::setprecision(1),0.10,"10.0%",double,0.1);
  251. TEST_NOPAR(as::percent,"1",double);
  252. TEST_FP1(as::currency,1345,"$1,345.00",int,1345);
  253. TEST_FP1(as::currency,1345.34,"$1,345.34",double,1345.34);
  254. TEST_NOPAR(as::currency,"$",double);
  255. #if BOOST_ICU_VER >= 402
  256. TEST_FP2(as::currency,as::currency_national,1345,"$1,345.00",int,1345);
  257. TEST_FP2(as::currency,as::currency_national,1345.34,"$1,345.34",double,1345.34);
  258. TEST_FP2(as::currency,as::currency_iso,1345,"USD1,345.00",int,1345);
  259. TEST_FP2(as::currency,as::currency_iso,1345.34,"USD1,345.34",double,1345.34);
  260. #endif
  261. TEST_FP1(as::spellout,10,"ten",int,10);
  262. #if 402 <= BOOST_ICU_VER && BOOST_ICU_VER < 408
  263. if(e_charset=="UTF-8") {
  264. TEST_FMT(as::ordinal,1,"1\xcb\xa2\xe1\xb5\x97"); // 1st with st as ligatures
  265. }
  266. #else
  267. TEST_FMT(as::ordinal,1,"1st");
  268. #endif
  269. time_t a_date = 3600*24*(31+4); // Feb 5th
  270. time_t a_time = 3600*15+60*33; // 15:33:05
  271. time_t a_timesec = 13;
  272. time_t a_datetime = a_date + a_time + a_timesec;
  273. TEST_FP2(as::date, as::gmt,a_datetime,"Feb 5, 1970",time_t,a_date);
  274. TEST_FP3(as::date,as::date_short ,as::gmt,a_datetime,"2/5/70",time_t,a_date);
  275. TEST_FP3(as::date,as::date_medium,as::gmt,a_datetime,"Feb 5, 1970",time_t,a_date);
  276. TEST_FP3(as::date,as::date_long ,as::gmt,a_datetime,"February 5, 1970",time_t,a_date);
  277. TEST_FP3(as::date,as::date_full ,as::gmt,a_datetime,"Thursday, February 5, 1970",time_t,a_date);
  278. TEST_NOPAR(as::date>>as::date_short,"aa/bb/cc",double);
  279. #if BOOST_ICU_VER >= 5901
  280. #define GMT_FULL "Greenwich Mean Time"
  281. #else
  282. #define GMT_FULL "GMT"
  283. #endif
  284. TEST_FP2(as::time, as::gmt,a_datetime,"3:33:13 PM",time_t,a_time+a_timesec);
  285. TEST_FP3(as::time,as::time_short ,as::gmt,a_datetime,"3:33 PM",time_t,a_time);
  286. TEST_FP3(as::time,as::time_medium,as::gmt,a_datetime,"3:33:13 PM",time_t,a_time+a_timesec);
  287. #if BOOST_ICU_VER >= 408
  288. TEST_FP3(as::time,as::time_long ,as::gmt,a_datetime,"3:33:13 PM GMT",time_t,a_time+a_timesec);
  289. #if BOOST_ICU_EXACT_VER != 40800
  290. // know bug #8675
  291. TEST_FP3(as::time,as::time_full ,as::gmt,a_datetime,"3:33:13 PM " GMT_FULL,time_t,a_time+a_timesec);
  292. #endif
  293. #else
  294. TEST_FP3(as::time,as::time_long ,as::gmt,a_datetime,"3:33:13 PM GMT+00:00",time_t,a_time+a_timesec);
  295. TEST_FP3(as::time,as::time_full ,as::gmt,a_datetime,"3:33:13 PM GMT+00:00",time_t,a_time+a_timesec);
  296. #endif
  297. TEST_NOPAR(as::time,"AM",double);
  298. TEST_FP2(as::time, as::time_zone("GMT+01:00"),a_datetime,"4:33:13 PM",time_t,a_time+a_timesec);
  299. TEST_FP3(as::time,as::time_short ,as::time_zone("GMT+01:00"),a_datetime,"4:33 PM",time_t,a_time);
  300. TEST_FP3(as::time,as::time_medium,as::time_zone("GMT+01:00"),a_datetime,"4:33:13 PM",time_t,a_time+a_timesec);
  301. #if U_ICU_VERSION_MAJOR_NUM >= 52
  302. #define GMT_P100 "GMT+1"
  303. #else
  304. #define GMT_P100 "GMT+01:00"
  305. #endif
  306. #if U_ICU_VERSION_MAJOR_NUM >= 50
  307. #define PERIOD ","
  308. #define ICUAT " at"
  309. #else
  310. #define PERIOD ""
  311. #define ICUAT ""
  312. #endif
  313. TEST_FP3(as::time,as::time_long ,as::time_zone("GMT+01:00"),a_datetime,"4:33:13 PM " GMT_P100,time_t,a_time+a_timesec);
  314. #if BOOST_ICU_VER == 308 && defined(__CYGWIN__)
  315. // Known faliture ICU issue
  316. #else
  317. TEST_FP3(as::time,as::time_full ,as::time_zone("GMT+01:00"),a_datetime,"4:33:13 PM GMT+01:00",time_t,a_time+a_timesec);
  318. #endif
  319. TEST_FP2(as::datetime, as::gmt,a_datetime,"Feb 5, 1970" PERIOD " 3:33:13 PM",time_t,a_datetime);
  320. TEST_FP4(as::datetime,as::date_short ,as::time_short ,as::gmt,a_datetime,"2/5/70" PERIOD " 3:33 PM",time_t,a_date+a_time);
  321. TEST_FP4(as::datetime,as::date_medium,as::time_medium,as::gmt,a_datetime,"Feb 5, 1970" PERIOD " 3:33:13 PM",time_t,a_datetime);
  322. #if BOOST_ICU_VER >= 408
  323. TEST_FP4(as::datetime,as::date_long ,as::time_long ,as::gmt,a_datetime,"February 5, 1970" ICUAT " 3:33:13 PM GMT",time_t,a_datetime);
  324. #if BOOST_ICU_EXACT_VER != 40800
  325. // know bug #8675
  326. TEST_FP4(as::datetime,as::date_full ,as::time_full ,as::gmt,a_datetime,"Thursday, February 5, 1970" ICUAT " 3:33:13 PM " GMT_FULL,time_t,a_datetime);
  327. #endif
  328. #else
  329. TEST_FP4(as::datetime,as::date_long ,as::time_long ,as::gmt,a_datetime,"February 5, 1970" PERIOD " 3:33:13 PM GMT+00:00",time_t,a_datetime);
  330. TEST_FP4(as::datetime,as::date_full ,as::time_full ,as::gmt,a_datetime,"Thursday, February 5, 1970" PERIOD " 3:33:13 PM GMT+00:00",time_t,a_datetime);
  331. #endif
  332. time_t now=time(0);
  333. time_t lnow = now + 3600 * 4;
  334. char local_time_str[256];
  335. std::string format="%H:%M:%S";
  336. std::basic_string<CharType> format_string(format.begin(),format.end());
  337. strftime(local_time_str,sizeof(local_time_str),format.c_str(),gmtime(&lnow));
  338. TEST_FMT(as::ftime(format_string),now,local_time_str);
  339. TEST_FMT(as::ftime(format_string)<<as::gmt<<as::local_time,now,local_time_str);
  340. std::string marks =
  341. "aAbB"
  342. "cdeh"
  343. "HIjm"
  344. "Mnpr"
  345. "RStT"
  346. "xXyY"
  347. "Z%";
  348. std::string result[]= {
  349. "Thu","Thursday","Feb","February", // aAbB
  350. #if BOOST_ICU_VER >= 408
  351. "Thursday, February 5, 1970" ICUAT " 3:33:13 PM " GMT_FULL, // c
  352. #else
  353. "Thursday, February 5, 1970 3:33:13 PM GMT+00:00", // c
  354. #endif
  355. "05","5","Feb", // deh
  356. "15","03","36","02", // HIjm
  357. "33","\n","PM", "03:33:13 PM",// Mnpr
  358. "15:33","13","\t","15:33:13", // RStT
  359. "Feb 5, 1970","3:33:13 PM","70","1970", // xXyY
  360. #if BOOST_ICU_VER >= 408
  361. GMT_FULL // Z
  362. #else
  363. "GMT+00:00" // Z
  364. #endif
  365. ,"%" }; // %
  366. for(unsigned i=0;i<marks.size();i++) {
  367. format_string.clear();
  368. format_string+=static_cast<CharType>('%');
  369. format_string+=static_cast<CharType>(marks[i]);
  370. TEST_FMT(as::ftime(format_string)<<as::gmt,a_datetime,result[i]);
  371. }
  372. std::string sample_f[]={
  373. "Now is %A, %H o'clo''ck ' or not ' ",
  374. "'test %H'",
  375. "%H'",
  376. "'%H'"
  377. };
  378. std::string expected_f[] = {
  379. "Now is Thursday, 15 o'clo''ck ' or not ' ",
  380. "'test 15'",
  381. "15'",
  382. "'15'"
  383. };
  384. for(unsigned i=0;i<sizeof(sample_f)/sizeof(sample_f[0]);i++) {
  385. format_string.assign(sample_f[i].begin(),sample_f[i].end());
  386. TEST_FMT(as::ftime(format_string)<<as::gmt,a_datetime,expected_f[i]);
  387. }
  388. }
  389. template<typename CharType>
  390. void test_format(std::string charset="UTF-8")
  391. {
  392. boost::locale::generator g;
  393. std::locale loc=g("en_US."+charset);
  394. FORMAT("{3} {1} {2}", 1 % 2 % 3,"3 1 2");
  395. FORMAT("{1} {2}", "hello" % 2,"hello 2");
  396. FORMAT("{1}",1200.1,"1200.1");
  397. FORMAT("Test {1,num}",1200.1,"Test 1,200.1");
  398. FORMAT("{{}} {1,number}",1200.1,"{} 1,200.1");
  399. #if BOOST_ICU_VER < 5601
  400. // bug #13276
  401. FORMAT("{1,num=sci,p=3}",13.1,"1.310E1");
  402. FORMAT("{1,num=scientific,p=3}",13.1,"1.310E1");
  403. #endif
  404. FORMAT("{1,num=fix,p=3}",13.1,"13.100");
  405. FORMAT("{1,num=fixed,p=3}",13.1,"13.100");
  406. FORMAT("{1,<,w=3,num}",-1,"-1 ");
  407. FORMAT("{1,>,w=3,num}",1," 1");
  408. FORMAT("{per,1}",0.1,"10%");
  409. FORMAT("{percent,1}",0.1,"10%");
  410. FORMAT("{1,cur}",1234,"$1,234.00");
  411. FORMAT("{1,currency}",1234,"$1,234.00");
  412. if(charset=="UTF-8") {
  413. if(U_ICU_VERSION_MAJOR_NUM >=4)
  414. FORMAT("{1,cur,locale=de_DE}",10,"10,00\xC2\xA0€");
  415. else
  416. FORMAT("{1,cur,locale=de_DE}",10,"10,00 €");
  417. }
  418. #if BOOST_ICU_VER >= 402
  419. FORMAT("{1,cur=nat}",1234,"$1,234.00");
  420. FORMAT("{1,cur=national}",1234,"$1,234.00");
  421. FORMAT("{1,cur=iso}",1234,"USD1,234.00");
  422. #endif
  423. FORMAT("{1,spell}",10,"ten");
  424. FORMAT("{1,spellout}",10,"ten");
  425. #if 402 <= BOOST_ICU_VER && BOOST_ICU_VER < 408
  426. if(charset=="UTF-8") {
  427. FORMAT("{1,ord}",1,"1\xcb\xa2\xe1\xb5\x97");
  428. FORMAT("{1,ordinal}",1,"1\xcb\xa2\xe1\xb5\x97");
  429. }
  430. #else
  431. FORMAT("{1,ord}",1,"1st");
  432. FORMAT("{1,ordinal}",1,"1st");
  433. #endif
  434. time_t now=time(0);
  435. time_t lnow = now + 3600 * 4;
  436. char local_time_str[256];
  437. std::string format="'%H:%M:%S'";
  438. std::basic_string<CharType> format_string(format.begin(),format.end());
  439. strftime(local_time_str,sizeof(local_time_str),format.c_str(),gmtime(&lnow));
  440. FORMAT("{1,ftime='''%H:%M:%S'''}",now,local_time_str);
  441. FORMAT("{1,local,ftime='''%H:%M:%S'''}",now,local_time_str);
  442. FORMAT("{1,ftime='''%H:%M:%S'''}",now,local_time_str);
  443. time_t a_date = 3600*24*(31+4); // Feb 5th
  444. time_t a_time = 3600*15+60*33; // 15:33:05
  445. time_t a_timesec = 13;
  446. time_t a_datetime = a_date + a_time + a_timesec;
  447. FORMAT("{1,date,gmt};{1,time,gmt};{1,datetime,gmt};{1,dt,gmt}",a_datetime,
  448. "Feb 5, 1970;3:33:13 PM;Feb 5, 1970" PERIOD " 3:33:13 PM;Feb 5, 1970" PERIOD " 3:33:13 PM");
  449. #if BOOST_ICU_VER >= 408
  450. FORMAT("{1,time=short,gmt};{1,time=medium,gmt};{1,time=long,gmt};{1,date=full,gmt}",a_datetime,
  451. "3:33 PM;3:33:13 PM;3:33:13 PM GMT;Thursday, February 5, 1970");
  452. FORMAT("{1,time=s,gmt};{1,time=m,gmt};{1,time=l,gmt};{1,date=f,gmt}",a_datetime,
  453. "3:33 PM;3:33:13 PM;3:33:13 PM GMT;Thursday, February 5, 1970");
  454. #else
  455. FORMAT("{1,time=short,gmt};{1,time=medium,gmt};{1,time=long,gmt};{1,date=full,gmt}",a_datetime,
  456. "3:33 PM;3:33:13 PM;3:33:13 PM GMT+00:00;Thursday, February 5, 1970");
  457. FORMAT("{1,time=s,gmt};{1,time=m,gmt};{1,time=l,gmt};{1,date=f,gmt}",a_datetime,
  458. "3:33 PM;3:33:13 PM;3:33:13 PM GMT+00:00;Thursday, February 5, 1970");
  459. #endif
  460. FORMAT("{1,time=s,tz=GMT+01:00}",a_datetime,"4:33 PM");
  461. FORMAT("{1,time=s,timezone=GMT+01:00}",a_datetime,"4:33 PM");
  462. FORMAT("{1,gmt,ftime='%H'''}",a_datetime,"15'");
  463. FORMAT("{1,gmt,ftime='''%H'}",a_datetime,"'15");
  464. FORMAT("{1,gmt,ftime='%H o''clock'}",a_datetime,"15 o'clock");
  465. // Test not a year of the week
  466. a_datetime=1388491200; // 2013-12-31 12:00 - check we don't use week of year
  467. FORMAT("{1,gmt,ftime='%Y'}",a_datetime,"2013");
  468. FORMAT("{1,gmt,ftime='%y'}",a_datetime,"13");
  469. FORMAT("{1,gmt,ftime='%D'}",a_datetime,"12/31/13");
  470. }
  471. int main()
  472. {
  473. try {
  474. boost::locale::time_zone::global("GMT+4:00");
  475. std::cout << "Testing char, UTF-8" << std::endl;
  476. test_manip<char>();
  477. test_format<char>();
  478. std::cout << "Testing char, ISO8859-1" << std::endl;
  479. test_manip<char>("ISO8859-1");
  480. test_format<char>("ISO8859-1");
  481. std::cout << "Testing wchar_t" << std::endl;
  482. test_manip<wchar_t>();
  483. test_format<wchar_t>();
  484. #ifdef BOOST_LOCALE_ENABLE_CHAR16_T
  485. std::cout << "Testing char16_t" << std::endl;
  486. test_manip<char16_t>();
  487. test_format<char16_t>();
  488. #endif
  489. #ifdef BOOST_LOCALE_ENABLE_CHAR32_T
  490. std::cout << "Testing char32_t" << std::endl;
  491. test_manip<char32_t>();
  492. test_format<char32_t>();
  493. #endif
  494. }
  495. catch(std::exception const &e) {
  496. std::cerr << "Failed " << e.what() << std::endl;
  497. return EXIT_FAILURE;
  498. }
  499. FINALIZE();
  500. }
  501. #endif // NOICU
  502. // vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4
  503. // boostinspect:noascii