• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <__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>
21 
22 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
23 #  pragma GCC system_header
24 #endif
25 
26 _LIBCPP_PUSH_MACROS
27 #include <__undef_macros>
28 
29 _LIBCPP_BEGIN_NAMESPACE_STD
30 
31 namespace chrono
32 {
33 
34 template <class _Clock, class _Duration = typename _Clock::duration>
35 class _LIBCPP_TEMPLATE_VIS time_point
36 {
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_;
46 
47 public:
time_point()48     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14 time_point() : __d_(duration::zero()) {}
time_point(const duration & __d)49     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14 explicit time_point(const duration& __d) : __d_(__d) {}
50 
51     // conversions
52     template <class _Duration2, __enable_if_t<is_convertible<_Duration2, duration>::value, int> = 0>
53     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14
time_point(const time_point<clock,_Duration2> & __t)54     time_point(const time_point<clock, _Duration2>& __t)
55             : __d_(__t.time_since_epoch()) {}
56 
57     // observer
58 
time_since_epoch()59     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14 duration time_since_epoch() const {return __d_;}
60 
61     // arithmetic
62 
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;}
65 
66     // special values
67 
min()68     _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR time_point min() _NOEXCEPT {return time_point(duration::min());}
max()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_SINCE_CXX14
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 >= 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)
96 {
97     return time_point<_Clock, _ToDuration>{chrono::floor<_ToDuration>(__t.time_since_epoch())};
98 }
99 
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)
104 {
105     return time_point<_Clock, _ToDuration>{chrono::ceil<_ToDuration>(__t.time_since_epoch())};
106 }
107 
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)
112 {
113     return time_point<_Clock, _ToDuration>{chrono::round<_ToDuration>(__t.time_since_epoch())};
114 }
115 
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)
120 {
121     return __d >= __d.zero() ? +__d : -__d;
122 }
123 #endif // _LIBCPP_STD_VER >= 17
124 
125 // time_point ==
126 
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)
131 {
132     return __lhs.time_since_epoch() == __rhs.time_since_epoch();
133 }
134 
135 #if _LIBCPP_STD_VER <= 17
136 
137 // time_point !=
138 
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)
143 {
144     return !(__lhs == __rhs);
145 }
146 
147 #endif // _LIBCPP_STD_VER <= 17
148 
149 // time_point <
150 
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)
155 {
156     return __lhs.time_since_epoch() < __rhs.time_since_epoch();
157 }
158 
159 // time_point >
160 
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)
165 {
166     return __rhs < __lhs;
167 }
168 
169 // time_point <=
170 
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)
175 {
176     return !(__rhs < __lhs);
177 }
178 
179 // time_point >=
180 
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)
185 {
186     return !(__lhs < __rhs);
187 }
188 
189 #if _LIBCPP_STD_VER >= 20
190 
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();
195 }
196 
197 #endif // _LIBCPP_STD_VER >= 20
198 
199 // time_point operator+(time_point x, duration y);
200 
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)
205 {
206     typedef time_point<_Clock, typename common_type<_Duration1, duration<_Rep2, _Period2> >::type> _Tr;
207     return _Tr (__lhs.time_since_epoch() + __rhs);
208 }
209 
210 // time_point operator+(duration x, time_point y);
211 
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)
216 {
217     return __rhs + __lhs;
218 }
219 
220 // time_point operator-(time_point x, duration y);
221 
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)
226 {
227     typedef time_point<_Clock, typename common_type<_Duration1, duration<_Rep2, _Period2> >::type> _Ret;
228     return _Ret(__lhs.time_since_epoch() -__rhs);
229 }
230 
231 // duration operator-(time_point x, time_point y);
232 
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)
237 {
238     return __lhs.time_since_epoch() - __rhs.time_since_epoch();
239 }
240 
241 } // namespace chrono
242 
243 _LIBCPP_END_NAMESPACE_STD
244 
245 _LIBCPP_POP_MACROS
246 
247 #endif // _LIBCPP___CHRONO_TIME_POINT_H
248