Back to home page

EIC code displayed by LXR

 
 

    


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 /* Copyright (c) 2002,2003 CrystalClear Software, Inc.
0005  * Use, modification and distribution is subject to the
0006  * Boost Software License, Version 1.0. (See accompanying
0007  * file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
0008  * Author: Jeff Garland, Bart Garst
0009  * $Date$
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   //! Represents some amount of elapsed time measure to a given resolution
0026   /*! This class represents a standard set of capabilities for all
0027       counted time durations.  Time duration implementations should derive
0028       from this class passing their type as the first template parameter.
0029       This design allows the subclass duration types to provide custom
0030       construction policies or other custom features not provided here.
0031 
0032       @tparam T The subclass type
0033       @tparam rep_type The time resolution traits for this duration type.
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   /* dividable, addable, and subtractable operator templates
0041    * won't work with this class (MSVC++ 6.0). return type
0042    * from '+=' is different than expected return type
0043    * from '+'. multipliable probably wont work
0044    * either (haven't tried) */
0045   {
0046   public:
0047     // A tag for type categorization. Can be used to detect Boost.DateTime duration types in generic code.
0048     typedef void _is_boost_date_time_duration;
0049     typedef T duration_type;  //the subclass
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     //! Construct from special_values
0067     BOOST_CXX14_CONSTEXPR time_duration(special_values sv) : ticks_(impl_type::from_special(sv))
0068     {}
0069     //! Returns smallest representable duration
0070     static BOOST_CXX14_CONSTEXPR duration_type unit()
0071     {
0072       return duration_type(0,0,0,1);
0073     }
0074     //! Return the number of ticks in a second
0075     static BOOST_CXX14_CONSTEXPR tick_type ticks_per_second()
0076     {
0077       return rep_type::res_adjust();
0078     }
0079     //! Provide the resolution of this duration type
0080     static BOOST_CXX14_CONSTEXPR time_resolutions resolution()
0081     {
0082       return rep_type::resolution();
0083     }
0084     //! Returns number of hours in the duration
0085     BOOST_CXX14_CONSTEXPR hour_type hours()   const
0086     {
0087       return static_cast<hour_type>(ticks() / (3600*ticks_per_second()));
0088     }
0089     //! Returns normalized number of minutes
0090     BOOST_CXX14_CONSTEXPR min_type minutes() const
0091     {
0092       return static_cast<min_type>((ticks() / (60*ticks_per_second())) % 60);
0093     }
0094     //! Returns normalized number of seconds (0..60)
0095     BOOST_CXX14_CONSTEXPR sec_type seconds() const
0096     {
0097       return static_cast<sec_type>((ticks()/ticks_per_second()) % 60);
0098     }
0099     //! Returns total number of seconds truncating any fractional seconds
0100     BOOST_CXX14_CONSTEXPR sec_type total_seconds() const
0101     {
0102       return static_cast<sec_type>(ticks() / ticks_per_second());
0103     }
0104     //! Returns total number of milliseconds truncating any fractional seconds
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     //! Returns total number of nanoseconds truncating any sub millisecond values
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     //! Returns total number of microseconds truncating any sub microsecond values
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     //! Returns count of fractional seconds at given resolution
0129     BOOST_CXX14_CONSTEXPR fractional_seconds_type fractional_seconds() const
0130     {
0131       return (ticks() % ticks_per_second());
0132     }
0133     //! Returns number of possible digits in fractional seconds
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     //! unary- Allows for time_duration td = -td1
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     //! Division operations on a duration with an integer.
0198     BOOST_CXX14_CONSTEXPR duration_type operator/=(int divisor)
0199     {
0200       ticks_ = ticks_ / divisor;
0201       return duration_type(ticks_);
0202     }
0203     //! Multiplication operations an a duration with an integer
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     //! Is ticks_ a special value?
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     //! Is duration pos-infinity
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     //! Is duration neg-infinity
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     //! Is duration not-a-date-time
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     //! Used for special_values output
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   //! Template for instantiating derived adjusting durations
0277   /* These templates are designed to work with multiples of
0278    * 10 for frac_of_second and resolution adjustment
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     // To avoid integer overflow we precompute the duration resolution conversion coefficient (ticket #3471)
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     // The argument (ss) must be an integral type
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 } } //namespace date_time
0305 
0306 
0307 
0308 
0309 #endif
0310