ex_print_holidays.xml 2.7 KB

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