]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
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 | */ | |
b32b8144 FG |
11 | |
12 | #include <boost/date_time/compiler_config.hpp> | |
7c673cae FG |
13 | #include <boost/date_time/int_adapter.hpp> |
14 | #include <boost/date_time/special_defs.hpp> | |
15 | #include <boost/date_time/date_duration.hpp> | |
16 | ||
17 | namespace boost { | |
18 | namespace date_time { | |
19 | ||
20 | ||
21 | //! Additional duration type that represents a number of n*7 days | |
22 | template <class duration_config> | |
b32b8144 | 23 | class BOOST_SYMBOL_VISIBLE weeks_duration : public date_duration<duration_config> { |
7c673cae | 24 | public: |
f67539c2 | 25 | BOOST_CXX14_CONSTEXPR weeks_duration(typename duration_config::impl_type w) |
7c673cae | 26 | : date_duration<duration_config>(w * 7) {} |
f67539c2 | 27 | BOOST_CXX14_CONSTEXPR weeks_duration(special_values sv) |
7c673cae FG |
28 | : date_duration<duration_config>(sv) {} |
29 | }; | |
30 | ||
31 | // predeclare | |
32 | template<class t> | |
b32b8144 | 33 | class BOOST_SYMBOL_VISIBLE years_duration; |
7c673cae FG |
34 | |
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. | |
39 | */ | |
40 | template<class base_config> | |
b32b8144 | 41 | class BOOST_SYMBOL_VISIBLE months_duration |
7c673cae FG |
42 | { |
43 | private: | |
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; | |
51 | public: | |
f67539c2 TL |
52 | BOOST_CXX14_CONSTEXPR months_duration(int_rep num) : _m(num) {} |
53 | BOOST_CXX14_CONSTEXPR months_duration(special_values sv) : _m(sv) | |
7c673cae FG |
54 | { |
55 | _m = int_rep::from_special(sv); | |
56 | } | |
57 | int_rep number_of_months() const { return _m; } | |
58 | //! returns a negative duration | |
f67539c2 | 59 | BOOST_CXX14_CONSTEXPR duration_type get_neg_offset(const date_type& d) const |
7c673cae FG |
60 | { |
61 | month_adjustor_type m_adj(_m.as_number()); | |
62 | return duration_type(m_adj.get_neg_offset(d)); | |
63 | } | |
f67539c2 | 64 | BOOST_CXX14_CONSTEXPR duration_type get_offset(const date_type& d) const |
7c673cae FG |
65 | { |
66 | month_adjustor_type m_adj(_m.as_number()); | |
67 | return duration_type(m_adj.get_offset(d)); | |
68 | } | |
f67539c2 | 69 | BOOST_CONSTEXPR bool operator==(const months_type& rhs) const |
7c673cae FG |
70 | { |
71 | return(_m == rhs._m); | |
72 | } | |
f67539c2 | 73 | BOOST_CONSTEXPR bool operator!=(const months_type& rhs) const |
7c673cae FG |
74 | { |
75 | return(_m != rhs._m); | |
76 | } | |
f67539c2 | 77 | BOOST_CXX14_CONSTEXPR months_type operator+(const months_type& rhs)const |
7c673cae FG |
78 | { |
79 | return months_type(_m + rhs._m); | |
80 | } | |
f67539c2 | 81 | BOOST_CXX14_CONSTEXPR months_type& operator+=(const months_type& rhs) |
7c673cae FG |
82 | { |
83 | _m = _m + rhs._m; | |
84 | return *this; | |
85 | } | |
f67539c2 | 86 | BOOST_CXX14_CONSTEXPR months_type operator-(const months_type& rhs)const |
7c673cae FG |
87 | { |
88 | return months_type(_m - rhs._m); | |
89 | } | |
f67539c2 | 90 | BOOST_CXX14_CONSTEXPR months_type& operator-=(const months_type& rhs) |
7c673cae FG |
91 | { |
92 | _m = _m - rhs._m; | |
93 | return *this; | |
94 | } | |
f67539c2 | 95 | BOOST_CXX14_CONSTEXPR months_type operator*(const int_type rhs)const |
7c673cae FG |
96 | { |
97 | return months_type(_m * rhs); | |
98 | } | |
f67539c2 | 99 | BOOST_CXX14_CONSTEXPR months_type& operator*=(const int_type rhs) |
7c673cae FG |
100 | { |
101 | _m = _m * rhs; | |
102 | return *this; | |
103 | } | |
f67539c2 | 104 | BOOST_CXX14_CONSTEXPR months_type operator/(const int_type rhs)const |
7c673cae FG |
105 | { |
106 | return months_type(_m / rhs); | |
107 | } | |
f67539c2 | 108 | BOOST_CXX14_CONSTEXPR months_type& operator/=(const int_type rhs) |
7c673cae FG |
109 | { |
110 | _m = _m / rhs; | |
111 | return *this; | |
112 | } | |
f67539c2 | 113 | BOOST_CXX14_CONSTEXPR months_type operator+(const years_type& y)const |
7c673cae FG |
114 | { |
115 | return months_type(y.number_of_years() * 12 + _m); | |
116 | } | |
f67539c2 | 117 | BOOST_CXX14_CONSTEXPR months_type& operator+=(const years_type& y) |
7c673cae FG |
118 | { |
119 | _m = y.number_of_years() * 12 + _m; | |
120 | return *this; | |
121 | } | |
f67539c2 | 122 | BOOST_CXX14_CONSTEXPR months_type operator-(const years_type& y) const |
7c673cae FG |
123 | { |
124 | return months_type(_m - y.number_of_years() * 12); | |
125 | } | |
f67539c2 | 126 | BOOST_CXX14_CONSTEXPR months_type& operator-=(const years_type& y) |
7c673cae FG |
127 | { |
128 | _m = _m - y.number_of_years() * 12; | |
129 | return *this; | |
130 | } | |
7c673cae | 131 | // |
f67539c2 | 132 | BOOST_CXX14_CONSTEXPR friend date_type operator+(const date_type& d, const months_type& m) |
7c673cae FG |
133 | { |
134 | return d + m.get_offset(d); | |
135 | } | |
f67539c2 | 136 | BOOST_CXX14_CONSTEXPR friend date_type operator+=(date_type& d, const months_type& m) |
7c673cae FG |
137 | { |
138 | return d += m.get_offset(d); | |
139 | } | |
f67539c2 | 140 | BOOST_CXX14_CONSTEXPR friend date_type operator-(const date_type& d, const months_type& m) |
7c673cae FG |
141 | { |
142 | // get_neg_offset returns a negative duration, so we add | |
143 | return d + m.get_neg_offset(d); | |
144 | } | |
f67539c2 | 145 | BOOST_CXX14_CONSTEXPR friend date_type operator-=(date_type& d, const months_type& m) |
7c673cae FG |
146 | { |
147 | // get_neg_offset returns a negative duration, so we add | |
148 | return d += m.get_neg_offset(d); | |
149 | } | |
7c673cae FG |
150 | private: |
151 | int_rep _m; | |
152 | }; | |
153 | ||
154 | //! additional duration type that represents a logical year | |
155 | /*! A logical year enables things like: "date(2002,Mar,2) + years(2) -> | |
156 | * 2004-Mar-2". If the date is a last day-of-the-month, the result will | |
157 | * also be a last-day-of-the-month (ie date(2001-Feb-28) + years(3) -> | |
158 | * 2004-Feb-29). | |
159 | */ | |
160 | template<class base_config> | |
b32b8144 | 161 | class BOOST_SYMBOL_VISIBLE years_duration |
7c673cae FG |
162 | { |
163 | private: | |
164 | typedef typename base_config::int_rep int_rep; | |
165 | typedef typename int_rep::int_type int_type; | |
166 | typedef typename base_config::date_type date_type; | |
167 | typedef typename date_type::duration_type duration_type; | |
168 | typedef typename base_config::month_adjustor_type month_adjustor_type; | |
169 | typedef years_duration<base_config> years_type; | |
170 | typedef months_duration<base_config> months_type; | |
171 | public: | |
f67539c2 TL |
172 | BOOST_CXX14_CONSTEXPR years_duration(int_rep num) : _y(num) {} |
173 | BOOST_CXX14_CONSTEXPR years_duration(special_values sv) : _y(sv) | |
7c673cae FG |
174 | { |
175 | _y = int_rep::from_special(sv); | |
176 | } | |
f67539c2 | 177 | BOOST_CXX14_CONSTEXPR int_rep number_of_years() const { return _y; } |
7c673cae | 178 | //! returns a negative duration |
f67539c2 | 179 | BOOST_CXX14_CONSTEXPR duration_type get_neg_offset(const date_type& d) const |
7c673cae FG |
180 | { |
181 | month_adjustor_type m_adj(_y.as_number() * 12); | |
182 | return duration_type(m_adj.get_neg_offset(d)); | |
183 | } | |
f67539c2 | 184 | BOOST_CXX14_CONSTEXPR duration_type get_offset(const date_type& d) const |
7c673cae FG |
185 | { |
186 | month_adjustor_type m_adj(_y.as_number() * 12); | |
187 | return duration_type(m_adj.get_offset(d)); | |
188 | } | |
f67539c2 | 189 | BOOST_CXX14_CONSTEXPR bool operator==(const years_type& rhs) const |
7c673cae FG |
190 | { |
191 | return(_y == rhs._y); | |
192 | } | |
193 | bool operator!=(const years_type& rhs) const | |
194 | { | |
195 | return(_y != rhs._y); | |
196 | } | |
f67539c2 | 197 | BOOST_CXX14_CONSTEXPR years_type operator+(const years_type& rhs)const |
7c673cae FG |
198 | { |
199 | return years_type(_y + rhs._y); | |
200 | } | |
f67539c2 | 201 | BOOST_CXX14_CONSTEXPR years_type& operator+=(const years_type& rhs) |
7c673cae FG |
202 | { |
203 | _y = _y + rhs._y; | |
204 | return *this; | |
205 | } | |
f67539c2 | 206 | BOOST_CXX14_CONSTEXPR years_type operator-(const years_type& rhs)const |
7c673cae FG |
207 | { |
208 | return years_type(_y - rhs._y); | |
209 | } | |
f67539c2 | 210 | BOOST_CXX14_CONSTEXPR years_type& operator-=(const years_type& rhs) |
7c673cae FG |
211 | { |
212 | _y = _y - rhs._y; | |
213 | return *this; | |
214 | } | |
f67539c2 | 215 | BOOST_CXX14_CONSTEXPR years_type operator*(const int_type rhs)const |
7c673cae FG |
216 | { |
217 | return years_type(_y * rhs); | |
218 | } | |
f67539c2 | 219 | BOOST_CXX14_CONSTEXPR years_type& operator*=(const int_type rhs) |
7c673cae FG |
220 | { |
221 | _y = _y * rhs; | |
222 | return *this; | |
223 | } | |
f67539c2 | 224 | BOOST_CXX14_CONSTEXPR years_type operator/(const int_type rhs)const |
7c673cae FG |
225 | { |
226 | return years_type(_y / rhs); | |
227 | } | |
f67539c2 | 228 | BOOST_CXX14_CONSTEXPR years_type& operator/=(const int_type rhs) |
7c673cae FG |
229 | { |
230 | _y = _y / rhs; | |
231 | return *this; | |
232 | } | |
f67539c2 | 233 | BOOST_CXX14_CONSTEXPR months_type operator+(const months_type& m) const |
7c673cae FG |
234 | { |
235 | return(months_type(_y * 12 + m.number_of_months())); | |
236 | } | |
f67539c2 | 237 | BOOST_CXX14_CONSTEXPR months_type operator-(const months_type& m) const |
7c673cae FG |
238 | { |
239 | return(months_type(_y * 12 - m.number_of_months())); | |
240 | } | |
7c673cae | 241 | // |
f67539c2 | 242 | BOOST_CXX14_CONSTEXPR friend date_type operator+(const date_type& d, const years_type& y) |
7c673cae FG |
243 | { |
244 | return d + y.get_offset(d); | |
245 | } | |
f67539c2 | 246 | BOOST_CXX14_CONSTEXPR friend date_type operator+=(date_type& d, const years_type& y) |
7c673cae FG |
247 | { |
248 | return d += y.get_offset(d); | |
249 | } | |
f67539c2 | 250 | BOOST_CXX14_CONSTEXPR friend date_type operator-(const date_type& d, const years_type& y) |
7c673cae FG |
251 | { |
252 | // get_neg_offset returns a negative duration, so we add | |
253 | return d + y.get_neg_offset(d); | |
254 | } | |
f67539c2 | 255 | BOOST_CXX14_CONSTEXPR friend date_type operator-=(date_type& d, const years_type& y) |
7c673cae FG |
256 | { |
257 | // get_neg_offset returns a negative duration, so we add | |
258 | return d += y.get_neg_offset(d); | |
259 | } | |
7c673cae FG |
260 | private: |
261 | int_rep _y; | |
262 | }; | |
7c673cae FG |
263 | }} // namespace boost::date_time |
264 | ||
265 | #endif // DATE_DURATION_TYPES_HPP___ |