time_duration.hpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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: 2009-06-04 04:24:49 -0400 (Thu, 04 Jun 2009) $
  9. */
  10. #include <boost/cstdint.hpp>
  11. #include <boost/operators.hpp>
  12. #include <boost/date_time/time_defs.hpp>
  13. #include <boost/date_time/special_defs.hpp>
  14. #include <boost/date_time/compiler_config.hpp>
  15. namespace boost {
  16. namespace date_time {
  17. //! Represents some amount of elapsed time measure to a given resolution
  18. /*! This class represents a standard set of capabilities for all
  19. counted time durations. Time duration implementations should derive
  20. from this class passing their type as the first template parameter.
  21. This design allows the subclass duration types to provide custom
  22. construction policies or other custom features not provided here.
  23. @param T The subclass type
  24. @param rep_type The time resolution traits for this duration type.
  25. */
  26. template<class T, typename rep_type>
  27. class time_duration : private
  28. boost::less_than_comparable<T
  29. , boost::equality_comparable<T
  30. > >
  31. /* dividable, addable, and subtractable operator templates
  32. * won't work with this class (MSVC++ 6.0). return type
  33. * from '+=' is different than expected return type
  34. * from '+'. multipliable probably wont work
  35. * either (haven't tried) */
  36. {
  37. public:
  38. typedef T duration_type; //the subclass
  39. typedef rep_type traits_type;
  40. typedef typename rep_type::day_type day_type;
  41. typedef typename rep_type::hour_type hour_type;
  42. typedef typename rep_type::min_type min_type;
  43. typedef typename rep_type::sec_type sec_type;
  44. typedef typename rep_type::fractional_seconds_type fractional_seconds_type;
  45. typedef typename rep_type::tick_type tick_type;
  46. typedef typename rep_type::impl_type impl_type;
  47. time_duration() : ticks_(0) {}
  48. time_duration(hour_type hours_in,
  49. min_type minutes_in,
  50. sec_type seconds_in=0,
  51. fractional_seconds_type frac_sec_in = 0) :
  52. ticks_(rep_type::to_tick_count(hours_in,minutes_in,seconds_in,frac_sec_in))
  53. {}
  54. // copy constructor required for dividable<>
  55. //! Construct from another time_duration (Copy constructor)
  56. time_duration(const time_duration<T, rep_type>& other)
  57. : ticks_(other.ticks_)
  58. {}
  59. //! Construct from special_values
  60. time_duration(special_values sv) : ticks_(impl_type::from_special(sv))
  61. {}
  62. //! Returns smallest representable duration
  63. static duration_type unit()
  64. {
  65. return duration_type(0,0,0,1);
  66. }
  67. //! Return the number of ticks in a second
  68. static tick_type ticks_per_second()
  69. {
  70. return rep_type::res_adjust();
  71. }
  72. //! Provide the resolution of this duration type
  73. static time_resolutions resolution()
  74. {
  75. return rep_type::resolution();
  76. }
  77. //! Returns number of hours in the duration
  78. hour_type hours() const
  79. {
  80. return static_cast<hour_type>(ticks() / (3600*ticks_per_second()));
  81. }
  82. //! Returns normalized number of minutes
  83. 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. 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. 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. 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. 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. 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. 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 unsigned short num_fractional_digits()
  128. {
  129. return rep_type::num_fractional_digits();
  130. }
  131. duration_type invert_sign() const
  132. {
  133. return duration_type(ticks_ * (-1));
  134. }
  135. bool is_negative() const
  136. {
  137. return ticks_ < 0;
  138. }
  139. bool operator<(const time_duration& rhs) const
  140. {
  141. return ticks_ < rhs.ticks_;
  142. }
  143. bool operator==(const time_duration& rhs) const
  144. {
  145. return ticks_ == rhs.ticks_;
  146. }
  147. //! unary- Allows for time_duration td = -td1
  148. duration_type operator-()const
  149. {
  150. return duration_type(ticks_ * (-1));
  151. }
  152. duration_type operator-(const duration_type& d) const
  153. {
  154. return duration_type(ticks_ - d.ticks_);
  155. }
  156. duration_type operator+(const duration_type& d) const
  157. {
  158. return duration_type(ticks_ + d.ticks_);
  159. }
  160. duration_type operator/(int divisor) const
  161. {
  162. return duration_type(ticks_ / divisor);
  163. }
  164. duration_type operator-=(const duration_type& d)
  165. {
  166. ticks_ = ticks_ - d.ticks_;
  167. return duration_type(ticks_);
  168. }
  169. duration_type operator+=(const duration_type& d)
  170. {
  171. ticks_ = ticks_ + d.ticks_;
  172. return duration_type(ticks_);
  173. }
  174. //! Division operations on a duration with an integer.
  175. duration_type operator/=(int divisor)
  176. {
  177. ticks_ = ticks_ / divisor;
  178. return duration_type(ticks_);
  179. }
  180. //! Multiplication operations an a duration with an integer
  181. duration_type operator*(int rhs) const
  182. {
  183. return duration_type(ticks_ * rhs);
  184. }
  185. duration_type operator*=(int divisor)
  186. {
  187. ticks_ = ticks_ * divisor;
  188. return duration_type(ticks_);
  189. }
  190. tick_type ticks() const
  191. {
  192. return traits_type::as_number(ticks_);
  193. }
  194. //! Is ticks_ a special value?
  195. bool is_special()const
  196. {
  197. if(traits_type::is_adapted())
  198. {
  199. return ticks_.is_special();
  200. }
  201. else{
  202. return false;
  203. }
  204. }
  205. //! Is duration pos-infinity
  206. bool is_pos_infinity()const
  207. {
  208. if(traits_type::is_adapted())
  209. {
  210. return ticks_.is_pos_infinity();
  211. }
  212. else{
  213. return false;
  214. }
  215. }
  216. //! Is duration neg-infinity
  217. bool is_neg_infinity()const
  218. {
  219. if(traits_type::is_adapted())
  220. {
  221. return ticks_.is_neg_infinity();
  222. }
  223. else{
  224. return false;
  225. }
  226. }
  227. //! Is duration not-a-date-time
  228. bool is_not_a_date_time()const
  229. {
  230. if(traits_type::is_adapted())
  231. {
  232. return ticks_.is_nan();
  233. }
  234. else{
  235. return false;
  236. }
  237. }
  238. //! Used for special_values output
  239. impl_type get_rep()const
  240. {
  241. return ticks_;
  242. }
  243. protected:
  244. explicit time_duration(impl_type in) : ticks_(in) {};
  245. impl_type ticks_;
  246. };
  247. //! Template for instantiating derived adjusting durations
  248. /* These templates are designed to work with multiples of
  249. * 10 for frac_of_second and resoultion adjustment
  250. */
  251. template<class base_duration, boost::int64_t frac_of_second>
  252. class subsecond_duration : public base_duration
  253. {
  254. public:
  255. typedef typename base_duration::traits_type traits_type;
  256. explicit subsecond_duration(boost::int64_t ss) :
  257. base_duration(0,0,0,ss*traits_type::res_adjust()/frac_of_second)
  258. {}
  259. };
  260. } } //namespace date_time
  261. #endif