time_point.hpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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. #ifndef BOOST_CHRONO_HEADER_ONLY
  26. // this must occur after all of the includes and before any code appears:
  27. #include <boost/config/abi_prefix.hpp> // must be the last #include
  28. #endif
  29. //----------------------------------------------------------------------------//
  30. // //
  31. // 20.9 Time utilities [time] //
  32. // synopsis //
  33. // //
  34. //----------------------------------------------------------------------------//
  35. namespace boost {
  36. namespace chrono {
  37. template <class Clock, class Duration = typename Clock::duration>
  38. class time_point;
  39. } // namespace chrono
  40. // common_type trait specializations
  41. template <class Clock, class Duration1, class Duration2>
  42. struct common_type<chrono::time_point<Clock, Duration1>,
  43. chrono::time_point<Clock, Duration2> >;
  44. //----------------------------------------------------------------------------//
  45. // 20.9.2.3 Specializations of common_type [time.traits.specializations] //
  46. //----------------------------------------------------------------------------//
  47. template <class Clock, class Duration1, class Duration2>
  48. struct common_type<chrono::time_point<Clock, Duration1>,
  49. chrono::time_point<Clock, Duration2> >
  50. {
  51. typedef chrono::time_point<Clock,
  52. typename common_type<Duration1, Duration2>::type> type;
  53. };
  54. namespace chrono {
  55. // time_point arithmetic
  56. template <class Clock, class Duration1, class Rep2, class Period2>
  57. inline BOOST_CONSTEXPR
  58. time_point<Clock,
  59. typename common_type<Duration1, duration<Rep2, Period2> >::type>
  60. operator+(
  61. const time_point<Clock, Duration1>& lhs,
  62. const duration<Rep2, Period2>& rhs);
  63. template <class Rep1, class Period1, class Clock, class Duration2>
  64. inline BOOST_CONSTEXPR
  65. time_point<Clock,
  66. typename common_type<duration<Rep1, Period1>, Duration2>::type>
  67. operator+(
  68. const duration<Rep1, Period1>& lhs,
  69. const time_point<Clock, Duration2>& rhs);
  70. template <class Clock, class Duration1, class Rep2, class Period2>
  71. inline BOOST_CONSTEXPR
  72. time_point<Clock,
  73. typename common_type<Duration1, duration<Rep2, Period2> >::type>
  74. operator-(
  75. const time_point<Clock, Duration1>& lhs,
  76. const duration<Rep2, Period2>& rhs);
  77. template <class Clock, class Duration1, class Duration2>
  78. inline BOOST_CONSTEXPR
  79. typename common_type<Duration1, Duration2>::type
  80. operator-(
  81. const time_point<Clock, Duration1>& lhs,
  82. const time_point<Clock,
  83. Duration2>& rhs);
  84. // time_point comparisons
  85. template <class Clock, class Duration1, class Duration2>
  86. inline BOOST_CONSTEXPR
  87. bool operator==(
  88. const time_point<Clock, Duration1>& lhs,
  89. const time_point<Clock, Duration2>& rhs);
  90. template <class Clock, class Duration1, class Duration2>
  91. inline BOOST_CONSTEXPR
  92. bool operator!=(
  93. const time_point<Clock, Duration1>& lhs,
  94. const time_point<Clock, Duration2>& rhs);
  95. template <class Clock, class Duration1, class Duration2>
  96. inline BOOST_CONSTEXPR
  97. bool operator< (
  98. const time_point<Clock, Duration1>& lhs,
  99. const time_point<Clock, Duration2>& rhs);
  100. template <class Clock, class Duration1, class Duration2>
  101. inline BOOST_CONSTEXPR
  102. bool operator<=(
  103. const time_point<Clock, Duration1>& lhs,
  104. const time_point<Clock, Duration2>& rhs);
  105. template <class Clock, class Duration1, class Duration2>
  106. inline BOOST_CONSTEXPR
  107. bool operator> (
  108. const time_point<Clock, Duration1>& lhs,
  109. const time_point<Clock, Duration2>& rhs);
  110. template <class Clock, class Duration1, class Duration2>
  111. inline BOOST_CONSTEXPR
  112. bool operator>=(
  113. const time_point<Clock, Duration1>& lhs,
  114. const time_point<Clock, Duration2>& rhs);
  115. // time_point_cast
  116. template <class ToDuration, class Clock, class Duration>
  117. inline BOOST_CONSTEXPR
  118. time_point<Clock, ToDuration> time_point_cast(const time_point<Clock, Duration>& t);
  119. //----------------------------------------------------------------------------//
  120. // //
  121. // 20.9.4 Class template time_point [time.point] //
  122. // //
  123. //----------------------------------------------------------------------------//
  124. template <class Clock, class Duration>
  125. class time_point
  126. {
  127. BOOST_CHRONO_STATIC_ASSERT(boost::chrono::detail::is_duration<Duration>::value,
  128. BOOST_CHRONO_SECOND_TEMPLATE_PARAMETER_OF_TIME_POINT_MUST_BE_A_BOOST_CHRONO_DURATION, (Duration));
  129. public:
  130. typedef Clock clock;
  131. typedef Duration duration;
  132. typedef typename duration::rep rep;
  133. typedef typename duration::period period;
  134. typedef Duration difference_type;
  135. private:
  136. duration d_;
  137. public:
  138. BOOST_FORCEINLINE BOOST_CONSTEXPR
  139. time_point() : d_(duration::zero())
  140. {}
  141. BOOST_FORCEINLINE BOOST_CONSTEXPR
  142. explicit time_point(const duration& d)
  143. : d_(d)
  144. {}
  145. // conversions
  146. template <class Duration2>
  147. BOOST_FORCEINLINE BOOST_CONSTEXPR
  148. time_point(const time_point<clock, Duration2>& t
  149. , typename boost::enable_if
  150. <
  151. boost::is_convertible<Duration2, duration>
  152. >::type* = 0
  153. )
  154. : d_(t.time_since_epoch())
  155. {
  156. }
  157. // observer
  158. BOOST_CONSTEXPR
  159. duration time_since_epoch() const
  160. {
  161. return d_;
  162. }
  163. // arithmetic
  164. #ifdef BOOST_CHRONO_EXTENSIONS
  165. BOOST_CONSTEXPR
  166. time_point operator+() const {return *this;}
  167. BOOST_CONSTEXPR
  168. time_point operator-() const {return time_point(-d_);}
  169. time_point& operator++() {++d_; return *this;}
  170. time_point operator++(int) {return time_point(d_++);}
  171. time_point& operator--() {--d_; return *this;}
  172. time_point operator--(int) {return time_point(d_--);}
  173. time_point& operator+=(const rep& r) {d_ += duration(r); return *this;}
  174. time_point& operator-=(const rep& r) {d_ -= duration(r); return *this;}
  175. #endif
  176. time_point& operator+=(const duration& d) {d_ += d; return *this;}
  177. time_point& operator-=(const duration& d) {d_ -= d; return *this;}
  178. // special values
  179. static BOOST_CHRONO_LIB_CONSTEXPR time_point
  180. min BOOST_PREVENT_MACRO_SUBSTITUTION ()
  181. {
  182. return time_point((duration::min)());
  183. }
  184. static BOOST_CHRONO_LIB_CONSTEXPR time_point
  185. max BOOST_PREVENT_MACRO_SUBSTITUTION ()
  186. {
  187. return time_point((duration::max)());
  188. }
  189. };
  190. //----------------------------------------------------------------------------//
  191. // 20.9.4.5 time_point non-member arithmetic [time.point.nonmember] //
  192. //----------------------------------------------------------------------------//
  193. // time_point operator+(time_point x, duration y);
  194. template <class Clock, class Duration1, class Rep2, class Period2>
  195. inline BOOST_CONSTEXPR
  196. time_point<Clock,
  197. typename common_type<Duration1, duration<Rep2, Period2> >::type>
  198. operator+(const time_point<Clock, Duration1>& lhs,
  199. const duration<Rep2, Period2>& rhs)
  200. {
  201. typedef typename common_type<Duration1, duration<Rep2, Period2> >::type CDuration;
  202. typedef time_point<
  203. Clock,
  204. CDuration
  205. > TimeResult;
  206. return TimeResult(lhs.time_since_epoch() + CDuration(rhs));
  207. }
  208. // time_point operator+(duration x, time_point y);
  209. template <class Rep1, class Period1, class Clock, class Duration2>
  210. inline BOOST_CONSTEXPR
  211. time_point<Clock,
  212. typename common_type<duration<Rep1, Period1>, Duration2>::type>
  213. operator+(const duration<Rep1, Period1>& lhs,
  214. const time_point<Clock, Duration2>& rhs)
  215. {
  216. return rhs + lhs;
  217. }
  218. // time_point operator-(time_point x, duration y);
  219. template <class Clock, class Duration1, class Rep2, class Period2>
  220. inline BOOST_CONSTEXPR
  221. time_point<Clock,
  222. typename common_type<Duration1, duration<Rep2, Period2> >::type>
  223. operator-(const time_point<Clock, Duration1>& lhs,
  224. const duration<Rep2, Period2>& rhs)
  225. {
  226. return lhs + (-rhs);
  227. }
  228. // duration operator-(time_point x, time_point y);
  229. template <class Clock, class Duration1, class Duration2>
  230. inline BOOST_CONSTEXPR
  231. typename common_type<Duration1, Duration2>::type
  232. operator-(const time_point<Clock, Duration1>& lhs,
  233. const time_point<Clock, Duration2>& rhs)
  234. {
  235. return lhs.time_since_epoch() - rhs.time_since_epoch();
  236. }
  237. //----------------------------------------------------------------------------//
  238. // 20.9.4.6 time_point comparisons [time.point.comparisons] //
  239. //----------------------------------------------------------------------------//
  240. // time_point ==
  241. template <class Clock, class Duration1, class Duration2>
  242. inline BOOST_CONSTEXPR
  243. bool
  244. operator==(const time_point<Clock, Duration1>& lhs,
  245. const time_point<Clock, Duration2>& rhs)
  246. {
  247. return lhs.time_since_epoch() == rhs.time_since_epoch();
  248. }
  249. // time_point !=
  250. template <class Clock, class Duration1, class Duration2>
  251. inline BOOST_CONSTEXPR
  252. bool
  253. operator!=(const time_point<Clock, Duration1>& lhs,
  254. const time_point<Clock, Duration2>& rhs)
  255. {
  256. return !(lhs == rhs);
  257. }
  258. // time_point <
  259. template <class Clock, class Duration1, class Duration2>
  260. inline BOOST_CONSTEXPR
  261. bool
  262. operator<(const time_point<Clock, Duration1>& lhs,
  263. const time_point<Clock, Duration2>& rhs)
  264. {
  265. return lhs.time_since_epoch() < rhs.time_since_epoch();
  266. }
  267. // time_point >
  268. template <class Clock, class Duration1, class Duration2>
  269. inline BOOST_CONSTEXPR
  270. bool
  271. operator>(const time_point<Clock, Duration1>& lhs,
  272. const time_point<Clock, Duration2>& rhs)
  273. {
  274. return rhs < lhs;
  275. }
  276. // time_point <=
  277. template <class Clock, class Duration1, class Duration2>
  278. inline BOOST_CONSTEXPR
  279. bool
  280. operator<=(const time_point<Clock, Duration1>& lhs,
  281. const time_point<Clock, Duration2>& rhs)
  282. {
  283. return !(rhs < lhs);
  284. }
  285. // time_point >=
  286. template <class Clock, class Duration1, class Duration2>
  287. inline BOOST_CONSTEXPR
  288. bool
  289. operator>=(const time_point<Clock, Duration1>& lhs,
  290. const time_point<Clock, Duration2>& rhs)
  291. {
  292. return !(lhs < rhs);
  293. }
  294. //----------------------------------------------------------------------------//
  295. // 20.9.4.7 time_point_cast [time.point.cast] //
  296. //----------------------------------------------------------------------------//
  297. template <class ToDuration, class Clock, class Duration>
  298. inline BOOST_CONSTEXPR
  299. time_point<Clock, ToDuration>
  300. time_point_cast(const time_point<Clock, Duration>& t)
  301. {
  302. return time_point<Clock, ToDuration>(
  303. duration_cast<ToDuration>(t.time_since_epoch()));
  304. }
  305. } // namespace chrono
  306. } // namespace boost
  307. #ifndef BOOST_CHRONO_HEADER_ONLY
  308. // the suffix header occurs after all of our code:
  309. #include <boost/config/abi_suffix.hpp> // pops abi_prefix.hpp pragmas
  310. #endif
  311. #endif // BOOST_CHRONO_TIME_POINT_HPP