xref: /freebsd/contrib/llvm-project/libcxx/include/__chrono/time_point.h (revision 8ddb146abcdf061be9f2c0db7e391697dafad85c)
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 //===----------------------------------------------------------------------===//
9 
10 #ifndef _LIBCPP___CHRONO_TIME_POINT_H
11 #define _LIBCPP___CHRONO_TIME_POINT_H
12 
13 #include <__chrono/duration.h>
14 #include <__config>
15 #include <limits>
16 #include <type_traits>
17 
18 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
19 #pragma GCC system_header
20 #endif
21 
22 _LIBCPP_PUSH_MACROS
23 #include <__undef_macros>
24 
25 _LIBCPP_BEGIN_NAMESPACE_STD
26 
27 namespace chrono
28 {
29 
30 template <class _Clock, class _Duration = typename _Clock::duration>
31 class _LIBCPP_TEMPLATE_VIS time_point
32 {
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_;
42 
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) {}
46 
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
52         <
53             is_convertible<_Duration2, duration>::value
54         >::type* = nullptr)
55             : __d_(t.time_since_epoch()) {}
56 
57     // observer
58 
59     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 duration time_since_epoch() const {return __d_;}
60 
61     // arithmetic
62 
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;}
65 
66     // special values
67 
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());}
70 };
71 
72 } // namespace chrono
73 
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> >
77 {
78     typedef chrono::time_point<_Clock, typename common_type<_Duration1, _Duration2>::type> type;
79 };
80 
81 namespace chrono {
82 
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)
87 {
88     return time_point<_Clock, _ToDuration>(chrono::duration_cast<_ToDuration>(__t.time_since_epoch()));
89 }
90 
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
95 <
96     __is_duration<_ToDuration>::value,
97     time_point<_Clock, _ToDuration>
98 >::type
99 floor(const time_point<_Clock, _Duration>& __t)
100 {
101     return time_point<_Clock, _ToDuration>{floor<_ToDuration>(__t.time_since_epoch())};
102 }
103 
104 template <class _ToDuration, class _Clock, class _Duration>
105 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
106 typename enable_if
107 <
108     __is_duration<_ToDuration>::value,
109     time_point<_Clock, _ToDuration>
110 >::type
111 ceil(const time_point<_Clock, _Duration>& __t)
112 {
113     return time_point<_Clock, _ToDuration>{ceil<_ToDuration>(__t.time_since_epoch())};
114 }
115 
116 template <class _ToDuration, class _Clock, class _Duration>
117 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
118 typename enable_if
119 <
120     __is_duration<_ToDuration>::value,
121     time_point<_Clock, _ToDuration>
122 >::type
123 round(const time_point<_Clock, _Duration>& __t)
124 {
125     return time_point<_Clock, _ToDuration>{round<_ToDuration>(__t.time_since_epoch())};
126 }
127 
128 template <class _Rep, class _Period>
129 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
130 typename enable_if
131 <
132     numeric_limits<_Rep>::is_signed,
133     duration<_Rep, _Period>
134 >::type
135 abs(duration<_Rep, _Period> __d)
136 {
137     return __d >= __d.zero() ? +__d : -__d;
138 }
139 #endif // _LIBCPP_STD_VER > 14
140 
141 // time_point ==
142 
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)
147 {
148     return __lhs.time_since_epoch() == __rhs.time_since_epoch();
149 }
150 
151 // time_point !=
152 
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)
157 {
158     return !(__lhs == __rhs);
159 }
160 
161 // time_point <
162 
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)
167 {
168     return __lhs.time_since_epoch() < __rhs.time_since_epoch();
169 }
170 
171 // time_point >
172 
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)
177 {
178     return __rhs < __lhs;
179 }
180 
181 // time_point <=
182 
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)
187 {
188     return !(__rhs < __lhs);
189 }
190 
191 // time_point >=
192 
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)
197 {
198     return !(__lhs < __rhs);
199 }
200 
201 // time_point operator+(time_point x, duration y);
202 
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)
207 {
208     typedef time_point<_Clock, typename common_type<_Duration1, duration<_Rep2, _Period2> >::type> _Tr;
209     return _Tr (__lhs.time_since_epoch() + __rhs);
210 }
211 
212 // time_point operator+(duration x, time_point y);
213 
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)
218 {
219     return __rhs + __lhs;
220 }
221 
222 // time_point operator-(time_point x, duration y);
223 
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)
228 {
229     typedef time_point<_Clock, typename common_type<_Duration1, duration<_Rep2, _Period2> >::type> _Ret;
230     return _Ret(__lhs.time_since_epoch() -__rhs);
231 }
232 
233 // duration operator-(time_point x, time_point y);
234 
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)
239 {
240     return __lhs.time_since_epoch() - __rhs.time_since_epoch();
241 }
242 
243 } // namespace chrono
244 
245 _LIBCPP_END_NAMESPACE_STD
246 
247 _LIBCPP_POP_MACROS
248 
249 #endif // _LIBCPP___CHRONO_TIME_POINT_H
250