time_duration.hpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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/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. // A tag for type categorization. Can be used to detect Boost.DateTime duration types in generic code.
  40. typedef void _is_boost_date_time_duration;
  41. typedef T duration_type; //the subclass
  42. typedef rep_type traits_type;
  43. typedef typename rep_type::day_type day_type;
  44. typedef typename rep_type::hour_type hour_type;
  45. typedef typename rep_type::min_type min_type;
  46. typedef typename rep_type::sec_type sec_type;
  47. typedef typename rep_type::fractional_seconds_type fractional_seconds_type;
  48. typedef typename rep_type::tick_type tick_type;
  49. typedef typename rep_type::impl_type impl_type;
  50. time_duration() : ticks_(0) {}
  51. time_duration(hour_type hours_in,
  52. min_type minutes_in,
  53. sec_type seconds_in=0,
  54. fractional_seconds_type frac_sec_in = 0) :
  55. ticks_(rep_type::to_tick_count(hours_in,minutes_in,seconds_in,frac_sec_in))
  56. {}
  57. // copy constructor required for dividable<>
  58. //! Construct from another time_duration (Copy constructor)
  59. time_duration(const time_duration<T, rep_type>& other)
  60. : ticks_(other.ticks_)
  61. {}
  62. //! Construct from special_values
  63. time_duration(special_values sv) : ticks_(impl_type::from_special(sv))
  64. {}
  65. //! Returns smallest representable duration
  66. static duration_type unit()
  67. {
  68. return duration_type(0,0,0,1);
  69. }
  70. //! Return the number of ticks in a second
  71. static tick_type ticks_per_second()
  72. {
  73. return rep_type::res_adjust();
  74. }
  75. //! Provide the resolution of this duration type
  76. static time_resolutions resolution()
  77. {
  78. return rep_type::resolution();
  79. }
  80. //! Returns number of hours in the duration
  81. hour_type hours() const
  82. {
  83. return static_cast<hour_type>(ticks() / (3600*ticks_per_second()));
  84. }
  85. //! Returns normalized number of minutes
  86. min_type minutes() const
  87. {
  88. return static_cast<min_type>((ticks() / (60*ticks_per_second())) % 60);
  89. }
  90. //! Returns normalized number of seconds (0..60)
  91. sec_type seconds() const
  92. {
  93. return static_cast<sec_type>((ticks()/ticks_per_second()) % 60);
  94. }
  95. //! Returns total number of seconds truncating any fractional seconds
  96. sec_type total_seconds() const
  97. {
  98. return static_cast<sec_type>(ticks() / ticks_per_second());
  99. }
  100. //! Returns total number of milliseconds truncating any fractional seconds
  101. tick_type total_milliseconds() const
  102. {
  103. if (ticks_per_second() < 1000) {
  104. return ticks() * (static_cast<tick_type>(1000) / ticks_per_second());
  105. }
  106. return ticks() / (ticks_per_second() / static_cast<tick_type>(1000)) ;
  107. }
  108. //! Returns total number of nanoseconds truncating any sub millisecond values
  109. tick_type total_nanoseconds() const
  110. {
  111. if (ticks_per_second() < 1000000000) {
  112. return ticks() * (static_cast<tick_type>(1000000000) / ticks_per_second());
  113. }
  114. return ticks() / (ticks_per_second() / static_cast<tick_type>(1000000000)) ;
  115. }
  116. //! Returns total number of microseconds truncating any sub microsecond values
  117. tick_type total_microseconds() const
  118. {
  119. if (ticks_per_second() < 1000000) {
  120. return ticks() * (static_cast<tick_type>(1000000) / ticks_per_second());
  121. }
  122. return ticks() / (ticks_per_second() / static_cast<tick_type>(1000000)) ;
  123. }
  124. //! Returns count of fractional seconds at given resolution
  125. fractional_seconds_type fractional_seconds() const
  126. {
  127. return (ticks() % ticks_per_second());
  128. }
  129. //! Returns number of possible digits in fractional seconds
  130. static unsigned short num_fractional_digits()
  131. {
  132. return rep_type::num_fractional_digits();
  133. }
  134. duration_type invert_sign() const
  135. {
  136. return duration_type(ticks_ * (-1));
  137. }
  138. bool is_negative() const
  139. {
  140. return ticks_ < 0;
  141. }
  142. bool operator<(const time_duration& rhs) const
  143. {
  144. return ticks_ < rhs.ticks_;
  145. }
  146. bool operator==(const time_duration& rhs) const
  147. {
  148. return ticks_ == rhs.ticks_;
  149. }
  150. //! unary- Allows for time_duration td = -td1
  151. duration_type operator-()const
  152. {
  153. return duration_type(ticks_ * (-1));
  154. }
  155. duration_type operator-(const duration_type& d) const
  156. {
  157. return duration_type(ticks_ - d.ticks_);
  158. }
  159. duration_type operator+(const duration_type& d) const
  160. {
  161. return duration_type(ticks_ + d.ticks_);
  162. }
  163. duration_type operator/(int divisor) const
  164. {
  165. return duration_type(ticks_ / divisor);
  166. }
  167. duration_type operator-=(const duration_type& d)
  168. {
  169. ticks_ = ticks_ - d.ticks_;
  170. return duration_type(ticks_);
  171. }
  172. duration_type operator+=(const duration_type& d)
  173. {
  174. ticks_ = ticks_ + d.ticks_;
  175. return duration_type(ticks_);
  176. }
  177. //! Division operations on a duration with an integer.
  178. duration_type operator/=(int divisor)
  179. {
  180. ticks_ = ticks_ / divisor;
  181. return duration_type(ticks_);
  182. }
  183. //! Multiplication operations an a duration with an integer
  184. duration_type operator*(int rhs) const
  185. {
  186. return duration_type(ticks_ * rhs);
  187. }
  188. duration_type operator*=(int divisor)
  189. {
  190. ticks_ = ticks_ * divisor;
  191. return duration_type(ticks_);
  192. }
  193. tick_type ticks() const
  194. {
  195. return traits_type::as_number(ticks_);
  196. }
  197. //! Is ticks_ a special value?
  198. bool is_special()const
  199. {
  200. if(traits_type::is_adapted())
  201. {
  202. return ticks_.is_special();
  203. }
  204. else{
  205. return false;
  206. }
  207. }
  208. //! Is duration pos-infinity
  209. bool is_pos_infinity()const
  210. {
  211. if(traits_type::is_adapted())
  212. {
  213. return ticks_.is_pos_infinity();
  214. }
  215. else{
  216. return false;
  217. }
  218. }
  219. //! Is duration neg-infinity
  220. bool is_neg_infinity()const
  221. {
  222. if(traits_type::is_adapted())
  223. {
  224. return ticks_.is_neg_infinity();
  225. }
  226. else{
  227. return false;
  228. }
  229. }
  230. //! Is duration not-a-date-time
  231. bool is_not_a_date_time()const
  232. {
  233. if(traits_type::is_adapted())
  234. {
  235. return ticks_.is_nan();
  236. }
  237. else{
  238. return false;
  239. }
  240. }
  241. //! Used for special_values output
  242. impl_type get_rep()const
  243. {
  244. return ticks_;
  245. }
  246. protected:
  247. explicit time_duration(impl_type in) : ticks_(in) {}
  248. impl_type ticks_;
  249. };
  250. //! Template for instantiating derived adjusting durations
  251. /* These templates are designed to work with multiples of
  252. * 10 for frac_of_second and resoultion adjustment
  253. */
  254. template<class base_duration, boost::int64_t frac_of_second>
  255. class subsecond_duration : public base_duration
  256. {
  257. public:
  258. typedef typename base_duration::impl_type impl_type;
  259. typedef typename base_duration::traits_type traits_type;
  260. private:
  261. // To avoid integer overflow we precompute the duration resolution conversion coefficient (ticket #3471)
  262. 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,\
  263. "The base duration resolution must be a multiple of the subsecond duration resolution");
  264. 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));
  265. public:
  266. explicit subsecond_duration(boost::int64_t ss) :
  267. base_duration(impl_type(traits_type::ticks_per_second >= frac_of_second ? ss * adjustment_ratio : ss / adjustment_ratio))
  268. {
  269. }
  270. };
  271. } } //namespace date_time
  272. #endif