1 /*
2 This example demonstrates a simple use of periods for the calculation
3 of date information.
4
5 The example calculates if a given date is a weekend or holiday
6 given an exclusion set. That is, each weekend or holiday is
7 entered into the set as a time interval. Then if a given date
8 is contained within any of the intervals it is considered to
9 be within the exclusion set and hence is a offtime.
10
11 Output:
12 Number Excluded Periods: 5
13 20020202/20020203
14 20020209/20020210
15 20020212/20020212
16 20020216/20020217
17 In Exclusion Period: 20020216 --> 20020216/20020217
18 20020223/20020224
19
20 */
21
22
23 #include "boost/date_time/gregorian/gregorian.hpp"
24 #include <set>
25 #include <algorithm>
26 #include <iostream>
27
28 typedef std::set<boost::gregorian::date_period> date_period_set;
29
30 //Simple population of the exclusion set
31 date_period_set
generateExclusion()32 generateExclusion()
33 {
34 using namespace boost::gregorian;
35 date_period periods_array[] =
36 { date_period(date(2002,Feb,2), date(2002,Feb,4)),//weekend of 2nd-3rd
37 date_period(date(2002,Feb,9), date(2002,Feb,11)),
38 date_period(date(2002,Feb,16), date(2002,Feb,18)),
39 date_period(date(2002,Feb,23), date(2002,Feb,25)),
40 date_period(date(2002,Feb,12), date(2002,Feb,13))//a random holiday 2-12
41 };
42 const int num_periods = sizeof(periods_array)/sizeof(date_period);
43
44 date_period_set ps;
45 //insert the periods in the set
46 std::insert_iterator<date_period_set> itr(ps, ps.begin());
47 std::copy(periods_array, periods_array+num_periods, itr );
48 return ps;
49
50 }
51
52
main()53 int main()
54 {
55 using namespace boost::gregorian;
56
57 date_period_set ps = generateExclusion();
58 std::cout << "Number Excluded Periods: " << ps.size() << std::endl;
59
60 date d(2002,Feb,16);
61 date_period_set::const_iterator i = ps.begin();
62 //print the periods, check for containment
63 for (;i != ps.end(); i++) {
64 std::cout << to_iso_string(*i) << std::endl;
65 //if date is in exclusion period then print it
66 if (i->contains(d)) {
67 std::cout << "In Exclusion Period: "
68 << to_iso_string(d) << " --> " << to_iso_string(*i)
69 << std::endl;
70 }
71 }
72
73 return 0;
74
75 }
76
77
78
79
80 /* Copyright 2001-2004: CrystalClear Software, Inc
81 * http://www.crystalclearsoftware.com
82 *
83 * Subject to the Boost Software License, Version 1.0.
84 * (See accompanying file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
85 */
86
87