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.txt or http://www.boost.org/LICENSE_1_0.txt) |
---|
8 | * Author: Jeff Garland, Bart Garst |
---|
9 | * $Date: 2008-11-12 14:37:53 -0500 (Wed, 12 Nov 2008) $ |
---|
10 | */ |
---|
11 | |
---|
12 | |
---|
13 | #include <boost/operators.hpp> |
---|
14 | #include <boost/date_time/special_defs.hpp> |
---|
15 | |
---|
16 | namespace boost { |
---|
17 | namespace date_time { |
---|
18 | |
---|
19 | |
---|
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 |
---|
28 | > > > > > |
---|
29 | { |
---|
30 | public: |
---|
31 | typedef typename duration_rep_traits::int_type duration_rep_type; |
---|
32 | typedef typename duration_rep_traits::impl_type duration_rep; |
---|
33 | |
---|
34 | //! Construct from a day count |
---|
35 | explicit date_duration(duration_rep day_count) : days_(day_count) {}; |
---|
36 | |
---|
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)) |
---|
41 | {} |
---|
42 | |
---|
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) : |
---|
46 | days_(other.days_) |
---|
47 | {} |
---|
48 | |
---|
49 | //! returns days_ as it's instantiated type - used for streaming |
---|
50 | duration_rep get_rep()const |
---|
51 | { |
---|
52 | return days_; |
---|
53 | } |
---|
54 | bool is_special()const |
---|
55 | { |
---|
56 | return days_.is_special(); |
---|
57 | } |
---|
58 | //! returns days as value, not object. |
---|
59 | duration_rep_type days() const |
---|
60 | { |
---|
61 | return duration_rep_traits::as_number(days_); |
---|
62 | } |
---|
63 | //! Returns the smallest duration -- used by to calculate 'end' |
---|
64 | static date_duration unit() |
---|
65 | { |
---|
66 | return date_duration<duration_rep_traits>(1); |
---|
67 | } |
---|
68 | //! Equality |
---|
69 | bool operator==(const date_duration& rhs) const |
---|
70 | { |
---|
71 | return days_ == rhs.days_; |
---|
72 | } |
---|
73 | //! Less |
---|
74 | bool operator<(const date_duration& rhs) const |
---|
75 | { |
---|
76 | return days_ < rhs.days_; |
---|
77 | } |
---|
78 | |
---|
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 */ |
---|
83 | |
---|
84 | //! Subtract another duration -- result is signed |
---|
85 | date_duration& operator-=(const date_duration& rhs) |
---|
86 | { |
---|
87 | //days_ -= rhs.days_; |
---|
88 | days_ = days_ - rhs.days_; |
---|
89 | return *this; |
---|
90 | } |
---|
91 | //! Add a duration -- result is signed |
---|
92 | date_duration& operator+=(const date_duration& rhs) |
---|
93 | { |
---|
94 | days_ = days_ + rhs.days_; |
---|
95 | return *this; |
---|
96 | } |
---|
97 | |
---|
98 | //! unary- Allows for dd = -date_duration(2); -> dd == -2 |
---|
99 | date_duration operator-() const |
---|
100 | { |
---|
101 | return date_duration<duration_rep_traits>(get_rep() * (-1)); |
---|
102 | } |
---|
103 | //! Division operations on a duration with an integer. |
---|
104 | date_duration& operator/=(int divisor) |
---|
105 | { |
---|
106 | days_ = days_ / divisor; |
---|
107 | return *this; |
---|
108 | } |
---|
109 | |
---|
110 | //! return sign information |
---|
111 | bool is_negative() const |
---|
112 | { |
---|
113 | return days_ < 0; |
---|
114 | } |
---|
115 | |
---|
116 | private: |
---|
117 | duration_rep days_; |
---|
118 | }; |
---|
119 | |
---|
120 | |
---|
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 |
---|
125 | { |
---|
126 | typedef long int_type; |
---|
127 | typedef long impl_type; |
---|
128 | static int_type as_number(impl_type i) { return i; }; |
---|
129 | }; |
---|
130 | |
---|
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 |
---|
135 | { |
---|
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(); }; |
---|
139 | }; |
---|
140 | |
---|
141 | |
---|
142 | } } //namspace date_time |
---|
143 | |
---|
144 | |
---|
145 | #endif |
---|
146 | |
---|