123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- #ifndef _DATE_TIME_ADJUST_FUNCTORS_HPP___
- #define _DATE_TIME_ADJUST_FUNCTORS_HPP___
- #include "boost/date_time/date.hpp"
- #include "boost/date_time/wrapping_int.hpp"
- namespace boost {
- namespace date_time {
-
- template<class date_type>
- class day_functor
- {
- public:
- typedef typename date_type::duration_type duration_type;
- day_functor(int f) : f_(f) {}
- duration_type get_offset(const date_type&) const
- {
- return duration_type(f_);
- }
- duration_type get_neg_offset(const date_type&) const
- {
- return duration_type(-f_);
- }
- private:
- int f_;
- };
-
-
- template<class date_type>
- class month_functor
- {
- public:
- typedef typename date_type::duration_type duration_type;
- typedef typename date_type::calendar_type cal_type;
- typedef typename cal_type::ymd_type ymd_type;
- typedef typename cal_type::day_type day_type;
- month_functor(int f) : f_(f), origDayOfMonth_(0) {}
- duration_type get_offset(const date_type& d) const
- {
- ymd_type ymd(d.year_month_day());
- if (origDayOfMonth_ == 0) {
- origDayOfMonth_ = ymd.day;
- day_type endOfMonthDay(cal_type::end_of_month_day(ymd.year,ymd.month));
- if (endOfMonthDay == ymd.day) {
- origDayOfMonth_ = -1;
- }
- }
- typedef date_time::wrapping_int2<short,1,12> wrap_int2;
- wrap_int2 wi(ymd.month);
-
- const typename ymd_type::year_type year(static_cast<typename ymd_type::year_type::value_type>(ymd.year + wi.add(f_)));
-
- day_type resultingEndOfMonthDay(cal_type::end_of_month_day(year, wi.as_int()));
-
- if (origDayOfMonth_ == -1) {
- return date_type(year, wi.as_int(), resultingEndOfMonthDay) - d;
- }
- day_type dayOfMonth = origDayOfMonth_;
- if (dayOfMonth > resultingEndOfMonthDay) {
- dayOfMonth = resultingEndOfMonthDay;
- }
- return date_type(year, wi.as_int(), dayOfMonth) - d;
- }
-
- duration_type get_neg_offset(const date_type& d) const
- {
- ymd_type ymd(d.year_month_day());
- if (origDayOfMonth_ == 0) {
- origDayOfMonth_ = ymd.day;
- day_type endOfMonthDay(cal_type::end_of_month_day(ymd.year,ymd.month));
- if (endOfMonthDay == ymd.day) {
- origDayOfMonth_ = -1;
- }
- }
- typedef date_time::wrapping_int2<short,1,12> wrap_int2;
- wrap_int2 wi(ymd.month);
-
- const typename ymd_type::year_type year(static_cast<typename ymd_type::year_type::value_type>(ymd.year + wi.subtract(f_)));
-
- day_type resultingEndOfMonthDay(cal_type::end_of_month_day(year, wi.as_int()));
-
- if (origDayOfMonth_ == -1) {
- return date_type(year, wi.as_int(), resultingEndOfMonthDay) - d;
- }
- day_type dayOfMonth = origDayOfMonth_;
- if (dayOfMonth > resultingEndOfMonthDay) {
- dayOfMonth = resultingEndOfMonthDay;
- }
- return date_type(year, wi.as_int(), dayOfMonth) - d;
- }
- private:
- int f_;
- mutable short origDayOfMonth_;
- };
-
- template<class date_type>
- class week_functor
- {
- public:
- typedef typename date_type::duration_type duration_type;
- typedef typename date_type::calendar_type calendar_type;
- week_functor(int f) : f_(f) {}
- duration_type get_offset(const date_type&) const
- {
- return duration_type(f_*static_cast<int>(calendar_type::days_in_week()));
- }
- duration_type get_neg_offset(const date_type&) const
- {
- return duration_type(-f_*static_cast<int>(calendar_type::days_in_week()));
- }
- private:
- int f_;
- };
-
- template<class date_type>
- class year_functor
- {
- public:
-
- typedef typename date_type::duration_type duration_type;
- year_functor(int f) : _mf(f * 12) {}
- duration_type get_offset(const date_type& d) const
- {
- return _mf.get_offset(d);
- }
- duration_type get_neg_offset(const date_type& d) const
- {
- return _mf.get_neg_offset(d);
- }
- private:
- month_functor<date_type> _mf;
- };
- } }
- #endif
|