1 
2 
3 /* Copyright (c) 2004 CrystalClear Software, Inc.
4  * Use, modification and distribution is subject to the
5  * Boost Software License, Version 1.0. (See accompanying
6  * file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
7  * Author: Jeff Garland, Bart Garst
8  * $Date$
9  */
10 
11 #include "boost/date_time/gregorian/gregorian.hpp"
12 #include "boost/date_time/date_facet.hpp"
13 #include "../testfrmwk.hpp"
14 #include <iostream>
15 #include <sstream>
16 
17 
18 template<class temporal_type, typename charT>
19 inline
20 void
teststreaming(std::string testname,temporal_type value,std::basic_string<charT> expected_result,const std::locale & locale=std::locale::classic ())21 teststreaming(std::string testname,
22               temporal_type value,
23               std::basic_string<charT> expected_result,
24               const std::locale& locale = std::locale::classic())
25 {
26   std::basic_stringstream<charT> ss;
27   ss.imbue(locale);
28   ss << value;
29   check_equal(testname, ss.str(), expected_result);
30 }
31 
32 
33 // collections for adding to facet
34 const char* const month_short_names[]={"*jan*","*feb*","*mar*",
35                                        "*apr*","*may*","*jun*",
36                                        "*jul*","*aug*","*sep*",
37                                        "*oct*","*nov*","*dec*"};
38 
39 const char* const month_long_names[]={"**January**","**February**","**March**",
40                                       "**April**","**May**","**June**",
41                                       "**July**","**August**","**September**",
42                                       "**October**","**November**","**December**"};
43 
44 const char* const weekday_short_names[]={"day1", "day2","day3","day4",
45                                          "day5","day6","day7"};
46 
47 const char* const weekday_long_names[]= {"Sun-0", "Mon-1", "Tue-2",
48                                          "Wed-3", "Thu-4",
49                                          "Fri-5", "Sat-6"};
50 
51 std::vector<std::basic_string<char> > short_weekday_names;
52 std::vector<std::basic_string<char> > long_weekday_names;
53 std::vector<std::basic_string<char> > short_month_names;
54 std::vector<std::basic_string<char> > long_month_names;
55 
56 #if !defined(BOOST_NO_STD_WSTRING)
57 // collections of test results
58 const std::wstring full_months[]={L"January",L"February",L"March",
59                                   L"April",L"May",L"June",
60                                   L"July",L"August",L"September",
61                                   L"October",L"November",L"December"};
62 const std::wstring short_months[]={L"Jan",L"Feb",L"Mar",
63                                    L"Apr",L"May",L"Jun",
64                                    L"Jul",L"Aug",L"Sep",
65                                    L"Oct",L"Nov",L"Dec"};
66 
67 const std::wstring full_weekdays[]= {L"Sunday", L"Monday",L"Tuesday",
68                                      L"Wednesday", L"Thursday",
69                                      L"Friday", L"Saturday"};
70 const std::wstring short_weekdays[]= {L"Sun", L"Mon",L"Tue",
71                                       L"Wed", L"Thu",
72                                       L"Fri", L"Sat"};
73 
74 //const whcar_t const
75 #endif // BOOST_NO_STD_WSTRING
76 
main()77 int main() {
78   using namespace boost::gregorian;
79 
80   std::copy(&month_short_names[0],
81             &month_short_names[12],
82             std::back_inserter(short_month_names));
83 
84   std::copy(&month_long_names[0],
85             &month_long_names[12],
86             std::back_inserter(long_month_names));
87 
88   std::copy(&weekday_short_names[0],
89             &weekday_short_names[7],
90             std::back_inserter(short_weekday_names));
91 
92   std::copy(&weekday_long_names[0],
93             &weekday_long_names[7],
94             std::back_inserter(long_weekday_names));
95 
96   {
97     std::stringstream ss;
98     date d(2004,Oct,31);
99     date_period dp(d, d + days(7));
100     ss << d;
101     check("to_string & default formats match",
102         to_simple_string(d) == ss.str());
103     ss.str("");
104     ss << dp;
105     check("to_string & default formats match",
106         to_simple_string(dp) == ss.str());
107   }
108 
109   {
110     date d(2004,Oct, 13);
111     date_period dp(d, d + days(7));
112     {
113       date_facet* datefacet = new date_facet();
114       datefacet->format(date_facet::standard_format_specifier);
115       std::cout.imbue(std::locale(std::locale::classic(), datefacet));
116       teststreaming("default classic date", d, std::string("10/13/04"),
117                     std::locale(std::locale::classic(), datefacet));
118       std::cout << "default classic date output: " << d << std::endl;
119 
120     }
121     {
122       date_facet* datefacet = new date_facet();
123       datefacet->format(date_facet::standard_format_specifier);
124       teststreaming("default classic date period", dp,
125                     std::string("[10/13/04/10/19/04]"),
126                     std::locale(std::locale::classic(), datefacet));
127     }
128 
129     {
130       date_facet* datefacet = new date_facet();
131       datefacet->format("%Y-%d-%b %a");
132       teststreaming("custom date facet date period", dp,
133                     std::string("[2004-13-Oct Wed/2004-19-Oct Tue]"),
134                     std::locale(std::locale::classic(), datefacet));
135     }
136 
137     {
138       date_facet* datefacet = new date_facet();
139       datefacet->set_iso_format();
140       teststreaming("custom date facet date", d,
141                     std::string("20041013"),
142                     std::locale(std::locale::classic(), datefacet));
143 
144     }
145     {
146       date_facet* datefacet = new date_facet();
147       datefacet->set_iso_format();
148       teststreaming("custom date facet date period", dp,
149                     std::string("[20041013/20041019]"),
150                     std::locale(std::locale::classic(), datefacet));
151     }
152 
153     {
154       date_facet* datefacet = new date_facet();
155       datefacet->set_iso_extended_format();
156       teststreaming("custom date facet date", d,
157                     std::string("2004-10-13"),
158                     std::locale(std::locale::classic(), datefacet));
159 
160     }
161     {
162       date_facet* datefacet = new date_facet();
163       datefacet->set_iso_extended_format();
164       teststreaming("custom date facet date period", dp,
165                     std::string("[2004-10-13/2004-10-19]"),
166                     std::locale(std::locale::classic(), datefacet));
167     }
168 
169     {
170       date_facet* datefacet = new date_facet();
171       datefacet->set_iso_extended_format();
172       period_formatter pf(period_formatter::AS_OPEN_RANGE, " / ", "[ ", " )", " ]");
173       datefacet->period_formatter(pf);
174       teststreaming("custom date facet date period - open range custom delimeters", dp,
175                     std::string("[ 2004-10-13 / 2004-10-20 )"),
176                     std::locale(std::locale::classic(), datefacet));
177     }
178 
179     // trac-11142: actually test delimiter_strings(...)
180     {
181         date_facet* datefacet = new date_facet();
182         datefacet->set_iso_extended_format();
183         period_formatter pf(period_formatter::AS_OPEN_RANGE, " / ", "[ ", " )", " ]");
184         pf.delimiter_strings(" to ", "from ", " inclusive", " exclusive");
185         datefacet->period_formatter(pf);
186         teststreaming("custom date facet date period - delimiter_strings", dp,
187             std::string("from 2004-10-13 to 2004-10-20 inclusive"),
188             std::locale(std::locale::classic(), datefacet));
189     }
190 
191     {
192       date_facet* datefacet = new date_facet("%A %b %d, %Y");
193       datefacet->short_month_names(short_month_names);
194       teststreaming("custom date facet -- custom short month names", d,
195                     std::string("Wednesday *oct* 13, 2004"),
196                     std::locale(std::locale::classic(), datefacet));
197     }
198 
199     {
200       date_facet* datefacet = new date_facet("%B %A %d, %Y");
201       datefacet->long_month_names(long_month_names);
202       teststreaming("custom date facet -- custom long month names", d,
203                     std::string("**October** Wednesday 13, 2004"),
204                     std::locale(std::locale::classic(), datefacet));
205     }
206 
207     {
208       date_facet* datefacet = new date_facet("%a - %b %d, %Y");
209       datefacet->short_weekday_names(short_weekday_names);
210       std::cout.imbue(std::locale(std::locale::classic(), datefacet));
211       std::cout << d << std::endl;
212       teststreaming("custom date facet -- custom short weekday names", d,
213                     std::string("day4 - Oct 13, 2004"),
214                     std::locale(std::locale::classic(), datefacet));
215     }
216 
217     {
218       date_facet* datefacet = new date_facet("%b %d, %Y ++ %A");
219       datefacet->long_weekday_names(long_weekday_names);
220       teststreaming("custom date facet -- custom short weekday names", d,
221                     std::string("Oct 13, 2004 ++ Wed-3"),
222                     std::locale(std::locale::classic(), datefacet));
223     }
224 
225     {//date
226       date_facet* datefacet = new date_facet("%Y-%b-%d %%d");
227       teststreaming("Literal '%' in date format", d,
228                     std::string("2004-Oct-13 %d"),
229                     std::locale(std::locale::classic(), datefacet));
230     }
231     {
232       date_facet* datefacet = new date_facet("%Y-%b-%d %%%d");
233       teststreaming("Multiple literal '%'s in date format", d,
234                     std::string("2004-Oct-13 %13"),
235                     std::locale(std::locale::classic(), datefacet));
236     }
237     {
238       date d1(2004,Oct, 13);
239       date_facet* datefacet = new date_facet("%d%m%y");
240       teststreaming("Single digit year and %y", d1,
241                     std::string("131004"),
242                     std::locale(std::locale::classic(), datefacet));
243     }
244     {//month
245       date_facet* datefacet = new date_facet();
246       datefacet->month_format("%b %%b");
247       teststreaming("Literal '%' in month format", d.month(),
248                     std::string("Oct %b"),
249                     std::locale(std::locale::classic(), datefacet));
250     }
251     {
252       date_facet* datefacet = new date_facet();
253       datefacet->month_format("%b %%%b");
254       teststreaming("Multiple literal '%'s in month format", d.month(),
255                     std::string("Oct %Oct"),
256                     std::locale(std::locale::classic(), datefacet));
257     }
258     {//weekday
259       date_facet* datefacet = new date_facet();
260       datefacet->weekday_format("%a %%a");
261       teststreaming("Literal '%' in weekday format", d.day_of_week(),
262                     std::string("Wed %a"),
263                     std::locale(std::locale::classic(), datefacet));
264     }
265     {
266       date_facet* datefacet = new date_facet();
267       datefacet->weekday_format("%a %%%a");
268       teststreaming("Multiple literal '%'s in weekday format", d.day_of_week(),
269                     std::string("Wed %Wed"),
270                     std::locale(std::locale::classic(), datefacet));
271     }
272 
273 
274 
275     date d_not_date(not_a_date_time);
276     teststreaming("special value, no special facet", d_not_date, std::string("not-a-date-time"));
277 
278 
279 //       std::cout.imbue(std::locale(std::locale::classic(), datefacet));
280 //       std::cout << d << std::endl;
281 
282 
283   }
284 
285   // date_generator tests
286   {
287     partial_date pd(31,Oct);
288     teststreaming("partial date", pd, std::string("31 Oct"));
289     first_kday_of_month fkd(Tuesday, Sep);
290     teststreaming("first kday", fkd, std::string("first Tue of Sep"));
291     nth_kday_of_month nkd2(nth_kday_of_month::second, Tuesday, Sep);
292     teststreaming("nth kday", nkd2, std::string("second Tue of Sep"));
293     nth_kday_of_month nkd3(nth_kday_of_month::third, Tuesday, Sep);
294     teststreaming("nth kday", nkd3, std::string("third Tue of Sep"));
295     nth_kday_of_month nkd4(nth_kday_of_month::fourth, Tuesday, Sep);
296     teststreaming("nth kday", nkd4, std::string("fourth Tue of Sep"));
297     nth_kday_of_month nkd5(nth_kday_of_month::fifth, Tuesday, Sep);
298     teststreaming("nth kday", nkd5, std::string("fifth Tue of Sep"));
299     last_kday_of_month lkd(Tuesday, Sep);
300     teststreaming("last kday", lkd, std::string("last Tue of Sep"));
301     first_kday_before fkb(Wednesday);
302     teststreaming("First before", fkb, std::string("Wed before"));
303     first_kday_after fka(Thursday);
304     teststreaming("First after", fka, std::string("Thu after"));
305   }
306 
307 #if !defined(BOOST_NO_STD_WSTRING)
308     date d(2004,Oct, 13);
309     date_period dp(d, d + days(7));
310     date d_not_date(not_a_date_time);
311 
312     teststreaming("special value, no special facet wide", d_not_date,
313                   std::wstring(L"not-a-date-time"));
314   {
315     wdate_facet* wdatefacet = new wdate_facet();
316     wdatefacet->format(wdate_facet::standard_format_specifier);
317     teststreaming("widestream default classic date", d,
318                   std::wstring(L"10/13/04"),
319                   std::locale(std::locale::classic(), wdatefacet));
320   }
321   {
322     wdate_facet* wdatefacet = new wdate_facet();
323     wdatefacet->format(wdate_facet::standard_format_specifier);
324     teststreaming("widestream default classic date period", dp,
325                   std::wstring(L"[10/13/04/10/19/04]"),
326                   std::locale(std::locale::classic(), wdatefacet));
327   }
328   {
329     wdate_facet* wdatefacet = new wdate_facet();
330     wdatefacet->format(L"%Y-%d-%b %a");
331     teststreaming("widestream custom date facet", d,
332                   std::wstring(L"2004-13-Oct Wed"),
333                   std::locale(std::locale::classic(), wdatefacet));
334   }
335   {
336     wdate_facet* wdatefacet = new wdate_facet();
337     wdatefacet->format(L"%Y-%d-%b %a");
338     teststreaming("widestream custom date facet date period", dp,
339                   std::wstring(L"[2004-13-Oct Wed/2004-19-Oct Tue]"),
340                   std::locale(std::locale::classic(), wdatefacet));
341   }
342   {
343     wdate_facet* wdatefacet = new wdate_facet();
344     wdatefacet->set_iso_extended_format();
345     wperiod_formatter pf(wperiod_formatter::AS_OPEN_RANGE, L" / ", L"[ ", L" )", L" ]");
346     wdatefacet->period_formatter(pf);
347     teststreaming("custom date facet date period - open range custom delimeters", dp,
348                   std::wstring(L"[ 2004-10-13 / 2004-10-20 )"),
349                   std::locale(std::locale::classic(), wdatefacet));
350   }
351   /************* small gregorian types tests *************/
352   wdate_facet* small_types_facet = new wdate_facet();
353   std::locale loc = std::locale(std::locale::classic(), small_types_facet);
354 
355   // greg_year test
356   greg_year gy(2004);
357   teststreaming("greg_year", gy, std::string("2004"));
358 
359   // greg_month tests
360   {
361     for(greg_month::value_type i = 0; i < 12; ++i) {
362       greg_month m(i+1); // month numbers 1-12
363       teststreaming("greg_month short", m, short_months[i], loc);
364     }
365     small_types_facet->month_format(L"%B"); // full name
366     for(greg_month::value_type i = 0; i < 12; ++i) {
367       greg_month m(i+1); // month numbers 1-12
368       teststreaming("greg_month full", m, full_months[i], loc);
369     }
370   }
371 
372   // greg_weekday tests
373   {
374     for(greg_weekday::value_type i = 0; i < 7; ++i) {
375       greg_weekday gw(i); // weekday numbers 0-6
376       teststreaming("greg_weekday short", gw, short_weekdays[i], loc);
377     }
378     small_types_facet->weekday_format(L"%A"); // full name
379     for(greg_weekday::value_type i = 0; i < 7; ++i) {
380       greg_weekday gw(i); // weekday numbers 0-6
381       teststreaming("greg_weekday full", gw, full_weekdays[i], loc);
382     }
383   }
384 #endif // BOOST_NO_STD_WSTRING
385 
386   return printTestStats();
387 }
388 
389