time_point.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. template <class _Clock, class _Duration = typename _Clock::duration>
  27. class _LIBCPP_TEMPLATE_VIS time_point {
  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_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 time_point() : __d_(duration::zero()) {}
  39. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 explicit time_point(const duration& __d) : __d_(__d) {}
  40. // conversions
  41. template <class _Duration2, __enable_if_t<is_convertible<_Duration2, duration>::value, int> = 0>
  42. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 time_point(const time_point<clock, _Duration2>& __t)
  43. : __d_(__t.time_since_epoch()) {}
  44. // observer
  45. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 duration time_since_epoch() const { return __d_; }
  46. // arithmetic
  47. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 time_point& operator+=(const duration& __d) {
  48. __d_ += __d;
  49. return *this;
  50. }
  51. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 time_point& operator-=(const duration& __d) {
  52. __d_ -= __d;
  53. return *this;
  54. }
  55. // special values
  56. _LIBCPP_HIDE_FROM_ABI static _LIBCPP_CONSTEXPR time_point min() _NOEXCEPT { return time_point(duration::min()); }
  57. _LIBCPP_HIDE_FROM_ABI static _LIBCPP_CONSTEXPR time_point max() _NOEXCEPT { return time_point(duration::max()); }
  58. };
  59. } // namespace chrono
  60. template <class _Clock, class _Duration1, class _Duration2>
  61. struct _LIBCPP_TEMPLATE_VIS
  62. common_type<chrono::time_point<_Clock, _Duration1>, chrono::time_point<_Clock, _Duration2> > {
  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_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 time_point<_Clock, _ToDuration>
  68. time_point_cast(const time_point<_Clock, _Duration>& __t) {
  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_HIDE_FROM_ABI _LIBCPP_CONSTEXPR time_point<_Clock, _ToDuration>
  74. floor(const time_point<_Clock, _Duration>& __t) {
  75. return time_point<_Clock, _ToDuration>{chrono::floor<_ToDuration>(__t.time_since_epoch())};
  76. }
  77. template <class _ToDuration, class _Clock, class _Duration, enable_if_t<__is_duration<_ToDuration>::value, int> = 0>
  78. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR time_point<_Clock, _ToDuration>
  79. ceil(const time_point<_Clock, _Duration>& __t) {
  80. return time_point<_Clock, _ToDuration>{chrono::ceil<_ToDuration>(__t.time_since_epoch())};
  81. }
  82. template <class _ToDuration, class _Clock, class _Duration, enable_if_t<__is_duration<_ToDuration>::value, int> = 0>
  83. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR time_point<_Clock, _ToDuration>
  84. round(const time_point<_Clock, _Duration>& __t) {
  85. return time_point<_Clock, _ToDuration>{chrono::round<_ToDuration>(__t.time_since_epoch())};
  86. }
  87. template <class _Rep, class _Period, enable_if_t<numeric_limits<_Rep>::is_signed, int> = 0>
  88. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR duration<_Rep, _Period> abs(duration<_Rep, _Period> __d) {
  89. return __d >= __d.zero() ? +__d : -__d;
  90. }
  91. #endif // _LIBCPP_STD_VER >= 17
  92. // time_point ==
  93. template <class _Clock, class _Duration1, class _Duration2>
  94. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 bool
  95. operator==(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs) {
  96. return __lhs.time_since_epoch() == __rhs.time_since_epoch();
  97. }
  98. #if _LIBCPP_STD_VER <= 17
  99. // time_point !=
  100. template <class _Clock, class _Duration1, class _Duration2>
  101. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 bool
  102. operator!=(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs) {
  103. return !(__lhs == __rhs);
  104. }
  105. #endif // _LIBCPP_STD_VER <= 17
  106. // time_point <
  107. template <class _Clock, class _Duration1, class _Duration2>
  108. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 bool
  109. operator<(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs) {
  110. return __lhs.time_since_epoch() < __rhs.time_since_epoch();
  111. }
  112. // time_point >
  113. template <class _Clock, class _Duration1, class _Duration2>
  114. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 bool
  115. operator>(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs) {
  116. return __rhs < __lhs;
  117. }
  118. // time_point <=
  119. template <class _Clock, class _Duration1, class _Duration2>
  120. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 bool
  121. operator<=(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs) {
  122. return !(__rhs < __lhs);
  123. }
  124. // time_point >=
  125. template <class _Clock, class _Duration1, class _Duration2>
  126. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 bool
  127. operator>=(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs) {
  128. return !(__lhs < __rhs);
  129. }
  130. #if _LIBCPP_STD_VER >= 20
  131. template <class _Clock, class _Duration1, three_way_comparable_with<_Duration1> _Duration2>
  132. _LIBCPP_HIDE_FROM_ABI constexpr auto
  133. operator<=>(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs) {
  134. return __lhs.time_since_epoch() <=> __rhs.time_since_epoch();
  135. }
  136. #endif // _LIBCPP_STD_VER >= 20
  137. // time_point operator+(time_point x, duration y);
  138. template <class _Clock, class _Duration1, class _Rep2, class _Period2>
  139. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14
  140. time_point<_Clock, typename common_type<_Duration1, duration<_Rep2, _Period2> >::type>
  141. operator+(const time_point<_Clock, _Duration1>& __lhs, const duration<_Rep2, _Period2>& __rhs) {
  142. typedef time_point<_Clock, typename common_type<_Duration1, duration<_Rep2, _Period2> >::type> _Tr;
  143. return _Tr(__lhs.time_since_epoch() + __rhs);
  144. }
  145. // time_point operator+(duration x, time_point y);
  146. template <class _Rep1, class _Period1, class _Clock, class _Duration2>
  147. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14
  148. time_point<_Clock, typename common_type<duration<_Rep1, _Period1>, _Duration2>::type>
  149. operator+(const duration<_Rep1, _Period1>& __lhs, const time_point<_Clock, _Duration2>& __rhs) {
  150. return __rhs + __lhs;
  151. }
  152. // time_point operator-(time_point x, duration y);
  153. template <class _Clock, class _Duration1, class _Rep2, class _Period2>
  154. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14
  155. time_point<_Clock, typename common_type<_Duration1, duration<_Rep2, _Period2> >::type>
  156. operator-(const time_point<_Clock, _Duration1>& __lhs, const duration<_Rep2, _Period2>& __rhs) {
  157. typedef time_point<_Clock, typename common_type<_Duration1, duration<_Rep2, _Period2> >::type> _Ret;
  158. return _Ret(__lhs.time_since_epoch() - __rhs);
  159. }
  160. // duration operator-(time_point x, time_point y);
  161. template <class _Clock, class _Duration1, class _Duration2>
  162. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 typename common_type<_Duration1, _Duration2>::type
  163. operator-(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs) {
  164. return __lhs.time_since_epoch() - __rhs.time_since_epoch();
  165. }
  166. } // namespace chrono
  167. _LIBCPP_END_NAMESPACE_STD
  168. _LIBCPP_POP_MACROS
  169. #endif // _LIBCPP___CHRONO_TIME_POINT_H