print_holidays.cpp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. #include "boost/date_time/gregorian/gregorian.hpp"
  14. #include <algorithm>
  15. #include <functional>
  16. #include <vector>
  17. #include <iostream>
  18. #include <set>
  19. void
  20. print_date(boost::gregorian::date d)
  21. {
  22. using namespace boost::gregorian;
  23. #if defined(BOOST_DATE_TIME_NO_LOCALE)
  24. std::cout << to_simple_string(d) << " [" << d.day_of_week() << "]\n";
  25. #else
  26. std::cout << d << " [" << d.day_of_week() << "]\n";
  27. #endif
  28. }
  29. int
  30. main() {
  31. using namespace boost::gregorian;
  32. std::cout << "Enter Year: ";
  33. greg_year::value_type year;
  34. std::cin >> year;
  35. //define a collection of holidays fixed by month and day
  36. std::vector<year_based_generator*> holidays;
  37. holidays.push_back(new partial_date(1,Jan)); //Western New Year
  38. holidays.push_back(new partial_date(4,Jul)); //US Independence Day
  39. holidays.push_back(new partial_date(25, Dec));//Christmas day
  40. //define a shorthand for the nth_day_of_the_week_in_month function object
  41. typedef nth_day_of_the_week_in_month nth_dow;
  42. //US labor day
  43. holidays.push_back(new nth_dow(nth_dow::first, Monday, Sep));
  44. //MLK Day
  45. holidays.push_back(new nth_dow(nth_dow::third, Monday, Jan));
  46. //Pres day
  47. holidays.push_back(new nth_dow(nth_dow::second, Tuesday, Feb));
  48. //Thanksgiving
  49. holidays.push_back(new nth_dow(nth_dow::fourth, Thursday, Nov));
  50. typedef std::set<date> date_set;
  51. date_set all_holidays;
  52. for(std::vector<year_based_generator*>::iterator it = holidays.begin();
  53. it != holidays.end(); ++it)
  54. {
  55. all_holidays.insert((*it)->get_date(year));
  56. }
  57. //print the holidays to the screen
  58. std::for_each(all_holidays.begin(), all_holidays.end(), print_date);
  59. std::cout << "Number Holidays: " << all_holidays.size() << std::endl;
  60. return 0;
  61. }
  62. /* Copyright 2001-2004: CrystalClear Software, Inc
  63. * http://www.crystalclearsoftware.com
  64. *
  65. * Subject to the Boost Software License, Version 1.0.
  66. * (See accompanying file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
  67. */