month_and_week_grid.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*-----------------------------------------------------------------------------+
  2. Interval Container Library
  3. Author: Joachim Faulhaber
  4. Copyright (c) 2007-2009: Joachim Faulhaber
  5. Copyright (c) 1999-2006: Cortex Software GmbH, Kantstrasse 57, Berlin
  6. +------------------------------------------------------------------------------+
  7. Distributed under the Boost Software License, Version 1.0.
  8. (See accompanying file LICENCE.txt or copy at
  9. http://www.boost.org/LICENSE_1_0.txt)
  10. +-----------------------------------------------------------------------------*/
  11. /** Example month_and_week_grid.cpp \file month_and_week_grid.cpp
  12. \brief Creating and combining time grids.
  13. A split_interval_set preserves all interval borders on insertion
  14. and intersection operations. So given a split_interval_set ...
  15. \code
  16. x = {[1, 3)}
  17. x.add( [2, 4)) then
  18. x == {[1,2)[2,3)[3,4)}
  19. \endcode
  20. ... using this property we can intersect splitting interval containers
  21. in order to iterate over intervals accounting for all changes of
  22. interval borders.
  23. In this example we provide an intersection of two split_interval_sets
  24. representing a month and week time grid.
  25. \include month_and_week_grid_/month_and_week_grid.cpp
  26. */
  27. //[example_month_and_week_grid
  28. // The next line includes <boost/gregorian/date.hpp>
  29. // and a few lines of adapter code.
  30. #include <boost/icl/gregorian.hpp>
  31. #include <iostream>
  32. #include <boost/icl/split_interval_set.hpp>
  33. using namespace std;
  34. using namespace boost::gregorian;
  35. using namespace boost::icl;
  36. typedef split_interval_set<boost::gregorian::date> date_grid;
  37. // This function splits a gregorian::date interval 'scope' into a month grid:
  38. // For every month contained in 'scope' that month is contained as interval
  39. // in the resulting split_interval_set.
  40. date_grid month_grid(const discrete_interval<date>& scope)
  41. {
  42. split_interval_set<date> month_grid;
  43. date frame_months_1st = first(scope).end_of_month() + days(1) - months(1);
  44. month_iterator month_iter(frame_months_1st);
  45. for(; month_iter <= last(scope); ++month_iter)
  46. month_grid += discrete_interval<date>::right_open(*month_iter, *month_iter + months(1));
  47. month_grid &= scope; // cut off the surplus
  48. return month_grid;
  49. }
  50. // This function splits a gregorian::date interval 'scope' into a week grid:
  51. // For every week contained in 'scope' that month is contained as interval
  52. // in the resulting split_interval_set.
  53. date_grid week_grid(const discrete_interval<date>& scope)
  54. {
  55. split_interval_set<date> week_grid;
  56. date frame_weeks_1st = first(scope) + days(days_until_weekday(first(scope), greg_weekday(Monday))) - weeks(1);
  57. week_iterator week_iter(frame_weeks_1st);
  58. for(; week_iter <= last(scope); ++week_iter)
  59. week_grid.insert(discrete_interval<date>::right_open(*week_iter, *week_iter + weeks(1)));
  60. week_grid &= scope; // cut off the surplus
  61. return week_grid;
  62. }
  63. // For a period of two months, starting from today, the function
  64. // computes a partitioning for months and weeks using intersection
  65. // operator &= on split_interval_sets.
  66. void month_and_time_grid()
  67. {
  68. date someday = day_clock::local_day();
  69. date thenday = someday + months(2);
  70. discrete_interval<date> itv = discrete_interval<date>::right_open(someday, thenday);
  71. // Compute a month grid
  72. date_grid month_and_week_grid = month_grid(itv);
  73. // Intersection of the month and week grids:
  74. month_and_week_grid &= week_grid(itv);
  75. cout << "interval : " << first(itv) << " - " << last(itv)
  76. << " month and week partitions:" << endl;
  77. cout << "---------------------------------------------------------------\n";
  78. for(date_grid::iterator it = month_and_week_grid.begin();
  79. it != month_and_week_grid.end(); it++)
  80. {
  81. if(first(*it).day() == 1)
  82. cout << "new month: ";
  83. else if(first(*it).day_of_week()==greg_weekday(Monday))
  84. cout << "new week : " ;
  85. else if(it == month_and_week_grid.begin())
  86. cout << "first day: " ;
  87. cout << first(*it) << " - " << last(*it) << endl;
  88. }
  89. }
  90. int main()
  91. {
  92. cout << ">>Interval Container Library: Sample month_and_time_grid.cpp <<\n";
  93. cout << "---------------------------------------------------------------\n";
  94. month_and_time_grid();
  95. return 0;
  96. }
  97. // Program output:
  98. /*
  99. >>Interval Container Library: Sample month_and_time_grid.cpp <<
  100. ---------------------------------------------------------------
  101. interval : 2008-Jun-22 - 2008-Aug-21 month and week partitions:
  102. ---------------------------------------------------------------
  103. first day: 2008-Jun-22 - 2008-Jun-22
  104. new week : 2008-Jun-23 - 2008-Jun-29
  105. new week : 2008-Jun-30 - 2008-Jun-30
  106. new month: 2008-Jul-01 - 2008-Jul-06
  107. new week : 2008-Jul-07 - 2008-Jul-13
  108. new week : 2008-Jul-14 - 2008-Jul-20
  109. new week : 2008-Jul-21 - 2008-Jul-27
  110. new week : 2008-Jul-28 - 2008-Jul-31
  111. new month: 2008-Aug-01 - 2008-Aug-03
  112. new week : 2008-Aug-04 - 2008-Aug-10
  113. new week : 2008-Aug-11 - 2008-Aug-17
  114. new week : 2008-Aug-18 - 2008-Aug-21
  115. */
  116. //]