![]() |
Home | Libraries | People | FAQ | More |
boost::date_time::nth_kday_of_month — Useful generator functor for finding holidays.
// In header: <boost/date_time/date_generators.hpp> template<typename date_type> class nth_kday_of_month : public { public: // types typedef ; typedef ; typedef ; typedef ; typedef ; enum week_num { first = =1, second, third, fourth, fifth }; // construct/copy/destruct (, , ); // public member functions () ; () ; () ; () ; const char * () ; () ; };
Based on the idea in Cal. Calc. for finding holidays that are the 'first Monday of September'. When instantiated with 'fifth' kday of month, the result will be the last kday of month which can be the fourth or fifth depending on the structure of the month.
The algorithm here basically guesses for the first day of the month. Then finds the first day of the correct type. That is, if the first of the month is a Tuesday and it needs Wednesday then we simply increment by a day and then we can add the length of a week until we get to the 'nth kday'. There are probably more efficient algorithms based on using a mod 7, but this one works reasonably well for basic applications.