Advertisement
Guest User

Untitled

a guest
Aug 30th, 2015
506
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.90 KB | None | 0 0
  1. #include <boost/date_time/gregorian/gregorian.hpp>
  2. #include <boost/format.hpp>
  3.  
  4. using namespace boost::gregorian;
  5.  
  6. int getMonthWeek(const date& d)     //here sunday is first week day
  7. {
  8.     auto getYearWeek = [](const date& d){return d.week_number()+(d.day_of_week()==Sunday?1:0);};
  9.     return getYearWeek(d) - getYearWeek(date{d.year(), d.month(), 1});
  10. }
  11.  
  12. int main() {
  13.     const int days_per_week = 7;
  14.     const int months_per_year = 12;
  15.     const int weeks_per_block = 6;
  16.     const int months_per_block = 3;
  17.     const int blocks_per_year = ceil( double( months_per_year ) / months_per_block );
  18.     const greg_year year = 2015;
  19.  
  20.     for(auto yearblock = 0; yearblock != blocks_per_year; yearblock++) {
  21.         int monthweek2blockmonth2weekday2monthday [weeks_per_block] [months_per_block] [days_per_week] = {};
  22.  
  23.         for(auto blockmonth = 0; blockmonth != months_per_block; blockmonth++) {
  24.             const int yearmonth = yearblock * months_per_block + blockmonth;
  25.  
  26.             if(yearmonth < months_per_year) {
  27.                 const greg_month gregmonth = yearmonth + 1;
  28.  
  29.                 std::cout << boost::str(boost::format("%|=22|") % gregmonth.as_long_string());
  30.  
  31.                 for(date d{ year, gregmonth, 1 }, d_last = d.end_of_month(); d <= d_last; d += date_duration(1))
  32.                     monthweek2blockmonth2weekday2monthday [getMonthWeek(d)] [blockmonth] [d.day_of_week()] = d.day();
  33.             }
  34.         }
  35.         std::cout << std::endl;
  36.  
  37.         for(const auto& blockmonth2weekday2monthday: monthweek2blockmonth2weekday2monthday) {
  38.             for(const auto& weekday2monthday: blockmonth2weekday2monthday) {
  39.                 for(const auto& monthday: weekday2monthday)
  40.                     std::cout << ( monthday ? boost::str(boost::format("%|3|") % monthday) : "   " );
  41.                 std::cout << " ";
  42.             }
  43.             std::cout << std::endl;
  44.         }
  45.     }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement