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 or |
---|
7 | * http://www.boost.org/LICENSE-1.0) |
---|
8 | * Author: Jeff Garland, Bart Garst |
---|
9 | * $Date: 2004/07/17 22:20:18 $ |
---|
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: |
---|
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) {} |
---|
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: |
---|
51 | months_duration(int_rep num) : _m(num) {} |
---|
52 | months_duration(special_values sv) : _m(sv) |
---|
53 | { |
---|
54 | _m = int_rep::from_special(sv); |
---|
55 | } |
---|
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 |
---|
59 | { |
---|
60 | month_adjustor_type m_adj(_m.as_number()); |
---|
61 | return duration_type(m_adj.get_neg_offset(d)); |
---|
62 | } |
---|
63 | 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 | } |
---|
68 | bool operator==(const months_type& rhs) const |
---|
69 | { |
---|
70 | return(_m == rhs._m); |
---|
71 | } |
---|
72 | bool operator!=(const months_type& rhs) const |
---|
73 | { |
---|
74 | return(_m != rhs._m); |
---|
75 | } |
---|
76 | months_type operator+(const months_type& rhs)const |
---|
77 | { |
---|
78 | return months_type(_m + rhs._m); |
---|
79 | } |
---|
80 | months_type& operator+=(const months_type& rhs) |
---|
81 | { |
---|
82 | _m = _m + rhs._m; |
---|
83 | return *this; |
---|
84 | } |
---|
85 | months_type operator-(const months_type& rhs)const |
---|
86 | { |
---|
87 | return months_type(_m - rhs._m); |
---|
88 | } |
---|
89 | months_type& operator-=(const months_type& rhs) |
---|
90 | { |
---|
91 | _m = _m - rhs._m; |
---|
92 | return *this; |
---|
93 | } |
---|
94 | months_type operator*(const int_type rhs)const |
---|
95 | { |
---|
96 | return months_type(_m * rhs); |
---|
97 | } |
---|
98 | months_type& operator*=(const int_type rhs) |
---|
99 | { |
---|
100 | _m = _m * rhs; |
---|
101 | return *this; |
---|
102 | } |
---|
103 | months_type operator/(const int_type rhs)const |
---|
104 | { |
---|
105 | return months_type(_m / rhs); |
---|
106 | } |
---|
107 | months_type& operator/=(const int_type rhs) |
---|
108 | { |
---|
109 | _m = _m / rhs; |
---|
110 | return *this; |
---|
111 | } |
---|
112 | months_type operator+(const years_type& y)const |
---|
113 | { |
---|
114 | return months_type(y.number_of_years() * 12 + _m); |
---|
115 | } |
---|
116 | months_type& operator+=(const years_type& y) |
---|
117 | { |
---|
118 | _m = y.number_of_years() * 12 + _m; |
---|
119 | return *this; |
---|
120 | } |
---|
121 | months_type operator-(const years_type& y) const |
---|
122 | { |
---|
123 | return months_type(_m - y.number_of_years() * 12); |
---|
124 | } |
---|
125 | months_type& operator-=(const years_type& y) |
---|
126 | { |
---|
127 | _m = _m - y.number_of_years() * 12; |
---|
128 | return *this; |
---|
129 | } |
---|
130 | |
---|
131 | // |
---|
132 | friend date_type operator+(const date_type& d, const months_type& m) |
---|
133 | { |
---|
134 | return d + m.get_offset(d); |
---|
135 | } |
---|
136 | friend date_type operator+=(date_type& d, const months_type& m) |
---|
137 | { |
---|
138 | return d += m.get_offset(d); |
---|
139 | } |
---|
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 | } |
---|
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: |
---|
173 | years_duration(int_rep num) : _y(num) {} |
---|
174 | years_duration(special_values sv) : _y(sv) |
---|
175 | { |
---|
176 | _y = int_rep::from_special(sv); |
---|
177 | } |
---|
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 |
---|
181 | { |
---|
182 | month_adjustor_type m_adj(_y.as_number() * 12); |
---|
183 | return duration_type(m_adj.get_neg_offset(d)); |
---|
184 | } |
---|
185 | 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 | } |
---|
190 | bool operator==(const years_type& rhs) const |
---|
191 | { |
---|
192 | return(_y == rhs._y); |
---|
193 | } |
---|
194 | bool operator!=(const years_type& rhs) const |
---|
195 | { |
---|
196 | return(_y != rhs._y); |
---|
197 | } |
---|
198 | years_type operator+(const years_type& rhs)const |
---|
199 | { |
---|
200 | return years_type(_y + rhs._y); |
---|
201 | } |
---|
202 | years_type& operator+=(const years_type& rhs) |
---|
203 | { |
---|
204 | _y = _y + rhs._y; |
---|
205 | return *this; |
---|
206 | } |
---|
207 | years_type operator-(const years_type& rhs)const |
---|
208 | { |
---|
209 | return years_type(_y - rhs._y); |
---|
210 | } |
---|
211 | years_type& operator-=(const years_type& rhs) |
---|
212 | { |
---|
213 | _y = _y - rhs._y; |
---|
214 | return *this; |
---|
215 | } |
---|
216 | years_type operator*(const int_type rhs)const |
---|
217 | { |
---|
218 | return years_type(_y * rhs); |
---|
219 | } |
---|
220 | years_type& operator*=(const int_type rhs) |
---|
221 | { |
---|
222 | _y = _y * rhs; |
---|
223 | return *this; |
---|
224 | } |
---|
225 | years_type operator/(const int_type rhs)const |
---|
226 | { |
---|
227 | return years_type(_y / rhs); |
---|
228 | } |
---|
229 | years_type& operator/=(const int_type rhs) |
---|
230 | { |
---|
231 | _y = _y / rhs; |
---|
232 | return *this; |
---|
233 | } |
---|
234 | months_type operator+(const months_type& m) const |
---|
235 | { |
---|
236 | return(months_type(_y * 12 + m.number_of_months())); |
---|
237 | } |
---|
238 | months_type operator-(const months_type& m) const |
---|
239 | { |
---|
240 | return(months_type(_y * 12 - m.number_of_months())); |
---|
241 | } |
---|
242 | |
---|
243 | // |
---|
244 | friend date_type operator+(const date_type& d, const years_type& y) |
---|
245 | { |
---|
246 | return d + y.get_offset(d); |
---|
247 | } |
---|
248 | friend date_type operator+=(date_type& d, const years_type& y) |
---|
249 | { |
---|
250 | return d += y.get_offset(d); |
---|
251 | } |
---|
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 | } |
---|
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___ |
---|