libstdc++
chrono
Go to the documentation of this file.
1// <chrono> -*- C++ -*-
2
3// Copyright (C) 2008-2021 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/chrono
26 * This is a Standard C++ Library header.
27 * @ingroup chrono
28 */
29
30#ifndef _GLIBCXX_CHRONO
31#define _GLIBCXX_CHRONO 1
32
33#pragma GCC system_header
34
35#if __cplusplus < 201103L
36# include <bits/c++0x_warning.h>
37#else
38
39#include <ratio>
40#include <type_traits>
41#include <limits>
42#include <ctime>
43#include <bits/parse_numbers.h> // for literals support.
44#if __cplusplus > 201703L
45# include <concepts>
46# include <compare>
47#endif
48
49namespace 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 /**
58 * @defgroup chrono Time
59 * @ingroup utilities
60 *
61 * Classes and functions for time.
62 *
63 * @since C++11
64 */
65
66 /** @namespace std::chrono
67 * @brief ISO C++ 2011 namespace for date and time utilities
68 * @ingroup chrono
69 */
70 namespace chrono
71 {
72 /// @addtogroup chrono
73 /// @{
74
75 /// `chrono::duration` represents a distance between two points in time
76 template<typename _Rep, typename _Period = ratio<1>>
77 struct duration;
78
79 /// `chrono::time_point` represents a point in time as measured by a clock
80 template<typename _Clock, typename _Dur = typename _Clock::duration>
81 struct time_point;
82 /// @}
83 }
84
85 /// @addtogroup chrono
86 /// @{
87
88 // 20.11.4.3 specialization of common_type (for duration, sfinae-friendly)
89
90 /// @cond undocumented
91
92 template<typename _CT, typename _Period1, typename _Period2, typename = void>
93 struct __duration_common_type
94 { };
95
96 template<typename _CT, typename _Period1, typename _Period2>
97 struct __duration_common_type<_CT, _Period1, _Period2,
98 __void_t<typename _CT::type>>
99 {
100 private:
101 using __gcd_num = __static_gcd<_Period1::num, _Period2::num>;
102 using __gcd_den = __static_gcd<_Period1::den, _Period2::den>;
103 using __cr = typename _CT::type;
104 using __r = ratio<__gcd_num::value,
105 (_Period1::den / __gcd_den::value) * _Period2::den>;
106
107 public:
108 using type = chrono::duration<__cr, typename __r::type>;
109 };
110
111 /// @endcond
112
113 /// @{
114 /// @relates chrono::duration
115
116 /// Specialization of common_type for chrono::duration types.
117 template<typename _Rep1, typename _Period1, typename _Rep2, typename _Period2>
118 struct common_type<chrono::duration<_Rep1, _Period1>,
119 chrono::duration<_Rep2, _Period2>>
120 : __duration_common_type<common_type<_Rep1, _Rep2>,
121 typename _Period1::type,
122 typename _Period2::type>
123 { };
124
125 /// Specialization of common_type for two identical chrono::duration types.
126 template<typename _Rep, typename _Period>
127 struct common_type<chrono::duration<_Rep, _Period>,
128 chrono::duration<_Rep, _Period>>
129 {
130 using type = chrono::duration<typename common_type<_Rep>::type,
131 typename _Period::type>;
132 };
133
134 /// Specialization of common_type for one chrono::duration type.
135 template<typename _Rep, typename _Period>
136 struct common_type<chrono::duration<_Rep, _Period>>
137 {
138 using type = chrono::duration<typename common_type<_Rep>::type,
139 typename _Period::type>;
140 };
141 /// @}
142
143 // 20.11.4.3 specialization of common_type (for time_point, sfinae-friendly)
144
145 /// @cond undocumented
146
147 template<typename _CT, typename _Clock, typename = void>
148 struct __timepoint_common_type
149 { };
150
151 template<typename _CT, typename _Clock>
152 struct __timepoint_common_type<_CT, _Clock, __void_t<typename _CT::type>>
153 {
154 using type = chrono::time_point<_Clock, typename _CT::type>;
155 };
156
157 /// @endcond
158
159 /// @{
160 /// @relates chrono::time_point
161
162 /// Specialization of common_type for chrono::time_point types.
163 template<typename _Clock, typename _Duration1, typename _Duration2>
164 struct common_type<chrono::time_point<_Clock, _Duration1>,
165 chrono::time_point<_Clock, _Duration2>>
166 : __timepoint_common_type<common_type<_Duration1, _Duration2>, _Clock>
167 { };
168
169 /// Specialization of common_type for two identical chrono::time_point types.
170 template<typename _Clock, typename _Duration>
171 struct common_type<chrono::time_point<_Clock, _Duration>,
172 chrono::time_point<_Clock, _Duration>>
173 { using type = chrono::time_point<_Clock, _Duration>; };
174
175 /// Specialization of common_type for one chrono::time_point type.
176 template<typename _Clock, typename _Duration>
177 struct common_type<chrono::time_point<_Clock, _Duration>>
178 { using type = chrono::time_point<_Clock, _Duration>; };
179 /// @}
180
181 /// @} group chrono
182
183 namespace chrono
184 {
185 /// @addtogroup chrono
186 /// @{
187
188 /// @cond undocumented
189
190 // Primary template for duration_cast impl.
191 template<typename _ToDur, typename _CF, typename _CR,
192 bool _NumIsOne = false, bool _DenIsOne = false>
193 struct __duration_cast_impl
194 {
195 template<typename _Rep, typename _Period>
196 static constexpr _ToDur
197 __cast(const duration<_Rep, _Period>& __d)
198 {
199 typedef typename _ToDur::rep __to_rep;
200 return _ToDur(static_cast<__to_rep>(static_cast<_CR>(__d.count())
201 * static_cast<_CR>(_CF::num)
202 / static_cast<_CR>(_CF::den)));
203 }
204 };
205
206 template<typename _ToDur, typename _CF, typename _CR>
207 struct __duration_cast_impl<_ToDur, _CF, _CR, true, true>
208 {
209 template<typename _Rep, typename _Period>
210 static constexpr _ToDur
211 __cast(const duration<_Rep, _Period>& __d)
212 {
213 typedef typename _ToDur::rep __to_rep;
214 return _ToDur(static_cast<__to_rep>(__d.count()));
215 }
216 };
217
218 template<typename _ToDur, typename _CF, typename _CR>
219 struct __duration_cast_impl<_ToDur, _CF, _CR, true, false>
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::den)));
228 }
229 };
230
231 template<typename _ToDur, typename _CF, typename _CR>
232 struct __duration_cast_impl<_ToDur, _CF, _CR, false, true>
233 {
234 template<typename _Rep, typename _Period>
235 static constexpr _ToDur
236 __cast(const duration<_Rep, _Period>& __d)
237 {
238 typedef typename _ToDur::rep __to_rep;
239 return _ToDur(static_cast<__to_rep>(
240 static_cast<_CR>(__d.count()) * static_cast<_CR>(_CF::num)));
241 }
242 };
243
244 template<typename _Tp>
245 struct __is_duration
246 : std::false_type
247 { };
248
249 template<typename _Rep, typename _Period>
250 struct __is_duration<duration<_Rep, _Period>>
251 : std::true_type
252 { };
253
254 template<typename _Tp>
255 using __enable_if_is_duration
256 = typename enable_if<__is_duration<_Tp>::value, _Tp>::type;
257
258 template<typename _Tp>
259 using __disable_if_is_duration
260 = typename enable_if<!__is_duration<_Tp>::value, _Tp>::type;
261
262 /// @endcond
263
264 /// duration_cast
265 template<typename _ToDur, typename _Rep, typename _Period>
266 constexpr __enable_if_is_duration<_ToDur>
267 duration_cast(const duration<_Rep, _Period>& __d)
268 {
269 typedef typename _ToDur::period __to_period;
270 typedef typename _ToDur::rep __to_rep;
271 typedef ratio_divide<_Period, __to_period> __cf;
272 typedef typename common_type<__to_rep, _Rep, intmax_t>::type
273 __cr;
274 typedef __duration_cast_impl<_ToDur, __cf, __cr,
275 __cf::num == 1, __cf::den == 1> __dc;
276 return __dc::__cast(__d);
277 }
278
279 /// treat_as_floating_point
280 template<typename _Rep>
281 struct treat_as_floating_point
282 : is_floating_point<_Rep>
283 { };
284
285#if __cplusplus > 201402L
286 template <typename _Rep>
287 inline constexpr bool treat_as_floating_point_v =
288 treat_as_floating_point<_Rep>::value;
289#endif // C++17
290
291#if __cplusplus > 201703L
292 template<typename _Tp>
293 struct is_clock;
294
295 template<typename _Tp>
296 inline constexpr bool is_clock_v = is_clock<_Tp>::value;
297
298#if __cpp_lib_concepts
299 template<typename _Tp>
300 struct is_clock : false_type
301 { };
302
303 template<typename _Tp>
304 requires requires {
305 typename _Tp::rep;
306 typename _Tp::period;
307 typename _Tp::duration;
308 typename _Tp::time_point::clock;
309 typename _Tp::time_point::duration;
310 { &_Tp::is_steady } -> same_as<const bool*>;
311 { _Tp::now() } -> same_as<typename _Tp::time_point>;
312 requires same_as<typename _Tp::duration,
313 duration<typename _Tp::rep, typename _Tp::period>>;
314 requires same_as<typename _Tp::time_point::duration,
315 typename _Tp::duration>;
316 }
317 struct is_clock<_Tp> : true_type
318 { };
319#else
320 template<typename _Tp, typename = void>
321 struct __is_clock_impl : false_type
322 { };
323
324 template<typename _Tp>
325 struct __is_clock_impl<_Tp,
326 void_t<typename _Tp::rep, typename _Tp::period,
327 typename _Tp::duration,
328 typename _Tp::time_point::duration,
329 decltype(_Tp::is_steady),
330 decltype(_Tp::now())>>
331 : __and_<is_same<typename _Tp::duration,
332 duration<typename _Tp::rep, typename _Tp::period>>,
333 is_same<typename _Tp::time_point::duration,
334 typename _Tp::duration>,
335 is_same<decltype(&_Tp::is_steady), const bool*>,
336 is_same<decltype(_Tp::now()), typename _Tp::time_point>>::type
337 { };
338
339 template<typename _Tp>
340 struct is_clock : __is_clock_impl<_Tp>::type
341 { };
342#endif
343#endif // C++20
344
345#if __cplusplus >= 201703L
346# define __cpp_lib_chrono 201611
347
348 template<typename _ToDur, typename _Rep, typename _Period>
349 constexpr __enable_if_is_duration<_ToDur>
350 floor(const duration<_Rep, _Period>& __d)
351 {
352 auto __to = chrono::duration_cast<_ToDur>(__d);
353 if (__to > __d)
354 return __to - _ToDur{1};
355 return __to;
356 }
357
358 template<typename _ToDur, typename _Rep, typename _Period>
359 constexpr __enable_if_is_duration<_ToDur>
360 ceil(const duration<_Rep, _Period>& __d)
361 {
362 auto __to = chrono::duration_cast<_ToDur>(__d);
363 if (__to < __d)
364 return __to + _ToDur{1};
365 return __to;
366 }
367
368 template <typename _ToDur, typename _Rep, typename _Period>
369 constexpr enable_if_t<
370 __and_<__is_duration<_ToDur>,
371 __not_<treat_as_floating_point<typename _ToDur::rep>>>::value,
372 _ToDur>
373 round(const duration<_Rep, _Period>& __d)
374 {
375 _ToDur __t0 = chrono::floor<_ToDur>(__d);
376 _ToDur __t1 = __t0 + _ToDur{1};
377 auto __diff0 = __d - __t0;
378 auto __diff1 = __t1 - __d;
379 if (__diff0 == __diff1)
380 {
381 if (__t0.count() & 1)
382 return __t1;
383 return __t0;
384 }
385 else if (__diff0 < __diff1)
386 return __t0;
387 return __t1;
388 }
389
390 template<typename _Rep, typename _Period>
391 constexpr
392 enable_if_t<numeric_limits<_Rep>::is_signed, duration<_Rep, _Period>>
393 abs(duration<_Rep, _Period> __d)
394 {
395 if (__d >= __d.zero())
396 return __d;
397 return -__d;
398 }
399
400 // Make chrono::ceil<D> also usable as chrono::__detail::ceil<D>.
401 namespace __detail { using chrono::ceil; }
402
403#else // ! C++17
404
405 // We want to use ceil even when compiling for earlier standards versions.
406 // C++11 only allows a single statement in a constexpr function, so we
407 // need to move the comparison into a separate function, __ceil_impl.
408 namespace __detail
409 {
410 template<typename _Tp, typename _Up>
411 constexpr _Tp
412 __ceil_impl(const _Tp& __t, const _Up& __u)
413 {
414 return (__t < __u) ? (__t + _Tp{1}) : __t;
415 }
416
417 // C++11-friendly version of std::chrono::ceil<D> for internal use.
418 template<typename _ToDur, typename _Rep, typename _Period>
419 constexpr _ToDur
420 ceil(const duration<_Rep, _Period>& __d)
421 {
422 return __detail::__ceil_impl(chrono::duration_cast<_ToDur>(__d), __d);
423 }
424 }
425#endif // C++17
426
427 /// duration_values
428 template<typename _Rep>
429 struct duration_values
430 {
431 static constexpr _Rep
432 zero() noexcept
433 { return _Rep(0); }
434
435 static constexpr _Rep
436 max() noexcept
437 { return numeric_limits<_Rep>::max(); }
438
439 static constexpr _Rep
440 min() noexcept
441 { return numeric_limits<_Rep>::lowest(); }
442 };
443
444 /// @cond undocumented
445
446 template<typename _Tp>
447 struct __is_ratio
448 : std::false_type
449 { };
450
451 template<intmax_t _Num, intmax_t _Den>
452 struct __is_ratio<ratio<_Num, _Den>>
453 : std::true_type
454 { };
455
456 /// @endcond
457
458 template<typename _Rep, typename _Period>
459 struct duration
460 {
461 private:
462 template<typename _Rep2>
463 using __is_float = treat_as_floating_point<_Rep2>;
464
465 static constexpr intmax_t
466 _S_gcd(intmax_t __m, intmax_t __n) noexcept
467 {
468 // Duration only allows positive periods so we don't need to
469 // handle negative values here (unlike __static_gcd and std::gcd).
470#if __cplusplus >= 201402L
471 do
472 {
473 intmax_t __rem = __m % __n;
474 __m = __n;
475 __n = __rem;
476 }
477 while (__n != 0);
478 return __m;
479#else
480 // C++11 doesn't allow loops in constexpr functions, but this
481 // recursive version can be more expensive to evaluate.
482 return (__n == 0) ? __m : _S_gcd(__n, __m % __n);
483#endif
484 }
485
486 // _GLIBCXX_RESOLVE_LIB_DEFECTS
487 // 2094. overflow shouldn't participate in overload resolution
488 // 3090. What is [2094] intended to mean?
489 // This only produces a valid type if no overflow occurs.
490 template<typename _R1, typename _R2,
491 intmax_t __gcd1 = _S_gcd(_R1::num, _R2::num),
492 intmax_t __gcd2 = _S_gcd(_R1::den, _R2::den)>
493 using __divide = ratio<(_R1::num / __gcd1) * (_R2::den / __gcd2),
494 (_R1::den / __gcd2) * (_R2::num / __gcd1)>;
495
496 // _Period2 is an exact multiple of _Period
497 template<typename _Period2>
498 using __is_harmonic
499 = __bool_constant<__divide<_Period2, _Period>::den == 1>;
500
501 public:
502
503 using rep = _Rep;
504 using period = typename _Period::type;
505
506 static_assert(!__is_duration<_Rep>::value, "rep cannot be a duration");
507 static_assert(__is_ratio<_Period>::value,
508 "period must be a specialization of ratio");
509 static_assert(_Period::num > 0, "period must be positive");
510
511 // 20.11.5.1 construction / copy / destroy
512 constexpr duration() = default;
513
514 duration(const duration&) = default;
515
516 // _GLIBCXX_RESOLVE_LIB_DEFECTS
517 // 3050. Conversion specification problem in chrono::duration
518 template<typename _Rep2, typename = _Require<
519 is_convertible<const _Rep2&, rep>,
520 __or_<__is_float<rep>, __not_<__is_float<_Rep2>>>>>
521 constexpr explicit duration(const _Rep2& __rep)
522 : __r(static_cast<rep>(__rep)) { }
523
524 template<typename _Rep2, typename _Period2, typename = _Require<
525 is_convertible<const _Rep2&, rep>,
526 __or_<__is_float<rep>,
527 __and_<__is_harmonic<_Period2>,
528 __not_<__is_float<_Rep2>>>>>>
529 constexpr duration(const duration<_Rep2, _Period2>& __d)
530 : __r(duration_cast<duration>(__d).count()) { }
531
532 ~duration() = default;
533 duration& operator=(const duration&) = default;
534
535 // 20.11.5.2 observer
536 constexpr rep
537 count() const
538 { return __r; }
539
540 // 20.11.5.3 arithmetic
541
542 constexpr duration<typename common_type<rep>::type, period>
543 operator+() const
544 { return duration<typename common_type<rep>::type, period>(__r); }
545
546 constexpr duration<typename common_type<rep>::type, period>
547 operator-() const
548 { return duration<typename common_type<rep>::type, period>(-__r); }
549
550 _GLIBCXX17_CONSTEXPR duration&
551 operator++()
552 {
553 ++__r;
554 return *this;
555 }
556
557 _GLIBCXX17_CONSTEXPR duration
558 operator++(int)
559 { return duration(__r++); }
560
561 _GLIBCXX17_CONSTEXPR duration&
562 operator--()
563 {
564 --__r;
565 return *this;
566 }
567
568 _GLIBCXX17_CONSTEXPR duration
569 operator--(int)
570 { return duration(__r--); }
571
572 _GLIBCXX17_CONSTEXPR duration&
573 operator+=(const duration& __d)
574 {
575 __r += __d.count();
576 return *this;
577 }
578
579 _GLIBCXX17_CONSTEXPR duration&
580 operator-=(const duration& __d)
581 {
582 __r -= __d.count();
583 return *this;
584 }
585
586 _GLIBCXX17_CONSTEXPR duration&
587 operator*=(const rep& __rhs)
588 {
589 __r *= __rhs;
590 return *this;
591 }
592
593 _GLIBCXX17_CONSTEXPR duration&
594 operator/=(const rep& __rhs)
595 {
596 __r /= __rhs;
597 return *this;
598 }
599
600 // DR 934.
601 template<typename _Rep2 = rep>
602 _GLIBCXX17_CONSTEXPR
603 typename enable_if<!treat_as_floating_point<_Rep2>::value,
604 duration&>::type
605 operator%=(const rep& __rhs)
606 {
607 __r %= __rhs;
608 return *this;
609 }
610
611 template<typename _Rep2 = rep>
612 _GLIBCXX17_CONSTEXPR
613 typename enable_if<!treat_as_floating_point<_Rep2>::value,
614 duration&>::type
615 operator%=(const duration& __d)
616 {
617 __r %= __d.count();
618 return *this;
619 }
620
621 // 20.11.5.4 special values
622 static constexpr duration
623 zero() noexcept
624 { return duration(duration_values<rep>::zero()); }
625
626 static constexpr duration
627 min() noexcept
628 { return duration(duration_values<rep>::min()); }
629
630 static constexpr duration
631 max() noexcept
632 { return duration(duration_values<rep>::max()); }
633
634 private:
635 rep __r;
636 };
637
638 /// @{
639 /// @relates std::chrono::duration
640
641 /// The sum of two durations.
642 template<typename _Rep1, typename _Period1,
643 typename _Rep2, typename _Period2>
644 constexpr typename common_type<duration<_Rep1, _Period1>,
645 duration<_Rep2, _Period2>>::type
646 operator+(const duration<_Rep1, _Period1>& __lhs,
647 const duration<_Rep2, _Period2>& __rhs)
648 {
649 typedef duration<_Rep1, _Period1> __dur1;
650 typedef duration<_Rep2, _Period2> __dur2;
651 typedef typename common_type<__dur1,__dur2>::type __cd;
652 return __cd(__cd(__lhs).count() + __cd(__rhs).count());
653 }
654
655 /// The difference between two durations.
656 template<typename _Rep1, typename _Period1,
657 typename _Rep2, typename _Period2>
658 constexpr typename common_type<duration<_Rep1, _Period1>,
659 duration<_Rep2, _Period2>>::type
660 operator-(const duration<_Rep1, _Period1>& __lhs,
661 const duration<_Rep2, _Period2>& __rhs)
662 {
663 typedef duration<_Rep1, _Period1> __dur1;
664 typedef duration<_Rep2, _Period2> __dur2;
665 typedef typename common_type<__dur1,__dur2>::type __cd;
666 return __cd(__cd(__lhs).count() - __cd(__rhs).count());
667 }
668
669 /// @}
670
671 /// @cond undocumented
672
673 // SFINAE helper to obtain common_type<_Rep1, _Rep2> only if _Rep2
674 // is implicitly convertible to it.
675 // _GLIBCXX_RESOLVE_LIB_DEFECTS
676 // 3050. Conversion specification problem in chrono::duration constructor
677 template<typename _Rep1, typename _Rep2,
678 typename _CRep = typename common_type<_Rep1, _Rep2>::type>
679 using __common_rep_t = typename
680 enable_if<is_convertible<const _Rep2&, _CRep>::value, _CRep>::type;
681
682 /// @endcond
683
684 /** @{
685 * Arithmetic operators for chrono::duration
686 * @relates std::chrono::duration
687 */
688
689 template<typename _Rep1, typename _Period, typename _Rep2>
690 constexpr duration<__common_rep_t<_Rep1, _Rep2>, _Period>
691 operator*(const duration<_Rep1, _Period>& __d, const _Rep2& __s)
692 {
693 typedef duration<typename common_type<_Rep1, _Rep2>::type, _Period>
694 __cd;
695 return __cd(__cd(__d).count() * __s);
696 }
697
698 template<typename _Rep1, typename _Rep2, typename _Period>
699 constexpr duration<__common_rep_t<_Rep2, _Rep1>, _Period>
700 operator*(const _Rep1& __s, const duration<_Rep2, _Period>& __d)
701 { return __d * __s; }
702
703 template<typename _Rep1, typename _Period, typename _Rep2>
704 constexpr
705 duration<__common_rep_t<_Rep1, __disable_if_is_duration<_Rep2>>, _Period>
706 operator/(const duration<_Rep1, _Period>& __d, const _Rep2& __s)
707 {
708 typedef duration<typename common_type<_Rep1, _Rep2>::type, _Period>
709 __cd;
710 return __cd(__cd(__d).count() / __s);
711 }
712
713 template<typename _Rep1, typename _Period1,
714 typename _Rep2, typename _Period2>
715 constexpr typename common_type<_Rep1, _Rep2>::type
716 operator/(const duration<_Rep1, _Period1>& __lhs,
717 const duration<_Rep2, _Period2>& __rhs)
718 {
719 typedef duration<_Rep1, _Period1> __dur1;
720 typedef duration<_Rep2, _Period2> __dur2;
721 typedef typename common_type<__dur1,__dur2>::type __cd;
722 return __cd(__lhs).count() / __cd(__rhs).count();
723 }
724
725 // DR 934.
726 template<typename _Rep1, typename _Period, typename _Rep2>
727 constexpr
728 duration<__common_rep_t<_Rep1, __disable_if_is_duration<_Rep2>>, _Period>
729 operator%(const duration<_Rep1, _Period>& __d, const _Rep2& __s)
730 {
731 typedef duration<typename common_type<_Rep1, _Rep2>::type, _Period>
732 __cd;
733 return __cd(__cd(__d).count() % __s);
734 }
735
736 template<typename _Rep1, typename _Period1,
737 typename _Rep2, typename _Period2>
738 constexpr typename common_type<duration<_Rep1, _Period1>,
739 duration<_Rep2, _Period2>>::type
740 operator%(const duration<_Rep1, _Period1>& __lhs,
741 const duration<_Rep2, _Period2>& __rhs)
742 {
743 typedef duration<_Rep1, _Period1> __dur1;
744 typedef duration<_Rep2, _Period2> __dur2;
745 typedef typename common_type<__dur1,__dur2>::type __cd;
746 return __cd(__cd(__lhs).count() % __cd(__rhs).count());
747 }
748 /// @}
749
750 // comparisons
751
752 /** @{
753 * Comparisons for chrono::duration
754 * @relates std::chrono::duration
755 */
756
757 template<typename _Rep1, typename _Period1,
758 typename _Rep2, typename _Period2>
759 constexpr bool
760 operator==(const duration<_Rep1, _Period1>& __lhs,
761 const duration<_Rep2, _Period2>& __rhs)
762 {
763 typedef duration<_Rep1, _Period1> __dur1;
764 typedef duration<_Rep2, _Period2> __dur2;
765 typedef typename common_type<__dur1,__dur2>::type __ct;
766 return __ct(__lhs).count() == __ct(__rhs).count();
767 }
768
769 template<typename _Rep1, typename _Period1,
770 typename _Rep2, typename _Period2>
771 constexpr bool
772 operator<(const duration<_Rep1, _Period1>& __lhs,
773 const duration<_Rep2, _Period2>& __rhs)
774 {
775 typedef duration<_Rep1, _Period1> __dur1;
776 typedef duration<_Rep2, _Period2> __dur2;
777 typedef typename common_type<__dur1,__dur2>::type __ct;
778 return __ct(__lhs).count() < __ct(__rhs).count();
779 }
780
781#if __cpp_lib_three_way_comparison
782 template<typename _Rep1, typename _Period1,
783 typename _Rep2, typename _Period2>
784 requires three_way_comparable<common_type_t<_Rep1, _Rep2>>
785 constexpr auto
786 operator<=>(const duration<_Rep1, _Period1>& __lhs,
787 const duration<_Rep2, _Period2>& __rhs)
788 {
789 using __ct = common_type_t<duration<_Rep1, _Period1>,
790 duration<_Rep2, _Period2>>;
791 return __ct(__lhs).count() <=> __ct(__rhs).count();
792 }
793#else
794 template<typename _Rep1, typename _Period1,
795 typename _Rep2, typename _Period2>
796 constexpr bool
797 operator!=(const duration<_Rep1, _Period1>& __lhs,
798 const duration<_Rep2, _Period2>& __rhs)
799 { return !(__lhs == __rhs); }
800#endif
801
802 template<typename _Rep1, typename _Period1,
803 typename _Rep2, typename _Period2>
804 constexpr bool
805 operator<=(const duration<_Rep1, _Period1>& __lhs,
806 const duration<_Rep2, _Period2>& __rhs)
807 { return !(__rhs < __lhs); }
808
809 template<typename _Rep1, typename _Period1,
810 typename _Rep2, typename _Period2>
811 constexpr bool
812 operator>(const duration<_Rep1, _Period1>& __lhs,
813 const duration<_Rep2, _Period2>& __rhs)
814 { return __rhs < __lhs; }
815
816 template<typename _Rep1, typename _Period1,
817 typename _Rep2, typename _Period2>
818 constexpr bool
819 operator>=(const duration<_Rep1, _Period1>& __lhs,
820 const duration<_Rep2, _Period2>& __rhs)
821 { return !(__lhs < __rhs); }
822
823 /// @}
824
825 /// @cond undocumented
826#ifdef _GLIBCXX_USE_C99_STDINT_TR1
827# define _GLIBCXX_CHRONO_INT64_T int64_t
828#elif defined __INT64_TYPE__
829# define _GLIBCXX_CHRONO_INT64_T __INT64_TYPE__
830#else
831 static_assert(std::numeric_limits<unsigned long long>::digits >= 64,
832 "Representation type for nanoseconds must have at least 64 bits");
833# define _GLIBCXX_CHRONO_INT64_T long long
834#endif
835 /// @endcond
836
837 /// nanoseconds
838 using nanoseconds = duration<_GLIBCXX_CHRONO_INT64_T, nano>;
839
840 /// microseconds
841 using microseconds = duration<_GLIBCXX_CHRONO_INT64_T, micro>;
842
843 /// milliseconds
844 using milliseconds = duration<_GLIBCXX_CHRONO_INT64_T, milli>;
845
846 /// seconds
847 using seconds = duration<_GLIBCXX_CHRONO_INT64_T>;
848
849 /// minutes
850 using minutes = duration<_GLIBCXX_CHRONO_INT64_T, ratio< 60>>;
851
852 /// hours
853 using hours = duration<_GLIBCXX_CHRONO_INT64_T, ratio<3600>>;
854
855#if __cplusplus > 201703L
856 /// days
857 using days = duration<_GLIBCXX_CHRONO_INT64_T, ratio<86400>>;
858
859 /// weeks
860 using weeks = duration<_GLIBCXX_CHRONO_INT64_T, ratio<604800>>;
861
862 /// years
863 using years = duration<_GLIBCXX_CHRONO_INT64_T, ratio<31556952>>;
864
865 /// months
866 using months = duration<_GLIBCXX_CHRONO_INT64_T, ratio<2629746>>;
867#endif // C++20
868
869#undef _GLIBCXX_CHRONO_INT64_T
870
871 template<typename _Clock, typename _Dur>
872 struct time_point
873 {
874 static_assert(__is_duration<_Dur>::value,
875 "duration must be a specialization of std::chrono::duration");
876
877 typedef _Clock clock;
878 typedef _Dur duration;
879 typedef typename duration::rep rep;
880 typedef typename duration::period period;
881
882 constexpr time_point() : __d(duration::zero())
883 { }
884
885 constexpr explicit time_point(const duration& __dur)
886 : __d(__dur)
887 { }
888
889 // conversions
890 template<typename _Dur2,
891 typename = _Require<is_convertible<_Dur2, _Dur>>>
892 constexpr time_point(const time_point<clock, _Dur2>& __t)
893 : __d(__t.time_since_epoch())
894 { }
895
896 // observer
897 constexpr duration
898 time_since_epoch() const
899 { return __d; }
900
901#if __cplusplus > 201703L
902 constexpr time_point&
903 operator++()
904 {
905 ++__d;
906 return *this;
907 }
908
909 constexpr time_point
910 operator++(int)
911 { return time_point{__d++}; }
912
913 constexpr time_point&
914 operator--()
915 {
916 --__d;
917 return *this;
918 }
919
920 constexpr time_point
921 operator--(int)
922 { return time_point{__d--}; }
923#endif
924
925 // arithmetic
926 _GLIBCXX17_CONSTEXPR time_point&
927 operator+=(const duration& __dur)
928 {
929 __d += __dur;
930 return *this;
931 }
932
933 _GLIBCXX17_CONSTEXPR time_point&
934 operator-=(const duration& __dur)
935 {
936 __d -= __dur;
937 return *this;
938 }
939
940 // special values
941 static constexpr time_point
942 min() noexcept
943 { return time_point(duration::min()); }
944
945 static constexpr time_point
946 max() noexcept
947 { return time_point(duration::max()); }
948
949 private:
950 duration __d;
951 };
952
953 /// time_point_cast
954 template<typename _ToDur, typename _Clock, typename _Dur>
955 constexpr typename enable_if<__is_duration<_ToDur>::value,
956 time_point<_Clock, _ToDur>>::type
957 time_point_cast(const time_point<_Clock, _Dur>& __t)
958 {
959 typedef time_point<_Clock, _ToDur> __time_point;
960 return __time_point(duration_cast<_ToDur>(__t.time_since_epoch()));
961 }
962
963#if __cplusplus > 201402L
964 template<typename _ToDur, typename _Clock, typename _Dur>
965 constexpr
966 enable_if_t<__is_duration<_ToDur>::value, time_point<_Clock, _ToDur>>
967 floor(const time_point<_Clock, _Dur>& __tp)
968 {
969 return time_point<_Clock, _ToDur>{
970 chrono::floor<_ToDur>(__tp.time_since_epoch())};
971 }
972
973 template<typename _ToDur, typename _Clock, typename _Dur>
974 constexpr
975 enable_if_t<__is_duration<_ToDur>::value, time_point<_Clock, _ToDur>>
976 ceil(const time_point<_Clock, _Dur>& __tp)
977 {
978 return time_point<_Clock, _ToDur>{
979 chrono::ceil<_ToDur>(__tp.time_since_epoch())};
980 }
981
982 template<typename _ToDur, typename _Clock, typename _Dur>
983 constexpr enable_if_t<
984 __and_<__is_duration<_ToDur>,
985 __not_<treat_as_floating_point<typename _ToDur::rep>>>::value,
986 time_point<_Clock, _ToDur>>
987 round(const time_point<_Clock, _Dur>& __tp)
988 {
989 return time_point<_Clock, _ToDur>{
990 chrono::round<_ToDur>(__tp.time_since_epoch())};
991 }
992#endif // C++17
993
994 /// @{
995 /// @relates time_point
996
997 /// Adjust a time point forwards by the given duration.
998 template<typename _Clock, typename _Dur1,
999 typename _Rep2, typename _Period2>
1000 constexpr time_point<_Clock,
1001 typename common_type<_Dur1, duration<_Rep2, _Period2>>::type>
1002 operator+(const time_point<_Clock, _Dur1>& __lhs,
1003 const duration<_Rep2, _Period2>& __rhs)
1004 {
1005 typedef duration<_Rep2, _Period2> __dur2;
1006 typedef typename common_type<_Dur1,__dur2>::type __ct;
1007 typedef time_point<_Clock, __ct> __time_point;
1008 return __time_point(__lhs.time_since_epoch() + __rhs);
1009 }
1010
1011 /// Adjust a time point forwards by the given duration.
1012 template<typename _Rep1, typename _Period1,
1013 typename _Clock, typename _Dur2>
1014 constexpr time_point<_Clock,
1015 typename common_type<duration<_Rep1, _Period1>, _Dur2>::type>
1016 operator+(const duration<_Rep1, _Period1>& __lhs,
1017 const time_point<_Clock, _Dur2>& __rhs)
1018 {
1019 typedef duration<_Rep1, _Period1> __dur1;
1020 typedef typename common_type<__dur1,_Dur2>::type __ct;
1021 typedef time_point<_Clock, __ct> __time_point;
1022 return __time_point(__rhs.time_since_epoch() + __lhs);
1023 }
1024
1025 /// Adjust a time point backwards by the given duration.
1026 template<typename _Clock, typename _Dur1,
1027 typename _Rep2, typename _Period2>
1028 constexpr time_point<_Clock,
1029 typename common_type<_Dur1, duration<_Rep2, _Period2>>::type>
1030 operator-(const time_point<_Clock, _Dur1>& __lhs,
1031 const duration<_Rep2, _Period2>& __rhs)
1032 {
1033 typedef duration<_Rep2, _Period2> __dur2;
1034 typedef typename common_type<_Dur1,__dur2>::type __ct;
1035 typedef time_point<_Clock, __ct> __time_point;
1036 return __time_point(__lhs.time_since_epoch() -__rhs);
1037 }
1038
1039 /// The difference between two time points (as a duration)
1040 template<typename _Clock, typename _Dur1, typename _Dur2>
1041 constexpr typename common_type<_Dur1, _Dur2>::type
1042 operator-(const time_point<_Clock, _Dur1>& __lhs,
1043 const time_point<_Clock, _Dur2>& __rhs)
1044 { return __lhs.time_since_epoch() - __rhs.time_since_epoch(); }
1045 /// @}
1046
1047 /** @{
1048 * Comparisons for time_point
1049 * @relates chrono::time_point
1050 */
1051
1052 template<typename _Clock, typename _Dur1, typename _Dur2>
1053 constexpr bool
1054 operator==(const time_point<_Clock, _Dur1>& __lhs,
1055 const time_point<_Clock, _Dur2>& __rhs)
1056 { return __lhs.time_since_epoch() == __rhs.time_since_epoch(); }
1057
1058#if __cpp_lib_three_way_comparison
1059 template<typename _Clock, typename _Dur1,
1060 three_way_comparable_with<_Dur1> _Dur2>
1061 constexpr auto
1062 operator<=>(const time_point<_Clock, _Dur1>& __lhs,
1063 const time_point<_Clock, _Dur2>& __rhs)
1064 { return __lhs.time_since_epoch() <=> __rhs.time_since_epoch(); }
1065#else
1066 template<typename _Clock, typename _Dur1, typename _Dur2>
1067 constexpr bool
1068 operator!=(const time_point<_Clock, _Dur1>& __lhs,
1069 const time_point<_Clock, _Dur2>& __rhs)
1070 { return !(__lhs == __rhs); }
1071#endif
1072
1073 template<typename _Clock, typename _Dur1, typename _Dur2>
1074 constexpr bool
1075 operator<(const time_point<_Clock, _Dur1>& __lhs,
1076 const time_point<_Clock, _Dur2>& __rhs)
1077 { return __lhs.time_since_epoch() < __rhs.time_since_epoch(); }
1078
1079 template<typename _Clock, typename _Dur1, typename _Dur2>
1080 constexpr bool
1081 operator<=(const time_point<_Clock, _Dur1>& __lhs,
1082 const time_point<_Clock, _Dur2>& __rhs)
1083 { return !(__rhs < __lhs); }
1084
1085 template<typename _Clock, typename _Dur1, typename _Dur2>
1086 constexpr bool
1087 operator>(const time_point<_Clock, _Dur1>& __lhs,
1088 const time_point<_Clock, _Dur2>& __rhs)
1089 { return __rhs < __lhs; }
1090
1091 template<typename _Clock, typename _Dur1, typename _Dur2>
1092 constexpr bool
1093 operator>=(const time_point<_Clock, _Dur1>& __lhs,
1094 const time_point<_Clock, _Dur2>& __rhs)
1095 { return !(__lhs < __rhs); }
1096
1097 /// @}
1098
1099 // Clocks.
1100
1101 // Why nanosecond resolution as the default?
1102 // Why have std::system_clock always count in the highest
1103 // resolution (ie nanoseconds), even if on some OSes the low 3
1104 // or 9 decimal digits will be always zero? This allows later
1105 // implementations to change the system_clock::now()
1106 // implementation any time to provide better resolution without
1107 // changing function signature or units.
1108
1109 // To support the (forward) evolution of the library's defined
1110 // clocks, wrap inside inline namespace so that the current
1111 // defintions of system_clock, steady_clock, and
1112 // high_resolution_clock types are uniquely mangled. This way, new
1113 // code can use the latests clocks, while the library can contain
1114 // compatibility definitions for previous versions. At some
1115 // point, when these clocks settle down, the inlined namespaces
1116 // can be removed. XXX GLIBCXX_ABI Deprecated
1117 inline namespace _V2 {
1118
1119 /**
1120 * @brief System clock.
1121 *
1122 * Time returned represents wall time from the system-wide clock.
1123 * @ingroup chrono
1124 */
1125 struct system_clock
1126 {
1127 typedef chrono::nanoseconds duration;
1128 typedef duration::rep rep;
1129 typedef duration::period period;
1130 typedef chrono::time_point<system_clock, duration> time_point;
1131
1132 static_assert(system_clock::duration::min()
1133 < system_clock::duration::zero(),
1134 "a clock's minimum duration cannot be less than its epoch");
1135
1136 static constexpr bool is_steady = false;
1137
1138 static time_point
1139 now() noexcept;
1140
1141 // Map to C API
1142 _GLIBCXX_TIME_BITS64_ABI_TAG
1143 static std::time_t
1144 to_time_t(const time_point& __t) noexcept
1145 {
1146 return std::time_t(duration_cast<chrono::seconds>
1147 (__t.time_since_epoch()).count());
1148 }
1149
1150 _GLIBCXX_TIME_BITS64_ABI_TAG
1151 static time_point
1152 from_time_t(std::time_t __t) noexcept
1153 {
1154 typedef chrono::time_point<system_clock, seconds> __from;
1155 return time_point_cast<system_clock::duration>
1156 (__from(chrono::seconds(__t)));
1157 }
1158 };
1159
1160
1161 /**
1162 * @brief Monotonic clock
1163 *
1164 * Time returned has the property of only increasing at a uniform rate.
1165 * @ingroup chrono
1166 */
1167 struct steady_clock
1168 {
1169 typedef chrono::nanoseconds duration;
1170 typedef duration::rep rep;
1171 typedef duration::period period;
1172 typedef chrono::time_point<steady_clock, duration> time_point;
1173
1174 static constexpr bool is_steady = true;
1175
1176 static time_point
1177 now() noexcept;
1178 };
1179
1180
1181 /**
1182 * @brief Highest-resolution clock
1183 *
1184 * This is the clock "with the shortest tick period." Alias to
1185 * std::system_clock until higher-than-nanosecond definitions
1186 * become feasible.
1187 * @ingroup chrono
1188 */
1189 using high_resolution_clock = system_clock;
1190
1191 } // end inline namespace _V2
1192
1193#if __cplusplus > 201703L
1194 template<typename _Duration>
1195 using sys_time = time_point<system_clock, _Duration>;
1196 using sys_seconds = sys_time<seconds>;
1197 using sys_days = sys_time<days>;
1198
1199 using file_clock = ::std::filesystem::__file_clock;
1200
1201 template<typename _Duration>
1202 using file_time = time_point<file_clock, _Duration>;
1203
1204 template<> struct is_clock<system_clock> : true_type { };
1205 template<> struct is_clock<steady_clock> : true_type { };
1206 template<> struct is_clock<file_clock> : true_type { };
1207
1208 template<> inline constexpr bool is_clock_v<system_clock> = true;
1209 template<> inline constexpr bool is_clock_v<steady_clock> = true;
1210 template<> inline constexpr bool is_clock_v<file_clock> = true;
1211
1212 struct local_t { };
1213 template<typename _Duration>
1214 using local_time = time_point<local_t, _Duration>;
1215 using local_seconds = local_time<seconds>;
1216 using local_days = local_time<days>;
1217
1218 class utc_clock;
1219 class tai_clock;
1220 class gps_clock;
1221
1222 template<typename _Duration>
1223 using utc_time = time_point<utc_clock, _Duration>;
1224 using utc_seconds = utc_time<seconds>;
1225
1226 template<typename _Duration>
1227 using tai_time = time_point<tai_clock, _Duration>;
1228 using tai_seconds = tai_time<seconds>;
1229
1230 template<typename _Duration>
1231 using gps_time = time_point<gps_clock, _Duration>;
1232 using gps_seconds = gps_time<seconds>;
1233
1234 template<> struct is_clock<utc_clock> : true_type { };
1235 template<> struct is_clock<tai_clock> : true_type { };
1236 template<> struct is_clock<gps_clock> : true_type { };
1237
1238 template<> inline constexpr bool is_clock_v<utc_clock> = true;
1239 template<> inline constexpr bool is_clock_v<tai_clock> = true;
1240 template<> inline constexpr bool is_clock_v<gps_clock> = true;
1241
1242 struct leap_second_info
1243 {
1244 bool is_leap_second;
1245 seconds elapsed;
1246 };
1247
1248 // CALENDRICAL TYPES
1249
1250 // CLASS DECLARATIONS
1251 class day;
1252 class month;
1253 class year;
1254 class weekday;
1255 class weekday_indexed;
1256 class weekday_last;
1257 class month_day;
1258 class month_day_last;
1259 class month_weekday;
1260 class month_weekday_last;
1261 class year_month;
1262 class year_month_day;
1263 class year_month_day_last;
1264 class year_month_weekday;
1265 class year_month_weekday_last;
1266
1267 struct last_spec
1268 {
1269 explicit last_spec() = default;
1270
1271 friend constexpr month_day_last
1272 operator/(int __m, last_spec) noexcept;
1273
1274 friend constexpr month_day_last
1275 operator/(last_spec, int __m) noexcept;
1276 };
1277
1278 inline constexpr last_spec last{};
1279
1280 namespace __detail
1281 {
1282 // Compute the remainder of the Euclidean division of __n divided by __d.
1283 // Euclidean division truncates toward negative infinity and always
1284 // produces a remainder in the range of [0,__d-1] (whereas standard
1285 // division truncates toward zero and yields a nonpositive remainder
1286 // for negative __n).
1287 constexpr unsigned
1288 __modulo(long long __n, unsigned __d)
1289 {
1290 if (__n >= 0)
1291 return __n % __d;
1292 else
1293 return (__d + (__n % __d)) % __d;
1294 }
1295
1296 inline constexpr unsigned __days_per_month[12]
1297 = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
1298 }
1299
1300 // DAY
1301
1302 class day
1303 {
1304 private:
1305 unsigned char _M_d;
1306
1307 public:
1308 day() = default;
1309
1310 explicit constexpr
1311 day(unsigned __d) noexcept
1312 : _M_d(__d)
1313 { }
1314
1315 constexpr day&
1316 operator++() noexcept
1317 {
1318 ++_M_d;
1319 return *this;
1320 }
1321
1322 constexpr day
1323 operator++(int) noexcept
1324 {
1325 auto __ret = *this;
1326 ++(*this);
1327 return __ret;
1328 }
1329
1330 constexpr day&
1331 operator--() noexcept
1332 {
1333 --_M_d;
1334 return *this;
1335 }
1336
1337 constexpr day
1338 operator--(int) noexcept
1339 {
1340 auto __ret = *this;
1341 --(*this);
1342 return __ret;
1343 }
1344
1345 constexpr day&
1346 operator+=(const days& __d) noexcept
1347 {
1348 *this = *this + __d;
1349 return *this;
1350 }
1351
1352 constexpr day&
1353 operator-=(const days& __d) noexcept
1354 {
1355 *this = *this - __d;
1356 return *this;
1357 }
1358
1359 constexpr explicit
1360 operator unsigned() const noexcept
1361 { return _M_d; }
1362
1363 constexpr bool
1364 ok() const noexcept
1365 { return 1 <= _M_d && _M_d <= 31; }
1366
1367 friend constexpr bool
1368 operator==(const day& __x, const day& __y) noexcept
1369 { return unsigned{__x} == unsigned{__y}; }
1370
1371 friend constexpr strong_ordering
1372 operator<=>(const day& __x, const day& __y) noexcept
1373 { return unsigned{__x} <=> unsigned{__y}; }
1374
1375 friend constexpr day
1376 operator+(const day& __x, const days& __y) noexcept
1377 { return day(unsigned{__x} + __y.count()); }
1378
1379 friend constexpr day
1380 operator+(const days& __x, const day& __y) noexcept
1381 { return __y + __x; }
1382
1383 friend constexpr day
1384 operator-(const day& __x, const days& __y) noexcept
1385 { return __x + -__y; }
1386
1387 friend constexpr days
1388 operator-(const day& __x, const day& __y) noexcept
1389 { return days{int(unsigned{__x}) - int(unsigned{__y})}; }
1390
1391 friend constexpr month_day
1392 operator/(const month& __m, const day& __d) noexcept;
1393
1394 friend constexpr month_day
1395 operator/(int __m, const day& __d) noexcept;
1396
1397 friend constexpr month_day
1398 operator/(const day& __d, const month& __m) noexcept;
1399
1400 friend constexpr month_day
1401 operator/(const day& __d, int __m) noexcept;
1402
1403 friend constexpr year_month_day
1404 operator/(const year_month& __ym, const day& __d) noexcept;
1405
1406 // TODO: Implement operator<<, to_stream, from_stream.
1407 };
1408
1409 // MONTH
1410
1411 class month
1412 {
1413 private:
1414 unsigned char _M_m;
1415
1416 public:
1417 month() = default;
1418
1419 explicit constexpr
1420 month(unsigned __m) noexcept
1421 : _M_m(__m)
1422 { }
1423
1424 constexpr month&
1425 operator++() noexcept
1426 {
1427 *this += months{1};
1428 return *this;
1429 }
1430
1431 constexpr month
1432 operator++(int) noexcept
1433 {
1434 auto __ret = *this;
1435 ++(*this);
1436 return __ret;
1437 }
1438
1439 constexpr month&
1440 operator--() noexcept
1441 {
1442 *this -= months{1};
1443 return *this;
1444 }
1445
1446 constexpr month
1447 operator--(int) noexcept
1448 {
1449 auto __ret = *this;
1450 --(*this);
1451 return __ret;
1452 }
1453
1454 constexpr month&
1455 operator+=(const months& __m) noexcept
1456 {
1457 *this = *this + __m;
1458 return *this;
1459 }
1460
1461 constexpr month&
1462 operator-=(const months& __m) noexcept
1463 {
1464 *this = *this - __m;
1465 return *this;
1466 }
1467
1468 explicit constexpr
1469 operator unsigned() const noexcept
1470 { return _M_m; }
1471
1472 constexpr bool
1473 ok() const noexcept
1474 { return 1 <= _M_m && _M_m <= 12; }
1475
1476 friend constexpr bool
1477 operator==(const month& __x, const month& __y) noexcept
1478 { return unsigned{__x} == unsigned{__y}; }
1479
1480 friend constexpr strong_ordering
1481 operator<=>(const month& __x, const month& __y) noexcept
1482 { return unsigned{__x} <=> unsigned{__y}; }
1483
1484 friend constexpr month
1485 operator+(const month& __x, const months& __y) noexcept
1486 {
1487 auto __n = static_cast<long long>(unsigned{__x}) + (__y.count() - 1);
1488 return month{__detail::__modulo(__n, 12) + 1};
1489 }
1490
1491 friend constexpr month
1492 operator+(const months& __x, const month& __y) noexcept
1493 { return __y + __x; }
1494
1495 friend constexpr month
1496 operator-(const month& __x, const months& __y) noexcept
1497 { return __x + -__y; }
1498
1499 friend constexpr months
1500 operator-(const month& __x, const month& __y) noexcept
1501 {
1502 const auto __dm = int(unsigned(__x)) - int(unsigned(__y));
1503 return months{__dm < 0 ? 12 + __dm : __dm};
1504 }
1505
1506 friend constexpr year_month
1507 operator/(const year& __y, const month& __m) noexcept;
1508
1509 friend constexpr month_day
1510 operator/(const month& __m, int __d) noexcept;
1511
1512 friend constexpr month_day_last
1513 operator/(const month& __m, last_spec) noexcept;
1514
1515 friend constexpr month_day_last
1516 operator/(last_spec, const month& __m) noexcept;
1517
1518 friend constexpr month_weekday
1519 operator/(const month& __m, const weekday_indexed& __wdi) noexcept;
1520
1521 friend constexpr month_weekday
1522 operator/(const weekday_indexed& __wdi, const month& __m) noexcept;
1523
1524 friend constexpr month_weekday_last
1525 operator/(const month& __m, const weekday_last& __wdl) noexcept;
1526
1527 friend constexpr month_weekday_last
1528 operator/(const weekday_last& __wdl, const month& __m) noexcept;
1529
1530 // TODO: Implement operator<<, to_stream, from_stream.
1531 };
1532
1533 inline constexpr month January{1};
1534 inline constexpr month February{2};
1535 inline constexpr month March{3};
1536 inline constexpr month April{4};
1537 inline constexpr month May{5};
1538 inline constexpr month June{6};
1539 inline constexpr month July{7};
1540 inline constexpr month August{8};
1541 inline constexpr month September{9};
1542 inline constexpr month October{10};
1543 inline constexpr month November{11};
1544 inline constexpr month December{12};
1545
1546 // YEAR
1547
1548 class year
1549 {
1550 private:
1551 short _M_y;
1552
1553 public:
1554 year() = default;
1555
1556 explicit constexpr
1557 year(int __y) noexcept
1558 : _M_y{static_cast<short>(__y)}
1559 { }
1560
1561 static constexpr year
1562 min() noexcept
1563 { return year{-32767}; }
1564
1565 static constexpr year
1566 max() noexcept
1567 { return year{32767}; }
1568
1569 constexpr year&
1570 operator++() noexcept
1571 {
1572 ++_M_y;
1573 return *this;
1574 }
1575
1576 constexpr year
1577 operator++(int) noexcept
1578 {
1579 auto __ret = *this;
1580 ++(*this);
1581 return __ret;
1582 }
1583
1584 constexpr year&
1585 operator--() noexcept
1586 {
1587 --_M_y;
1588 return *this;
1589 }
1590
1591 constexpr year
1592 operator--(int) noexcept
1593 {
1594 auto __ret = *this;
1595 --(*this);
1596 return __ret;
1597 }
1598
1599 constexpr year&
1600 operator+=(const years& __y) noexcept
1601 {
1602 *this = *this + __y;
1603 return *this;
1604 }
1605
1606 constexpr year&
1607 operator-=(const years& __y) noexcept
1608 {
1609 *this = *this - __y;
1610 return *this;
1611 }
1612
1613 constexpr year
1614 operator+() const noexcept
1615 { return *this; }
1616
1617 constexpr year
1618 operator-() const noexcept
1619 { return year{-_M_y}; }
1620
1621 constexpr bool
1622 is_leap() const noexcept
1623 {
1624 // Testing divisibility by 100 first gives better performance, that is,
1625 // return (_M_y % 100 != 0 || _M_y % 400 == 0) && _M_y % 4 == 0;
1626
1627 // It gets even faster if _M_y is in [-536870800, 536870999]
1628 // (which is the case here) and _M_y % 100 is replaced by
1629 // __is_multiple_of_100 below.
1630
1631 // References:
1632 // [1] https://github.com/cassioneri/calendar
1633 // [2] https://accu.org/journals/overload/28/155/overload155.pdf#page=16
1634
1635 constexpr uint32_t __multiplier = 42949673;
1636 constexpr uint32_t __bound = 42949669;
1637 constexpr uint32_t __max_dividend = 1073741799;
1638 constexpr uint32_t __offset = __max_dividend / 2 / 100 * 100;
1639 const bool __is_multiple_of_100
1640 = __multiplier * (_M_y + __offset) < __bound;
1641 return (!__is_multiple_of_100 || _M_y % 400 == 0) && _M_y % 4 == 0;
1642 }
1643
1644 explicit constexpr
1645 operator int() const noexcept
1646 { return _M_y; }
1647
1648 constexpr bool
1649 ok() const noexcept
1650 { return min()._M_y <= _M_y && _M_y <= max()._M_y; }
1651
1652 friend constexpr bool
1653 operator==(const year& __x, const year& __y) noexcept
1654 { return int{__x} == int{__y}; }
1655
1656 friend constexpr strong_ordering
1657 operator<=>(const year& __x, const year& __y) noexcept
1658 { return int{__x} <=> int{__y}; }
1659
1660 friend constexpr year
1661 operator+(const year& __x, const years& __y) noexcept
1662 { return year{int{__x} + static_cast<int>(__y.count())}; }
1663
1664 friend constexpr year
1665 operator+(const years& __x, const year& __y) noexcept
1666 { return __y + __x; }
1667
1668 friend constexpr year
1669 operator-(const year& __x, const years& __y) noexcept
1670 { return __x + -__y; }
1671
1672 friend constexpr years
1673 operator-(const year& __x, const year& __y) noexcept
1674 { return years{int{__x} - int{__y}}; }
1675
1676 friend constexpr year_month
1677 operator/(const year& __y, int __m) noexcept;
1678
1679 friend constexpr year_month_day
1680 operator/(const year& __y, const month_day& __md) noexcept;
1681
1682 friend constexpr year_month_day
1683 operator/(const month_day& __md, const year& __y) noexcept;
1684
1685 friend constexpr year_month_day_last
1686 operator/(const year& __y, const month_day_last& __mdl) noexcept;
1687
1688 friend constexpr year_month_day_last
1689 operator/(const month_day_last& __mdl, const year& __y) noexcept;
1690
1691 friend constexpr year_month_weekday
1692 operator/(const year& __y, const month_weekday& __mwd) noexcept;
1693
1694 friend constexpr year_month_weekday
1695 operator/(const month_weekday& __mwd, const year& __y) noexcept;
1696
1697 friend constexpr year_month_weekday_last
1698 operator/(const year& __y, const month_weekday_last& __mwdl) noexcept;
1699
1700 friend constexpr year_month_weekday_last
1701 operator/(const month_weekday_last& __mwdl, const year& __y) noexcept;
1702
1703 // TODO: Implement operator<<, to_stream, from_stream.
1704 };
1705
1706 // WEEKDAY
1707
1708 class weekday
1709 {
1710 private:
1711 unsigned char _M_wd;
1712
1713 static constexpr weekday
1714 _S_from_days(const days& __d)
1715 {
1716 auto __n = __d.count();
1717 return weekday(__n >= -4 ? (__n + 4) % 7 : (__n + 5) % 7 + 6);
1718 }
1719
1720 public:
1721 weekday() = default;
1722
1723 explicit constexpr
1724 weekday(unsigned __wd) noexcept
1725 : _M_wd(__wd == 7 ? 0 : __wd) // __wd % 7 ?
1726 { }
1727
1728 constexpr
1729 weekday(const sys_days& __dp) noexcept
1730 : weekday{_S_from_days(__dp.time_since_epoch())}
1731 { }
1732
1733 explicit constexpr
1734 weekday(const local_days& __dp) noexcept
1735 : weekday{sys_days{__dp.time_since_epoch()}}
1736 { }
1737
1738 constexpr weekday&
1739 operator++() noexcept
1740 {
1741 *this += days{1};
1742 return *this;
1743 }
1744
1745 constexpr weekday
1746 operator++(int) noexcept
1747 {
1748 auto __ret = *this;
1749 ++(*this);
1750 return __ret;
1751 }
1752
1753 constexpr weekday&
1754 operator--() noexcept
1755 {
1756 *this -= days{1};
1757 return *this;
1758 }
1759
1760 constexpr weekday
1761 operator--(int) noexcept
1762 {
1763 auto __ret = *this;
1764 --(*this);
1765 return __ret;
1766 }
1767
1768 constexpr weekday&
1769 operator+=(const days& __d) noexcept
1770 {
1771 *this = *this + __d;
1772 return *this;
1773 }
1774
1775 constexpr weekday&
1776 operator-=(const days& __d) noexcept
1777 {
1778 *this = *this - __d;
1779 return *this;
1780 }
1781
1782 constexpr unsigned
1783 c_encoding() const noexcept
1784 { return _M_wd; }
1785
1786 constexpr unsigned
1787 iso_encoding() const noexcept
1788 { return _M_wd == 0u ? 7u : _M_wd; }
1789
1790 constexpr bool
1791 ok() const noexcept
1792 { return _M_wd <= 6; }
1793
1794 constexpr weekday_indexed
1795 operator[](unsigned __index) const noexcept;
1796
1797 constexpr weekday_last
1798 operator[](last_spec) const noexcept;
1799
1800 friend constexpr bool
1801 operator==(const weekday& __x, const weekday& __y) noexcept
1802 { return __x._M_wd == __y._M_wd; }
1803
1804 friend constexpr weekday
1805 operator+(const weekday& __x, const days& __y) noexcept
1806 {
1807 auto __n = static_cast<long long>(__x._M_wd) + __y.count();
1808 return weekday{__detail::__modulo(__n, 7)};
1809 }
1810
1811 friend constexpr weekday
1812 operator+(const days& __x, const weekday& __y) noexcept
1813 { return __y + __x; }
1814
1815 friend constexpr weekday
1816 operator-(const weekday& __x, const days& __y) noexcept
1817 { return __x + -__y; }
1818
1819 friend constexpr days
1820 operator-(const weekday& __x, const weekday& __y) noexcept
1821 {
1822 auto __n = static_cast<long long>(__x._M_wd) - __y._M_wd;
1823 return days{__detail::__modulo(__n, 7)};
1824 }
1825
1826 // TODO: operator<<, from_stream.
1827 };
1828
1829 inline constexpr weekday Sunday{0};
1830 inline constexpr weekday Monday{1};
1831 inline constexpr weekday Tuesday{2};
1832 inline constexpr weekday Wednesday{3};
1833 inline constexpr weekday Thursday{4};
1834 inline constexpr weekday Friday{5};
1835 inline constexpr weekday Saturday{6};
1836
1837 // WEEKDAY_INDEXED
1838
1839 class weekday_indexed
1840 {
1841 private:
1842 chrono::weekday _M_wd;
1843 unsigned char _M_index;
1844
1845 public:
1846 weekday_indexed() = default;
1847
1848 constexpr
1849 weekday_indexed(const chrono::weekday& __wd, unsigned __index) noexcept
1850 : _M_wd(__wd), _M_index(__index)
1851 { }
1852
1853 constexpr chrono::weekday
1854 weekday() const noexcept
1855 { return _M_wd; }
1856
1857 constexpr unsigned
1858 index() const noexcept
1859 { return _M_index; };
1860
1861 constexpr bool
1862 ok() const noexcept
1863 { return _M_wd.ok() && 1 <= _M_index && _M_index <= 5; }
1864
1865 friend constexpr bool
1866 operator==(const weekday_indexed& __x, const weekday_indexed& __y) noexcept
1867 { return __x.weekday() == __y.weekday() && __x.index() == __y.index(); }
1868
1869 friend constexpr month_weekday
1870 operator/(const month& __m, const weekday_indexed& __wdi) noexcept;
1871
1872 friend constexpr month_weekday
1873 operator/(int __m, const weekday_indexed& __wdi) noexcept;
1874
1875 friend constexpr month_weekday
1876 operator/(const weekday_indexed& __wdi, const month& __m) noexcept;
1877
1878 friend constexpr month_weekday
1879 operator/(const weekday_indexed& __wdi, int __m) noexcept;
1880
1881 friend constexpr year_month_weekday
1882 operator/(const year_month& __ym, const weekday_indexed& __wdi) noexcept;
1883
1884 // TODO: Implement operator<<.
1885 };
1886
1887 constexpr weekday_indexed
1888 weekday::operator[](unsigned __index) const noexcept
1889 { return {*this, __index}; }
1890
1891 // WEEKDAY_LAST
1892
1893 class weekday_last
1894 {
1895 private:
1896 chrono::weekday _M_wd;
1897
1898 public:
1899 explicit constexpr
1900 weekday_last(const chrono::weekday& __wd) noexcept
1901 : _M_wd{__wd}
1902 { }
1903
1904 constexpr chrono::weekday
1905 weekday() const noexcept
1906 { return _M_wd; }
1907
1908 constexpr bool
1909 ok() const noexcept
1910 { return _M_wd.ok(); }
1911
1912 friend constexpr bool
1913 operator==(const weekday_last& __x, const weekday_last& __y) noexcept
1914 { return __x.weekday() == __y.weekday(); }
1915
1916 friend constexpr month_weekday_last
1917 operator/(int __m, const weekday_last& __wdl) noexcept;
1918
1919 friend constexpr month_weekday_last
1920 operator/(const weekday_last& __wdl, int __m) noexcept;
1921
1922 friend constexpr year_month_weekday_last
1923 operator/(const year_month& __ym, const weekday_last& __wdl) noexcept;
1924
1925 // TODO: Implement operator<<.
1926 };
1927
1928 constexpr weekday_last
1929 weekday::operator[](last_spec) const noexcept
1930 { return weekday_last{*this}; }
1931
1932 // MONTH_DAY
1933
1934 class month_day
1935 {
1936 private:
1937 chrono::month _M_m;
1938 chrono::day _M_d;
1939
1940 public:
1941 month_day() = default;
1942
1943 constexpr
1944 month_day(const chrono::month& __m, const chrono::day& __d) noexcept
1945 : _M_m{__m}, _M_d{__d}
1946 { }
1947
1948 constexpr chrono::month
1949 month() const noexcept
1950 { return _M_m; }
1951
1952 constexpr chrono::day
1953 day() const noexcept
1954 { return _M_d; }
1955
1956 constexpr bool
1957 ok() const noexcept
1958 {
1959 return _M_m.ok()
1960 && 1u <= unsigned(_M_d)
1961 && unsigned(_M_d) <= __detail::__days_per_month[unsigned(_M_m) - 1];
1962 }
1963
1964 friend constexpr bool
1965 operator==(const month_day& __x, const month_day& __y) noexcept
1966 { return __x.month() == __y.month() && __x.day() == __y.day(); }
1967
1968 friend constexpr strong_ordering
1969 operator<=>(const month_day& __x, const month_day& __y) noexcept
1970 = default;
1971
1972 friend constexpr month_day
1973 operator/(const chrono::month& __m, const chrono::day& __d) noexcept
1974 { return {__m, __d}; }
1975
1976 friend constexpr month_day
1977 operator/(const chrono::month& __m, int __d) noexcept
1978 { return {__m, chrono::day(unsigned(__d))}; }
1979
1980 friend constexpr month_day
1981 operator/(int __m, const chrono::day& __d) noexcept
1982 { return {chrono::month(unsigned(__m)), __d}; }
1983
1984 friend constexpr month_day
1985 operator/(const chrono::day& __d, const chrono::month& __m) noexcept
1986 { return {__m, __d}; }
1987
1988 friend constexpr month_day
1989 operator/(const chrono::day& __d, int __m) noexcept
1990 { return {chrono::month(unsigned(__m)), __d}; }
1991
1992 friend constexpr year_month_day
1993 operator/(int __y, const month_day& __md) noexcept;
1994
1995 friend constexpr year_month_day
1996 operator/(const month_day& __md, int __y) noexcept;
1997
1998 // TODO: Implement operator<<, from_stream.
1999 };
2000
2001 // MONTH_DAY_LAST
2002
2003 class month_day_last
2004 {
2005 private:
2006 chrono::month _M_m;
2007
2008 public:
2009 explicit constexpr
2010 month_day_last(const chrono::month& __m) noexcept
2011 : _M_m{__m}
2012 { }
2013
2014 constexpr chrono::month
2015 month() const noexcept
2016 { return _M_m; }
2017
2018 constexpr bool
2019 ok() const noexcept
2020 { return _M_m.ok(); }
2021
2022 friend constexpr bool
2023 operator==(const month_day_last& __x, const month_day_last& __y) noexcept
2024 { return __x.month() == __y.month(); }
2025
2026 friend constexpr strong_ordering
2027 operator<=>(const month_day_last& __x, const month_day_last& __y) noexcept
2028 = default;
2029
2030 friend constexpr month_day_last
2031 operator/(const chrono::month& __m, last_spec) noexcept
2032 { return month_day_last{__m}; }
2033
2034 friend constexpr month_day_last
2035 operator/(int __m, last_spec) noexcept
2036 { return chrono::month(unsigned(__m)) / last; }
2037
2038 friend constexpr month_day_last
2039 operator/(last_spec, const chrono::month& __m) noexcept
2040 { return __m / last; }
2041
2042 friend constexpr month_day_last
2043 operator/(last_spec, int __m) noexcept
2044 { return __m / last; }
2045
2046 friend constexpr year_month_day_last
2047 operator/(int __y, const month_day_last& __mdl) noexcept;
2048
2049 friend constexpr year_month_day_last
2050 operator/(const month_day_last& __mdl, int __y) noexcept;
2051
2052 // TODO: Implement operator<<.
2053 };
2054
2055 // MONTH_WEEKDAY
2056
2057 class month_weekday
2058 {
2059 private:
2060 chrono::month _M_m;
2061 chrono::weekday_indexed _M_wdi;
2062
2063 public:
2064 constexpr
2065 month_weekday(const chrono::month& __m,
2066 const chrono::weekday_indexed& __wdi) noexcept
2067 : _M_m{__m}, _M_wdi{__wdi}
2068 { }
2069
2070 constexpr chrono::month
2071 month() const noexcept
2072 { return _M_m; }
2073
2074 constexpr chrono::weekday_indexed
2075 weekday_indexed() const noexcept
2076 { return _M_wdi; }
2077
2078 constexpr bool
2079 ok() const noexcept
2080 { return _M_m.ok() && _M_wdi.ok(); }
2081
2082 friend constexpr bool
2083 operator==(const month_weekday& __x, const month_weekday& __y) noexcept
2084 {
2085 return __x.month() == __y.month()
2086 && __x.weekday_indexed() == __y.weekday_indexed();
2087 }
2088
2089 friend constexpr month_weekday
2090 operator/(const chrono::month& __m,
2091 const chrono::weekday_indexed& __wdi) noexcept
2092 { return {__m, __wdi}; }
2093
2094 friend constexpr month_weekday
2095 operator/(int __m, const chrono::weekday_indexed& __wdi) noexcept
2096 { return chrono::month(unsigned(__m)) / __wdi; }
2097
2098 friend constexpr month_weekday
2099 operator/(const chrono::weekday_indexed& __wdi,
2100 const chrono::month& __m) noexcept
2101 { return __m / __wdi; }
2102
2103 friend constexpr month_weekday
2104 operator/(const chrono::weekday_indexed& __wdi, int __m) noexcept
2105 { return __m / __wdi; }
2106
2107 friend constexpr year_month_weekday
2108 operator/(int __y, const month_weekday& __mwd) noexcept;
2109
2110 friend constexpr year_month_weekday
2111 operator/(const month_weekday& __mwd, int __y) noexcept;
2112
2113 // TODO: Implement operator<<.
2114 };
2115
2116 // MONTH_WEEKDAY_LAST
2117
2118 class month_weekday_last
2119 {
2120 private:
2121 chrono::month _M_m;
2122 chrono::weekday_last _M_wdl;
2123
2124 public:
2125 constexpr
2126 month_weekday_last(const chrono::month& __m,
2127 const chrono::weekday_last& __wdl) noexcept
2128 :_M_m{__m}, _M_wdl{__wdl}
2129 { }
2130
2131 constexpr chrono::month
2132 month() const noexcept
2133 { return _M_m; }
2134
2135 constexpr chrono::weekday_last
2136 weekday_last() const noexcept
2137 { return _M_wdl; }
2138
2139 constexpr bool
2140 ok() const noexcept
2141 { return _M_m.ok() && _M_wdl.ok(); }
2142
2143 friend constexpr bool
2144 operator==(const month_weekday_last& __x,
2145 const month_weekday_last& __y) noexcept
2146 {
2147 return __x.month() == __y.month()
2148 && __x.weekday_last() == __y.weekday_last();
2149 }
2150
2151 friend constexpr month_weekday_last
2152 operator/(const chrono::month& __m,
2153 const chrono::weekday_last& __wdl) noexcept
2154 { return {__m, __wdl}; }
2155
2156 friend constexpr month_weekday_last
2157 operator/(int __m, const chrono::weekday_last& __wdl) noexcept
2158 { return chrono::month(unsigned(__m)) / __wdl; }
2159
2160 friend constexpr month_weekday_last
2161 operator/(const chrono::weekday_last& __wdl,
2162 const chrono::month& __m) noexcept
2163 { return __m / __wdl; }
2164
2165 friend constexpr month_weekday_last
2166 operator/(const chrono::weekday_last& __wdl, int __m) noexcept
2167 { return chrono::month(unsigned(__m)) / __wdl; }
2168
2169 friend constexpr year_month_weekday_last
2170 operator/(int __y, const month_weekday_last& __mwdl) noexcept;
2171
2172 friend constexpr year_month_weekday_last
2173 operator/(const month_weekday_last& __mwdl, int __y) noexcept;
2174
2175 // TODO: Implement operator<<.
2176 };
2177
2178 // YEAR_MONTH
2179
2180 namespace __detail
2181 {
2182 // [time.cal.ym], [time.cal.ymd], etc constrain the 'months'-based
2183 // addition/subtraction operator overloads like so:
2184 //
2185 // Constraints: if the argument supplied by the caller for the months
2186 // parameter is convertible to years, its implicit conversion sequence
2187 // to years is worse than its implicit conversion sequence to months.
2188 //
2189 // We realize this constraint by templatizing the 'months'-based
2190 // overloads (using a dummy defaulted template parameter), so that
2191 // overload resolution doesn't select the 'months'-based overload unless
2192 // the implicit conversion sequence to 'months' is better than that to
2193 // 'years'.
2194 using __months_years_conversion_disambiguator = void;
2195 }
2196
2197 class year_month
2198 {
2199 private:
2200 chrono::year _M_y;
2201 chrono::month _M_m;
2202
2203 public:
2204 year_month() = default;
2205
2206 constexpr
2207 year_month(const chrono::year& __y, const chrono::month& __m) noexcept
2208 : _M_y{__y}, _M_m{__m}
2209 { }
2210
2211 constexpr chrono::year
2212 year() const noexcept
2213 { return _M_y; }
2214
2215 constexpr chrono::month
2216 month() const noexcept
2217 { return _M_m; }
2218
2219 template<typename = __detail::__months_years_conversion_disambiguator>
2220 constexpr year_month&
2221 operator+=(const months& __dm) noexcept
2222 {
2223 *this = *this + __dm;
2224 return *this;
2225 }
2226
2227 template<typename = __detail::__months_years_conversion_disambiguator>
2228 constexpr year_month&
2229 operator-=(const months& __dm) noexcept
2230 {
2231 *this = *this - __dm;
2232 return *this;
2233 }
2234
2235 constexpr year_month&
2236 operator+=(const years& __dy) noexcept
2237 {
2238 *this = *this + __dy;
2239 return *this;
2240 }
2241
2242 constexpr year_month&
2243 operator-=(const years& __dy) noexcept
2244 {
2245 *this = *this - __dy;
2246 return *this;
2247 }
2248
2249 constexpr bool
2250 ok() const noexcept
2251 { return _M_y.ok() && _M_m.ok(); }
2252
2253 friend constexpr bool
2254 operator==(const year_month& __x, const year_month& __y) noexcept
2255 { return __x.year() == __y.year() && __x.month() == __y.month(); }
2256
2257 friend constexpr strong_ordering
2258 operator<=>(const year_month& __x, const year_month& __y) noexcept
2259 = default;
2260
2261 template<typename = __detail::__months_years_conversion_disambiguator>
2262 friend constexpr year_month
2263 operator+(const year_month& __ym, const months& __dm) noexcept
2264 {
2265 // TODO: Optimize?
2266 auto __m = __ym.month() + __dm;
2267 auto __i = int(unsigned(__ym.month())) - 1 + __dm.count();
2268 auto __y = (__i < 0
2269 ? __ym.year() + years{(__i - 11) / 12}
2270 : __ym.year() + years{__i / 12});
2271 return __y / __m;
2272 }
2273
2274 template<typename = __detail::__months_years_conversion_disambiguator>
2275 friend constexpr year_month
2276 operator+(const months& __dm, const year_month& __ym) noexcept
2277 { return __ym + __dm; }
2278
2279 template<typename = __detail::__months_years_conversion_disambiguator>
2280 friend constexpr year_month
2281 operator-(const year_month& __ym, const months& __dm) noexcept
2282 { return __ym + -__dm; }
2283
2284 friend constexpr months
2285 operator-(const year_month& __x, const year_month& __y) noexcept
2286 {
2287 return (__x.year() - __y.year()
2288 + months{static_cast<int>(unsigned{__x.month()})
2289 - static_cast<int>(unsigned{__y.month()})});
2290 }
2291
2292 friend constexpr year_month
2293 operator+(const year_month& __ym, const years& __dy) noexcept
2294 { return (__ym.year() + __dy) / __ym.month(); }
2295
2296 friend constexpr year_month
2297 operator+(const years& __dy, const year_month& __ym) noexcept
2298 { return __ym + __dy; }
2299
2300 friend constexpr year_month
2301 operator-(const year_month& __ym, const years& __dy) noexcept
2302 { return __ym + -__dy; }
2303
2304 friend constexpr year_month
2305 operator/(const chrono::year& __y, const chrono::month& __m) noexcept
2306 { return {__y, __m}; }
2307
2308 friend constexpr year_month
2309 operator/(const chrono::year& __y, int __m) noexcept
2310 { return {__y, chrono::month(unsigned(__m))}; }
2311
2312 friend constexpr year_month_day
2313 operator/(const year_month& __ym, int __d) noexcept;
2314
2315 friend constexpr year_month_day_last
2316 operator/(const year_month& __ym, last_spec) noexcept;
2317
2318 // TODO: Implement operator<<, from_stream.
2319 };
2320
2321 // YEAR_MONTH_DAY
2322
2323 class year_month_day
2324 {
2325 private:
2326 chrono::year _M_y;
2327 chrono::month _M_m;
2328 chrono::day _M_d;
2329
2330 static constexpr year_month_day _S_from_days(const days& __dp) noexcept;
2331
2332 constexpr days _M_days_since_epoch() const noexcept;
2333
2334 public:
2335 year_month_day() = default;
2336
2337 constexpr
2338 year_month_day(const chrono::year& __y, const chrono::month& __m,
2339 const chrono::day& __d) noexcept
2340 : _M_y{__y}, _M_m{__m}, _M_d{__d}
2341 { }
2342
2343 constexpr
2344 year_month_day(const year_month_day_last& __ymdl) noexcept;
2345
2346 constexpr
2347 year_month_day(const sys_days& __dp) noexcept
2348 : year_month_day(_S_from_days(__dp.time_since_epoch()))
2349 { }
2350
2351 explicit constexpr
2352 year_month_day(const local_days& __dp) noexcept
2353 : year_month_day(sys_days{__dp.time_since_epoch()})
2354 { }
2355
2356 template<typename = __detail::__months_years_conversion_disambiguator>
2357 constexpr year_month_day&
2358 operator+=(const months& __m) noexcept
2359 {
2360 *this = *this + __m;
2361 return *this;
2362 }
2363
2364 template<typename = __detail::__months_years_conversion_disambiguator>
2365 constexpr year_month_day&
2366 operator-=(const months& __m) noexcept
2367 {
2368 *this = *this - __m;
2369 return *this;
2370 }
2371
2372 constexpr year_month_day&
2373 operator+=(const years& __y) noexcept
2374 {
2375 *this = *this + __y;
2376 return *this;
2377 }
2378
2379 constexpr year_month_day&
2380 operator-=(const years& __y) noexcept
2381 {
2382 *this = *this - __y;
2383 return *this;
2384 }
2385
2386 constexpr chrono::year
2387 year() const noexcept
2388 { return _M_y; }
2389
2390 constexpr chrono::month
2391 month() const noexcept
2392 { return _M_m; }
2393
2394 constexpr chrono::day
2395 day() const noexcept
2396 { return _M_d; }
2397
2398 constexpr
2399 operator sys_days() const noexcept
2400 { return sys_days{_M_days_since_epoch()}; }
2401
2402 explicit constexpr
2403 operator local_days() const noexcept
2404 { return local_days{sys_days{*this}.time_since_epoch()}; }
2405
2406 constexpr bool ok() const noexcept;
2407
2408 friend constexpr bool
2409 operator==(const year_month_day& __x, const year_month_day& __y) noexcept
2410 {
2411 return __x.year() == __y.year()
2412 && __x.month() == __y.month()
2413 && __x.day() == __y.day();
2414 }
2415
2416 friend constexpr strong_ordering
2417 operator<=>(const year_month_day& __x, const year_month_day& __y) noexcept
2418 = default;
2419
2420 template<typename = __detail::__months_years_conversion_disambiguator>
2421 friend constexpr year_month_day
2422 operator+(const year_month_day& __ymd, const months& __dm) noexcept
2423 { return (__ymd.year() / __ymd.month() + __dm) / __ymd.day(); }
2424
2425 template<typename = __detail::__months_years_conversion_disambiguator>
2426 friend constexpr year_month_day
2427 operator+(const months& __dm, const year_month_day& __ymd) noexcept
2428 { return __ymd + __dm; }
2429
2430 friend constexpr year_month_day
2431 operator+(const year_month_day& __ymd, const years& __dy) noexcept
2432 { return (__ymd.year() + __dy) / __ymd.month() / __ymd.day(); }
2433
2434 friend constexpr year_month_day
2435 operator+(const years& __dy, const year_month_day& __ymd) noexcept
2436 { return __ymd + __dy; }
2437
2438 template<typename = __detail::__months_years_conversion_disambiguator>
2439 friend constexpr year_month_day
2440 operator-(const year_month_day& __ymd, const months& __dm) noexcept
2441 { return __ymd + -__dm; }
2442
2443 friend constexpr year_month_day
2444 operator-(const year_month_day& __ymd, const years& __dy) noexcept
2445 { return __ymd + -__dy; }
2446
2447 friend constexpr year_month_day
2448 operator/(const year_month& __ym, const chrono::day& __d) noexcept
2449 { return {__ym.year(), __ym.month(), __d}; }
2450
2451 friend constexpr year_month_day
2452 operator/(const year_month& __ym, int __d) noexcept
2453 { return __ym / chrono::day{unsigned(__d)}; }
2454
2455 friend constexpr year_month_day
2456 operator/(const chrono::year& __y, const month_day& __md) noexcept
2457 { return __y / __md.month() / __md.day(); }
2458
2459 friend constexpr year_month_day
2460 operator/(int __y, const month_day& __md) noexcept
2461 { return chrono::year{__y} / __md; }
2462
2463 friend constexpr year_month_day
2464 operator/(const month_day& __md, const chrono::year& __y) noexcept
2465 { return __y / __md; }
2466
2467 friend constexpr year_month_day
2468 operator/(const month_day& __md, int __y) noexcept
2469 { return chrono::year(__y) / __md; }
2470
2471 // TODO: Implement operator<<, from_stream.
2472 };
2473
2474 // Construct from days since 1970/01/01.
2475 // Proposition 6.3 of Neri and Schneider,
2476 // "Euclidean Affine Functions and Applications to Calendar Algorithms".
2477 // https://arxiv.org/abs/2102.06959
2478 constexpr year_month_day
2479 year_month_day::_S_from_days(const days& __dp) noexcept
2480 {
2481 constexpr auto __z2 = static_cast<uint32_t>(-1468000);
2482 constexpr auto __r2_e3 = static_cast<uint32_t>(536895458);
2483
2484 const auto __r0 = static_cast<uint32_t>(__dp.count()) + __r2_e3;
2485
2486 const auto __n1 = 4 * __r0 + 3;
2487 const auto __q1 = __n1 / 146097;
2488 const auto __r1 = __n1 % 146097 / 4;
2489
2490 constexpr auto __p32 = static_cast<uint64_t>(1) << 32;
2491 const auto __n2 = 4 * __r1 + 3;
2492 const auto __u2 = static_cast<uint64_t>(2939745) * __n2;
2493 const auto __q2 = static_cast<uint32_t>(__u2 / __p32);
2494 const auto __r2 = static_cast<uint32_t>(__u2 % __p32) / 2939745 / 4;
2495
2496 constexpr auto __p16 = static_cast<uint32_t>(1) << 16;
2497 const auto __n3 = 2141 * __r2 + 197913;
2498 const auto __q3 = __n3 / __p16;
2499 const auto __r3 = __n3 % __p16 / 2141;
2500
2501 const auto __y0 = 100 * __q1 + __q2;
2502 const auto __m0 = __q3;
2503 const auto __d0 = __r3;
2504
2505 const auto __j = __r2 >= 306;
2506 const auto __y1 = __y0 + __j;
2507 const auto __m1 = __j ? __m0 - 12 : __m0;
2508 const auto __d1 = __d0 + 1;
2509
2510 return year_month_day{chrono::year{static_cast<int>(__y1 + __z2)},
2511 chrono::month{__m1}, chrono::day{__d1}};
2512 }
2513
2514 // Days since 1970/01/01.
2515 // Proposition 6.2 of Neri and Schneider,
2516 // "Euclidean Affine Functions and Applications to Calendar Algorithms".
2517 // https://arxiv.org/abs/2102.06959
2518 constexpr days
2519 year_month_day::_M_days_since_epoch() const noexcept
2520 {
2521 auto constexpr __z2 = static_cast<uint32_t>(-1468000);
2522 auto constexpr __r2_e3 = static_cast<uint32_t>(536895458);
2523
2524 const auto __y1 = static_cast<uint32_t>(static_cast<int>(_M_y)) - __z2;
2525 const auto __m1 = static_cast<uint32_t>(static_cast<unsigned>(_M_m));
2526 const auto __d1 = static_cast<uint32_t>(static_cast<unsigned>(_M_d));
2527
2528 const auto __j = static_cast<uint32_t>(__m1 < 3);
2529 const auto __y0 = __y1 - __j;
2530 const auto __m0 = __j ? __m1 + 12 : __m1;
2531 const auto __d0 = __d1 - 1;
2532
2533 const auto __q1 = __y0 / 100;
2534 const auto __yc = 1461 * __y0 / 4 - __q1 + __q1 / 4;
2535 const auto __mc = (979 *__m0 - 2919) / 32;
2536 const auto __dc = __d0;
2537
2538 return days{static_cast<int32_t>(__yc + __mc + __dc - __r2_e3)};
2539 }
2540
2541 // YEAR_MONTH_DAY_LAST
2542
2543 class year_month_day_last
2544 {
2545 private:
2546 chrono::year _M_y;
2547 chrono::month_day_last _M_mdl;
2548
2549 public:
2550 constexpr
2551 year_month_day_last(const chrono::year& __y,
2552 const chrono::month_day_last& __mdl) noexcept
2553 : _M_y{__y}, _M_mdl{__mdl}
2554 { }
2555
2556 template<typename = __detail::__months_years_conversion_disambiguator>
2557 constexpr year_month_day_last&
2558 operator+=(const months& __m) noexcept
2559 {
2560 *this = *this + __m;
2561 return *this;
2562 }
2563
2564 template<typename = __detail::__months_years_conversion_disambiguator>
2565 constexpr year_month_day_last&
2566 operator-=(const months& __m) noexcept
2567 {
2568 *this = *this - __m;
2569 return *this;
2570 }
2571
2572 constexpr year_month_day_last&
2573 operator+=(const years& __y) noexcept
2574 {
2575 *this = *this + __y;
2576 return *this;
2577 }
2578
2579 constexpr year_month_day_last&
2580 operator-=(const years& __y) noexcept
2581 {
2582 *this = *this - __y;
2583 return *this;
2584 }
2585
2586 constexpr chrono::year
2587 year() const noexcept
2588 { return _M_y; }
2589
2590 constexpr chrono::month
2591 month() const noexcept
2592 { return _M_mdl.month(); }
2593
2594 constexpr chrono::month_day_last
2595 month_day_last() const noexcept
2596 { return _M_mdl; }
2597
2598 // Return A day representing the last day of this year, month pair.
2599 constexpr chrono::day
2600 day() const noexcept
2601 {
2602 const auto __m = static_cast<unsigned>(month());
2603
2604 // Excluding February, the last day of month __m is either 30 or 31 or,
2605 // in another words, it is 30 + b = 30 | b, where b is in {0, 1}.
2606
2607 // If __m in {1, 3, 4, 5, 6, 7}, then b is 1 if, and only if __m is odd.
2608 // Hence, b = __m & 1 = (__m ^ 0) & 1.
2609
2610 // If __m in {8, 9, 10, 11, 12}, then b is 1 if, and only if __m is even.
2611 // Hence, b = (__m ^ 1) & 1.
2612
2613 // Therefore, b = (__m ^ c) & 1, where c = 0, if __m < 8, or c = 1 if
2614 // __m >= 8, that is, c = __m >> 3.
2615
2616 // The above mathematically justifies this implementation whose
2617 // performance does not depend on look-up tables being on the L1 cache.
2618 return chrono::day{__m != 2 ? ((__m ^ (__m >> 3)) & 1) | 30
2619 : _M_y.is_leap() ? 29 : 28};
2620 }
2621
2622 constexpr
2623 operator sys_days() const noexcept
2624 { return sys_days{year() / month() / day()}; }
2625
2626 explicit constexpr
2627 operator local_days() const noexcept
2628 { return local_days{sys_days{*this}.time_since_epoch()}; }
2629
2630 constexpr bool
2631 ok() const noexcept
2632 { return _M_y.ok() && _M_mdl.ok(); }
2633
2634 friend constexpr bool
2635 operator==(const year_month_day_last& __x,
2636 const year_month_day_last& __y) noexcept
2637 {
2638 return __x.year() == __y.year()
2639 && __x.month_day_last() == __y.month_day_last();
2640 }
2641
2642 friend constexpr strong_ordering
2643 operator<=>(const year_month_day_last& __x,
2644 const year_month_day_last& __y) noexcept
2645 = default;
2646
2647 template<typename = __detail::__months_years_conversion_disambiguator>
2648 friend constexpr year_month_day_last
2649 operator+(const year_month_day_last& __ymdl,
2650 const months& __dm) noexcept
2651 { return (__ymdl.year() / __ymdl.month() + __dm) / last; }
2652
2653 template<typename = __detail::__months_years_conversion_disambiguator>
2654 friend constexpr year_month_day_last
2655 operator+(const months& __dm,
2656 const year_month_day_last& __ymdl) noexcept
2657 { return __ymdl + __dm; }
2658
2659 template<typename = __detail::__months_years_conversion_disambiguator>
2660 friend constexpr year_month_day_last
2661 operator-(const year_month_day_last& __ymdl,
2662 const months& __dm) noexcept
2663 { return __ymdl + -__dm; }
2664
2665 friend constexpr year_month_day_last
2666 operator+(const year_month_day_last& __ymdl,
2667 const years& __dy) noexcept
2668 { return {__ymdl.year() + __dy, __ymdl.month_day_last()}; }
2669
2670 friend constexpr year_month_day_last
2671 operator+(const years& __dy,
2672 const year_month_day_last& __ymdl) noexcept
2673 { return __ymdl + __dy; }
2674
2675 friend constexpr year_month_day_last
2676 operator-(const year_month_day_last& __ymdl,
2677 const years& __dy) noexcept
2678 { return __ymdl + -__dy; }
2679
2680 friend constexpr year_month_day_last
2681 operator/(const year_month& __ym, last_spec) noexcept
2682 { return {__ym.year(), chrono::month_day_last{__ym.month()}}; }
2683
2684 friend constexpr year_month_day_last
2685 operator/(const chrono::year& __y,
2686 const chrono::month_day_last& __mdl) noexcept
2687 { return {__y, __mdl}; }
2688
2689 friend constexpr year_month_day_last
2690 operator/(int __y, const chrono::month_day_last& __mdl) noexcept
2691 { return chrono::year(__y) / __mdl; }
2692
2693 friend constexpr year_month_day_last
2694 operator/(const chrono::month_day_last& __mdl,
2695 const chrono::year& __y) noexcept
2696 { return __y / __mdl; }
2697
2698 friend constexpr year_month_day_last
2699 operator/(const chrono::month_day_last& __mdl, int __y) noexcept
2700 { return chrono::year(__y) / __mdl; }
2701
2702 // TODO: Implement operator<<.
2703 };
2704
2705 // year_month_day ctor from year_month_day_last
2706 constexpr
2707 year_month_day::year_month_day(const year_month_day_last& __ymdl) noexcept
2708 : _M_y{__ymdl.year()}, _M_m{__ymdl.month()}, _M_d{__ymdl.day()}
2709 { }
2710
2711 constexpr bool
2712 year_month_day::ok() const noexcept
2713 {
2714 if (!_M_y.ok() || !_M_m.ok())
2715 return false;
2716 return chrono::day{1} <= _M_d && _M_d <= (_M_y / _M_m / last).day();
2717 }
2718
2719 // YEAR_MONTH_WEEKDAY
2720
2721 class year_month_weekday
2722 {
2723 private:
2724 chrono::year _M_y;
2725 chrono::month _M_m;
2726 chrono::weekday_indexed _M_wdi;
2727
2728 static constexpr year_month_weekday
2729 _S_from_sys_days(const sys_days& __dp)
2730 {
2731 year_month_day __ymd{__dp};
2732 chrono::weekday __wd{__dp};
2733 auto __index = __wd[(unsigned{__ymd.day()} - 1) / 7 + 1];
2734 return {__ymd.year(), __ymd.month(), __index};
2735 }
2736
2737 public:
2738 year_month_weekday() = default;
2739
2740 constexpr
2741 year_month_weekday(const chrono::year& __y, const chrono::month& __m,
2742 const chrono::weekday_indexed& __wdi) noexcept
2743 : _M_y{__y}, _M_m{__m}, _M_wdi{__wdi}
2744 { }
2745
2746 constexpr
2747 year_month_weekday(const sys_days& __dp) noexcept
2748 : year_month_weekday{_S_from_sys_days(__dp)}
2749 { }
2750
2751 explicit constexpr
2752 year_month_weekday(const local_days& __dp) noexcept
2753 : year_month_weekday{sys_days{__dp.time_since_epoch()}}
2754 { }
2755
2756 template<typename = __detail::__months_years_conversion_disambiguator>
2757 constexpr year_month_weekday&
2758 operator+=(const months& __m) noexcept
2759 {
2760 *this = *this + __m;
2761 return *this;
2762 }
2763
2764 template<typename = __detail::__months_years_conversion_disambiguator>
2765 constexpr year_month_weekday&
2766 operator-=(const months& __m) noexcept
2767 {
2768 *this = *this - __m;
2769 return *this;
2770 }
2771
2772 constexpr year_month_weekday&
2773 operator+=(const years& __y) noexcept
2774 {
2775 *this = *this + __y;
2776 return *this;
2777 }
2778
2779 constexpr year_month_weekday&
2780 operator-=(const years& __y) noexcept
2781 {
2782 *this = *this - __y;
2783 return *this;
2784 }
2785
2786 constexpr chrono::year
2787 year() const noexcept
2788 { return _M_y; }
2789
2790 constexpr chrono::month
2791 month() const noexcept
2792 { return _M_m; }
2793
2794 constexpr chrono::weekday
2795 weekday() const noexcept
2796 { return _M_wdi.weekday(); }
2797
2798 constexpr unsigned
2799 index() const noexcept
2800 { return _M_wdi.index(); }
2801
2802 constexpr chrono::weekday_indexed
2803 weekday_indexed() const noexcept
2804 { return _M_wdi; }
2805
2806 constexpr
2807 operator sys_days() const noexcept
2808 {
2809 auto __d = sys_days{year() / month() / 1};
2810 return __d + (weekday() - chrono::weekday(__d)
2811 + days{(static_cast<int>(index())-1)*7});
2812 }
2813
2814 explicit constexpr
2815 operator local_days() const noexcept
2816 { return local_days{sys_days{*this}.time_since_epoch()}; }
2817
2818 constexpr bool
2819 ok() const noexcept
2820 {
2821 if (!_M_y.ok() || !_M_m.ok() || !_M_wdi.ok())
2822 return false;
2823 if (_M_wdi.index() <= 4)
2824 return true;
2825 days __d = (_M_wdi.weekday()
2826 - chrono::weekday{sys_days{_M_y / _M_m / 1}}
2827 + days((_M_wdi.index()-1)*7 + 1));
2828 __glibcxx_assert(__d.count() >= 1);
2829 return __d.count() <= unsigned{(_M_y / _M_m / last).day()};
2830 }
2831
2832 friend constexpr bool
2833 operator==(const year_month_weekday& __x,
2834 const year_month_weekday& __y) noexcept
2835 {
2836 return __x.year() == __y.year()
2837 && __x.month() == __y.month()
2838 && __x.weekday_indexed() == __y.weekday_indexed();
2839 }
2840
2841 template<typename = __detail::__months_years_conversion_disambiguator>
2842 friend constexpr year_month_weekday
2843 operator+(const year_month_weekday& __ymwd, const months& __dm) noexcept
2844 {
2845 return ((__ymwd.year() / __ymwd.month() + __dm)
2846 / __ymwd.weekday_indexed());
2847 }
2848
2849 template<typename = __detail::__months_years_conversion_disambiguator>
2850 friend constexpr year_month_weekday
2851 operator+(const months& __dm, const year_month_weekday& __ymwd) noexcept
2852 { return __ymwd + __dm; }
2853
2854 friend constexpr year_month_weekday
2855 operator+(const year_month_weekday& __ymwd, const years& __dy) noexcept
2856 { return {__ymwd.year() + __dy, __ymwd.month(), __ymwd.weekday_indexed()}; }
2857
2858 friend constexpr year_month_weekday
2859 operator+(const years& __dy, const year_month_weekday& __ymwd) noexcept
2860 { return __ymwd + __dy; }
2861
2862 template<typename = __detail::__months_years_conversion_disambiguator>
2863 friend constexpr year_month_weekday
2864 operator-(const year_month_weekday& __ymwd, const months& __dm) noexcept
2865 { return __ymwd + -__dm; }
2866
2867 friend constexpr year_month_weekday
2868 operator-(const year_month_weekday& __ymwd, const years& __dy) noexcept
2869 { return __ymwd + -__dy; }
2870
2871 friend constexpr year_month_weekday
2872 operator/(const year_month& __ym,
2873 const chrono::weekday_indexed& __wdi) noexcept
2874 { return {__ym.year(), __ym.month(), __wdi}; }
2875
2876 friend constexpr year_month_weekday
2877 operator/(const chrono::year& __y, const month_weekday& __mwd) noexcept
2878 { return {__y, __mwd.month(), __mwd.weekday_indexed()}; }
2879
2880 friend constexpr year_month_weekday
2881 operator/(int __y, const month_weekday& __mwd) noexcept
2882 { return chrono::year(__y) / __mwd; }
2883
2884 friend constexpr year_month_weekday
2885 operator/(const month_weekday& __mwd, const chrono::year& __y) noexcept
2886 { return __y / __mwd; }
2887
2888 friend constexpr year_month_weekday
2889 operator/(const month_weekday& __mwd, int __y) noexcept
2890 { return chrono::year(__y) / __mwd; }
2891
2892 // TODO: Implement operator<<.
2893 };
2894
2895 // YEAR_MONTH_WEEKDAY_LAST
2896
2897 class year_month_weekday_last
2898 {
2899 private:
2900 chrono::year _M_y;
2901 chrono::month _M_m;
2902 chrono::weekday_last _M_wdl;
2903
2904 public:
2905 constexpr
2906 year_month_weekday_last(const chrono::year& __y, const chrono::month& __m,
2907 const chrono::weekday_last& __wdl) noexcept
2908 : _M_y{__y}, _M_m{__m}, _M_wdl{__wdl}
2909 { }
2910
2911 template<typename = __detail::__months_years_conversion_disambiguator>
2912 constexpr year_month_weekday_last&
2913 operator+=(const months& __m) noexcept
2914 {
2915 *this = *this + __m;
2916 return *this;
2917 }
2918
2919 template<typename = __detail::__months_years_conversion_disambiguator>
2920 constexpr year_month_weekday_last&
2921 operator-=(const months& __m) noexcept
2922 {
2923 *this = *this - __m;
2924 return *this;
2925 }
2926
2927 constexpr year_month_weekday_last&
2928 operator+=(const years& __y) noexcept
2929 {
2930 *this = *this + __y;
2931 return *this;
2932 }
2933
2934 constexpr year_month_weekday_last&
2935 operator-=(const years& __y) noexcept
2936 {
2937 *this = *this - __y;
2938 return *this;
2939 }
2940
2941 constexpr chrono::year
2942 year() const noexcept
2943 { return _M_y; }
2944
2945 constexpr chrono::month
2946 month() const noexcept
2947 { return _M_m; }
2948
2949 constexpr chrono::weekday
2950 weekday() const noexcept
2951 { return _M_wdl.weekday(); }
2952
2953 constexpr chrono::weekday_last
2954 weekday_last() const noexcept
2955 { return _M_wdl; }
2956
2957 constexpr
2958 operator sys_days() const noexcept
2959 {
2960 const auto __d = sys_days{_M_y / _M_m / last};
2961 return sys_days{(__d - (chrono::weekday{__d}
2962 - _M_wdl.weekday())).time_since_epoch()};
2963 }
2964
2965 explicit constexpr
2966 operator local_days() const noexcept
2967 { return local_days{sys_days{*this}.time_since_epoch()}; }
2968
2969 constexpr bool
2970 ok() const noexcept
2971 { return _M_y.ok() && _M_m.ok() && _M_wdl.ok(); }
2972
2973 friend constexpr bool
2974 operator==(const year_month_weekday_last& __x,
2975 const year_month_weekday_last& __y) noexcept
2976 {
2977 return __x.year() == __y.year()
2978 && __x.month() == __y.month()
2979 && __x.weekday_last() == __y.weekday_last();
2980 }
2981
2982 template<typename = __detail::__months_years_conversion_disambiguator>
2983 friend constexpr year_month_weekday_last
2984 operator+(const year_month_weekday_last& __ymwdl,
2985 const months& __dm) noexcept
2986 {
2987 return ((__ymwdl.year() / __ymwdl.month() + __dm)
2988 / __ymwdl.weekday_last());
2989 }
2990
2991 template<typename = __detail::__months_years_conversion_disambiguator>
2992 friend constexpr year_month_weekday_last
2993 operator+(const months& __dm,
2994 const year_month_weekday_last& __ymwdl) noexcept
2995 { return __ymwdl + __dm; }
2996
2997 friend constexpr year_month_weekday_last
2998 operator+(const year_month_weekday_last& __ymwdl,
2999 const years& __dy) noexcept
3000 { return {__ymwdl.year() + __dy, __ymwdl.month(), __ymwdl.weekday_last()}; }
3001
3002 friend constexpr year_month_weekday_last
3003 operator+(const years& __dy,
3004 const year_month_weekday_last& __ymwdl) noexcept
3005 { return __ymwdl + __dy; }
3006
3007 template<typename = __detail::__months_years_conversion_disambiguator>
3008 friend constexpr year_month_weekday_last
3009 operator-(const year_month_weekday_last& __ymwdl,
3010 const months& __dm) noexcept
3011 { return __ymwdl + -__dm; }
3012
3013 friend constexpr year_month_weekday_last
3014 operator-(const year_month_weekday_last& __ymwdl,
3015 const years& __dy) noexcept
3016 { return __ymwdl + -__dy; }
3017
3018 friend constexpr year_month_weekday_last
3019 operator/(const year_month& __ym,
3020 const chrono::weekday_last& __wdl) noexcept
3021 { return {__ym.year(), __ym.month(), __wdl}; }
3022
3023 friend constexpr year_month_weekday_last
3024 operator/(const chrono::year& __y,
3025 const chrono::month_weekday_last& __mwdl) noexcept
3026 { return {__y, __mwdl.month(), __mwdl.weekday_last()}; }
3027
3028 friend constexpr year_month_weekday_last
3029 operator/(int __y, const chrono::month_weekday_last& __mwdl) noexcept
3030 { return chrono::year(__y) / __mwdl; }
3031
3032 friend constexpr year_month_weekday_last
3033 operator/(const chrono::month_weekday_last& __mwdl,
3034 const chrono::year& __y) noexcept
3035 { return __y / __mwdl; }
3036
3037 friend constexpr year_month_weekday_last
3038 operator/(const chrono::month_weekday_last& __mwdl, int __y) noexcept
3039 { return chrono::year(__y) / __mwdl; }
3040
3041 // TODO: Implement operator<<.
3042 };
3043
3044 // HH_MM_SS
3045
3046 namespace __detail
3047 {
3048 consteval long long
3049 __pow10(unsigned __n)
3050 {
3051 long long __r = 1;
3052 while (__n-- > 0)
3053 __r *= 10;
3054 return __r;
3055 }
3056 }
3057
3058 template<typename _Duration>
3059 class hh_mm_ss
3060 {
3061 private:
3062 static constexpr int
3063 _S_fractional_width()
3064 {
3065 int __multiplicity_2 = 0;
3066 int __multiplicity_5 = 0;
3067 auto __den = _Duration::period::den;
3068 while ((__den % 2) == 0)
3069 {
3070 ++__multiplicity_2;
3071 __den /= 2;
3072 }
3073 while ((__den % 5) == 0)
3074 {
3075 ++__multiplicity_5;
3076 __den /= 5;
3077 }
3078 if (__den != 1)
3079 return 6;
3080
3081 int __width = (__multiplicity_2 > __multiplicity_5
3082 ? __multiplicity_2 : __multiplicity_5);
3083 if (__width > 18)
3084 __width = 18;
3085 return __width;
3086 }
3087
3088 constexpr
3089 hh_mm_ss(_Duration __d, bool __is_neg)
3090 : _M_is_neg(__is_neg),
3091 _M_h (duration_cast<chrono::hours>(__d)),
3092 _M_m (duration_cast<chrono::minutes>(__d - hours())),
3093 _M_s (duration_cast<chrono::seconds>(__d - hours() - minutes()))
3094 {
3095 auto __ss = __d - hours() - minutes() - seconds();
3096 if constexpr (treat_as_floating_point_v<typename precision::rep>)
3097 _M_ss = __ss;
3098 else
3099 _M_ss = duration_cast<precision>(__ss);
3100 }
3101
3102 static constexpr _Duration
3103 _S_abs(_Duration __d)
3104 {
3105 if constexpr (numeric_limits<typename _Duration::rep>::is_signed)
3106 return chrono::abs(__d);
3107 else
3108 return __d;
3109 }
3110
3111 public:
3112 static constexpr unsigned fractional_width = {_S_fractional_width()};
3113
3114 using precision
3115 = duration<common_type_t<typename _Duration::rep,
3116 chrono::seconds::rep>,
3117 ratio<1, __detail::__pow10(fractional_width)>>;
3118
3119 constexpr
3120 hh_mm_ss() noexcept
3121 : hh_mm_ss{_Duration::zero()}
3122 { }
3123
3124 constexpr explicit
3125 hh_mm_ss(_Duration __d)
3126 : hh_mm_ss(_S_abs(__d), __d < _Duration::zero())
3127 { }
3128
3129 constexpr bool
3130 is_negative() const noexcept
3131 { return _M_is_neg; }
3132
3133 constexpr chrono::hours
3134 hours() const noexcept
3135 { return _M_h; }
3136
3137 constexpr chrono::minutes
3138 minutes() const noexcept
3139 { return _M_m; }
3140
3141 constexpr chrono::seconds
3142 seconds() const noexcept
3143 { return _M_s; }
3144
3145 constexpr precision
3146 subseconds() const noexcept
3147 { return _M_ss; }
3148
3149 constexpr explicit
3150 operator precision() const noexcept
3151 { return to_duration(); }
3152
3153 constexpr precision
3154 to_duration() const noexcept
3155 {
3156 if (_M_is_neg)
3157 return -(_M_h + _M_m + _M_s + _M_ss);
3158 else
3159 return _M_h + _M_m + _M_s + _M_ss;
3160 }
3161
3162 // TODO: Implement operator<<.
3163
3164 private:
3165 bool _M_is_neg;
3166 chrono::hours _M_h;
3167 chrono::minutes _M_m;
3168 chrono::seconds _M_s;
3169 precision _M_ss;
3170 };
3171#endif // C++20
3172
3173 /// @} group chrono
3174 } // namespace chrono
3175
3176#if __cplusplus > 201103L
3177
3178#define __cpp_lib_chrono_udls 201304
3179
3180 inline namespace literals
3181 {
3182 /** ISO C++ 2014 namespace for suffixes for duration literals.
3183 *
3184 * These suffixes can be used to create `chrono::duration` values with
3185 * tick periods of hours, minutes, seconds, milliseconds, microseconds
3186 * or nanoseconds. For example, `std::chrono::seconds(5)` can be written
3187 * as `5s` after making the suffix visible in the current scope.
3188 * The suffixes can be made visible by a using-directive or
3189 * using-declaration such as:
3190 * - `using namespace std::chrono_literals;`
3191 * - `using namespace std::literals;`
3192 * - `using namespace std::chrono;`
3193 * - `using namespace std;`
3194 * - `using std::chrono_literals::operator""s;`
3195 *
3196 * The result of these suffixes on an integer literal is one of the
3197 * standard typedefs such as `std::chrono::hours`.
3198 * The result on a floating-point literal is a duration type with the
3199 * specified tick period and an unspecified floating-point representation,
3200 * for example `1.5e2ms` might be equivalent to
3201 * `chrono::duration<long double, chrono::milli>(1.5e2)`.
3202 *
3203 * @ingroup chrono
3204 */
3205 inline namespace chrono_literals
3206 {
3207 /// @addtogroup chrono
3208 /// @{
3209
3210#pragma GCC diagnostic push
3211#pragma GCC diagnostic ignored "-Wliteral-suffix"
3212 /// @cond undocumented
3213 template<typename _Dur, char... _Digits>
3214 constexpr _Dur __check_overflow()
3215 {
3216 using _Val = __parse_int::_Parse_int<_Digits...>;
3217 constexpr typename _Dur::rep __repval = _Val::value;
3218 static_assert(__repval >= 0 && __repval == _Val::value,
3219 "literal value cannot be represented by duration type");
3220 return _Dur(__repval);
3221 }
3222 /// @endcond
3223
3224 /// Literal suffix for durations representing non-integer hours
3225 constexpr chrono::duration<long double, ratio<3600,1>>
3226 operator""h(long double __hours)
3227 { return chrono::duration<long double, ratio<3600,1>>{__hours}; }
3228
3229 /// Literal suffix for durations of type `std::chrono::hours`
3230 template <char... _Digits>
3231 constexpr chrono::hours
3232 operator""h()
3233 { return __check_overflow<chrono::hours, _Digits...>(); }
3234
3235 /// Literal suffix for durations representing non-integer minutes
3236 constexpr chrono::duration<long double, ratio<60,1>>
3237 operator""min(long double __mins)
3238 { return chrono::duration<long double, ratio<60,1>>{__mins}; }
3239
3240 /// Literal suffix for durations of type `std::chrono::minutes`
3241 template <char... _Digits>
3242 constexpr chrono::minutes
3243 operator""min()
3244 { return __check_overflow<chrono::minutes, _Digits...>(); }
3245
3246 /// Literal suffix for durations representing non-integer seconds
3247 constexpr chrono::duration<long double>
3248 operator""s(long double __secs)
3249 { return chrono::duration<long double>{__secs}; }
3250
3251 /// Literal suffix for durations of type `std::chrono::seconds`
3252 template <char... _Digits>
3253 constexpr chrono::seconds
3254 operator""s()
3255 { return __check_overflow<chrono::seconds, _Digits...>(); }
3256
3257 /// Literal suffix for durations representing non-integer milliseconds
3258 constexpr chrono::duration<long double, milli>
3259 operator""ms(long double __msecs)
3260 { return chrono::duration<long double, milli>{__msecs}; }
3261
3262 /// Literal suffix for durations of type `std::chrono::milliseconds`
3263 template <char... _Digits>
3264 constexpr chrono::milliseconds
3265 operator""ms()
3266 { return __check_overflow<chrono::milliseconds, _Digits...>(); }
3267
3268 /// Literal suffix for durations representing non-integer microseconds
3269 constexpr chrono::duration<long double, micro>
3270 operator""us(long double __usecs)
3271 { return chrono::duration<long double, micro>{__usecs}; }
3272
3273 /// Literal suffix for durations of type `std::chrono::microseconds`
3274 template <char... _Digits>
3275 constexpr chrono::microseconds
3276 operator""us()
3277 { return __check_overflow<chrono::microseconds, _Digits...>(); }
3278
3279 /// Literal suffix for durations representing non-integer nanoseconds
3280 constexpr chrono::duration<long double, nano>
3281 operator""ns(long double __nsecs)
3282 { return chrono::duration<long double, nano>{__nsecs}; }
3283
3284 /// Literal suffix for durations of type `std::chrono::nanoseconds`
3285 template <char... _Digits>
3286 constexpr chrono::nanoseconds
3287 operator""ns()
3288 { return __check_overflow<chrono::nanoseconds, _Digits...>(); }
3289
3290#if __cplusplus > 201703L
3291 constexpr chrono::day
3292 operator""d(unsigned long long __d) noexcept
3293 { return chrono::day{static_cast<unsigned>(__d)}; }
3294
3295 constexpr chrono::year
3296 operator""y(unsigned long long __y) noexcept
3297 { return chrono::year{static_cast<int>(__y)}; }
3298#endif // C++20
3299
3300#pragma GCC diagnostic pop
3301 /// @}
3302 } // inline namespace chrono_literals
3303 } // inline namespace literals
3304
3305 namespace chrono
3306 {
3307 using namespace literals::chrono_literals;
3308 } // namespace chrono
3309
3310#if __cplusplus > 201703L
3311 namespace chrono
3312 {
3313 /// @addtogroup chrono
3314 /// @{
3315
3316 // 12/24 HOURS FUNCTIONS
3317
3318 constexpr bool
3319 is_am(const hours& __h) noexcept
3320 { return 0h <= __h && __h <= 11h; }
3321
3322 constexpr bool
3323 is_pm(const hours& __h) noexcept
3324 { return 12h <= __h && __h <= 23h; }
3325
3326 constexpr hours
3327 make12(const hours& __h) noexcept
3328 {
3329 if (__h == 0h)
3330 return 12h;
3331 else if (__h > 12h)
3332 return __h - 12h;
3333 return __h;
3334 }
3335
3336 constexpr hours
3337 make24(const hours& __h, bool __is_pm) noexcept
3338 {
3339 if (!__is_pm)
3340 {
3341 if (__h == 12h)
3342 return 0h;
3343 else
3344 return __h;
3345 }
3346 else
3347 {
3348 if (__h == 12h)
3349 return __h;
3350 else
3351 return __h + 12h;
3352 }
3353 }
3354 /// @} group chrono
3355 } // namespace chrono
3356#endif
3357
3358#if __cplusplus >= 201703L
3359 namespace filesystem
3360 {
3361 struct __file_clock
3362 {
3363 using duration = chrono::nanoseconds;
3364 using rep = duration::rep;
3365 using period = duration::period;
3366 using time_point = chrono::time_point<__file_clock>;
3367 static constexpr bool is_steady = false;
3368
3369 static time_point
3370 now() noexcept
3371 { return _S_from_sys(chrono::system_clock::now()); }
3372
3373#if __cplusplus > 201703L
3374 template<typename _Dur>
3375 static
3376 chrono::file_time<_Dur>
3377 from_sys(const chrono::sys_time<_Dur>& __t) noexcept
3378 { return _S_from_sys(__t); }
3379
3380 // For internal use only
3381 template<typename _Dur>
3382 static
3383 chrono::sys_time<_Dur>
3384 to_sys(const chrono::file_time<_Dur>& __t) noexcept
3385 { return _S_to_sys(__t); }
3386#endif // C++20
3387
3388 private:
3389 using __sys_clock = chrono::system_clock;
3390
3391 // This clock's (unspecified) epoch is 2174-01-01 00:00:00 UTC.
3392 // A signed 64-bit duration with nanosecond resolution gives roughly
3393 // +/- 292 years, which covers the 1901-2446 date range for ext4.
3394 static constexpr chrono::seconds _S_epoch_diff{6437664000};
3395
3396 protected:
3397 // For internal use only
3398 template<typename _Dur>
3399 static
3400 chrono::time_point<__file_clock, _Dur>
3401 _S_from_sys(const chrono::time_point<__sys_clock, _Dur>& __t) noexcept
3402 {
3403 using __file_time = chrono::time_point<__file_clock, _Dur>;
3404 return __file_time{__t.time_since_epoch()} - _S_epoch_diff;
3405 }
3406
3407 // For internal use only
3408 template<typename _Dur>
3409 static
3410 chrono::time_point<__sys_clock, _Dur>
3411 _S_to_sys(const chrono::time_point<__file_clock, _Dur>& __t) noexcept
3412 {
3413 using __sys_time = chrono::time_point<__sys_clock, _Dur>;
3414 return __sys_time{__t.time_since_epoch()} + _S_epoch_diff;
3415 }
3416 };
3417 } // namespace filesystem
3418#endif // C++17
3419#endif // C++14
3420
3421_GLIBCXX_END_NAMESPACE_VERSION
3422} // namespace std
3423
3424#endif // C++11
3425
3426#endif //_GLIBCXX_CHRONO