1 #ifndef DATE_TIME_DATE_DURATION__
2 #define DATE_TIME_DATE_DURATION__
4 /* Copyright (c) 2002,2003 CrystalClear Software, Inc.
5 * Use, modification and distribution is subject to the
6 * Boost Software License, Version 1.0. (See accompanying
7 * file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
8 * Author: Jeff Garland, Bart Garst
13 #include <boost/operators.hpp>
14 #include <boost/date_time/special_defs.hpp>
20 //! Duration type with date level resolution
21 template<class duration_rep_traits
>
22 class date_duration
: private
23 boost::less_than_comparable1
< date_duration
< duration_rep_traits
>
24 , boost::equality_comparable1
< date_duration
< duration_rep_traits
>
25 , boost::addable1
< date_duration
< duration_rep_traits
>
26 , boost::subtractable1
< date_duration
< duration_rep_traits
>
27 , boost::dividable2
< date_duration
< duration_rep_traits
>, int
31 typedef typename
duration_rep_traits::int_type duration_rep_type
;
32 typedef typename
duration_rep_traits::impl_type duration_rep
;
34 //! Construct from a day count
35 explicit date_duration(duration_rep day_count
) : days_(day_count
) {};
37 /*! construct from special_values - only works when
38 * instantiated with duration_traits_adapted */
39 date_duration(special_values sv
) :
40 days_(duration_rep::from_special(sv
))
43 // copy constructor required for addable<> & subtractable<>
44 //! Construct from another date_duration (Copy Constructor)
45 date_duration(const date_duration
<duration_rep_traits
>& other
) :
49 //! returns days_ as it's instantiated type - used for streaming
50 duration_rep
get_rep()const
54 bool is_special()const
56 return days_
.is_special();
58 //! returns days as value, not object.
59 duration_rep_type
days() const
61 return duration_rep_traits::as_number(days_
);
63 //! Returns the smallest duration -- used by to calculate 'end'
64 static date_duration
unit()
66 return date_duration
<duration_rep_traits
>(1);
69 bool operator==(const date_duration
& rhs
) const
71 return days_
== rhs
.days_
;
74 bool operator<(const date_duration
& rhs
) const
76 return days_
< rhs
.days_
;
79 /* For shortcut operators (+=, -=, etc) simply using
80 * "days_ += days_" may not work. If instantiated with
81 * an int_adapter, shortcut operators are not present,
82 * so this will not compile */
84 //! Subtract another duration -- result is signed
85 date_duration
& operator-=(const date_duration
& rhs
)
88 days_
= days_
- rhs
.days_
;
91 //! Add a duration -- result is signed
92 date_duration
& operator+=(const date_duration
& rhs
)
94 days_
= days_
+ rhs
.days_
;
98 //! unary- Allows for dd = -date_duration(2); -> dd == -2
99 date_duration
operator-() const
101 return date_duration
<duration_rep_traits
>(get_rep() * (-1));
103 //! Division operations on a duration with an integer.
104 date_duration
& operator/=(int divisor
)
106 days_
= days_
/ divisor
;
110 //! return sign information
111 bool is_negative() const
121 /*! Struct for instantiating date_duration with <b>NO</b> special values
122 * functionality. Allows for transparent implementation of either
123 * date_duration<long> or date_duration<int_adapter<long> > */
124 struct duration_traits_long
126 typedef long int_type
;
127 typedef long impl_type
;
128 static int_type
as_number(impl_type i
) { return i
; };
131 /*! Struct for instantiating date_duration <b>WITH</b> special values
132 * functionality. Allows for transparent implementation of either
133 * date_duration<long> or date_duration<int_adapter<long> > */
134 struct duration_traits_adapted
136 typedef long int_type
;
137 typedef boost::date_time::int_adapter
<long> impl_type
;
138 static int_type
as_number(impl_type i
) { return i
.as_number(); };
142 } } //namspace date_time