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 #include <boost/date_time/int_adapter.hpp>
17 
18 namespace boost {
19 namespace date_time {
20 
21 
22   //! Duration type with date level resolution
23   template<class duration_rep_traits>
24   class BOOST_SYMBOL_VISIBLE date_duration : private
25               boost::less_than_comparable1< date_duration< duration_rep_traits >
26             , boost::equality_comparable1< date_duration< duration_rep_traits >
27             , boost::addable1< date_duration< duration_rep_traits >
28             , boost::subtractable1< date_duration< duration_rep_traits >
29             , boost::dividable2< date_duration< duration_rep_traits >, int
30             > > > > >
31   {
32   public:
33     typedef typename duration_rep_traits::int_type duration_rep_type;
34     typedef typename duration_rep_traits::impl_type duration_rep;
35 
36     //! Construct from a day count
date_duration(duration_rep day_count)37     BOOST_CXX14_CONSTEXPR explicit date_duration(duration_rep day_count) : days_(day_count) {}
38 
39     /*! construct from special_values - only works when
40      * instantiated with duration_traits_adapted */
date_duration(special_values sv)41     BOOST_CXX14_CONSTEXPR date_duration(special_values sv) :
42             days_(duration_rep::from_special(sv))
43     {}
44 
45     //! returns days_ as it's instantiated type - used for streaming
get_rep() const46     BOOST_CXX14_CONSTEXPR duration_rep get_rep()const
47     {
48         return days_;
49     }
as_special() const50     BOOST_CXX14_CONSTEXPR special_values as_special() const
51     {
52         return days_.as_special();
53     }
is_special() const54     BOOST_CXX14_CONSTEXPR bool is_special()const
55     {
56         return days_.is_special();
57     }
58     //! returns days as value, not object.
days() const59     BOOST_CXX14_CONSTEXPR 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'
unit()64     static BOOST_CXX14_CONSTEXPR date_duration unit()
65     {
66         return date_duration<duration_rep_traits>(1);
67     }
68     //! Equality
operator ==(const date_duration & rhs) const69     BOOST_CXX14_CONSTEXPR bool operator==(const date_duration& rhs) const
70     {
71         return days_ == rhs.days_;
72     }
73     //! Less
operator <(const date_duration & rhs) const74     BOOST_CXX14_CONSTEXPR 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
operator -=(const date_duration & rhs)85     BOOST_CXX14_CONSTEXPR 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
operator +=(const date_duration & rhs)92     BOOST_CXX14_CONSTEXPR 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
operator -() const99     BOOST_CXX14_CONSTEXPR 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.
operator /=(int divisor)104     BOOST_CXX14_CONSTEXPR date_duration& operator/=(int divisor)
105     {
106         days_ = days_ / divisor;
107         return *this;
108     }
109 
110     //! return sign information
is_negative() const111     BOOST_CXX14_CONSTEXPR 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 BOOST_SYMBOL_VISIBLE duration_traits_long
125   {
126     typedef long int_type;
127     typedef long impl_type;
as_numberboost::date_time::duration_traits_long128     static BOOST_CXX14_CONSTEXPR 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 BOOST_SYMBOL_VISIBLE duration_traits_adapted
135   {
136     typedef long int_type;
137     typedef boost::date_time::int_adapter<long> impl_type;
as_numberboost::date_time::duration_traits_adapted138     static BOOST_CXX14_CONSTEXPR int_type as_number(impl_type i) { return i.as_number(); }
139   };
140 
141 
142 } } //namspace date_time
143 
144 
145 #endif
146 
147