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