123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295 |
- #ifndef DATE_TIME_TIME_DURATION_HPP___
- #define DATE_TIME_TIME_DURATION_HPP___
- #include <boost/cstdint.hpp>
- #include <boost/operators.hpp>
- #include <boost/static_assert.hpp>
- #include <boost/date_time/time_defs.hpp>
- #include <boost/date_time/special_defs.hpp>
- #include <boost/date_time/compiler_config.hpp>
- namespace boost {
- namespace date_time {
-
-
- template<class T, typename rep_type>
- class time_duration : private
- boost::less_than_comparable<T
- , boost::equality_comparable<T
- > >
-
- {
- public:
-
- typedef void _is_boost_date_time_duration;
- typedef T duration_type;
- typedef rep_type traits_type;
- typedef typename rep_type::day_type day_type;
- typedef typename rep_type::hour_type hour_type;
- typedef typename rep_type::min_type min_type;
- typedef typename rep_type::sec_type sec_type;
- typedef typename rep_type::fractional_seconds_type fractional_seconds_type;
- typedef typename rep_type::tick_type tick_type;
- typedef typename rep_type::impl_type impl_type;
- 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) :
- ticks_(rep_type::to_tick_count(hours_in,minutes_in,seconds_in,frac_sec_in))
- {}
-
-
- time_duration(const time_duration<T, rep_type>& other)
- : ticks_(other.ticks_)
- {}
-
- time_duration(special_values sv) : ticks_(impl_type::from_special(sv))
- {}
-
- static duration_type unit()
- {
- return duration_type(0,0,0,1);
- }
-
- static tick_type ticks_per_second()
- {
- return rep_type::res_adjust();
- }
-
- static time_resolutions resolution()
- {
- return rep_type::resolution();
- }
-
- hour_type hours() const
- {
- return static_cast<hour_type>(ticks() / (3600*ticks_per_second()));
- }
-
- min_type minutes() const
- {
- return static_cast<min_type>((ticks() / (60*ticks_per_second())) % 60);
- }
-
- sec_type seconds() const
- {
- return static_cast<sec_type>((ticks()/ticks_per_second()) % 60);
- }
-
- sec_type total_seconds() const
- {
- return static_cast<sec_type>(ticks() / ticks_per_second());
- }
-
- tick_type total_milliseconds() const
- {
- if (ticks_per_second() < 1000) {
- return ticks() * (static_cast<tick_type>(1000) / ticks_per_second());
- }
- return ticks() / (ticks_per_second() / static_cast<tick_type>(1000)) ;
- }
-
- tick_type total_nanoseconds() const
- {
- if (ticks_per_second() < 1000000000) {
- return ticks() * (static_cast<tick_type>(1000000000) / ticks_per_second());
- }
- return ticks() / (ticks_per_second() / static_cast<tick_type>(1000000000)) ;
- }
-
- tick_type total_microseconds() const
- {
- if (ticks_per_second() < 1000000) {
- return ticks() * (static_cast<tick_type>(1000000) / ticks_per_second());
- }
- return ticks() / (ticks_per_second() / static_cast<tick_type>(1000000)) ;
- }
-
- fractional_seconds_type fractional_seconds() const
- {
- return (ticks() % ticks_per_second());
- }
-
- static unsigned short num_fractional_digits()
- {
- return rep_type::num_fractional_digits();
- }
- duration_type invert_sign() const
- {
- return duration_type(ticks_ * (-1));
- }
- bool is_negative() const
- {
- return ticks_ < 0;
- }
- bool operator<(const time_duration& rhs) const
- {
- return ticks_ < rhs.ticks_;
- }
- bool operator==(const time_duration& rhs) const
- {
- return ticks_ == rhs.ticks_;
- }
-
- duration_type operator-()const
- {
- return duration_type(ticks_ * (-1));
- }
- duration_type operator-(const duration_type& d) const
- {
- return duration_type(ticks_ - d.ticks_);
- }
- duration_type operator+(const duration_type& d) const
- {
- return duration_type(ticks_ + d.ticks_);
- }
- duration_type operator/(int divisor) const
- {
- return duration_type(ticks_ / divisor);
- }
- duration_type operator-=(const duration_type& d)
- {
- ticks_ = ticks_ - d.ticks_;
- return duration_type(ticks_);
- }
- duration_type operator+=(const duration_type& d)
- {
- ticks_ = ticks_ + d.ticks_;
- return duration_type(ticks_);
- }
-
- duration_type operator/=(int divisor)
- {
- ticks_ = ticks_ / divisor;
- return duration_type(ticks_);
- }
-
- duration_type operator*(int rhs) const
- {
- return duration_type(ticks_ * rhs);
- }
- duration_type operator*=(int divisor)
- {
- ticks_ = ticks_ * divisor;
- return duration_type(ticks_);
- }
- tick_type ticks() const
- {
- return traits_type::as_number(ticks_);
- }
-
- bool is_special()const
- {
- if(traits_type::is_adapted())
- {
- return ticks_.is_special();
- }
- else{
- return false;
- }
- }
-
- bool is_pos_infinity()const
- {
- if(traits_type::is_adapted())
- {
- return ticks_.is_pos_infinity();
- }
- else{
- return false;
- }
- }
-
- bool is_neg_infinity()const
- {
- if(traits_type::is_adapted())
- {
- return ticks_.is_neg_infinity();
- }
- else{
- return false;
- }
- }
-
- bool is_not_a_date_time()const
- {
- if(traits_type::is_adapted())
- {
- return ticks_.is_nan();
- }
- else{
- return false;
- }
- }
-
- impl_type get_rep()const
- {
- return ticks_;
- }
- protected:
- explicit time_duration(impl_type in) : ticks_(in) {}
- impl_type ticks_;
- };
-
-
- template<class base_duration, boost::int64_t frac_of_second>
- class subsecond_duration : public base_duration
- {
- public:
- typedef typename base_duration::impl_type impl_type;
- typedef typename base_duration::traits_type traits_type;
- private:
-
- 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,\
- "The base duration resolution must be a multiple of the subsecond duration resolution");
- 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));
- public:
- explicit subsecond_duration(boost::int64_t ss) :
- base_duration(impl_type(traits_type::ticks_per_second >= frac_of_second ? ss * adjustment_ratio : ss / adjustment_ratio))
- {
- }
- };
- } }
- #endif
|