1 #ifndef DATE_DURATION_TYPES_HPP___ 2 #define DATE_DURATION_TYPES_HPP___ 3 4 /* Copyright (c) 2004 CrystalClear Software, Inc. 5 * Subject to the Boost Software License, Version 1.0. 6 * (See accompanying file LICENSE_1_0.txt or 7 * http://www.boost.org/LICENSE_1_0.txt) 8 * Author: Jeff Garland, Bart Garst 9 * $Date$ 10 */ 11 12 #include <boost/date_time/int_adapter.hpp> 13 #include <boost/date_time/special_defs.hpp> 14 #include <boost/date_time/date_duration.hpp> 15 16 namespace boost { 17 namespace date_time { 18 19 20 //! Additional duration type that represents a number of n*7 days 21 template <class duration_config> 22 class weeks_duration : public date_duration<duration_config> { 23 public: weeks_duration(typename duration_config::impl_type w)24 weeks_duration(typename duration_config::impl_type w) 25 : date_duration<duration_config>(w * 7) {} weeks_duration(special_values sv)26 weeks_duration(special_values sv) 27 : date_duration<duration_config>(sv) {} 28 }; 29 30 // predeclare 31 template<class t> 32 class years_duration; 33 34 //! additional duration type that represents a logical month 35 /*! A logical month enables things like: "date(2002,Mar,2) + months(2) -> 36 * 2002-May2". If the date is a last day-of-the-month, the result will 37 * also be a last-day-of-the-month. 38 */ 39 template<class base_config> 40 class months_duration 41 { 42 private: 43 typedef typename base_config::int_rep int_rep; 44 typedef typename int_rep::int_type int_type; 45 typedef typename base_config::date_type date_type; 46 typedef typename date_type::duration_type duration_type; 47 typedef typename base_config::month_adjustor_type month_adjustor_type; 48 typedef months_duration<base_config> months_type; 49 typedef years_duration<base_config> years_type; 50 public: months_duration(int_rep num)51 months_duration(int_rep num) : _m(num) {} months_duration(special_values sv)52 months_duration(special_values sv) : _m(sv) 53 { 54 _m = int_rep::from_special(sv); 55 } number_of_months() const56 int_rep number_of_months() const { return _m; } 57 //! returns a negative duration get_neg_offset(const date_type & d) const58 duration_type get_neg_offset(const date_type& d) const 59 { 60 month_adjustor_type m_adj(_m.as_number()); 61 return duration_type(m_adj.get_neg_offset(d)); 62 } get_offset(const date_type & d) const63 duration_type get_offset(const date_type& d) const 64 { 65 month_adjustor_type m_adj(_m.as_number()); 66 return duration_type(m_adj.get_offset(d)); 67 } operator ==(const months_type & rhs) const68 bool operator==(const months_type& rhs) const 69 { 70 return(_m == rhs._m); 71 } operator !=(const months_type & rhs) const72 bool operator!=(const months_type& rhs) const 73 { 74 return(_m != rhs._m); 75 } operator +(const months_type & rhs) const76 months_type operator+(const months_type& rhs)const 77 { 78 return months_type(_m + rhs._m); 79 } operator +=(const months_type & rhs)80 months_type& operator+=(const months_type& rhs) 81 { 82 _m = _m + rhs._m; 83 return *this; 84 } operator -(const months_type & rhs) const85 months_type operator-(const months_type& rhs)const 86 { 87 return months_type(_m - rhs._m); 88 } operator -=(const months_type & rhs)89 months_type& operator-=(const months_type& rhs) 90 { 91 _m = _m - rhs._m; 92 return *this; 93 } operator *(const int_type rhs) const94 months_type operator*(const int_type rhs)const 95 { 96 return months_type(_m * rhs); 97 } operator *=(const int_type rhs)98 months_type& operator*=(const int_type rhs) 99 { 100 _m = _m * rhs; 101 return *this; 102 } operator /(const int_type rhs) const103 months_type operator/(const int_type rhs)const 104 { 105 return months_type(_m / rhs); 106 } operator /=(const int_type rhs)107 months_type& operator/=(const int_type rhs) 108 { 109 _m = _m / rhs; 110 return *this; 111 } operator +(const years_type & y) const112 months_type operator+(const years_type& y)const 113 { 114 return months_type(y.number_of_years() * 12 + _m); 115 } operator +=(const years_type & y)116 months_type& operator+=(const years_type& y) 117 { 118 _m = y.number_of_years() * 12 + _m; 119 return *this; 120 } operator -(const years_type & y) const121 months_type operator-(const years_type& y) const 122 { 123 return months_type(_m - y.number_of_years() * 12); 124 } operator -=(const years_type & y)125 months_type& operator-=(const years_type& y) 126 { 127 _m = _m - y.number_of_years() * 12; 128 return *this; 129 } 130 131 // operator +(const date_type & d,const months_type & m)132 friend date_type operator+(const date_type& d, const months_type& m) 133 { 134 return d + m.get_offset(d); 135 } operator +=(date_type & d,const months_type & m)136 friend date_type operator+=(date_type& d, const months_type& m) 137 { 138 return d += m.get_offset(d); 139 } operator -(const date_type & d,const months_type & m)140 friend date_type operator-(const date_type& d, const months_type& m) 141 { 142 // get_neg_offset returns a negative duration, so we add 143 return d + m.get_neg_offset(d); 144 } operator -=(date_type & d,const months_type & m)145 friend date_type operator-=(date_type& d, const months_type& m) 146 { 147 // get_neg_offset returns a negative duration, so we add 148 return d += m.get_neg_offset(d); 149 } 150 151 private: 152 int_rep _m; 153 }; 154 155 //! additional duration type that represents a logical year 156 /*! A logical year enables things like: "date(2002,Mar,2) + years(2) -> 157 * 2004-Mar-2". If the date is a last day-of-the-month, the result will 158 * also be a last-day-of-the-month (ie date(2001-Feb-28) + years(3) -> 159 * 2004-Feb-29). 160 */ 161 template<class base_config> 162 class years_duration 163 { 164 private: 165 typedef typename base_config::int_rep int_rep; 166 typedef typename int_rep::int_type int_type; 167 typedef typename base_config::date_type date_type; 168 typedef typename date_type::duration_type duration_type; 169 typedef typename base_config::month_adjustor_type month_adjustor_type; 170 typedef years_duration<base_config> years_type; 171 typedef months_duration<base_config> months_type; 172 public: years_duration(int_rep num)173 years_duration(int_rep num) : _y(num) {} years_duration(special_values sv)174 years_duration(special_values sv) : _y(sv) 175 { 176 _y = int_rep::from_special(sv); 177 } number_of_years() const178 int_rep number_of_years() const { return _y; } 179 //! returns a negative duration get_neg_offset(const date_type & d) const180 duration_type get_neg_offset(const date_type& d) const 181 { 182 month_adjustor_type m_adj(_y.as_number() * 12); 183 return duration_type(m_adj.get_neg_offset(d)); 184 } get_offset(const date_type & d) const185 duration_type get_offset(const date_type& d) const 186 { 187 month_adjustor_type m_adj(_y.as_number() * 12); 188 return duration_type(m_adj.get_offset(d)); 189 } operator ==(const years_type & rhs) const190 bool operator==(const years_type& rhs) const 191 { 192 return(_y == rhs._y); 193 } operator !=(const years_type & rhs) const194 bool operator!=(const years_type& rhs) const 195 { 196 return(_y != rhs._y); 197 } operator +(const years_type & rhs) const198 years_type operator+(const years_type& rhs)const 199 { 200 return years_type(_y + rhs._y); 201 } operator +=(const years_type & rhs)202 years_type& operator+=(const years_type& rhs) 203 { 204 _y = _y + rhs._y; 205 return *this; 206 } operator -(const years_type & rhs) const207 years_type operator-(const years_type& rhs)const 208 { 209 return years_type(_y - rhs._y); 210 } operator -=(const years_type & rhs)211 years_type& operator-=(const years_type& rhs) 212 { 213 _y = _y - rhs._y; 214 return *this; 215 } operator *(const int_type rhs) const216 years_type operator*(const int_type rhs)const 217 { 218 return years_type(_y * rhs); 219 } operator *=(const int_type rhs)220 years_type& operator*=(const int_type rhs) 221 { 222 _y = _y * rhs; 223 return *this; 224 } operator /(const int_type rhs) const225 years_type operator/(const int_type rhs)const 226 { 227 return years_type(_y / rhs); 228 } operator /=(const int_type rhs)229 years_type& operator/=(const int_type rhs) 230 { 231 _y = _y / rhs; 232 return *this; 233 } operator +(const months_type & m) const234 months_type operator+(const months_type& m) const 235 { 236 return(months_type(_y * 12 + m.number_of_months())); 237 } operator -(const months_type & m) const238 months_type operator-(const months_type& m) const 239 { 240 return(months_type(_y * 12 - m.number_of_months())); 241 } 242 243 // operator +(const date_type & d,const years_type & y)244 friend date_type operator+(const date_type& d, const years_type& y) 245 { 246 return d + y.get_offset(d); 247 } operator +=(date_type & d,const years_type & y)248 friend date_type operator+=(date_type& d, const years_type& y) 249 { 250 return d += y.get_offset(d); 251 } operator -(const date_type & d,const years_type & y)252 friend date_type operator-(const date_type& d, const years_type& y) 253 { 254 // get_neg_offset returns a negative duration, so we add 255 return d + y.get_neg_offset(d); 256 } operator -=(date_type & d,const years_type & y)257 friend date_type operator-=(date_type& d, const years_type& y) 258 { 259 // get_neg_offset returns a negative duration, so we add 260 return d += y.get_neg_offset(d); 261 } 262 263 private: 264 int_rep _y; 265 }; 266 267 }} // namespace boost::date_time 268 269 #endif // DATE_DURATION_TYPES_HPP___ 270