]>
Commit | Line | Data |
---|---|---|
7f78718b JW |
1 | // chrono::duration and chrono::time_point -*- C++ -*- |
2 | ||
a945c346 | 3 | // Copyright (C) 2008-2024 Free Software Foundation, Inc. |
7f78718b JW |
4 | // |
5 | // This file is part of the GNU ISO C++ Library. This library is free | |
6 | // software; you can redistribute it and/or modify it under the | |
7 | // terms of the GNU General Public License as published by the | |
8 | // Free Software Foundation; either version 3, or (at your option) | |
9 | // any later version. | |
10 | ||
11 | // This library is distributed in the hope that it will be useful, | |
12 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | // GNU General Public License for more details. | |
15 | ||
16 | // Under Section 7 of GPL version 3, you are granted additional | |
17 | // permissions described in the GCC Runtime Library Exception, version | |
18 | // 3.1, as published by the Free Software Foundation. | |
19 | ||
20 | // You should have received a copy of the GNU General Public License and | |
21 | // a copy of the GCC Runtime Library Exception along with this program; | |
22 | // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see | |
23 | // <http://www.gnu.org/licenses/>. | |
24 | ||
25 | /** @file include/bits/chrono.h | |
26 | * This is an internal header file, included by other library headers. | |
27 | * Do not attempt to use it directly. @headername{chrono} | |
28 | */ | |
29 | ||
30 | #ifndef _GLIBCXX_CHRONO_H | |
31 | #define _GLIBCXX_CHRONO_H 1 | |
32 | ||
33 | #pragma GCC system_header | |
34 | ||
35 | #if __cplusplus >= 201103L | |
36 | ||
37 | #include <ratio> | |
38 | #include <type_traits> | |
39 | #include <limits> | |
1dde83f0 JW |
40 | #if _GLIBCXX_HOSTED |
41 | # include <ctime> | |
42 | #endif | |
7f78718b JW |
43 | #include <bits/parse_numbers.h> // for literals support. |
44 | #if __cplusplus >= 202002L | |
45 | # include <concepts> | |
46 | # include <compare> | |
47 | #endif | |
48 | ||
7ffa63df JW |
49 | #include <bits/version.h> |
50 | ||
7f78718b JW |
51 | namespace std _GLIBCXX_VISIBILITY(default) |
52 | { | |
53 | _GLIBCXX_BEGIN_NAMESPACE_VERSION | |
54 | ||
1dde83f0 | 55 | #if __cplusplus >= 201703L && _GLIBCXX_HOSTED |
7f78718b JW |
56 | namespace filesystem { struct __file_clock; }; |
57 | #endif | |
58 | ||
59 | namespace chrono | |
60 | { | |
61 | /// @addtogroup chrono | |
62 | /// @{ | |
63 | ||
64 | /// `chrono::duration` represents a distance between two points in time | |
65 | template<typename _Rep, typename _Period = ratio<1>> | |
7eec3114 | 66 | class duration; |
7f78718b JW |
67 | |
68 | /// `chrono::time_point` represents a point in time as measured by a clock | |
69 | template<typename _Clock, typename _Dur = typename _Clock::duration> | |
7eec3114 | 70 | class time_point; |
7f78718b JW |
71 | /// @} |
72 | } | |
73 | ||
74 | /// @addtogroup chrono | |
75 | /// @{ | |
76 | ||
77 | // 20.11.4.3 specialization of common_type (for duration, sfinae-friendly) | |
78 | ||
79 | /// @cond undocumented | |
80 | ||
81 | template<typename _CT, typename _Period1, typename _Period2, typename = void> | |
82 | struct __duration_common_type | |
83 | { }; | |
84 | ||
85 | template<typename _CT, typename _Period1, typename _Period2> | |
86 | struct __duration_common_type<_CT, _Period1, _Period2, | |
87 | __void_t<typename _CT::type>> | |
88 | { | |
89 | private: | |
90 | using __gcd_num = __static_gcd<_Period1::num, _Period2::num>; | |
91 | using __gcd_den = __static_gcd<_Period1::den, _Period2::den>; | |
92 | using __cr = typename _CT::type; | |
93 | using __r = ratio<__gcd_num::value, | |
94 | (_Period1::den / __gcd_den::value) * _Period2::den>; | |
95 | ||
96 | public: | |
97 | using type = chrono::duration<__cr, typename __r::type>; | |
98 | }; | |
99 | ||
100 | /// @endcond | |
101 | ||
102 | /// @{ | |
103 | /// @relates chrono::duration | |
104 | ||
105 | /// Specialization of common_type for chrono::duration types. | |
106 | template<typename _Rep1, typename _Period1, typename _Rep2, typename _Period2> | |
107 | struct common_type<chrono::duration<_Rep1, _Period1>, | |
108 | chrono::duration<_Rep2, _Period2>> | |
109 | : __duration_common_type<common_type<_Rep1, _Rep2>, | |
110 | typename _Period1::type, | |
111 | typename _Period2::type> | |
112 | { }; | |
113 | ||
114 | /// Specialization of common_type for two identical chrono::duration types. | |
115 | template<typename _Rep, typename _Period> | |
116 | struct common_type<chrono::duration<_Rep, _Period>, | |
117 | chrono::duration<_Rep, _Period>> | |
118 | { | |
119 | using type = chrono::duration<typename common_type<_Rep>::type, | |
120 | typename _Period::type>; | |
121 | }; | |
122 | ||
123 | /// Specialization of common_type for one chrono::duration type. | |
124 | template<typename _Rep, typename _Period> | |
125 | struct common_type<chrono::duration<_Rep, _Period>> | |
126 | { | |
127 | using type = chrono::duration<typename common_type<_Rep>::type, | |
128 | typename _Period::type>; | |
129 | }; | |
130 | /// @} | |
131 | ||
132 | // 20.11.4.3 specialization of common_type (for time_point, sfinae-friendly) | |
133 | ||
134 | /// @cond undocumented | |
135 | ||
136 | template<typename _CT, typename _Clock, typename = void> | |
137 | struct __timepoint_common_type | |
138 | { }; | |
139 | ||
140 | template<typename _CT, typename _Clock> | |
141 | struct __timepoint_common_type<_CT, _Clock, __void_t<typename _CT::type>> | |
142 | { | |
143 | using type = chrono::time_point<_Clock, typename _CT::type>; | |
144 | }; | |
145 | ||
146 | /// @endcond | |
147 | ||
148 | /// @{ | |
149 | /// @relates chrono::time_point | |
150 | ||
151 | /// Specialization of common_type for chrono::time_point types. | |
152 | template<typename _Clock, typename _Duration1, typename _Duration2> | |
153 | struct common_type<chrono::time_point<_Clock, _Duration1>, | |
154 | chrono::time_point<_Clock, _Duration2>> | |
155 | : __timepoint_common_type<common_type<_Duration1, _Duration2>, _Clock> | |
156 | { }; | |
157 | ||
158 | /// Specialization of common_type for two identical chrono::time_point types. | |
159 | template<typename _Clock, typename _Duration> | |
160 | struct common_type<chrono::time_point<_Clock, _Duration>, | |
161 | chrono::time_point<_Clock, _Duration>> | |
162 | { using type = chrono::time_point<_Clock, _Duration>; }; | |
163 | ||
164 | /// Specialization of common_type for one chrono::time_point type. | |
165 | template<typename _Clock, typename _Duration> | |
166 | struct common_type<chrono::time_point<_Clock, _Duration>> | |
167 | { using type = chrono::time_point<_Clock, _Duration>; }; | |
168 | /// @} | |
169 | ||
170 | /// @} group chrono | |
171 | ||
172 | namespace chrono | |
173 | { | |
174 | /// @addtogroup chrono | |
175 | /// @{ | |
176 | ||
177 | /// @cond undocumented | |
178 | ||
179 | // Primary template for duration_cast impl. | |
180 | template<typename _ToDur, typename _CF, typename _CR, | |
181 | bool _NumIsOne = false, bool _DenIsOne = false> | |
182 | struct __duration_cast_impl | |
183 | { | |
184 | template<typename _Rep, typename _Period> | |
185 | static constexpr _ToDur | |
186 | __cast(const duration<_Rep, _Period>& __d) | |
187 | { | |
188 | typedef typename _ToDur::rep __to_rep; | |
189 | return _ToDur(static_cast<__to_rep>(static_cast<_CR>(__d.count()) | |
190 | * static_cast<_CR>(_CF::num) | |
191 | / static_cast<_CR>(_CF::den))); | |
192 | } | |
193 | }; | |
194 | ||
195 | template<typename _ToDur, typename _CF, typename _CR> | |
196 | struct __duration_cast_impl<_ToDur, _CF, _CR, true, true> | |
197 | { | |
198 | template<typename _Rep, typename _Period> | |
199 | static constexpr _ToDur | |
200 | __cast(const duration<_Rep, _Period>& __d) | |
201 | { | |
202 | typedef typename _ToDur::rep __to_rep; | |
203 | return _ToDur(static_cast<__to_rep>(__d.count())); | |
204 | } | |
205 | }; | |
206 | ||
207 | template<typename _ToDur, typename _CF, typename _CR> | |
208 | struct __duration_cast_impl<_ToDur, _CF, _CR, true, false> | |
209 | { | |
210 | template<typename _Rep, typename _Period> | |
211 | static constexpr _ToDur | |
212 | __cast(const duration<_Rep, _Period>& __d) | |
213 | { | |
214 | typedef typename _ToDur::rep __to_rep; | |
215 | return _ToDur(static_cast<__to_rep>( | |
216 | static_cast<_CR>(__d.count()) / static_cast<_CR>(_CF::den))); | |
217 | } | |
218 | }; | |
219 | ||
220 | template<typename _ToDur, typename _CF, typename _CR> | |
221 | struct __duration_cast_impl<_ToDur, _CF, _CR, false, true> | |
222 | { | |
223 | template<typename _Rep, typename _Period> | |
224 | static constexpr _ToDur | |
225 | __cast(const duration<_Rep, _Period>& __d) | |
226 | { | |
227 | typedef typename _ToDur::rep __to_rep; | |
228 | return _ToDur(static_cast<__to_rep>( | |
229 | static_cast<_CR>(__d.count()) * static_cast<_CR>(_CF::num))); | |
230 | } | |
231 | }; | |
232 | ||
233 | template<typename _Tp> | |
234 | struct __is_duration | |
235 | : std::false_type | |
236 | { }; | |
237 | ||
238 | template<typename _Rep, typename _Period> | |
239 | struct __is_duration<duration<_Rep, _Period>> | |
240 | : std::true_type | |
241 | { }; | |
242 | ||
243 | template<typename _Tp> | |
244 | using __enable_if_is_duration | |
245 | = typename enable_if<__is_duration<_Tp>::value, _Tp>::type; | |
246 | ||
247 | template<typename _Tp> | |
248 | using __disable_if_is_duration | |
249 | = typename enable_if<!__is_duration<_Tp>::value, _Tp>::type; | |
250 | ||
4d771291 | 251 | #if __cplusplus >= 201703L |
d2d3826c JW |
252 | template<typename _Tp> |
253 | inline constexpr bool __is_duration_v = false; | |
254 | template<typename _Rep, typename _Period> | |
255 | inline constexpr bool __is_duration_v<duration<_Rep, _Period>> = true; | |
256 | template<typename _Tp> | |
257 | inline constexpr bool __is_time_point_v = false; | |
258 | template<typename _Clock, typename _Dur> | |
259 | inline constexpr bool __is_time_point_v<time_point<_Clock, _Dur>> = true; | |
260 | #endif | |
261 | ||
7f78718b JW |
262 | /// @endcond |
263 | ||
646e979c JW |
264 | /** Convert a `duration` to type `ToDur`. |
265 | * | |
266 | * If the duration cannot be represented accurately in the result type, | |
267 | * returns the result of integer truncation (i.e., rounded towards zero). | |
268 | * | |
269 | * @tparam _ToDur The result type must be a `duration`. | |
270 | * @param __d A duration. | |
271 | * @return The value of `__d` converted to type `_ToDur`. | |
272 | * @since C++11 | |
273 | */ | |
7f78718b | 274 | template<typename _ToDur, typename _Rep, typename _Period> |
646e979c | 275 | _GLIBCXX_NODISCARD |
7f78718b JW |
276 | constexpr __enable_if_is_duration<_ToDur> |
277 | duration_cast(const duration<_Rep, _Period>& __d) | |
278 | { | |
d2d3826c JW |
279 | #if __cpp_inline_variables && __cpp_if_constexpr |
280 | if constexpr (is_same_v<_ToDur, duration<_Rep, _Period>>) | |
281 | return __d; | |
282 | else | |
b3a2b307 | 283 | { |
d2d3826c | 284 | #endif |
d2d3826c JW |
285 | using __to_period = typename _ToDur::period; |
286 | using __to_rep = typename _ToDur::rep; | |
287 | using __cf = ratio_divide<_Period, __to_period>; | |
288 | using __cr = typename common_type<__to_rep, _Rep, intmax_t>::type; | |
289 | using __dc = __duration_cast_impl<_ToDur, __cf, __cr, | |
290 | __cf::num == 1, __cf::den == 1>; | |
291 | return __dc::__cast(__d); | |
b3a2b307 JW |
292 | #if __cpp_inline_variables && __cpp_if_constexpr |
293 | } | |
294 | #endif | |
7f78718b JW |
295 | } |
296 | ||
646e979c JW |
297 | /** Trait indicating whether to treat a type as a floating-point type. |
298 | * | |
299 | * The chrono library uses this trait to tell whether a `duration` can | |
300 | * represent fractional values of the given precision, or only integral | |
301 | * values. | |
302 | * | |
303 | * You should specialize this trait for your own numeric types that are | |
304 | * used with `duration` and can represent non-integral values. | |
305 | * | |
306 | * @since C++11 | |
307 | */ | |
7f78718b JW |
308 | template<typename _Rep> |
309 | struct treat_as_floating_point | |
310 | : is_floating_point<_Rep> | |
311 | { }; | |
312 | ||
313 | #if __cplusplus > 201402L | |
314 | template <typename _Rep> | |
315 | inline constexpr bool treat_as_floating_point_v = | |
316 | treat_as_floating_point<_Rep>::value; | |
d2d3826c JW |
317 | |
318 | template<> | |
319 | inline constexpr bool treat_as_floating_point_v<int> = false; | |
320 | template<> | |
321 | inline constexpr bool treat_as_floating_point_v<long> = false; | |
322 | template<> | |
323 | inline constexpr bool treat_as_floating_point_v<long long> = false; | |
324 | template<> | |
325 | inline constexpr bool treat_as_floating_point_v<float> = true; | |
326 | template<> | |
327 | inline constexpr bool treat_as_floating_point_v<double> = true; | |
328 | template<> | |
329 | inline constexpr bool treat_as_floating_point_v<long double> = true; | |
7f78718b JW |
330 | #endif // C++17 |
331 | ||
332 | #if __cplusplus > 201703L | |
7f78718b JW |
333 | #if __cpp_lib_concepts |
334 | template<typename _Tp> | |
48099f7d | 335 | inline constexpr bool is_clock_v = false; |
7f78718b JW |
336 | |
337 | template<typename _Tp> | |
338 | requires requires { | |
339 | typename _Tp::rep; | |
340 | typename _Tp::period; | |
341 | typename _Tp::duration; | |
342 | typename _Tp::time_point::clock; | |
343 | typename _Tp::time_point::duration; | |
344 | { &_Tp::is_steady } -> same_as<const bool*>; | |
345 | { _Tp::now() } -> same_as<typename _Tp::time_point>; | |
346 | requires same_as<typename _Tp::duration, | |
347 | duration<typename _Tp::rep, typename _Tp::period>>; | |
348 | requires same_as<typename _Tp::time_point::duration, | |
349 | typename _Tp::duration>; | |
350 | } | |
48099f7d | 351 | inline constexpr bool is_clock_v<_Tp> = true; |
7f78718b JW |
352 | #else |
353 | template<typename _Tp, typename = void> | |
48099f7d | 354 | inline constexpr bool is_clock_v = false; |
7f78718b JW |
355 | |
356 | template<typename _Tp> | |
48099f7d JW |
357 | inline constexpr bool |
358 | is_clock_v<_Tp, void_t<typename _Tp::rep, typename _Tp::period, | |
359 | typename _Tp::duration, | |
360 | typename _Tp::time_point::duration, | |
361 | decltype(_Tp::is_steady), | |
362 | decltype(_Tp::now())>> | |
363 | = __and_v<is_same<typename _Tp::duration, | |
364 | duration<typename _Tp::rep, typename _Tp::period>>, | |
365 | is_same<typename _Tp::time_point::duration, | |
366 | typename _Tp::duration>, | |
367 | is_same<decltype(&_Tp::is_steady), const bool*>, | |
368 | is_same<decltype(_Tp::now()), typename _Tp::time_point>>; | |
369 | #endif | |
7f78718b JW |
370 | |
371 | template<typename _Tp> | |
48099f7d JW |
372 | struct is_clock |
373 | : bool_constant<is_clock_v<_Tp>> | |
7f78718b | 374 | { }; |
7f78718b JW |
375 | #endif // C++20 |
376 | ||
1dde83f0 | 377 | #if __cplusplus >= 201703L // C++ >= 17 |
646e979c JW |
378 | /** Convert a `duration` to type `ToDur` and round down. |
379 | * | |
380 | * If the duration cannot be represented exactly in the result type, | |
381 | * returns the closest value that is less than the argument. | |
382 | * | |
383 | * @tparam _ToDur The result type must be a `duration`. | |
384 | * @param __d A duration. | |
385 | * @return The value of `__d` converted to type `_ToDur`. | |
386 | * @since C++17 | |
387 | */ | |
7f78718b | 388 | template<typename _ToDur, typename _Rep, typename _Period> |
646e979c | 389 | [[nodiscard]] constexpr __enable_if_is_duration<_ToDur> |
7f78718b JW |
390 | floor(const duration<_Rep, _Period>& __d) |
391 | { | |
392 | auto __to = chrono::duration_cast<_ToDur>(__d); | |
393 | if (__to > __d) | |
394 | return __to - _ToDur{1}; | |
395 | return __to; | |
396 | } | |
397 | ||
646e979c JW |
398 | /** Convert a `duration` to type `ToDur` and round up. |
399 | * | |
400 | * If the duration cannot be represented exactly in the result type, | |
401 | * returns the closest value that is greater than the argument. | |
402 | * | |
403 | * @tparam _ToDur The result type must be a `duration`. | |
404 | * @param __d A duration. | |
405 | * @return The value of `__d` converted to type `_ToDur`. | |
406 | * @since C++17 | |
407 | */ | |
7f78718b | 408 | template<typename _ToDur, typename _Rep, typename _Period> |
646e979c | 409 | [[nodiscard]] constexpr __enable_if_is_duration<_ToDur> |
7f78718b JW |
410 | ceil(const duration<_Rep, _Period>& __d) |
411 | { | |
412 | auto __to = chrono::duration_cast<_ToDur>(__d); | |
413 | if (__to < __d) | |
414 | return __to + _ToDur{1}; | |
415 | return __to; | |
416 | } | |
417 | ||
646e979c JW |
418 | /** Convert a `duration` to type `ToDur` and round to the closest value. |
419 | * | |
420 | * If the duration cannot be represented exactly in the result type, | |
421 | * returns the closest value, rounding ties to even. | |
422 | * | |
423 | * @tparam _ToDur The result type must be a `duration` with a | |
424 | * non-floating-point `rep` type. | |
425 | * @param __d A duration. | |
426 | * @return The value of `__d` converted to type `_ToDur`. | |
427 | * @since C++17 | |
428 | */ | |
7f78718b | 429 | template <typename _ToDur, typename _Rep, typename _Period> |
646e979c JW |
430 | [[nodiscard]] constexpr |
431 | enable_if_t< | |
7f78718b JW |
432 | __and_<__is_duration<_ToDur>, |
433 | __not_<treat_as_floating_point<typename _ToDur::rep>>>::value, | |
434 | _ToDur> | |
435 | round(const duration<_Rep, _Period>& __d) | |
436 | { | |
437 | _ToDur __t0 = chrono::floor<_ToDur>(__d); | |
438 | _ToDur __t1 = __t0 + _ToDur{1}; | |
439 | auto __diff0 = __d - __t0; | |
440 | auto __diff1 = __t1 - __d; | |
441 | if (__diff0 == __diff1) | |
646e979c | 442 | { |
7f78718b | 443 | if (__t0.count() & 1) |
646e979c | 444 | return __t1; |
7f78718b | 445 | return __t0; |
646e979c | 446 | } |
7f78718b | 447 | else if (__diff0 < __diff1) |
646e979c | 448 | return __t0; |
7f78718b JW |
449 | return __t1; |
450 | } | |
451 | ||
646e979c JW |
452 | /** The absolute (non-negative) value of a duration. |
453 | * | |
454 | * @param __d A duration with a signed `rep` type. | |
455 | * @return A duration of the same type as the argument, with value |d|. | |
456 | * @since C++17 | |
457 | */ | |
7f78718b | 458 | template<typename _Rep, typename _Period> |
646e979c | 459 | [[nodiscard]] constexpr |
7f78718b JW |
460 | enable_if_t<numeric_limits<_Rep>::is_signed, duration<_Rep, _Period>> |
461 | abs(duration<_Rep, _Period> __d) | |
462 | { | |
463 | if (__d >= __d.zero()) | |
464 | return __d; | |
465 | return -__d; | |
466 | } | |
467 | ||
468 | // Make chrono::ceil<D> also usable as chrono::__detail::ceil<D>. | |
469 | namespace __detail { using chrono::ceil; } | |
470 | ||
7ffa63df | 471 | #else // ! __glibcxx_chrono |
7f78718b JW |
472 | |
473 | // We want to use ceil even when compiling for earlier standards versions. | |
474 | // C++11 only allows a single statement in a constexpr function, so we | |
475 | // need to move the comparison into a separate function, __ceil_impl. | |
476 | namespace __detail | |
477 | { | |
478 | template<typename _Tp, typename _Up> | |
479 | constexpr _Tp | |
480 | __ceil_impl(const _Tp& __t, const _Up& __u) | |
481 | { | |
482 | return (__t < __u) ? (__t + _Tp{1}) : __t; | |
483 | } | |
484 | ||
485 | // C++11-friendly version of std::chrono::ceil<D> for internal use. | |
486 | template<typename _ToDur, typename _Rep, typename _Period> | |
487 | constexpr _ToDur | |
488 | ceil(const duration<_Rep, _Period>& __d) | |
489 | { | |
490 | return __detail::__ceil_impl(chrono::duration_cast<_ToDur>(__d), __d); | |
491 | } | |
492 | } | |
7ffa63df | 493 | #endif // __glibcxx_chrono |
7f78718b JW |
494 | |
495 | /// duration_values | |
496 | template<typename _Rep> | |
497 | struct duration_values | |
498 | { | |
499 | static constexpr _Rep | |
500 | zero() noexcept | |
501 | { return _Rep(0); } | |
502 | ||
503 | static constexpr _Rep | |
504 | max() noexcept | |
505 | { return numeric_limits<_Rep>::max(); } | |
506 | ||
507 | static constexpr _Rep | |
508 | min() noexcept | |
509 | { return numeric_limits<_Rep>::lowest(); } | |
510 | }; | |
511 | ||
7f78718b | 512 | template<typename _Rep, typename _Period> |
7eec3114 | 513 | class duration |
7f78718b | 514 | { |
2d614822 JW |
515 | static_assert(!__is_duration<_Rep>::value, |
516 | "rep cannot be a std::chrono::duration"); | |
ed77dcb9 | 517 | static_assert(__is_ratio<_Period>::value, |
2d614822 | 518 | "period must be a specialization of std::ratio"); |
ed77dcb9 JW |
519 | static_assert(_Period::num > 0, "period must be positive"); |
520 | ||
7f78718b JW |
521 | template<typename _Rep2> |
522 | using __is_float = treat_as_floating_point<_Rep2>; | |
523 | ||
524 | static constexpr intmax_t | |
525 | _S_gcd(intmax_t __m, intmax_t __n) noexcept | |
526 | { | |
527 | // Duration only allows positive periods so we don't need to | |
528 | // handle negative values here (unlike __static_gcd and std::gcd). | |
529 | #if __cplusplus >= 201402L | |
530 | do | |
531 | { | |
532 | intmax_t __rem = __m % __n; | |
533 | __m = __n; | |
534 | __n = __rem; | |
535 | } | |
536 | while (__n != 0); | |
537 | return __m; | |
538 | #else | |
539 | // C++11 doesn't allow loops in constexpr functions, but this | |
540 | // recursive version can be more expensive to evaluate. | |
541 | return (__n == 0) ? __m : _S_gcd(__n, __m % __n); | |
542 | #endif | |
543 | } | |
544 | ||
545 | // _GLIBCXX_RESOLVE_LIB_DEFECTS | |
546 | // 2094. overflow shouldn't participate in overload resolution | |
547 | // 3090. What is [2094] intended to mean? | |
548 | // This only produces a valid type if no overflow occurs. | |
549 | template<typename _R1, typename _R2, | |
550 | intmax_t __gcd1 = _S_gcd(_R1::num, _R2::num), | |
551 | intmax_t __gcd2 = _S_gcd(_R1::den, _R2::den)> | |
552 | using __divide = ratio<(_R1::num / __gcd1) * (_R2::den / __gcd2), | |
553 | (_R1::den / __gcd2) * (_R2::num / __gcd1)>; | |
554 | ||
555 | // _Period2 is an exact multiple of _Period | |
556 | template<typename _Period2> | |
557 | using __is_harmonic | |
558 | = __bool_constant<__divide<_Period2, _Period>::den == 1>; | |
559 | ||
560 | public: | |
561 | ||
562 | using rep = _Rep; | |
563 | using period = typename _Period::type; | |
564 | ||
7f78718b JW |
565 | // 20.11.5.1 construction / copy / destroy |
566 | constexpr duration() = default; | |
567 | ||
568 | duration(const duration&) = default; | |
569 | ||
570 | // _GLIBCXX_RESOLVE_LIB_DEFECTS | |
571 | // 3050. Conversion specification problem in chrono::duration | |
572 | template<typename _Rep2, typename = _Require< | |
573 | is_convertible<const _Rep2&, rep>, | |
574 | __or_<__is_float<rep>, __not_<__is_float<_Rep2>>>>> | |
575 | constexpr explicit duration(const _Rep2& __rep) | |
576 | : __r(static_cast<rep>(__rep)) { } | |
577 | ||
578 | template<typename _Rep2, typename _Period2, typename = _Require< | |
579 | is_convertible<const _Rep2&, rep>, | |
580 | __or_<__is_float<rep>, | |
581 | __and_<__is_harmonic<_Period2>, | |
582 | __not_<__is_float<_Rep2>>>>>> | |
583 | constexpr duration(const duration<_Rep2, _Period2>& __d) | |
584 | : __r(duration_cast<duration>(__d).count()) { } | |
585 | ||
586 | ~duration() = default; | |
587 | duration& operator=(const duration&) = default; | |
588 | ||
589 | // 20.11.5.2 observer | |
590 | constexpr rep | |
591 | count() const | |
592 | { return __r; } | |
593 | ||
594 | // 20.11.5.3 arithmetic | |
595 | ||
596 | constexpr duration<typename common_type<rep>::type, period> | |
597 | operator+() const | |
598 | { return duration<typename common_type<rep>::type, period>(__r); } | |
599 | ||
600 | constexpr duration<typename common_type<rep>::type, period> | |
601 | operator-() const | |
602 | { return duration<typename common_type<rep>::type, period>(-__r); } | |
603 | ||
604 | _GLIBCXX17_CONSTEXPR duration& | |
605 | operator++() | |
606 | { | |
607 | ++__r; | |
608 | return *this; | |
609 | } | |
610 | ||
611 | _GLIBCXX17_CONSTEXPR duration | |
612 | operator++(int) | |
613 | { return duration(__r++); } | |
614 | ||
615 | _GLIBCXX17_CONSTEXPR duration& | |
616 | operator--() | |
617 | { | |
618 | --__r; | |
619 | return *this; | |
620 | } | |
621 | ||
622 | _GLIBCXX17_CONSTEXPR duration | |
623 | operator--(int) | |
624 | { return duration(__r--); } | |
625 | ||
626 | _GLIBCXX17_CONSTEXPR duration& | |
627 | operator+=(const duration& __d) | |
628 | { | |
629 | __r += __d.count(); | |
630 | return *this; | |
631 | } | |
632 | ||
633 | _GLIBCXX17_CONSTEXPR duration& | |
634 | operator-=(const duration& __d) | |
635 | { | |
636 | __r -= __d.count(); | |
637 | return *this; | |
638 | } | |
639 | ||
640 | _GLIBCXX17_CONSTEXPR duration& | |
641 | operator*=(const rep& __rhs) | |
642 | { | |
643 | __r *= __rhs; | |
644 | return *this; | |
645 | } | |
646 | ||
647 | _GLIBCXX17_CONSTEXPR duration& | |
648 | operator/=(const rep& __rhs) | |
649 | { | |
650 | __r /= __rhs; | |
651 | return *this; | |
652 | } | |
653 | ||
654 | // DR 934. | |
655 | template<typename _Rep2 = rep> | |
656 | _GLIBCXX17_CONSTEXPR | |
d2d3826c | 657 | __enable_if_t<!treat_as_floating_point<_Rep2>::value, duration&> |
7f78718b JW |
658 | operator%=(const rep& __rhs) |
659 | { | |
660 | __r %= __rhs; | |
661 | return *this; | |
662 | } | |
663 | ||
664 | template<typename _Rep2 = rep> | |
665 | _GLIBCXX17_CONSTEXPR | |
d2d3826c | 666 | __enable_if_t<!treat_as_floating_point<_Rep2>::value, duration&> |
7f78718b JW |
667 | operator%=(const duration& __d) |
668 | { | |
669 | __r %= __d.count(); | |
670 | return *this; | |
671 | } | |
672 | ||
673 | // 20.11.5.4 special values | |
674 | static constexpr duration | |
675 | zero() noexcept | |
676 | { return duration(duration_values<rep>::zero()); } | |
677 | ||
678 | static constexpr duration | |
679 | min() noexcept | |
680 | { return duration(duration_values<rep>::min()); } | |
681 | ||
682 | static constexpr duration | |
683 | max() noexcept | |
684 | { return duration(duration_values<rep>::max()); } | |
685 | ||
686 | private: | |
687 | rep __r; | |
688 | }; | |
689 | ||
690 | /// @{ | |
691 | /// @relates std::chrono::duration | |
692 | ||
693 | /// The sum of two durations. | |
694 | template<typename _Rep1, typename _Period1, | |
695 | typename _Rep2, typename _Period2> | |
696 | constexpr typename common_type<duration<_Rep1, _Period1>, | |
697 | duration<_Rep2, _Period2>>::type | |
698 | operator+(const duration<_Rep1, _Period1>& __lhs, | |
699 | const duration<_Rep2, _Period2>& __rhs) | |
700 | { | |
701 | typedef duration<_Rep1, _Period1> __dur1; | |
702 | typedef duration<_Rep2, _Period2> __dur2; | |
703 | typedef typename common_type<__dur1,__dur2>::type __cd; | |
704 | return __cd(__cd(__lhs).count() + __cd(__rhs).count()); | |
705 | } | |
706 | ||
707 | /// The difference between two durations. | |
708 | template<typename _Rep1, typename _Period1, | |
709 | typename _Rep2, typename _Period2> | |
710 | constexpr typename common_type<duration<_Rep1, _Period1>, | |
711 | duration<_Rep2, _Period2>>::type | |
712 | operator-(const duration<_Rep1, _Period1>& __lhs, | |
713 | const duration<_Rep2, _Period2>& __rhs) | |
714 | { | |
715 | typedef duration<_Rep1, _Period1> __dur1; | |
716 | typedef duration<_Rep2, _Period2> __dur2; | |
717 | typedef typename common_type<__dur1,__dur2>::type __cd; | |
718 | return __cd(__cd(__lhs).count() - __cd(__rhs).count()); | |
719 | } | |
720 | ||
721 | /// @} | |
722 | ||
723 | /// @cond undocumented | |
724 | ||
725 | // SFINAE helper to obtain common_type<_Rep1, _Rep2> only if _Rep2 | |
726 | // is implicitly convertible to it. | |
727 | // _GLIBCXX_RESOLVE_LIB_DEFECTS | |
728 | // 3050. Conversion specification problem in chrono::duration constructor | |
729 | template<typename _Rep1, typename _Rep2, | |
730 | typename _CRep = typename common_type<_Rep1, _Rep2>::type> | |
731 | using __common_rep_t = typename | |
732 | enable_if<is_convertible<const _Rep2&, _CRep>::value, _CRep>::type; | |
733 | ||
734 | /// @endcond | |
735 | ||
736 | /** @{ | |
737 | * Arithmetic operators for chrono::duration | |
738 | * @relates std::chrono::duration | |
739 | */ | |
740 | ||
741 | template<typename _Rep1, typename _Period, typename _Rep2> | |
742 | constexpr duration<__common_rep_t<_Rep1, _Rep2>, _Period> | |
743 | operator*(const duration<_Rep1, _Period>& __d, const _Rep2& __s) | |
744 | { | |
745 | typedef duration<typename common_type<_Rep1, _Rep2>::type, _Period> | |
746 | __cd; | |
747 | return __cd(__cd(__d).count() * __s); | |
748 | } | |
749 | ||
750 | template<typename _Rep1, typename _Rep2, typename _Period> | |
751 | constexpr duration<__common_rep_t<_Rep2, _Rep1>, _Period> | |
752 | operator*(const _Rep1& __s, const duration<_Rep2, _Period>& __d) | |
753 | { return __d * __s; } | |
754 | ||
755 | template<typename _Rep1, typename _Period, typename _Rep2> | |
756 | constexpr | |
757 | duration<__common_rep_t<_Rep1, __disable_if_is_duration<_Rep2>>, _Period> | |
758 | operator/(const duration<_Rep1, _Period>& __d, const _Rep2& __s) | |
759 | { | |
760 | typedef duration<typename common_type<_Rep1, _Rep2>::type, _Period> | |
761 | __cd; | |
762 | return __cd(__cd(__d).count() / __s); | |
763 | } | |
764 | ||
765 | template<typename _Rep1, typename _Period1, | |
766 | typename _Rep2, typename _Period2> | |
767 | constexpr typename common_type<_Rep1, _Rep2>::type | |
768 | operator/(const duration<_Rep1, _Period1>& __lhs, | |
769 | const duration<_Rep2, _Period2>& __rhs) | |
770 | { | |
771 | typedef duration<_Rep1, _Period1> __dur1; | |
772 | typedef duration<_Rep2, _Period2> __dur2; | |
773 | typedef typename common_type<__dur1,__dur2>::type __cd; | |
774 | return __cd(__lhs).count() / __cd(__rhs).count(); | |
775 | } | |
776 | ||
777 | // DR 934. | |
778 | template<typename _Rep1, typename _Period, typename _Rep2> | |
779 | constexpr | |
780 | duration<__common_rep_t<_Rep1, __disable_if_is_duration<_Rep2>>, _Period> | |
781 | operator%(const duration<_Rep1, _Period>& __d, const _Rep2& __s) | |
782 | { | |
783 | typedef duration<typename common_type<_Rep1, _Rep2>::type, _Period> | |
784 | __cd; | |
785 | return __cd(__cd(__d).count() % __s); | |
786 | } | |
787 | ||
788 | template<typename _Rep1, typename _Period1, | |
789 | typename _Rep2, typename _Period2> | |
790 | constexpr typename common_type<duration<_Rep1, _Period1>, | |
791 | duration<_Rep2, _Period2>>::type | |
792 | operator%(const duration<_Rep1, _Period1>& __lhs, | |
793 | const duration<_Rep2, _Period2>& __rhs) | |
794 | { | |
795 | typedef duration<_Rep1, _Period1> __dur1; | |
796 | typedef duration<_Rep2, _Period2> __dur2; | |
797 | typedef typename common_type<__dur1,__dur2>::type __cd; | |
798 | return __cd(__cd(__lhs).count() % __cd(__rhs).count()); | |
799 | } | |
800 | /// @} | |
801 | ||
802 | // comparisons | |
803 | ||
804 | /** @{ | |
805 | * Comparisons for chrono::duration | |
806 | * @relates std::chrono::duration | |
807 | */ | |
808 | ||
809 | template<typename _Rep1, typename _Period1, | |
810 | typename _Rep2, typename _Period2> | |
811 | constexpr bool | |
812 | operator==(const duration<_Rep1, _Period1>& __lhs, | |
813 | const duration<_Rep2, _Period2>& __rhs) | |
814 | { | |
815 | typedef duration<_Rep1, _Period1> __dur1; | |
816 | typedef duration<_Rep2, _Period2> __dur2; | |
817 | typedef typename common_type<__dur1,__dur2>::type __ct; | |
818 | return __ct(__lhs).count() == __ct(__rhs).count(); | |
819 | } | |
820 | ||
821 | template<typename _Rep1, typename _Period1, | |
822 | typename _Rep2, typename _Period2> | |
823 | constexpr bool | |
824 | operator<(const duration<_Rep1, _Period1>& __lhs, | |
825 | const duration<_Rep2, _Period2>& __rhs) | |
826 | { | |
827 | typedef duration<_Rep1, _Period1> __dur1; | |
828 | typedef duration<_Rep2, _Period2> __dur2; | |
829 | typedef typename common_type<__dur1,__dur2>::type __ct; | |
830 | return __ct(__lhs).count() < __ct(__rhs).count(); | |
831 | } | |
832 | ||
833 | #if __cpp_lib_three_way_comparison | |
834 | template<typename _Rep1, typename _Period1, | |
835 | typename _Rep2, typename _Period2> | |
836 | requires three_way_comparable<common_type_t<_Rep1, _Rep2>> | |
837 | constexpr auto | |
838 | operator<=>(const duration<_Rep1, _Period1>& __lhs, | |
839 | const duration<_Rep2, _Period2>& __rhs) | |
840 | { | |
841 | using __ct = common_type_t<duration<_Rep1, _Period1>, | |
842 | duration<_Rep2, _Period2>>; | |
843 | return __ct(__lhs).count() <=> __ct(__rhs).count(); | |
844 | } | |
845 | #else | |
846 | template<typename _Rep1, typename _Period1, | |
847 | typename _Rep2, typename _Period2> | |
848 | constexpr bool | |
849 | operator!=(const duration<_Rep1, _Period1>& __lhs, | |
850 | const duration<_Rep2, _Period2>& __rhs) | |
851 | { return !(__lhs == __rhs); } | |
852 | #endif | |
853 | ||
854 | template<typename _Rep1, typename _Period1, | |
855 | typename _Rep2, typename _Period2> | |
856 | constexpr bool | |
857 | operator<=(const duration<_Rep1, _Period1>& __lhs, | |
858 | const duration<_Rep2, _Period2>& __rhs) | |
859 | { return !(__rhs < __lhs); } | |
860 | ||
861 | template<typename _Rep1, typename _Period1, | |
862 | typename _Rep2, typename _Period2> | |
863 | constexpr bool | |
864 | operator>(const duration<_Rep1, _Period1>& __lhs, | |
865 | const duration<_Rep2, _Period2>& __rhs) | |
866 | { return __rhs < __lhs; } | |
867 | ||
868 | template<typename _Rep1, typename _Period1, | |
869 | typename _Rep2, typename _Period2> | |
870 | constexpr bool | |
871 | operator>=(const duration<_Rep1, _Period1>& __lhs, | |
872 | const duration<_Rep2, _Period2>& __rhs) | |
873 | { return !(__lhs < __rhs); } | |
874 | ||
875 | /// @} | |
876 | ||
877 | /// @cond undocumented | |
5baabdb1 | 878 | #ifdef _GLIBCXX_USE_C99_STDINT |
7f78718b JW |
879 | # define _GLIBCXX_CHRONO_INT64_T int64_t |
880 | #elif defined __INT64_TYPE__ | |
881 | # define _GLIBCXX_CHRONO_INT64_T __INT64_TYPE__ | |
882 | #else | |
883 | static_assert(std::numeric_limits<unsigned long long>::digits >= 64, | |
884 | "Representation type for nanoseconds must have at least 64 bits"); | |
885 | # define _GLIBCXX_CHRONO_INT64_T long long | |
886 | #endif | |
887 | /// @endcond | |
888 | ||
889 | /// nanoseconds | |
890 | using nanoseconds = duration<_GLIBCXX_CHRONO_INT64_T, nano>; | |
891 | ||
892 | /// microseconds | |
893 | using microseconds = duration<_GLIBCXX_CHRONO_INT64_T, micro>; | |
894 | ||
895 | /// milliseconds | |
896 | using milliseconds = duration<_GLIBCXX_CHRONO_INT64_T, milli>; | |
897 | ||
898 | /// seconds | |
899 | using seconds = duration<_GLIBCXX_CHRONO_INT64_T>; | |
900 | ||
901 | /// minutes | |
902 | using minutes = duration<_GLIBCXX_CHRONO_INT64_T, ratio< 60>>; | |
903 | ||
904 | /// hours | |
905 | using hours = duration<_GLIBCXX_CHRONO_INT64_T, ratio<3600>>; | |
906 | ||
907 | #if __cplusplus > 201703L | |
908 | /// days | |
909 | using days = duration<_GLIBCXX_CHRONO_INT64_T, ratio<86400>>; | |
910 | ||
911 | /// weeks | |
912 | using weeks = duration<_GLIBCXX_CHRONO_INT64_T, ratio<604800>>; | |
913 | ||
914 | /// years | |
915 | using years = duration<_GLIBCXX_CHRONO_INT64_T, ratio<31556952>>; | |
916 | ||
917 | /// months | |
918 | using months = duration<_GLIBCXX_CHRONO_INT64_T, ratio<2629746>>; | |
919 | #endif // C++20 | |
920 | ||
921 | #undef _GLIBCXX_CHRONO_INT64_T | |
922 | ||
923 | template<typename _Clock, typename _Dur> | |
7eec3114 | 924 | class time_point |
7f78718b JW |
925 | { |
926 | static_assert(__is_duration<_Dur>::value, | |
927 | "duration must be a specialization of std::chrono::duration"); | |
928 | ||
7eec3114 | 929 | public: |
7f78718b JW |
930 | typedef _Clock clock; |
931 | typedef _Dur duration; | |
932 | typedef typename duration::rep rep; | |
933 | typedef typename duration::period period; | |
934 | ||
935 | constexpr time_point() : __d(duration::zero()) | |
936 | { } | |
937 | ||
938 | constexpr explicit time_point(const duration& __dur) | |
939 | : __d(__dur) | |
940 | { } | |
941 | ||
942 | // conversions | |
943 | template<typename _Dur2, | |
944 | typename = _Require<is_convertible<_Dur2, _Dur>>> | |
945 | constexpr time_point(const time_point<clock, _Dur2>& __t) | |
946 | : __d(__t.time_since_epoch()) | |
947 | { } | |
948 | ||
949 | // observer | |
950 | constexpr duration | |
951 | time_since_epoch() const | |
952 | { return __d; } | |
953 | ||
954 | #if __cplusplus > 201703L | |
955 | constexpr time_point& | |
956 | operator++() | |
957 | { | |
958 | ++__d; | |
959 | return *this; | |
960 | } | |
961 | ||
962 | constexpr time_point | |
963 | operator++(int) | |
964 | { return time_point{__d++}; } | |
965 | ||
966 | constexpr time_point& | |
967 | operator--() | |
968 | { | |
969 | --__d; | |
970 | return *this; | |
971 | } | |
972 | ||
973 | constexpr time_point | |
974 | operator--(int) | |
975 | { return time_point{__d--}; } | |
976 | #endif | |
977 | ||
978 | // arithmetic | |
979 | _GLIBCXX17_CONSTEXPR time_point& | |
980 | operator+=(const duration& __dur) | |
981 | { | |
982 | __d += __dur; | |
983 | return *this; | |
984 | } | |
985 | ||
986 | _GLIBCXX17_CONSTEXPR time_point& | |
987 | operator-=(const duration& __dur) | |
988 | { | |
989 | __d -= __dur; | |
990 | return *this; | |
991 | } | |
992 | ||
993 | // special values | |
994 | static constexpr time_point | |
995 | min() noexcept | |
996 | { return time_point(duration::min()); } | |
997 | ||
998 | static constexpr time_point | |
999 | max() noexcept | |
1000 | { return time_point(duration::max()); } | |
1001 | ||
1002 | private: | |
1003 | duration __d; | |
1004 | }; | |
1005 | ||
646e979c JW |
1006 | /** Convert a `time_point` to use `duration` type `ToDur`. |
1007 | * | |
1008 | * The result is the same time point as measured by the same clock, but | |
1009 | * using the specified `duration` to represent the time. | |
1010 | * If the time point cannot be represented accurately in the result type, | |
1011 | * returns the result of integer truncation (i.e., rounded towards zero). | |
1012 | * | |
1013 | * @tparam _ToDur The `duration` type to use for the result. | |
1014 | * @param __t A time point. | |
1015 | * @return The value of `__t` converted to use type `_ToDur`. | |
1016 | * @since C++11 | |
1017 | */ | |
7f78718b | 1018 | template<typename _ToDur, typename _Clock, typename _Dur> |
646e979c JW |
1019 | _GLIBCXX_NODISCARD constexpr |
1020 | __enable_if_t<__is_duration<_ToDur>::value, time_point<_Clock, _ToDur>> | |
7f78718b JW |
1021 | time_point_cast(const time_point<_Clock, _Dur>& __t) |
1022 | { | |
1023 | typedef time_point<_Clock, _ToDur> __time_point; | |
1024 | return __time_point(duration_cast<_ToDur>(__t.time_since_epoch())); | |
1025 | } | |
1026 | ||
1027 | #if __cplusplus > 201402L | |
646e979c JW |
1028 | /** Convert a `time_point` to type `ToDur` and round down. |
1029 | * | |
1030 | * The result is the same time point as measured by the same clock, but | |
1031 | * using the specified `duration` to represent the time. | |
1032 | * If the time point cannot be represented exactly in the result type, | |
1033 | * returns the closest value that is less than the argument. | |
1034 | * | |
1035 | * @tparam _ToDur The `duration` type to use for the result. | |
1036 | * @param __t A time point. | |
1037 | * @return The value of `__d` converted to type `_ToDur`. | |
1038 | * @since C++17 | |
1039 | */ | |
7f78718b | 1040 | template<typename _ToDur, typename _Clock, typename _Dur> |
646e979c | 1041 | [[nodiscard]] constexpr |
d2d3826c | 1042 | enable_if_t<__is_duration_v<_ToDur>, time_point<_Clock, _ToDur>> |
7f78718b JW |
1043 | floor(const time_point<_Clock, _Dur>& __tp) |
1044 | { | |
1045 | return time_point<_Clock, _ToDur>{ | |
1046 | chrono::floor<_ToDur>(__tp.time_since_epoch())}; | |
1047 | } | |
1048 | ||
646e979c JW |
1049 | /** Convert a `time_point` to type `ToDur` and round up. |
1050 | * | |
1051 | * The result is the same time point as measured by the same clock, but | |
1052 | * using the specified `duration` to represent the time. | |
1053 | * If the time point cannot be represented exactly in the result type, | |
1054 | * returns the closest value that is greater than the argument. | |
1055 | * | |
1056 | * @tparam _ToDur The `duration` type to use for the result. | |
1057 | * @param __t A time point. | |
1058 | * @return The value of `__d` converted to type `_ToDur`. | |
1059 | * @since C++17 | |
1060 | */ | |
7f78718b | 1061 | template<typename _ToDur, typename _Clock, typename _Dur> |
646e979c | 1062 | [[nodiscard]] constexpr |
d2d3826c | 1063 | enable_if_t<__is_duration_v<_ToDur>, time_point<_Clock, _ToDur>> |
7f78718b JW |
1064 | ceil(const time_point<_Clock, _Dur>& __tp) |
1065 | { | |
1066 | return time_point<_Clock, _ToDur>{ | |
1067 | chrono::ceil<_ToDur>(__tp.time_since_epoch())}; | |
1068 | } | |
1069 | ||
646e979c JW |
1070 | /** Convert a `time_point` to type `ToDur` and round to the closest value. |
1071 | * | |
1072 | * The result is the same time point as measured by the same clock, but | |
1073 | * using the specified `duration` to represent the time. | |
1074 | * If the time point cannot be represented exactly in the result type, | |
1075 | * returns the closest value, rounding ties to even. | |
1076 | * | |
1077 | * @tparam _ToDur The `duration` type to use for the result, | |
1078 | * which must have a non-floating-point `rep` type. | |
1079 | * @param __t A time point. | |
1080 | * @return The value of `__d` converted to type `_ToDur`. | |
1081 | * @since C++17 | |
1082 | */ | |
7f78718b | 1083 | template<typename _ToDur, typename _Clock, typename _Dur> |
646e979c | 1084 | [[nodiscard]] constexpr |
d2d3826c JW |
1085 | enable_if_t<__is_duration_v<_ToDur> |
1086 | && !treat_as_floating_point_v<typename _ToDur::rep>, | |
1087 | time_point<_Clock, _ToDur>> | |
7f78718b JW |
1088 | round(const time_point<_Clock, _Dur>& __tp) |
1089 | { | |
1090 | return time_point<_Clock, _ToDur>{ | |
1091 | chrono::round<_ToDur>(__tp.time_since_epoch())}; | |
1092 | } | |
1093 | #endif // C++17 | |
1094 | ||
1095 | /// @{ | |
1096 | /// @relates time_point | |
1097 | ||
1098 | /// Adjust a time point forwards by the given duration. | |
1099 | template<typename _Clock, typename _Dur1, | |
1100 | typename _Rep2, typename _Period2> | |
1101 | constexpr time_point<_Clock, | |
1102 | typename common_type<_Dur1, duration<_Rep2, _Period2>>::type> | |
1103 | operator+(const time_point<_Clock, _Dur1>& __lhs, | |
1104 | const duration<_Rep2, _Period2>& __rhs) | |
1105 | { | |
1106 | typedef duration<_Rep2, _Period2> __dur2; | |
1107 | typedef typename common_type<_Dur1,__dur2>::type __ct; | |
1108 | typedef time_point<_Clock, __ct> __time_point; | |
1109 | return __time_point(__lhs.time_since_epoch() + __rhs); | |
1110 | } | |
1111 | ||
1112 | /// Adjust a time point forwards by the given duration. | |
1113 | template<typename _Rep1, typename _Period1, | |
1114 | typename _Clock, typename _Dur2> | |
1115 | constexpr time_point<_Clock, | |
1116 | typename common_type<duration<_Rep1, _Period1>, _Dur2>::type> | |
1117 | operator+(const duration<_Rep1, _Period1>& __lhs, | |
1118 | const time_point<_Clock, _Dur2>& __rhs) | |
1119 | { | |
1120 | typedef duration<_Rep1, _Period1> __dur1; | |
1121 | typedef typename common_type<__dur1,_Dur2>::type __ct; | |
1122 | typedef time_point<_Clock, __ct> __time_point; | |
1123 | return __time_point(__rhs.time_since_epoch() + __lhs); | |
1124 | } | |
1125 | ||
1126 | /// Adjust a time point backwards by the given duration. | |
1127 | template<typename _Clock, typename _Dur1, | |
1128 | typename _Rep2, typename _Period2> | |
1129 | constexpr time_point<_Clock, | |
1130 | typename common_type<_Dur1, duration<_Rep2, _Period2>>::type> | |
1131 | operator-(const time_point<_Clock, _Dur1>& __lhs, | |
1132 | const duration<_Rep2, _Period2>& __rhs) | |
1133 | { | |
1134 | typedef duration<_Rep2, _Period2> __dur2; | |
1135 | typedef typename common_type<_Dur1,__dur2>::type __ct; | |
1136 | typedef time_point<_Clock, __ct> __time_point; | |
1137 | return __time_point(__lhs.time_since_epoch() -__rhs); | |
1138 | } | |
1139 | ||
1140 | /// The difference between two time points (as a duration) | |
1141 | template<typename _Clock, typename _Dur1, typename _Dur2> | |
1142 | constexpr typename common_type<_Dur1, _Dur2>::type | |
1143 | operator-(const time_point<_Clock, _Dur1>& __lhs, | |
1144 | const time_point<_Clock, _Dur2>& __rhs) | |
1145 | { return __lhs.time_since_epoch() - __rhs.time_since_epoch(); } | |
1146 | /// @} | |
1147 | ||
1148 | /** @{ | |
1149 | * Comparisons for time_point | |
1150 | * @relates chrono::time_point | |
1151 | */ | |
1152 | ||
1153 | template<typename _Clock, typename _Dur1, typename _Dur2> | |
1154 | constexpr bool | |
1155 | operator==(const time_point<_Clock, _Dur1>& __lhs, | |
1156 | const time_point<_Clock, _Dur2>& __rhs) | |
1157 | { return __lhs.time_since_epoch() == __rhs.time_since_epoch(); } | |
1158 | ||
1159 | #if __cpp_lib_three_way_comparison | |
1160 | template<typename _Clock, typename _Dur1, | |
1161 | three_way_comparable_with<_Dur1> _Dur2> | |
1162 | constexpr auto | |
1163 | operator<=>(const time_point<_Clock, _Dur1>& __lhs, | |
1164 | const time_point<_Clock, _Dur2>& __rhs) | |
1165 | { return __lhs.time_since_epoch() <=> __rhs.time_since_epoch(); } | |
1166 | #else | |
1167 | template<typename _Clock, typename _Dur1, typename _Dur2> | |
1168 | constexpr bool | |
1169 | operator!=(const time_point<_Clock, _Dur1>& __lhs, | |
1170 | const time_point<_Clock, _Dur2>& __rhs) | |
1171 | { return !(__lhs == __rhs); } | |
1172 | #endif | |
1173 | ||
1174 | template<typename _Clock, typename _Dur1, typename _Dur2> | |
1175 | constexpr bool | |
1176 | operator<(const time_point<_Clock, _Dur1>& __lhs, | |
1177 | const time_point<_Clock, _Dur2>& __rhs) | |
1178 | { return __lhs.time_since_epoch() < __rhs.time_since_epoch(); } | |
1179 | ||
1180 | template<typename _Clock, typename _Dur1, typename _Dur2> | |
1181 | constexpr bool | |
1182 | operator<=(const time_point<_Clock, _Dur1>& __lhs, | |
1183 | const time_point<_Clock, _Dur2>& __rhs) | |
1184 | { return !(__rhs < __lhs); } | |
1185 | ||
1186 | template<typename _Clock, typename _Dur1, typename _Dur2> | |
1187 | constexpr bool | |
1188 | operator>(const time_point<_Clock, _Dur1>& __lhs, | |
1189 | const time_point<_Clock, _Dur2>& __rhs) | |
1190 | { return __rhs < __lhs; } | |
1191 | ||
1192 | template<typename _Clock, typename _Dur1, typename _Dur2> | |
1193 | constexpr bool | |
1194 | operator>=(const time_point<_Clock, _Dur1>& __lhs, | |
1195 | const time_point<_Clock, _Dur2>& __rhs) | |
1196 | { return !(__lhs < __rhs); } | |
1197 | ||
1198 | /// @} | |
1199 | /// @} group chrono | |
1200 | ||
1dde83f0 | 1201 | #if _GLIBCXX_HOSTED |
7f78718b JW |
1202 | // Clocks. |
1203 | ||
1204 | // Why nanosecond resolution as the default? | |
1205 | // Why have std::system_clock always count in the highest | |
1206 | // resolution (ie nanoseconds), even if on some OSes the low 3 | |
1207 | // or 9 decimal digits will be always zero? This allows later | |
1208 | // implementations to change the system_clock::now() | |
1209 | // implementation any time to provide better resolution without | |
1210 | // changing function signature or units. | |
1211 | ||
1212 | // To support the (forward) evolution of the library's defined | |
1213 | // clocks, wrap inside inline namespace so that the current | |
1214 | // defintions of system_clock, steady_clock, and | |
1215 | // high_resolution_clock types are uniquely mangled. This way, new | |
1216 | // code can use the latests clocks, while the library can contain | |
1217 | // compatibility definitions for previous versions. At some | |
1218 | // point, when these clocks settle down, the inlined namespaces | |
1219 | // can be removed. XXX GLIBCXX_ABI Deprecated | |
e4905f11 | 1220 | _GLIBCXX_BEGIN_INLINE_ABI_NAMESPACE(_V2) |
7f78718b JW |
1221 | |
1222 | /** | |
1223 | * @brief System clock. | |
1224 | * | |
1225 | * Time returned represents wall time from the system-wide clock. | |
1226 | * @ingroup chrono | |
1227 | */ | |
1228 | struct system_clock | |
1229 | { | |
1230 | typedef chrono::nanoseconds duration; | |
1231 | typedef duration::rep rep; | |
1232 | typedef duration::period period; | |
1233 | typedef chrono::time_point<system_clock, duration> time_point; | |
1234 | ||
1235 | static_assert(system_clock::duration::min() | |
1236 | < system_clock::duration::zero(), | |
1237 | "a clock's minimum duration cannot be less than its epoch"); | |
1238 | ||
1239 | static constexpr bool is_steady = false; | |
1240 | ||
1241 | static time_point | |
1242 | now() noexcept; | |
1243 | ||
1244 | // Map to C API | |
1245 | static std::time_t | |
1246 | to_time_t(const time_point& __t) noexcept | |
1247 | { | |
1248 | return std::time_t(duration_cast<chrono::seconds> | |
1249 | (__t.time_since_epoch()).count()); | |
1250 | } | |
1251 | ||
1252 | static time_point | |
1253 | from_time_t(std::time_t __t) noexcept | |
1254 | { | |
1255 | typedef chrono::time_point<system_clock, seconds> __from; | |
1256 | return time_point_cast<system_clock::duration> | |
1257 | (__from(chrono::seconds(__t))); | |
1258 | } | |
1259 | }; | |
1260 | ||
1261 | ||
1262 | /** | |
1263 | * @brief Monotonic clock | |
1264 | * | |
1265 | * Time returned has the property of only increasing at a uniform rate. | |
1266 | * @ingroup chrono | |
1267 | */ | |
1268 | struct steady_clock | |
1269 | { | |
1270 | typedef chrono::nanoseconds duration; | |
1271 | typedef duration::rep rep; | |
1272 | typedef duration::period period; | |
1273 | typedef chrono::time_point<steady_clock, duration> time_point; | |
1274 | ||
1275 | static constexpr bool is_steady = true; | |
1276 | ||
1277 | static time_point | |
1278 | now() noexcept; | |
1279 | }; | |
1280 | ||
1281 | ||
1282 | /** | |
1283 | * @brief Highest-resolution clock | |
1284 | * | |
1285 | * This is the clock "with the shortest tick period." Alias to | |
1286 | * std::system_clock until higher-than-nanosecond definitions | |
1287 | * become feasible. | |
1288 | * @ingroup chrono | |
1289 | */ | |
1290 | using high_resolution_clock = system_clock; | |
1291 | ||
e4905f11 | 1292 | _GLIBCXX_END_INLINE_ABI_NAMESPACE(_V2) |
7f78718b JW |
1293 | |
1294 | #if __cplusplus >= 202002L | |
1295 | /// @addtogroup chrono | |
1296 | /// @{ | |
1297 | template<typename _Duration> | |
1298 | using sys_time = time_point<system_clock, _Duration>; | |
1299 | using sys_seconds = sys_time<seconds>; | |
1300 | using sys_days = sys_time<days>; | |
1301 | ||
1302 | using file_clock = ::std::filesystem::__file_clock; | |
1303 | ||
1304 | template<typename _Duration> | |
1305 | using file_time = time_point<file_clock, _Duration>; | |
1306 | ||
1307 | template<> struct is_clock<system_clock> : true_type { }; | |
1308 | template<> struct is_clock<steady_clock> : true_type { }; | |
1309 | template<> struct is_clock<file_clock> : true_type { }; | |
1310 | ||
1311 | template<> inline constexpr bool is_clock_v<system_clock> = true; | |
1312 | template<> inline constexpr bool is_clock_v<steady_clock> = true; | |
1313 | template<> inline constexpr bool is_clock_v<file_clock> = true; | |
1314 | /// @} | |
1315 | #endif // C++20 | |
1dde83f0 JW |
1316 | #elif __cplusplus >= 202002L |
1317 | // Define a fake clock like chrono::local_t so that sys_time etc. | |
1318 | // can be used for freestanding. | |
1319 | struct __sys_t; | |
1320 | template<typename _Duration> | |
1321 | using sys_time = time_point<__sys_t, _Duration>; | |
1322 | using sys_seconds = sys_time<seconds>; | |
1323 | using sys_days = sys_time<days>; | |
1324 | #endif // _GLIBCXX_HOSTED | |
7f78718b JW |
1325 | } // namespace chrono |
1326 | ||
1dde83f0 | 1327 | #ifdef __glibcxx_chrono_udls // C++ >= 14 |
7f78718b JW |
1328 | inline namespace literals |
1329 | { | |
1330 | /** ISO C++ 2014 namespace for suffixes for duration literals. | |
1331 | * | |
1332 | * These suffixes can be used to create `chrono::duration` values with | |
1333 | * tick periods of hours, minutes, seconds, milliseconds, microseconds | |
1334 | * or nanoseconds. For example, `std::chrono::seconds(5)` can be written | |
1335 | * as `5s` after making the suffix visible in the current scope. | |
1336 | * The suffixes can be made visible by a using-directive or | |
1337 | * using-declaration such as: | |
1338 | * - `using namespace std::chrono_literals;` | |
1339 | * - `using namespace std::literals;` | |
1340 | * - `using namespace std::chrono;` | |
1341 | * - `using namespace std;` | |
1342 | * - `using std::chrono_literals::operator""s;` | |
1343 | * | |
1344 | * The result of these suffixes on an integer literal is one of the | |
1345 | * standard typedefs such as `std::chrono::hours`. | |
1346 | * The result on a floating-point literal is a duration type with the | |
1347 | * specified tick period and an unspecified floating-point representation, | |
1348 | * for example `1.5e2ms` might be equivalent to | |
1349 | * `chrono::duration<long double, chrono::milli>(1.5e2)`. | |
1350 | * | |
1351 | * @since C+14 | |
1352 | * @ingroup chrono | |
1353 | */ | |
1354 | inline namespace chrono_literals | |
1355 | { | |
1356 | /// @addtogroup chrono | |
1357 | /// @{ | |
1358 | ||
1359 | #pragma GCC diagnostic push | |
1360 | #pragma GCC diagnostic ignored "-Wliteral-suffix" | |
1361 | /// @cond undocumented | |
1362 | template<typename _Dur, char... _Digits> | |
1363 | constexpr _Dur __check_overflow() | |
1364 | { | |
1365 | using _Val = __parse_int::_Parse_int<_Digits...>; | |
1366 | constexpr typename _Dur::rep __repval = _Val::value; | |
1367 | static_assert(__repval >= 0 && __repval == _Val::value, | |
1368 | "literal value cannot be represented by duration type"); | |
1369 | return _Dur(__repval); | |
1370 | } | |
1371 | /// @endcond | |
1372 | ||
1373 | /// Literal suffix for durations representing non-integer hours | |
1374 | constexpr chrono::duration<long double, ratio<3600,1>> | |
1375 | operator""h(long double __hours) | |
1376 | { return chrono::duration<long double, ratio<3600,1>>{__hours}; } | |
1377 | ||
1378 | /// Literal suffix for durations of type `std::chrono::hours` | |
1379 | template <char... _Digits> | |
1380 | constexpr chrono::hours | |
1381 | operator""h() | |
1382 | { return __check_overflow<chrono::hours, _Digits...>(); } | |
1383 | ||
1384 | /// Literal suffix for durations representing non-integer minutes | |
1385 | constexpr chrono::duration<long double, ratio<60,1>> | |
1386 | operator""min(long double __mins) | |
1387 | { return chrono::duration<long double, ratio<60,1>>{__mins}; } | |
1388 | ||
1389 | /// Literal suffix for durations of type `std::chrono::minutes` | |
1390 | template <char... _Digits> | |
1391 | constexpr chrono::minutes | |
1392 | operator""min() | |
1393 | { return __check_overflow<chrono::minutes, _Digits...>(); } | |
1394 | ||
1395 | /// Literal suffix for durations representing non-integer seconds | |
1396 | constexpr chrono::duration<long double> | |
1397 | operator""s(long double __secs) | |
1398 | { return chrono::duration<long double>{__secs}; } | |
1399 | ||
1400 | /// Literal suffix for durations of type `std::chrono::seconds` | |
1401 | template <char... _Digits> | |
1402 | constexpr chrono::seconds | |
1403 | operator""s() | |
1404 | { return __check_overflow<chrono::seconds, _Digits...>(); } | |
1405 | ||
1406 | /// Literal suffix for durations representing non-integer milliseconds | |
1407 | constexpr chrono::duration<long double, milli> | |
1408 | operator""ms(long double __msecs) | |
1409 | { return chrono::duration<long double, milli>{__msecs}; } | |
1410 | ||
1411 | /// Literal suffix for durations of type `std::chrono::milliseconds` | |
1412 | template <char... _Digits> | |
1413 | constexpr chrono::milliseconds | |
1414 | operator""ms() | |
1415 | { return __check_overflow<chrono::milliseconds, _Digits...>(); } | |
1416 | ||
1417 | /// Literal suffix for durations representing non-integer microseconds | |
1418 | constexpr chrono::duration<long double, micro> | |
1419 | operator""us(long double __usecs) | |
1420 | { return chrono::duration<long double, micro>{__usecs}; } | |
1421 | ||
1422 | /// Literal suffix for durations of type `std::chrono::microseconds` | |
1423 | template <char... _Digits> | |
1424 | constexpr chrono::microseconds | |
1425 | operator""us() | |
1426 | { return __check_overflow<chrono::microseconds, _Digits...>(); } | |
1427 | ||
1428 | /// Literal suffix for durations representing non-integer nanoseconds | |
1429 | constexpr chrono::duration<long double, nano> | |
1430 | operator""ns(long double __nsecs) | |
1431 | { return chrono::duration<long double, nano>{__nsecs}; } | |
1432 | ||
1433 | /// Literal suffix for durations of type `std::chrono::nanoseconds` | |
1434 | template <char... _Digits> | |
1435 | constexpr chrono::nanoseconds | |
1436 | operator""ns() | |
1437 | { return __check_overflow<chrono::nanoseconds, _Digits...>(); } | |
1438 | ||
1439 | #pragma GCC diagnostic pop | |
1440 | /// @} | |
1441 | } // inline namespace chrono_literals | |
1442 | } // inline namespace literals | |
1443 | ||
1444 | namespace chrono | |
1445 | { | |
1446 | using namespace literals::chrono_literals; | |
1447 | } // namespace chrono | |
7ffa63df | 1448 | #endif // __glibcxx_chrono_udls |
7f78718b | 1449 | |
1dde83f0 | 1450 | #if __cplusplus >= 201703L && _GLIBCXX_HOSTED |
7f78718b JW |
1451 | namespace filesystem |
1452 | { | |
1453 | struct __file_clock | |
1454 | { | |
1455 | using duration = chrono::nanoseconds; | |
1456 | using rep = duration::rep; | |
1457 | using period = duration::period; | |
1458 | using time_point = chrono::time_point<__file_clock>; | |
1459 | static constexpr bool is_steady = false; | |
1460 | ||
1461 | static time_point | |
1462 | now() noexcept | |
1463 | { return _S_from_sys(chrono::system_clock::now()); } | |
1464 | ||
1465 | #if __cplusplus > 201703L | |
1466 | template<typename _Dur> | |
1467 | static | |
fba15da8 | 1468 | chrono::file_time<common_type_t<_Dur, chrono::seconds>> |
7f78718b JW |
1469 | from_sys(const chrono::sys_time<_Dur>& __t) noexcept |
1470 | { return _S_from_sys(__t); } | |
1471 | ||
1472 | // For internal use only | |
1473 | template<typename _Dur> | |
1474 | static | |
fba15da8 | 1475 | chrono::sys_time<common_type_t<_Dur, chrono::seconds>> |
7f78718b JW |
1476 | to_sys(const chrono::file_time<_Dur>& __t) noexcept |
1477 | { return _S_to_sys(__t); } | |
1478 | #endif // C++20 | |
1479 | ||
1480 | private: | |
1481 | using __sys_clock = chrono::system_clock; | |
1482 | ||
1483 | // This clock's (unspecified) epoch is 2174-01-01 00:00:00 UTC. | |
1484 | // A signed 64-bit duration with nanosecond resolution gives roughly | |
1485 | // +/- 292 years, which covers the 1901-2446 date range for ext4. | |
1486 | static constexpr chrono::seconds _S_epoch_diff{6437664000}; | |
1487 | ||
1488 | protected: | |
1489 | // For internal use only | |
1490 | template<typename _Dur> | |
1491 | static | |
fba15da8 | 1492 | chrono::time_point<__file_clock, common_type_t<_Dur, chrono::seconds>> |
7f78718b JW |
1493 | _S_from_sys(const chrono::time_point<__sys_clock, _Dur>& __t) noexcept |
1494 | { | |
fba15da8 JW |
1495 | using _CDur = common_type_t<_Dur, chrono::seconds>; |
1496 | using __file_time = chrono::time_point<__file_clock, _CDur>; | |
7f78718b JW |
1497 | return __file_time{__t.time_since_epoch()} - _S_epoch_diff; |
1498 | } | |
1499 | ||
1500 | // For internal use only | |
1501 | template<typename _Dur> | |
1502 | static | |
fba15da8 | 1503 | chrono::time_point<__sys_clock, common_type_t<_Dur, chrono::seconds>> |
7f78718b JW |
1504 | _S_to_sys(const chrono::time_point<__file_clock, _Dur>& __t) noexcept |
1505 | { | |
fba15da8 JW |
1506 | using _CDur = common_type_t<_Dur, chrono::seconds>; |
1507 | using __sys_time = chrono::time_point<__sys_clock, _CDur>; | |
7f78718b JW |
1508 | return __sys_time{__t.time_since_epoch()} + _S_epoch_diff; |
1509 | } | |
1510 | }; | |
1511 | } // namespace filesystem | |
1dde83f0 | 1512 | #endif // C++17 && HOSTED |
7f78718b JW |
1513 | |
1514 | _GLIBCXX_END_NAMESPACE_VERSION | |
1515 | } // namespace std | |
1516 | ||
1517 | #endif // C++11 | |
1518 | ||
1519 | #endif //_GLIBCXX_CHRONO_H |