time_point.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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 <__compare/ordering.h>
  13. #include <__compare/three_way_comparable.h>
  14. #include <__config>
  15. #include <__type_traits/common_type.h>
  16. #include <__type_traits/enable_if.h>
  17. #include <__type_traits/is_convertible.h>
  18. #include <limits>
  19. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  20. # pragma GCC system_header
  21. #endif
  22. _LIBCPP_PUSH_MACROS
  23. #include <__undef_macros>
  24. _LIBCPP_BEGIN_NAMESPACE_STD
  25. namespace chrono
  26. {
  27. template <class _Clock, class _Duration = typename _Clock::duration>
  28. class _LIBCPP_TEMPLATE_VIS time_point
  29. {
  30. static_assert(__is_duration<_Duration>::value,
  31. "Second template parameter of time_point must be a std::chrono::duration");
  32. public:
  33. typedef _Clock clock;
  34. typedef _Duration duration;
  35. typedef typename duration::rep rep;
  36. typedef typename duration::period period;
  37. private:
  38. duration __d_;
  39. public:
  40. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14 time_point() : __d_(duration::zero()) {}
  41. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14 explicit time_point(const duration& __d) : __d_(__d) {}
  42. // conversions
  43. template <class _Duration2, __enable_if_t<is_convertible<_Duration2, duration>::value, int> = 0>
  44. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14
  45. time_point(const time_point<clock, _Duration2>& __t)
  46. : __d_(__t.time_since_epoch()) {}
  47. // observer
  48. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14 duration time_since_epoch() const {return __d_;}
  49. // arithmetic
  50. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX17 time_point& operator+=(const duration& __d) {__d_ += __d; return *this;}
  51. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX17 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_SINCE_CXX14
  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 >= 17
  72. template <class _ToDuration, class _Clock, class _Duration, enable_if_t<__is_duration<_ToDuration>::value, int> = 0>
  73. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
  74. time_point<_Clock, _ToDuration>
  75. floor(const time_point<_Clock, _Duration>& __t)
  76. {
  77. return time_point<_Clock, _ToDuration>{chrono::floor<_ToDuration>(__t.time_since_epoch())};
  78. }
  79. template <class _ToDuration, class _Clock, class _Duration, enable_if_t<__is_duration<_ToDuration>::value, int> = 0>
  80. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
  81. time_point<_Clock, _ToDuration>
  82. ceil(const time_point<_Clock, _Duration>& __t)
  83. {
  84. return time_point<_Clock, _ToDuration>{chrono::ceil<_ToDuration>(__t.time_since_epoch())};
  85. }
  86. template <class _ToDuration, class _Clock, class _Duration, enable_if_t<__is_duration<_ToDuration>::value, int> = 0>
  87. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
  88. time_point<_Clock, _ToDuration>
  89. round(const time_point<_Clock, _Duration>& __t)
  90. {
  91. return time_point<_Clock, _ToDuration>{chrono::round<_ToDuration>(__t.time_since_epoch())};
  92. }
  93. template <class _Rep, class _Period, enable_if_t<numeric_limits<_Rep>::is_signed, int> = 0>
  94. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
  95. duration<_Rep, _Period>
  96. abs(duration<_Rep, _Period> __d)
  97. {
  98. return __d >= __d.zero() ? +__d : -__d;
  99. }
  100. #endif // _LIBCPP_STD_VER >= 17
  101. // time_point ==
  102. template <class _Clock, class _Duration1, class _Duration2>
  103. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14
  104. bool
  105. operator==(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs)
  106. {
  107. return __lhs.time_since_epoch() == __rhs.time_since_epoch();
  108. }
  109. #if _LIBCPP_STD_VER <= 17
  110. // time_point !=
  111. template <class _Clock, class _Duration1, class _Duration2>
  112. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14
  113. bool
  114. operator!=(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs)
  115. {
  116. return !(__lhs == __rhs);
  117. }
  118. #endif // _LIBCPP_STD_VER <= 17
  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 __rhs < __lhs;
  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 !(__rhs < __lhs);
  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 !(__lhs < __rhs);
  150. }
  151. #if _LIBCPP_STD_VER >= 20
  152. template <class _Clock, class _Duration1, three_way_comparable_with<_Duration1> _Duration2>
  153. _LIBCPP_HIDE_FROM_ABI constexpr auto
  154. operator<=>(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs) {
  155. return __lhs.time_since_epoch() <=> __rhs.time_since_epoch();
  156. }
  157. #endif // _LIBCPP_STD_VER >= 20
  158. // time_point operator+(time_point x, duration y);
  159. template <class _Clock, class _Duration1, class _Rep2, class _Period2>
  160. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14
  161. time_point<_Clock, typename common_type<_Duration1, duration<_Rep2, _Period2> >::type>
  162. operator+(const time_point<_Clock, _Duration1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
  163. {
  164. typedef time_point<_Clock, typename common_type<_Duration1, duration<_Rep2, _Period2> >::type> _Tr;
  165. return _Tr (__lhs.time_since_epoch() + __rhs);
  166. }
  167. // time_point operator+(duration x, time_point y);
  168. template <class _Rep1, class _Period1, class _Clock, class _Duration2>
  169. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14
  170. time_point<_Clock, typename common_type<duration<_Rep1, _Period1>, _Duration2>::type>
  171. operator+(const duration<_Rep1, _Period1>& __lhs, const time_point<_Clock, _Duration2>& __rhs)
  172. {
  173. return __rhs + __lhs;
  174. }
  175. // time_point operator-(time_point x, duration y);
  176. template <class _Clock, class _Duration1, class _Rep2, class _Period2>
  177. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14
  178. time_point<_Clock, typename common_type<_Duration1, duration<_Rep2, _Period2> >::type>
  179. operator-(const time_point<_Clock, _Duration1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
  180. {
  181. typedef time_point<_Clock, typename common_type<_Duration1, duration<_Rep2, _Period2> >::type> _Ret;
  182. return _Ret(__lhs.time_since_epoch() -__rhs);
  183. }
  184. // duration operator-(time_point x, time_point y);
  185. template <class _Clock, class _Duration1, class _Duration2>
  186. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14
  187. typename common_type<_Duration1, _Duration2>::type
  188. operator-(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs)
  189. {
  190. return __lhs.time_since_epoch() - __rhs.time_since_epoch();
  191. }
  192. } // namespace chrono
  193. _LIBCPP_END_NAMESPACE_STD
  194. _LIBCPP_POP_MACROS
  195. #endif // _LIBCPP___CHRONO_TIME_POINT_H