annotate DEPENDENCIES/generic/include/boost/date_time/adjust_functors.hpp @ 133:4acb5d8d80b6 tip

Don't fail environmental check if README.md exists (but .txt and no-suffix don't)
author Chris Cannam
date Tue, 30 Jul 2019 12:25:44 +0100
parents c530137014c0
children
rev   line source
Chris@16 1 #ifndef _DATE_TIME_ADJUST_FUNCTORS_HPP___
Chris@16 2 #define _DATE_TIME_ADJUST_FUNCTORS_HPP___
Chris@16 3
Chris@16 4 /* Copyright (c) 2002,2003 CrystalClear Software, Inc.
Chris@16 5 * Use, modification and distribution is subject to the
Chris@16 6 * Boost Software License, Version 1.0. (See accompanying
Chris@16 7 * file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
Chris@16 8 * Author: Jeff Garland, Bart Garst
Chris@101 9 * $Date$
Chris@16 10 */
Chris@16 11
Chris@16 12 #include "boost/date_time/date.hpp"
Chris@16 13 #include "boost/date_time/wrapping_int.hpp"
Chris@16 14
Chris@16 15 namespace boost {
Chris@16 16 namespace date_time {
Chris@16 17
Chris@16 18
Chris@16 19 //! Functor to iterate a fixed number of days
Chris@16 20 template<class date_type>
Chris@16 21 class day_functor
Chris@16 22 {
Chris@16 23 public:
Chris@16 24 typedef typename date_type::duration_type duration_type;
Chris@16 25 day_functor(int f) : f_(f) {}
Chris@16 26 duration_type get_offset(const date_type& d) const
Chris@16 27 {
Chris@16 28 // why is 'd' a parameter???
Chris@16 29 // fix compiler warnings
Chris@16 30 d.year();
Chris@16 31 return duration_type(f_);
Chris@16 32 }
Chris@16 33 duration_type get_neg_offset(const date_type& d) const
Chris@16 34 {
Chris@16 35 // fix compiler warnings
Chris@16 36 d.year();
Chris@16 37 return duration_type(-f_);
Chris@16 38 }
Chris@16 39 private:
Chris@16 40 int f_;
Chris@16 41 };
Chris@16 42
Chris@16 43
Chris@16 44 //! Provides calculation to find next nth month given a date
Chris@16 45 /*! This adjustment function provides the logic for 'month-based'
Chris@16 46 * advancement on a ymd based calendar. The policy it uses
Chris@16 47 * to handle the non existant end of month days is to back
Chris@16 48 * up to the last day of the month. Also, if the starting
Chris@16 49 * date is the last day of a month, this functor will attempt
Chris@16 50 * to adjust to the end of the month.
Chris@16 51
Chris@16 52 */
Chris@16 53 template<class date_type>
Chris@16 54 class month_functor
Chris@16 55 {
Chris@16 56 public:
Chris@16 57 typedef typename date_type::duration_type duration_type;
Chris@16 58 typedef typename date_type::calendar_type cal_type;
Chris@16 59 typedef typename cal_type::ymd_type ymd_type;
Chris@16 60 typedef typename cal_type::day_type day_type;
Chris@16 61
Chris@16 62 month_functor(int f) : f_(f), origDayOfMonth_(0) {}
Chris@16 63 duration_type get_offset(const date_type& d) const
Chris@16 64 {
Chris@16 65 ymd_type ymd(d.year_month_day());
Chris@16 66 if (origDayOfMonth_ == 0) {
Chris@16 67 origDayOfMonth_ = ymd.day;
Chris@16 68 day_type endOfMonthDay(cal_type::end_of_month_day(ymd.year,ymd.month));
Chris@16 69 if (endOfMonthDay == ymd.day) {
Chris@16 70 origDayOfMonth_ = -1; //force the value to the end of month
Chris@16 71 }
Chris@16 72 }
Chris@16 73 typedef date_time::wrapping_int2<short,1,12> wrap_int2;
Chris@16 74 typedef typename wrap_int2::int_type int_type;
Chris@16 75 wrap_int2 wi(ymd.month);
Chris@16 76 //calc the year wrap around, add() returns 0 or 1 if wrapped
Chris@16 77 int_type year = wi.add(static_cast<int_type>(f_));
Chris@16 78 year = static_cast<int_type>(year + ymd.year); //calculate resulting year
Chris@16 79 // std::cout << "trace wi: " << wi.as_int() << std::endl;
Chris@16 80 // std::cout << "trace year: " << year << std::endl;
Chris@16 81 //find the last day for the new month
Chris@16 82 day_type resultingEndOfMonthDay(cal_type::end_of_month_day(year, wi.as_int()));
Chris@16 83 //original was the end of month -- force to last day of month
Chris@16 84 if (origDayOfMonth_ == -1) {
Chris@16 85 return date_type(year, wi.as_int(), resultingEndOfMonthDay) - d;
Chris@16 86 }
Chris@16 87 day_type dayOfMonth = origDayOfMonth_;
Chris@16 88 if (dayOfMonth > resultingEndOfMonthDay) {
Chris@16 89 dayOfMonth = resultingEndOfMonthDay;
Chris@16 90 }
Chris@16 91 return date_type(year, wi.as_int(), dayOfMonth) - d;
Chris@16 92 }
Chris@16 93 //! Returns a negative duration_type
Chris@16 94 duration_type get_neg_offset(const date_type& d) const
Chris@16 95 {
Chris@16 96 ymd_type ymd(d.year_month_day());
Chris@16 97 if (origDayOfMonth_ == 0) {
Chris@16 98 origDayOfMonth_ = ymd.day;
Chris@16 99 day_type endOfMonthDay(cal_type::end_of_month_day(ymd.year,ymd.month));
Chris@16 100 if (endOfMonthDay == ymd.day) {
Chris@16 101 origDayOfMonth_ = -1; //force the value to the end of month
Chris@16 102 }
Chris@16 103 }
Chris@16 104 typedef date_time::wrapping_int2<short,1,12> wrap_int2;
Chris@16 105 typedef typename wrap_int2::int_type int_type;
Chris@16 106 wrap_int2 wi(ymd.month);
Chris@16 107 //calc the year wrap around, add() returns 0 or 1 if wrapped
Chris@16 108 int_type year = wi.subtract(static_cast<int_type>(f_));
Chris@16 109 year = static_cast<int_type>(year + ymd.year); //calculate resulting year
Chris@16 110 //find the last day for the new month
Chris@16 111 day_type resultingEndOfMonthDay(cal_type::end_of_month_day(year, wi.as_int()));
Chris@16 112 //original was the end of month -- force to last day of month
Chris@16 113 if (origDayOfMonth_ == -1) {
Chris@16 114 return date_type(year, wi.as_int(), resultingEndOfMonthDay) - d;
Chris@16 115 }
Chris@16 116 day_type dayOfMonth = origDayOfMonth_;
Chris@16 117 if (dayOfMonth > resultingEndOfMonthDay) {
Chris@16 118 dayOfMonth = resultingEndOfMonthDay;
Chris@16 119 }
Chris@16 120 return date_type(year, wi.as_int(), dayOfMonth) - d;
Chris@16 121 }
Chris@16 122 private:
Chris@16 123 int f_;
Chris@16 124 mutable short origDayOfMonth_;
Chris@16 125 };
Chris@16 126
Chris@16 127
Chris@16 128 //! Functor to iterate a over weeks
Chris@16 129 template<class date_type>
Chris@16 130 class week_functor
Chris@16 131 {
Chris@16 132 public:
Chris@16 133 typedef typename date_type::duration_type duration_type;
Chris@16 134 typedef typename date_type::calendar_type calendar_type;
Chris@16 135 week_functor(int f) : f_(f) {}
Chris@16 136 duration_type get_offset(const date_type& d) const
Chris@16 137 {
Chris@16 138 // why is 'd' a parameter???
Chris@16 139 // fix compiler warnings
Chris@16 140 d.year();
Chris@16 141 return duration_type(f_*calendar_type::days_in_week());
Chris@16 142 }
Chris@16 143 duration_type get_neg_offset(const date_type& d) const
Chris@16 144 {
Chris@16 145 // fix compiler warnings
Chris@16 146 d.year();
Chris@16 147 return duration_type(-f_*calendar_type::days_in_week());
Chris@16 148 }
Chris@16 149 private:
Chris@16 150 int f_;
Chris@16 151 };
Chris@16 152
Chris@16 153 //! Functor to iterate by a year adjusting for leap years
Chris@16 154 template<class date_type>
Chris@16 155 class year_functor
Chris@16 156 {
Chris@16 157 public:
Chris@16 158 //typedef typename date_type::year_type year_type;
Chris@16 159 typedef typename date_type::duration_type duration_type;
Chris@16 160 year_functor(int f) : _mf(f * 12) {}
Chris@16 161 duration_type get_offset(const date_type& d) const
Chris@16 162 {
Chris@16 163 return _mf.get_offset(d);
Chris@16 164 }
Chris@16 165 duration_type get_neg_offset(const date_type& d) const
Chris@16 166 {
Chris@16 167 return _mf.get_neg_offset(d);
Chris@16 168 }
Chris@16 169 private:
Chris@16 170 month_functor<date_type> _mf;
Chris@16 171 };
Chris@16 172
Chris@16 173
Chris@16 174 } }//namespace date_time
Chris@16 175
Chris@16 176
Chris@16 177 #endif
Chris@16 178