1 /* Copyright (c) 2002,2003 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  * $Date$
7  */
8 
9 #include <iostream>
10 #include "boost/date_time/posix_time/posix_time.hpp"
11 #include "../testfrmwk.hpp"
12 #if defined(BOOST_DATE_TIME_INCLUDE_LIMITED_HEADERS)
13 #include "boost/date_time/gregorian/formatters_limited.hpp"
14 #else
15 #include "boost/date_time/gregorian/formatters.hpp"
16 #endif
17 
iterate_backward(const boost::posix_time::ptime * answers,int ary_len,const boost::posix_time::time_duration & td)18 void iterate_backward(const boost::posix_time::ptime *answers, int ary_len,
19     const boost::posix_time::time_duration& td)
20 {
21   using namespace boost::posix_time;
22   using namespace boost::gregorian;
23 
24   int i = ary_len -1;
25   ptime end = answers[i];
26   time_iterator titr(end,td);
27 
28   std::cout << "counting down by previous duration..." << std::endl;
29   for (; titr >= answers[0]; --titr) {
30     std::cout << to_simple_string(*titr) << std::endl;
31     check("iterating backward", answers[i] == *titr);
32     --i;
33   }
34   check("iterating backward count", i == -1); // check the number of iterations
35   std::cout << std::endl;
36 }
37 
38 int
main()39 main()
40 {
41   using namespace boost::posix_time;
42   using namespace boost::gregorian;
43 
44   date d(2000,Jan,20);
45   ptime start(d);
46   const ptime answer1[] = {ptime(d), ptime(d,seconds(1)),
47                            ptime(d,seconds(2)), ptime(d,seconds(3))};
48   int i=0;
49   time_iterator titr(start,seconds(1));
50   for (; titr < ptime(d,seconds(4)); ++titr) {
51     std::cout << to_simple_string(*titr) << std::endl;
52     check("iterator -- 1 sec", answer1[i] == *titr);
53     i++;
54   }
55   check("iterator -- 1 sec", i == 4); // check the number of iterations
56 
57   iterate_backward(answer1, 4, seconds(1));
58 
59   //iterate by hours
60   const ptime answer2[] = {ptime(d), ptime(d,hours(1)),
61                            ptime(d,hours(2)), ptime(d,hours(3))};
62   i=0;
63   time_iterator titr2(start,hours(1));
64   for (; titr2 < ptime(d,hours(4)); ++titr2) {
65     std::cout << to_simple_string(*titr2) << std::endl;
66     check("iterator -- 1 hour", answer2[i] == *titr2);
67     i++;
68   }
69   check("iterator -- 1 hour", i == 4); // check the number of iterations
70 
71   iterate_backward(answer2, 4, hours(1));
72 
73 
74   //iterate by 15 mintues
75   const ptime answer3[] = {ptime(d), ptime(d,minutes(15)),
76                            ptime(d,minutes(30)), ptime(d,minutes(45)),
77                            ptime(d,minutes(60)), ptime(d,minutes(75))};
78   i=0;
79   time_iterator titr3(start,minutes(15));
80   for (; titr3 < ptime(d,time_duration(1,20,0)); ++titr3) {
81     std::cout << to_simple_string(*titr3) << std::endl;
82     check("iterator -- 15 min", answer3[i] == *titr3);
83     i++;
84   }
85   check("iterator -- 15 min", i == 6); // check the number of iterations
86 
87   iterate_backward(answer3, 6, minutes(15));
88 
89   //iterate by .1 seconds
90   const ptime answer4[] = {ptime(d), ptime(d,time_duration(0,0,0,1000)),
91                            ptime(d,time_duration(0,0,0,2000)),
92                            ptime(d,time_duration(0,0,0,3000))};
93   i=0;
94   time_iterator titr4(start,time_duration(0,0,0,1000));
95   for (; titr4 < ptime(d,time_duration(0,0,0,4000)); ++titr4) {
96     std::cout << to_simple_string(*titr4) << std::endl;
97     check("iterator -- tenth sec", answer4[i] == *titr4);
98     i++;
99   }
100   check("iterator -- tenth sec", i == 4); // check the number of iterations
101 
102   iterate_backward(answer4, 4, time_duration(0,0,0,1000));
103 
104   //iterate by crazy duration
105   time_duration crzyd = duration_from_string("2:18:32.423");
106   const ptime answer5[] = {ptime(d), ptime(d,crzyd),
107                            ptime(d, crzyd * 2),
108                            ptime(d, crzyd * 3)};
109   i=0;
110   time_iterator titr5(start,crzyd);
111   for (; titr5 < ptime(d,crzyd * 4); ++titr5) {
112     std::cout << to_simple_string(*titr5) << std::endl;
113     check("iterator -- crazy duration", answer5[i] == *titr5);
114     i++;
115   }
116   check("iterator -- crazy duration", i == 4); // check the number of iterations
117 
118   iterate_backward(answer5, 4, crzyd);
119 
120   //iterate up by neg_dur
121   time_duration pos_dur = minutes(3);
122   time_duration neg_dur = -pos_dur;
123   time_iterator up_itr(start, neg_dur), dn_itr(start, pos_dur);
124 
125   for(i=0; i < 10; ++i)
126   {
127     check("up-by-neg == backward iterate", *up_itr == *dn_itr);
128     ++up_itr; // up by -3
129     --dn_itr; // down by 3
130   }
131 
132 
133   return printTestStats();
134 }
135 
136