time_point.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. // -*- C++ -*-
  2. //===----------------------------------------------------------------------===//
  3. //
  4. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  5. // See https://llvm.org/LICENSE.txt for license information.
  6. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #ifndef _LIBCPP___CHRONO_TIME_POINT_H
  10. #define _LIBCPP___CHRONO_TIME_POINT_H
  11. #include <__chrono/duration.h>
  12. #include <__config>
  13. #include <limits>
  14. #include <type_traits>
  15. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  16. # pragma GCC system_header
  17. #endif
  18. _LIBCPP_PUSH_MACROS
  19. #include <__undef_macros>
  20. _LIBCPP_BEGIN_NAMESPACE_STD
  21. namespace chrono
  22. {
  23. template <class _Clock, class _Duration = typename _Clock::duration>
  24. class _LIBCPP_TEMPLATE_VIS time_point
  25. {
  26. static_assert(__is_duration<_Duration>::value,
  27. "Second template parameter of time_point must be a std::chrono::duration");
  28. public:
  29. typedef _Clock clock;
  30. typedef _Duration duration;
  31. typedef typename duration::rep rep;
  32. typedef typename duration::period period;
  33. private:
  34. duration __d_;
  35. public:
  36. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 time_point() : __d_(duration::zero()) {}
  37. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 explicit time_point(const duration& __d) : __d_(__d) {}
  38. // conversions
  39. template <class _Duration2>
  40. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  41. time_point(const time_point<clock, _Duration2>& t,
  42. typename enable_if
  43. <
  44. is_convertible<_Duration2, duration>::value
  45. >::type* = nullptr)
  46. : __d_(t.time_since_epoch()) {}
  47. // observer
  48. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 duration time_since_epoch() const {return __d_;}
  49. // arithmetic
  50. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 time_point& operator+=(const duration& __d) {__d_ += __d; return *this;}
  51. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 time_point& operator-=(const duration& __d) {__d_ -= __d; return *this;}
  52. // special values
  53. _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR time_point min() _NOEXCEPT {return time_point(duration::min());}
  54. _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR time_point max() _NOEXCEPT {return time_point(duration::max());}
  55. };
  56. } // namespace chrono
  57. template <class _Clock, class _Duration1, class _Duration2>
  58. struct _LIBCPP_TEMPLATE_VIS common_type<chrono::time_point<_Clock, _Duration1>,
  59. chrono::time_point<_Clock, _Duration2> >
  60. {
  61. typedef chrono::time_point<_Clock, typename common_type<_Duration1, _Duration2>::type> type;
  62. };
  63. namespace chrono {
  64. template <class _ToDuration, class _Clock, class _Duration>
  65. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  66. time_point<_Clock, _ToDuration>
  67. time_point_cast(const time_point<_Clock, _Duration>& __t)
  68. {
  69. return time_point<_Clock, _ToDuration>(chrono::duration_cast<_ToDuration>(__t.time_since_epoch()));
  70. }
  71. #if _LIBCPP_STD_VER > 14
  72. template <class _ToDuration, class _Clock, class _Duration>
  73. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
  74. typename enable_if
  75. <
  76. __is_duration<_ToDuration>::value,
  77. time_point<_Clock, _ToDuration>
  78. >::type
  79. floor(const time_point<_Clock, _Duration>& __t)
  80. {
  81. return time_point<_Clock, _ToDuration>{floor<_ToDuration>(__t.time_since_epoch())};
  82. }
  83. template <class _ToDuration, class _Clock, class _Duration>
  84. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
  85. typename enable_if
  86. <
  87. __is_duration<_ToDuration>::value,
  88. time_point<_Clock, _ToDuration>
  89. >::type
  90. ceil(const time_point<_Clock, _Duration>& __t)
  91. {
  92. return time_point<_Clock, _ToDuration>{ceil<_ToDuration>(__t.time_since_epoch())};
  93. }
  94. template <class _ToDuration, class _Clock, class _Duration>
  95. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
  96. typename enable_if
  97. <
  98. __is_duration<_ToDuration>::value,
  99. time_point<_Clock, _ToDuration>
  100. >::type
  101. round(const time_point<_Clock, _Duration>& __t)
  102. {
  103. return time_point<_Clock, _ToDuration>{round<_ToDuration>(__t.time_since_epoch())};
  104. }
  105. template <class _Rep, class _Period>
  106. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
  107. typename enable_if
  108. <
  109. numeric_limits<_Rep>::is_signed,
  110. duration<_Rep, _Period>
  111. >::type
  112. abs(duration<_Rep, _Period> __d)
  113. {
  114. return __d >= __d.zero() ? +__d : -__d;
  115. }
  116. #endif // _LIBCPP_STD_VER > 14
  117. // time_point ==
  118. template <class _Clock, class _Duration1, class _Duration2>
  119. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  120. bool
  121. operator==(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs)
  122. {
  123. return __lhs.time_since_epoch() == __rhs.time_since_epoch();
  124. }
  125. // time_point !=
  126. template <class _Clock, class _Duration1, class _Duration2>
  127. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  128. bool
  129. operator!=(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs)
  130. {
  131. return !(__lhs == __rhs);
  132. }
  133. // time_point <
  134. template <class _Clock, class _Duration1, class _Duration2>
  135. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  136. bool
  137. operator<(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs)
  138. {
  139. return __lhs.time_since_epoch() < __rhs.time_since_epoch();
  140. }
  141. // time_point >
  142. template <class _Clock, class _Duration1, class _Duration2>
  143. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  144. bool
  145. operator>(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs)
  146. {
  147. return __rhs < __lhs;
  148. }
  149. // time_point <=
  150. template <class _Clock, class _Duration1, class _Duration2>
  151. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  152. bool
  153. operator<=(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs)
  154. {
  155. return !(__rhs < __lhs);
  156. }
  157. // time_point >=
  158. template <class _Clock, class _Duration1, class _Duration2>
  159. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  160. bool
  161. operator>=(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs)
  162. {
  163. return !(__lhs < __rhs);
  164. }
  165. // time_point operator+(time_point x, duration y);
  166. template <class _Clock, class _Duration1, class _Rep2, class _Period2>
  167. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  168. time_point<_Clock, typename common_type<_Duration1, duration<_Rep2, _Period2> >::type>
  169. operator+(const time_point<_Clock, _Duration1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
  170. {
  171. typedef time_point<_Clock, typename common_type<_Duration1, duration<_Rep2, _Period2> >::type> _Tr;
  172. return _Tr (__lhs.time_since_epoch() + __rhs);
  173. }
  174. // time_point operator+(duration x, time_point y);
  175. template <class _Rep1, class _Period1, class _Clock, class _Duration2>
  176. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  177. time_point<_Clock, typename common_type<duration<_Rep1, _Period1>, _Duration2>::type>
  178. operator+(const duration<_Rep1, _Period1>& __lhs, const time_point<_Clock, _Duration2>& __rhs)
  179. {
  180. return __rhs + __lhs;
  181. }
  182. // time_point operator-(time_point x, duration y);
  183. template <class _Clock, class _Duration1, class _Rep2, class _Period2>
  184. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  185. time_point<_Clock, typename common_type<_Duration1, duration<_Rep2, _Period2> >::type>
  186. operator-(const time_point<_Clock, _Duration1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
  187. {
  188. typedef time_point<_Clock, typename common_type<_Duration1, duration<_Rep2, _Period2> >::type> _Ret;
  189. return _Ret(__lhs.time_since_epoch() -__rhs);
  190. }
  191. // duration operator-(time_point x, time_point y);
  192. template <class _Clock, class _Duration1, class _Duration2>
  193. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  194. typename common_type<_Duration1, _Duration2>::type
  195. operator-(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs)
  196. {
  197. return __lhs.time_since_epoch() - __rhs.time_since_epoch();
  198. }
  199. } // namespace chrono
  200. _LIBCPP_END_NAMESPACE_STD
  201. _LIBCPP_POP_MACROS
  202. #endif // _LIBCPP___CHRONO_TIME_POINT_H