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