Boost-Graph
view release on metacpan or search on metacpan
include/boost/date_time/date_generators.hpp view on Meta::CPAN
throw std::invalid_argument(ss.str());
//return date_type(1,1,1); // should never reach
} else {
return date_type(y, month_, day_);
}
}
date_type operator()(year_type y) const
{
return get_date(y);
//return date_type(y, month_, day_);
}
bool operator==(const partial_date& rhs) const
{
return (month_ == rhs.month_) && (day_ == rhs.day_);
}
bool operator<(const partial_date& rhs) const
{
if (month_ < rhs.month_) return true;
if (month_ > rhs.month_) return false;
//months are equal
return (day_ < rhs.day_);
}
// added for streaming purposes
month_type month() const
{
return month_;
}
day_type day() const
{
return day_;
}
//! Returns string suitable for use in POSIX time zone string
/*! Returns string formatted with up to 3 digits:
* Jan-01 == "0"
* Feb-29 == "58"
* Dec-31 == "365" */
virtual std::string to_string() const
{
std::stringstream ss;
date_type d(2004, month_, day_);
unsigned short c = d.day_of_year();
c--; // numbered 0-365 while day_of_year is 1 based...
ss << c;
return ss.str();
}
private:
day_type day_;
month_type month_;
};
//! Useful generator functor for finding holidays
/*! 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.
* \ingroup date_alg
*/
template<class date_type>
class nth_kday_of_month : public year_based_generator<date_type>
{
public:
typedef typename date_type::calendar_type calendar_type;
typedef typename calendar_type::day_of_week_type day_of_week_type;
typedef typename calendar_type::month_type month_type;
typedef typename calendar_type::year_type year_type;
typedef typename date_type::duration_type duration_type;
enum week_num {first=1, second, third, fourth, fifth};
nth_kday_of_month(week_num week_no,
day_of_week_type dow,
month_type m) :
month_(m),
wn_(week_no),
dow_(dow)
{}
//! Return a concrete date when provided with a year specific year.
date_type get_date(year_type y) const
{
date_type d(y, month_, 1); //first day of month
duration_type one_day(1);
duration_type one_week(7);
while (dow_ != d.day_of_week()) {
d = d + one_day;
}
int week = 1;
while (week < wn_) {
d = d + one_week;
week++;
}
// remove wrapping to next month behavior
if(d.month() != month_) {
d = d - one_week;
}
return d;
}
// added for streaming
month_type month() const
{
return month_;
}
week_num nth_week() const
{
return wn_;
}
day_of_week_type day_of_week() const
{
return dow_;
}
const char* nth_week_as_str() const
( run in 0.696 second using v1.01-cache-2.11-cpan-39bf76dae61 )