2 //===----------------------------------------------------------------------===//
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
8 //===----------------------------------------------------------------------===//
10 #ifndef _LIBCPP___CHRONO_TIME_POINT_H
11 #define _LIBCPP___CHRONO_TIME_POINT_H
13 #include <__chrono/duration.h>
14 #include <__compare/ordering.h>
15 #include <__compare/three_way_comparable.h>
17 #include <__type_traits/common_type.h>
18 #include <__type_traits/enable_if.h>
19 #include <__type_traits/is_convertible.h>
22 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
23 # pragma GCC system_header
27 #include <__undef_macros>
29 _LIBCPP_BEGIN_NAMESPACE_STD
34 template <class _Clock
, class _Duration
= typename
_Clock::duration
>
35 class _LIBCPP_TEMPLATE_VIS time_point
37 static_assert(__is_duration
<_Duration
>::value
,
38 "Second template parameter of time_point must be a std::chrono::duration");
41 typedef _Duration duration
;
42 typedef typename
duration::rep rep
;
43 typedef typename
duration::period period
;
48 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14
time_point() : __d_(duration::zero()) {}
49 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14
explicit time_point(const duration
& __d
) : __d_(__d
) {}
52 template <class _Duration2
, __enable_if_t
<is_convertible
<_Duration2
, duration
>::value
, int> = 0>
53 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14
54 time_point(const time_point
<clock
, _Duration2
>& __t
)
55 : __d_(__t
.time_since_epoch()) {}
59 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14 duration
time_since_epoch() const {return __d_
;}
63 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX17 time_point
& operator+=(const duration
& __d
) {__d_
+= __d
; return *this;}
64 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX17 time_point
& operator-=(const duration
& __d
) {__d_
-= __d
; return *this;}
68 _LIBCPP_INLINE_VISIBILITY
static _LIBCPP_CONSTEXPR time_point
min() _NOEXCEPT
{return time_point(duration::min());}
69 _LIBCPP_INLINE_VISIBILITY
static _LIBCPP_CONSTEXPR time_point
max() _NOEXCEPT
{return time_point(duration::max());}
74 template <class _Clock
, class _Duration1
, class _Duration2
>
75 struct _LIBCPP_TEMPLATE_VIS common_type
<chrono::time_point
<_Clock
, _Duration1
>,
76 chrono::time_point
<_Clock
, _Duration2
> >
78 typedef chrono::time_point
<_Clock
, typename common_type
<_Duration1
, _Duration2
>::type
> type
;
83 template <class _ToDuration
, class _Clock
, class _Duration
>
84 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14
85 time_point
<_Clock
, _ToDuration
>
86 time_point_cast(const time_point
<_Clock
, _Duration
>& __t
)
88 return time_point
<_Clock
, _ToDuration
>(chrono::duration_cast
<_ToDuration
>(__t
.time_since_epoch()));
91 #if _LIBCPP_STD_VER >= 17
92 template <class _ToDuration
, class _Clock
, class _Duration
, enable_if_t
<__is_duration
<_ToDuration
>::value
, int> = 0>
93 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
94 time_point
<_Clock
, _ToDuration
>
95 floor(const time_point
<_Clock
, _Duration
>& __t
)
97 return time_point
<_Clock
, _ToDuration
>{chrono::floor
<_ToDuration
>(__t
.time_since_epoch())};
100 template <class _ToDuration
, class _Clock
, class _Duration
, enable_if_t
<__is_duration
<_ToDuration
>::value
, int> = 0>
101 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
102 time_point
<_Clock
, _ToDuration
>
103 ceil(const time_point
<_Clock
, _Duration
>& __t
)
105 return time_point
<_Clock
, _ToDuration
>{chrono::ceil
<_ToDuration
>(__t
.time_since_epoch())};
108 template <class _ToDuration
, class _Clock
, class _Duration
, enable_if_t
<__is_duration
<_ToDuration
>::value
, int> = 0>
109 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
110 time_point
<_Clock
, _ToDuration
>
111 round(const time_point
<_Clock
, _Duration
>& __t
)
113 return time_point
<_Clock
, _ToDuration
>{chrono::round
<_ToDuration
>(__t
.time_since_epoch())};
116 template <class _Rep
, class _Period
, enable_if_t
<numeric_limits
<_Rep
>::is_signed
, int> = 0>
117 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
118 duration
<_Rep
, _Period
>
119 abs(duration
<_Rep
, _Period
> __d
)
121 return __d
>= __d
.zero() ? +__d
: -__d
;
123 #endif // _LIBCPP_STD_VER >= 17
127 template <class _Clock
, class _Duration1
, class _Duration2
>
128 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14
130 operator==(const time_point
<_Clock
, _Duration1
>& __lhs
, const time_point
<_Clock
, _Duration2
>& __rhs
)
132 return __lhs
.time_since_epoch() == __rhs
.time_since_epoch();
135 #if _LIBCPP_STD_VER <= 17
139 template <class _Clock
, class _Duration1
, class _Duration2
>
140 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14
142 operator!=(const time_point
<_Clock
, _Duration1
>& __lhs
, const time_point
<_Clock
, _Duration2
>& __rhs
)
144 return !(__lhs
== __rhs
);
147 #endif // _LIBCPP_STD_VER <= 17
151 template <class _Clock
, class _Duration1
, class _Duration2
>
152 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14
154 operator<(const time_point
<_Clock
, _Duration1
>& __lhs
, const time_point
<_Clock
, _Duration2
>& __rhs
)
156 return __lhs
.time_since_epoch() < __rhs
.time_since_epoch();
161 template <class _Clock
, class _Duration1
, class _Duration2
>
162 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14
164 operator>(const time_point
<_Clock
, _Duration1
>& __lhs
, const time_point
<_Clock
, _Duration2
>& __rhs
)
166 return __rhs
< __lhs
;
171 template <class _Clock
, class _Duration1
, class _Duration2
>
172 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14
174 operator<=(const time_point
<_Clock
, _Duration1
>& __lhs
, const time_point
<_Clock
, _Duration2
>& __rhs
)
176 return !(__rhs
< __lhs
);
181 template <class _Clock
, class _Duration1
, class _Duration2
>
182 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14
184 operator>=(const time_point
<_Clock
, _Duration1
>& __lhs
, const time_point
<_Clock
, _Duration2
>& __rhs
)
186 return !(__lhs
< __rhs
);
189 #if _LIBCPP_STD_VER >= 20
191 template <class _Clock
, class _Duration1
, three_way_comparable_with
<_Duration1
> _Duration2
>
192 _LIBCPP_HIDE_FROM_ABI
constexpr auto
193 operator<=>(const time_point
<_Clock
, _Duration1
>& __lhs
, const time_point
<_Clock
, _Duration2
>& __rhs
) {
194 return __lhs
.time_since_epoch() <=> __rhs
.time_since_epoch();
197 #endif // _LIBCPP_STD_VER >= 20
199 // time_point operator+(time_point x, duration y);
201 template <class _Clock
, class _Duration1
, class _Rep2
, class _Period2
>
202 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14
203 time_point
<_Clock
, typename common_type
<_Duration1
, duration
<_Rep2
, _Period2
> >::type
>
204 operator+(const time_point
<_Clock
, _Duration1
>& __lhs
, const duration
<_Rep2
, _Period2
>& __rhs
)
206 typedef time_point
<_Clock
, typename common_type
<_Duration1
, duration
<_Rep2
, _Period2
> >::type
> _Tr
;
207 return _Tr (__lhs
.time_since_epoch() + __rhs
);
210 // time_point operator+(duration x, time_point y);
212 template <class _Rep1
, class _Period1
, class _Clock
, class _Duration2
>
213 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14
214 time_point
<_Clock
, typename common_type
<duration
<_Rep1
, _Period1
>, _Duration2
>::type
>
215 operator+(const duration
<_Rep1
, _Period1
>& __lhs
, const time_point
<_Clock
, _Duration2
>& __rhs
)
217 return __rhs
+ __lhs
;
220 // time_point operator-(time_point x, duration y);
222 template <class _Clock
, class _Duration1
, class _Rep2
, class _Period2
>
223 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14
224 time_point
<_Clock
, typename common_type
<_Duration1
, duration
<_Rep2
, _Period2
> >::type
>
225 operator-(const time_point
<_Clock
, _Duration1
>& __lhs
, const duration
<_Rep2
, _Period2
>& __rhs
)
227 typedef time_point
<_Clock
, typename common_type
<_Duration1
, duration
<_Rep2
, _Period2
> >::type
> _Ret
;
228 return _Ret(__lhs
.time_since_epoch() -__rhs
);
231 // duration operator-(time_point x, time_point y);
233 template <class _Clock
, class _Duration1
, class _Duration2
>
234 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14
235 typename common_type
<_Duration1
, _Duration2
>::type
236 operator-(const time_point
<_Clock
, _Duration1
>& __lhs
, const time_point
<_Clock
, _Duration2
>& __rhs
)
238 return __lhs
.time_since_epoch() - __rhs
.time_since_epoch();
241 } // namespace chrono
243 _LIBCPP_END_NAMESPACE_STD
247 #endif // _LIBCPP___CHRONO_TIME_POINT_H