| 1 | #ifndef GREG_DURATION_HPP___ |
| 2 | #define GREG_DURATION_HPP___ |
| 3 | |
| 4 | /* Copyright (c) 2002,2003, 2020 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 | #include <boost/date_time/compiler_config.hpp> |
| 13 | #include <boost/date_time/date_duration.hpp> |
| 14 | #include <boost/date_time/int_adapter.hpp> |
| 15 | #include <boost/date_time/special_defs.hpp> |
| 16 | |
| 17 | namespace boost { |
| 18 | namespace gregorian { |
| 19 | |
| 20 | //!An internal date representation that includes infinities, not a date |
| 21 | typedef boost::date_time::duration_traits_adapted date_duration_rep; |
| 22 | |
| 23 | //! Durations in days for gregorian system |
| 24 | /*! \ingroup date_basics |
| 25 | */ |
| 26 | class BOOST_SYMBOL_VISIBLE date_duration : |
| 27 | public boost::date_time::date_duration< date_duration_rep > |
| 28 | { |
| 29 | typedef boost::date_time::date_duration< date_duration_rep > base_type; |
| 30 | |
| 31 | public: |
| 32 | typedef base_type::duration_rep duration_rep; |
| 33 | |
| 34 | //! Construct from a day count |
| 35 | BOOST_CXX14_CONSTEXPR explicit |
| 36 | date_duration(duration_rep day_count = 0) : base_type(day_count) {} |
| 37 | |
| 38 | //! construct from special_values |
| 39 | BOOST_CXX14_CONSTEXPR |
| 40 | date_duration(date_time::special_values sv) : base_type(sv) {} |
| 41 | |
| 42 | //! Construct from another date_duration |
| 43 | BOOST_CXX14_CONSTEXPR |
| 44 | date_duration(const base_type& other) : base_type(other) |
| 45 | {} |
| 46 | |
| 47 | // Relational operators |
| 48 | // NOTE: Because of date_time::date_duration< T > design choice we don't use Boost.Operators here, |
| 49 | // because we need the class to be a direct base. Either lose EBO, or define operators by hand. |
| 50 | // The latter is more effecient. |
| 51 | BOOST_CXX14_CONSTEXPR bool operator== (const date_duration& rhs) const |
| 52 | { |
| 53 | return base_type::operator== (rhs); |
| 54 | } |
| 55 | BOOST_CXX14_CONSTEXPR bool operator!= (const date_duration& rhs) const |
| 56 | { |
| 57 | return !operator== (rhs); |
| 58 | } |
| 59 | BOOST_CXX14_CONSTEXPR bool operator< (const date_duration& rhs) const |
| 60 | { |
| 61 | return base_type::operator< (rhs); |
| 62 | } |
| 63 | BOOST_CXX14_CONSTEXPR bool operator> (const date_duration& rhs) const |
| 64 | { |
| 65 | return !(base_type::operator< (rhs) || base_type::operator== (rhs)); |
| 66 | } |
| 67 | BOOST_CXX14_CONSTEXPR bool operator<= (const date_duration& rhs) const |
| 68 | { |
| 69 | return (base_type::operator< (rhs) || base_type::operator== (rhs)); |
| 70 | } |
| 71 | BOOST_CXX14_CONSTEXPR bool operator>= (const date_duration& rhs) const |
| 72 | { |
| 73 | return !base_type::operator< (rhs); |
| 74 | } |
| 75 | |
| 76 | //! Subtract another duration -- result is signed |
| 77 | BOOST_CXX14_CONSTEXPR date_duration& operator-= (const date_duration& rhs) |
| 78 | { |
| 79 | base_type::operator-= (rhs); |
| 80 | return *this; |
| 81 | } |
| 82 | |
| 83 | BOOST_CXX14_CONSTEXPR friend |
| 84 | date_duration operator- (date_duration rhs, date_duration const& lhs); |
| 85 | |
| 86 | //! Add a duration -- result is signed |
| 87 | BOOST_CXX14_CONSTEXPR date_duration& operator+= (const date_duration& rhs) |
| 88 | { |
| 89 | base_type::operator+= (rhs); |
| 90 | return *this; |
| 91 | } |
| 92 | |
| 93 | BOOST_CXX14_CONSTEXPR friend |
| 94 | date_duration operator+ (date_duration rhs, date_duration const& lhs); |
| 95 | |
| 96 | //! unary- Allows for dd = -date_duration(2); -> dd == -2 |
| 97 | BOOST_CXX14_CONSTEXPR date_duration operator- ()const |
| 98 | { |
| 99 | return date_duration(get_rep() * (-1)); |
| 100 | } |
| 101 | |
| 102 | //! Division operations on a duration with an integer. |
| 103 | BOOST_CXX14_CONSTEXPR date_duration& operator/= (int divisor) |
| 104 | { |
| 105 | base_type::operator/= (divisor); |
| 106 | return *this; |
| 107 | } |
| 108 | |
| 109 | BOOST_CXX14_CONSTEXPR friend date_duration operator/ (date_duration rhs, int lhs); |
| 110 | |
| 111 | //! Returns the smallest duration -- used by to calculate 'end' |
| 112 | static BOOST_CXX14_CONSTEXPR date_duration unit() |
| 113 | { |
| 114 | return date_duration(base_type::unit().get_rep()); |
| 115 | } |
| 116 | }; |
| 117 | |
| 118 | inline BOOST_CXX14_CONSTEXPR |
| 119 | date_duration operator- (date_duration rhs, date_duration const& lhs) |
| 120 | { |
| 121 | rhs -= lhs; |
| 122 | return rhs; |
| 123 | } |
| 124 | |
| 125 | inline BOOST_CXX14_CONSTEXPR |
| 126 | date_duration operator+ (date_duration rhs, date_duration const& lhs) |
| 127 | { |
| 128 | rhs += lhs; |
| 129 | return rhs; |
| 130 | } |
| 131 | |
| 132 | inline BOOST_CXX14_CONSTEXPR date_duration operator/ (date_duration rhs, int lhs) |
| 133 | { |
| 134 | rhs /= lhs; |
| 135 | return rhs; |
| 136 | } |
| 137 | |
| 138 | //! Shorthand for date_duration |
| 139 | typedef date_duration days; |
| 140 | |
| 141 | } } //namespace gregorian |
| 142 | |
| 143 | #if defined(BOOST_DATE_TIME_OPTIONAL_GREGORIAN_TYPES) |
| 144 | #include <boost/date_time/date_duration_types.hpp> |
| 145 | #endif |
| 146 | |
| 147 | #endif |
| 148 | |