]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
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> | |
b32b8144 | 15 | #include <boost/date_time/compiler_config.hpp> |
7c673cae FG |
16 | |
17 | namespace boost { | |
18 | namespace date_time { | |
19 | ||
20 | ||
21 | //! Duration type with date level resolution | |
22 | template<class duration_rep_traits> | |
b32b8144 | 23 | class BOOST_SYMBOL_VISIBLE date_duration : private |
7c673cae FG |
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 | } | |
11fdf7f2 TL |
55 | special_values as_special() const |
56 | { | |
57 | return days_.as_special(); | |
58 | } | |
7c673cae FG |
59 | bool is_special()const |
60 | { | |
61 | return days_.is_special(); | |
62 | } | |
63 | //! returns days as value, not object. | |
64 | duration_rep_type days() const | |
65 | { | |
66 | return duration_rep_traits::as_number(days_); | |
67 | } | |
68 | //! Returns the smallest duration -- used by to calculate 'end' | |
69 | static date_duration unit() | |
70 | { | |
71 | return date_duration<duration_rep_traits>(1); | |
72 | } | |
73 | //! Equality | |
74 | bool operator==(const date_duration& rhs) const | |
75 | { | |
76 | return days_ == rhs.days_; | |
77 | } | |
78 | //! Less | |
79 | bool operator<(const date_duration& rhs) const | |
80 | { | |
81 | return days_ < rhs.days_; | |
82 | } | |
83 | ||
84 | /* For shortcut operators (+=, -=, etc) simply using | |
85 | * "days_ += days_" may not work. If instantiated with | |
86 | * an int_adapter, shortcut operators are not present, | |
87 | * so this will not compile */ | |
88 | ||
89 | //! Subtract another duration -- result is signed | |
90 | date_duration& operator-=(const date_duration& rhs) | |
91 | { | |
92 | //days_ -= rhs.days_; | |
93 | days_ = days_ - rhs.days_; | |
94 | return *this; | |
95 | } | |
96 | //! Add a duration -- result is signed | |
97 | date_duration& operator+=(const date_duration& rhs) | |
98 | { | |
99 | days_ = days_ + rhs.days_; | |
100 | return *this; | |
101 | } | |
102 | ||
103 | //! unary- Allows for dd = -date_duration(2); -> dd == -2 | |
104 | date_duration operator-() const | |
105 | { | |
106 | return date_duration<duration_rep_traits>(get_rep() * (-1)); | |
107 | } | |
108 | //! Division operations on a duration with an integer. | |
109 | date_duration& operator/=(int divisor) | |
110 | { | |
111 | days_ = days_ / divisor; | |
112 | return *this; | |
113 | } | |
114 | ||
115 | //! return sign information | |
116 | bool is_negative() const | |
117 | { | |
118 | return days_ < 0; | |
119 | } | |
120 | ||
121 | private: | |
122 | duration_rep days_; | |
123 | }; | |
124 | ||
125 | ||
126 | /*! Struct for instantiating date_duration with <b>NO</b> special values | |
127 | * functionality. Allows for transparent implementation of either | |
128 | * date_duration<long> or date_duration<int_adapter<long> > */ | |
b32b8144 | 129 | struct BOOST_SYMBOL_VISIBLE duration_traits_long |
7c673cae FG |
130 | { |
131 | typedef long int_type; | |
132 | typedef long impl_type; | |
133 | static int_type as_number(impl_type i) { return i; } | |
134 | }; | |
135 | ||
136 | /*! Struct for instantiating date_duration <b>WITH</b> special values | |
137 | * functionality. Allows for transparent implementation of either | |
138 | * date_duration<long> or date_duration<int_adapter<long> > */ | |
b32b8144 | 139 | struct BOOST_SYMBOL_VISIBLE duration_traits_adapted |
7c673cae FG |
140 | { |
141 | typedef long int_type; | |
142 | typedef boost::date_time::int_adapter<long> impl_type; | |
143 | static int_type as_number(impl_type i) { return i.as_number(); } | |
144 | }; | |
145 | ||
146 | ||
147 | } } //namspace date_time | |
148 | ||
149 | ||
150 | #endif | |
151 |