time_duration.hpp 10 KB

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