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
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"
18 #ifndef BOOST_DATE_TIME_NO_LOCALE
20 const char* const de_short_month_names
[]={"Jan","Feb","Mar","Apr","Mai","Jun","Jul","Aug","Sep","Okt","Nov","Dez", "NAM"};
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"};
25 const char* const de_short_weekday_names
[]={"Son", "Mon", "Die","Mit", "Don", "Fre", "Sam"};
27 const char* const de_long_weekday_names
[]={"Sonntag", "Montag", "Dienstag","Mittwoch", "Donnerstag", "Freitag", "Samstag"};
32 const char* const es_short_month_names[]={"Ene","Feb","Mar","Abr","Pue","Jun","Jul","Ago","Sep","Oct","Nov","Dic", "NAM"};
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"};
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
45 using namespace boost::gregorian
;
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
;
53 // special_values tests
55 date_facet_base
* f
= new date_facet_base();
56 std::locale
loc(std::locale::classic(), f
);
58 date
d(not_a_date_time
);
60 check("Special value, stream out nadt" , ss
.str() == std::string("not-a-date-time"));
64 check("Special value, stream out neg_infin" , ss
.str() == std::string("-infinity"));
68 check("Special value, stream out pos_infin" , ss
.str() == std::string("+infinity"));
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
,
75 boost::date_time::ymd_order_dmy
);
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"));
83 ss
.str(""); //reset string stream
85 month_formatter::format_month(m
, 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(m, ss, gdnp);
90 // check("check german long month: " + ss.str(),
91 // ss.str() == std::string("Oktober"));
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"));
102 typedef boost::date_time::ostream_date_formatter
<date
, date_facet_base
> datef
;
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"));
112 std::cout
<< "global: " << global
.name() << std::endl
;
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
));
119 std::locale
global2(global
,
120 new date_facet(de_short_month_names
,
122 de_special_value_names
,
123 de_long_weekday_names
,
124 de_long_weekday_names
));
126 check("facet registered here",
127 std::has_facet
<boost::date_time::date_names_put
<facet_config
> >(global2
));
129 std::stringstream os2
;
131 datef::date_put(d1
, os2
);
132 check("check string imbued ostream: "+os2
.str(),
133 os2
.str() == std::string("2002-Okt-01"));
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"));
141 os2
.str(""); //clear stream
143 check("check string imbued ostream: "+os2
.str(),
144 os2
.str() == std::string("+unbegrenztheit"));
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"));
154 std::stringstream os3
;
156 check("check any old ostream: "+os3
.str(),
157 os3
.str() == std::string("2002-Oct-01"));
159 std::ofstream
f("test_facet_file.out");
160 f
<< d1
<< std::endl
;
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
,
166 de_special_value_names
,
167 de_short_weekday_names
,
168 de_long_weekday_names
,
170 boost::date_time::ymd_order_dmy
,
171 boost::date_time::month_as_integer
));
173 os3
.imbue(german_dates1
);
176 check("check date order: "+os3
.str(),
177 os3
.str() == std::string("01.10.2002"));
179 std::locale
german_dates2(global
,
180 new date_facet(de_short_month_names
,
182 de_special_value_names
,
183 de_short_weekday_names
,
184 de_long_weekday_names
,
186 boost::date_time::ymd_order_iso
,
187 boost::date_time::month_as_short_string
));
189 os3
.imbue(german_dates2
);
192 check("check date order: "+os3
.str(),
193 os3
.str() == std::string("2002 Okt 01"));
195 std::locale
german_dates3(global
,
196 new date_facet(de_short_month_names
,
198 de_special_value_names
,
199 de_short_weekday_names
,
200 de_long_weekday_names
,
202 boost::date_time::ymd_order_us
,
203 boost::date_time::month_as_long_string
));
205 os3
.imbue(german_dates3
);
208 check("check date order: "+os3
.str(),
209 os3
.str() == std::string("Oktober 01 2002"));
211 date_period
dp(d1
, date_duration(3));
214 check("check date period: "+os3
.str(),
215 os3
.str() == std::string("[Oktober 01 2002/Oktober 03 2002]"));
218 /*******************************************************************/
219 /* Streaming operations for date durations */
220 /*******************************************************************/
222 date_duration
dur(26);
223 std::stringstream ss2
;
225 check("date_duration stream out", ss2
.str() == std::string("26"));
227 dur
= date_duration(boost::date_time::pos_infin
);
230 check("date_duration stream out", ss2
.str() == std::string("+infinity"));
232 /*******************************************************************/
233 /* Streaming operations for date generator functions */
234 /*******************************************************************/
236 partial_date
pd(26, Jun
);
237 //std::stringstream ss2;
240 check("partial date stream out", ss2
.str() == std::string("26 Jun"));
243 nth_kday_of_month
nkm(nth_kday_of_month::second
, Friday
, Sep
);
245 check("nth kday of month", ss2
.str() == std::string("second Fri of Sep"));
248 first_kday_of_month
fkm(Saturday
, May
);
250 check("first kday of month", ss2
.str() == std::string("first Sat of May"));
253 last_kday_of_month
lkm(Monday
, Aug
);
255 check("last kday of month", ss2
.str() == std::string("last Mon of Aug"));
258 first_kday_after
fka(Thursday
);//fkb.get_date(d)
260 check("first kday after", ss2
.str() == std::string("Thu after"));
263 first_kday_before
fkb(Tuesday
); // same ^
265 check("first kday after", ss2
.str() == std::string("Tue before"));
267 std::cout
<< pd
<< '\n'
275 /*******************************************************************/
276 /* Input Streaming for greg_month */
277 /*******************************************************************/
279 std::stringstream
ss1("January");
280 std::stringstream
ss2("dec"); // misspelled
281 std::stringstream
german("Okt");
282 german
.imbue(global2
);
285 check("Stream in month", m
== greg_month(Jan
));
286 #ifndef BOOST_NO_STD_WSTRING
287 std::wstringstream
ws1(L
"Dec");
289 check("Wide Stream in month", m
== greg_month(Dec
));
291 check("Wide Stream in not supported by this compiler", false);
292 #endif // BOOST_NO_STD_WSTRING
294 check("Stream in German month", m
== greg_month(Oct
));
296 ss2
>> m
; // misspelled
297 check("Bad month exception NOT thrown (misspelled name)", false);
299 check("Bad month exception caught (misspelled name)", true);
301 check("Bad month exception NOT caught (misspelled name)", false);
304 /*******************************************************************/
305 /* Input Streaming for greg_weekday */
306 /*******************************************************************/
308 std::stringstream
ss1("Sun");
309 std::stringstream
ss2("Wensday"); // misspelled
310 std::stringstream
german("Mittwoch"); // Wednesday
311 german
.imbue(global2
);
312 greg_weekday
wd(Friday
); //something not Sunday...
314 check("Stream in weekday", wd
== greg_weekday(Sunday
));
315 #ifndef BOOST_NO_STD_WSTRING
316 std::wstringstream
ws1(L
"Saturday");
318 check("Wide Stream in weekday", wd
== greg_weekday(Saturday
));
320 check("Wide Stream in not supported by this compiler", false);
321 #endif // BOOST_NO_STD_WSTRING
323 check("Stream in German weekday", wd
== greg_weekday(Wednesday
));
326 check("Bad weekday exception NOT thrown (misspelled name)", false);
327 }catch(bad_weekday
&){
328 check("Bad weekday exception caught (misspelled name)", true);
330 check("Bad weekday exception NOT caught (misspelled name)", false);
335 check("No tests executed - Locales not supported by this compiler", false);
337 #endif //BOOST_DATE_TIME_NO_LOCALE
339 return printTestStats();