Imported Upstream version 1.64.0
[platform/upstream/boost.git] / boost / date_time / date_duration_types.hpp
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/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>
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>
23   class BOOST_SYMBOL_VISIBLE weeks_duration : public date_duration<duration_config> {
24   public:
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) {}
29   };
30
31   // predeclare
32   template<class t>
33   class BOOST_SYMBOL_VISIBLE years_duration;
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>
41   class BOOST_SYMBOL_VISIBLE months_duration
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:
52       months_duration(int_rep num) : _m(num) {}
53       months_duration(special_values sv) : _m(sv) 
54       {
55         _m = int_rep::from_special(sv);
56       }
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
60       {
61         month_adjustor_type m_adj(_m.as_number());
62         return duration_type(m_adj.get_neg_offset(d));
63       }
64       duration_type get_offset(const date_type& d) const
65       {
66         month_adjustor_type m_adj(_m.as_number());
67         return duration_type(m_adj.get_offset(d));
68       }
69       bool operator==(const months_type& rhs) const
70       {
71         return(_m == rhs._m);
72       }
73       bool operator!=(const months_type& rhs) const
74       {
75         return(_m != rhs._m);
76       }
77       months_type operator+(const months_type& rhs)const
78       {
79         return months_type(_m + rhs._m);
80       }
81       months_type& operator+=(const months_type& rhs)
82       {
83         _m = _m + rhs._m;
84         return *this;
85       }
86       months_type operator-(const months_type& rhs)const
87       {
88         return months_type(_m - rhs._m);
89       }
90       months_type& operator-=(const months_type& rhs)
91       {
92         _m = _m - rhs._m;
93         return *this;
94       }
95       months_type operator*(const int_type rhs)const
96       {
97         return months_type(_m * rhs);
98       }
99       months_type& operator*=(const int_type rhs)
100       {
101         _m = _m * rhs;
102         return *this;
103       }
104       months_type operator/(const int_type rhs)const
105       {
106         return months_type(_m / rhs);
107       }
108       months_type& operator/=(const int_type rhs)
109       {
110         _m = _m / rhs;
111         return *this;
112       }
113       months_type operator+(const years_type& y)const
114       {
115         return months_type(y.number_of_years() * 12 + _m);
116       }
117       months_type& operator+=(const years_type& y)
118       {
119         _m = y.number_of_years() * 12 + _m;
120         return *this;
121       }
122       months_type operator-(const years_type& y) const
123       {
124         return months_type(_m - y.number_of_years() * 12);
125       }
126       months_type& operator-=(const years_type& y)
127       {
128         _m = _m - y.number_of_years() * 12;
129         return *this;
130       }
131
132       //
133       friend date_type operator+(const date_type& d, const months_type& m)
134       {
135         return d + m.get_offset(d);
136       }
137       friend date_type operator+=(date_type& d, const months_type& m)
138       {
139         return d += m.get_offset(d);
140       }
141       friend date_type operator-(const date_type& d, const months_type& m)
142       {
143         // get_neg_offset returns a negative duration, so we add
144         return d + m.get_neg_offset(d);
145       }
146       friend date_type operator-=(date_type& d, const months_type& m)
147       {
148         // get_neg_offset returns a negative duration, so we add
149         return d += m.get_neg_offset(d);
150       }
151         
152     private:
153       int_rep _m;
154   };
155
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) ->
160    * 2004-Feb-29).
161    */
162   template<class base_config>
163   class BOOST_SYMBOL_VISIBLE years_duration
164   {
165     private:
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;
173     public:
174       years_duration(int_rep num) : _y(num) {}
175       years_duration(special_values sv) : _y(sv) 
176       {
177         _y = int_rep::from_special(sv);
178       }
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
182       {
183         month_adjustor_type m_adj(_y.as_number() * 12);
184         return duration_type(m_adj.get_neg_offset(d));
185       }
186       duration_type get_offset(const date_type& d) const
187       {
188         month_adjustor_type m_adj(_y.as_number() * 12);
189         return duration_type(m_adj.get_offset(d));
190       }
191       bool operator==(const years_type& rhs) const
192       {
193         return(_y == rhs._y);
194       }
195       bool operator!=(const years_type& rhs) const
196       {
197         return(_y != rhs._y);
198       }
199       years_type operator+(const years_type& rhs)const
200       {
201         return years_type(_y + rhs._y);
202       }
203       years_type& operator+=(const years_type& rhs)
204       {
205         _y = _y + rhs._y;
206         return *this;
207       }
208       years_type operator-(const years_type& rhs)const
209       {
210         return years_type(_y - rhs._y);
211       }
212       years_type& operator-=(const years_type& rhs)
213       {
214         _y = _y - rhs._y;
215         return *this;
216       }
217       years_type operator*(const int_type rhs)const
218       {
219         return years_type(_y * rhs);
220       }
221       years_type& operator*=(const int_type rhs)
222       {
223         _y = _y * rhs;
224         return *this;
225       }
226       years_type operator/(const int_type rhs)const
227       {
228         return years_type(_y / rhs);
229       }
230       years_type& operator/=(const int_type rhs)
231       {
232         _y = _y / rhs;
233         return *this;
234       }
235       months_type operator+(const months_type& m) const
236       {
237         return(months_type(_y * 12 + m.number_of_months()));
238       }
239       months_type operator-(const months_type& m) const
240       {
241         return(months_type(_y * 12 - m.number_of_months()));
242       }
243
244       //
245       friend date_type operator+(const date_type& d, const years_type& y)
246       {
247         return d + y.get_offset(d);
248       }
249       friend date_type operator+=(date_type& d, const years_type& y)
250       {
251         return d += y.get_offset(d);
252       }
253       friend date_type operator-(const date_type& d, const years_type& y)
254       {
255         // get_neg_offset returns a negative duration, so we add
256         return d + y.get_neg_offset(d);
257       }
258       friend date_type operator-=(date_type& d, const years_type& y)
259       {
260         // get_neg_offset returns a negative duration, so we add
261         return d += y.get_neg_offset(d);
262       }
263
264     private:
265       int_rep _y;
266   };
267
268 }} // namespace boost::date_time
269
270 #endif // DATE_DURATION_TYPES_HPP___