xref: /openbsd-src/gnu/llvm/libcxx/include/__chrono/time_point.h (revision 4bdff4bed0e3d54e55670334c7d0077db4170f86)
1*4bdff4beSrobert // -*- C++ -*-
2*4bdff4beSrobert //===----------------------------------------------------------------------===//
3*4bdff4beSrobert //
4*4bdff4beSrobert // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5*4bdff4beSrobert // See https://llvm.org/LICENSE.txt for license information.
6*4bdff4beSrobert // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7*4bdff4beSrobert //
8*4bdff4beSrobert //===----------------------------------------------------------------------===//
9*4bdff4beSrobert 
10*4bdff4beSrobert #ifndef _LIBCPP___CHRONO_TIME_POINT_H
11*4bdff4beSrobert #define _LIBCPP___CHRONO_TIME_POINT_H
12*4bdff4beSrobert 
13*4bdff4beSrobert #include <__chrono/duration.h>
14*4bdff4beSrobert #include <__config>
15*4bdff4beSrobert #include <__type_traits/common_type.h>
16*4bdff4beSrobert #include <__type_traits/enable_if.h>
17*4bdff4beSrobert #include <__type_traits/is_convertible.h>
18*4bdff4beSrobert #include <limits>
19*4bdff4beSrobert 
20*4bdff4beSrobert #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
21*4bdff4beSrobert #  pragma GCC system_header
22*4bdff4beSrobert #endif
23*4bdff4beSrobert 
24*4bdff4beSrobert _LIBCPP_PUSH_MACROS
25*4bdff4beSrobert #include <__undef_macros>
26*4bdff4beSrobert 
27*4bdff4beSrobert _LIBCPP_BEGIN_NAMESPACE_STD
28*4bdff4beSrobert 
29*4bdff4beSrobert namespace chrono
30*4bdff4beSrobert {
31*4bdff4beSrobert 
32*4bdff4beSrobert template <class _Clock, class _Duration = typename _Clock::duration>
33*4bdff4beSrobert class _LIBCPP_TEMPLATE_VIS time_point
34*4bdff4beSrobert {
35*4bdff4beSrobert     static_assert(__is_duration<_Duration>::value,
36*4bdff4beSrobert                   "Second template parameter of time_point must be a std::chrono::duration");
37*4bdff4beSrobert public:
38*4bdff4beSrobert     typedef _Clock                    clock;
39*4bdff4beSrobert     typedef _Duration                 duration;
40*4bdff4beSrobert     typedef typename duration::rep    rep;
41*4bdff4beSrobert     typedef typename duration::period period;
42*4bdff4beSrobert private:
43*4bdff4beSrobert     duration __d_;
44*4bdff4beSrobert 
45*4bdff4beSrobert public:
time_point()46*4bdff4beSrobert     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14 time_point() : __d_(duration::zero()) {}
time_point(const duration & __d)47*4bdff4beSrobert     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14 explicit time_point(const duration& __d) : __d_(__d) {}
48*4bdff4beSrobert 
49*4bdff4beSrobert     // conversions
50*4bdff4beSrobert     template <class _Duration2>
51*4bdff4beSrobert     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14
52*4bdff4beSrobert     time_point(const time_point<clock, _Duration2>& __t,
53*4bdff4beSrobert         typename enable_if
54*4bdff4beSrobert         <
55*4bdff4beSrobert             is_convertible<_Duration2, duration>::value
56*4bdff4beSrobert         >::type* = nullptr)
57*4bdff4beSrobert             : __d_(__t.time_since_epoch()) {}
58*4bdff4beSrobert 
59*4bdff4beSrobert     // observer
60*4bdff4beSrobert 
time_since_epoch()61*4bdff4beSrobert     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14 duration time_since_epoch() const {return __d_;}
62*4bdff4beSrobert 
63*4bdff4beSrobert     // arithmetic
64*4bdff4beSrobert 
65*4bdff4beSrobert     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX17 time_point& operator+=(const duration& __d) {__d_ += __d; return *this;}
66*4bdff4beSrobert     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX17 time_point& operator-=(const duration& __d) {__d_ -= __d; return *this;}
67*4bdff4beSrobert 
68*4bdff4beSrobert     // special values
69*4bdff4beSrobert 
min()70*4bdff4beSrobert     _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR time_point min() _NOEXCEPT {return time_point(duration::min());}
max()71*4bdff4beSrobert     _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR time_point max() _NOEXCEPT {return time_point(duration::max());}
72*4bdff4beSrobert };
73*4bdff4beSrobert 
74*4bdff4beSrobert } // namespace chrono
75*4bdff4beSrobert 
76*4bdff4beSrobert template <class _Clock, class _Duration1, class _Duration2>
77*4bdff4beSrobert struct _LIBCPP_TEMPLATE_VIS common_type<chrono::time_point<_Clock, _Duration1>,
78*4bdff4beSrobert                                          chrono::time_point<_Clock, _Duration2> >
79*4bdff4beSrobert {
80*4bdff4beSrobert     typedef chrono::time_point<_Clock, typename common_type<_Duration1, _Duration2>::type> type;
81*4bdff4beSrobert };
82*4bdff4beSrobert 
83*4bdff4beSrobert namespace chrono {
84*4bdff4beSrobert 
85*4bdff4beSrobert template <class _ToDuration, class _Clock, class _Duration>
86*4bdff4beSrobert inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14
87*4bdff4beSrobert time_point<_Clock, _ToDuration>
88*4bdff4beSrobert time_point_cast(const time_point<_Clock, _Duration>& __t)
89*4bdff4beSrobert {
90*4bdff4beSrobert     return time_point<_Clock, _ToDuration>(chrono::duration_cast<_ToDuration>(__t.time_since_epoch()));
91*4bdff4beSrobert }
92*4bdff4beSrobert 
93*4bdff4beSrobert #if _LIBCPP_STD_VER > 14
94*4bdff4beSrobert template <class _ToDuration, class _Clock, class _Duration>
95*4bdff4beSrobert inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
96*4bdff4beSrobert typename enable_if
97*4bdff4beSrobert <
98*4bdff4beSrobert     __is_duration<_ToDuration>::value,
99*4bdff4beSrobert     time_point<_Clock, _ToDuration>
100*4bdff4beSrobert >::type
101*4bdff4beSrobert floor(const time_point<_Clock, _Duration>& __t)
102*4bdff4beSrobert {
103*4bdff4beSrobert     return time_point<_Clock, _ToDuration>{chrono::floor<_ToDuration>(__t.time_since_epoch())};
104*4bdff4beSrobert }
105*4bdff4beSrobert 
106*4bdff4beSrobert template <class _ToDuration, class _Clock, class _Duration>
107*4bdff4beSrobert inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
108*4bdff4beSrobert typename enable_if
109*4bdff4beSrobert <
110*4bdff4beSrobert     __is_duration<_ToDuration>::value,
111*4bdff4beSrobert     time_point<_Clock, _ToDuration>
112*4bdff4beSrobert >::type
113*4bdff4beSrobert ceil(const time_point<_Clock, _Duration>& __t)
114*4bdff4beSrobert {
115*4bdff4beSrobert     return time_point<_Clock, _ToDuration>{chrono::ceil<_ToDuration>(__t.time_since_epoch())};
116*4bdff4beSrobert }
117*4bdff4beSrobert 
118*4bdff4beSrobert template <class _ToDuration, class _Clock, class _Duration>
119*4bdff4beSrobert inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
120*4bdff4beSrobert typename enable_if
121*4bdff4beSrobert <
122*4bdff4beSrobert     __is_duration<_ToDuration>::value,
123*4bdff4beSrobert     time_point<_Clock, _ToDuration>
124*4bdff4beSrobert >::type
125*4bdff4beSrobert round(const time_point<_Clock, _Duration>& __t)
126*4bdff4beSrobert {
127*4bdff4beSrobert     return time_point<_Clock, _ToDuration>{chrono::round<_ToDuration>(__t.time_since_epoch())};
128*4bdff4beSrobert }
129*4bdff4beSrobert 
130*4bdff4beSrobert template <class _Rep, class _Period>
131*4bdff4beSrobert inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
132*4bdff4beSrobert typename enable_if
133*4bdff4beSrobert <
134*4bdff4beSrobert     numeric_limits<_Rep>::is_signed,
135*4bdff4beSrobert     duration<_Rep, _Period>
136*4bdff4beSrobert >::type
137*4bdff4beSrobert abs(duration<_Rep, _Period> __d)
138*4bdff4beSrobert {
139*4bdff4beSrobert     return __d >= __d.zero() ? +__d : -__d;
140*4bdff4beSrobert }
141*4bdff4beSrobert #endif // _LIBCPP_STD_VER > 14
142*4bdff4beSrobert 
143*4bdff4beSrobert // time_point ==
144*4bdff4beSrobert 
145*4bdff4beSrobert template <class _Clock, class _Duration1, class _Duration2>
146*4bdff4beSrobert inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14
147*4bdff4beSrobert bool
148*4bdff4beSrobert operator==(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs)
149*4bdff4beSrobert {
150*4bdff4beSrobert     return __lhs.time_since_epoch() == __rhs.time_since_epoch();
151*4bdff4beSrobert }
152*4bdff4beSrobert 
153*4bdff4beSrobert // time_point !=
154*4bdff4beSrobert 
155*4bdff4beSrobert template <class _Clock, class _Duration1, class _Duration2>
156*4bdff4beSrobert inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14
157*4bdff4beSrobert bool
158*4bdff4beSrobert operator!=(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs)
159*4bdff4beSrobert {
160*4bdff4beSrobert     return !(__lhs == __rhs);
161*4bdff4beSrobert }
162*4bdff4beSrobert 
163*4bdff4beSrobert // time_point <
164*4bdff4beSrobert 
165*4bdff4beSrobert template <class _Clock, class _Duration1, class _Duration2>
166*4bdff4beSrobert inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14
167*4bdff4beSrobert bool
168*4bdff4beSrobert operator<(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs)
169*4bdff4beSrobert {
170*4bdff4beSrobert     return __lhs.time_since_epoch() < __rhs.time_since_epoch();
171*4bdff4beSrobert }
172*4bdff4beSrobert 
173*4bdff4beSrobert // time_point >
174*4bdff4beSrobert 
175*4bdff4beSrobert template <class _Clock, class _Duration1, class _Duration2>
176*4bdff4beSrobert inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14
177*4bdff4beSrobert bool
178*4bdff4beSrobert operator>(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs)
179*4bdff4beSrobert {
180*4bdff4beSrobert     return __rhs < __lhs;
181*4bdff4beSrobert }
182*4bdff4beSrobert 
183*4bdff4beSrobert // time_point <=
184*4bdff4beSrobert 
185*4bdff4beSrobert template <class _Clock, class _Duration1, class _Duration2>
186*4bdff4beSrobert inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14
187*4bdff4beSrobert bool
188*4bdff4beSrobert operator<=(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs)
189*4bdff4beSrobert {
190*4bdff4beSrobert     return !(__rhs < __lhs);
191*4bdff4beSrobert }
192*4bdff4beSrobert 
193*4bdff4beSrobert // time_point >=
194*4bdff4beSrobert 
195*4bdff4beSrobert template <class _Clock, class _Duration1, class _Duration2>
196*4bdff4beSrobert inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14
197*4bdff4beSrobert bool
198*4bdff4beSrobert operator>=(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs)
199*4bdff4beSrobert {
200*4bdff4beSrobert     return !(__lhs < __rhs);
201*4bdff4beSrobert }
202*4bdff4beSrobert 
203*4bdff4beSrobert // time_point operator+(time_point x, duration y);
204*4bdff4beSrobert 
205*4bdff4beSrobert template <class _Clock, class _Duration1, class _Rep2, class _Period2>
206*4bdff4beSrobert inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14
207*4bdff4beSrobert time_point<_Clock, typename common_type<_Duration1, duration<_Rep2, _Period2> >::type>
208*4bdff4beSrobert operator+(const time_point<_Clock, _Duration1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
209*4bdff4beSrobert {
210*4bdff4beSrobert     typedef time_point<_Clock, typename common_type<_Duration1, duration<_Rep2, _Period2> >::type> _Tr;
211*4bdff4beSrobert     return _Tr (__lhs.time_since_epoch() + __rhs);
212*4bdff4beSrobert }
213*4bdff4beSrobert 
214*4bdff4beSrobert // time_point operator+(duration x, time_point y);
215*4bdff4beSrobert 
216*4bdff4beSrobert template <class _Rep1, class _Period1, class _Clock, class _Duration2>
217*4bdff4beSrobert inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14
218*4bdff4beSrobert time_point<_Clock, typename common_type<duration<_Rep1, _Period1>, _Duration2>::type>
219*4bdff4beSrobert operator+(const duration<_Rep1, _Period1>& __lhs, const time_point<_Clock, _Duration2>& __rhs)
220*4bdff4beSrobert {
221*4bdff4beSrobert     return __rhs + __lhs;
222*4bdff4beSrobert }
223*4bdff4beSrobert 
224*4bdff4beSrobert // time_point operator-(time_point x, duration y);
225*4bdff4beSrobert 
226*4bdff4beSrobert template <class _Clock, class _Duration1, class _Rep2, class _Period2>
227*4bdff4beSrobert inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14
228*4bdff4beSrobert time_point<_Clock, typename common_type<_Duration1, duration<_Rep2, _Period2> >::type>
229*4bdff4beSrobert operator-(const time_point<_Clock, _Duration1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
230*4bdff4beSrobert {
231*4bdff4beSrobert     typedef time_point<_Clock, typename common_type<_Duration1, duration<_Rep2, _Period2> >::type> _Ret;
232*4bdff4beSrobert     return _Ret(__lhs.time_since_epoch() -__rhs);
233*4bdff4beSrobert }
234*4bdff4beSrobert 
235*4bdff4beSrobert // duration operator-(time_point x, time_point y);
236*4bdff4beSrobert 
237*4bdff4beSrobert template <class _Clock, class _Duration1, class _Duration2>
238*4bdff4beSrobert inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14
239*4bdff4beSrobert typename common_type<_Duration1, _Duration2>::type
240*4bdff4beSrobert operator-(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs)
241*4bdff4beSrobert {
242*4bdff4beSrobert     return __lhs.time_since_epoch() - __rhs.time_since_epoch();
243*4bdff4beSrobert }
244*4bdff4beSrobert 
245*4bdff4beSrobert } // namespace chrono
246*4bdff4beSrobert 
247*4bdff4beSrobert _LIBCPP_END_NAMESPACE_STD
248*4bdff4beSrobert 
249*4bdff4beSrobert _LIBCPP_POP_MACROS
250*4bdff4beSrobert 
251*4bdff4beSrobert #endif // _LIBCPP___CHRONO_TIME_POINT_H
252