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>
16 #include <type_traits>
18 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
19 #pragma GCC system_header
23 #include <__undef_macros>
25 _LIBCPP_BEGIN_NAMESPACE_STD
30 template <class _Clock
, class _Duration
= typename
_Clock::duration
>
31 class _LIBCPP_TEMPLATE_VIS time_point
33 static_assert(__is_duration
<_Duration
>::value
,
34 "Second template parameter of time_point must be a std::chrono::duration");
37 typedef _Duration duration
;
38 typedef typename
duration::rep rep
;
39 typedef typename
duration::period period
;
44 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
time_point() : __d_(duration::zero()) {}
45 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
explicit time_point(const duration
& __d
) : __d_(__d
) {}
48 template <class _Duration2
>
49 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
50 time_point(const time_point
<clock
, _Duration2
>& t
,
53 is_convertible
<_Duration2
, duration
>::value
55 : __d_(t
.time_since_epoch()) {}
59 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 duration
time_since_epoch() const {return __d_
;}
63 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 time_point
& operator+=(const duration
& __d
) {__d_
+= __d
; return *this;}
64 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 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_AFTER_CXX11
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 > 14
92 template <class _ToDuration
, class _Clock
, class _Duration
>
93 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
96 __is_duration
<_ToDuration
>::value
,
97 time_point
<_Clock
, _ToDuration
>
99 floor(const time_point
<_Clock
, _Duration
>& __t
)
101 return time_point
<_Clock
, _ToDuration
>{floor
<_ToDuration
>(__t
.time_since_epoch())};
104 template <class _ToDuration
, class _Clock
, class _Duration
>
105 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
108 __is_duration
<_ToDuration
>::value
,
109 time_point
<_Clock
, _ToDuration
>
111 ceil(const time_point
<_Clock
, _Duration
>& __t
)
113 return time_point
<_Clock
, _ToDuration
>{ceil
<_ToDuration
>(__t
.time_since_epoch())};
116 template <class _ToDuration
, class _Clock
, class _Duration
>
117 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
120 __is_duration
<_ToDuration
>::value
,
121 time_point
<_Clock
, _ToDuration
>
123 round(const time_point
<_Clock
, _Duration
>& __t
)
125 return time_point
<_Clock
, _ToDuration
>{round
<_ToDuration
>(__t
.time_since_epoch())};
128 template <class _Rep
, class _Period
>
129 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
132 numeric_limits
<_Rep
>::is_signed
,
133 duration
<_Rep
, _Period
>
135 abs(duration
<_Rep
, _Period
> __d
)
137 return __d
>= __d
.zero() ? +__d
: -__d
;
139 #endif // _LIBCPP_STD_VER > 14
143 template <class _Clock
, class _Duration1
, class _Duration2
>
144 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
146 operator==(const time_point
<_Clock
, _Duration1
>& __lhs
, const time_point
<_Clock
, _Duration2
>& __rhs
)
148 return __lhs
.time_since_epoch() == __rhs
.time_since_epoch();
153 template <class _Clock
, class _Duration1
, class _Duration2
>
154 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
156 operator!=(const time_point
<_Clock
, _Duration1
>& __lhs
, const time_point
<_Clock
, _Duration2
>& __rhs
)
158 return !(__lhs
== __rhs
);
163 template <class _Clock
, class _Duration1
, class _Duration2
>
164 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
166 operator<(const time_point
<_Clock
, _Duration1
>& __lhs
, const time_point
<_Clock
, _Duration2
>& __rhs
)
168 return __lhs
.time_since_epoch() < __rhs
.time_since_epoch();
173 template <class _Clock
, class _Duration1
, class _Duration2
>
174 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
176 operator>(const time_point
<_Clock
, _Duration1
>& __lhs
, const time_point
<_Clock
, _Duration2
>& __rhs
)
178 return __rhs
< __lhs
;
183 template <class _Clock
, class _Duration1
, class _Duration2
>
184 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
186 operator<=(const time_point
<_Clock
, _Duration1
>& __lhs
, const time_point
<_Clock
, _Duration2
>& __rhs
)
188 return !(__rhs
< __lhs
);
193 template <class _Clock
, class _Duration1
, class _Duration2
>
194 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
196 operator>=(const time_point
<_Clock
, _Duration1
>& __lhs
, const time_point
<_Clock
, _Duration2
>& __rhs
)
198 return !(__lhs
< __rhs
);
201 // time_point operator+(time_point x, duration y);
203 template <class _Clock
, class _Duration1
, class _Rep2
, class _Period2
>
204 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
205 time_point
<_Clock
, typename common_type
<_Duration1
, duration
<_Rep2
, _Period2
> >::type
>
206 operator+(const time_point
<_Clock
, _Duration1
>& __lhs
, const duration
<_Rep2
, _Period2
>& __rhs
)
208 typedef time_point
<_Clock
, typename common_type
<_Duration1
, duration
<_Rep2
, _Period2
> >::type
> _Tr
;
209 return _Tr (__lhs
.time_since_epoch() + __rhs
);
212 // time_point operator+(duration x, time_point y);
214 template <class _Rep1
, class _Period1
, class _Clock
, class _Duration2
>
215 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
216 time_point
<_Clock
, typename common_type
<duration
<_Rep1
, _Period1
>, _Duration2
>::type
>
217 operator+(const duration
<_Rep1
, _Period1
>& __lhs
, const time_point
<_Clock
, _Duration2
>& __rhs
)
219 return __rhs
+ __lhs
;
222 // time_point operator-(time_point x, duration y);
224 template <class _Clock
, class _Duration1
, class _Rep2
, class _Period2
>
225 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
226 time_point
<_Clock
, typename common_type
<_Duration1
, duration
<_Rep2
, _Period2
> >::type
>
227 operator-(const time_point
<_Clock
, _Duration1
>& __lhs
, const duration
<_Rep2
, _Period2
>& __rhs
)
229 typedef time_point
<_Clock
, typename common_type
<_Duration1
, duration
<_Rep2
, _Period2
> >::type
> _Ret
;
230 return _Ret(__lhs
.time_since_epoch() -__rhs
);
233 // duration operator-(time_point x, time_point y);
235 template <class _Clock
, class _Duration1
, class _Duration2
>
236 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
237 typename common_type
<_Duration1
, _Duration2
>::type
238 operator-(const time_point
<_Clock
, _Duration1
>& __lhs
, const time_point
<_Clock
, _Duration2
>& __rhs
)
240 return __lhs
.time_since_epoch() - __rhs
.time_since_epoch();
243 } // namespace chrono
245 _LIBCPP_END_NAMESPACE_STD
249 #endif // _LIBCPP___CHRONO_TIME_POINT_H