| 1 | #ifndef DATE_TIME_TIME_DURATION_HPP___ |
| 2 | #define DATE_TIME_TIME_DURATION_HPP___ |
| 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 | #include <boost/cstdint.hpp> |
| 13 | #include <boost/operators.hpp> |
| 14 | #include <boost/static_assert.hpp> |
| 15 | #include <boost/date_time/time_defs.hpp> |
| 16 | #include <boost/date_time/special_defs.hpp> |
| 17 | #include <boost/date_time/compiler_config.hpp> |
| 18 | |
| 19 | namespace boost { |
| 20 | namespace date_time { |
| 21 | |
| 22 | |
| 23 | //! Represents some amount of elapsed time measure to a given resolution |
| 24 | /*! This class represents a standard set of capabilities for all |
| 25 | counted time durations. Time duration implementations should derive |
| 26 | from this class passing their type as the first template parameter. |
| 27 | This design allows the subclass duration types to provide custom |
| 28 | construction policies or other custom features not provided here. |
| 29 | |
| 30 | @param T The subclass type |
| 31 | @param rep_type The time resolution traits for this duration type. |
| 32 | */ |
| 33 | template<class T, typename rep_type> |
| 34 | class BOOST_SYMBOL_VISIBLE time_duration : private |
| 35 | boost::less_than_comparable<T |
| 36 | , boost::equality_comparable<T |
| 37 | > > |
| 38 | /* dividable, addable, and subtractable operator templates |
| 39 | * won't work with this class (MSVC++ 6.0). return type |
| 40 | * from '+=' is different than expected return type |
| 41 | * from '+'. multipliable probably wont work |
| 42 | * either (haven't tried) */ |
| 43 | { |
| 44 | public: |
| 45 | // A tag for type categorization. Can be used to detect Boost.DateTime duration types in generic code. |
| 46 | typedef void _is_boost_date_time_duration; |
| 47 | typedef T duration_type; //the subclass |
| 48 | typedef rep_type traits_type; |
| 49 | typedef typename rep_type::day_type day_type; |
| 50 | typedef typename rep_type::hour_type hour_type; |
| 51 | typedef typename rep_type::min_type min_type; |
| 52 | typedef typename rep_type::sec_type sec_type; |
| 53 | typedef typename rep_type::fractional_seconds_type fractional_seconds_type; |
| 54 | typedef typename rep_type::tick_type tick_type; |
| 55 | typedef typename rep_type::impl_type impl_type; |
| 56 | |
| 57 | time_duration() : ticks_(0) {} |
| 58 | time_duration(hour_type hours_in, |
| 59 | min_type minutes_in, |
| 60 | sec_type seconds_in=0, |
| 61 | fractional_seconds_type frac_sec_in = 0) : |
| 62 | ticks_(rep_type::to_tick_count(hours_in,minutes_in,seconds_in,frac_sec_in)) |
| 63 | {} |
| 64 | // copy constructor required for dividable<> |
| 65 | //! Construct from another time_duration (Copy constructor) |
| 66 | time_duration(const time_duration<T, rep_type>& other) |
| 67 | : ticks_(other.ticks_) |
| 68 | {} |
| 69 | //! Construct from special_values |
| 70 | time_duration(special_values sv) : ticks_(impl_type::from_special(sv)) |
| 71 | {} |
| 72 | //! Returns smallest representable duration |
| 73 | static duration_type unit() |
| 74 | { |
| 75 | return duration_type(0,0,0,1); |
| 76 | } |
| 77 | //! Return the number of ticks in a second |
| 78 | static tick_type ticks_per_second() |
| 79 | { |
| 80 | return rep_type::res_adjust(); |
| 81 | } |
| 82 | //! Provide the resolution of this duration type |
| 83 | static time_resolutions resolution() |
| 84 | { |
| 85 | return rep_type::resolution(); |
| 86 | } |
| 87 | //! Returns number of hours in the duration |
| 88 | hour_type hours() const |
| 89 | { |
| 90 | return static_cast<hour_type>(ticks() / (3600*ticks_per_second())); |
| 91 | } |
| 92 | //! Returns normalized number of minutes |
| 93 | min_type minutes() const |
| 94 | { |
| 95 | return static_cast<min_type>((ticks() / (60*ticks_per_second())) % 60); |
| 96 | } |
| 97 | //! Returns normalized number of seconds (0..60) |
| 98 | sec_type seconds() const |
| 99 | { |
| 100 | return static_cast<sec_type>((ticks()/ticks_per_second()) % 60); |
| 101 | } |
| 102 | //! Returns total number of seconds truncating any fractional seconds |
| 103 | sec_type total_seconds() const |
| 104 | { |
| 105 | return static_cast<sec_type>(ticks() / ticks_per_second()); |
| 106 | } |
| 107 | //! Returns total number of milliseconds truncating any fractional seconds |
| 108 | tick_type total_milliseconds() const |
| 109 | { |
| 110 | if (ticks_per_second() < 1000) { |
| 111 | return ticks() * (static_cast<tick_type>(1000) / ticks_per_second()); |
| 112 | } |
| 113 | return ticks() / (ticks_per_second() / static_cast<tick_type>(1000)) ; |
| 114 | } |
| 115 | //! Returns total number of nanoseconds truncating any sub millisecond values |
| 116 | tick_type total_nanoseconds() const |
| 117 | { |
| 118 | if (ticks_per_second() < 1000000000) { |
| 119 | return ticks() * (static_cast<tick_type>(1000000000) / ticks_per_second()); |
| 120 | } |
| 121 | return ticks() / (ticks_per_second() / static_cast<tick_type>(1000000000)) ; |
| 122 | } |
| 123 | //! Returns total number of microseconds truncating any sub microsecond values |
| 124 | tick_type total_microseconds() const |
| 125 | { |
| 126 | if (ticks_per_second() < 1000000) { |
| 127 | return ticks() * (static_cast<tick_type>(1000000) / ticks_per_second()); |
| 128 | } |
| 129 | return ticks() / (ticks_per_second() / static_cast<tick_type>(1000000)) ; |
| 130 | } |
| 131 | //! Returns count of fractional seconds at given resolution |
| 132 | fractional_seconds_type fractional_seconds() const |
| 133 | { |
| 134 | return (ticks() % ticks_per_second()); |
| 135 | } |
| 136 | //! Returns number of possible digits in fractional seconds |
| 137 | static unsigned short num_fractional_digits() |
| 138 | { |
| 139 | return rep_type::num_fractional_digits(); |
| 140 | } |
| 141 | duration_type invert_sign() const |
| 142 | { |
| 143 | return duration_type(ticks_ * (-1)); |
| 144 | } |
| 145 | bool is_negative() const |
| 146 | { |
| 147 | return ticks_ < 0; |
| 148 | } |
| 149 | bool operator<(const time_duration& rhs) const |
| 150 | { |
| 151 | return ticks_ < rhs.ticks_; |
| 152 | } |
| 153 | bool operator==(const time_duration& rhs) const |
| 154 | { |
| 155 | return ticks_ == rhs.ticks_; |
| 156 | } |
| 157 | //! unary- Allows for time_duration td = -td1 |
| 158 | duration_type operator-()const |
| 159 | { |
| 160 | return duration_type(ticks_ * (-1)); |
| 161 | } |
| 162 | duration_type operator-(const duration_type& d) const |
| 163 | { |
| 164 | return duration_type(ticks_ - d.ticks_); |
| 165 | } |
| 166 | duration_type operator+(const duration_type& d) const |
| 167 | { |
| 168 | return duration_type(ticks_ + d.ticks_); |
| 169 | } |
| 170 | duration_type operator/(int divisor) const |
| 171 | { |
| 172 | return duration_type(ticks_ / divisor); |
| 173 | } |
| 174 | duration_type operator-=(const duration_type& d) |
| 175 | { |
| 176 | ticks_ = ticks_ - d.ticks_; |
| 177 | return duration_type(ticks_); |
| 178 | } |
| 179 | duration_type operator+=(const duration_type& d) |
| 180 | { |
| 181 | ticks_ = ticks_ + d.ticks_; |
| 182 | return duration_type(ticks_); |
| 183 | } |
| 184 | //! Division operations on a duration with an integer. |
| 185 | duration_type operator/=(int divisor) |
| 186 | { |
| 187 | ticks_ = ticks_ / divisor; |
| 188 | return duration_type(ticks_); |
| 189 | } |
| 190 | //! Multiplication operations an a duration with an integer |
| 191 | duration_type operator*(int rhs) const |
| 192 | { |
| 193 | return duration_type(ticks_ * rhs); |
| 194 | } |
| 195 | duration_type operator*=(int divisor) |
| 196 | { |
| 197 | ticks_ = ticks_ * divisor; |
| 198 | return duration_type(ticks_); |
| 199 | } |
| 200 | tick_type ticks() const |
| 201 | { |
| 202 | return traits_type::as_number(ticks_); |
| 203 | } |
| 204 | |
| 205 | //! Is ticks_ a special value? |
| 206 | bool is_special()const |
| 207 | { |
| 208 | if(traits_type::is_adapted()) |
| 209 | { |
| 210 | return ticks_.is_special(); |
| 211 | } |
| 212 | else{ |
| 213 | return false; |
| 214 | } |
| 215 | } |
| 216 | //! Is duration pos-infinity |
| 217 | bool is_pos_infinity()const |
| 218 | { |
| 219 | if(traits_type::is_adapted()) |
| 220 | { |
| 221 | return ticks_.is_pos_infinity(); |
| 222 | } |
| 223 | else{ |
| 224 | return false; |
| 225 | } |
| 226 | } |
| 227 | //! Is duration neg-infinity |
| 228 | bool is_neg_infinity()const |
| 229 | { |
| 230 | if(traits_type::is_adapted()) |
| 231 | { |
| 232 | return ticks_.is_neg_infinity(); |
| 233 | } |
| 234 | else{ |
| 235 | return false; |
| 236 | } |
| 237 | } |
| 238 | //! Is duration not-a-date-time |
| 239 | bool is_not_a_date_time()const |
| 240 | { |
| 241 | if(traits_type::is_adapted()) |
| 242 | { |
| 243 | return ticks_.is_nan(); |
| 244 | } |
| 245 | else{ |
| 246 | return false; |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | //! Used for special_values output |
| 251 | impl_type get_rep()const |
| 252 | { |
| 253 | return ticks_; |
| 254 | } |
| 255 | |
| 256 | protected: |
| 257 | explicit time_duration(impl_type in) : ticks_(in) {} |
| 258 | impl_type ticks_; |
| 259 | }; |
| 260 | |
| 261 | |
| 262 | |
| 263 | //! Template for instantiating derived adjusting durations |
| 264 | /* These templates are designed to work with multiples of |
| 265 | * 10 for frac_of_second and resoultion adjustment |
| 266 | */ |
| 267 | template<class base_duration, boost::int64_t frac_of_second> |
| 268 | class BOOST_SYMBOL_VISIBLE subsecond_duration : public base_duration |
| 269 | { |
| 270 | public: |
| 271 | typedef typename base_duration::impl_type impl_type; |
| 272 | typedef typename base_duration::traits_type traits_type; |
| 273 | |
| 274 | private: |
| 275 | // To avoid integer overflow we precompute the duration resolution conversion coefficient (ticket #3471) |
| 276 | BOOST_STATIC_ASSERT_MSG((traits_type::ticks_per_second >= frac_of_second ? traits_type::ticks_per_second % frac_of_second : frac_of_second % traits_type::ticks_per_second) == 0,\ |
| 277 | "The base duration resolution must be a multiple of the subsecond duration resolution" ); |
| 278 | BOOST_STATIC_CONSTANT(boost::int64_t, adjustment_ratio = (traits_type::ticks_per_second >= frac_of_second ? traits_type::ticks_per_second / frac_of_second : frac_of_second / traits_type::ticks_per_second)); |
| 279 | |
| 280 | public: |
| 281 | explicit subsecond_duration(boost::int64_t ss) : |
| 282 | base_duration(impl_type(traits_type::ticks_per_second >= frac_of_second ? ss * adjustment_ratio : ss / adjustment_ratio)) |
| 283 | { |
| 284 | } |
| 285 | }; |
| 286 | |
| 287 | |
| 288 | |
| 289 | } } //namespace date_time |
| 290 | |
| 291 | |
| 292 | |
| 293 | |
| 294 | #endif |
| 295 | |
| 296 | |