Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libcxx / include / __chrono / time_point.h
blob741165bd3f43bb3d2c5eaf0c6544388994f9c85a
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 <__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>
22 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
23 # pragma GCC system_header
24 #endif
26 _LIBCPP_PUSH_MACROS
27 #include <__undef_macros>
29 _LIBCPP_BEGIN_NAMESPACE_STD
31 namespace chrono
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");
39 public:
40 typedef _Clock clock;
41 typedef _Duration duration;
42 typedef typename duration::rep rep;
43 typedef typename duration::period period;
44 private:
45 duration __d_;
47 public:
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) {}
51 // conversions
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()) {}
57 // observer
59 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14 duration time_since_epoch() const {return __d_;}
61 // arithmetic
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;}
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_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
125 // time_point ==
127 template <class _Clock, class _Duration1, class _Duration2>
128 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14
129 bool
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
137 // time_point !=
139 template <class _Clock, class _Duration1, class _Duration2>
140 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14
141 bool
142 operator!=(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs)
144 return !(__lhs == __rhs);
147 #endif // _LIBCPP_STD_VER <= 17
149 // time_point <
151 template <class _Clock, class _Duration1, class _Duration2>
152 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14
153 bool
154 operator<(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs)
156 return __lhs.time_since_epoch() < __rhs.time_since_epoch();
159 // time_point >
161 template <class _Clock, class _Duration1, class _Duration2>
162 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14
163 bool
164 operator>(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs)
166 return __rhs < __lhs;
169 // time_point <=
171 template <class _Clock, class _Duration1, class _Duration2>
172 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14
173 bool
174 operator<=(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs)
176 return !(__rhs < __lhs);
179 // time_point >=
181 template <class _Clock, class _Duration1, class _Duration2>
182 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14
183 bool
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
245 _LIBCPP_POP_MACROS
247 #endif // _LIBCPP___CHRONO_TIME_POINT_H