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$ |
10 | */ |
11 | |
12 | |
13 | #include <boost/operators.hpp> |
14 | #include <boost/date_time/special_defs.hpp> |
15 | #include <boost/date_time/compiler_config.hpp> |
16 | |
17 | namespace boost { |
18 | namespace date_time { |
19 | |
20 | |
21 | //! Duration type with date level resolution |
22 | template<class duration_rep_traits> |
23 | class BOOST_SYMBOL_VISIBLE date_duration : private |
24 | boost::less_than_comparable1< date_duration< duration_rep_traits > |
25 | , boost::equality_comparable1< date_duration< duration_rep_traits > |
26 | , boost::addable1< date_duration< duration_rep_traits > |
27 | , boost::subtractable1< date_duration< duration_rep_traits > |
28 | , boost::dividable2< date_duration< duration_rep_traits >, int |
29 | > > > > > |
30 | { |
31 | public: |
32 | typedef typename duration_rep_traits::int_type duration_rep_type; |
33 | typedef typename duration_rep_traits::impl_type duration_rep; |
34 | |
35 | //! Construct from a day count |
36 | explicit date_duration(duration_rep day_count) : days_(day_count) {} |
37 | |
38 | /*! construct from special_values - only works when |
39 | * instantiated with duration_traits_adapted */ |
40 | date_duration(special_values sv) : |
41 | days_(duration_rep::from_special(sv)) |
42 | {} |
43 | |
44 | // copy constructor required for addable<> & subtractable<> |
45 | //! Construct from another date_duration (Copy Constructor) |
46 | date_duration(const date_duration<duration_rep_traits>& other) : |
47 | days_(other.days_) |
48 | {} |
49 | |
50 | //! returns days_ as it's instantiated type - used for streaming |
51 | duration_rep get_rep()const |
52 | { |
53 | return days_; |
54 | } |
55 | bool is_special()const |
56 | { |
57 | return days_.is_special(); |
58 | } |
59 | //! returns days as value, not object. |
60 | duration_rep_type days() const |
61 | { |
62 | return duration_rep_traits::as_number(days_); |
63 | } |
64 | //! Returns the smallest duration -- used by to calculate 'end' |
65 | static date_duration unit() |
66 | { |
67 | return date_duration<duration_rep_traits>(1); |
68 | } |
69 | //! Equality |
70 | bool operator==(const date_duration& rhs) const |
71 | { |
72 | return days_ == rhs.days_; |
73 | } |
74 | //! Less |
75 | bool operator<(const date_duration& rhs) const |
76 | { |
77 | return days_ < rhs.days_; |
78 | } |
79 | |
80 | /* For shortcut operators (+=, -=, etc) simply using |
81 | * "days_ += days_" may not work. If instantiated with |
82 | * an int_adapter, shortcut operators are not present, |
83 | * so this will not compile */ |
84 | |
85 | //! Subtract another duration -- result is signed |
86 | date_duration& operator-=(const date_duration& rhs) |
87 | { |
88 | //days_ -= rhs.days_; |
89 | days_ = days_ - rhs.days_; |
90 | return *this; |
91 | } |
92 | //! Add a duration -- result is signed |
93 | date_duration& operator+=(const date_duration& rhs) |
94 | { |
95 | days_ = days_ + rhs.days_; |
96 | return *this; |
97 | } |
98 | |
99 | //! unary- Allows for dd = -date_duration(2); -> dd == -2 |
100 | date_duration operator-() const |
101 | { |
102 | return date_duration<duration_rep_traits>(get_rep() * (-1)); |
103 | } |
104 | //! Division operations on a duration with an integer. |
105 | date_duration& operator/=(int divisor) |
106 | { |
107 | days_ = days_ / divisor; |
108 | return *this; |
109 | } |
110 | |
111 | //! return sign information |
112 | bool is_negative() const |
113 | { |
114 | return days_ < 0; |
115 | } |
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 BOOST_SYMBOL_VISIBLE 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 BOOST_SYMBOL_VISIBLE 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 | |
148 | |