time_point.h 8.2 KB

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