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.print_holidays">
11  <title>Print Holidays</title>
12
13  <para>
14    This is an example of using functors to define a holiday schedule
15  </para>
16  <programlisting>
17    <![CDATA[
18
19  /* Generate a set of dates using a collection of date generators
20   * Output looks like:
21   * Enter Year: 2002
22   * 2002-Jan-01 [Tue]
23   * 2002-Jan-21 [Mon]
24   * 2002-Feb-12 [Tue]
25   * 2002-Jul-04 [Thu]
26   * 2002-Sep-02 [Mon]
27   * 2002-Nov-28 [Thu]
28   * 2002-Dec-25 [Wed]
29   * Number Holidays: 7
30   */
31
32  #include "boost/date_time/gregorian/gregorian.hpp"
33  #include <algorithm>
34  #include <functional>
35  #include <vector>
36  #include <iostream>
37  #include <set>
38
39  void
40  print_date(boost::gregorian::date d)
41  {
42    using namespace boost::gregorian;
43  #if defined(BOOST_DATE_TIME_NO_LOCALE)
44    std::cout << to_simple_string(d) << " [" << d.day_of_week() << "]\n";
45  #else
46    std::cout << d << " [" << d.day_of_week() << "]\n";
47  #endif
48  }
49
50
51  int
52  main() {
53
54    std::cout << "Enter Year: ";
55    int year;
56    std::cin >> year;
57
58    using namespace boost::gregorian;
59
60    //define a collection of holidays fixed by month and day
61    std::vector<year_based_generator*> holidays;
62    holidays.push_back(new partial_date(1,Jan)); //Western New Year
63    holidays.push_back(new partial_date(4,Jul)); //US Independence Day
64    holidays.push_back(new partial_date(25, Dec));//Christmas day
65
66
67    //define a shorthand for the nth_day_of_the_week_in_month function object
68    typedef nth_day_of_the_week_in_month nth_dow;
69
70    //US labor day
71    holidays.push_back(new nth_dow(nth_dow::first,  Monday,   Sep));
72    //MLK Day
73    holidays.push_back(new nth_dow(nth_dow::third,  Monday,   Jan));
74    //Pres day
75    holidays.push_back(new nth_dow(nth_dow::second, Tuesday,  Feb));
76    //Thanksgiving
77    holidays.push_back(new nth_dow(nth_dow::fourth, Thursday, Nov));
78
79    typedef std::set<date> date_set;
80    date_set all_holidays;
81
82    for(std::vector<year_based_generator*>::iterator it = holidays.begin();
83        it != holidays.end(); ++it)
84    {
85      all_holidays.insert((*it)->get_date(year));
86    }
87
88    //print the holidays to the screen
89    std::for_each(all_holidays.begin(), all_holidays.end(), print_date);
90    std::cout << "Number Holidays: " << all_holidays.size() << std::endl;
91
92    return 0;
93  }
94
95    ]]>
96  </programlisting>
97</section>
98