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/local_time/local_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
18 using namespace boost::posix_time;
19 using namespace boost::gregorian;
20 using namespace boost::local_time;
21
iterate_backward(const local_date_time * answers,int ary_len,const time_duration & td)22 void iterate_backward(const local_date_time *answers,
23 int ary_len,
24 const time_duration& td)
25 {
26 int i = ary_len -1;
27 local_date_time end = answers[i];
28 local_time_iterator titr(end,td);
29
30 std::cout << "counting down by previous duration..." << std::endl;
31 for (; titr >= answers[0]; --titr) {
32 std::cout << *titr << std::endl;
33 check("iterating backward", answers[i] == *titr);
34 --i;
35 }
36 check("iterating backward count", i == -1); // check the number of iterations
37 std::cout << std::endl;
38 }
39
40 int
main()41 main()
42 {
43
44 time_zone_ptr ny_tz(new posix_time_zone("EST-05EDT,M4.1.0,M10.5.0"));
45
46 //set up a time right on the dst boundary -- iterator will
47 //jump forward an hour at the boundary
48 date d(2005,Apr,3);
49 time_duration dst_offset(1,59,59);
50 local_date_time start(d, dst_offset, ny_tz, false);
51
52 const local_date_time answer1[] =
53 {
54 local_date_time(d,dst_offset, ny_tz, false),
55 local_date_time(d,hours(3), ny_tz, true),
56 local_date_time(d,hours(3)+seconds(1), ny_tz, true),
57 local_date_time(d,hours(3)+seconds(2), ny_tz, true)
58 };
59
60 int i=0;
61 local_time_iterator titr(start,seconds(1));
62 local_date_time end = start + seconds(4);
63 for (; titr < end; ++titr) {
64 std::cout << (*titr) << std::endl;
65 check("iterator -- 1 sec", answer1[i] == *titr);
66 i++;
67 }
68 check("iterator -- 1 sec -- num iterations", i == 4); // check the number of iterations
69
70 iterate_backward(answer1, 4, seconds(1));
71
72 //iterate by hours
73 const local_date_time answer2[] =
74 { local_date_time(d,dst_offset, ny_tz, false),
75 local_date_time(d,dst_offset+hours(2), ny_tz, true),
76 local_date_time(d,dst_offset+hours(3), ny_tz, true),
77 local_date_time(d,dst_offset+hours(4), ny_tz, true)
78 };
79 i=0;
80 local_time_iterator titr2(start,hours(1));
81 local_date_time end2 = start + hours(4);
82 for (; titr2 < end2; ++titr2) {
83 std::cout << *titr2 << std::endl;
84 check("iterator -- 1 hour", answer2[i] == *titr2);
85 i++;
86 }
87 check("iterator -- 1 hour -- num iterations", i == 4);
88
89 iterate_backward(answer2, 4, hours(1));
90
91
92
93 return printTestStats();
94 }
95
96