1 <?xml version=
"1.0" encoding=
"utf-8"?>
2 <!DOCTYPE library PUBLIC
"-//Boost//DTD BoostBook XML V1.0//EN"
3 "../../../tools/boostbook/dtd/boostbook.dtd">
5 <!-- Copyright (c) 2001-2004 CrystalClear Software, Inc.
6 Subject to the Boost Software License, Version 1.0.
7 (See accompanying file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
10 <section id=
"date_time.examples.date_period_calc">
11 <title>Date Period Calculations
</title>
14 Calculates if a date is in an 'irregular' collection of periods using period calculation functions.
19 This example demonstrates a simple use of periods for the calculation
22 The example calculates if a given date is a weekend or holiday
23 given an exclusion set. That is, each weekend or holiday is
24 entered into the set as a time interval. Then if a given date
25 is contained within any of the intervals it is considered to
26 be within the exclusion set and hence is a offtime.
29 Number Excluded Periods:
5
34 In Exclusion Period:
20020216 -->
20020216/
20020217
40 #include
"boost/date_time/gregorian/gregorian.hpp"
45 typedef std::set
<boost::gregorian::date_period> date_period_set;
47 //Simple population of the exclusion set
51 using namespace boost::gregorian;
52 date_period periods_array[] =
53 { date_period(date(
2002,Feb,
2), date(
2002,Feb,
4)),//weekend of
2nd-
3rd
54 date_period(date(
2002,Feb,
9), date(
2002,Feb,
11)),
55 date_period(date(
2002,Feb,
16), date(
2002,Feb,
18)),
56 date_period(date(
2002,Feb,
23), date(
2002,Feb,
25)),
57 date_period(date(
2002,Feb,
12), date(
2002,Feb,
13))//a random holiday
2-
12
59 const int num_periods = sizeof(periods_array)/sizeof(date_period);
62 //insert the periods in the set
63 std::insert_iterator
<date_period_set> itr(ps, ps.begin());
64 std::copy(periods_array, periods_array+num_periods, itr );
72 using namespace boost::gregorian;
74 date_period_set ps = generateExclusion();
75 std::cout <<
"Number Excluded Periods: " << ps.size() << std::endl;
78 date_period_set::const_iterator i = ps.begin();
79 //print the periods, check for containment
80 for (;i != ps.end(); i++) {
81 std::cout << to_iso_string(*i) << std::endl;
82 //if date is in exclusion period then print it
84 std::cout <<
"In Exclusion Period: "
85 << to_iso_string(d) <<
" --> " << to_iso_string(*i)