ex_date_period_calc.xml 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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.date_period_calc">
  9. <title>Date Period Calculations</title>
  10. <para>
  11. Calculates if a date is in an 'irregular' collection of periods using period calculation functions.
  12. </para>
  13. <programlisting>
  14. <![CDATA[
  15. /*
  16. This example demonstrates a simple use of periods for the calculation
  17. of date information.
  18. The example calculates if a given date is a weekend or holiday
  19. given an exclusion set. That is, each weekend or holiday is
  20. entered into the set as a time interval. Then if a given date
  21. is contained within any of the intervals it is considered to
  22. be within the exclusion set and hence is a offtime.
  23. Output:
  24. Number Excluded Periods: 5
  25. 20020202/20020203
  26. 20020209/20020210
  27. 20020212/20020212
  28. 20020216/20020217
  29. In Exclusion Period: 20020216 --> 20020216/20020217
  30. 20020223/20020224
  31. */
  32. #include "boost/date_time/gregorian/gregorian.hpp"
  33. #include <set>
  34. #include <algorithm>
  35. #include <iostream>
  36. typedef std::set<boost::gregorian::date_period> date_period_set;
  37. //Simple population of the exclusion set
  38. date_period_set
  39. generateExclusion()
  40. {
  41. using namespace boost::gregorian;
  42. date_period periods_array[] =
  43. { date_period(date(2002,Feb,2), date(2002,Feb,4)),//weekend of 2nd-3rd
  44. date_period(date(2002,Feb,9), date(2002,Feb,11)),
  45. date_period(date(2002,Feb,16), date(2002,Feb,18)),
  46. date_period(date(2002,Feb,23), date(2002,Feb,25)),
  47. date_period(date(2002,Feb,12), date(2002,Feb,13))//a random holiday 2-12
  48. };
  49. const int num_periods = sizeof(periods_array)/sizeof(date_period);
  50. date_period_set ps;
  51. //insert the periods in the set
  52. std::insert_iterator<date_period_set> itr(ps, ps.begin());
  53. std::copy(periods_array, periods_array+num_periods, itr );
  54. return ps;
  55. }
  56. int main()
  57. {
  58. using namespace boost::gregorian;
  59. date_period_set ps = generateExclusion();
  60. std::cout << "Number Excluded Periods: " << ps.size() << std::endl;
  61. date d(2002,Feb,16);
  62. date_period_set::const_iterator i = ps.begin();
  63. //print the periods, check for containment
  64. for (;i != ps.end(); i++) {
  65. std::cout << to_iso_string(*i) << std::endl;
  66. //if date is in exclusion period then print it
  67. if (i->contains(d)) {
  68. std::cout << "In Exclusion Period: "
  69. << to_iso_string(d) << " --> " << to_iso_string(*i)
  70. << std::endl;
  71. }
  72. }
  73. return 0;
  74. }
  75. ]]>
  76. </programlisting>
  77. </section>