1/* Copyright (c) 2002,2003,2005 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 */
7
8#include <sstream>
9#include <iostream>
10#include <fstream>
11
12#include "boost/date_time/gregorian/greg_month.hpp"
13#include "boost/date_time/gregorian/greg_facet.hpp"
14#include "boost/date_time/date_format_simple.hpp"
15#include "boost/date_time/gregorian/gregorian.hpp"
16#include "../testfrmwk.hpp"
17
18#ifndef BOOST_DATE_TIME_NO_LOCALE
19
20 const char* const de_short_month_names[]={"Jan","Feb","Mar","Apr","Mai","Jun","Jul","Aug","Sep","Okt","Nov","Dez", "NAM"};
21
22 const char* const de_long_month_names[]={"Januar","Februar","Marz","April","Mai","Juni","Juli","August","September","Oktober","November","Dezember","NichtDerMonat"};
23 const char* const de_special_value_names[]={"NichtDatumzeit","-unbegrenztheit", "+unbegrenztheit"};
24
25const char* const de_short_weekday_names[]={"Son", "Mon", "Die","Mit", "Don", "Fre", "Sam"};
26
27 const char* const de_long_weekday_names[]={"Sonntag", "Montag", "Dienstag","Mittwoch", "Donnerstag", "Freitag", "Samstag"};
28
29#endif
30
31/** Not used for now
32 const char* const es_short_month_names[]={"Ene","Feb","Mar","Abr","Pue","Jun","Jul","Ago","Sep","Oct","Nov","Dic", "NAM"};
33
34 const char* const es_long_month_names[]={"Enero","Febrero","Marcha","Abril","Pueda","Junio","Julio","Agosto","Septiembre","Octubre","Noviembre","Diciembre","NoAMes"};
35 const char* const es_special_value_names[]={"NoUnRatoDeLaFacha","-infinito", "+infinito"};
36**/
37int
38main()
39{
40// std::locale native("");
41// std::cout << "native: " << native.name() << std::endl;
42//#ifndef BOOST_NO_STD_LOCALE
43#ifndef BOOST_DATE_TIME_NO_LOCALE
44
45 using namespace boost::gregorian;
46
47 typedef greg_facet_config facet_config;
48 typedef boost::date_time::all_date_names_put<facet_config> date_facet;
49 typedef boost::date_time::date_names_put<facet_config> date_facet_base;
50 typedef boost::date_time::ostream_month_formatter<date_facet_base> month_formatter;
51
52 {
53 // special_values tests
54 std::stringstream ss;
55 date_facet_base* f = new date_facet_base();
56 std::locale loc(std::locale::classic(), f);
57 ss.imbue(loc: loc);
58 date d(not_a_date_time);
59 ss << d;
60 check(testname: "Special value, stream out nadt" , testcond: ss.str() == std::string("not-a-date-time"));
61 ss.str(s: "");
62 d = date(neg_infin);
63 ss << d;
64 check(testname: "Special value, stream out neg_infin" , testcond: ss.str() == std::string("-infinity"));
65 ss.str(s: "");
66 d = date(pos_infin);
67 ss << d;
68 check(testname: "Special value, stream out pos_infin" , testcond: ss.str() == std::string("+infinity"));
69 }
70
71 date_facet gdnp(de_short_month_names, de_long_month_names,
72 de_special_value_names, de_long_weekday_names,
73 de_long_weekday_names,
74 '.',
75 boost::date_time::ymd_order_dmy);
76
77 std::stringstream ss;
78 std::ostreambuf_iterator<char> coi(ss);
79 gdnp.put_month_short(oitr&: coi, moy: Oct);
80 check(testname: "check german short month: " + ss.str(),
81 testcond: ss.str() == std::string("Okt"));
82
83 ss.str(s: ""); //reset string stream
84 greg_month mo(Oct);
85 month_formatter::format_month(month: mo, os&: ss, f: gdnp);
86 check(testname: "check german short month: " + ss.str(),
87 testcond: ss.str() == std::string("Okt"));
88 ss.str(s: ""); //reset string stream
89// month_formatter::format_month(mo, ss, gdnp);
90// check("check german long month: " + ss.str(),
91// ss.str() == std::string("Oktober"));
92
93
94 greg_year_month_day ymd(2002,Oct,1);
95 typedef boost::date_time::ostream_ymd_formatter<greg_year_month_day, date_facet_base> ymd_formatter;
96 ss.str(s: ""); //reset string stream
97 ymd_formatter::ymd_put(ymd, os&: ss, f: gdnp);
98 check(testname: "check ymd: " + ss.str(),
99 testcond: ss.str() == std::string("01.Okt.2002"));
100
101
102 typedef boost::date_time::ostream_date_formatter<date, date_facet_base> datef;
103
104 std::stringstream os;
105 date d1(2002, Oct, 1);
106 datef::date_put(d: d1, os, f: gdnp);
107 check(testname: "ostream low level check string:"+os.str(),
108 testcond: os.str() == std::string("01.Okt.2002"));
109
110// //Locale tests
111 std::locale global;
112 std::cout << "global: " << global.name() << std::endl;
113
114 // put a facet into a locale
115 //check for a facet p319
116 check(testname: "no registered facet here",
117 testcond: !std::has_facet<date_facet>(loc: global));
118
119 std::locale global2(global,
120 new date_facet(de_short_month_names,
121 de_long_month_names,
122 de_special_value_names,
123 de_long_weekday_names,
124 de_long_weekday_names));
125
126 check(testname: "facet registered here",
127 testcond: std::has_facet<boost::date_time::date_names_put<facet_config> >(loc: global2));
128
129 std::stringstream os2;
130 os2.imbue(loc: global2);
131 datef::date_put(d: d1, os&: os2);
132 check(testname: "check string imbued ostream: "+os2.str(),
133 testcond: os2.str() == std::string("2002-Okt-01"));
134
135 date infin(pos_infin);
136 os2.str(s: ""); //clear stream
137 datef::date_put(d: infin, os&: os2);
138 check(testname: "check string imbued ostream: "+os2.str(),
139 testcond: os2.str() == std::string("+unbegrenztheit"));
140
141 os2.str(s: ""); //clear stream
142 os2 << infin;
143 check(testname: "check string imbued ostream: "+os2.str(),
144 testcond: os2.str() == std::string("+unbegrenztheit"));
145
146
147 date nadt(not_a_date_time);
148 os2.str(s: ""); //clear stream
149 datef::date_put(d: nadt, os&: os2);
150 check(testname: "check string imbued ostream: "+os2.str(),
151 testcond: os2.str() == std::string("NichtDatumzeit"));
152
153
154 std::stringstream os3;
155 os3 << d1;
156 check(testname: "check any old ostream: "+os3.str(),
157 testcond: os3.str() == std::string("2002-Oct-01"));
158
159 std::ofstream f("test_facet_file.out");
160 f << d1 << std::endl;
161
162// // date formatter that takes locale and gets facet from locale
163 std::locale german_dates1(global,
164 new date_facet(de_short_month_names,
165 de_long_month_names,
166 de_special_value_names,
167 de_short_weekday_names,
168 de_long_weekday_names,
169 '.',
170 boost::date_time::ymd_order_dmy,
171 boost::date_time::month_as_integer));
172
173 os3.imbue(loc: german_dates1);
174 os3.str(s: "");
175 os3 << d1;
176 check(testname: "check date order: "+os3.str(),
177 testcond: os3.str() == std::string("01.10.2002"));
178
179 std::locale german_dates2(global,
180 new date_facet(de_short_month_names,
181 de_long_month_names,
182 de_special_value_names,
183 de_short_weekday_names,
184 de_long_weekday_names,
185 ' ',
186 boost::date_time::ymd_order_iso,
187 boost::date_time::month_as_short_string));
188
189 os3.imbue(loc: german_dates2);
190 os3.str(s: "");
191 os3 << d1;
192 check(testname: "check date order: "+os3.str(),
193 testcond: os3.str() == std::string("2002 Okt 01"));
194
195 std::locale german_dates3(global,
196 new date_facet(de_short_month_names,
197 de_long_month_names,
198 de_special_value_names,
199 de_short_weekday_names,
200 de_long_weekday_names,
201 ' ',
202 boost::date_time::ymd_order_us,
203 boost::date_time::month_as_long_string));
204
205 os3.imbue(loc: german_dates3);
206 os3.str(s: "");
207 os3 << d1;
208 check(testname: "check date order: "+os3.str(),
209 testcond: os3.str() == std::string("Oktober 01 2002"));
210
211 date_period dp(d1, date_duration(3));
212 os3.str(s: "");
213 os3 << dp;
214 check(testname: "check date period: "+os3.str(),
215 testcond: os3.str() == std::string("[Oktober 01 2002/Oktober 03 2002]"));
216
217
218 /*******************************************************************/
219 /* Streaming operations for date durations */
220 /*******************************************************************/
221
222 date_duration dur(26);
223 std::stringstream ss2;
224 ss2 << dur;
225 check(testname: "date_duration stream out", testcond: ss2.str() == std::string("26"));
226
227 dur = date_duration(boost::date_time::pos_infin);
228 ss2.str(s: "");
229 ss2 << dur;
230 check(testname: "date_duration stream out", testcond: ss2.str() == std::string("+infinity"));
231
232 /*******************************************************************/
233 /* Streaming operations for date generator functions */
234 /*******************************************************************/
235
236 partial_date pd(26, Jun);
237 //std::stringstream ss2;
238 ss2.str(s: "");
239 ss2 << pd;
240 check(testname: "partial date stream out", testcond: ss2.str() == std::string("26 Jun"));
241
242 ss2.str(s: "");
243 nth_kday_of_month nkm(nth_kday_of_month::second, Friday, Sep);
244 ss2 << nkm;
245 check(testname: "nth kday of month", testcond: ss2.str() == std::string("second Fri of Sep"));
246
247 ss2.str(s: "");
248 first_kday_of_month fkm(Saturday, May);
249 ss2 << fkm;
250 check(testname: "first kday of month", testcond: ss2.str() == std::string("first Sat of May"));
251
252 ss2.str(s: "");
253 last_kday_of_month lkm(Monday, Aug);
254 ss2 << lkm;
255 check(testname: "last kday of month", testcond: ss2.str() == std::string("last Mon of Aug"));
256
257 ss2.str(s: "");
258 first_kday_after fka(Thursday);//fkb.get_date(d)
259 ss2 << fka;
260 check(testname: "first kday after", testcond: ss2.str() == std::string("Thu after"));
261
262 ss2.str(s: "");
263 first_kday_before fkb(Tuesday); // same ^
264 ss2 << fkb;
265 check(testname: "first kday after", testcond: ss2.str() == std::string("Tue before"));
266
267 std::cout << pd << '\n'
268 << nkm << '\n'
269 << fkm << '\n'
270 << lkm << '\n'
271 << fka << '\n'
272 << fkb << '\n'
273 << std::endl;
274
275 /*******************************************************************/
276 /* Input Streaming for greg_month */
277 /*******************************************************************/
278 {
279 std::stringstream ss1("January");
280 std::stringstream ss2m("dec"); // misspelled
281 std::stringstream german("Okt");
282 german.imbue(loc: global2);
283 greg_month m(3);
284 ss1 >> m;
285 check(testname: "Stream in month", testcond: m == greg_month(Jan));
286#ifndef BOOST_NO_STD_WSTRING
287 std::wstringstream ws1(L"Dec");
288 ws1 >> m;
289 check(testname: "Wide Stream in month", testcond: m == greg_month(Dec));
290#else
291 check("Wide Stream in not supported by this compiler", false);
292#endif // BOOST_NO_STD_WSTRING
293 german >> m;
294 check(testname: "Stream in German month", testcond: m == greg_month(Oct));
295 try{
296 ss2m >> m; // misspelled
297 check(testname: "Bad month exception NOT thrown (misspelled name)", testcond: false);
298 }catch(bad_month&){
299 check(testname: "Bad month exception caught (misspelled name)", testcond: true);
300 }catch(...){
301 check(testname: "Bad month exception NOT caught (misspelled name)", testcond: false);
302 }
303 }
304 /*******************************************************************/
305 /* Input Streaming for greg_weekday */
306 /*******************************************************************/
307 {
308 std::stringstream ss1("Sun");
309 std::stringstream ss2w("Wensday"); // misspelled
310 std::stringstream german("Mittwoch"); // Wednesday
311 german.imbue(loc: global2);
312 greg_weekday wd(Friday); //something not Sunday...
313 ss1 >> wd;
314 check(testname: "Stream in weekday", testcond: wd == greg_weekday(Sunday));
315#ifndef BOOST_NO_STD_WSTRING
316 std::wstringstream ws1(L"Saturday");
317 ws1 >> wd;
318 check(testname: "Wide Stream in weekday", testcond: wd == greg_weekday(Saturday));
319#else
320 check("Wide Stream in not supported by this compiler", false);
321#endif // BOOST_NO_STD_WSTRING
322 german >> wd;
323 check(testname: "Stream in German weekday", testcond: wd == greg_weekday(Wednesday));
324 try{
325 ss2w >> wd;
326 check(testname: "Bad weekday exception NOT thrown (misspelled name)", testcond: false);
327 }catch(bad_weekday&){
328 check(testname: "Bad weekday exception caught (misspelled name)", testcond: true);
329 }catch(...){
330 check(testname: "Bad weekday exception NOT caught (misspelled name)", testcond: false);
331 }
332 }
333
334#else
335 check("No tests executed - Locales not supported by this compiler", false);
336
337#endif //BOOST_DATE_TIME_NO_LOCALE
338
339 return printTestStats();
340
341}
342
343

source code of boost/libs/date_time/test/gregorian/testfacet.cpp