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 
25 const 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 **/
37 int
main()38 main()
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);
58     date d(not_a_date_time);
59     ss << d;
60     check("Special value, stream out nadt" , ss.str() == std::string("not-a-date-time"));
61     ss.str("");
62     d = date(neg_infin);
63     ss << d;
64     check("Special value, stream out neg_infin" , ss.str() == std::string("-infinity"));
65     ss.str("");
66     d = date(pos_infin);
67     ss << d;
68     check("Special value, stream out pos_infin" , 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(coi, Oct);
80   check("check german short month: " + ss.str(),
81         ss.str() == std::string("Okt"));
82 
83   ss.str(""); //reset string stream
84   greg_month mo(Oct);
85   month_formatter::format_month(mo, ss, gdnp);
86   check("check german short month: " + ss.str(),
87         ss.str() == std::string("Okt"));
88   ss.str(""); //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(""); //reset string stream
97   ymd_formatter::ymd_put(ymd, ss, gdnp);
98   check("check ymd: " + ss.str(),
99         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(d1, os, gdnp);
107   check("ostream low level check string:"+os.str(),
108         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("no registered facet here",
117         !std::has_facet<date_facet>(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("facet registered here",
127         std::has_facet<boost::date_time::date_names_put<facet_config> >(global2));
128 
129   std::stringstream os2;
130   os2.imbue(global2);
131   datef::date_put(d1, os2);
132   check("check string imbued ostream: "+os2.str(),
133         os2.str() == std::string("2002-Okt-01"));
134 
135   date infin(pos_infin);
136   os2.str(""); //clear stream
137   datef::date_put(infin, os2);
138   check("check string imbued ostream: "+os2.str(),
139         os2.str() == std::string("+unbegrenztheit"));
140 
141   os2.str(""); //clear stream
142   os2 << infin;
143   check("check string imbued ostream: "+os2.str(),
144         os2.str() == std::string("+unbegrenztheit"));
145 
146 
147   date nadt(not_a_date_time);
148   os2.str(""); //clear stream
149   datef::date_put(nadt, os2);
150   check("check string imbued ostream: "+os2.str(),
151         os2.str() == std::string("NichtDatumzeit"));
152 
153 
154   std::stringstream os3;
155   os3 << d1;
156   check("check any old ostream: "+os3.str(),
157         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(german_dates1);
174   os3.str("");
175   os3 << d1;
176   check("check date order: "+os3.str(),
177         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(german_dates2);
190   os3.str("");
191   os3 << d1;
192   check("check date order: "+os3.str(),
193         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(german_dates3);
206   os3.str("");
207   os3 << d1;
208   check("check date order: "+os3.str(),
209         os3.str() == std::string("Oktober 01 2002"));
210 
211   date_period dp(d1, date_duration(3));
212   os3.str("");
213   os3 << dp;
214   check("check date period: "+os3.str(),
215         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("date_duration stream out", ss2.str() == std::string("26"));
226 
227   dur = date_duration(boost::date_time::pos_infin);
228   ss2.str("");
229   ss2 << dur;
230   check("date_duration stream out", 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("");
239   ss2 << pd;
240   check("partial date stream out", ss2.str() == std::string("26 Jun"));
241 
242   ss2.str("");
243   nth_kday_of_month nkm(nth_kday_of_month::second, Friday, Sep);
244   ss2 << nkm;
245   check("nth kday of month", ss2.str() == std::string("second Fri of Sep"));
246 
247   ss2.str("");
248   first_kday_of_month fkm(Saturday, May);
249   ss2 << fkm;
250   check("first kday of month", ss2.str() == std::string("first Sat of May"));
251 
252   ss2.str("");
253   last_kday_of_month lkm(Monday, Aug);
254   ss2 << lkm;
255   check("last kday of month", ss2.str() == std::string("last Mon of Aug"));
256 
257   ss2.str("");
258   first_kday_after  fka(Thursday);//fkb.get_date(d)
259   ss2 << fka;
260   check("first kday after", ss2.str() == std::string("Thu after"));
261 
262   ss2.str("");
263   first_kday_before fkb(Tuesday); // same ^
264   ss2 << fkb;
265   check("first kday after", 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(global2);
283     greg_month m(3);
284     ss1 >> m;
285     check("Stream in month", m == greg_month(Jan));
286 #ifndef BOOST_NO_STD_WSTRING
287     std::wstringstream ws1(L"Dec");
288     ws1 >> m;
289     check("Wide Stream in month", 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("Stream in German month", m == greg_month(Oct));
295     try{
296       ss2m >> m; // misspelled
297       check("Bad month exception NOT thrown (misspelled name)", false);
298     }catch(bad_month&){
299       check("Bad month exception caught (misspelled name)", true);
300     }catch(...){
301       check("Bad month exception NOT caught (misspelled name)", 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(global2);
312     greg_weekday wd(Friday); //something not Sunday...
313     ss1 >> wd;
314     check("Stream in weekday", wd == greg_weekday(Sunday));
315 #ifndef BOOST_NO_STD_WSTRING
316     std::wstringstream ws1(L"Saturday");
317     ws1 >> wd;
318     check("Wide Stream in weekday", 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("Stream in German weekday", wd == greg_weekday(Wednesday));
324     try{
325       ss2w >> wd;
326       check("Bad weekday exception NOT thrown (misspelled name)", false);
327     }catch(bad_weekday&){
328       check("Bad weekday exception caught (misspelled name)", true);
329     }catch(...){
330       check("Bad weekday exception NOT caught (misspelled name)", 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