time_point.hpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. // duration.hpp --------------------------------------------------------------//
  2. // Copyright 2008 Howard Hinnant
  3. // Copyright 2008 Beman Dawes
  4. // Copyright 2009-2012 Vicente J. Botet Escriba
  5. // Distributed under the Boost Software License, Version 1.0.
  6. // See http://www.boost.org/LICENSE_1_0.txt
  7. /*
  8. This code was derived by Beman Dawes from Howard Hinnant's time2_demo prototype.
  9. Many thanks to Howard for making his code available under the Boost license.
  10. The original code was modified to conform to Boost conventions and to section
  11. 20.9 Time utilities [time] of the C++ committee's working paper N2798.
  12. See http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2798.pdf.
  13. time2_demo contained this comment:
  14. Much thanks to Andrei Alexandrescu,
  15. Walter Brown,
  16. Peter Dimov,
  17. Jeff Garland,
  18. Terry Golubiewski,
  19. Daniel Krugler,
  20. Anthony Williams.
  21. */
  22. #ifndef BOOST_CHRONO_TIME_POINT_HPP
  23. #define BOOST_CHRONO_TIME_POINT_HPP
  24. #include <boost/chrono/duration.hpp>
  25. #include <iostream>
  26. #ifndef BOOST_CHRONO_HEADER_ONLY
  27. // this must occur after all of the includes and before any code appears:
  28. #include <boost/config/abi_prefix.hpp> // must be the last #include
  29. #endif
  30. //----------------------------------------------------------------------------//
  31. // //
  32. // 20.9 Time utilities [time] //
  33. // synopsis //
  34. // //
  35. //----------------------------------------------------------------------------//
  36. namespace boost {
  37. namespace chrono {
  38. template <class Clock, class Duration = typename Clock::duration>
  39. class time_point;
  40. } // namespace chrono
  41. // common_type trait specializations
  42. template <class Clock, class Duration1, class Duration2>
  43. struct common_type<chrono::time_point<Clock, Duration1>,
  44. chrono::time_point<Clock, Duration2> >;
  45. //----------------------------------------------------------------------------//
  46. // 20.9.2.3 Specializations of common_type [time.traits.specializations] //
  47. //----------------------------------------------------------------------------//
  48. template <class Clock, class Duration1, class Duration2>
  49. struct common_type<chrono::time_point<Clock, Duration1>,
  50. chrono::time_point<Clock, Duration2> >
  51. {
  52. typedef chrono::time_point<Clock,
  53. typename common_type<Duration1, Duration2>::type> type;
  54. };
  55. namespace chrono {
  56. // time_point arithmetic
  57. template <class Clock, class Duration1, class Rep2, class Period2>
  58. inline BOOST_CONSTEXPR
  59. time_point<Clock,
  60. typename common_type<Duration1, duration<Rep2, Period2> >::type>
  61. operator+(
  62. const time_point<Clock, Duration1>& lhs,
  63. const duration<Rep2, Period2>& rhs);
  64. template <class Rep1, class Period1, class Clock, class Duration2>
  65. inline BOOST_CONSTEXPR
  66. time_point<Clock,
  67. typename common_type<duration<Rep1, Period1>, Duration2>::type>
  68. operator+(
  69. const duration<Rep1, Period1>& lhs,
  70. const time_point<Clock, Duration2>& rhs);
  71. template <class Clock, class Duration1, class Rep2, class Period2>
  72. inline BOOST_CONSTEXPR
  73. time_point<Clock,
  74. typename common_type<Duration1, duration<Rep2, Period2> >::type>
  75. operator-(
  76. const time_point<Clock, Duration1>& lhs,
  77. const duration<Rep2, Period2>& rhs);
  78. template <class Clock, class Duration1, class Duration2>
  79. inline BOOST_CONSTEXPR
  80. typename common_type<Duration1, Duration2>::type
  81. operator-(
  82. const time_point<Clock, Duration1>& lhs,
  83. const time_point<Clock,
  84. Duration2>& rhs);
  85. // time_point comparisons
  86. template <class Clock, class Duration1, class Duration2>
  87. inline BOOST_CONSTEXPR
  88. bool operator==(
  89. const time_point<Clock, Duration1>& lhs,
  90. const time_point<Clock, Duration2>& rhs);
  91. template <class Clock, class Duration1, class Duration2>
  92. inline BOOST_CONSTEXPR
  93. bool operator!=(
  94. const time_point<Clock, Duration1>& lhs,
  95. const time_point<Clock, Duration2>& rhs);
  96. template <class Clock, class Duration1, class Duration2>
  97. inline BOOST_CONSTEXPR
  98. bool operator< (
  99. const time_point<Clock, Duration1>& lhs,
  100. const time_point<Clock, Duration2>& rhs);
  101. template <class Clock, class Duration1, class Duration2>
  102. inline BOOST_CONSTEXPR
  103. bool operator<=(
  104. const time_point<Clock, Duration1>& lhs,
  105. const time_point<Clock, Duration2>& rhs);
  106. template <class Clock, class Duration1, class Duration2>
  107. inline BOOST_CONSTEXPR
  108. bool operator> (
  109. const time_point<Clock, Duration1>& lhs,
  110. const time_point<Clock, Duration2>& rhs);
  111. template <class Clock, class Duration1, class Duration2>
  112. inline BOOST_CONSTEXPR
  113. bool operator>=(
  114. const time_point<Clock, Duration1>& lhs,
  115. const time_point<Clock, Duration2>& rhs);
  116. // time_point_cast
  117. template <class ToDuration, class Clock, class Duration>
  118. inline BOOST_CONSTEXPR
  119. time_point<Clock, ToDuration> time_point_cast(const time_point<Clock, Duration>& t);
  120. //----------------------------------------------------------------------------//
  121. // //
  122. // 20.9.4 Class template time_point [time.point] //
  123. // //
  124. //----------------------------------------------------------------------------//
  125. template <class Clock, class Duration>
  126. class time_point
  127. {
  128. BOOST_CHRONO_STATIC_ASSERT(boost::chrono::detail::is_duration<Duration>::value,
  129. BOOST_CHRONO_SECOND_TEMPLATE_PARAMETER_OF_TIME_POINT_MUST_BE_A_BOOST_CHRONO_DURATION, (Duration));
  130. public:
  131. typedef Clock clock;
  132. typedef Duration duration;
  133. typedef typename duration::rep rep;
  134. typedef typename duration::period period;
  135. typedef Duration difference_type;
  136. private:
  137. duration d_;
  138. public:
  139. BOOST_FORCEINLINE BOOST_CONSTEXPR
  140. time_point() : d_(duration::zero())
  141. {}
  142. BOOST_FORCEINLINE BOOST_CONSTEXPR
  143. explicit time_point(const duration& d)
  144. : d_(d)
  145. {}
  146. // conversions
  147. template <class Duration2>
  148. BOOST_FORCEINLINE BOOST_CONSTEXPR
  149. time_point(const time_point<clock, Duration2>& t
  150. , typename boost::enable_if
  151. <
  152. boost::is_convertible<Duration2, duration>
  153. >::type* = 0
  154. )
  155. : d_(t.time_since_epoch())
  156. {
  157. }
  158. // observer
  159. BOOST_CONSTEXPR
  160. duration time_since_epoch() const
  161. {
  162. return d_;
  163. }
  164. // arithmetic
  165. #ifdef BOOST_CHRONO_EXTENSIONS
  166. BOOST_CONSTEXPR
  167. time_point operator+() const {return *this;}
  168. BOOST_CONSTEXPR
  169. time_point operator-() const {return time_point(-d_);}
  170. time_point& operator++() {++d_; return *this;}
  171. time_point operator++(int) {return time_point(d_++);}
  172. time_point& operator--() {--d_; return *this;}
  173. time_point operator--(int) {return time_point(d_--);}
  174. time_point& operator+=(const rep& r) {d_ += duration(r); return *this;}
  175. time_point& operator-=(const rep& r) {d_ -= duration(r); return *this;}
  176. #endif
  177. time_point& operator+=(const duration& d) {d_ += d; return *this;}
  178. time_point& operator-=(const duration& d) {d_ -= d; return *this;}
  179. // special values
  180. static BOOST_CHRONO_LIB_CONSTEXPR time_point
  181. min BOOST_PREVENT_MACRO_SUBSTITUTION ()
  182. {
  183. return time_point((duration::min)());
  184. }
  185. static BOOST_CHRONO_LIB_CONSTEXPR time_point
  186. max BOOST_PREVENT_MACRO_SUBSTITUTION ()
  187. {
  188. return time_point((duration::max)());
  189. }
  190. };
  191. //----------------------------------------------------------------------------//
  192. // 20.9.4.5 time_point non-member arithmetic [time.point.nonmember] //
  193. //----------------------------------------------------------------------------//
  194. // time_point operator+(time_point x, duration y);
  195. template <class Clock, class Duration1, class Rep2, class Period2>
  196. inline BOOST_CONSTEXPR
  197. time_point<Clock,
  198. typename common_type<Duration1, duration<Rep2, Period2> >::type>
  199. operator+(const time_point<Clock, Duration1>& lhs,
  200. const duration<Rep2, Period2>& rhs)
  201. {
  202. typedef typename common_type<Duration1, duration<Rep2, Period2> >::type CDuration;
  203. typedef time_point<
  204. Clock,
  205. CDuration
  206. > TimeResult;
  207. return TimeResult(lhs.time_since_epoch() + CDuration(rhs));
  208. }
  209. // time_point operator+(duration x, time_point y);
  210. template <class Rep1, class Period1, class Clock, class Duration2>
  211. inline BOOST_CONSTEXPR
  212. time_point<Clock,
  213. typename common_type<duration<Rep1, Period1>, Duration2>::type>
  214. operator+(const duration<Rep1, Period1>& lhs,
  215. const time_point<Clock, Duration2>& rhs)
  216. {
  217. return rhs + lhs;
  218. }
  219. // time_point operator-(time_point x, duration y);
  220. template <class Clock, class Duration1, class Rep2, class Period2>
  221. inline BOOST_CONSTEXPR
  222. time_point<Clock,
  223. typename common_type<Duration1, duration<Rep2, Period2> >::type>
  224. operator-(const time_point<Clock, Duration1>& lhs,
  225. const duration<Rep2, Period2>& rhs)
  226. {
  227. return lhs + (-rhs);
  228. }
  229. // duration operator-(time_point x, time_point y);
  230. template <class Clock, class Duration1, class Duration2>
  231. inline BOOST_CONSTEXPR
  232. typename common_type<Duration1, Duration2>::type
  233. operator-(const time_point<Clock, Duration1>& lhs,
  234. const time_point<Clock, Duration2>& rhs)
  235. {
  236. return lhs.time_since_epoch() - rhs.time_since_epoch();
  237. }
  238. //----------------------------------------------------------------------------//
  239. // 20.9.4.6 time_point comparisons [time.point.comparisons] //
  240. //----------------------------------------------------------------------------//
  241. // time_point ==
  242. template <class Clock, class Duration1, class Duration2>
  243. inline BOOST_CONSTEXPR
  244. bool
  245. operator==(const time_point<Clock, Duration1>& lhs,
  246. const time_point<Clock, Duration2>& rhs)
  247. {
  248. return lhs.time_since_epoch() == rhs.time_since_epoch();
  249. }
  250. // time_point !=
  251. template <class Clock, class Duration1, class Duration2>
  252. inline BOOST_CONSTEXPR
  253. bool
  254. operator!=(const time_point<Clock, Duration1>& lhs,
  255. const time_point<Clock, Duration2>& rhs)
  256. {
  257. return !(lhs == rhs);
  258. }
  259. // time_point <
  260. template <class Clock, class Duration1, class Duration2>
  261. inline BOOST_CONSTEXPR
  262. bool
  263. operator<(const time_point<Clock, Duration1>& lhs,
  264. const time_point<Clock, Duration2>& rhs)
  265. {
  266. return lhs.time_since_epoch() < rhs.time_since_epoch();
  267. }
  268. // time_point >
  269. template <class Clock, class Duration1, class Duration2>
  270. inline BOOST_CONSTEXPR
  271. bool
  272. operator>(const time_point<Clock, Duration1>& lhs,
  273. const time_point<Clock, Duration2>& rhs)
  274. {
  275. return rhs < lhs;
  276. }
  277. // time_point <=
  278. template <class Clock, class Duration1, class Duration2>
  279. inline BOOST_CONSTEXPR
  280. bool
  281. operator<=(const time_point<Clock, Duration1>& lhs,
  282. const time_point<Clock, Duration2>& rhs)
  283. {
  284. return !(rhs < lhs);
  285. }
  286. // time_point >=
  287. template <class Clock, class Duration1, class Duration2>
  288. inline BOOST_CONSTEXPR
  289. bool
  290. operator>=(const time_point<Clock, Duration1>& lhs,
  291. const time_point<Clock, Duration2>& rhs)
  292. {
  293. return !(lhs < rhs);
  294. }
  295. //----------------------------------------------------------------------------//
  296. // 20.9.4.7 time_point_cast [time.point.cast] //
  297. //----------------------------------------------------------------------------//
  298. template <class ToDuration, class Clock, class Duration>
  299. inline BOOST_CONSTEXPR
  300. time_point<Clock, ToDuration>
  301. time_point_cast(const time_point<Clock, Duration>& t)
  302. {
  303. return time_point<Clock, ToDuration>(
  304. duration_cast<ToDuration>(t.time_since_epoch()));
  305. }
  306. } // namespace chrono
  307. } // namespace boost
  308. #ifndef BOOST_CHRONO_HEADER_ONLY
  309. // the suffix header occurs after all of our code:
  310. #include <boost/config/abi_suffix.hpp> // pops abi_prefix.hpp pragmas
  311. #endif
  312. #endif // BOOST_CHRONO_TIME_POINT_HPP