• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Generate a set of dates using a collection of date generators
2  * Output looks like:
3  * Enter Year: 2002
4  * 2002-Jan-01 [Tue]
5  * 2002-Jan-21 [Mon]
6  * 2002-Feb-12 [Tue]
7  * 2002-Jul-04 [Thu]
8  * 2002-Sep-02 [Mon]
9  * 2002-Nov-28 [Thu]
10  * 2002-Dec-25 [Wed]
11  * Number Holidays: 7
12  */
13 
14 #include "boost/date_time/gregorian/gregorian.hpp"
15 #include <algorithm>
16 #include <functional>
17 #include <vector>
18 #include <iostream>
19 #include <set>
20 
21 void
print_date(boost::gregorian::date d)22 print_date(boost::gregorian::date d)
23 {
24   using namespace boost::gregorian;
25 #if defined(BOOST_DATE_TIME_NO_LOCALE)
26   std::cout << to_simple_string(d) << " [" << d.day_of_week() << "]\n";
27 #else
28   std::cout << d << " [" << d.day_of_week() << "]\n";
29 #endif
30 }
31 
32 
33 int
main()34 main() {
35 
36   using namespace boost::gregorian;
37 
38   std::cout << "Enter Year: ";
39   greg_year::value_type year;
40   std::cin >> year;
41 
42   //define a collection of holidays fixed by month and day
43   std::vector<year_based_generator*> holidays;
44   holidays.push_back(new partial_date(1,Jan)); //Western New Year
45   holidays.push_back(new partial_date(4,Jul)); //US Independence Day
46   holidays.push_back(new partial_date(25, Dec));//Christmas day
47 
48 
49   //define a shorthand for the nth_day_of_the_week_in_month function object
50   typedef nth_day_of_the_week_in_month nth_dow;
51 
52   //US labor day
53   holidays.push_back(new nth_dow(nth_dow::first,  Monday,   Sep));
54   //MLK Day
55   holidays.push_back(new nth_dow(nth_dow::third,  Monday,   Jan));
56   //Pres day
57   holidays.push_back(new nth_dow(nth_dow::second, Tuesday,  Feb));
58   //Thanksgiving
59   holidays.push_back(new nth_dow(nth_dow::fourth, Thursday, Nov));
60 
61   typedef std::set<date> date_set;
62   date_set all_holidays;
63 
64   for(std::vector<year_based_generator*>::iterator it = holidays.begin();
65       it != holidays.end(); ++it)
66   {
67     all_holidays.insert((*it)->get_date(year));
68   }
69 
70   //print the holidays to the screen
71   std::for_each(all_holidays.begin(), all_holidays.end(), print_date);
72   std::cout << "Number Holidays: " << all_holidays.size() << std::endl;
73 
74   return 0;
75 }
76 
77 /*  Copyright 2001-2004: CrystalClear Software, Inc
78  *  http://www.crystalclearsoftware.com
79  *
80  *  Subject to the Boost Software License, Version 1.0.
81  * (See accompanying file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
82  */
83 
84