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