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