test_date_time.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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. #include <boost/locale/date_time.hpp>
  9. #include <boost/locale/generator.hpp>
  10. #include <boost/locale/formatting.hpp>
  11. #include <boost/locale/localization_backend.hpp>
  12. #include <iomanip>
  13. #include "test_locale.hpp"
  14. #ifdef BOOST_LOCALE_WITH_ICU
  15. #include <unicode/uversion.h>
  16. #define BOOST_ICU_VER (U_ICU_VERSION_MAJOR_NUM*100 + U_ICU_VERSION_MINOR_NUM)
  17. #else
  18. #define BOOST_ICU_VER 406
  19. #endif
  20. #ifdef BOOST_MSVC
  21. # pragma warning(disable : 4244) // loose data
  22. #endif
  23. #define RESET() do { time_point = base_time_point; ss.str(""); } while(0)
  24. #define TESTR(X) do { TEST(X); RESET(); } while(0)
  25. //#define TESTEQSR(t,X) do { ss << (t); TESTR(ss.str() == X); } while(0)
  26. #define TESTEQSR(t,X) do { ss << (t); if(ss.str()!=X) { std::cerr <<"[" << ss.str() <<"]!=[" <<X<<"]" << std::endl; } TESTR(ss.str() == X); } while(0)
  27. int main()
  28. {
  29. try {
  30. using namespace boost::locale;
  31. using namespace boost::locale::period;
  32. std::string def[] = {
  33. #ifdef BOOST_LOCALE_WITH_ICU
  34. "icu" ,
  35. #endif
  36. #ifndef BOOST_LOCALE_NO_STD_BACKEND
  37. "std" ,
  38. #endif
  39. #ifndef BOOST_LOCALE_NO_POSIX_BACKEND
  40. "posix",
  41. #endif
  42. #ifndef BOOST_LOCALE_NO_WINAPI_BACKEND
  43. "winapi",
  44. #endif
  45. };
  46. for(int type = 0 ; type < int(sizeof(def)/sizeof(def[0])) ; type ++ ) {
  47. boost::locale::localization_backend_manager tmp_backend = boost::locale::localization_backend_manager::global();
  48. tmp_backend.select(def[type]);
  49. boost::locale::localization_backend_manager::global(tmp_backend);
  50. std::cout << "Testing for backend: " << def[type] << std::endl;
  51. std::string backend_name = def[type];
  52. {
  53. boost::locale::generator g;
  54. std::locale loc=g("en_US.UTF-8");
  55. std::locale::global(loc);
  56. std::string tz("GMT");
  57. time_zone::global(tz);
  58. calendar cal(loc,tz);
  59. TEST(calendar() == cal);
  60. TEST(calendar(loc) == cal);
  61. TEST(calendar(tz) == cal);
  62. TEST(calendar(loc,"GMT+01:00") != cal);
  63. TEST(calendar(g("ru_RU.UTF-8")) != cal);
  64. TEST(cal.minimum(month())==0);
  65. TEST(cal.maximum(month())==11);
  66. TEST(cal.minimum(day())==1);
  67. TEST(cal.greatest_minimum(day())==1);
  68. TEST(cal.least_maximum(day())==28);
  69. TEST(cal.maximum(day())==31);
  70. TEST(calendar(g("ar_EG.UTF-8")).first_day_of_week() == 7);
  71. TEST(calendar(g("he_IL.UTF-8")).first_day_of_week() == 1);
  72. TEST(calendar(g("ru_RU.UTF-8")).first_day_of_week() == 2);
  73. std::ostringstream ss;
  74. ss.imbue(loc);
  75. ss<<boost::locale::as::time_zone(tz);
  76. date_time time_point;
  77. time_point=year(1970) + february() + day(5);
  78. ss << as::ftime("%Y-%m-%d")<< time_point;
  79. TEST(ss.str() == "1970-02-05");
  80. time_point = 3 * hour_12() + 1 * am_pm() + 33 * minute() + 13 * second();
  81. ss.str("");
  82. ss << as::ftime("%Y-%m-%d %H:%M:%S") << time_point;
  83. TEST( ss.str() == "1970-02-05 15:33:13"); ss.str("");
  84. time_t a_date = 3600*24*(31+4); // Feb 5th
  85. time_t a_time = 3600*15+60*33; // 15:33:05
  86. time_t a_timesec = 13;
  87. time_t a_datetime = a_date + a_time + a_timesec;
  88. date_time base_time_point=date_time(a_datetime);
  89. RESET();
  90. time_point += hour();
  91. TESTEQSR(time_point,"1970-02-05 16:33:13");
  92. TEST(time_point.minimum(day())==1);
  93. TEST(time_point.maximum(day())==28);
  94. time_point += year() * 2 + 1 *month();
  95. TESTEQSR(time_point,"1972-03-05 15:33:13");
  96. time_point -= minute();
  97. TESTEQSR( time_point, "1970-02-05 15:32:13");
  98. time_point <<= minute() * 30;
  99. TESTEQSR( time_point, "1970-02-05 15:03:13");
  100. time_point >>= minute(40);
  101. TESTEQSR( time_point, "1970-02-05 15:53:13");
  102. TEST((time_point + month()) / month() == 2);
  103. TEST(month(time_point + month(1)) == 2);
  104. TEST(time_point / month() == 1);
  105. TEST((time_point - month()) / month()== 0);
  106. TEST(time_point / month() == 1);
  107. TEST((time_point << month()) / month()== 2);
  108. TEST(time_point / month()== 1);
  109. TEST((time_point >> month()) / month()== 0);
  110. TEST(time_point / month()== 1);
  111. TEST( (time_point + 2 * hour() - time_point) / minute() == 120);
  112. TEST( (time_point + month()- time_point) / day() == 28);
  113. TEST( (time_point + 2* month()- (time_point+month())) / day() == 31);
  114. TEST( day(time_point + 2* month()- (time_point+month())) == 31);
  115. TESTEQSR( time_point + hour(), "1970-02-05 16:33:13");
  116. TESTEQSR( time_point - hour(2), "1970-02-05 13:33:13");
  117. TESTEQSR( time_point >> minute(), "1970-02-05 15:32:13");
  118. TESTEQSR( time_point << second(), "1970-02-05 15:33:14");
  119. TEST(time_point == time_point);
  120. TEST(!(time_point != time_point));
  121. TEST(time_point.get(hour()) == 15);
  122. TEST(time_point/hour() == 15);
  123. TEST(time_point+year() != time_point);
  124. TEST(time_point - minute() <= time_point);
  125. TEST(time_point <= time_point);
  126. TEST(time_point + minute() >= time_point);
  127. TEST(time_point >= time_point);
  128. TEST(time_point < time_point + second());
  129. TEST(!(time_point < time_point - second()));
  130. TEST(time_point > time_point - second());
  131. TEST(!(time_point > time_point + second()));
  132. TEST(time_point.get(day()) == 5);
  133. TEST(time_point.get(year()) == 1970);
  134. TEST(time_point.get(era()) == 1);
  135. TEST(time_point.get(year()) == 1970);
  136. TEST(time_point.get(extended_year()) == 1970);
  137. if(backend_name == "icu") {
  138. time_point=extended_year(-3);
  139. TEST(time_point.get(era()) == 0);
  140. TEST(time_point.get(year()) == 4);
  141. }
  142. RESET();
  143. TEST(time_point.get(month()) == 1);
  144. TEST(time_point.get(day()) == 5);
  145. TEST(time_point.get(day_of_year()) == 36);
  146. TEST(time_point.get(day_of_week()) == 5);
  147. TEST(time_point.get(day_of_week_in_month())==1);
  148. time_point=date_time(a_datetime,calendar(g("ru_RU.UTF-8")));
  149. TEST(time_point.get(day_of_week_local()) == 4);
  150. time_point = year(2026) + january() + day(1);
  151. TEST(time_point.get(day_of_week()) == 5);
  152. TEST(time_point.get(week_of_year()) == 1);
  153. TEST(time_point.get(week_of_month()) == 1);
  154. time_point = day_of_week() * 1;
  155. TEST(time_point.get(day()) == 4);
  156. TEST(time_point.get(week_of_year()) == 1);
  157. TEST(time_point.get(week_of_month()) == 1);
  158. time_point += day() * 1;
  159. TEST(time_point.get(week_of_year()) == 2);
  160. TEST(time_point.get(week_of_month()) == 2);
  161. time_point = february() + day() * 2;
  162. TEST(time_point.get(week_of_year()) == 6);
  163. if(backend_name!="icu" || BOOST_ICU_VER<408 || BOOST_ICU_VER > 6000) {
  164. TEST(time_point.get(week_of_month()) == 1);
  165. }
  166. else {
  167. // cldr changes
  168. TEST(time_point.get(week_of_month()) == 2);
  169. }
  170. time_point = year(2010) + january() + day() * 3;
  171. if(backend_name!="icu" || BOOST_ICU_VER<408 || BOOST_ICU_VER > 6000) {
  172. TEST(time_point.get(week_of_year()) == 53);
  173. }
  174. else {
  175. TEST(time_point.get(week_of_year()) == 1);
  176. }
  177. time_point = year()*2010 + january() + day() * 4;
  178. if(backend_name!="icu" || BOOST_ICU_VER<408 || BOOST_ICU_VER > 6000) {
  179. TEST(time_point.get(week_of_year()) == 1);
  180. }
  181. else {
  182. TEST(time_point.get(week_of_year()) == 2);
  183. }
  184. time_point = year()*2010 + january() + day() * 10;
  185. if(backend_name!="icu" || BOOST_ICU_VER<408 || BOOST_ICU_VER > 6000) {
  186. TEST(time_point.get(week_of_year()) == 1);
  187. }
  188. else {
  189. TEST(time_point.get(week_of_year()) == 2);
  190. }
  191. time_point = year()*2010 + january() + day() * 11;
  192. if(backend_name!="icu" || BOOST_ICU_VER<408 || BOOST_ICU_VER > 6000) {
  193. TEST(time_point.get(week_of_year()) == 2);
  194. }
  195. else {
  196. TEST(time_point.get(week_of_year()) == 3);
  197. }
  198. RESET();
  199. TEST(time_point.get(hour()) == 15);
  200. TEST(date_time(a_datetime,calendar("GMT+01:00")).get(hour()) ==16);
  201. TEST(time_point.get(hour_12()) == 3);
  202. TEST(time_point.get(am_pm()) == 1);
  203. TEST(time_point.get(minute()) == 33);
  204. TEST(time_point.get(second()) == 13);
  205. TEST(date_time(year()* 1984 + february() + day()).get(week_of_year())==5);
  206. TEST(time_point.get(week_of_month()) == 1);
  207. RESET();
  208. // Make sure we don't get year() < 1970 so the test would
  209. // work on windows where mktime supports positive time_t
  210. // only
  211. time_point = year() * 2010;
  212. TEST((time_point + year() *1 - hour() * 1 - time_point) / year() == 0);
  213. TEST((time_point + year() *1 - time_point) / year() == 1);
  214. TEST((time_point + year() *1 + hour() * 1 - time_point) / year() == 1);
  215. TEST((time_point - year() *1 + hour() * 1 - time_point) / year() == 0);
  216. TEST((time_point - year() *1 - time_point) / year() == -1);
  217. TEST((time_point - year() *1 - hour() * 1 - time_point) / year() == -1);
  218. RESET();
  219. time_point.time(24*3600 * 2);
  220. time_point = year() * 2011;
  221. time_point = march();
  222. time_point = day() * 29;
  223. date_time tmp_save = time_point;
  224. time_point = year() * 2011;
  225. time_point = february();
  226. time_point = day() * 5;
  227. TEST(time_point.get(year()) == 2011);
  228. TEST(time_point.get(month()) == 2); // march
  229. TEST(time_point.get(day()) == 5);
  230. time_point = tmp_save;
  231. time_point = year() * 2011 + february() + day() * 5;
  232. TEST(time_point.get(year()) == 2011);
  233. TEST(time_point.get(month()) == 1); // february
  234. TEST(time_point.get(day()) == 5);
  235. } // test
  236. } // for loop
  237. }
  238. catch(std::exception const &e) {
  239. std::cerr << "Failed " << e.what() << std::endl;
  240. return EXIT_FAILURE;
  241. }
  242. FINALIZE();
  243. }
  244. // vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4
  245. // boostinspect:noascii