[lld][WebAssembly] Reinstate mistakenly disabled test. NFC
[llvm-project.git] / libcxx / include / __chrono / time_point.h
blobc042e125145ab7049181f304374a7b97c6dda692
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 //===----------------------------------------------------------------------===//
10 #ifndef _LIBCPP___CHRONO_TIME_POINT_H
11 #define _LIBCPP___CHRONO_TIME_POINT_H
13 #include <__chrono/duration.h>
14 #include <__config>
15 #include <limits>
16 #include <type_traits>
18 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
19 #pragma GCC system_header
20 #endif
22 _LIBCPP_PUSH_MACROS
23 #include <__undef_macros>
25 _LIBCPP_BEGIN_NAMESPACE_STD
27 namespace chrono
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");
35 public:
36 typedef _Clock clock;
37 typedef _Duration duration;
38 typedef typename duration::rep rep;
39 typedef typename duration::period period;
40 private:
41 duration __d_;
43 public:
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) {}
47 // conversions
48 template <class _Duration2>
49 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
50 time_point(const time_point<clock, _Duration2>& t,
51 typename enable_if
53 is_convertible<_Duration2, duration>::value
54 >::type* = nullptr)
55 : __d_(t.time_since_epoch()) {}
57 // observer
59 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 duration time_since_epoch() const {return __d_;}
61 // arithmetic
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;}
66 // special values
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());}
72 } // namespace chrono
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;
81 namespace chrono {
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
94 typename enable_if
96 __is_duration<_ToDuration>::value,
97 time_point<_Clock, _ToDuration>
98 >::type
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
106 typename enable_if
108 __is_duration<_ToDuration>::value,
109 time_point<_Clock, _ToDuration>
110 >::type
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
118 typename enable_if
120 __is_duration<_ToDuration>::value,
121 time_point<_Clock, _ToDuration>
122 >::type
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
130 typename enable_if
132 numeric_limits<_Rep>::is_signed,
133 duration<_Rep, _Period>
134 >::type
135 abs(duration<_Rep, _Period> __d)
137 return __d >= __d.zero() ? +__d : -__d;
139 #endif // _LIBCPP_STD_VER > 14
141 // time_point ==
143 template <class _Clock, class _Duration1, class _Duration2>
144 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
145 bool
146 operator==(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs)
148 return __lhs.time_since_epoch() == __rhs.time_since_epoch();
151 // time_point !=
153 template <class _Clock, class _Duration1, class _Duration2>
154 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
155 bool
156 operator!=(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs)
158 return !(__lhs == __rhs);
161 // time_point <
163 template <class _Clock, class _Duration1, class _Duration2>
164 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
165 bool
166 operator<(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs)
168 return __lhs.time_since_epoch() < __rhs.time_since_epoch();
171 // time_point >
173 template <class _Clock, class _Duration1, class _Duration2>
174 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
175 bool
176 operator>(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs)
178 return __rhs < __lhs;
181 // time_point <=
183 template <class _Clock, class _Duration1, class _Duration2>
184 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
185 bool
186 operator<=(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs)
188 return !(__rhs < __lhs);
191 // time_point >=
193 template <class _Clock, class _Duration1, class _Duration2>
194 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
195 bool
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
247 _LIBCPP_POP_MACROS
249 #endif // _LIBCPP___CHRONO_TIME_POINT_H