1 /* Copyright (c) 2003-2004 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: Bart Garst
6  * $Date$
7  */
8 #include <iostream>
9 #include <sstream>
10 #include <boost/date_time/gregorian/gregorian.hpp>
11 #include "../testfrmwk.hpp"
12 
13 using namespace boost::gregorian;
14 
main()15 int main(){
16 #if defined(BOOST_NO_STD_WSTRING) || \
17     defined(BOOST_DATE_TIME_INCLUDE_LIMITED_HEADERS)
18   check("No wstring/wstream support for this compiler", false);
19 #else
20   std::wstring res, ws;
21   std::wstringstream wss;
22   /* date_period is used because almost all the date-type objects
23    * that have the operator<< can be easily accessed from it.
24    * Those are: date, greg_month, greg_day_of_week,
25    * date_period, date_duration (also date_generators) */
26   date_period dp(date(2003,Aug,21), date(2004,May,27));
27 
28 
29   // date
30   wss << dp.begin();
31   res = L"2003-Aug-21";
32   check("date operator<<", wss.str() == res);
33   wss.str(L"");
34   ws = to_simple_wstring(dp.begin());
35   check("date to_simple_string", ws == res);
36   ws = to_iso_wstring(dp.begin());
37   res = L"20030821";
38   check("date to_iso_string", ws == res);
39   ws = to_iso_extended_wstring(dp.begin());
40   res = L"2003-08-21";
41   check("date to_iso_extended_string", ws == res);
42   ws = to_sql_wstring(dp.begin());
43   check("date to_sql_string", ws == res);
44   wss.str(L"");
45 #ifndef BOOST_NO_STD_ITERATOR_TRAITS
46   {
47     res = L"2003-Aug-21";
48     std::wstringstream wss2(L"2003-Aug-21");
49     date testdate(not_a_date_time);
50     wss2 >> testdate;
51     check("date operator>>", to_simple_wstring(testdate) == res);
52   }
53 #else
54   check("no date operator>> for this compiler", false);
55 #endif
56 
57   // greg_month
58   wss << dp.begin().month();
59   res = L"08";
60   check("greg_month", wss.str() == res);
61   wss.str(L"");
62   ws = dp.begin().month().as_short_wstring();
63   res = L"Aug";
64   check("greg_month as_short_wstring", ws == res);
65   ws = dp.begin().month().as_long_wstring();
66   res = L"August";
67   check("greg_month as_long_wstring", ws == res);
68   /*{
69     std::wstringstream wss2(L"August");
70     greg_month testmonth(not_a_date_time);
71     wss2 >> testmonth;
72     check("greg_month operator>>", to_simple_wstring(testmonth) == res);
73   }*/
74 
75   // greg_day_of_week
76   wss << dp.begin().day_of_week();
77   res = L"Thu";
78   check("greg_day_of_week", wss.str() == res);
79   wss.str(L"");
80   ws = dp.begin().day_of_week().as_short_wstring();
81   check("greg_day_of_week as_short_wstring", ws == res);
82   ws = dp.begin().day_of_week().as_long_wstring();
83   res = L"Thursday";
84   check("greg_day_of_week as_long_wstring", ws == res);
85   /*{
86     std::wstringstream wss2(L"Thu");
87     greg_day_of_week testday(not_a_date_time);
88     wss2 >> testday;
89     check("greg_day_of_week operator>>", to_simple_wstring(testday) == res);
90   }*/
91 
92   // date_period
93   wss << dp;
94   res = L"[2003-Aug-21/2004-May-26]";
95   check("date_period", wss.str() == res);
96   wss.str(L"");
97   ws = to_simple_wstring(dp);
98   check("date_period to_simple_string", ws == res);
99   res = L"20030821/20040526";
100   ws = to_iso_wstring(dp);
101   check("date_period to_iso_string", ws == res);
102   {
103     std::wstringstream wss2(L"[2003-Aug-21/2004-May-27]");
104     res = L"[2003-Aug-21/2004-May-26]";
105     // following line gives an ambiguous overload of op>>
106     //date_period testperiod(date(not_a_date_time),date_duration(not_a_date_time));
107     date_period testperiod(date(2003,Aug,21), date(2004,May,27));
108     wss2 >> testperiod;
109     check("date_period operator>>", to_simple_wstring(testperiod) == res);
110   }
111 
112   // date_duration
113   wss << dp.length();
114   res = L"280";
115   check("date_duration", wss.str() == res);
116   wss.str(L"");
117   {
118     std::wstringstream wss2(L"280");
119     date_duration testduration(not_a_date_time);
120     wss2 >> testduration;
121     check("date_duration operator>>", testduration.days() == 280);
122   }
123 
124   // special values
125   date sv_d(neg_infin);
126   date_duration sv_dd(pos_infin);
127   //date_period sv_dp(sv_d,sv_dd);
128   // sv-date
129   wss << sv_d;
130   res = L"-infinity";
131   check("date operator<< special value", wss.str() == res);
132   wss.str(L"");
133   ws = to_simple_wstring(sv_d);
134   check("date to_simple_string special value", ws == res);
135   // sv-date_duration
136   wss << sv_dd;
137   res = L"+infinity";
138   check("date_duration operator<< special value", wss.str() == res);
139   wss.str(L"");
140   // sv-date_period
141   /*
142    * works - just gives unexpected results:
143    *[-infinity/not-a-date-time]
144   wss << sv_dp;
145   res = L"[2003-Aug-21/2004-May-26]";
146   check("date_period", wss.str() == res);
147   std::wcout << wss.str() << std::endl;
148   wss.str(L"");
149   */
150 
151   // date_generators
152   first_day_of_the_week_after fka(Monday);
153   wss << fka;
154   res = L"Mon after";
155   check("first_kday_after", wss.str() == res);
156   wss.str(L"");
157 
158   first_day_of_the_week_before fkb(Monday);
159   wss << fkb;
160   res = L"Mon before";
161   check("first_kday_before", wss.str() == res);
162   wss.str(L"");
163 
164   first_day_of_the_week_in_month fkom(Monday,Jan);
165   wss << fkom;
166   res = L"first Mon of Jan";
167   check("first_kday_of_month", wss.str() == res);
168   wss.str(L"");
169 
170   last_day_of_the_week_in_month lkom(Monday,Jan);
171   wss << lkom;
172   res = L"last Mon of Jan";
173   check("last_kday_of_month", wss.str() == res);
174   wss.str(L"");
175 
176 #endif
177   return printTestStats();
178 }
179