Boost C++ Libraries

...one of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards

This is the documentation for an old version of boost. Click here for the latest Boost documentation.
PrevUpHomeNext

Class template nth_kday_of_month

boost::date_time::nth_kday_of_month — Useful generator functor for finding holidays.

Synopsis

// In header: <boost/date_time/date_generators.hpp>

template<typename date_type> 
class nth_kday_of_month :
  public boost::date_time::year_based_generator< date_type >
{
public:
  // types
  typedef date_type::calendar_type        calendar_type;   
  typedef calendar_type::day_of_week_type day_of_week_type;
  typedef calendar_type::month_type       month_type;      
  typedef calendar_type::year_type        year_type;       
  typedef date_type::duration_type        duration_type;   

  enum week_num { first = =1, second, third, fourth, fifth };

  // construct/copy/destruct
  nth_kday_of_month(week_num, day_of_week_type, month_type);

  // public member functions
  virtual date_type get_date(year_type) const;
  month_type month() const;
  week_num nth_week() const;
  day_of_week_type day_of_week() const;
  const char * nth_week_as_str() const;
  virtual std::string to_string() const;
};

Description

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 Wenesday 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.

nth_kday_of_month public construct/copy/destruct

  1. nth_kday_of_month(week_num week_no, day_of_week_type dow, month_type m);

nth_kday_of_month public member functions

  1. virtual date_type get_date(year_type y) const;
    Return a concrete date when provided with a year specific year.
  2. month_type month() const;
  3. week_num nth_week() const;
  4. day_of_week_type day_of_week() const;
  5. const char * nth_week_as_str() const;
  6. virtual std::string to_string() const;
    Returns string suitable for use in POSIX time zone string.

    Returns a string formatted as "M4.3.0" ==> 3rd Sunday in April.


PrevUpHomeNext