File indexing completed on 2025-12-16 09:45:17
0001 #ifndef DATE_TIME_TIME_DURATION_HPP___
0002 #define DATE_TIME_TIME_DURATION_HPP___
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012 #include <boost/core/enable_if.hpp>
0013 #include <boost/cstdint.hpp>
0014 #include <boost/date_time/compiler_config.hpp>
0015 #include <boost/date_time/special_defs.hpp>
0016 #include <boost/date_time/time_defs.hpp>
0017 #include <boost/operators.hpp>
0018 #include <boost/static_assert.hpp>
0019 #include <boost/type_traits/is_integral.hpp>
0020
0021 namespace boost {
0022 namespace date_time {
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035 template<class T, typename rep_type>
0036 class BOOST_SYMBOL_VISIBLE time_duration : private
0037 boost::less_than_comparable<T
0038 , boost::equality_comparable<T
0039 > >
0040
0041
0042
0043
0044
0045 {
0046 public:
0047
0048 typedef void _is_boost_date_time_duration;
0049 typedef T duration_type;
0050 typedef rep_type traits_type;
0051 typedef typename rep_type::day_type day_type;
0052 typedef typename rep_type::hour_type hour_type;
0053 typedef typename rep_type::min_type min_type;
0054 typedef typename rep_type::sec_type sec_type;
0055 typedef typename rep_type::fractional_seconds_type fractional_seconds_type;
0056 typedef typename rep_type::tick_type tick_type;
0057 typedef typename rep_type::impl_type impl_type;
0058
0059 BOOST_CXX14_CONSTEXPR time_duration() : ticks_(0) {}
0060 BOOST_CXX14_CONSTEXPR time_duration(hour_type hours_in,
0061 min_type minutes_in,
0062 sec_type seconds_in=0,
0063 fractional_seconds_type frac_sec_in = 0) :
0064 ticks_(rep_type::to_tick_count(hours_in,minutes_in,seconds_in,frac_sec_in))
0065 {}
0066
0067 BOOST_CXX14_CONSTEXPR time_duration(special_values sv) : ticks_(impl_type::from_special(sv))
0068 {}
0069
0070 static BOOST_CXX14_CONSTEXPR duration_type unit()
0071 {
0072 return duration_type(0,0,0,1);
0073 }
0074
0075 static BOOST_CXX14_CONSTEXPR tick_type ticks_per_second()
0076 {
0077 return rep_type::res_adjust();
0078 }
0079
0080 static BOOST_CXX14_CONSTEXPR time_resolutions resolution()
0081 {
0082 return rep_type::resolution();
0083 }
0084
0085 BOOST_CXX14_CONSTEXPR hour_type hours() const
0086 {
0087 return static_cast<hour_type>(ticks() / (3600*ticks_per_second()));
0088 }
0089
0090 BOOST_CXX14_CONSTEXPR min_type minutes() const
0091 {
0092 return static_cast<min_type>((ticks() / (60*ticks_per_second())) % 60);
0093 }
0094
0095 BOOST_CXX14_CONSTEXPR sec_type seconds() const
0096 {
0097 return static_cast<sec_type>((ticks()/ticks_per_second()) % 60);
0098 }
0099
0100 BOOST_CXX14_CONSTEXPR sec_type total_seconds() const
0101 {
0102 return static_cast<sec_type>(ticks() / ticks_per_second());
0103 }
0104
0105 BOOST_CXX14_CONSTEXPR tick_type total_milliseconds() const
0106 {
0107 if (ticks_per_second() < 1000) {
0108 return ticks() * (static_cast<tick_type>(1000) / ticks_per_second());
0109 }
0110 return ticks() / (ticks_per_second() / static_cast<tick_type>(1000)) ;
0111 }
0112
0113 BOOST_CXX14_CONSTEXPR tick_type total_nanoseconds() const
0114 {
0115 if (ticks_per_second() < 1000000000) {
0116 return ticks() * (static_cast<tick_type>(1000000000) / ticks_per_second());
0117 }
0118 return ticks() / (ticks_per_second() / static_cast<tick_type>(1000000000)) ;
0119 }
0120
0121 BOOST_CXX14_CONSTEXPR tick_type total_microseconds() const
0122 {
0123 if (ticks_per_second() < 1000000) {
0124 return ticks() * (static_cast<tick_type>(1000000) / ticks_per_second());
0125 }
0126 return ticks() / (ticks_per_second() / static_cast<tick_type>(1000000)) ;
0127 }
0128
0129 BOOST_CXX14_CONSTEXPR fractional_seconds_type fractional_seconds() const
0130 {
0131 return (ticks() % ticks_per_second());
0132 }
0133
0134 static BOOST_CXX14_CONSTEXPR unsigned short num_fractional_digits()
0135 {
0136 return rep_type::num_fractional_digits();
0137 }
0138 BOOST_CXX14_CONSTEXPR duration_type invert_sign() const
0139 {
0140 return duration_type(ticks_ * (-1));
0141 }
0142 BOOST_CXX14_CONSTEXPR duration_type abs() const
0143 {
0144 if ( is_negative() )
0145 {
0146 return invert_sign();
0147 }
0148 return duration_type(ticks_);
0149 }
0150 BOOST_CONSTEXPR bool is_negative() const
0151 {
0152 return ticks_ < 0;
0153 }
0154 BOOST_CONSTEXPR bool is_zero() const
0155 {
0156 return ticks_ == 0;
0157 }
0158 BOOST_CONSTEXPR bool is_positive() const
0159 {
0160 return ticks_ > 0;
0161 }
0162 BOOST_CONSTEXPR bool operator<(const time_duration& rhs) const
0163 {
0164 return ticks_ < rhs.ticks_;
0165 }
0166 BOOST_CONSTEXPR bool operator==(const time_duration& rhs) const
0167 {
0168 return ticks_ == rhs.ticks_;
0169 }
0170
0171 BOOST_CONSTEXPR duration_type operator-()const
0172 {
0173 return duration_type(ticks_ * (-1));
0174 }
0175 BOOST_CONSTEXPR duration_type operator-(const duration_type& d) const
0176 {
0177 return duration_type(ticks_ - d.ticks_);
0178 }
0179 BOOST_CONSTEXPR duration_type operator+(const duration_type& d) const
0180 {
0181 return duration_type(ticks_ + d.ticks_);
0182 }
0183 BOOST_CONSTEXPR duration_type operator/(int divisor) const
0184 {
0185 return duration_type(ticks_ / divisor);
0186 }
0187 BOOST_CXX14_CONSTEXPR duration_type operator-=(const duration_type& d)
0188 {
0189 ticks_ = ticks_ - d.ticks_;
0190 return duration_type(ticks_);
0191 }
0192 BOOST_CXX14_CONSTEXPR duration_type operator+=(const duration_type& d)
0193 {
0194 ticks_ = ticks_ + d.ticks_;
0195 return duration_type(ticks_);
0196 }
0197
0198 BOOST_CXX14_CONSTEXPR duration_type operator/=(int divisor)
0199 {
0200 ticks_ = ticks_ / divisor;
0201 return duration_type(ticks_);
0202 }
0203
0204 BOOST_CXX14_CONSTEXPR duration_type operator*(int rhs) const
0205 {
0206 return duration_type(ticks_ * rhs);
0207 }
0208 BOOST_CXX14_CONSTEXPR duration_type operator*=(int divisor)
0209 {
0210 ticks_ = ticks_ * divisor;
0211 return duration_type(ticks_);
0212 }
0213 BOOST_CXX14_CONSTEXPR tick_type ticks() const
0214 {
0215 return traits_type::as_number(ticks_);
0216 }
0217
0218
0219 BOOST_CXX14_CONSTEXPR bool is_special()const
0220 {
0221 if(traits_type::is_adapted())
0222 {
0223 return ticks_.is_special();
0224 }
0225 else{
0226 return false;
0227 }
0228 }
0229
0230 BOOST_CXX14_CONSTEXPR bool is_pos_infinity()const
0231 {
0232 if(traits_type::is_adapted())
0233 {
0234 return ticks_.is_pos_infinity();
0235 }
0236 else{
0237 return false;
0238 }
0239 }
0240
0241 BOOST_CXX14_CONSTEXPR bool is_neg_infinity()const
0242 {
0243 if(traits_type::is_adapted())
0244 {
0245 return ticks_.is_neg_infinity();
0246 }
0247 else{
0248 return false;
0249 }
0250 }
0251
0252 BOOST_CXX14_CONSTEXPR bool is_not_a_date_time()const
0253 {
0254 if(traits_type::is_adapted())
0255 {
0256 return ticks_.is_nan();
0257 }
0258 else{
0259 return false;
0260 }
0261 }
0262
0263
0264 BOOST_CONSTEXPR impl_type get_rep()const
0265 {
0266 return ticks_;
0267 }
0268
0269 protected:
0270 BOOST_CXX14_CONSTEXPR explicit time_duration(impl_type in) : ticks_(in) {}
0271 impl_type ticks_;
0272 };
0273
0274
0275
0276
0277
0278
0279
0280 template<class base_duration, boost::int64_t frac_of_second>
0281 class BOOST_SYMBOL_VISIBLE subsecond_duration : public base_duration
0282 {
0283 public:
0284 typedef typename base_duration::impl_type impl_type;
0285 typedef typename base_duration::traits_type traits_type;
0286
0287 private:
0288
0289 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,\
0290 "The base duration resolution must be a multiple of the subsecond duration resolution");
0291 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));
0292
0293 public:
0294
0295 template <typename T>
0296 BOOST_CXX14_CONSTEXPR explicit subsecond_duration(T const& ss,
0297 typename boost::enable_if<boost::is_integral<T>,
0298 void>::type* = BOOST_DATE_TIME_NULLPTR) :
0299 base_duration(impl_type(traits_type::ticks_per_second >= frac_of_second ? ss * adjustment_ratio : ss / adjustment_ratio))
0300 {
0301 }
0302 };
0303
0304 } }
0305
0306
0307
0308
0309 #endif
0310