time_duration.hpp 9.0 KB

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