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"> 4 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) 8--> 9 10<section id="date_time.examples.date_period_calc"> 11 <title>Date Period Calculations</title> 12 13 <para> 14 Calculates if a date is in an 'irregular' collection of periods using period calculation functions. 15 </para> 16 <programlisting> 17 <![CDATA[ 18 /* 19 This example demonstrates a simple use of periods for the calculation 20 of date information. 21 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. 27 28 Output: 29 Number Excluded Periods: 5 30 20020202/20020203 31 20020209/20020210 32 20020212/20020212 33 20020216/20020217 34 In Exclusion Period: 20020216 --> 20020216/20020217 35 20020223/20020224 36 37 */ 38 39 40 #include "boost/date_time/gregorian/gregorian.hpp" 41 #include <set> 42 #include <algorithm> 43 #include <iostream> 44 45 typedef std::set<boost::gregorian::date_period> date_period_set; 46 47 //Simple population of the exclusion set 48 date_period_set 49 generateExclusion() 50 { 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 58 }; 59 const int num_periods = sizeof(periods_array)/sizeof(date_period); 60 61 date_period_set ps; 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 ); 65 return ps; 66 67 } 68 69 70 int main() 71 { 72 using namespace boost::gregorian; 73 74 date_period_set ps = generateExclusion(); 75 std::cout << "Number Excluded Periods: " << ps.size() << std::endl; 76 77 date d(2002,Feb,16); 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 83 if (i->contains(d)) { 84 std::cout << "In Exclusion Period: " 85 << to_iso_string(d) << " --> " << to_iso_string(*i) 86 << std::endl; 87 } 88 } 89 90 return 0; 91 92 } 93 94 ]]> 95 </programlisting> 96</section> 97