1 | #ifndef DATE_TIME_DATE_DURATION__
|
---|
2 | #define DATE_TIME_DATE_DURATION__
|
---|
3 |
|
---|
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 or http://www.boost.org/LICENSE-1.0)
|
---|
8 | * Author: Jeff Garland, Bart Garst
|
---|
9 | * $Date: 2003/12/03 01:56:13 $
|
---|
10 | */
|
---|
11 |
|
---|
12 |
|
---|
13 | #include <boost/operators.hpp>
|
---|
14 |
|
---|
15 | namespace boost {
|
---|
16 | namespace date_time {
|
---|
17 |
|
---|
18 |
|
---|
19 | //! Duration type with date level resolution
|
---|
20 | template<class duration_rep_traits>
|
---|
21 | class date_duration : private
|
---|
22 | boost::less_than_comparable<date_duration< duration_rep_traits>
|
---|
23 | , boost::equality_comparable< date_duration< duration_rep_traits>
|
---|
24 | , boost::addable< date_duration< duration_rep_traits>
|
---|
25 | , boost::subtractable< date_duration< duration_rep_traits>
|
---|
26 | > > > >
|
---|
27 | {
|
---|
28 | public:
|
---|
29 | typedef typename duration_rep_traits::int_type duration_rep_type;
|
---|
30 | typedef typename duration_rep_traits::impl_type duration_rep;
|
---|
31 |
|
---|
32 | //! Construct from a day count
|
---|
33 | explicit date_duration(duration_rep day_count) : days_(day_count) {};
|
---|
34 |
|
---|
35 | /*! construct from special_values - only works when
|
---|
36 | * instantiated with duration_traits_adapted */
|
---|
37 | date_duration(special_values sv) :
|
---|
38 | days_(duration_rep::from_special(sv))
|
---|
39 | {}
|
---|
40 |
|
---|
41 | // copy constructor required for addable<> & subtractable<>
|
---|
42 | //! Construct from another date_duration (Copy Constructor)
|
---|
43 | date_duration(const date_duration<duration_rep_traits>& other) :
|
---|
44 | days_(other.days_)
|
---|
45 | {}
|
---|
46 |
|
---|
47 | //! returns days_ as it's instantiated type - used for streaming
|
---|
48 | duration_rep get_rep()const
|
---|
49 | {
|
---|
50 | return days_;
|
---|
51 | }
|
---|
52 | bool is_special()const
|
---|
53 | {
|
---|
54 | return days_.is_special();
|
---|
55 | }
|
---|
56 | //! returns days as value, not object.
|
---|
57 | duration_rep_type days() const
|
---|
58 | {
|
---|
59 | return duration_rep_traits::as_number(days_);
|
---|
60 | }
|
---|
61 | //! Returns the smallest duration -- used by to calculate 'end'
|
---|
62 | static date_duration unit()
|
---|
63 | {
|
---|
64 | return date_duration<duration_rep_traits>(1);
|
---|
65 | }
|
---|
66 | //! Equality
|
---|
67 | bool operator==(const date_duration& rhs) const
|
---|
68 | {
|
---|
69 | return days_ == rhs.days_;
|
---|
70 | }
|
---|
71 | //! Less
|
---|
72 | bool operator<(const date_duration& rhs) const
|
---|
73 | {
|
---|
74 | return days_ < rhs.days_;
|
---|
75 | }
|
---|
76 |
|
---|
77 | /* For shortcut operators (+=, -=, etc) simply using
|
---|
78 | * "days_ += days_" may not work. If instantiated with
|
---|
79 | * an int_adapter, shortcut operators are not present,
|
---|
80 | * so this will not compile */
|
---|
81 |
|
---|
82 | //! Subtract another duration -- result is signed
|
---|
83 | date_duration operator-=(const date_duration& rhs)
|
---|
84 | {
|
---|
85 | //days_ -= rhs.days_;
|
---|
86 | days_ = days_ - rhs.days_;
|
---|
87 | return *this;
|
---|
88 | }
|
---|
89 | //! Add a duration -- result is signed
|
---|
90 | date_duration operator+=(const date_duration& rhs)
|
---|
91 | {
|
---|
92 | days_ = days_ + rhs.days_;
|
---|
93 | return *this;
|
---|
94 | }
|
---|
95 |
|
---|
96 | //! unary- Allows for dd = -date_duration(2); -> dd == -2
|
---|
97 | date_duration operator-()const
|
---|
98 | {
|
---|
99 | return date_duration<duration_rep_traits>(get_rep() * (-1));
|
---|
100 | }
|
---|
101 | //! Division operations on a duration with an integer.
|
---|
102 | date_duration<duration_rep_traits> operator/=(int divisor)
|
---|
103 | {
|
---|
104 | days_ = days_ / divisor;
|
---|
105 | return *this;
|
---|
106 | }
|
---|
107 | date_duration<duration_rep_traits> operator/(int divisor)
|
---|
108 | {
|
---|
109 | return date_duration<duration_rep_traits>(days_ / divisor);
|
---|
110 | }
|
---|
111 |
|
---|
112 | //! return sign information
|
---|
113 | bool is_negative() const
|
---|
114 | {
|
---|
115 | return days_ < 0;
|
---|
116 | }
|
---|
117 | private:
|
---|
118 | duration_rep days_;
|
---|
119 | };
|
---|
120 |
|
---|
121 |
|
---|
122 | /*! Struct for instantiating date_duration with <b>NO</b> special values
|
---|
123 | * functionality. Allows for transparent implementation of either
|
---|
124 | * date_duration<long> or date_duration<int_adapter<long> > */
|
---|
125 | struct duration_traits_long
|
---|
126 | {
|
---|
127 | typedef long int_type;
|
---|
128 | typedef long impl_type;
|
---|
129 | static int_type as_number(impl_type i) { return i; };
|
---|
130 | };
|
---|
131 |
|
---|
132 | /*! Struct for instantiating date_duration <b>WITH</b> special values
|
---|
133 | * functionality. Allows for transparent implementation of either
|
---|
134 | * date_duration<long> or date_duration<int_adapter<long> > */
|
---|
135 | struct duration_traits_adapted
|
---|
136 | {
|
---|
137 | typedef long int_type;
|
---|
138 | typedef boost::date_time::int_adapter<long> impl_type;
|
---|
139 | static int_type as_number(impl_type i) { return i.as_number(); };
|
---|
140 | };
|
---|
141 |
|
---|
142 |
|
---|
143 | } } //namspace date_time
|
---|
144 |
|
---|
145 |
|
---|
146 | #endif
|
---|
147 |
|
---|