Chris@16: #ifndef DATE_TIME_DATE_GENERATORS_HPP__ Chris@16: #define DATE_TIME_DATE_GENERATORS_HPP__ Chris@16: Chris@16: /* Copyright (c) 2002,2003,2005 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: /*! @file date_generators.hpp Chris@16: Definition and implementation of date algorithm templates Chris@16: */ Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: namespace boost { Chris@16: namespace date_time { Chris@16: Chris@16: //! Base class for all generators that take a year and produce a date. Chris@16: /*! This class is a base class for polymorphic function objects that take Chris@16: a year and produce a concrete date. Chris@16: @param date_type The type representing a date. This type must Chris@16: export a calender_type which defines a year_type. Chris@16: */ Chris@16: template Chris@16: class year_based_generator Chris@16: { Chris@16: public: Chris@16: typedef typename date_type::calendar_type calendar_type; Chris@16: typedef typename calendar_type::year_type year_type; Chris@16: year_based_generator() {} Chris@16: virtual ~year_based_generator() {} Chris@16: virtual date_type get_date(year_type y) const = 0; Chris@16: //! Returns a string for use in a POSIX time_zone string Chris@16: virtual std::string to_string() const =0; Chris@16: }; Chris@16: Chris@16: //! Generates a date by applying the year to the given month and day. Chris@16: /*! Chris@16: Example usage: Chris@16: @code Chris@16: partial_date pd(1, Jan); Chris@16: partial_date pd2(70); Chris@16: date d = pd.get_date(2002); //2002-Jan-01 Chris@16: date d2 = pd2.get_date(2002); //2002-Mar-10 Chris@16: @endcode Chris@16: \ingroup date_alg Chris@16: */ Chris@16: template Chris@16: class partial_date : public year_based_generator Chris@16: { Chris@16: public: Chris@16: typedef typename date_type::calendar_type calendar_type; Chris@16: typedef typename calendar_type::day_type day_type; Chris@16: typedef typename calendar_type::month_type month_type; Chris@16: typedef typename calendar_type::year_type year_type; Chris@16: typedef typename date_type::duration_type duration_type; Chris@16: typedef typename duration_type::duration_rep duration_rep; Chris@16: partial_date(day_type d, month_type m) : Chris@16: day_(d), Chris@16: month_(m) Chris@16: {} Chris@16: //! Partial date created from number of days into year. Range 1-366 Chris@16: /*! Allowable values range from 1 to 366. 1=Jan1, 366=Dec31. If argument Chris@16: * exceeds range, partial_date will be created with closest in-range value. Chris@16: * 60 will always be Feb29, if get_date() is called with a non-leap year Chris@16: * an exception will be thrown */ Chris@16: partial_date(duration_rep days) : Chris@16: day_(1), // default values Chris@16: month_(1) Chris@16: { Chris@16: date_type d1(2000,1,1); Chris@16: if(days > 1) { Chris@16: if(days > 366) // prevents wrapping Chris@16: { Chris@16: days = 366; Chris@16: } Chris@16: days = days - 1; Chris@16: duration_type dd(days); Chris@16: d1 = d1 + dd; Chris@16: } Chris@16: day_ = d1.day(); Chris@16: month_ = d1.month(); Chris@16: } Chris@16: //! Return a concrete date when provided with a year specific year. Chris@16: /*! Will throw an 'invalid_argument' exception if a partial_date object, Chris@16: * instantiated with Feb-29, has get_date called with a non-leap year. Chris@16: * Example: Chris@16: * @code Chris@16: * partial_date pd(29, Feb); Chris@16: * pd.get_date(2003); // throws invalid_argument exception Chris@16: * pg.get_date(2000); // returns 2000-2-29 Chris@16: * @endcode Chris@16: */ Chris@16: date_type get_date(year_type y) const Chris@16: { Chris@16: if((day_ == 29) && (month_ == 2) && !(calendar_type::is_leap_year(y))) { Chris@16: std::ostringstream ss; Chris@16: ss << "No Feb 29th in given year of " << y << "."; Chris@16: boost::throw_exception(std::invalid_argument(ss.str())); Chris@16: } Chris@16: return date_type(y, month_, day_); Chris@16: } Chris@16: date_type operator()(year_type y) const Chris@16: { Chris@16: return get_date(y); Chris@16: //return date_type(y, month_, day_); Chris@16: } Chris@16: bool operator==(const partial_date& rhs) const Chris@16: { Chris@16: return (month_ == rhs.month_) && (day_ == rhs.day_); Chris@16: } Chris@16: bool operator<(const partial_date& rhs) const Chris@16: { Chris@16: if (month_ < rhs.month_) return true; Chris@16: if (month_ > rhs.month_) return false; Chris@16: //months are equal Chris@16: return (day_ < rhs.day_); Chris@16: } Chris@16: Chris@16: // added for streaming purposes Chris@16: month_type month() const Chris@16: { Chris@16: return month_; Chris@16: } Chris@16: day_type day() const Chris@16: { Chris@16: return day_; Chris@16: } Chris@16: Chris@16: //! Returns string suitable for use in POSIX time zone string Chris@16: /*! Returns string formatted with up to 3 digits: Chris@16: * Jan-01 == "0" Chris@16: * Feb-29 == "58" Chris@16: * Dec-31 == "365" */ Chris@16: virtual std::string to_string() const Chris@16: { Chris@16: std::ostringstream ss; Chris@16: date_type d(2004, month_, day_); Chris@16: unsigned short c = d.day_of_year(); Chris@16: c--; // numbered 0-365 while day_of_year is 1 based... Chris@16: ss << c; Chris@16: return ss.str(); Chris@16: } Chris@16: private: Chris@16: day_type day_; Chris@16: month_type month_; Chris@16: }; Chris@16: Chris@16: Chris@16: //! Returns nth arg as string. 1 -> "first", 2 -> "second", max is 5. Chris@16: BOOST_DATE_TIME_DECL const char* nth_as_str(int n); Chris@16: Chris@16: //! Useful generator functor for finding holidays Chris@16: /*! Based on the idea in Cal. Calc. for finding holidays that are Chris@16: * the 'first Monday of September'. When instantiated with Chris@16: * 'fifth' kday of month, the result will be the last kday of month Chris@16: * which can be the fourth or fifth depending on the structure of Chris@16: * the month. Chris@16: * Chris@16: * The algorithm here basically guesses for the first Chris@16: * day of the month. Then finds the first day of the correct Chris@16: * type. That is, if the first of the month is a Tuesday Chris@16: * and it needs Wenesday then we simply increment by a day Chris@16: * and then we can add the length of a week until we get Chris@16: * to the 'nth kday'. There are probably more efficient Chris@16: * algorithms based on using a mod 7, but this one works Chris@16: * reasonably well for basic applications. Chris@16: * \ingroup date_alg Chris@16: */ Chris@16: template Chris@16: class nth_kday_of_month : public year_based_generator Chris@16: { Chris@16: public: Chris@16: typedef typename date_type::calendar_type calendar_type; Chris@16: typedef typename calendar_type::day_of_week_type day_of_week_type; Chris@16: typedef typename calendar_type::month_type month_type; Chris@16: typedef typename calendar_type::year_type year_type; Chris@16: typedef typename date_type::duration_type duration_type; Chris@16: enum week_num {first=1, second, third, fourth, fifth}; Chris@16: nth_kday_of_month(week_num week_no, Chris@16: day_of_week_type dow, Chris@16: month_type m) : Chris@16: month_(m), Chris@16: wn_(week_no), Chris@16: dow_(dow) Chris@16: {} Chris@16: //! Return a concrete date when provided with a year specific year. Chris@16: date_type get_date(year_type y) const Chris@16: { Chris@16: date_type d(y, month_, 1); //first day of month Chris@16: duration_type one_day(1); Chris@16: duration_type one_week(7); Chris@16: while (dow_ != d.day_of_week()) { Chris@16: d = d + one_day; Chris@16: } Chris@16: int week = 1; Chris@16: while (week < wn_) { Chris@16: d = d + one_week; Chris@16: week++; Chris@16: } Chris@16: // remove wrapping to next month behavior Chris@16: if(d.month() != month_) { Chris@16: d = d - one_week; Chris@16: } Chris@16: return d; Chris@16: } Chris@16: // added for streaming Chris@16: month_type month() const Chris@16: { Chris@16: return month_; Chris@16: } Chris@16: week_num nth_week() const Chris@16: { Chris@16: return wn_; Chris@16: } Chris@16: day_of_week_type day_of_week() const Chris@16: { Chris@16: return dow_; Chris@16: } Chris@16: const char* nth_week_as_str() const Chris@16: { Chris@16: return nth_as_str(wn_); Chris@16: } Chris@16: //! Returns string suitable for use in POSIX time zone string Chris@16: /*! Returns a string formatted as "M4.3.0" ==> 3rd Sunday in April. */ Chris@16: virtual std::string to_string() const Chris@16: { Chris@16: std::ostringstream ss; Chris@16: ss << 'M' Chris@16: << static_cast(month_) << '.' Chris@16: << static_cast(wn_) << '.' Chris@16: << static_cast(dow_); Chris@16: return ss.str(); Chris@16: } Chris@16: private: Chris@16: month_type month_; Chris@16: week_num wn_; Chris@16: day_of_week_type dow_; Chris@16: }; Chris@16: Chris@16: //! Useful generator functor for finding holidays and daylight savings Chris@16: /*! Similar to nth_kday_of_month, but requires less paramters Chris@16: * \ingroup date_alg Chris@16: */ Chris@16: template Chris@16: class first_kday_of_month : public year_based_generator Chris@16: { Chris@16: public: Chris@16: typedef typename date_type::calendar_type calendar_type; Chris@16: typedef typename calendar_type::day_of_week_type day_of_week_type; Chris@16: typedef typename calendar_type::month_type month_type; Chris@16: typedef typename calendar_type::year_type year_type; Chris@16: typedef typename date_type::duration_type duration_type; Chris@16: //!Specify the first 'Sunday' in 'April' spec Chris@16: /*!@param dow The day of week, eg: Sunday, Monday, etc Chris@16: * @param m The month of the year, eg: Jan, Feb, Mar, etc Chris@16: */ Chris@16: first_kday_of_month(day_of_week_type dow, month_type m) : Chris@16: month_(m), Chris@16: dow_(dow) Chris@16: {} Chris@16: //! Return a concrete date when provided with a year specific year. Chris@16: date_type get_date(year_type year) const Chris@16: { Chris@16: date_type d(year, month_,1); Chris@16: duration_type one_day(1); Chris@16: while (dow_ != d.day_of_week()) { Chris@16: d = d + one_day; Chris@16: } Chris@16: return d; Chris@16: } Chris@16: // added for streaming Chris@16: month_type month() const Chris@16: { Chris@16: return month_; Chris@16: } Chris@16: day_of_week_type day_of_week() const Chris@16: { Chris@16: return dow_; Chris@16: } Chris@16: //! Returns string suitable for use in POSIX time zone string Chris@16: /*! Returns a string formatted as "M4.1.0" ==> 1st Sunday in April. */ Chris@16: virtual std::string to_string() const Chris@16: { Chris@16: std::ostringstream ss; Chris@16: ss << 'M' Chris@16: << static_cast(month_) << '.' Chris@16: << 1 << '.' Chris@16: << static_cast(dow_); Chris@16: return ss.str(); Chris@16: } Chris@16: private: Chris@16: month_type month_; Chris@16: day_of_week_type dow_; Chris@16: }; Chris@16: Chris@16: Chris@16: Chris@16: //! Calculate something like Last Sunday of January Chris@16: /*! Useful generator functor for finding holidays and daylight savings Chris@16: * Get the last day of the month and then calculate the difference Chris@16: * to the last previous day. Chris@16: * @param date_type A date class that exports day_of_week, month_type, etc. Chris@16: * \ingroup date_alg Chris@16: */ Chris@16: template Chris@16: class last_kday_of_month : public year_based_generator Chris@16: { Chris@16: public: Chris@16: typedef typename date_type::calendar_type calendar_type; Chris@16: typedef typename calendar_type::day_of_week_type day_of_week_type; Chris@16: typedef typename calendar_type::month_type month_type; Chris@16: typedef typename calendar_type::year_type year_type; Chris@16: typedef typename date_type::duration_type duration_type; Chris@16: //!Specify the date spec like last 'Sunday' in 'April' spec Chris@16: /*!@param dow The day of week, eg: Sunday, Monday, etc Chris@16: * @param m The month of the year, eg: Jan, Feb, Mar, etc Chris@16: */ Chris@16: last_kday_of_month(day_of_week_type dow, month_type m) : Chris@16: month_(m), Chris@16: dow_(dow) Chris@16: {} Chris@16: //! Return a concrete date when provided with a year specific year. Chris@16: date_type get_date(year_type year) const Chris@16: { Chris@16: date_type d(year, month_, calendar_type::end_of_month_day(year,month_)); Chris@16: duration_type one_day(1); Chris@16: while (dow_ != d.day_of_week()) { Chris@16: d = d - one_day; Chris@16: } Chris@16: return d; Chris@16: } Chris@16: // added for streaming Chris@16: month_type month() const Chris@16: { Chris@16: return month_; Chris@16: } Chris@16: day_of_week_type day_of_week() const Chris@16: { Chris@16: return dow_; Chris@16: } Chris@16: //! Returns string suitable for use in POSIX time zone string Chris@16: /*! Returns a string formatted as "M4.5.0" ==> last Sunday in April. */ Chris@16: virtual std::string to_string() const Chris@16: { Chris@16: std::ostringstream ss; Chris@16: ss << 'M' Chris@16: << static_cast(month_) << '.' Chris@16: << 5 << '.' Chris@16: << static_cast(dow_); Chris@16: return ss.str(); Chris@16: } Chris@16: private: Chris@16: month_type month_; Chris@16: day_of_week_type dow_; Chris@16: }; Chris@16: Chris@16: Chris@16: //! Calculate something like "First Sunday after Jan 1,2002 Chris@16: /*! Date generator that takes a date and finds kday after Chris@16: *@code Chris@16: typedef boost::date_time::first_kday_after firstkdayafter; Chris@16: firstkdayafter fkaf(Monday); Chris@16: fkaf.get_date(date(2002,Feb,1)); Chris@16: @endcode Chris@16: * \ingroup date_alg Chris@16: */ Chris@16: template Chris@16: class first_kday_after Chris@16: { Chris@16: public: Chris@16: typedef typename date_type::calendar_type calendar_type; Chris@16: typedef typename calendar_type::day_of_week_type day_of_week_type; Chris@16: typedef typename date_type::duration_type duration_type; Chris@16: first_kday_after(day_of_week_type dow) : Chris@16: dow_(dow) Chris@16: {} Chris@16: //! Return next kday given. Chris@16: date_type get_date(date_type start_day) const Chris@16: { Chris@16: duration_type one_day(1); Chris@16: date_type d = start_day + one_day; Chris@16: while (dow_ != d.day_of_week()) { Chris@16: d = d + one_day; Chris@16: } Chris@16: return d; Chris@16: } Chris@16: // added for streaming Chris@16: day_of_week_type day_of_week() const Chris@16: { Chris@16: return dow_; Chris@16: } Chris@16: private: Chris@16: day_of_week_type dow_; Chris@16: }; Chris@16: Chris@16: //! Calculate something like "First Sunday before Jan 1,2002 Chris@16: /*! Date generator that takes a date and finds kday after Chris@16: *@code Chris@16: typedef boost::date_time::first_kday_before firstkdaybefore; Chris@16: firstkdaybefore fkbf(Monday); Chris@16: fkbf.get_date(date(2002,Feb,1)); Chris@16: @endcode Chris@16: * \ingroup date_alg Chris@16: */ Chris@16: template Chris@16: class first_kday_before Chris@16: { Chris@16: public: Chris@16: typedef typename date_type::calendar_type calendar_type; Chris@16: typedef typename calendar_type::day_of_week_type day_of_week_type; Chris@16: typedef typename date_type::duration_type duration_type; Chris@16: first_kday_before(day_of_week_type dow) : Chris@16: dow_(dow) Chris@16: {} Chris@16: //! Return next kday given. Chris@16: date_type get_date(date_type start_day) const Chris@16: { Chris@16: duration_type one_day(1); Chris@16: date_type d = start_day - one_day; Chris@16: while (dow_ != d.day_of_week()) { Chris@16: d = d - one_day; Chris@16: } Chris@16: return d; Chris@16: } Chris@16: // added for streaming Chris@16: day_of_week_type day_of_week() const Chris@16: { Chris@16: return dow_; Chris@16: } Chris@16: private: Chris@16: day_of_week_type dow_; Chris@16: }; Chris@16: Chris@16: //! Calculates the number of days until the next weekday Chris@16: /*! Calculates the number of days until the next weekday. Chris@16: * If the date given falls on a Sunday and the given weekday Chris@16: * is Tuesday the result will be 2 days */ Chris@16: template Chris@16: inline Chris@16: typename date_type::duration_type days_until_weekday(const date_type& d, const weekday_type& wd) Chris@16: { Chris@16: typedef typename date_type::duration_type duration_type; Chris@16: duration_type wks(0); Chris@16: duration_type dd(wd.as_number() - d.day_of_week().as_number()); Chris@16: if(dd.is_negative()){ Chris@16: wks = duration_type(7); Chris@16: } Chris@16: return dd + wks; Chris@16: } Chris@16: Chris@16: //! Calculates the number of days since the previous weekday Chris@16: /*! Calculates the number of days since the previous weekday Chris@16: * If the date given falls on a Sunday and the given weekday Chris@16: * is Tuesday the result will be 5 days. The answer will be a positive Chris@16: * number because Tuesday is 5 days before Sunday, not -5 days before. */ Chris@16: template Chris@16: inline Chris@16: typename date_type::duration_type days_before_weekday(const date_type& d, const weekday_type& wd) Chris@16: { Chris@16: typedef typename date_type::duration_type duration_type; Chris@16: duration_type wks(0); Chris@16: duration_type dd(wd.as_number() - d.day_of_week().as_number()); Chris@16: if(dd.days() > 0){ Chris@16: wks = duration_type(7); Chris@16: } Chris@16: // we want a number of days, not an offset. The value returned must Chris@16: // be zero or larger. Chris@16: return (-dd + wks); Chris@16: } Chris@16: Chris@16: //! Generates a date object representing the date of the following weekday from the given date Chris@16: /*! Generates a date object representing the date of the following Chris@16: * weekday from the given date. If the date given is 2004-May-9 Chris@16: * (a Sunday) and the given weekday is Tuesday then the resulting date Chris@16: * will be 2004-May-11. */ Chris@16: template Chris@16: inline Chris@16: date_type next_weekday(const date_type& d, const weekday_type& wd) Chris@16: { Chris@16: return d + days_until_weekday(d, wd); Chris@16: } Chris@16: Chris@16: //! Generates a date object representing the date of the previous weekday from the given date Chris@16: /*! Generates a date object representing the date of the previous Chris@16: * weekday from the given date. If the date given is 2004-May-9 Chris@16: * (a Sunday) and the given weekday is Tuesday then the resulting date Chris@16: * will be 2004-May-4. */ Chris@16: template Chris@16: inline Chris@16: date_type previous_weekday(const date_type& d, const weekday_type& wd) Chris@16: { Chris@16: return d - days_before_weekday(d, wd); Chris@16: } Chris@16: Chris@16: } } //namespace date_time Chris@16: Chris@16: Chris@16: Chris@16: Chris@16: #endif Chris@16: