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