diff --git a/cpp/meetup/meetup.cpp b/cpp/meetup/meetup.cpp index 29c3abc..f300c7d 100644 --- a/cpp/meetup/meetup.cpp +++ b/cpp/meetup/meetup.cpp @@ -1,20 +1,266 @@ #include "meetup.h" + namespace meetup { -using namespace boost::gregorian; -typedef nth_day_of_the_week_in_month nth_dow; +scheduler::scheduler(month_t month, year_t year) + : year_(year) + , month_(month) +{} -scheduler::scheduler(int month, int year) - : month_(month) - , year_(year) -{ -} date_t scheduler::monteenth() const { - return nth_dow(/*nth_dow::first*/nth_dow::second, Monday, month_).get_date(year_); + return teenth_day(weekdays::Monday); } +date_t scheduler::tuesteenth() const +{ + return teenth_day(weekdays::Tuesday); +} + +date_t scheduler::wednesteenth() const +{ + return teenth_day(weekdays::Wednesday); +} + +date_t scheduler::thursteenth() const +{ + return teenth_day(weekdays::Thursday); +} + +date_t scheduler::friteenth() const +{ + return teenth_day(weekdays::Friday); +} + +date_t scheduler::saturteenth() const +{ + return teenth_day(weekdays::Saturday); +} + +date_t scheduler::sunteenth() const +{ + return teenth_day(weekdays::Sunday); +} + +date_t scheduler::first_monday() const +{ + return first_weekday(weekdays::Monday); +} + +date_t scheduler::first_tuesday() const +{ + return first_weekday(weekdays::Tuesday); +} + +date_t scheduler::first_wednesday() const +{ + return first_weekday(weekdays::Wednesday); +} + +date_t scheduler::first_thursday() const +{ + return first_weekday(weekdays::Thursday); +} + +date_t scheduler::first_friday() const +{ + return first_weekday(weekdays::Friday); +} + +date_t scheduler::first_saturday() const +{ + return first_weekday(weekdays::Saturday); +} + +date_t scheduler::first_sunday() const +{ + return first_weekday(weekdays::Sunday); +} + +date_t scheduler::second_monday() const +{ + return second_weekday(weekdays::Monday); +} + +date_t scheduler::second_tuesday() const +{ + return second_weekday(weekdays::Tuesday); +} + +date_t scheduler::second_wednesday() const +{ + return second_weekday(weekdays::Wednesday); +} + +date_t scheduler::second_thursday() const +{ + return second_weekday(weekdays::Thursday); +} + +date_t scheduler::second_friday() const +{ + return second_weekday(weekdays::Friday); +} + +date_t scheduler::second_saturday() const +{ + return second_weekday(weekdays::Saturday); +} + +date_t scheduler::second_sunday() const +{ + return second_weekday(weekdays::Sunday); +} + +date_t scheduler::third_monday() const +{ + return third_weekday(weekdays::Monday); +} + +date_t scheduler::third_tuesday() const +{ + return third_weekday(weekdays::Tuesday); +} + +date_t scheduler::third_wednesday() const +{ + return third_weekday(weekdays::Wednesday); +} + +date_t scheduler::third_thursday() const +{ + return third_weekday(weekdays::Thursday); +} + +date_t scheduler::third_friday() const +{ + return third_weekday(weekdays::Friday); +} + +date_t scheduler::third_saturday() const +{ + return third_weekday(weekdays::Saturday); +} + +date_t scheduler::third_sunday() const +{ + return third_weekday(weekdays::Sunday); +} + +date_t scheduler::fourth_monday() const +{ + return fourth_weekday(weekdays::Monday); +} + +date_t scheduler::fourth_tuesday() const +{ + return fourth_weekday(weekdays::Tuesday); +} + +date_t scheduler::fourth_wednesday() const +{ + return fourth_weekday(weekdays::Wednesday); +} + +date_t scheduler::fourth_thursday() const +{ + return fourth_weekday(weekdays::Thursday); +} + +date_t scheduler::fourth_friday() const +{ + return fourth_weekday(weekdays::Friday); +} + +date_t scheduler::fourth_saturday() const +{ + return fourth_weekday(weekdays::Saturday); +} + +date_t scheduler::fourth_sunday() const +{ + return fourth_weekday(weekdays::Sunday); +} + +date_t scheduler::last_monday() const +{ + return last_weekday(weekdays::Monday); +} + +date_t scheduler::last_tuesday() const +{ + return last_weekday(weekdays::Tuesday); +} + +date_t scheduler::last_wednesday() const +{ + return last_weekday(weekdays::Wednesday); +} + +date_t scheduler::last_thursday() const +{ + return last_weekday(weekdays::Thursday); +} + +date_t scheduler::last_friday() const +{ + return last_weekday(weekdays::Friday); +} + +date_t scheduler::last_saturday() const +{ + return last_weekday(weekdays::Saturday); +} + +date_t scheduler::last_sunday() const +{ + return last_weekday(weekdays::Sunday); +} + +namespace { + +typedef boost::gregorian::first_day_of_the_week_in_month first_dow; +typedef boost::gregorian::first_day_of_the_week_after first_dow_after; +typedef boost::gregorian::last_day_of_the_week_in_month last_dow; +} + +date_t scheduler::teenth_day(weekdays day) const +{ + return first_dow_after(day).get_date({year_, month_, 12}); +} + +date_t scheduler::nth_weekday(nth_dow::week_num n, weekdays day) const +{ + return nth_dow(n, day, month_).get_date(year_); +} + +date_t scheduler::first_weekday(weekdays day) const +{ + return first_dow(day, month_).get_date(year_); +} + +date_t scheduler::last_weekday(weekdays day) const +{ + return last_dow(day, month_).get_date(year_); +} + +date_t scheduler::second_weekday(weekdays day) const +{ + return nth_weekday(nth_dow::second, day); +} + +date_t scheduler::third_weekday(weekdays day) const +{ + return nth_weekday(nth_dow::third, day); +} + +date_t scheduler::fourth_weekday(weekdays day) const +{ + return nth_weekday(nth_dow::fourth, day); +} + + } diff --git a/cpp/meetup/meetup.h b/cpp/meetup/meetup.h index d766e28..0820fab 100644 --- a/cpp/meetup/meetup.h +++ b/cpp/meetup/meetup.h @@ -1,21 +1,89 @@ #pragma once +#define EXERCISM_RUN_ALL_TESTS + #include namespace meetup { typedef boost::gregorian::date date_t; +typedef date_t::month_type month_t; +typedef date_t::year_type year_t; class scheduler { - int month_, year_; - public: - scheduler(int month, int year); - date_t monteenth() const; + scheduler(month_t month, year_t year); + + + date_t monteenth() const; + date_t tuesteenth() const; + date_t wednesteenth() const; + date_t thursteenth() const; + date_t friteenth() const; + date_t saturteenth() const; + date_t sunteenth() const; + + date_t first_monday() const; + date_t first_tuesday() const; + date_t first_wednesday() const; + date_t first_thursday() const; + date_t first_friday() const; + date_t first_saturday() const; + date_t first_sunday() const; + + date_t second_monday() const; + date_t second_tuesday() const; + date_t second_wednesday() const; + date_t second_thursday() const; + date_t second_friday() const; + date_t second_saturday() const; + date_t second_sunday() const; + + date_t third_monday() const; + date_t third_tuesday() const; + date_t third_wednesday() const; + date_t third_thursday() const; + date_t third_friday() const; + date_t third_saturday() const; + date_t third_sunday() const; + + date_t fourth_monday() const; + date_t fourth_tuesday() const; + date_t fourth_wednesday() const; + date_t fourth_thursday() const; + date_t fourth_friday() const; + date_t fourth_saturday() const; + date_t fourth_sunday() const; + + date_t last_monday() const; + date_t last_tuesday() const; + date_t last_wednesday() const; + date_t last_thursday() const; + date_t last_friday() const; + date_t last_saturday() const; + date_t last_sunday() const; + +private: + + typedef boost::date_time::weekdays weekdays; + typedef boost::gregorian::nth_day_of_the_week_in_month nth_dow; + typedef nth_dow::week_num week_num; + + date_t teenth_day(weekdays day) const; + + date_t nth_weekday(week_num n, weekdays day) const; + + date_t first_weekday(weekdays day) const; + date_t second_weekday(weekdays day) const; + date_t third_weekday(weekdays day) const; + date_t fourth_weekday(weekdays day) const; + date_t last_weekday(weekdays day) const; + + const year_t year_; + const month_t month_; }; - }