Line data Source code
1 : // chrono::duration and chrono::time_point -*- C++ -*-
2 :
3 : // Copyright (C) 2008-2023 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 : namespace std _GLIBCXX_VISIBILITY(default)
48 : {
49 : _GLIBCXX_BEGIN_NAMESPACE_VERSION
50 :
51 : #if __cplusplus >= 201703L
52 : namespace filesystem { struct __file_clock; };
53 : #endif
54 :
55 : namespace chrono
56 : {
57 : /// @addtogroup chrono
58 : /// @{
59 :
60 : /// `chrono::duration` represents a distance between two points in time
61 : template<typename _Rep, typename _Period = ratio<1>>
62 : class duration;
63 :
64 : /// `chrono::time_point` represents a point in time as measured by a clock
65 : template<typename _Clock, typename _Dur = typename _Clock::duration>
66 : class time_point;
67 : /// @}
68 : }
69 :
70 : /// @addtogroup chrono
71 : /// @{
72 :
73 : // 20.11.4.3 specialization of common_type (for duration, sfinae-friendly)
74 :
75 : /// @cond undocumented
76 :
77 : template<typename _CT, typename _Period1, typename _Period2, typename = void>
78 : struct __duration_common_type
79 : { };
80 :
81 : template<typename _CT, typename _Period1, typename _Period2>
82 : struct __duration_common_type<_CT, _Period1, _Period2,
83 : __void_t<typename _CT::type>>
84 : {
85 : private:
86 : using __gcd_num = __static_gcd<_Period1::num, _Period2::num>;
87 : using __gcd_den = __static_gcd<_Period1::den, _Period2::den>;
88 : using __cr = typename _CT::type;
89 : using __r = ratio<__gcd_num::value,
90 : (_Period1::den / __gcd_den::value) * _Period2::den>;
91 :
92 : public:
93 : using type = chrono::duration<__cr, typename __r::type>;
94 : };
95 :
96 : /// @endcond
97 :
98 : /// @{
99 : /// @relates chrono::duration
100 :
101 : /// Specialization of common_type for chrono::duration types.
102 : template<typename _Rep1, typename _Period1, typename _Rep2, typename _Period2>
103 : struct common_type<chrono::duration<_Rep1, _Period1>,
104 : chrono::duration<_Rep2, _Period2>>
105 : : __duration_common_type<common_type<_Rep1, _Rep2>,
106 : typename _Period1::type,
107 : typename _Period2::type>
108 : { };
109 :
110 : /// Specialization of common_type for two identical chrono::duration types.
111 : template<typename _Rep, typename _Period>
112 : struct common_type<chrono::duration<_Rep, _Period>,
113 : chrono::duration<_Rep, _Period>>
114 : {
115 : using type = chrono::duration<typename common_type<_Rep>::type,
116 : typename _Period::type>;
117 : };
118 :
119 : /// Specialization of common_type for one chrono::duration type.
120 : template<typename _Rep, typename _Period>
121 : struct common_type<chrono::duration<_Rep, _Period>>
122 : {
123 : using type = chrono::duration<typename common_type<_Rep>::type,
124 : typename _Period::type>;
125 : };
126 : /// @}
127 :
128 : // 20.11.4.3 specialization of common_type (for time_point, sfinae-friendly)
129 :
130 : /// @cond undocumented
131 :
132 : template<typename _CT, typename _Clock, typename = void>
133 : struct __timepoint_common_type
134 : { };
135 :
136 : template<typename _CT, typename _Clock>
137 : struct __timepoint_common_type<_CT, _Clock, __void_t<typename _CT::type>>
138 : {
139 : using type = chrono::time_point<_Clock, typename _CT::type>;
140 : };
141 :
142 : /// @endcond
143 :
144 : /// @{
145 : /// @relates chrono::time_point
146 :
147 : /// Specialization of common_type for chrono::time_point types.
148 : template<typename _Clock, typename _Duration1, typename _Duration2>
149 : struct common_type<chrono::time_point<_Clock, _Duration1>,
150 : chrono::time_point<_Clock, _Duration2>>
151 : : __timepoint_common_type<common_type<_Duration1, _Duration2>, _Clock>
152 : { };
153 :
154 : /// Specialization of common_type for two identical chrono::time_point types.
155 : template<typename _Clock, typename _Duration>
156 : struct common_type<chrono::time_point<_Clock, _Duration>,
157 : chrono::time_point<_Clock, _Duration>>
158 : { using type = chrono::time_point<_Clock, _Duration>; };
159 :
160 : /// Specialization of common_type for one chrono::time_point type.
161 : template<typename _Clock, typename _Duration>
162 : struct common_type<chrono::time_point<_Clock, _Duration>>
163 : { using type = chrono::time_point<_Clock, _Duration>; };
164 : /// @}
165 :
166 : /// @} group chrono
167 :
168 : namespace chrono
169 : {
170 : /// @addtogroup chrono
171 : /// @{
172 :
173 : /// @cond undocumented
174 :
175 : // Primary template for duration_cast impl.
176 : template<typename _ToDur, typename _CF, typename _CR,
177 : bool _NumIsOne = false, bool _DenIsOne = false>
178 : struct __duration_cast_impl
179 : {
180 : template<typename _Rep, typename _Period>
181 : static constexpr _ToDur
182 : __cast(const duration<_Rep, _Period>& __d)
183 : {
184 : typedef typename _ToDur::rep __to_rep;
185 : return _ToDur(static_cast<__to_rep>(static_cast<_CR>(__d.count())
186 : * static_cast<_CR>(_CF::num)
187 : / static_cast<_CR>(_CF::den)));
188 : }
189 : };
190 :
191 : template<typename _ToDur, typename _CF, typename _CR>
192 : struct __duration_cast_impl<_ToDur, _CF, _CR, true, true>
193 : {
194 : template<typename _Rep, typename _Period>
195 : static constexpr _ToDur
196 : __cast(const duration<_Rep, _Period>& __d)
197 : {
198 : typedef typename _ToDur::rep __to_rep;
199 : return _ToDur(static_cast<__to_rep>(__d.count()));
200 : }
201 : };
202 :
203 : template<typename _ToDur, typename _CF, typename _CR>
204 : struct __duration_cast_impl<_ToDur, _CF, _CR, true, false>
205 : {
206 : template<typename _Rep, typename _Period>
207 : static constexpr _ToDur
208 3564 : __cast(const duration<_Rep, _Period>& __d)
209 : {
210 : typedef typename _ToDur::rep __to_rep;
211 7128 : return _ToDur(static_cast<__to_rep>(
212 3564 : static_cast<_CR>(__d.count()) / static_cast<_CR>(_CF::den)));
213 : }
214 : };
215 :
216 : template<typename _ToDur, typename _CF, typename _CR>
217 : struct __duration_cast_impl<_ToDur, _CF, _CR, false, true>
218 : {
219 : template<typename _Rep, typename _Period>
220 : static constexpr _ToDur
221 0 : __cast(const duration<_Rep, _Period>& __d)
222 : {
223 : typedef typename _ToDur::rep __to_rep;
224 0 : return _ToDur(static_cast<__to_rep>(
225 0 : static_cast<_CR>(__d.count()) * static_cast<_CR>(_CF::num)));
226 : }
227 : };
228 :
229 : template<typename _Tp>
230 : struct __is_duration
231 : : std::false_type
232 : { };
233 :
234 : template<typename _Rep, typename _Period>
235 : struct __is_duration<duration<_Rep, _Period>>
236 : : std::true_type
237 : { };
238 :
239 : template<typename _Tp>
240 : using __enable_if_is_duration
241 : = typename enable_if<__is_duration<_Tp>::value, _Tp>::type;
242 :
243 : template<typename _Tp>
244 : using __disable_if_is_duration
245 : = typename enable_if<!__is_duration<_Tp>::value, _Tp>::type;
246 :
247 : #if __cplusplus >= 201703L
248 : template<typename _Tp>
249 : inline constexpr bool __is_duration_v = false;
250 : template<typename _Rep, typename _Period>
251 : inline constexpr bool __is_duration_v<duration<_Rep, _Period>> = true;
252 : template<typename _Tp>
253 : inline constexpr bool __is_time_point_v = false;
254 : template<typename _Clock, typename _Dur>
255 : inline constexpr bool __is_time_point_v<time_point<_Clock, _Dur>> = true;
256 : #endif
257 :
258 : /// @endcond
259 :
260 : /** Convert a `duration` to type `ToDur`.
261 : *
262 : * If the duration cannot be represented accurately in the result type,
263 : * returns the result of integer truncation (i.e., rounded towards zero).
264 : *
265 : * @tparam _ToDur The result type must be a `duration`.
266 : * @param __d A duration.
267 : * @return The value of `__d` converted to type `_ToDur`.
268 : * @since C++11
269 : */
270 : template<typename _ToDur, typename _Rep, typename _Period>
271 : _GLIBCXX_NODISCARD
272 : constexpr __enable_if_is_duration<_ToDur>
273 5900 : duration_cast(const duration<_Rep, _Period>& __d)
274 : {
275 : #if __cpp_inline_variables && __cpp_if_constexpr
276 : if constexpr (is_same_v<_ToDur, duration<_Rep, _Period>>)
277 2336 : return __d;
278 : else
279 : {
280 : #endif
281 : using __to_period = typename _ToDur::period;
282 : using __to_rep = typename _ToDur::rep;
283 : using __cf = ratio_divide<_Period, __to_period>;
284 : using __cr = typename common_type<__to_rep, _Rep, intmax_t>::type;
285 : using __dc = __duration_cast_impl<_ToDur, __cf, __cr,
286 : __cf::num == 1, __cf::den == 1>;
287 3564 : return __dc::__cast(__d);
288 : #if __cpp_inline_variables && __cpp_if_constexpr
289 : }
290 : #endif
291 : }
292 :
293 : /** Trait indicating whether to treat a type as a floating-point type.
294 : *
295 : * The chrono library uses this trait to tell whether a `duration` can
296 : * represent fractional values of the given precision, or only integral
297 : * values.
298 : *
299 : * You should specialize this trait for your own numeric types that are
300 : * used with `duration` and can represent non-integral values.
301 : *
302 : * @since C++11
303 : */
304 : template<typename _Rep>
305 : struct treat_as_floating_point
306 : : is_floating_point<_Rep>
307 : { };
308 :
309 : #if __cplusplus > 201402L
310 : template <typename _Rep>
311 : inline constexpr bool treat_as_floating_point_v =
312 : treat_as_floating_point<_Rep>::value;
313 :
314 : template<>
315 : inline constexpr bool treat_as_floating_point_v<int> = false;
316 : template<>
317 : inline constexpr bool treat_as_floating_point_v<long> = false;
318 : template<>
319 : inline constexpr bool treat_as_floating_point_v<long long> = false;
320 : template<>
321 : inline constexpr bool treat_as_floating_point_v<float> = true;
322 : template<>
323 : inline constexpr bool treat_as_floating_point_v<double> = true;
324 : template<>
325 : inline constexpr bool treat_as_floating_point_v<long double> = true;
326 : #endif // C++17
327 :
328 : #if __cplusplus > 201703L
329 : #if __cpp_lib_concepts
330 : template<typename _Tp>
331 : inline constexpr bool is_clock_v = false;
332 :
333 : template<typename _Tp>
334 : requires requires {
335 : typename _Tp::rep;
336 : typename _Tp::period;
337 : typename _Tp::duration;
338 : typename _Tp::time_point::clock;
339 : typename _Tp::time_point::duration;
340 : { &_Tp::is_steady } -> same_as<const bool*>;
341 : { _Tp::now() } -> same_as<typename _Tp::time_point>;
342 : requires same_as<typename _Tp::duration,
343 : duration<typename _Tp::rep, typename _Tp::period>>;
344 : requires same_as<typename _Tp::time_point::duration,
345 : typename _Tp::duration>;
346 : }
347 : inline constexpr bool is_clock_v<_Tp> = true;
348 : #else
349 : template<typename _Tp, typename = void>
350 : inline constexpr bool is_clock_v = false;
351 :
352 : template<typename _Tp>
353 : inline constexpr bool
354 : is_clock_v<_Tp, void_t<typename _Tp::rep, typename _Tp::period,
355 : typename _Tp::duration,
356 : typename _Tp::time_point::duration,
357 : decltype(_Tp::is_steady),
358 : decltype(_Tp::now())>>
359 : = __and_v<is_same<typename _Tp::duration,
360 : duration<typename _Tp::rep, typename _Tp::period>>,
361 : is_same<typename _Tp::time_point::duration,
362 : typename _Tp::duration>,
363 : is_same<decltype(&_Tp::is_steady), const bool*>,
364 : is_same<decltype(_Tp::now()), typename _Tp::time_point>>;
365 : #endif
366 :
367 : template<typename _Tp>
368 : struct is_clock
369 : : bool_constant<is_clock_v<_Tp>>
370 : { };
371 : #endif // C++20
372 :
373 : #if __cplusplus >= 201703L
374 : # define __cpp_lib_chrono 201611L
375 :
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 // ! C++17
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 // C++17
492 :
493 : /// duration_values
494 : template<typename _Rep>
495 : struct duration_values
496 : {
497 : static constexpr _Rep
498 0 : zero() noexcept
499 0 : { 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(_R1::num, _R2::num),
549 : intmax_t __gcd2 = _S_gcd(_R1::den, _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 5346 : constexpr explicit duration(const _Rep2& __rep)
574 5346 : : __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 0 : constexpr duration(const duration<_Rep2, _Period2>& __d)
582 0 : : __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 13028 : count() const
590 13028 : { 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 0 : zero() noexcept
674 0 : { 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 1782 : 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 1782 : 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 0 : 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 0 : 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 0 : operator<=(const duration<_Rep1, _Period1>& __lhs,
856 : const duration<_Rep2, _Period2>& __rhs)
857 0 : { 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_TR1
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 7682 : time_since_epoch() const
950 7682 : { 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 1782 : operator-(const time_point<_Clock, _Dur1>& __lhs,
1142 : const time_point<_Clock, _Dur2>& __rhs)
1143 1782 : { 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 : (__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 : (__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 : #if __cplusplus >= 201402L
1316 : #define __cpp_lib_chrono_udls 201304L
1317 :
1318 : inline namespace literals
1319 : {
1320 : /** ISO C++ 2014 namespace for suffixes for duration literals.
1321 : *
1322 : * These suffixes can be used to create `chrono::duration` values with
1323 : * tick periods of hours, minutes, seconds, milliseconds, microseconds
1324 : * or nanoseconds. For example, `std::chrono::seconds(5)` can be written
1325 : * as `5s` after making the suffix visible in the current scope.
1326 : * The suffixes can be made visible by a using-directive or
1327 : * using-declaration such as:
1328 : * - `using namespace std::chrono_literals;`
1329 : * - `using namespace std::literals;`
1330 : * - `using namespace std::chrono;`
1331 : * - `using namespace std;`
1332 : * - `using std::chrono_literals::operator""s;`
1333 : *
1334 : * The result of these suffixes on an integer literal is one of the
1335 : * standard typedefs such as `std::chrono::hours`.
1336 : * The result on a floating-point literal is a duration type with the
1337 : * specified tick period and an unspecified floating-point representation,
1338 : * for example `1.5e2ms` might be equivalent to
1339 : * `chrono::duration<long double, chrono::milli>(1.5e2)`.
1340 : *
1341 : * @since C+14
1342 : * @ingroup chrono
1343 : */
1344 : inline namespace chrono_literals
1345 : {
1346 : /// @addtogroup chrono
1347 : /// @{
1348 :
1349 : #pragma GCC diagnostic push
1350 : #pragma GCC diagnostic ignored "-Wliteral-suffix"
1351 : /// @cond undocumented
1352 : template<typename _Dur, char... _Digits>
1353 : constexpr _Dur __check_overflow()
1354 : {
1355 : using _Val = __parse_int::_Parse_int<_Digits...>;
1356 : constexpr typename _Dur::rep __repval = _Val::value;
1357 : static_assert(__repval >= 0 && __repval == _Val::value,
1358 : "literal value cannot be represented by duration type");
1359 : return _Dur(__repval);
1360 : }
1361 : /// @endcond
1362 :
1363 : /// Literal suffix for durations representing non-integer hours
1364 : constexpr chrono::duration<long double, ratio<3600,1>>
1365 : operator""h(long double __hours)
1366 : { return chrono::duration<long double, ratio<3600,1>>{__hours}; }
1367 :
1368 : /// Literal suffix for durations of type `std::chrono::hours`
1369 : template <char... _Digits>
1370 : constexpr chrono::hours
1371 : operator""h()
1372 : { return __check_overflow<chrono::hours, _Digits...>(); }
1373 :
1374 : /// Literal suffix for durations representing non-integer minutes
1375 : constexpr chrono::duration<long double, ratio<60,1>>
1376 : operator""min(long double __mins)
1377 : { return chrono::duration<long double, ratio<60,1>>{__mins}; }
1378 :
1379 : /// Literal suffix for durations of type `std::chrono::minutes`
1380 : template <char... _Digits>
1381 : constexpr chrono::minutes
1382 : operator""min()
1383 : { return __check_overflow<chrono::minutes, _Digits...>(); }
1384 :
1385 : /// Literal suffix for durations representing non-integer seconds
1386 : constexpr chrono::duration<long double>
1387 : operator""s(long double __secs)
1388 : { return chrono::duration<long double>{__secs}; }
1389 :
1390 : /// Literal suffix for durations of type `std::chrono::seconds`
1391 : template <char... _Digits>
1392 : constexpr chrono::seconds
1393 : operator""s()
1394 : { return __check_overflow<chrono::seconds, _Digits...>(); }
1395 :
1396 : /// Literal suffix for durations representing non-integer milliseconds
1397 : constexpr chrono::duration<long double, milli>
1398 : operator""ms(long double __msecs)
1399 : { return chrono::duration<long double, milli>{__msecs}; }
1400 :
1401 : /// Literal suffix for durations of type `std::chrono::milliseconds`
1402 : template <char... _Digits>
1403 : constexpr chrono::milliseconds
1404 : operator""ms()
1405 : { return __check_overflow<chrono::milliseconds, _Digits...>(); }
1406 :
1407 : /// Literal suffix for durations representing non-integer microseconds
1408 : constexpr chrono::duration<long double, micro>
1409 : operator""us(long double __usecs)
1410 : { return chrono::duration<long double, micro>{__usecs}; }
1411 :
1412 : /// Literal suffix for durations of type `std::chrono::microseconds`
1413 : template <char... _Digits>
1414 : constexpr chrono::microseconds
1415 : operator""us()
1416 : { return __check_overflow<chrono::microseconds, _Digits...>(); }
1417 :
1418 : /// Literal suffix for durations representing non-integer nanoseconds
1419 : constexpr chrono::duration<long double, nano>
1420 : operator""ns(long double __nsecs)
1421 : { return chrono::duration<long double, nano>{__nsecs}; }
1422 :
1423 : /// Literal suffix for durations of type `std::chrono::nanoseconds`
1424 : template <char... _Digits>
1425 : constexpr chrono::nanoseconds
1426 : operator""ns()
1427 : { return __check_overflow<chrono::nanoseconds, _Digits...>(); }
1428 :
1429 : #pragma GCC diagnostic pop
1430 : /// @}
1431 : } // inline namespace chrono_literals
1432 : } // inline namespace literals
1433 :
1434 : namespace chrono
1435 : {
1436 : using namespace literals::chrono_literals;
1437 : } // namespace chrono
1438 : #endif // C++14
1439 :
1440 : #if __cplusplus >= 201703L
1441 : namespace filesystem
1442 : {
1443 : struct __file_clock
1444 : {
1445 : using duration = chrono::nanoseconds;
1446 : using rep = duration::rep;
1447 : using period = duration::period;
1448 : using time_point = chrono::time_point<__file_clock>;
1449 : static constexpr bool is_steady = false;
1450 :
1451 : static time_point
1452 : now() noexcept
1453 : { return _S_from_sys(chrono::system_clock::now()); }
1454 :
1455 : #if __cplusplus > 201703L
1456 : template<typename _Dur>
1457 : static
1458 : chrono::file_time<common_type_t<_Dur, chrono::seconds>>
1459 : from_sys(const chrono::sys_time<_Dur>& __t) noexcept
1460 : { return _S_from_sys(__t); }
1461 :
1462 : // For internal use only
1463 : template<typename _Dur>
1464 : static
1465 : chrono::sys_time<common_type_t<_Dur, chrono::seconds>>
1466 : to_sys(const chrono::file_time<_Dur>& __t) noexcept
1467 : { return _S_to_sys(__t); }
1468 : #endif // C++20
1469 :
1470 : private:
1471 : using __sys_clock = chrono::system_clock;
1472 :
1473 : // This clock's (unspecified) epoch is 2174-01-01 00:00:00 UTC.
1474 : // A signed 64-bit duration with nanosecond resolution gives roughly
1475 : // +/- 292 years, which covers the 1901-2446 date range for ext4.
1476 : static constexpr chrono::seconds _S_epoch_diff{6437664000};
1477 :
1478 : protected:
1479 : // For internal use only
1480 : template<typename _Dur>
1481 : static
1482 : chrono::time_point<__file_clock, common_type_t<_Dur, chrono::seconds>>
1483 : _S_from_sys(const chrono::time_point<__sys_clock, _Dur>& __t) noexcept
1484 : {
1485 : using _CDur = common_type_t<_Dur, chrono::seconds>;
1486 : using __file_time = chrono::time_point<__file_clock, _CDur>;
1487 : return __file_time{__t.time_since_epoch()} - _S_epoch_diff;
1488 : }
1489 :
1490 : // For internal use only
1491 : template<typename _Dur>
1492 : static
1493 : chrono::time_point<__sys_clock, common_type_t<_Dur, chrono::seconds>>
1494 : _S_to_sys(const chrono::time_point<__file_clock, _Dur>& __t) noexcept
1495 : {
1496 : using _CDur = common_type_t<_Dur, chrono::seconds>;
1497 : using __sys_time = chrono::time_point<__sys_clock, _CDur>;
1498 : return __sys_time{__t.time_since_epoch()} + _S_epoch_diff;
1499 : }
1500 : };
1501 : } // namespace filesystem
1502 : #endif // C++17
1503 :
1504 : _GLIBCXX_END_NAMESPACE_VERSION
1505 : } // namespace std
1506 :
1507 : #endif // C++11
1508 :
1509 : #endif //_GLIBCXX_CHRONO_H
|