Chris@16: #ifndef _DATE_TIME_ADJUST_FUNCTORS_HPP___ Chris@16: #define _DATE_TIME_ADJUST_FUNCTORS_HPP___ Chris@16: Chris@16: /* Copyright (c) 2002,2003 CrystalClear Software, Inc. Chris@16: * Use, modification and distribution is subject to the Chris@16: * Boost Software License, Version 1.0. (See accompanying Chris@16: * file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt) Chris@16: * Author: Jeff Garland, Bart Garst Chris@101: * $Date$ Chris@16: */ Chris@16: Chris@16: #include "boost/date_time/date.hpp" Chris@16: #include "boost/date_time/wrapping_int.hpp" Chris@16: Chris@16: namespace boost { Chris@16: namespace date_time { Chris@16: Chris@16: Chris@16: //! Functor to iterate a fixed number of days Chris@16: template Chris@16: class day_functor Chris@16: { Chris@16: public: Chris@16: typedef typename date_type::duration_type duration_type; Chris@16: day_functor(int f) : f_(f) {} Chris@16: duration_type get_offset(const date_type& d) const Chris@16: { Chris@16: // why is 'd' a parameter??? Chris@16: // fix compiler warnings Chris@16: d.year(); Chris@16: return duration_type(f_); Chris@16: } Chris@16: duration_type get_neg_offset(const date_type& d) const Chris@16: { Chris@16: // fix compiler warnings Chris@16: d.year(); Chris@16: return duration_type(-f_); Chris@16: } Chris@16: private: Chris@16: int f_; Chris@16: }; Chris@16: Chris@16: Chris@16: //! Provides calculation to find next nth month given a date Chris@16: /*! This adjustment function provides the logic for 'month-based' Chris@16: * advancement on a ymd based calendar. The policy it uses Chris@16: * to handle the non existant end of month days is to back Chris@16: * up to the last day of the month. Also, if the starting Chris@16: * date is the last day of a month, this functor will attempt Chris@16: * to adjust to the end of the month. Chris@16: Chris@16: */ Chris@16: template Chris@16: class month_functor Chris@16: { Chris@16: public: Chris@16: typedef typename date_type::duration_type duration_type; Chris@16: typedef typename date_type::calendar_type cal_type; Chris@16: typedef typename cal_type::ymd_type ymd_type; Chris@16: typedef typename cal_type::day_type day_type; Chris@16: Chris@16: month_functor(int f) : f_(f), origDayOfMonth_(0) {} Chris@16: duration_type get_offset(const date_type& d) const Chris@16: { Chris@16: ymd_type ymd(d.year_month_day()); Chris@16: if (origDayOfMonth_ == 0) { Chris@16: origDayOfMonth_ = ymd.day; Chris@16: day_type endOfMonthDay(cal_type::end_of_month_day(ymd.year,ymd.month)); Chris@16: if (endOfMonthDay == ymd.day) { Chris@16: origDayOfMonth_ = -1; //force the value to the end of month Chris@16: } Chris@16: } Chris@16: typedef date_time::wrapping_int2 wrap_int2; Chris@16: typedef typename wrap_int2::int_type int_type; Chris@16: wrap_int2 wi(ymd.month); Chris@16: //calc the year wrap around, add() returns 0 or 1 if wrapped Chris@16: int_type year = wi.add(static_cast(f_)); Chris@16: year = static_cast(year + ymd.year); //calculate resulting year Chris@16: // std::cout << "trace wi: " << wi.as_int() << std::endl; Chris@16: // std::cout << "trace year: " << year << std::endl; Chris@16: //find the last day for the new month Chris@16: day_type resultingEndOfMonthDay(cal_type::end_of_month_day(year, wi.as_int())); Chris@16: //original was the end of month -- force to last day of month Chris@16: if (origDayOfMonth_ == -1) { Chris@16: return date_type(year, wi.as_int(), resultingEndOfMonthDay) - d; Chris@16: } Chris@16: day_type dayOfMonth = origDayOfMonth_; Chris@16: if (dayOfMonth > resultingEndOfMonthDay) { Chris@16: dayOfMonth = resultingEndOfMonthDay; Chris@16: } Chris@16: return date_type(year, wi.as_int(), dayOfMonth) - d; Chris@16: } Chris@16: //! Returns a negative duration_type Chris@16: duration_type get_neg_offset(const date_type& d) const Chris@16: { Chris@16: ymd_type ymd(d.year_month_day()); Chris@16: if (origDayOfMonth_ == 0) { Chris@16: origDayOfMonth_ = ymd.day; Chris@16: day_type endOfMonthDay(cal_type::end_of_month_day(ymd.year,ymd.month)); Chris@16: if (endOfMonthDay == ymd.day) { Chris@16: origDayOfMonth_ = -1; //force the value to the end of month Chris@16: } Chris@16: } Chris@16: typedef date_time::wrapping_int2 wrap_int2; Chris@16: typedef typename wrap_int2::int_type int_type; Chris@16: wrap_int2 wi(ymd.month); Chris@16: //calc the year wrap around, add() returns 0 or 1 if wrapped Chris@16: int_type year = wi.subtract(static_cast(f_)); Chris@16: year = static_cast(year + ymd.year); //calculate resulting year Chris@16: //find the last day for the new month Chris@16: day_type resultingEndOfMonthDay(cal_type::end_of_month_day(year, wi.as_int())); Chris@16: //original was the end of month -- force to last day of month Chris@16: if (origDayOfMonth_ == -1) { Chris@16: return date_type(year, wi.as_int(), resultingEndOfMonthDay) - d; Chris@16: } Chris@16: day_type dayOfMonth = origDayOfMonth_; Chris@16: if (dayOfMonth > resultingEndOfMonthDay) { Chris@16: dayOfMonth = resultingEndOfMonthDay; Chris@16: } Chris@16: return date_type(year, wi.as_int(), dayOfMonth) - d; Chris@16: } Chris@16: private: Chris@16: int f_; Chris@16: mutable short origDayOfMonth_; Chris@16: }; Chris@16: Chris@16: Chris@16: //! Functor to iterate a over weeks Chris@16: template Chris@16: class week_functor Chris@16: { Chris@16: public: Chris@16: typedef typename date_type::duration_type duration_type; Chris@16: typedef typename date_type::calendar_type calendar_type; Chris@16: week_functor(int f) : f_(f) {} Chris@16: duration_type get_offset(const date_type& d) const Chris@16: { Chris@16: // why is 'd' a parameter??? Chris@16: // fix compiler warnings Chris@16: d.year(); Chris@16: return duration_type(f_*calendar_type::days_in_week()); Chris@16: } Chris@16: duration_type get_neg_offset(const date_type& d) const Chris@16: { Chris@16: // fix compiler warnings Chris@16: d.year(); Chris@16: return duration_type(-f_*calendar_type::days_in_week()); Chris@16: } Chris@16: private: Chris@16: int f_; Chris@16: }; Chris@16: Chris@16: //! Functor to iterate by a year adjusting for leap years Chris@16: template Chris@16: class year_functor Chris@16: { Chris@16: public: Chris@16: //typedef typename date_type::year_type year_type; Chris@16: typedef typename date_type::duration_type duration_type; Chris@16: year_functor(int f) : _mf(f * 12) {} Chris@16: duration_type get_offset(const date_type& d) const Chris@16: { Chris@16: return _mf.get_offset(d); Chris@16: } Chris@16: duration_type get_neg_offset(const date_type& d) const Chris@16: { Chris@16: return _mf.get_neg_offset(d); Chris@16: } Chris@16: private: Chris@16: month_functor _mf; Chris@16: }; Chris@16: Chris@16: Chris@16: } }//namespace date_time Chris@16: Chris@16: Chris@16: #endif Chris@16: