123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509 |
- #ifndef DATE_TIME_DATE_GENERATORS_HPP__
- #define DATE_TIME_DATE_GENERATORS_HPP__
- #include <stdexcept>
- #include <sstream>
- #include <boost/throw_exception.hpp>
- #include <boost/date_time/date.hpp>
- #include <boost/date_time/compiler_config.hpp>
- namespace boost {
- namespace date_time {
-
-
- template<class date_type>
- class year_based_generator
- {
- public:
- typedef typename date_type::calendar_type calendar_type;
- typedef typename calendar_type::year_type year_type;
- year_based_generator() {};
- virtual ~year_based_generator() {};
- virtual date_type get_date(year_type y) const = 0;
-
- virtual std::string to_string() const =0;
- };
-
-
-
- template<class date_type>
- class partial_date : public year_based_generator<date_type>
- {
- public:
- typedef typename date_type::calendar_type calendar_type;
- typedef typename calendar_type::day_type day_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;
- typedef typename duration_type::duration_rep duration_rep;
- partial_date(day_type d, month_type m) :
- day_(d),
- month_(m)
- {}
-
-
- partial_date(duration_rep days) :
- day_(1),
- month_(1)
- {
- date_type d1(2000,1,1);
- if(days > 1) {
- if(days > 366)
- {
- days = 366;
- }
- days = days - 1;
- duration_type dd(days);
- d1 = d1 + dd;
- }
- day_ = d1.day();
- month_ = d1.month();
- }
-
-
- date_type get_date(year_type y) const
- {
- if((day_ == 29) && (month_ == 2) && !(calendar_type::is_leap_year(y))) {
- std::ostringstream ss;
- ss << "No Feb 29th in given year of " << y << ".";
- boost::throw_exception(std::invalid_argument(ss.str()));
- }
- return date_type(y, month_, day_);
- }
- date_type operator()(year_type y) const
- {
- return get_date(y);
-
- }
- 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;
-
- return (day_ < rhs.day_);
- }
-
-
- month_type month() const
- {
- return month_;
- }
- day_type day() const
- {
- return day_;
- }
-
-
- virtual std::string to_string() const
- {
- std::ostringstream ss;
- date_type d(2004, month_, day_);
- unsigned short c = d.day_of_year();
- c--;
- ss << c;
- return ss.str();
- }
- private:
- day_type day_;
- month_type month_;
- };
-
- BOOST_DATE_TIME_DECL const char* nth_as_str(int n);
-
-
- 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)
- {}
-
- date_type get_date(year_type y) const
- {
- date_type d(y, month_, 1);
- 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++;
- }
-
- if(d.month() != month_) {
- d = d - one_week;
- }
- return d;
- }
-
- 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
- {
- return nth_as_str(wn_);
- }
-
-
- virtual std::string to_string() const
- {
- std::ostringstream ss;
- ss << 'M'
- << static_cast<int>(month_) << '.'
- << static_cast<int>(wn_) << '.'
- << static_cast<int>(dow_);
- return ss.str();
- }
- private:
- month_type month_;
- week_num wn_;
- day_of_week_type dow_;
- };
-
-
-
- template<class date_type>
- class first_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;
-
-
- first_kday_of_month(day_of_week_type dow, month_type m) :
- month_(m),
- dow_(dow)
- {}
-
- date_type get_date(year_type year) const
- {
- date_type d(year, month_,1);
- duration_type one_day(1);
- while (dow_ != d.day_of_week()) {
- d = d + one_day;
- }
- return d;
- }
-
- month_type month() const
- {
- return month_;
- }
- day_of_week_type day_of_week() const
- {
- return dow_;
- }
-
-
- virtual std::string to_string() const
- {
- std::ostringstream ss;
- ss << 'M'
- << static_cast<int>(month_) << '.'
- << 1 << '.'
- << static_cast<int>(dow_);
- return ss.str();
- }
- private:
- month_type month_;
- day_of_week_type dow_;
- };
-
-
-
-
-
- template<class date_type>
- class last_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;
-
-
- last_kday_of_month(day_of_week_type dow, month_type m) :
- month_(m),
- dow_(dow)
- {}
-
- date_type get_date(year_type year) const
- {
- date_type d(year, month_, calendar_type::end_of_month_day(year,month_));
- duration_type one_day(1);
- while (dow_ != d.day_of_week()) {
- d = d - one_day;
- }
- return d;
- }
-
- month_type month() const
- {
- return month_;
- }
- day_of_week_type day_of_week() const
- {
- return dow_;
- }
-
-
- virtual std::string to_string() const
- {
- std::ostringstream ss;
- ss << 'M'
- << static_cast<int>(month_) << '.'
- << 5 << '.'
- << static_cast<int>(dow_);
- return ss.str();
- }
- private:
- month_type month_;
- day_of_week_type dow_;
- };
-
-
-
-
- template<class date_type>
- class first_kday_after
- {
- public:
- typedef typename date_type::calendar_type calendar_type;
- typedef typename calendar_type::day_of_week_type day_of_week_type;
- typedef typename date_type::duration_type duration_type;
- first_kday_after(day_of_week_type dow) :
- dow_(dow)
- {}
-
- date_type get_date(date_type start_day) const
- {
- duration_type one_day(1);
- date_type d = start_day + one_day;
- while (dow_ != d.day_of_week()) {
- d = d + one_day;
- }
- return d;
- }
-
- day_of_week_type day_of_week() const
- {
- return dow_;
- }
- private:
- day_of_week_type dow_;
- };
-
-
-
- template<class date_type>
- class first_kday_before
- {
- public:
- typedef typename date_type::calendar_type calendar_type;
- typedef typename calendar_type::day_of_week_type day_of_week_type;
- typedef typename date_type::duration_type duration_type;
- first_kday_before(day_of_week_type dow) :
- dow_(dow)
- {}
-
- date_type get_date(date_type start_day) const
- {
- duration_type one_day(1);
- date_type d = start_day - one_day;
- while (dow_ != d.day_of_week()) {
- d = d - one_day;
- }
- return d;
- }
-
- day_of_week_type day_of_week() const
- {
- return dow_;
- }
- private:
- day_of_week_type dow_;
- };
-
-
-
- template<typename date_type, class weekday_type>
- inline
- typename date_type::duration_type days_until_weekday(const date_type& d, const weekday_type& wd)
- {
- typedef typename date_type::duration_type duration_type;
- duration_type wks(0);
- duration_type dd(wd.as_number() - d.day_of_week().as_number());
- if(dd.is_negative()){
- wks = duration_type(7);
- }
- return dd + wks;
- }
-
-
- template<typename date_type, class weekday_type>
- inline
- typename date_type::duration_type days_before_weekday(const date_type& d, const weekday_type& wd)
- {
- typedef typename date_type::duration_type duration_type;
- duration_type wks(0);
- duration_type dd(wd.as_number() - d.day_of_week().as_number());
- if(dd.days() > 0){
- wks = duration_type(7);
- }
-
-
- return (-dd + wks);
- }
-
-
- template<class date_type, class weekday_type>
- inline
- date_type next_weekday(const date_type& d, const weekday_type& wd)
- {
- return d + days_until_weekday(d, wd);
- }
-
-
- template<class date_type, class weekday_type>
- inline
- date_type previous_weekday(const date_type& d, const weekday_type& wd)
- {
- return d - days_before_weekday(d, wd);
- }
- } }
- #endif
|