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 "boost/date_time/gregorian/gregorian.hpp"
9 #include "../testfrmwk.hpp"
10 #include <iostream>
11 #include <string>
12 #include <sstream>
13 
14 int
main()15 main()
16 {
17 
18   using namespace boost::gregorian;
19 
20   partial_date pd1(1,Jan);
21   date d = pd1.get_date(2000);
22   check("Partial date to_string()", pd1.to_string() == std::string("0"));
23   check("Partial date getdate",     date(2000,1,1) == d);
24   d = pd1.get_date(2001);
25   check("Partial date getdate",     date(2001,1,1) == d);
26   partial_date pd2(1,Feb);
27   check("Partial date to_string()", pd2.to_string() == std::string("31"));
28   check("Partial date operator==",  pd1 == pd1);
29   check("Partial date operator==",  !(pd1 == pd2));
30   check("Partial date operator==",  !(pd2 == pd1));
31   check("Partial date operator<",   !(pd1 < pd1));
32   check("Partial date operator<",    pd1 < pd2);
33   check("Partial date operator<",   !(pd2 < pd1));
34 
35   typedef last_day_of_the_week_in_month lastkday;
36 
37   //Find last Sunday in Feb
38   lastkday lsif(Sunday, Feb);
39   std::cout << lsif.get_date(2002) << std::endl; //24th
40   check("Last kday",     date(2002,Feb,24) == lsif.get_date(2002));
41   check("Last kday to_string()", lsif.to_string() == std::string("M2.5.0"));
42   lastkday ltif(Thursday, Feb);
43   check("Last kday",     date(2002,Feb,28) == ltif.get_date(2002));
44   check("Last kday to_string()", ltif.to_string() == std::string("M2.5.4"));
45   lastkday lfif(Friday, Feb);
46   check("Last kday",     date(2002,Feb,22) == lfif.get_date(2002));
47   check("Last kday to_string()", lfif.to_string() == std::string("M2.5.5"));
48 
49   typedef first_day_of_the_week_in_month firstkday;
50 
51   firstkday fsif(Sunday, Feb);
52   std::cout << fsif.get_date(2002) << std::endl; //24th
53   check("First kday",     date(2002,Feb,3) == fsif.get_date(2002));
54   check("First kday to_string()", fsif.to_string() == std::string("M2.1.0"));
55   firstkday ftif(Thursday, Feb);
56   check("First kday",     date(2002,Feb,7) == ftif.get_date(2002));
57   check("First kday to_string()", ftif.to_string() == std::string("M2.1.4"));
58   firstkday ffif(Friday, Feb);
59   check("First kday",     date(2002,Feb,1) == ffif.get_date(2002));
60   check("First kday to_string()", ffif.to_string() == std::string("M2.1.5"));
61 
62   typedef first_day_of_the_week_after firstkdayafter;
63   firstkdayafter fkaf(Monday);
64   std::cout << fkaf.get_date(date(2002,Feb,1)) << std::endl; //feb 4
65   check("kday after",date(2002,Feb,4) == fkaf.get_date(date(2002,Feb,1)));
66   firstkdayafter fkaf2(Thursday);
67   check("kday after",date(2002,Feb,7) == fkaf2.get_date(date(2002,Feb,1)));
68   check("kday after",date(2002,Feb,28)== fkaf2.get_date(date(2002,Feb,21)));
69 
70   typedef first_day_of_the_week_before firstkdaybefore;
71   firstkdaybefore fkbf(Monday);
72   std::cout << fkaf.get_date(date(2002,Feb,10))
73             << std::endl; //feb 4
74   check("kday before",date(2002,Feb,4) == fkbf.get_date(date(2002,Feb,10)));
75   firstkdaybefore fkbf2(Thursday);
76   check("kday before",date(2002,Jan,31) == fkbf2.get_date(date(2002,Feb,1)));
77   check("kday before",date(2002,Feb,7)== fkbf2.get_date(date(2002,Feb,14)));
78 
79   typedef nth_day_of_the_week_in_month nthkdayofmonth;
80   nthkdayofmonth nkd1(nthkdayofmonth::third, Sunday, Jul);
81   check("nth_kday 1", date(1969, Jul, 20) == nkd1.get_date(1969));
82   check("Nth kday to_string()", nkd1.to_string() == std::string("M7.3.0"));
83   nthkdayofmonth nkd2(nthkdayofmonth::second, Monday, Dec);
84   check("nth_kday 2", date(1980, Dec, 8) == nkd2.get_date(1980));
85   check("Nth kday to_string()", nkd2.to_string() == std::string("M12.2.1"));
86   nthkdayofmonth nkd3(nthkdayofmonth::fifth, Wednesday, Jan);
87   check("nth_kday fifth wed jan 2003 2003-Jan-29",
88         date(2003, Jan, 29) == nkd3.get_date(2003));
89   check("Nth kday to_string()", nkd3.to_string() == std::string("M1.5.3"));
90   nthkdayofmonth nkd4(nthkdayofmonth::fifth, Monday, Jan);
91   check("nth_kday fifth mon jan 2003 (actaully 4th) 2003-Jan-27",
92         date(2003, Jan, 27) == nkd4.get_date(2003));
93   check("Nth kday to_string()", nkd4.to_string() == std::string("M1.5.1"));
94 
95   // greg date_generator functions tests
96   {
97     date sunday(2003,Feb,2),tuesday(2003,Feb,4);
98     date friday(2003,Feb,7),saturday(2003,Feb,8);
99     greg_weekday sat(Saturday), tue(Tuesday), fri(Friday), sund(Sunday);
100 
101     check("Days until weekday" , days_until_weekday(saturday, sund) == days(1));
102     check("Days until weekday" , days_until_weekday(friday, tue) == days(4));
103     check("Days until weekday" , days_until_weekday(tuesday, fri) == days(3));
104     check("Days until weekday" , days_until_weekday(sunday, sat) == days(6));
105     check("Days until weekday" , days_until_weekday(sunday, sund) == days(0));
106     check("Days until weekday" , days_until_weekday(tuesday, tue) == days(0));
107 
108     check("Days before weekday" , days_before_weekday(saturday, sund) == days(6));
109     check("Days before weekday" , days_before_weekday(friday, tue) == days(3));
110     check("Days before weekday" , days_before_weekday(tuesday, fri) == days(4));
111     check("Days before weekday" , days_before_weekday(sunday, sat) == days(1));
112     check("Days before weekday" , days_before_weekday(sunday, sund) == days(0));
113     check("Days before weekday" , days_before_weekday(tuesday, tue) == days(0));
114 
115     check("Date of next weekday", next_weekday(saturday, sund)== date(2003,Feb,9));
116     check("Date of next weekday", next_weekday(friday, tue) == date(2003,Feb,11));
117     check("Date of next weekday", next_weekday(tuesday, fri) == date(2003,Feb,7));
118     check("Date of next weekday", next_weekday(sunday, sat) == date(2003,Feb,8));
119     check("Date of next weekday", next_weekday(sunday, sund) == sunday);
120     check("Date of next weekday", next_weekday(tuesday, tue) == tuesday);
121 
122     check("Date of previous weekday", previous_weekday(saturday, sund)== date(2003,Feb,2));
123     check("Date of previous weekday", previous_weekday(friday, tue) == date(2003,Feb,4));
124     check("Date of previous weekday", previous_weekday(tuesday, fri) == date(2003,Jan,31));
125     check("Date of previous weekday", previous_weekday(sunday, sat) == date(2003,Feb,1));
126     check("Date of previous weekday", previous_weekday(sunday, sund) == sunday);
127     check("Date of previous weekday", previous_weekday(tuesday, tue) == tuesday);
128 
129   }
130 #ifndef BOOST_DATE_TIME_NO_LOCALE
131 #if !defined(USE_DATE_TIME_PRE_1_33_FACET_IO)
132   //TODO: this is temporary condition -- don't force a failure...
133   //  check("no streaming implemented for new facet", false);
134 #else
135   // streaming tests...
136   std::stringstream ss("");
137   std::string s("");
138 
139   ss.str("");
140   ss << pd1;
141   s = "01 Jan";
142   check("streaming partial_date", ss.str() == s);
143   std::cout << ss.str() << std::endl;
144 
145   ss.str("");
146   ss << lsif;
147   s = "last Sun of Feb";
148   check("streaming last_kday_of_month", ss.str() == s);
149 
150   ss.str("");
151   ss << fsif;
152   s = "first Sun of Feb";
153   check("streaming first_kday_of_month", ss.str() == s);
154 
155   ss.str("");
156   ss << fkaf;
157   s = "Mon after";
158   check("streaming first_kday_after", ss.str() == s);
159 
160   ss.str("");
161   ss << fkbf;
162   s = "Mon before";
163   check("streaming first_kday_before", ss.str() == s);
164 
165   ss.str("");
166   ss << nkd1;
167   s = "third Sun of Jul";
168   check("streaming nth_kday", ss.str() == s);
169 #endif // USE_DATE_TIME_PRE_1_33_FACET_IO
170 #endif // NO_LOCAL
171 
172   return printTestStats();
173 
174 }
175 
176