1 #ifndef DATE_DURATION_TYPES_HPP___
2 #define DATE_DURATION_TYPES_HPP___
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
12 #include <boost/date_time/compiler_config.hpp>
13 #include <boost/date_time/int_adapter.hpp>
14 #include <boost/date_time/special_defs.hpp>
15 #include <boost/date_time/date_duration.hpp>
21 //! Additional duration type that represents a number of n*7 days
22 template <class duration_config>
23 class BOOST_SYMBOL_VISIBLE weeks_duration : public date_duration<duration_config> {
25 weeks_duration(typename duration_config::impl_type w)
26 : date_duration<duration_config>(w * 7) {}
27 weeks_duration(special_values sv)
28 : date_duration<duration_config>(sv) {}
33 class BOOST_SYMBOL_VISIBLE years_duration;
35 //! additional duration type that represents a logical month
36 /*! A logical month enables things like: "date(2002,Mar,2) + months(2) ->
37 * 2002-May2". If the date is a last day-of-the-month, the result will
38 * also be a last-day-of-the-month.
40 template<class base_config>
41 class BOOST_SYMBOL_VISIBLE months_duration
44 typedef typename base_config::int_rep int_rep;
45 typedef typename int_rep::int_type int_type;
46 typedef typename base_config::date_type date_type;
47 typedef typename date_type::duration_type duration_type;
48 typedef typename base_config::month_adjustor_type month_adjustor_type;
49 typedef months_duration<base_config> months_type;
50 typedef years_duration<base_config> years_type;
52 months_duration(int_rep num) : _m(num) {}
53 months_duration(special_values sv) : _m(sv)
55 _m = int_rep::from_special(sv);
57 int_rep number_of_months() const { return _m; }
58 //! returns a negative duration
59 duration_type get_neg_offset(const date_type& d) const
61 month_adjustor_type m_adj(_m.as_number());
62 return duration_type(m_adj.get_neg_offset(d));
64 duration_type get_offset(const date_type& d) const
66 month_adjustor_type m_adj(_m.as_number());
67 return duration_type(m_adj.get_offset(d));
69 bool operator==(const months_type& rhs) const
73 bool operator!=(const months_type& rhs) const
77 months_type operator+(const months_type& rhs)const
79 return months_type(_m + rhs._m);
81 months_type& operator+=(const months_type& rhs)
86 months_type operator-(const months_type& rhs)const
88 return months_type(_m - rhs._m);
90 months_type& operator-=(const months_type& rhs)
95 months_type operator*(const int_type rhs)const
97 return months_type(_m * rhs);
99 months_type& operator*=(const int_type rhs)
104 months_type operator/(const int_type rhs)const
106 return months_type(_m / rhs);
108 months_type& operator/=(const int_type rhs)
113 months_type operator+(const years_type& y)const
115 return months_type(y.number_of_years() * 12 + _m);
117 months_type& operator+=(const years_type& y)
119 _m = y.number_of_years() * 12 + _m;
122 months_type operator-(const years_type& y) const
124 return months_type(_m - y.number_of_years() * 12);
126 months_type& operator-=(const years_type& y)
128 _m = _m - y.number_of_years() * 12;
133 friend date_type operator+(const date_type& d, const months_type& m)
135 return d + m.get_offset(d);
137 friend date_type operator+=(date_type& d, const months_type& m)
139 return d += m.get_offset(d);
141 friend date_type operator-(const date_type& d, const months_type& m)
143 // get_neg_offset returns a negative duration, so we add
144 return d + m.get_neg_offset(d);
146 friend date_type operator-=(date_type& d, const months_type& m)
148 // get_neg_offset returns a negative duration, so we add
149 return d += m.get_neg_offset(d);
156 //! additional duration type that represents a logical year
157 /*! A logical year enables things like: "date(2002,Mar,2) + years(2) ->
158 * 2004-Mar-2". If the date is a last day-of-the-month, the result will
159 * also be a last-day-of-the-month (ie date(2001-Feb-28) + years(3) ->
162 template<class base_config>
163 class BOOST_SYMBOL_VISIBLE years_duration
166 typedef typename base_config::int_rep int_rep;
167 typedef typename int_rep::int_type int_type;
168 typedef typename base_config::date_type date_type;
169 typedef typename date_type::duration_type duration_type;
170 typedef typename base_config::month_adjustor_type month_adjustor_type;
171 typedef years_duration<base_config> years_type;
172 typedef months_duration<base_config> months_type;
174 years_duration(int_rep num) : _y(num) {}
175 years_duration(special_values sv) : _y(sv)
177 _y = int_rep::from_special(sv);
179 int_rep number_of_years() const { return _y; }
180 //! returns a negative duration
181 duration_type get_neg_offset(const date_type& d) const
183 month_adjustor_type m_adj(_y.as_number() * 12);
184 return duration_type(m_adj.get_neg_offset(d));
186 duration_type get_offset(const date_type& d) const
188 month_adjustor_type m_adj(_y.as_number() * 12);
189 return duration_type(m_adj.get_offset(d));
191 bool operator==(const years_type& rhs) const
193 return(_y == rhs._y);
195 bool operator!=(const years_type& rhs) const
197 return(_y != rhs._y);
199 years_type operator+(const years_type& rhs)const
201 return years_type(_y + rhs._y);
203 years_type& operator+=(const years_type& rhs)
208 years_type operator-(const years_type& rhs)const
210 return years_type(_y - rhs._y);
212 years_type& operator-=(const years_type& rhs)
217 years_type operator*(const int_type rhs)const
219 return years_type(_y * rhs);
221 years_type& operator*=(const int_type rhs)
226 years_type operator/(const int_type rhs)const
228 return years_type(_y / rhs);
230 years_type& operator/=(const int_type rhs)
235 months_type operator+(const months_type& m) const
237 return(months_type(_y * 12 + m.number_of_months()));
239 months_type operator-(const months_type& m) const
241 return(months_type(_y * 12 - m.number_of_months()));
245 friend date_type operator+(const date_type& d, const years_type& y)
247 return d + y.get_offset(d);
249 friend date_type operator+=(date_type& d, const years_type& y)
251 return d += y.get_offset(d);
253 friend date_type operator-(const date_type& d, const years_type& y)
255 // get_neg_offset returns a negative duration, so we add
256 return d + y.get_neg_offset(d);
258 friend date_type operator-=(date_type& d, const years_type& y)
260 // get_neg_offset returns a negative duration, so we add
261 return d += y.get_neg_offset(d);
268 }} // namespace boost::date_time
270 #endif // DATE_DURATION_TYPES_HPP___