fix doc example typo
[boost.git] / boost / date_time / date_duration_types.hpp
blob8c0e9860d0cecc23b1cab429c07e0757e2c4c968
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
9 * $Date$
12 #include <boost/date_time/int_adapter.hpp>
13 #include <boost/date_time/special_defs.hpp>
14 #include <boost/date_time/date_duration.hpp>
16 namespace boost {
17 namespace date_time {
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:
24 weeks_duration(typename duration_config::impl_type w)
25 : date_duration<duration_config>(w * 7) {}
26 weeks_duration(special_values sv)
27 : date_duration<duration_config>(sv) {}
30 // predeclare
31 template<class t>
32 class years_duration;
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.
39 template<class base_config>
40 class months_duration
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:
51 months_duration(int_rep num) : _m(num) {}
52 months_duration(special_values sv) : _m(sv)
54 _m = int_rep::from_special(sv);
56 int_rep number_of_months() const { return _m; }
57 //! returns a negative duration
58 duration_type get_neg_offset(const date_type& d) const
60 month_adjustor_type m_adj(_m.as_number());
61 return duration_type(m_adj.get_neg_offset(d));
63 duration_type get_offset(const date_type& d) const
65 month_adjustor_type m_adj(_m.as_number());
66 return duration_type(m_adj.get_offset(d));
68 bool operator==(const months_type& rhs) const
70 return(_m == rhs._m);
72 bool operator!=(const months_type& rhs) const
74 return(_m != rhs._m);
76 months_type operator+(const months_type& rhs)const
78 return months_type(_m + rhs._m);
80 months_type& operator+=(const months_type& rhs)
82 _m = _m + rhs._m;
83 return *this;
85 months_type operator-(const months_type& rhs)const
87 return months_type(_m - rhs._m);
89 months_type& operator-=(const months_type& rhs)
91 _m = _m - rhs._m;
92 return *this;
94 months_type operator*(const int_type rhs)const
96 return months_type(_m * rhs);
98 months_type& operator*=(const int_type rhs)
100 _m = _m * rhs;
101 return *this;
103 months_type operator/(const int_type rhs)const
105 return months_type(_m / rhs);
107 months_type& operator/=(const int_type rhs)
109 _m = _m / rhs;
110 return *this;
112 months_type operator+(const years_type& y)const
114 return months_type(y.number_of_years() * 12 + _m);
116 months_type& operator+=(const years_type& y)
118 _m = y.number_of_years() * 12 + _m;
119 return *this;
121 months_type operator-(const years_type& y) const
123 return months_type(_m - y.number_of_years() * 12);
125 months_type& operator-=(const years_type& y)
127 _m = _m - y.number_of_years() * 12;
128 return *this;
132 friend date_type operator+(const date_type& d, const months_type& m)
134 return d + m.get_offset(d);
136 friend date_type operator+=(date_type& d, const months_type& m)
138 return d += m.get_offset(d);
140 friend date_type operator-(const date_type& d, const months_type& m)
142 // get_neg_offset returns a negative duration, so we add
143 return d + m.get_neg_offset(d);
145 friend date_type operator-=(date_type& d, const months_type& m)
147 // get_neg_offset returns a negative duration, so we add
148 return d += m.get_neg_offset(d);
151 private:
152 int_rep _m;
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).
161 template<class base_config>
162 class years_duration
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:
173 years_duration(int_rep num) : _y(num) {}
174 years_duration(special_values sv) : _y(sv)
176 _y = int_rep::from_special(sv);
178 int_rep number_of_years() const { return _y; }
179 //! returns a negative duration
180 duration_type get_neg_offset(const date_type& d) const
182 month_adjustor_type m_adj(_y.as_number() * 12);
183 return duration_type(m_adj.get_neg_offset(d));
185 duration_type get_offset(const date_type& d) const
187 month_adjustor_type m_adj(_y.as_number() * 12);
188 return duration_type(m_adj.get_offset(d));
190 bool operator==(const years_type& rhs) const
192 return(_y == rhs._y);
194 bool operator!=(const years_type& rhs) const
196 return(_y != rhs._y);
198 years_type operator+(const years_type& rhs)const
200 return years_type(_y + rhs._y);
202 years_type& operator+=(const years_type& rhs)
204 _y = _y + rhs._y;
205 return *this;
207 years_type operator-(const years_type& rhs)const
209 return years_type(_y - rhs._y);
211 years_type& operator-=(const years_type& rhs)
213 _y = _y - rhs._y;
214 return *this;
216 years_type operator*(const int_type rhs)const
218 return years_type(_y * rhs);
220 years_type& operator*=(const int_type rhs)
222 _y = _y * rhs;
223 return *this;
225 years_type operator/(const int_type rhs)const
227 return years_type(_y / rhs);
229 years_type& operator/=(const int_type rhs)
231 _y = _y / rhs;
232 return *this;
234 months_type operator+(const months_type& m) const
236 return(months_type(_y * 12 + m.number_of_months()));
238 months_type operator-(const months_type& m) const
240 return(months_type(_y * 12 - m.number_of_months()));
244 friend date_type operator+(const date_type& d, const years_type& y)
246 return d + y.get_offset(d);
248 friend date_type operator+=(date_type& d, const years_type& y)
250 return d += y.get_offset(d);
252 friend date_type operator-(const date_type& d, const years_type& y)
254 // get_neg_offset returns a negative duration, so we add
255 return d + y.get_neg_offset(d);
257 friend date_type operator-=(date_type& d, const years_type& y)
259 // get_neg_offset returns a negative duration, so we add
260 return d += y.get_neg_offset(d);
263 private:
264 int_rep _y;
267 }} // namespace boost::date_time
269 #endif // DATE_DURATION_TYPES_HPP___