libstdc++
chrono
Go to the documentation of this file.
1// <chrono> -*- C++ -*-
2
3// Copyright (C) 2008-2025 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// [time]
26
27/** @file include/chrono
28 * This is a Standard C++ Library header.
29 * @ingroup chrono
30 */
31
32#ifndef _GLIBCXX_CHRONO
33#define _GLIBCXX_CHRONO 1
34
35#ifdef _GLIBCXX_SYSHDR
36#pragma GCC system_header
37#endif
38
39#ifdef _GLIBCXX_NO_FREESTANDING_CHRONO
40# include <bits/requires_hosted.h> // for <ctime> and clocks
41#endif
42
43#if __cplusplus < 201103L
44# include <bits/c++0x_warning.h>
45#else
46
47#define __glibcxx_want_chrono
48#define __glibcxx_want_chrono_udls
49#include <bits/version.h>
50
51#include <bits/chrono.h>
52
53#if __cpp_lib_bitops >= 201907L
54# include <bit> // __countr_zero
55#endif
56#ifdef __glibcxx_chrono_cxx20
57# include <bits/stl_algo.h> // upper_bound
58# include <bits/range_access.h> // begin/end for arrays
59#endif
60#if __cpp_lib_chrono >= 201803L // C++20 && HOSTED && USE_CXX11_ABI
61# include <sstream>
62# include <string>
63# include <vector>
64# include <bits/shared_ptr.h>
65# include <bits/unique_ptr.h>
66#endif
67#if __glibcxx_chrono_cxx20 >= 202306L // C++26
68# include <bits/functional_hash.h>
69#endif
70
71namespace std _GLIBCXX_VISIBILITY(default)
72{
73_GLIBCXX_BEGIN_NAMESPACE_VERSION
74
75 /**
76 * @defgroup chrono Time
77 * @ingroup utilities
78 *
79 * Classes and functions for time.
80 *
81 * @since C++11
82 */
83
84 /** @namespace std::chrono
85 * @brief ISO C++ 2011 namespace for date and time utilities
86 * @ingroup chrono
87 */
88 namespace chrono
89 {
90#ifdef __glibcxx_chrono_cxx20
91 /// @addtogroup chrono
92 /// @{
93 struct local_t { };
94 template<typename _Duration>
95 using local_time = time_point<local_t, _Duration>;
96 using local_seconds = local_time<seconds>;
97 using local_days = local_time<days>;
98
99#if _GLIBCXX_HOSTED
100 class utc_clock;
101 class tai_clock;
102 class gps_clock;
103
104 template<typename _Duration>
105 using utc_time = time_point<utc_clock, _Duration>;
106 using utc_seconds = utc_time<seconds>;
107
108 template<typename _Duration>
109 using tai_time = time_point<tai_clock, _Duration>;
110 using tai_seconds = tai_time<seconds>;
111
112 template<typename _Duration>
113 using gps_time = time_point<gps_clock, _Duration>;
114 using gps_seconds = gps_time<seconds>;
115
116 template<> struct is_clock<utc_clock> : true_type { };
117 template<> struct is_clock<tai_clock> : true_type { };
118 template<> struct is_clock<gps_clock> : true_type { };
119
120 template<> inline constexpr bool is_clock_v<utc_clock> = true;
121 template<> inline constexpr bool is_clock_v<tai_clock> = true;
122 template<> inline constexpr bool is_clock_v<gps_clock> = true;
123
124 struct leap_second_info
125 {
126 bool is_leap_second;
127 seconds elapsed;
128 };
129
130 template<typename _Duration>
131 leap_second_info
132 get_leap_second_info(const utc_time<_Duration>& __ut);
133
134 /** A clock that measures Universal Coordinated Time (UTC).
135 *
136 * The epoch is 1970-01-01 00:00:00.
137 *
138 * @since C++20
139 */
141 {
142 public:
143 using rep = system_clock::rep;
144 using period = system_clock::period;
145 using duration = chrono::duration<rep, period>;
146 using time_point = chrono::time_point<utc_clock>;
147 static constexpr bool is_steady = false;
148
149 [[nodiscard]]
150 static time_point
151 now()
152 { return from_sys(system_clock::now()); }
153
154 template<typename _Duration>
155 [[nodiscard]]
156 static sys_time<common_type_t<_Duration, seconds>>
157 to_sys(const utc_time<_Duration>& __t)
158 {
160 const auto __li = chrono::get_leap_second_info(__t);
161 sys_time<_CDur> __s{__t.time_since_epoch() - __li.elapsed};
162 if (__li.is_leap_second)
163 __s = chrono::floor<seconds>(__s) + seconds{1} - _CDur{1};
164 return __s;
165 }
166
167 template<typename _Duration>
168 [[nodiscard]]
169 static utc_time<common_type_t<_Duration, seconds>>
170 from_sys(const sys_time<_Duration>& __t);
171 };
172
173 /** A clock that measures International Atomic Time.
174 *
175 * The epoch is 1958-01-01 00:00:00.
176 *
177 * @since C++20
178 */
180 {
181 public:
182 using rep = system_clock::rep;
183 using period = system_clock::period;
184 using duration = chrono::duration<rep, period>;
185 using time_point = chrono::time_point<tai_clock>;
186 static constexpr bool is_steady = false;
187
188 [[nodiscard]]
189 static time_point
190 now(); // in src/c++20/clock.cc
191
192 template<typename _Duration>
193 [[nodiscard]]
194 static utc_time<common_type_t<_Duration, seconds>>
195 to_utc(const tai_time<_Duration>& __t)
196 {
198 return utc_time<_CDur>{__t.time_since_epoch()} - 378691210s;
199 }
200
201 template<typename _Duration>
202 [[nodiscard]]
203 static tai_time<common_type_t<_Duration, seconds>>
204 from_utc(const utc_time<_Duration>& __t)
205 {
207 return tai_time<_CDur>{__t.time_since_epoch()} + 378691210s;
208 }
209 };
210
211 /** A clock that measures GPS time.
212 *
213 * The epoch is 1980-01-06 00:00:00.
214 *
215 * @since C++20
216 */
218 {
219 public:
220 using rep = system_clock::rep;
221 using period = system_clock::period;
222 using duration = chrono::duration<rep, period>;
223 using time_point = chrono::time_point<gps_clock>;
224 static constexpr bool is_steady = false;
225
226 [[nodiscard]]
227 static time_point
228 now(); // in src/c++20/clock.cc
229
230 template<typename _Duration>
231 [[nodiscard]]
232 static utc_time<common_type_t<_Duration, seconds>>
233 to_utc(const gps_time<_Duration>& __t)
234 {
236 return utc_time<_CDur>{__t.time_since_epoch()} + 315964809s;
237 }
238
239 template<typename _Duration>
240 [[nodiscard]]
241 static gps_time<common_type_t<_Duration, seconds>>
242 from_utc(const utc_time<_Duration>& __t)
243 {
245 return gps_time<_CDur>{__t.time_since_epoch()} - 315964809s;
246 }
247 };
248#endif // _GLIBCXX_HOSTED
249
250 template<typename _DestClock, typename _SourceClock>
251 struct clock_time_conversion
252 { };
253
254 // Identity conversions
255
256 template<typename _Clock>
257 struct clock_time_conversion<_Clock, _Clock>
258 {
259 template<typename _Duration>
260 time_point<_Clock, _Duration>
261 operator()(const time_point<_Clock, _Duration>& __t) const
262 { return __t; }
263 };
264
265#if _GLIBCXX_HOSTED
266 template<>
267 struct clock_time_conversion<system_clock, system_clock>
268 {
269 template<typename _Duration>
270 sys_time<_Duration>
271 operator()(const sys_time<_Duration>& __t) const
272 { return __t; }
273 };
274
275 template<>
276 struct clock_time_conversion<utc_clock, utc_clock>
277 {
278 template<typename _Duration>
279 utc_time<_Duration>
280 operator()(const utc_time<_Duration>& __t) const
281 { return __t; }
282 };
283
284 // Conversions between system_clock and utc_clock
285
286 template<>
287 struct clock_time_conversion<utc_clock, system_clock>
288 {
289 template<typename _Duration>
290 utc_time<common_type_t<_Duration, seconds>>
291 operator()(const sys_time<_Duration>& __t) const
292 { return utc_clock::from_sys(__t); }
293 };
294
295 template<>
296 struct clock_time_conversion<system_clock, utc_clock>
297 {
298 template<typename _Duration>
299 sys_time<common_type_t<_Duration, seconds>>
300 operator()(const utc_time<_Duration>& __t) const
301 { return utc_clock::to_sys(__t); }
302 };
303
304 template<typename _Tp, typename _Clock>
305 inline constexpr bool __is_time_point_for_v = false;
306
307 template<typename _Clock, typename _Duration>
308 inline constexpr bool
309 __is_time_point_for_v<time_point<_Clock, _Duration>, _Clock> = true;
310
311 // Conversions between system_clock and other clocks
312
313 template<typename _SourceClock>
314 struct clock_time_conversion<system_clock, _SourceClock>
315 {
316 template<typename _Duration, typename _Src = _SourceClock>
317 auto
318 operator()(const time_point<_SourceClock, _Duration>& __t) const
319 -> decltype(_Src::to_sys(__t))
320 {
321 using _Ret = decltype(_SourceClock::to_sys(__t));
322 static_assert(__is_time_point_for_v<_Ret, system_clock>);
323 return _SourceClock::to_sys(__t);
324 }
325 };
326
327 template<typename _DestClock>
328 struct clock_time_conversion<_DestClock, system_clock>
329 {
330 template<typename _Duration, typename _Dest = _DestClock>
331 auto
332 operator()(const sys_time<_Duration>& __t) const
333 -> decltype(_Dest::from_sys(__t))
334 {
335 using _Ret = decltype(_DestClock::from_sys(__t));
336 static_assert(__is_time_point_for_v<_Ret, _DestClock>);
337 return _DestClock::from_sys(__t);
338 }
339 };
340
341 // Conversions between utc_clock and other clocks
342
343 template<typename _SourceClock>
344 struct clock_time_conversion<utc_clock, _SourceClock>
345 {
346 template<typename _Duration, typename _Src = _SourceClock>
347 auto
348 operator()(const time_point<_SourceClock, _Duration>& __t) const
349 -> decltype(_Src::to_utc(__t))
350 {
351 using _Ret = decltype(_SourceClock::to_utc(__t));
352 static_assert(__is_time_point_for_v<_Ret, utc_clock>);
353 return _SourceClock::to_utc(__t);
354 }
355 };
356
357 template<typename _DestClock>
358 struct clock_time_conversion<_DestClock, utc_clock>
359 {
360 template<typename _Duration, typename _Dest = _DestClock>
361 auto
362 operator()(const utc_time<_Duration>& __t) const
363 -> decltype(_Dest::from_utc(__t))
364 {
365 using _Ret = decltype(_DestClock::from_utc(__t));
366 static_assert(__is_time_point_for_v<_Ret, _DestClock>);
367 return _DestClock::from_utc(__t);
368 }
369 };
370#endif // _GLIBCXX_HOSTED
371
372 /// @cond undocumented
373 namespace __detail
374 {
375 template<typename _DestClock, typename _SourceClock, typename _Duration>
376 concept __clock_convs
377 = requires (const time_point<_SourceClock, _Duration>& __t) {
378 clock_time_conversion<_DestClock, _SourceClock>{}(__t);
379 };
380
381#if _GLIBCXX_HOSTED
382 template<typename _DestClock, typename _SourceClock, typename _Duration>
383 concept __clock_convs_sys
384 = requires (const time_point<_SourceClock, _Duration>& __t) {
385 clock_time_conversion<_DestClock, system_clock>{}(
386 clock_time_conversion<system_clock, _SourceClock>{}(__t));
387 };
388
389 template<typename _DestClock, typename _SourceClock, typename _Duration>
390 concept __clock_convs_utc
391 = requires (const time_point<_SourceClock, _Duration>& __t) {
392 clock_time_conversion<_DestClock, utc_clock>{}(
393 clock_time_conversion<utc_clock, _SourceClock>{}(__t));
394 };
395
396 template<typename _DestClock, typename _SourceClock, typename _Duration>
397 concept __clock_convs_sys_utc
398 = requires (const time_point<_SourceClock, _Duration>& __t) {
399 clock_time_conversion<_DestClock, utc_clock>{}(
400 clock_time_conversion<utc_clock, system_clock>{}(
401 clock_time_conversion<system_clock, _SourceClock>{}(__t)));
402 };
403
404 template<typename _DestClock, typename _SourceClock, typename _Duration>
405 concept __clock_convs_utc_sys
406 = requires (const time_point<_SourceClock, _Duration>& __t) {
407 clock_time_conversion<_DestClock, system_clock>{}(
408 clock_time_conversion<system_clock, utc_clock>{}(
409 clock_time_conversion<utc_clock, _SourceClock>{}(__t)));
410 };
411#endif // _GLIBCXX_HOSTED
412 } // namespace __detail
413 /// @endcond
414
415 /// Convert a time point to a different clock.
416 template<typename _DestClock, typename _SourceClock, typename _Duration>
417 [[nodiscard]]
418 inline auto
420 requires __detail::__clock_convs<_DestClock, _SourceClock, _Duration>
421#if _GLIBCXX_HOSTED
422 || __detail::__clock_convs_sys<_DestClock, _SourceClock, _Duration>
423 || __detail::__clock_convs_utc<_DestClock, _SourceClock, _Duration>
424 || __detail::__clock_convs_sys_utc<_DestClock, _SourceClock, _Duration>
425 || __detail::__clock_convs_utc_sys<_DestClock, _SourceClock, _Duration>
426#endif // _GLIBCXX_HOSTED
427 {
428 constexpr bool __direct
429 = __detail::__clock_convs<_DestClock, _SourceClock, _Duration>;
430 if constexpr (__direct)
431 {
432 return clock_time_conversion<_DestClock, _SourceClock>{}(__t);
433 }
434#if _GLIBCXX_HOSTED
435 else
436 {
437 constexpr bool __convert_via_sys_clock
438 = __detail::__clock_convs_sys<_DestClock, _SourceClock, _Duration>;
439 constexpr bool __convert_via_utc_clock
440 = __detail::__clock_convs_utc<_DestClock, _SourceClock, _Duration>;
441 if constexpr (__convert_via_sys_clock)
442 {
443 static_assert(!__convert_via_utc_clock,
444 "clock_cast requires a unique best conversion, but "
445 "conversion is possible via system_clock and also via"
446 "utc_clock");
447 return clock_time_conversion<_DestClock, system_clock>{}(
448 clock_time_conversion<system_clock, _SourceClock>{}(__t));
449 }
450 else if constexpr (__convert_via_utc_clock)
451 {
452 return clock_time_conversion<_DestClock, utc_clock>{}(
453 clock_time_conversion<utc_clock, _SourceClock>{}(__t));
454 }
455 else
456 {
457 constexpr bool __convert_via_sys_and_utc_clocks
458 = __detail::__clock_convs_sys_utc<_DestClock,
459 _SourceClock,
460 _Duration>;
461
462 if constexpr (__convert_via_sys_and_utc_clocks)
463 {
464 constexpr bool __convert_via_utc_and_sys_clocks
465 = __detail::__clock_convs_utc_sys<_DestClock,
466 _SourceClock,
467 _Duration>;
468 static_assert(!__convert_via_utc_and_sys_clocks,
469 "clock_cast requires a unique best conversion, but "
470 "conversion is possible via system_clock followed by "
471 "utc_clock, and also via utc_clock followed by "
472 "system_clock");
473 return clock_time_conversion<_DestClock, utc_clock>{}(
474 clock_time_conversion<utc_clock, system_clock>{}(
475 clock_time_conversion<system_clock, _SourceClock>{}(__t)));
476 }
477 else
478 {
479 return clock_time_conversion<_DestClock, system_clock>{}(
480 clock_time_conversion<system_clock, utc_clock>{}(
481 clock_time_conversion<utc_clock, _SourceClock>{}(__t)));
482 }
483 }
484 }
485#endif // _GLIBCXX_HOSTED
486 }
487
488 // CALENDRICAL TYPES
489
490 // CLASS DECLARATIONS
491 class day;
492 class month;
493 class year;
494 class weekday;
495 class weekday_indexed;
496 class weekday_last;
497 class month_day;
498 class month_day_last;
499 class month_weekday;
500 class month_weekday_last;
501 class year_month;
502 class year_month_day;
503 class year_month_day_last;
504 class year_month_weekday;
505 class year_month_weekday_last;
506
507 struct last_spec
508 {
509 explicit last_spec() = default;
510
511 friend constexpr month_day_last
512 operator/(int __m, last_spec) noexcept;
513
514 friend constexpr month_day_last
515 operator/(last_spec, int __m) noexcept;
516 };
517
518 inline constexpr last_spec last{};
519
520 namespace __detail
521 {
522 // Helper to __add_modulo and __sub_modulo.
523 template <unsigned __d, typename _Tp>
524 consteval auto
525 __modulo_offset()
526 {
527 using _Up = make_unsigned_t<_Tp>;
528 auto constexpr __a = _Up(-1) - _Up(255 + __d - 2);
529 auto constexpr __b = _Up(__d * (__a / __d) - 1);
530 // Notice: b <= a - 1 <= _Up(-1) - (255 + d - 1) and b % d = d - 1.
531 return _Up(-1) - __b; // >= 255 + d - 1
532 }
533
534 // Compute the remainder of the Euclidean division of __x + __y divided by
535 // __d without overflowing. Typically, __x <= 255 + d - 1 is sum of
536 // weekday/month with a shift in [0, d - 1] and __y is a duration count.
537 template <unsigned __d, typename _Tp>
538 constexpr unsigned
539 __add_modulo(unsigned __x, _Tp __y)
540 {
541 using _Up = make_unsigned_t<_Tp>;
542 // For __y >= 0, _Up(__y) has the same mathematical value as __y and
543 // this function simply returns (__x + _Up(__y)) % d. Typically, this
544 // doesn't overflow since the range of _Up contains many more positive
545 // values than _Tp's. For __y < 0, _Up(__y) has a mathematical value in
546 // the upper-half range of _Up so that adding a positive value to it
547 // might overflow. Moreover, most likely, _Up(__y) != __y mod d. To
548 // fix both issues we subtract from _Up(__y) an __offset >=
549 // 255 + d - 1 to make room for the addition to __x and shift the modulo
550 // to the correct value.
551 auto const __offset = __y >= 0 ? _Up(0) : __modulo_offset<__d, _Tp>();
552 return (__x + _Up(__y) - __offset) % __d;
553 }
554
555 // Similar to __add_modulo but for __x - __y.
556 template <unsigned __d, typename _Tp>
557 constexpr unsigned
558 __sub_modulo(unsigned __x, _Tp __y)
559 {
560 using _Up = make_unsigned_t<_Tp>;
561 auto const __offset = __y <= 0 ? _Up(0) : __modulo_offset<__d, _Tp>();
562 return (__x - _Up(__y) - __offset) % __d;
563 }
564
565 inline constexpr unsigned __days_per_month[12]
566 = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
567 }
568
569 // DAY
570
571 class day
572 {
573 private:
574 unsigned char _M_d;
575
576 public:
577 day() = default;
578
579 explicit constexpr
580 day(unsigned __d) noexcept
581 : _M_d(__d)
582 { }
583
584 constexpr day&
585 operator++() noexcept
586 {
587 ++_M_d;
588 return *this;
589 }
590
591 constexpr day
592 operator++(int) noexcept
593 {
594 auto __ret = *this;
595 ++(*this);
596 return __ret;
597 }
598
599 constexpr day&
600 operator--() noexcept
601 {
602 --_M_d;
603 return *this;
604 }
605
606 constexpr day
607 operator--(int) noexcept
608 {
609 auto __ret = *this;
610 --(*this);
611 return __ret;
612 }
613
614 constexpr day&
615 operator+=(const days& __d) noexcept
616 {
617 *this = *this + __d;
618 return *this;
619 }
620
621 constexpr day&
622 operator-=(const days& __d) noexcept
623 {
624 *this = *this - __d;
625 return *this;
626 }
627
628 constexpr explicit
629 operator unsigned() const noexcept
630 { return _M_d; }
631
632 constexpr bool
633 ok() const noexcept
634 { return 1 <= _M_d && _M_d <= 31; }
635
636 friend constexpr bool
637 operator==(const day& __x, const day& __y) noexcept
638 { return unsigned{__x} == unsigned{__y}; }
639
640 friend constexpr strong_ordering
641 operator<=>(const day& __x, const day& __y) noexcept
642 { return unsigned{__x} <=> unsigned{__y}; }
643
644 friend constexpr day
645 operator+(const day& __x, const days& __y) noexcept
646 { return day(unsigned{__x} + __y.count()); }
647
648 friend constexpr day
649 operator+(const days& __x, const day& __y) noexcept
650 { return __y + __x; }
651
652 friend constexpr day
653 operator-(const day& __x, const days& __y) noexcept
654 { return __x + -__y; }
655
656 friend constexpr days
657 operator-(const day& __x, const day& __y) noexcept
658 { return days{int(unsigned{__x}) - int(unsigned{__y})}; }
659
660 friend constexpr month_day
661 operator/(const month& __m, const day& __d) noexcept;
662
663 friend constexpr month_day
664 operator/(int __m, const day& __d) noexcept;
665
666 friend constexpr month_day
667 operator/(const day& __d, const month& __m) noexcept;
668
669 friend constexpr month_day
670 operator/(const day& __d, int __m) noexcept;
671
672 friend constexpr year_month_day
673 operator/(const year_month& __ym, const day& __d) noexcept;
674 };
675
676 // MONTH
677
678 class month
679 {
680 private:
681 unsigned char _M_m;
682
683 public:
684 month() = default;
685
686 explicit constexpr
687 month(unsigned __m) noexcept
688 : _M_m(__m)
689 { }
690
691 constexpr month&
692 operator++() noexcept
693 {
694 *this += months{1};
695 return *this;
696 }
697
698 constexpr month
699 operator++(int) noexcept
700 {
701 auto __ret = *this;
702 ++(*this);
703 return __ret;
704 }
705
706 constexpr month&
707 operator--() noexcept
708 {
709 *this -= months{1};
710 return *this;
711 }
712
713 constexpr month
714 operator--(int) noexcept
715 {
716 auto __ret = *this;
717 --(*this);
718 return __ret;
719 }
720
721 constexpr month&
722 operator+=(const months& __m) noexcept
723 {
724 *this = *this + __m;
725 return *this;
726 }
727
728 constexpr month&
729 operator-=(const months& __m) noexcept
730 {
731 *this = *this - __m;
732 return *this;
733 }
734
735 explicit constexpr
736 operator unsigned() const noexcept
737 { return _M_m; }
738
739 constexpr bool
740 ok() const noexcept
741 { return 1 <= _M_m && _M_m <= 12; }
742
743 friend constexpr bool
744 operator==(const month& __x, const month& __y) noexcept
745 { return unsigned{__x} == unsigned{__y}; }
746
747 friend constexpr strong_ordering
748 operator<=>(const month& __x, const month& __y) noexcept
749 { return unsigned{__x} <=> unsigned{__y}; }
750
751 friend constexpr month
752 operator+(const month& __x, const months& __y) noexcept
753 {
754 // modulo(x + (y - 1), 12) = modulo(x + (y - 1) + 12, 12)
755 // = modulo((x + 11) + y , 12)
756 return month{1 + __detail::__add_modulo<12>(
757 unsigned{__x} + 11, __y.count())};
758 }
759
760 friend constexpr month
761 operator+(const months& __x, const month& __y) noexcept
762 { return __y + __x; }
763
764 friend constexpr month
765 operator-(const month& __x, const months& __y) noexcept
766 {
767 // modulo(x + (-y - 1), 12) = modulo(x + (-y - 1) + 12, 12)
768 // = modulo((x + 11) - y , 12)
769 return month{1 + __detail::__sub_modulo<12>(
770 unsigned{__x} + 11, __y.count())};
771 }
772
773 friend constexpr months
774 operator-(const month& __x, const month& __y) noexcept
775 {
776 const auto __dm = int(unsigned(__x)) - int(unsigned(__y));
777 return months{__dm < 0 ? 12 + __dm : __dm};
778 }
779
780 friend constexpr year_month
781 operator/(const year& __y, const month& __m) noexcept;
782
783 friend constexpr month_day
784 operator/(const month& __m, int __d) noexcept;
785
786 friend constexpr month_day_last
787 operator/(const month& __m, last_spec) noexcept;
788
789 friend constexpr month_day_last
790 operator/(last_spec, const month& __m) noexcept;
791
792 friend constexpr month_weekday
793 operator/(const month& __m, const weekday_indexed& __wdi) noexcept;
794
795 friend constexpr month_weekday
796 operator/(const weekday_indexed& __wdi, const month& __m) noexcept;
797
798 friend constexpr month_weekday_last
799 operator/(const month& __m, const weekday_last& __wdl) noexcept;
800
801 friend constexpr month_weekday_last
802 operator/(const weekday_last& __wdl, const month& __m) noexcept;
803 };
804
805 inline constexpr month January{1};
806 inline constexpr month February{2};
807 inline constexpr month March{3};
808 inline constexpr month April{4};
809 inline constexpr month May{5};
810 inline constexpr month June{6};
811 inline constexpr month July{7};
812 inline constexpr month August{8};
813 inline constexpr month September{9};
814 inline constexpr month October{10};
815 inline constexpr month November{11};
816 inline constexpr month December{12};
817
818 // YEAR
819
820 class year
821 {
822 private:
823 short _M_y;
824
825 public:
826 year() = default;
827
828 explicit constexpr
829 year(int __y) noexcept
830 : _M_y{static_cast<short>(__y)}
831 { }
832
833 static constexpr year
834 min() noexcept
835 { return year{-32767}; }
836
837 static constexpr year
838 max() noexcept
839 { return year{32767}; }
840
841 constexpr year&
842 operator++() noexcept
843 {
844 ++_M_y;
845 return *this;
846 }
847
848 constexpr year
849 operator++(int) noexcept
850 {
851 auto __ret = *this;
852 ++(*this);
853 return __ret;
854 }
855
856 constexpr year&
857 operator--() noexcept
858 {
859 --_M_y;
860 return *this;
861 }
862
863 constexpr year
864 operator--(int) noexcept
865 {
866 auto __ret = *this;
867 --(*this);
868 return __ret;
869 }
870
871 constexpr year&
872 operator+=(const years& __y) noexcept
873 {
874 *this = *this + __y;
875 return *this;
876 }
877
878 constexpr year&
879 operator-=(const years& __y) noexcept
880 {
881 *this = *this - __y;
882 return *this;
883 }
884
885 constexpr year
886 operator+() const noexcept
887 { return *this; }
888
889 constexpr year
890 operator-() const noexcept
891 { return year{-_M_y}; }
892
893 constexpr bool
894 is_leap() const noexcept
895 {
896 // Testing divisibility by 100 first gives better performance [1], i.e.,
897 // return _M_y % 100 == 0 ? _M_y % 400 == 0 : _M_y % 16 == 0;
898 // Furthermore, if _M_y % 100 == 0, then _M_y % 400 == 0 is equivalent
899 // to _M_y % 16 == 0, so we can simplify it to
900 // return _M_y % 100 == 0 ? _M_y % 16 == 0 : _M_y % 4 == 0. // #1
901 // Similarly, we can replace 100 with 25 (which is good since
902 // _M_y % 25 == 0 requires one fewer instruction than _M_y % 100 == 0
903 // [2]):
904 // return _M_y % 25 == 0 ? _M_y % 16 == 0 : _M_y % 4 == 0. // #2
905 // Indeed, first assume _M_y % 4 != 0. Then _M_y % 16 != 0 and hence,
906 // _M_y % 4 == 0 and _M_y % 16 == 0 are both false. Therefore, #2
907 // returns false as it should (regardless of _M_y % 25.) Now assume
908 // _M_y % 4 == 0. In this case, _M_y % 25 == 0 if, and only if,
909 // _M_y % 100 == 0, that is, #1 and #2 are equivalent. Finally, #2 is
910 // equivalent to
911 // return (_M_y & (_M_y % 25 == 0 ? 15 : 3)) == 0.
912
913 // References:
914 // [1] https://github.com/cassioneri/calendar
915 // [2] https://godbolt.org/z/55G8rn77e
916 // [3] https://gcc.gnu.org/pipermail/libstdc++/2021-June/052815.html
917
918 return (_M_y & (_M_y % 25 == 0 ? 15 : 3)) == 0;
919 }
920
921 explicit constexpr
922 operator int() const noexcept
923 { return _M_y; }
924
925 constexpr bool
926 ok() const noexcept
927 { return min()._M_y <= _M_y && _M_y <= max()._M_y; }
928
929 friend constexpr bool
930 operator==(const year& __x, const year& __y) noexcept
931 { return int{__x} == int{__y}; }
932
933 friend constexpr strong_ordering
934 operator<=>(const year& __x, const year& __y) noexcept
935 { return int{__x} <=> int{__y}; }
936
937 friend constexpr year
938 operator+(const year& __x, const years& __y) noexcept
939 { return year{int{__x} + static_cast<int>(__y.count())}; }
940
941 friend constexpr year
942 operator+(const years& __x, const year& __y) noexcept
943 { return __y + __x; }
944
945 friend constexpr year
946 operator-(const year& __x, const years& __y) noexcept
947 { return __x + -__y; }
948
949 friend constexpr years
950 operator-(const year& __x, const year& __y) noexcept
951 { return years{int{__x} - int{__y}}; }
952
953 friend constexpr year_month
954 operator/(const year& __y, int __m) noexcept;
955
956 friend constexpr year_month_day
957 operator/(const year& __y, const month_day& __md) noexcept;
958
959 friend constexpr year_month_day
960 operator/(const month_day& __md, const year& __y) noexcept;
961
962 friend constexpr year_month_day_last
963 operator/(const year& __y, const month_day_last& __mdl) noexcept;
964
965 friend constexpr year_month_day_last
966 operator/(const month_day_last& __mdl, const year& __y) noexcept;
967
968 friend constexpr year_month_weekday
969 operator/(const year& __y, const month_weekday& __mwd) noexcept;
970
971 friend constexpr year_month_weekday
972 operator/(const month_weekday& __mwd, const year& __y) noexcept;
973
974 friend constexpr year_month_weekday_last
975 operator/(const year& __y, const month_weekday_last& __mwdl) noexcept;
976
977 friend constexpr year_month_weekday_last
978 operator/(const month_weekday_last& __mwdl, const year& __y) noexcept;
979 };
980
981 // WEEKDAY
982
983 class weekday
984 {
985 private:
986 unsigned char _M_wd;
987
988 static constexpr weekday
989 _S_from_days(const days& __d)
990 {
991 return weekday{__detail::__add_modulo<7>(4, __d.count())};
992 }
993
994 public:
995 weekday() = default;
996
997 explicit constexpr
998 weekday(unsigned __wd) noexcept
999 : _M_wd(__wd == 7 ? 0 : __wd) // __wd % 7 ?
1000 { }
1001
1002 constexpr
1003 weekday(const sys_days& __dp) noexcept
1004 : weekday{_S_from_days(__dp.time_since_epoch())}
1005 { }
1006
1007 explicit constexpr
1008 weekday(const local_days& __dp) noexcept
1009 : weekday{sys_days{__dp.time_since_epoch()}}
1010 { }
1011
1012 constexpr weekday&
1013 operator++() noexcept
1014 {
1015 *this += days{1};
1016 return *this;
1017 }
1018
1019 constexpr weekday
1020 operator++(int) noexcept
1021 {
1022 auto __ret = *this;
1023 ++(*this);
1024 return __ret;
1025 }
1026
1027 constexpr weekday&
1028 operator--() noexcept
1029 {
1030 *this -= days{1};
1031 return *this;
1032 }
1033
1034 constexpr weekday
1035 operator--(int) noexcept
1036 {
1037 auto __ret = *this;
1038 --(*this);
1039 return __ret;
1040 }
1041
1042 constexpr weekday&
1043 operator+=(const days& __d) noexcept
1044 {
1045 *this = *this + __d;
1046 return *this;
1047 }
1048
1049 constexpr weekday&
1050 operator-=(const days& __d) noexcept
1051 {
1052 *this = *this - __d;
1053 return *this;
1054 }
1055
1056 constexpr unsigned
1057 c_encoding() const noexcept
1058 { return _M_wd; }
1059
1060 constexpr unsigned
1061 iso_encoding() const noexcept
1062 { return _M_wd == 0u ? 7u : _M_wd; }
1063
1064 constexpr bool
1065 ok() const noexcept
1066 { return _M_wd <= 6; }
1067
1068 constexpr weekday_indexed
1069 operator[](unsigned __index) const noexcept;
1070
1071 constexpr weekday_last
1072 operator[](last_spec) const noexcept;
1073
1074 friend constexpr bool
1075 operator==(const weekday& __x, const weekday& __y) noexcept
1076 { return __x._M_wd == __y._M_wd; }
1077
1078 friend constexpr weekday
1079 operator+(const weekday& __x, const days& __y) noexcept
1080 {
1081 return weekday{__detail::__add_modulo<7>(__x._M_wd, __y.count())};
1082 }
1083
1084 friend constexpr weekday
1085 operator+(const days& __x, const weekday& __y) noexcept
1086 { return __y + __x; }
1087
1088 friend constexpr weekday
1089 operator-(const weekday& __x, const days& __y) noexcept
1090 {
1091 return weekday{__detail::__sub_modulo<7>(__x._M_wd, __y.count())};
1092 }
1093
1094 friend constexpr days
1095 operator-(const weekday& __x, const weekday& __y) noexcept
1096 {
1097 const auto __n = __x.c_encoding() - __y.c_encoding();
1098 return static_cast<int>(__n) >= 0 ? days{__n} : days{__n + 7};
1099 }
1100 };
1101
1102 inline constexpr weekday Sunday{0};
1103 inline constexpr weekday Monday{1};
1104 inline constexpr weekday Tuesday{2};
1105 inline constexpr weekday Wednesday{3};
1106 inline constexpr weekday Thursday{4};
1107 inline constexpr weekday Friday{5};
1108 inline constexpr weekday Saturday{6};
1109
1110 // WEEKDAY_INDEXED
1111
1112 class weekday_indexed
1113 {
1114 private:
1115 chrono::weekday _M_wd;
1116 unsigned char _M_index;
1117
1118 public:
1119 weekday_indexed() = default;
1120
1121 constexpr
1122 weekday_indexed(const chrono::weekday& __wd, unsigned __index) noexcept
1123 : _M_wd(__wd), _M_index(__index)
1124 { }
1125
1126 constexpr chrono::weekday
1127 weekday() const noexcept
1128 { return _M_wd; }
1129
1130 constexpr unsigned
1131 index() const noexcept
1132 { return _M_index; };
1133
1134 constexpr bool
1135 ok() const noexcept
1136 { return _M_wd.ok() && 1 <= _M_index && _M_index <= 5; }
1137
1138 friend constexpr bool
1139 operator==(const weekday_indexed& __x, const weekday_indexed& __y) noexcept
1140 { return __x.weekday() == __y.weekday() && __x.index() == __y.index(); }
1141
1142 friend constexpr month_weekday
1143 operator/(const month& __m, const weekday_indexed& __wdi) noexcept;
1144
1145 friend constexpr month_weekday
1146 operator/(int __m, const weekday_indexed& __wdi) noexcept;
1147
1148 friend constexpr month_weekday
1149 operator/(const weekday_indexed& __wdi, const month& __m) noexcept;
1150
1151 friend constexpr month_weekday
1152 operator/(const weekday_indexed& __wdi, int __m) noexcept;
1153
1154 friend constexpr year_month_weekday
1155 operator/(const year_month& __ym, const weekday_indexed& __wdi) noexcept;
1156 };
1157
1158 constexpr weekday_indexed
1159 weekday::operator[](unsigned __index) const noexcept
1160 { return {*this, __index}; }
1161
1162 // WEEKDAY_LAST
1163
1164 class weekday_last
1165 {
1166 private:
1167 chrono::weekday _M_wd;
1168
1169 public:
1170 explicit constexpr
1171 weekday_last(const chrono::weekday& __wd) noexcept
1172 : _M_wd{__wd}
1173 { }
1174
1175 constexpr chrono::weekday
1176 weekday() const noexcept
1177 { return _M_wd; }
1178
1179 constexpr bool
1180 ok() const noexcept
1181 { return _M_wd.ok(); }
1182
1183 friend constexpr bool
1184 operator==(const weekday_last& __x, const weekday_last& __y) noexcept
1185 { return __x.weekday() == __y.weekday(); }
1186
1187 friend constexpr month_weekday_last
1188 operator/(int __m, const weekday_last& __wdl) noexcept;
1189
1190 friend constexpr month_weekday_last
1191 operator/(const weekday_last& __wdl, int __m) noexcept;
1192
1193 friend constexpr year_month_weekday_last
1194 operator/(const year_month& __ym, const weekday_last& __wdl) noexcept;
1195 };
1196
1197 constexpr weekday_last
1198 weekday::operator[](last_spec) const noexcept
1199 { return weekday_last{*this}; }
1200
1201 // MONTH_DAY
1202
1203 class month_day
1204 {
1205 private:
1206 chrono::month _M_m;
1207 chrono::day _M_d;
1208
1209 public:
1210 month_day() = default;
1211
1212 constexpr
1213 month_day(const chrono::month& __m, const chrono::day& __d) noexcept
1214 : _M_m{__m}, _M_d{__d}
1215 { }
1216
1217 constexpr chrono::month
1218 month() const noexcept
1219 { return _M_m; }
1220
1221 constexpr chrono::day
1222 day() const noexcept
1223 { return _M_d; }
1224
1225 constexpr bool
1226 ok() const noexcept
1227 {
1228 return _M_m.ok()
1229 && 1u <= unsigned(_M_d)
1230 && unsigned(_M_d) <= __detail::__days_per_month[unsigned(_M_m) - 1];
1231 }
1232
1233 friend constexpr bool
1234 operator==(const month_day& __x, const month_day& __y) noexcept
1235 { return __x.month() == __y.month() && __x.day() == __y.day(); }
1236
1237 friend constexpr strong_ordering
1238 operator<=>(const month_day& __x, const month_day& __y) noexcept
1239 = default;
1240
1241 friend constexpr month_day
1242 operator/(const chrono::month& __m, const chrono::day& __d) noexcept
1243 { return {__m, __d}; }
1244
1245 friend constexpr month_day
1246 operator/(const chrono::month& __m, int __d) noexcept
1247 { return {__m, chrono::day(unsigned(__d))}; }
1248
1249 friend constexpr month_day
1250 operator/(int __m, const chrono::day& __d) noexcept
1251 { return {chrono::month(unsigned(__m)), __d}; }
1252
1253 friend constexpr month_day
1254 operator/(const chrono::day& __d, const chrono::month& __m) noexcept
1255 { return {__m, __d}; }
1256
1257 friend constexpr month_day
1258 operator/(const chrono::day& __d, int __m) noexcept
1259 { return {chrono::month(unsigned(__m)), __d}; }
1260
1261 friend constexpr year_month_day
1262 operator/(int __y, const month_day& __md) noexcept;
1263
1264 friend constexpr year_month_day
1265 operator/(const month_day& __md, int __y) noexcept;
1266 };
1267
1268 // MONTH_DAY_LAST
1269
1270 class month_day_last
1271 {
1272 private:
1273 chrono::month _M_m;
1274
1275 public:
1276 explicit constexpr
1277 month_day_last(const chrono::month& __m) noexcept
1278 : _M_m{__m}
1279 { }
1280
1281 constexpr chrono::month
1282 month() const noexcept
1283 { return _M_m; }
1284
1285 constexpr bool
1286 ok() const noexcept
1287 { return _M_m.ok(); }
1288
1289 friend constexpr bool
1290 operator==(const month_day_last& __x, const month_day_last& __y) noexcept
1291 { return __x.month() == __y.month(); }
1292
1293 friend constexpr strong_ordering
1294 operator<=>(const month_day_last& __x, const month_day_last& __y) noexcept
1295 = default;
1296
1297 friend constexpr month_day_last
1298 operator/(const chrono::month& __m, last_spec) noexcept
1299 { return month_day_last{__m}; }
1300
1301 friend constexpr month_day_last
1302 operator/(int __m, last_spec) noexcept
1303 { return chrono::month(unsigned(__m)) / last; }
1304
1305 friend constexpr month_day_last
1306 operator/(last_spec, const chrono::month& __m) noexcept
1307 { return __m / last; }
1308
1309 friend constexpr month_day_last
1310 operator/(last_spec, int __m) noexcept
1311 { return __m / last; }
1312
1313 friend constexpr year_month_day_last
1314 operator/(int __y, const month_day_last& __mdl) noexcept;
1315
1316 friend constexpr year_month_day_last
1317 operator/(const month_day_last& __mdl, int __y) noexcept;
1318 };
1319
1320 // MONTH_WEEKDAY
1321
1322 class month_weekday
1323 {
1324 private:
1325 chrono::month _M_m;
1326 chrono::weekday_indexed _M_wdi;
1327
1328 public:
1329 constexpr
1330 month_weekday(const chrono::month& __m,
1331 const chrono::weekday_indexed& __wdi) noexcept
1332 : _M_m{__m}, _M_wdi{__wdi}
1333 { }
1334
1335 constexpr chrono::month
1336 month() const noexcept
1337 { return _M_m; }
1338
1339 constexpr chrono::weekday_indexed
1340 weekday_indexed() const noexcept
1341 { return _M_wdi; }
1342
1343 constexpr bool
1344 ok() const noexcept
1345 { return _M_m.ok() && _M_wdi.ok(); }
1346
1347 friend constexpr bool
1348 operator==(const month_weekday& __x, const month_weekday& __y) noexcept
1349 {
1350 return __x.month() == __y.month()
1351 && __x.weekday_indexed() == __y.weekday_indexed();
1352 }
1353
1354 friend constexpr month_weekday
1355 operator/(const chrono::month& __m,
1356 const chrono::weekday_indexed& __wdi) noexcept
1357 { return {__m, __wdi}; }
1358
1359 friend constexpr month_weekday
1360 operator/(int __m, const chrono::weekday_indexed& __wdi) noexcept
1361 { return chrono::month(unsigned(__m)) / __wdi; }
1362
1363 friend constexpr month_weekday
1364 operator/(const chrono::weekday_indexed& __wdi,
1365 const chrono::month& __m) noexcept
1366 { return __m / __wdi; }
1367
1368 friend constexpr month_weekday
1369 operator/(const chrono::weekday_indexed& __wdi, int __m) noexcept
1370 { return __m / __wdi; }
1371
1372 friend constexpr year_month_weekday
1373 operator/(int __y, const month_weekday& __mwd) noexcept;
1374
1375 friend constexpr year_month_weekday
1376 operator/(const month_weekday& __mwd, int __y) noexcept;
1377 };
1378
1379 // MONTH_WEEKDAY_LAST
1380
1381 class month_weekday_last
1382 {
1383 private:
1384 chrono::month _M_m;
1385 chrono::weekday_last _M_wdl;
1386
1387 public:
1388 constexpr
1389 month_weekday_last(const chrono::month& __m,
1390 const chrono::weekday_last& __wdl) noexcept
1391 :_M_m{__m}, _M_wdl{__wdl}
1392 { }
1393
1394 constexpr chrono::month
1395 month() const noexcept
1396 { return _M_m; }
1397
1398 constexpr chrono::weekday_last
1399 weekday_last() const noexcept
1400 { return _M_wdl; }
1401
1402 constexpr bool
1403 ok() const noexcept
1404 { return _M_m.ok() && _M_wdl.ok(); }
1405
1406 friend constexpr bool
1407 operator==(const month_weekday_last& __x,
1408 const month_weekday_last& __y) noexcept
1409 {
1410 return __x.month() == __y.month()
1411 && __x.weekday_last() == __y.weekday_last();
1412 }
1413
1414 friend constexpr month_weekday_last
1415 operator/(const chrono::month& __m,
1416 const chrono::weekday_last& __wdl) noexcept
1417 { return {__m, __wdl}; }
1418
1419 friend constexpr month_weekday_last
1420 operator/(int __m, const chrono::weekday_last& __wdl) noexcept
1421 { return chrono::month(unsigned(__m)) / __wdl; }
1422
1423 friend constexpr month_weekday_last
1424 operator/(const chrono::weekday_last& __wdl,
1425 const chrono::month& __m) noexcept
1426 { return __m / __wdl; }
1427
1428 friend constexpr month_weekday_last
1429 operator/(const chrono::weekday_last& __wdl, int __m) noexcept
1430 { return chrono::month(unsigned(__m)) / __wdl; }
1431
1432 friend constexpr year_month_weekday_last
1433 operator/(int __y, const month_weekday_last& __mwdl) noexcept;
1434
1435 friend constexpr year_month_weekday_last
1436 operator/(const month_weekday_last& __mwdl, int __y) noexcept;
1437 };
1438
1439 // YEAR_MONTH
1440
1441 namespace __detail
1442 {
1443 // [time.cal.ym], [time.cal.ymd], etc constrain the 'months'-based
1444 // addition/subtraction operator overloads like so:
1445 //
1446 // Constraints: if the argument supplied by the caller for the months
1447 // parameter is convertible to years, its implicit conversion sequence
1448 // to years is worse than its implicit conversion sequence to months.
1449 //
1450 // We realize this constraint by templatizing the 'months'-based
1451 // overloads (using a dummy defaulted template parameter), so that
1452 // overload resolution doesn't select the 'months'-based overload unless
1453 // the implicit conversion sequence to 'months' is better than that to
1454 // 'years'.
1455 using __months_years_conversion_disambiguator = void;
1456 }
1457
1458 class year_month
1459 {
1460 private:
1461 chrono::year _M_y;
1462 chrono::month _M_m;
1463
1464 public:
1465 year_month() = default;
1466
1467 constexpr
1468 year_month(const chrono::year& __y, const chrono::month& __m) noexcept
1469 : _M_y{__y}, _M_m{__m}
1470 { }
1471
1472 constexpr chrono::year
1473 year() const noexcept
1474 { return _M_y; }
1475
1476 constexpr chrono::month
1477 month() const noexcept
1478 { return _M_m; }
1479
1480 template<typename = __detail::__months_years_conversion_disambiguator>
1481 constexpr year_month&
1482 operator+=(const months& __dm) noexcept
1483 {
1484 *this = *this + __dm;
1485 return *this;
1486 }
1487
1488 template<typename = __detail::__months_years_conversion_disambiguator>
1489 constexpr year_month&
1490 operator-=(const months& __dm) noexcept
1491 {
1492 *this = *this - __dm;
1493 return *this;
1494 }
1495
1496 constexpr year_month&
1497 operator+=(const years& __dy) noexcept
1498 {
1499 *this = *this + __dy;
1500 return *this;
1501 }
1502
1503 constexpr year_month&
1504 operator-=(const years& __dy) noexcept
1505 {
1506 *this = *this - __dy;
1507 return *this;
1508 }
1509
1510 constexpr bool
1511 ok() const noexcept
1512 { return _M_y.ok() && _M_m.ok(); }
1513
1514 friend constexpr bool
1515 operator==(const year_month& __x, const year_month& __y) noexcept
1516 { return __x.year() == __y.year() && __x.month() == __y.month(); }
1517
1518 friend constexpr strong_ordering
1519 operator<=>(const year_month& __x, const year_month& __y) noexcept
1520 = default;
1521
1522 template<typename = __detail::__months_years_conversion_disambiguator>
1523 friend constexpr year_month
1524 operator+(const year_month& __ym, const months& __dm) noexcept
1525 {
1526 // TODO: Optimize?
1527 auto __m = __ym.month() + __dm;
1528 auto __i = int(unsigned(__ym.month())) - 1 + __dm.count();
1529 auto __y = (__i < 0
1530 ? __ym.year() + years{(__i - 11) / 12}
1531 : __ym.year() + years{__i / 12});
1532 return __y / __m;
1533 }
1534
1535 template<typename = __detail::__months_years_conversion_disambiguator>
1536 friend constexpr year_month
1537 operator+(const months& __dm, const year_month& __ym) noexcept
1538 { return __ym + __dm; }
1539
1540 template<typename = __detail::__months_years_conversion_disambiguator>
1541 friend constexpr year_month
1542 operator-(const year_month& __ym, const months& __dm) noexcept
1543 { return __ym + -__dm; }
1544
1545 friend constexpr months
1546 operator-(const year_month& __x, const year_month& __y) noexcept
1547 {
1548 return (__x.year() - __y.year()
1549 + months{static_cast<int>(unsigned{__x.month()})
1550 - static_cast<int>(unsigned{__y.month()})});
1551 }
1552
1553 friend constexpr year_month
1554 operator+(const year_month& __ym, const years& __dy) noexcept
1555 { return (__ym.year() + __dy) / __ym.month(); }
1556
1557 friend constexpr year_month
1558 operator+(const years& __dy, const year_month& __ym) noexcept
1559 { return __ym + __dy; }
1560
1561 friend constexpr year_month
1562 operator-(const year_month& __ym, const years& __dy) noexcept
1563 { return __ym + -__dy; }
1564
1565 friend constexpr year_month
1566 operator/(const chrono::year& __y, const chrono::month& __m) noexcept
1567 { return {__y, __m}; }
1568
1569 friend constexpr year_month
1570 operator/(const chrono::year& __y, int __m) noexcept
1571 { return {__y, chrono::month(unsigned(__m))}; }
1572
1573 friend constexpr year_month_day
1574 operator/(const year_month& __ym, int __d) noexcept;
1575
1576 friend constexpr year_month_day_last
1577 operator/(const year_month& __ym, last_spec) noexcept;
1578 };
1579
1580 // YEAR_MONTH_DAY
1581
1582 class year_month_day
1583 {
1584 private:
1585 chrono::year _M_y;
1586 chrono::month _M_m;
1587 chrono::day _M_d;
1588
1589 static constexpr year_month_day _S_from_days(const days& __dp) noexcept;
1590
1591 constexpr days _M_days_since_epoch() const noexcept;
1592
1593 public:
1594 year_month_day() = default;
1595
1596 constexpr
1597 year_month_day(const chrono::year& __y, const chrono::month& __m,
1598 const chrono::day& __d) noexcept
1599 : _M_y{__y}, _M_m{__m}, _M_d{__d}
1600 { }
1601
1602 constexpr
1603 year_month_day(const year_month_day_last& __ymdl) noexcept;
1604
1605 constexpr
1606 year_month_day(const sys_days& __dp) noexcept
1607 : year_month_day(_S_from_days(__dp.time_since_epoch()))
1608 { }
1609
1610 explicit constexpr
1611 year_month_day(const local_days& __dp) noexcept
1612 : year_month_day(sys_days{__dp.time_since_epoch()})
1613 { }
1614
1615 template<typename = __detail::__months_years_conversion_disambiguator>
1616 constexpr year_month_day&
1617 operator+=(const months& __m) noexcept
1618 {
1619 *this = *this + __m;
1620 return *this;
1621 }
1622
1623 template<typename = __detail::__months_years_conversion_disambiguator>
1624 constexpr year_month_day&
1625 operator-=(const months& __m) noexcept
1626 {
1627 *this = *this - __m;
1628 return *this;
1629 }
1630
1631 constexpr year_month_day&
1632 operator+=(const years& __y) noexcept
1633 {
1634 *this = *this + __y;
1635 return *this;
1636 }
1637
1638 constexpr year_month_day&
1639 operator-=(const years& __y) noexcept
1640 {
1641 *this = *this - __y;
1642 return *this;
1643 }
1644
1645 constexpr chrono::year
1646 year() const noexcept
1647 { return _M_y; }
1648
1649 constexpr chrono::month
1650 month() const noexcept
1651 { return _M_m; }
1652
1653 constexpr chrono::day
1654 day() const noexcept
1655 { return _M_d; }
1656
1657 constexpr
1658 operator sys_days() const noexcept
1659 { return sys_days{_M_days_since_epoch()}; }
1660
1661 explicit constexpr
1662 operator local_days() const noexcept
1663 { return local_days{sys_days{*this}.time_since_epoch()}; }
1664
1665 constexpr bool ok() const noexcept;
1666
1667 friend constexpr bool
1668 operator==(const year_month_day& __x, const year_month_day& __y) noexcept
1669 {
1670 return __x.year() == __y.year()
1671 && __x.month() == __y.month()
1672 && __x.day() == __y.day();
1673 }
1674
1675 friend constexpr strong_ordering
1676 operator<=>(const year_month_day& __x, const year_month_day& __y) noexcept
1677 = default;
1678
1679 template<typename = __detail::__months_years_conversion_disambiguator>
1680 friend constexpr year_month_day
1681 operator+(const year_month_day& __ymd, const months& __dm) noexcept
1682 { return (__ymd.year() / __ymd.month() + __dm) / __ymd.day(); }
1683
1684 template<typename = __detail::__months_years_conversion_disambiguator>
1685 friend constexpr year_month_day
1686 operator+(const months& __dm, const year_month_day& __ymd) noexcept
1687 { return __ymd + __dm; }
1688
1689 friend constexpr year_month_day
1690 operator+(const year_month_day& __ymd, const years& __dy) noexcept
1691 { return (__ymd.year() + __dy) / __ymd.month() / __ymd.day(); }
1692
1693 friend constexpr year_month_day
1694 operator+(const years& __dy, const year_month_day& __ymd) noexcept
1695 { return __ymd + __dy; }
1696
1697 template<typename = __detail::__months_years_conversion_disambiguator>
1698 friend constexpr year_month_day
1699 operator-(const year_month_day& __ymd, const months& __dm) noexcept
1700 { return __ymd + -__dm; }
1701
1702 friend constexpr year_month_day
1703 operator-(const year_month_day& __ymd, const years& __dy) noexcept
1704 { return __ymd + -__dy; }
1705
1706 friend constexpr year_month_day
1707 operator/(const year_month& __ym, const chrono::day& __d) noexcept
1708 { return {__ym.year(), __ym.month(), __d}; }
1709
1710 friend constexpr year_month_day
1711 operator/(const year_month& __ym, int __d) noexcept
1712 { return __ym / chrono::day{unsigned(__d)}; }
1713
1714 friend constexpr year_month_day
1715 operator/(const chrono::year& __y, const month_day& __md) noexcept
1716 { return __y / __md.month() / __md.day(); }
1717
1718 friend constexpr year_month_day
1719 operator/(int __y, const month_day& __md) noexcept
1720 { return chrono::year{__y} / __md; }
1721
1722 friend constexpr year_month_day
1723 operator/(const month_day& __md, const chrono::year& __y) noexcept
1724 { return __y / __md; }
1725
1726 friend constexpr year_month_day
1727 operator/(const month_day& __md, int __y) noexcept
1728 { return chrono::year(__y) / __md; }
1729 };
1730
1731 // Construct from days since 1970/01/01.
1732 // Proposition 6.3 of Neri and Schneider,
1733 // "Euclidean Affine Functions and Applications to Calendar Algorithms".
1734 // https://arxiv.org/abs/2102.06959
1735 constexpr year_month_day
1736 year_month_day::_S_from_days(const days& __dp) noexcept
1737 {
1738 constexpr auto __z2 = static_cast<uint32_t>(-1468000);
1739 constexpr auto __r2_e3 = static_cast<uint32_t>(536895458);
1740
1741 const auto __r0 = static_cast<uint32_t>(__dp.count()) + __r2_e3;
1742
1743 const auto __n1 = 4 * __r0 + 3;
1744 const auto __q1 = __n1 / 146097;
1745 const auto __r1 = __n1 % 146097 / 4;
1746
1747 constexpr auto __p32 = static_cast<uint64_t>(1) << 32;
1748 const auto __n2 = 4 * __r1 + 3;
1749 const auto __u2 = static_cast<uint64_t>(2939745) * __n2;
1750 const auto __q2 = static_cast<uint32_t>(__u2 / __p32);
1751 const auto __r2 = static_cast<uint32_t>(__u2 % __p32) / 2939745 / 4;
1752
1753 constexpr auto __p16 = static_cast<uint32_t>(1) << 16;
1754 const auto __n3 = 2141 * __r2 + 197913;
1755 const auto __q3 = __n3 / __p16;
1756 const auto __r3 = __n3 % __p16 / 2141;
1757
1758 const auto __y0 = 100 * __q1 + __q2;
1759 const auto __m0 = __q3;
1760 const auto __d0 = __r3;
1761
1762 const auto __j = __r2 >= 306;
1763 const auto __y1 = __y0 + __j;
1764 const auto __m1 = __j ? __m0 - 12 : __m0;
1765 const auto __d1 = __d0 + 1;
1766
1767 return year_month_day{chrono::year{static_cast<int>(__y1 + __z2)},
1768 chrono::month{__m1}, chrono::day{__d1}};
1769 }
1770
1771 // Days since 1970/01/01.
1772 // Proposition 6.2 of Neri and Schneider,
1773 // "Euclidean Affine Functions and Applications to Calendar Algorithms".
1774 // https://arxiv.org/abs/2102.06959
1775 constexpr days
1776 year_month_day::_M_days_since_epoch() const noexcept
1777 {
1778 auto constexpr __z2 = static_cast<uint32_t>(-1468000);
1779 auto constexpr __r2_e3 = static_cast<uint32_t>(536895458);
1780
1781 const auto __y1 = static_cast<uint32_t>(static_cast<int>(_M_y)) - __z2;
1782 const auto __m1 = static_cast<uint32_t>(static_cast<unsigned>(_M_m));
1783 const auto __d1 = static_cast<uint32_t>(static_cast<unsigned>(_M_d));
1784
1785 const auto __j = static_cast<uint32_t>(__m1 < 3);
1786 const auto __y0 = __y1 - __j;
1787 const auto __m0 = __j ? __m1 + 12 : __m1;
1788 const auto __d0 = __d1 - 1;
1789
1790 const auto __q1 = __y0 / 100;
1791 const auto __yc = 1461 * __y0 / 4 - __q1 + __q1 / 4;
1792 const auto __mc = (979 *__m0 - 2919) / 32;
1793 const auto __dc = __d0;
1794
1795 return days{static_cast<int32_t>(__yc + __mc + __dc - __r2_e3)};
1796 }
1797
1798 // YEAR_MONTH_DAY_LAST
1799
1800 class year_month_day_last
1801 {
1802 private:
1803 chrono::year _M_y;
1804 chrono::month_day_last _M_mdl;
1805
1806 public:
1807 constexpr
1808 year_month_day_last(const chrono::year& __y,
1809 const chrono::month_day_last& __mdl) noexcept
1810 : _M_y{__y}, _M_mdl{__mdl}
1811 { }
1812
1813 template<typename = __detail::__months_years_conversion_disambiguator>
1814 constexpr year_month_day_last&
1815 operator+=(const months& __m) noexcept
1816 {
1817 *this = *this + __m;
1818 return *this;
1819 }
1820
1821 template<typename = __detail::__months_years_conversion_disambiguator>
1822 constexpr year_month_day_last&
1823 operator-=(const months& __m) noexcept
1824 {
1825 *this = *this - __m;
1826 return *this;
1827 }
1828
1829 constexpr year_month_day_last&
1830 operator+=(const years& __y) noexcept
1831 {
1832 *this = *this + __y;
1833 return *this;
1834 }
1835
1836 constexpr year_month_day_last&
1837 operator-=(const years& __y) noexcept
1838 {
1839 *this = *this - __y;
1840 return *this;
1841 }
1842
1843 constexpr chrono::year
1844 year() const noexcept
1845 { return _M_y; }
1846
1847 constexpr chrono::month
1848 month() const noexcept
1849 { return _M_mdl.month(); }
1850
1851 constexpr chrono::month_day_last
1852 month_day_last() const noexcept
1853 { return _M_mdl; }
1854
1855 // Return A day representing the last day of this year, month pair.
1856 constexpr chrono::day
1857 day() const noexcept
1858 {
1859 const auto __m = static_cast<unsigned>(month());
1860
1861 // The result is unspecified if __m < 1 or __m > 12. Hence, assume
1862 // 1 <= __m <= 12. For __m != 2, day() == 30 or day() == 31 or, in
1863 // other words, day () == 30 | b, where b is in {0, 1}.
1864
1865 // If __m in {1, 3, 4, 5, 6, 7}, then b is 1 if, and only if, __m is
1866 // odd. Hence, b = __m & 1 = (__m ^ 0) & 1.
1867
1868 // If __m in {8, 9, 10, 11, 12}, then b is 1 if, and only if, __m is
1869 // even. Hence, b = (__m ^ 1) & 1.
1870
1871 // Therefore, b = (__m ^ c) & 1, where c = 0, if __m < 8, or c = 1 if
1872 // __m >= 8, that is, c = __m >> 3.
1873
1874 // Since 30 = (11110)_2 and __m <= 31 = (11111)_2, the "& 1" in b's
1875 // calculation is unnecessary.
1876
1877 // The performance of this implementation does not depend on look-up
1878 // tables being on the L1 cache.
1879 return chrono::day{__m != 2 ? (__m ^ (__m >> 3)) | 30
1880 : _M_y.is_leap() ? 29 : 28};
1881 }
1882
1883 constexpr
1884 operator sys_days() const noexcept
1885 { return sys_days{year() / month() / day()}; }
1886
1887 explicit constexpr
1888 operator local_days() const noexcept
1889 { return local_days{sys_days{*this}.time_since_epoch()}; }
1890
1891 constexpr bool
1892 ok() const noexcept
1893 { return _M_y.ok() && _M_mdl.ok(); }
1894
1895 friend constexpr bool
1896 operator==(const year_month_day_last& __x,
1897 const year_month_day_last& __y) noexcept
1898 {
1899 return __x.year() == __y.year()
1900 && __x.month_day_last() == __y.month_day_last();
1901 }
1902
1903 friend constexpr strong_ordering
1904 operator<=>(const year_month_day_last& __x,
1905 const year_month_day_last& __y) noexcept
1906 = default;
1907
1908 template<typename = __detail::__months_years_conversion_disambiguator>
1909 friend constexpr year_month_day_last
1910 operator+(const year_month_day_last& __ymdl,
1911 const months& __dm) noexcept
1912 { return (__ymdl.year() / __ymdl.month() + __dm) / last; }
1913
1914 template<typename = __detail::__months_years_conversion_disambiguator>
1915 friend constexpr year_month_day_last
1916 operator+(const months& __dm,
1917 const year_month_day_last& __ymdl) noexcept
1918 { return __ymdl + __dm; }
1919
1920 template<typename = __detail::__months_years_conversion_disambiguator>
1921 friend constexpr year_month_day_last
1922 operator-(const year_month_day_last& __ymdl,
1923 const months& __dm) noexcept
1924 { return __ymdl + -__dm; }
1925
1926 friend constexpr year_month_day_last
1927 operator+(const year_month_day_last& __ymdl,
1928 const years& __dy) noexcept
1929 { return {__ymdl.year() + __dy, __ymdl.month_day_last()}; }
1930
1931 friend constexpr year_month_day_last
1932 operator+(const years& __dy,
1933 const year_month_day_last& __ymdl) noexcept
1934 { return __ymdl + __dy; }
1935
1936 friend constexpr year_month_day_last
1937 operator-(const year_month_day_last& __ymdl,
1938 const years& __dy) noexcept
1939 { return __ymdl + -__dy; }
1940
1941 friend constexpr year_month_day_last
1942 operator/(const year_month& __ym, last_spec) noexcept
1943 { return {__ym.year(), chrono::month_day_last{__ym.month()}}; }
1944
1945 friend constexpr year_month_day_last
1946 operator/(const chrono::year& __y,
1947 const chrono::month_day_last& __mdl) noexcept
1948 { return {__y, __mdl}; }
1949
1950 friend constexpr year_month_day_last
1951 operator/(int __y, const chrono::month_day_last& __mdl) noexcept
1952 { return chrono::year(__y) / __mdl; }
1953
1954 friend constexpr year_month_day_last
1955 operator/(const chrono::month_day_last& __mdl,
1956 const chrono::year& __y) noexcept
1957 { return __y / __mdl; }
1958
1959 friend constexpr year_month_day_last
1960 operator/(const chrono::month_day_last& __mdl, int __y) noexcept
1961 { return chrono::year(__y) / __mdl; }
1962 };
1963
1964 // year_month_day ctor from year_month_day_last
1965 constexpr
1966 year_month_day::year_month_day(const year_month_day_last& __ymdl) noexcept
1967 : _M_y{__ymdl.year()}, _M_m{__ymdl.month()}, _M_d{__ymdl.day()}
1968 { }
1969
1970 constexpr bool
1971 year_month_day::ok() const noexcept
1972 {
1973 if (!_M_y.ok() || !_M_m.ok())
1974 return false;
1975 return chrono::day{1} <= _M_d && _M_d <= (_M_y / _M_m / last).day();
1976 }
1977
1978 // YEAR_MONTH_WEEKDAY
1979
1980 class year_month_weekday
1981 {
1982 private:
1983 chrono::year _M_y;
1984 chrono::month _M_m;
1985 chrono::weekday_indexed _M_wdi;
1986
1987 static constexpr year_month_weekday
1988 _S_from_sys_days(const sys_days& __dp)
1989 {
1990 year_month_day __ymd{__dp};
1991 chrono::weekday __wd{__dp};
1992 auto __index = __wd[(unsigned{__ymd.day()} - 1) / 7 + 1];
1993 return {__ymd.year(), __ymd.month(), __index};
1994 }
1995
1996 public:
1997 year_month_weekday() = default;
1998
1999 constexpr
2000 year_month_weekday(const chrono::year& __y, const chrono::month& __m,
2001 const chrono::weekday_indexed& __wdi) noexcept
2002 : _M_y{__y}, _M_m{__m}, _M_wdi{__wdi}
2003 { }
2004
2005 constexpr
2006 year_month_weekday(const sys_days& __dp) noexcept
2007 : year_month_weekday{_S_from_sys_days(__dp)}
2008 { }
2009
2010 explicit constexpr
2011 year_month_weekday(const local_days& __dp) noexcept
2012 : year_month_weekday{sys_days{__dp.time_since_epoch()}}
2013 { }
2014
2015 template<typename = __detail::__months_years_conversion_disambiguator>
2016 constexpr year_month_weekday&
2017 operator+=(const months& __m) noexcept
2018 {
2019 *this = *this + __m;
2020 return *this;
2021 }
2022
2023 template<typename = __detail::__months_years_conversion_disambiguator>
2024 constexpr year_month_weekday&
2025 operator-=(const months& __m) noexcept
2026 {
2027 *this = *this - __m;
2028 return *this;
2029 }
2030
2031 constexpr year_month_weekday&
2032 operator+=(const years& __y) noexcept
2033 {
2034 *this = *this + __y;
2035 return *this;
2036 }
2037
2038 constexpr year_month_weekday&
2039 operator-=(const years& __y) noexcept
2040 {
2041 *this = *this - __y;
2042 return *this;
2043 }
2044
2045 constexpr chrono::year
2046 year() const noexcept
2047 { return _M_y; }
2048
2049 constexpr chrono::month
2050 month() const noexcept
2051 { return _M_m; }
2052
2053 constexpr chrono::weekday
2054 weekday() const noexcept
2055 { return _M_wdi.weekday(); }
2056
2057 constexpr unsigned
2058 index() const noexcept
2059 { return _M_wdi.index(); }
2060
2061 constexpr chrono::weekday_indexed
2062 weekday_indexed() const noexcept
2063 { return _M_wdi; }
2064
2065 constexpr
2066 operator sys_days() const noexcept
2067 {
2068 auto __d = sys_days{year() / month() / 1};
2069 return __d + (weekday() - chrono::weekday(__d)
2070 + days{(static_cast<int>(index())-1)*7});
2071 }
2072
2073 explicit constexpr
2074 operator local_days() const noexcept
2075 { return local_days{sys_days{*this}.time_since_epoch()}; }
2076
2077 constexpr bool
2078 ok() const noexcept
2079 {
2080 if (!_M_y.ok() || !_M_m.ok() || !_M_wdi.ok())
2081 return false;
2082 if (_M_wdi.index() <= 4)
2083 return true;
2084 days __d = (_M_wdi.weekday()
2085 - chrono::weekday{sys_days{_M_y / _M_m / 1}}
2086 + days((_M_wdi.index()-1)*7 + 1));
2087 __glibcxx_assert(__d.count() >= 1);
2088 return (unsigned)__d.count() <= (unsigned)(_M_y / _M_m / last).day();
2089 }
2090
2091 friend constexpr bool
2092 operator==(const year_month_weekday& __x,
2093 const year_month_weekday& __y) noexcept
2094 {
2095 return __x.year() == __y.year()
2096 && __x.month() == __y.month()
2097 && __x.weekday_indexed() == __y.weekday_indexed();
2098 }
2099
2100 template<typename = __detail::__months_years_conversion_disambiguator>
2101 friend constexpr year_month_weekday
2102 operator+(const year_month_weekday& __ymwd, const months& __dm) noexcept
2103 {
2104 return ((__ymwd.year() / __ymwd.month() + __dm)
2105 / __ymwd.weekday_indexed());
2106 }
2107
2108 template<typename = __detail::__months_years_conversion_disambiguator>
2109 friend constexpr year_month_weekday
2110 operator+(const months& __dm, const year_month_weekday& __ymwd) noexcept
2111 { return __ymwd + __dm; }
2112
2113 friend constexpr year_month_weekday
2114 operator+(const year_month_weekday& __ymwd, const years& __dy) noexcept
2115 { return {__ymwd.year() + __dy, __ymwd.month(), __ymwd.weekday_indexed()}; }
2116
2117 friend constexpr year_month_weekday
2118 operator+(const years& __dy, const year_month_weekday& __ymwd) noexcept
2119 { return __ymwd + __dy; }
2120
2121 template<typename = __detail::__months_years_conversion_disambiguator>
2122 friend constexpr year_month_weekday
2123 operator-(const year_month_weekday& __ymwd, const months& __dm) noexcept
2124 { return __ymwd + -__dm; }
2125
2126 friend constexpr year_month_weekday
2127 operator-(const year_month_weekday& __ymwd, const years& __dy) noexcept
2128 { return __ymwd + -__dy; }
2129
2130 friend constexpr year_month_weekday
2131 operator/(const year_month& __ym,
2132 const chrono::weekday_indexed& __wdi) noexcept
2133 { return {__ym.year(), __ym.month(), __wdi}; }
2134
2135 friend constexpr year_month_weekday
2136 operator/(const chrono::year& __y, const month_weekday& __mwd) noexcept
2137 { return {__y, __mwd.month(), __mwd.weekday_indexed()}; }
2138
2139 friend constexpr year_month_weekday
2140 operator/(int __y, const month_weekday& __mwd) noexcept
2141 { return chrono::year(__y) / __mwd; }
2142
2143 friend constexpr year_month_weekday
2144 operator/(const month_weekday& __mwd, const chrono::year& __y) noexcept
2145 { return __y / __mwd; }
2146
2147 friend constexpr year_month_weekday
2148 operator/(const month_weekday& __mwd, int __y) noexcept
2149 { return chrono::year(__y) / __mwd; }
2150 };
2151
2152 // YEAR_MONTH_WEEKDAY_LAST
2153
2154 class year_month_weekday_last
2155 {
2156 private:
2157 chrono::year _M_y;
2158 chrono::month _M_m;
2159 chrono::weekday_last _M_wdl;
2160
2161 public:
2162 constexpr
2163 year_month_weekday_last(const chrono::year& __y, const chrono::month& __m,
2164 const chrono::weekday_last& __wdl) noexcept
2165 : _M_y{__y}, _M_m{__m}, _M_wdl{__wdl}
2166 { }
2167
2168 template<typename = __detail::__months_years_conversion_disambiguator>
2169 constexpr year_month_weekday_last&
2170 operator+=(const months& __m) noexcept
2171 {
2172 *this = *this + __m;
2173 return *this;
2174 }
2175
2176 template<typename = __detail::__months_years_conversion_disambiguator>
2177 constexpr year_month_weekday_last&
2178 operator-=(const months& __m) noexcept
2179 {
2180 *this = *this - __m;
2181 return *this;
2182 }
2183
2184 constexpr year_month_weekday_last&
2185 operator+=(const years& __y) noexcept
2186 {
2187 *this = *this + __y;
2188 return *this;
2189 }
2190
2191 constexpr year_month_weekday_last&
2192 operator-=(const years& __y) noexcept
2193 {
2194 *this = *this - __y;
2195 return *this;
2196 }
2197
2198 constexpr chrono::year
2199 year() const noexcept
2200 { return _M_y; }
2201
2202 constexpr chrono::month
2203 month() const noexcept
2204 { return _M_m; }
2205
2206 constexpr chrono::weekday
2207 weekday() const noexcept
2208 { return _M_wdl.weekday(); }
2209
2210 constexpr chrono::weekday_last
2211 weekday_last() const noexcept
2212 { return _M_wdl; }
2213
2214 constexpr
2215 operator sys_days() const noexcept
2216 {
2217 const auto __d = sys_days{_M_y / _M_m / last};
2218 return sys_days{(__d - (chrono::weekday{__d}
2219 - _M_wdl.weekday())).time_since_epoch()};
2220 }
2221
2222 explicit constexpr
2223 operator local_days() const noexcept
2224 { return local_days{sys_days{*this}.time_since_epoch()}; }
2225
2226 constexpr bool
2227 ok() const noexcept
2228 { return _M_y.ok() && _M_m.ok() && _M_wdl.ok(); }
2229
2230 friend constexpr bool
2231 operator==(const year_month_weekday_last& __x,
2232 const year_month_weekday_last& __y) noexcept
2233 {
2234 return __x.year() == __y.year()
2235 && __x.month() == __y.month()
2236 && __x.weekday_last() == __y.weekday_last();
2237 }
2238
2239 template<typename = __detail::__months_years_conversion_disambiguator>
2240 friend constexpr year_month_weekday_last
2241 operator+(const year_month_weekday_last& __ymwdl,
2242 const months& __dm) noexcept
2243 {
2244 return ((__ymwdl.year() / __ymwdl.month() + __dm)
2245 / __ymwdl.weekday_last());
2246 }
2247
2248 template<typename = __detail::__months_years_conversion_disambiguator>
2249 friend constexpr year_month_weekday_last
2250 operator+(const months& __dm,
2251 const year_month_weekday_last& __ymwdl) noexcept
2252 { return __ymwdl + __dm; }
2253
2254 friend constexpr year_month_weekday_last
2255 operator+(const year_month_weekday_last& __ymwdl,
2256 const years& __dy) noexcept
2257 { return {__ymwdl.year() + __dy, __ymwdl.month(), __ymwdl.weekday_last()}; }
2258
2259 friend constexpr year_month_weekday_last
2260 operator+(const years& __dy,
2261 const year_month_weekday_last& __ymwdl) noexcept
2262 { return __ymwdl + __dy; }
2263
2264 template<typename = __detail::__months_years_conversion_disambiguator>
2265 friend constexpr year_month_weekday_last
2266 operator-(const year_month_weekday_last& __ymwdl,
2267 const months& __dm) noexcept
2268 { return __ymwdl + -__dm; }
2269
2270 friend constexpr year_month_weekday_last
2271 operator-(const year_month_weekday_last& __ymwdl,
2272 const years& __dy) noexcept
2273 { return __ymwdl + -__dy; }
2274
2275 friend constexpr year_month_weekday_last
2276 operator/(const year_month& __ym,
2277 const chrono::weekday_last& __wdl) noexcept
2278 { return {__ym.year(), __ym.month(), __wdl}; }
2279
2280 friend constexpr year_month_weekday_last
2281 operator/(const chrono::year& __y,
2282 const chrono::month_weekday_last& __mwdl) noexcept
2283 { return {__y, __mwdl.month(), __mwdl.weekday_last()}; }
2284
2285 friend constexpr year_month_weekday_last
2286 operator/(int __y, const chrono::month_weekday_last& __mwdl) noexcept
2287 { return chrono::year(__y) / __mwdl; }
2288
2289 friend constexpr year_month_weekday_last
2290 operator/(const chrono::month_weekday_last& __mwdl,
2291 const chrono::year& __y) noexcept
2292 { return __y / __mwdl; }
2293
2294 friend constexpr year_month_weekday_last
2295 operator/(const chrono::month_weekday_last& __mwdl, int __y) noexcept
2296 { return chrono::year(__y) / __mwdl; }
2297 };
2298
2299 // HH_MM_SS
2300
2301 /// @cond undocumented
2302 namespace __detail
2303 {
2304 consteval long long
2305 __pow10(unsigned __n)
2306 {
2307 long long __r = 1;
2308 while (__n-- > 0)
2309 __r *= 10;
2310 return __r;
2311 }
2312 }
2313 /// @endcond
2314
2315 /** Utility for splitting a duration into hours, minutes, and seconds
2316 *
2317 * This is a convenience type that provides accessors for the constituent
2318 * parts (hours, minutes, seconds and subseconds) of a duration.
2319 *
2320 * @since C++20
2321 */
2322 template<typename _Duration>
2323 class hh_mm_ss
2324 {
2325 static_assert( __is_duration<_Duration>::value );
2326
2327 private:
2328 static consteval int
2329 _S_fractional_width()
2330 {
2331 auto __den = _Duration::period::den;
2332 const int __multiplicity_2 = std::__countr_zero((uintmax_t)__den);
2333 __den >>= __multiplicity_2;
2334 int __multiplicity_5 = 0;
2335 while ((__den % 5) == 0)
2336 {
2337 ++__multiplicity_5;
2338 __den /= 5;
2339 }
2340 if (__den != 1)
2341 return 6;
2342
2343 int __width = (__multiplicity_2 > __multiplicity_5
2344 ? __multiplicity_2 : __multiplicity_5);
2345 if (__width > 18)
2346 __width = 18;
2347 return __width;
2348 }
2349
2350 constexpr
2351 hh_mm_ss(_Duration __d, bool __is_neg)
2352 : _M_h (duration_cast<chrono::hours>(__d)),
2353 _M_m (duration_cast<chrono::minutes>(__d - hours())),
2354 _M_s (duration_cast<chrono::seconds>(__d - hours() - minutes())),
2355 _M_is_neg(__is_neg)
2356 {
2357 auto __ss = __d - hours() - minutes() - seconds();
2358 if constexpr (treat_as_floating_point_v<typename precision::rep>)
2359 _M_ss._M_r = __ss.count();
2360 else if constexpr (precision::period::den != 1)
2361 _M_ss._M_r = duration_cast<precision>(__ss).count();
2362 }
2363
2364 static constexpr _Duration
2365 _S_abs(_Duration __d)
2366 {
2368 return chrono::abs(__d);
2369 else
2370 return __d;
2371 }
2372
2373 public:
2374 static constexpr unsigned fractional_width = {_S_fractional_width()};
2375
2376 using precision
2377 = duration<common_type_t<typename _Duration::rep,
2378 chrono::seconds::rep>,
2379 ratio<1, __detail::__pow10(fractional_width)>>;
2380
2381 constexpr hh_mm_ss() noexcept = default;
2382
2383 constexpr explicit
2384 hh_mm_ss(_Duration __d)
2385 : hh_mm_ss(_S_abs(__d), __d < _Duration::zero())
2386 { }
2387
2388 constexpr bool
2389 is_negative() const noexcept
2390 {
2391 if constexpr (!_S_is_unsigned)
2392 return _M_is_neg;
2393 else
2394 return false;
2395 }
2396
2397 constexpr chrono::hours
2398 hours() const noexcept
2399 { return _M_h; }
2400
2401 constexpr chrono::minutes
2402 minutes() const noexcept
2403 { return _M_m; }
2404
2405 constexpr chrono::seconds
2406 seconds() const noexcept
2407 { return _M_s; }
2408
2409 constexpr precision
2410 subseconds() const noexcept
2411 { return static_cast<precision>(_M_ss); }
2412
2413 constexpr explicit
2414 operator precision() const noexcept
2415 { return to_duration(); }
2416
2417 constexpr precision
2418 to_duration() const noexcept
2419 {
2420 if constexpr (!_S_is_unsigned)
2421 if (_M_is_neg)
2422 return -(_M_h + _M_m + _M_s + subseconds());
2423 return _M_h + _M_m + _M_s + subseconds();
2424 }
2425
2426 private:
2427 static constexpr bool _S_is_unsigned
2428 = __and_v<is_integral<typename _Duration::rep>,
2430
2431 template<typename _Ratio>
2432 using __byte_duration = duration<unsigned char, _Ratio>;
2433
2434 // The type of the _M_ss member that holds the subsecond precision.
2435 template<typename _Dur>
2436 struct __subseconds
2437 {
2438 typename _Dur::rep _M_r{};
2439
2440 constexpr explicit
2441 operator _Dur() const noexcept
2442 { return _Dur(_M_r); }
2443 };
2444
2445 // An empty class if this precision doesn't need subseconds.
2446 template<typename _Rep>
2447 requires (!treat_as_floating_point_v<_Rep>)
2448 struct __subseconds<duration<_Rep, ratio<1>>>
2449 {
2450 constexpr explicit
2451 operator duration<_Rep, ratio<1>>() const noexcept
2452 { return {}; }
2453 };
2454
2455 template<typename _Rep, typename _Period>
2456 requires (!treat_as_floating_point_v<_Rep>)
2457 && ratio_less_v<_Period, ratio<1, 1>>
2458 && ratio_greater_equal_v<_Period, ratio<1, 250>>
2459 struct __subseconds<duration<_Rep, _Period>>
2460 {
2461 unsigned char _M_r{};
2462
2463 constexpr explicit
2464 operator duration<_Rep, _Period>() const noexcept
2465 { return duration<_Rep, _Period>(_M_r); }
2466 };
2467
2468 template<typename _Rep, typename _Period>
2469 requires (!treat_as_floating_point_v<_Rep>)
2470 && ratio_less_v<_Period, ratio<1, 250>>
2471 && ratio_greater_equal_v<_Period, ratio<1, 4000000000>>
2472 struct __subseconds<duration<_Rep, _Period>>
2473 {
2474 uint_least32_t _M_r{};
2475
2476 constexpr explicit
2477 operator duration<_Rep, _Period>() const noexcept
2478 { return duration<_Rep, _Period>(_M_r); }
2479 };
2480
2481 chrono::hours _M_h{};
2482 __byte_duration<ratio<60>> _M_m{};
2483 __byte_duration<ratio<1>> _M_s{};
2484 bool _M_is_neg{};
2485 __subseconds<precision> _M_ss{};
2486 };
2487
2488 // 12/24 HOURS FUNCTIONS
2489
2490 constexpr bool
2491 is_am(const hours& __h) noexcept
2492 { return 0h <= __h && __h <= 11h; }
2493
2494 constexpr bool
2495 is_pm(const hours& __h) noexcept
2496 { return 12h <= __h && __h <= 23h; }
2497
2498 constexpr hours
2499 make12(const hours& __h) noexcept
2500 {
2501 if (__h == 0h)
2502 return 12h;
2503 else if (__h > 12h)
2504 return __h - 12h;
2505 return __h;
2506 }
2507
2508 constexpr hours
2509 make24(const hours& __h, bool __is_pm) noexcept
2510 {
2511 if (!__is_pm)
2512 {
2513 if (__h == 12h)
2514 return 0h;
2515 else
2516 return __h;
2517 }
2518 else
2519 {
2520 if (__h == 12h)
2521 return __h;
2522 else
2523 return __h + 12h;
2524 }
2525 }
2526
2527#if _GLIBCXX_HOSTED
2528#if _GLIBCXX_USE_CXX11_ABI || ! _GLIBCXX_USE_DUAL_ABI
2529 // C++20 [time.zones] Time zones
2530
2531 struct tzdb;
2532
2533 struct sys_info
2534 {
2535 sys_seconds begin;
2536 sys_seconds end;
2537 seconds offset;
2538 minutes save;
2539 string abbrev;
2540 };
2541
2542 struct local_info
2543 {
2544 static constexpr int unique = 0;
2545 static constexpr int nonexistent = 1;
2546 static constexpr int ambiguous = 2;
2547
2548 int result;
2549 sys_info first;
2550 sys_info second;
2551 };
2552
2553 class nonexistent_local_time : public runtime_error
2554 {
2555 public:
2556 template<typename _Duration>
2557 nonexistent_local_time(const local_time<_Duration>& __tp,
2558 const local_info& __i)
2559 : runtime_error(_S_make_what_str(__tp, __i))
2560 { __glibcxx_assert(__i.result == local_info::nonexistent); }
2561
2562 private:
2563 template<typename _Duration>
2564 static string
2565 _S_make_what_str(const local_time<_Duration>& __tp,
2566 const local_info& __i)
2567 {
2568 std::ostringstream __os;
2569 __os << __tp << " is in a gap between\n"
2570 << local_seconds(__i.first.end.time_since_epoch())
2571 + __i.first.offset << ' ' << __i.first.abbrev << " and\n"
2572 << local_seconds(__i.second.begin.time_since_epoch())
2573 + __i.second.offset << ' ' << __i.second.abbrev
2574 << " which are both equivalent to\n"
2575 << __i.first.end << " UTC";
2576 return std::move(__os).str();
2577 }
2578 };
2579
2580 class ambiguous_local_time : public runtime_error
2581 {
2582 public:
2583 template<typename _Duration>
2584 ambiguous_local_time(const local_time<_Duration>& __tp,
2585 const local_info& __i)
2586 : runtime_error(_S_make_what_str(__tp, __i))
2587 { __glibcxx_assert(__i.result == local_info::ambiguous); }
2588
2589 private:
2590 template<typename _Duration>
2591 static string
2592 _S_make_what_str(const local_time<_Duration>& __tp,
2593 const local_info& __i)
2594 {
2595 std::ostringstream __os;
2596 __os << __tp << " is ambiguous. It could be\n"
2597 << __tp << ' ' << __i.first.abbrev << " == "
2598 << __tp - __i.first.offset << " UTC or\n"
2599 << __tp << ' ' << __i.second.abbrev << " == "
2600 << __tp - __i.second.offset << " UTC";
2601 return std::move(__os).str();
2602 }
2603 };
2604
2605 template<typename _Duration>
2606 [[noreturn]] void
2607 __throw_bad_local_time(const local_time<_Duration>& __tp,
2608 const local_info& __i)
2609 {
2610#if __cpp_exceptions
2611 if (__i.result == local_info::nonexistent)
2612 throw nonexistent_local_time(__tp, __i);
2613 throw ambiguous_local_time(__tp, __i);
2614#else
2615 __builtin_abort();
2616#endif
2617 }
2618
2619 enum class choose { earliest, latest };
2620
2621 class time_zone
2622 {
2623 public:
2624 time_zone(time_zone&&) = default;
2625 time_zone& operator=(time_zone&&) = default;
2626
2627 ~time_zone();
2628
2629 [[nodiscard]]
2630 string_view name() const noexcept { return _M_name; }
2631
2632 template<typename _Duration>
2633 sys_info
2634 get_info(const sys_time<_Duration>& __st) const
2635 { return _M_get_sys_info(chrono::floor<seconds>(__st)); }
2636
2637 template<typename _Duration>
2638 local_info
2639 get_info(const local_time<_Duration>& __tp) const
2640 { return _M_get_local_info(chrono::floor<seconds>(__tp)); }
2641
2642 template<typename _Duration>
2643 sys_time<common_type_t<_Duration, seconds>>
2644 to_sys(const local_time<_Duration>& __tp) const
2645 {
2646 local_info __info = get_info(__tp);
2647
2648 if (__info.result != local_info::unique)
2649 __throw_bad_local_time(__tp, __info);
2650
2651 return sys_time<_Duration>(__tp.time_since_epoch())
2652 - __info.first.offset;
2653 }
2654
2655 template<typename _Duration>
2656 sys_time<common_type_t<_Duration, seconds>>
2657 to_sys(const local_time<_Duration>& __tp, choose __z) const
2658 {
2659 local_info __info = get_info(__tp);
2660
2661 if (__info.result == local_info::nonexistent)
2662 return __info.first.end; // Last second of the previous sys_info.
2663
2664 sys_time<_Duration> __st(__tp.time_since_epoch());
2665
2666 if (__info.result == local_info::ambiguous && __z == choose::latest)
2667 return __st - __info.second.offset; // Time in the later sys_info.
2668 // else if __z == earliest, use __info.first.offset as below:
2669
2670 return __st - __info.first.offset;
2671 }
2672
2673 template<typename _Duration>
2674 local_time<common_type_t<_Duration, seconds>>
2675 to_local(const sys_time<_Duration>& __tp) const
2676 {
2677 auto __d = (__tp + get_info(__tp).offset).time_since_epoch();
2678 return local_time<common_type_t<_Duration, seconds>>(__d);
2679 }
2680
2681 [[nodiscard]] friend bool
2682 operator==(const time_zone& __x, const time_zone& __y) noexcept
2683 { return __x._M_name == __y._M_name; }
2684
2685 [[nodiscard]] friend strong_ordering
2686 operator<=>(const time_zone& __x, const time_zone& __y) noexcept
2687 { return __x._M_name <=> __y._M_name; }
2688
2689 private:
2690 sys_info _M_get_sys_info(sys_seconds) const;
2691 local_info _M_get_local_info(local_seconds) const;
2692
2693 friend const tzdb& reload_tzdb();
2694 friend struct tzdb;
2695 friend class tzdb_list;
2696
2697 struct _Impl;
2698
2699 explicit time_zone(unique_ptr<_Impl> __p);
2700 string _M_name;
2701 unique_ptr<_Impl> _M_impl;
2702 };
2703
2704 const time_zone* locate_zone(string_view __tz_name);
2705 const time_zone* current_zone();
2706
2707 /** The list of `chrono::tzdb` objects
2708 *
2709 * A single object of this type is constructed by the C++ runtime,
2710 * and can be accessed by calling `chrono::get_tzdb_list()`.
2711 *
2712 * The front of the list is the current `tzdb` object and can be accessed
2713 * via `chrono::get_tzdb_list().front()` or `chrono::get_tzdb()` or
2714 * `*chrono::get_tzdb_list().begin()`.
2715 *
2716 * The `chrono::reload_tzdb()` function will check for a newer version
2717 * and if found, insert it at the front of the list.
2718 *
2719 * @since C++20
2720 */
2721 class tzdb_list
2722 {
2723 struct _Node;
2724
2725 public:
2726 tzdb_list(const tzdb_list&) = delete;
2727 tzdb_list& operator=(const tzdb_list&) = delete;
2728
2729 /** An iterator into the `tzdb_list`
2730 *
2731 * As a extension, in libstdc++ each `tzdb` is reference-counted
2732 * and the `const_iterator` type shares ownership of the object it
2733 * refers to. This ensures that a `tzdb` erased from the list will
2734 * not be destroyed while there is an iterator that refers to it.
2735 */
2736 class const_iterator
2737 {
2738 public:
2739 using value_type = tzdb;
2740 using reference = const tzdb&;
2741 using pointer = const tzdb*;
2742 using difference_type = ptrdiff_t;
2743 using iterator_category = forward_iterator_tag;
2744
2745 constexpr const_iterator() = default;
2746 const_iterator(const const_iterator&) = default;
2747 const_iterator(const_iterator&&) = default;
2748 const_iterator& operator=(const const_iterator&) = default;
2749 const_iterator& operator=(const_iterator&&) = default;
2750
2751 reference operator*() const noexcept;
2752 pointer operator->() const noexcept { return &**this; }
2753 const_iterator& operator++();
2754 const_iterator operator++(int);
2755
2756 bool operator==(const const_iterator&) const noexcept = default;
2757
2758 private:
2759 explicit const_iterator(const shared_ptr<_Node>&) noexcept;
2760
2761 friend class tzdb_list;
2762
2763 shared_ptr<_Node> _M_node;
2764 void* _M_reserved = nullptr;
2765 };
2766
2767 /** Access the current `tzdb` at the front of the list.
2768 *
2769 * This returns a reference to the same object as `chrono::get_tzdb()`.
2770 *
2771 * @returns A reference to the current tzdb object.
2772 * @since C++20
2773 */
2774 const tzdb& front() const noexcept;
2775
2776 /** Remove the tzdb object _after_ the one the iterator refers to.
2777 *
2778 * Calling this function concurrently with any of `front()`, `begin()`,
2779 * or `end()` does not cause a data race, but in general this function
2780 * is not thread-safe. The behaviour may be undefined if erasing an
2781 * element from the list while another thread is calling the same
2782 * function, or incrementing an iterator into the list, or accessing
2783 * the element being erased (unless it is accessed through an iterator).
2784 *
2785 * @param __p A dereferenceable iterator.
2786 * @returns An iterator the element after the one that was erased
2787 * (or `end()` if there is no such element).
2788 * @since C++20
2789 */
2791
2792 const_iterator begin() const noexcept;
2793 const_iterator end() const noexcept { return {}; }
2794 const_iterator cbegin() const noexcept { return begin(); }
2795 const_iterator cend() const noexcept { return end(); }
2796
2797 private:
2798 constexpr explicit tzdb_list(nullptr_t);
2799
2800 friend tzdb_list& get_tzdb_list();
2801 friend const tzdb& get_tzdb();
2802 friend const tzdb& reload_tzdb();
2803 friend struct tzdb;
2804 friend class leap_second;
2805 friend struct time_zone::_Impl;
2806 friend class time_zone_link;
2807 };
2808
2809 class time_zone_link
2810 {
2811 public:
2812 time_zone_link(time_zone_link&&) = default;
2813 time_zone_link& operator=(time_zone_link&&) = default;
2814
2815 string_view name() const noexcept { return _M_name; }
2816 string_view target() const noexcept { return _M_target; }
2817
2818 friend bool
2819 operator==(const time_zone_link& __x, const time_zone_link& __y) noexcept
2820 { return __x.name() == __y.name(); }
2821
2822 friend strong_ordering
2823 operator<=>(const time_zone_link& __x, const time_zone_link& __y) noexcept
2824 { return __x.name() <=> __y.name(); }
2825
2826 private:
2827 friend const tzdb& reload_tzdb();
2828 friend struct tzdb_list::_Node;
2829
2830 explicit time_zone_link(nullptr_t) { }
2831
2832 string _M_name;
2833 string _M_target;
2834 };
2835
2836 class leap_second
2837 {
2838 public:
2839 leap_second(const leap_second&) = default;
2840 leap_second& operator=(const leap_second&) = default;
2841
2842 [[nodiscard]]
2843 constexpr sys_seconds
2844 date() const noexcept
2845 {
2846 if (_M_s >= _M_s.zero()) [[likely]]
2847 return sys_seconds(_M_s);
2848 return sys_seconds(-_M_s);
2849 }
2850
2851 [[nodiscard]]
2852 constexpr seconds
2853 value() const noexcept
2854 {
2855 if (_M_s >= _M_s.zero()) [[likely]]
2856 return seconds(1);
2857 return seconds(-1);
2858 }
2859
2860 // This can be defaulted because the database will never contain two
2861 // leap_second objects with the same date but different signs.
2862 [[nodiscard]] friend constexpr bool
2863 operator==(const leap_second&, const leap_second&) noexcept = default;
2864
2865 [[nodiscard]] friend constexpr strong_ordering
2866 operator<=>(const leap_second& __x, const leap_second& __y) noexcept
2867 { return __x.date() <=> __y.date(); }
2868
2869 template<typename _Duration>
2870 [[nodiscard]] friend constexpr bool
2871 operator==(const leap_second& __x,
2872 const sys_time<_Duration>& __y) noexcept
2873 { return __x.date() == __y; }
2874
2875 template<typename _Duration>
2876 [[nodiscard]] friend constexpr bool
2877 operator<(const leap_second& __x,
2878 const sys_time<_Duration>& __y) noexcept
2879 { return __x.date() < __y; }
2880
2881 template<typename _Duration>
2882 [[nodiscard]] friend constexpr bool
2883 operator<(const sys_time<_Duration>& __x,
2884 const leap_second& __y) noexcept
2885 { return __x < __y.date(); }
2886
2887 template<typename _Duration>
2888 [[nodiscard]] friend constexpr bool
2889 operator>(const leap_second& __x,
2890 const sys_time<_Duration>& __y) noexcept
2891 { return __y < __x.date(); }
2892
2893 template<typename _Duration>
2894 [[nodiscard]] friend constexpr bool
2895 operator>(const sys_time<_Duration>& __x,
2896 const leap_second& __y) noexcept
2897 { return __y.date() < __x; }
2898
2899 template<typename _Duration>
2900 [[nodiscard]] friend constexpr bool
2901 operator<=(const leap_second& __x,
2902 const sys_time<_Duration>& __y) noexcept
2903 { return !(__y < __x.date()); }
2904
2905 template<typename _Duration>
2906 [[nodiscard]] friend constexpr bool
2907 operator<=(const sys_time<_Duration>& __x,
2908 const leap_second& __y) noexcept
2909 { return !(__y.date() < __x); }
2910
2911 template<typename _Duration>
2912 [[nodiscard]] friend constexpr bool
2913 operator>=(const leap_second& __x,
2914 const sys_time<_Duration>& __y) noexcept
2915 { return !(__x.date() < __y); }
2916
2917 template<typename _Duration>
2918 [[nodiscard]] friend constexpr bool
2919 operator>=(const sys_time<_Duration>& __x,
2920 const leap_second& __y) noexcept
2921 { return !(__x < __y.date()); }
2922
2923 // This is a simplified form of the constraint specified in the standard,
2924 // three_way_comparable_with<sys_seconds, sys_time<_Duration>>.
2925 template<three_way_comparable_with<seconds> _Duration>
2926 [[nodiscard]] friend constexpr auto
2927 operator<=>(const leap_second& __x,
2928 const sys_time<_Duration>& __y) noexcept
2929 { return __x.date() <=> __y; }
2930
2931 private:
2932 explicit leap_second(seconds::rep __s) : _M_s(__s) { }
2933
2934 friend struct tzdb_list::_Node;
2935
2936 friend const tzdb& reload_tzdb();
2937
2938 template<typename _Duration>
2939 friend leap_second_info
2940 get_leap_second_info(const utc_time<_Duration>&);
2941
2942 seconds _M_s; // == date().time_since_epoch() * value().count()
2943 };
2944
2945 template<class _Tp> struct zoned_traits { };
2946
2947 template<>
2948 struct zoned_traits<const time_zone*>
2949 {
2950 static const time_zone*
2951 default_zone()
2952 { return std::chrono::locate_zone("UTC"); }
2953
2954 static const time_zone*
2955 locate_zone(string_view __name)
2956 { return std::chrono::locate_zone(__name); }
2957 };
2958
2959 struct tzdb
2960 {
2961 string version;
2962 _GLIBCXX_STD_C::vector<time_zone> zones;
2963 _GLIBCXX_STD_C::vector<time_zone_link> links;
2964 _GLIBCXX_STD_C::vector<leap_second> leap_seconds;
2965
2966 const time_zone*
2967 locate_zone(string_view __tz_name) const;
2968
2969 const time_zone*
2970 current_zone() const;
2971
2972 private:
2973 friend const tzdb& reload_tzdb();
2974 friend class time_zone;
2975 friend struct tzdb_list::_Node;
2976 };
2977
2978 tzdb_list& get_tzdb_list();
2979 const tzdb& get_tzdb();
2980
2981 const tzdb& reload_tzdb();
2982 string remote_version();
2983
2984 template<typename _Duration, typename _TimeZonePtr = const time_zone*>
2985 class zoned_time
2986 {
2987 static_assert(__is_duration_v<_Duration>);
2988
2989 using _Traits = zoned_traits<_TimeZonePtr>;
2990
2991 // Every constructor that accepts a string_view as its first parameter
2992 // does not participate in class template argument deduction.
2993 using string_view = type_identity_t<std::string_view>;
2994
2995 public:
2996 using duration = common_type_t<_Duration, seconds>;
2997
2998 zoned_time() requires requires { _Traits::default_zone(); }
2999 { }
3000
3001 zoned_time(const zoned_time&) = default;
3002 zoned_time& operator=(const zoned_time&) = default;
3003
3004 zoned_time(const sys_time<_Duration>& __st)
3005 requires requires { _Traits::default_zone(); }
3006 : _M_tp(__st)
3007 { }
3008
3009 explicit
3010 zoned_time(_TimeZonePtr __z) : _M_zone(std::move(__z)) { }
3011
3012 explicit
3013 zoned_time(string_view __name)
3014 requires requires {
3015 _TimeZonePtr{_Traits::locate_zone(std::string_view{})};
3016 }
3017 : _M_zone(_Traits::locate_zone(__name))
3018 { }
3019
3020 template<typename _Duration2>
3021 zoned_time(const zoned_time<_Duration2, _TimeZonePtr>& __zt)
3022 requires is_convertible_v<sys_time<_Duration2>, sys_time<_Duration>>
3023 : _M_zone(__zt._M_zone), _M_tp(__zt._M_tp)
3024 { }
3025
3026 zoned_time(_TimeZonePtr __z, const sys_time<_Duration>& __st)
3027 : _M_zone(std::move(__z)), _M_tp(__st)
3028 { }
3029
3030 zoned_time(string_view __name, const sys_time<_Duration>& __st)
3031 : zoned_time(_Traits::locate_zone(__name), __st)
3032 { }
3033
3034 zoned_time(_TimeZonePtr __z, const local_time<_Duration>& __tp)
3035 requires requires {
3036 { __z->to_sys(__tp) } -> convertible_to<sys_time<_Duration>>;
3037 }
3038 : _M_zone(std::move(__z)), _M_tp(_M_zone->to_sys(__tp))
3039 { }
3040
3041 zoned_time(string_view __name, const local_time<_Duration>& __tp)
3042 requires requires (_TimeZonePtr __z) {
3043 { _Traits::locate_zone(__name) } -> convertible_to<_TimeZonePtr>;
3044 { __z->to_sys(__tp) } -> convertible_to<sys_time<_Duration>>;
3045 }
3046 : zoned_time(_Traits::locate_zone(__name), __tp)
3047 { }
3048
3049 zoned_time(_TimeZonePtr __z, const local_time<_Duration>& __tp,
3050 choose __c)
3051 requires requires {
3052 { __z->to_sys(__tp, __c) } -> convertible_to<sys_time<_Duration>>;
3053 }
3054 : _M_zone(std::move(__z)), _M_tp(_M_zone->to_sys(__tp, __c))
3055 { }
3056
3057 zoned_time(string_view __name, const local_time<_Duration>& __tp,
3058 choose __c)
3059 requires requires (_TimeZonePtr __z) {
3060 { _Traits::locate_zone(__name) } -> convertible_to<_TimeZonePtr>;
3061 { __z->to_sys(__tp, __c) } -> convertible_to<sys_time<_Duration>>;
3062 }
3063 : _M_zone(_Traits::locate_zone(__name)),
3064 _M_tp(_M_zone->to_sys(__tp, __c))
3065 { }
3066
3067 template<typename _Duration2, typename _TimeZonePtr2>
3068 zoned_time(_TimeZonePtr __z,
3069 const zoned_time<_Duration2, _TimeZonePtr2>& __zt)
3070 requires is_convertible_v<sys_time<_Duration2>, sys_time<_Duration>>
3071 : _M_zone(__z), _M_tp(__zt._M_tp)
3072 { }
3073
3074 template<typename _Duration2, typename _TimeZonePtr2>
3075 zoned_time(_TimeZonePtr __z,
3076 const zoned_time<_Duration2, _TimeZonePtr2>& __zt,
3077 choose)
3078 requires is_convertible_v<sys_time<_Duration2>, sys_time<_Duration>>
3079 : _M_zone(__z), _M_tp(__zt._M_tp)
3080 { }
3081
3082 template<typename _Duration2, typename _TimeZonePtr2>
3083 zoned_time(string_view __name,
3084 const zoned_time<_Duration2, _TimeZonePtr2>& __zt)
3085 requires is_convertible_v<sys_time<_Duration2>, sys_time<_Duration>>
3086 && requires {
3087 { _Traits::locate_zone(__name) } -> convertible_to<_TimeZonePtr>;
3088 }
3089 : _M_zone(_Traits::locate_zone(__name)), _M_tp(__zt._M_tp)
3090 { }
3091
3092 template<typename _Duration2, typename _TimeZonePtr2>
3093 zoned_time(string_view __name,
3094 const zoned_time<_Duration2, _TimeZonePtr2>& __zt,
3095 choose)
3096 requires is_convertible_v<sys_time<_Duration2>, sys_time<_Duration>>
3097 && requires {
3098 { _Traits::locate_zone(__name) } -> convertible_to<_TimeZonePtr>;
3099 }
3100 : _M_zone(_Traits::locate_zone(__name)), _M_tp(__zt._M_tp)
3101 { }
3102
3103 zoned_time&
3104 operator=(const sys_time<_Duration>& __st)
3105 {
3106 _M_tp = __st;
3107 return *this;
3108 }
3109
3110 zoned_time&
3111 operator=(const local_time<_Duration>& __lt)
3112 {
3113 _M_tp = _M_zone->to_sys(__lt);
3114 return *this;
3115 }
3116
3117 [[nodiscard]]
3118 operator sys_time<duration>() const { return _M_tp; }
3119
3120 [[nodiscard]]
3121 explicit operator local_time<duration>() const
3122 { return get_local_time(); }
3123
3124 [[nodiscard]]
3125 _TimeZonePtr
3126 get_time_zone() const
3127 { return _M_zone; }
3128
3129 [[nodiscard]]
3130 local_time<duration>
3131 get_local_time() const
3132 { return _M_zone->to_local(_M_tp); }
3133
3134 [[nodiscard]]
3135 sys_time<duration>
3136 get_sys_time() const
3137 { return _M_tp; }
3138
3139 [[nodiscard]]
3140 sys_info
3141 get_info() const
3142 { return _M_zone->get_info(_M_tp); }
3143
3144 [[nodiscard]] friend bool
3145 operator==(const zoned_time&, const zoned_time&) = default;
3146
3147 private:
3148 _TimeZonePtr _M_zone{ _Traits::default_zone() };
3149 sys_time<duration> _M_tp{};
3150
3151 template<typename _Duration2, typename _TimeZonePtr2>
3152 friend class zoned_time;
3153 };
3154
3155 zoned_time() -> zoned_time<seconds>;
3156
3157 template<typename _Duration>
3158 zoned_time(sys_time<_Duration>)
3159 -> zoned_time<common_type_t<_Duration, seconds>>;
3160
3161 /// @cond undocumented
3162 template<typename _TimeZonePtrOrName>
3163 using __time_zone_representation
3164 = __conditional_t<is_convertible_v<_TimeZonePtrOrName, string_view>,
3165 const time_zone*,
3166 remove_cvref_t<_TimeZonePtrOrName>>;
3167 /// @endcond
3168
3169 template<typename _TimeZonePtrOrName>
3170 zoned_time(_TimeZonePtrOrName&&)
3171 -> zoned_time<seconds, __time_zone_representation<_TimeZonePtrOrName>>;
3172
3173 template<typename _TimeZonePtrOrName, typename _Duration>
3174 zoned_time(_TimeZonePtrOrName&&, sys_time<_Duration>)
3175 -> zoned_time<common_type_t<_Duration, seconds>,
3176 __time_zone_representation<_TimeZonePtrOrName>>;
3177
3178 template<typename _TimeZonePtrOrName, typename _Duration>
3179 zoned_time(_TimeZonePtrOrName&&, local_time<_Duration>,
3180 choose = choose::earliest)
3181 -> zoned_time<common_type_t<_Duration, seconds>,
3182 __time_zone_representation<_TimeZonePtrOrName>>;
3183
3184 template<typename _Duration, typename _TimeZonePtrOrName,
3185 typename _TimeZonePtr2>
3186 zoned_time(_TimeZonePtrOrName&&, zoned_time<_Duration, _TimeZonePtr2>,
3187 choose = choose::earliest)
3188 -> zoned_time<common_type_t<_Duration, seconds>,
3189 __time_zone_representation<_TimeZonePtrOrName>>;
3190
3191 template<typename _Dur1, typename _TZPtr1, typename _Dur2, typename _TZPtr2>
3192 [[nodiscard]]
3193 inline bool
3194 operator==(const zoned_time<_Dur1, _TZPtr1>& __x,
3195 const zoned_time<_Dur2, _TZPtr2>& __y)
3196 {
3197 return __x.get_time_zone() == __y.get_time_zone()
3198 && __x.get_sys_time() == __y.get_sys_time();
3199 }
3200
3201 using zoned_seconds = zoned_time<seconds>;
3202#endif // _GLIBCXX_USE_CXX11_ABI || ! _GLIBCXX_USE_DUAL_ABI
3203
3204namespace __detail
3205{
3206 inline leap_second_info
3207 __get_leap_second_info(sys_seconds __ss, bool __is_utc)
3208 {
3209 if (__ss < sys_seconds{}) [[unlikely]]
3210 return {};
3211
3212 const seconds::rep __leaps[] {
3213 78796800, // 1 Jul 1972
3214 94694400, // 1 Jan 1973
3215 126230400, // 1 Jan 1974
3216 157766400, // 1 Jan 1975
3217 189302400, // 1 Jan 1976
3218 220924800, // 1 Jan 1977
3219 252460800, // 1 Jan 1978
3220 283996800, // 1 Jan 1979
3221 315532800, // 1 Jan 1980
3222 362793600, // 1 Jul 1981
3223 394329600, // 1 Jul 1982
3224 425865600, // 1 Jul 1983
3225 489024000, // 1 Jul 1985
3226 567993600, // 1 Jan 1988
3227 631152000, // 1 Jan 1990
3228 662688000, // 1 Jan 1991
3229 709948800, // 1 Jul 1992
3230 741484800, // 1 Jul 1993
3231 773020800, // 1 Jul 1994
3232 820454400, // 1 Jan 1996
3233 867715200, // 1 Jul 1997
3234 915148800, // 1 Jan 1999
3235 1136073600, // 1 Jan 2006
3236 1230768000, // 1 Jan 2009
3237 1341100800, // 1 Jul 2012
3238 1435708800, // 1 Jul 2015
3239 1483228800, // 1 Jan 2017
3240 };
3241 // The list above is known to be valid until (at least) this date
3242 // and only contains positive leap seconds.
3243 const sys_seconds __expires(1766880000s); // 2025-12-28 00:00:00 UTC
3244
3245#if _GLIBCXX_USE_CXX11_ABI || ! _GLIBCXX_USE_DUAL_ABI
3246 if (__ss > __expires)
3247 {
3248 // Use updated leap_seconds from tzdb.
3249 size_t __n = std::size(__leaps);
3250
3251 auto __db = get_tzdb_list().begin();
3252 auto __first = __db->leap_seconds.begin() + __n;
3253 auto __last = __db->leap_seconds.end();
3254 auto __pos = std::upper_bound(__first, __last, __ss);
3255 seconds __elapsed(__n);
3256 for (auto __i = __first; __i != __pos; ++__i)
3257 __elapsed += __i->value();
3258
3259 if (__is_utc)
3260 {
3261 // Convert utc_time to sys_time:
3262 __ss -= __elapsed;
3263 // See if that sys_time is before (or during) previous leap sec:
3264 if (__pos != __first && __ss < __pos[-1])
3265 {
3266 if ((__ss + 1s) >= __pos[-1])
3267 return {true, __elapsed};
3268 __elapsed -= __pos[-1].value();
3269 }
3270 }
3271 return {false, __elapsed};
3272 }
3273 else
3274#endif
3275 {
3276 seconds::rep __s = __ss.time_since_epoch().count();
3277 const seconds::rep* __first = std::begin(__leaps);
3278 const seconds::rep* __last = std::end(__leaps);
3279
3280 // Don't bother searching the list if we're after the last one.
3281 if (__s > (__last[-1] + (__last - __first) + 1))
3282 return { false, seconds(__last - __first) };
3283
3284 auto __pos = std::upper_bound(__first, __last, __s);
3285 seconds __elapsed{__pos - __first};
3286 if (__is_utc)
3287 {
3288 // Convert utc_time to sys_time:
3289 __s -= __elapsed.count();
3290 // See if that sys_time is before (or during) previous leap sec:
3291 if (__pos != __first && __s < __pos[-1])
3292 {
3293 if ((__s + 1) >= __pos[-1])
3294 return {true, __elapsed};
3295 --__elapsed;
3296 }
3297 }
3298 return {false, __elapsed};
3299 }
3300 }
3301} // namespace __detail
3302
3303 template<typename _Duration>
3304 [[nodiscard]]
3305 inline leap_second_info
3306 get_leap_second_info(const utc_time<_Duration>& __ut)
3307 {
3308 auto __s = chrono::duration_cast<seconds>(__ut.time_since_epoch());
3309 return __detail::__get_leap_second_info(sys_seconds(__s), true);
3310 }
3311
3312 template<typename _Duration>
3313 [[nodiscard]]
3314 inline utc_time<common_type_t<_Duration, seconds>>
3315 utc_clock::from_sys(const sys_time<_Duration>& __t)
3316 {
3317 using _CDur = common_type_t<_Duration, seconds>;
3318 auto __s = chrono::time_point_cast<seconds>(__t);
3319 const auto __li = __detail::__get_leap_second_info(__s, false);
3320 return utc_time<_CDur>{__t.time_since_epoch()} + __li.elapsed;
3321 }
3322#endif // _GLIBCXX_HOSTED
3323
3324 /// @} group chrono
3325#endif // C++20
3326 } // namespace chrono
3327
3328#if __glibcxx_chrono_cxx20 >= 202306 // C++26
3329 // Hash support [time.hash]
3330
3331 template<typename _Tp>
3332 concept __is_nothrow_copy_hashable = requires(const _Tp& __t) {
3333 { hash<_Tp>{}(_Tp(__t)) } noexcept -> same_as<size_t>;
3334 };
3335
3336 namespace chrono {
3337
3338 template<typename _T1, typename... _Ts>
3339 [[__gnu__::__always_inline__]]
3340 constexpr auto
3341 __pack_ints(_T1 __v1, _Ts... __vs)
3342 {
3343 using _ResT = decltype([] {
3344 constexpr size_t __tsize = (sizeof(_T1) + ... + sizeof(_Ts));
3345 if constexpr (__tsize <= 1)
3346 return static_cast<unsigned char>(0);
3347 else if constexpr (__tsize <= 2)
3348 return static_cast<__UINT16_TYPE__>(0);
3349 else if constexpr (__tsize <= 4)
3350 return static_cast<__UINT32_TYPE__>(0);
3351 else if constexpr (__tsize <= 8)
3352 return static_cast<__UINT64_TYPE__>(0);
3353 else
3354 static_assert(__tsize <= 8);
3355 }());
3356
3357 _ResT __res = __v1;
3358 ((__res = (__res << (sizeof(_Ts) * __CHAR_BIT__) | _ResT(__vs))), ...);
3359 return __res;
3360 }
3361
3362 template<typename _Tp>
3363 [[__gnu__::__always_inline__]]
3364 constexpr auto
3365 __as_int(_Tp __val)
3366 {
3367 if constexpr (is_same_v<_Tp, year>)
3368 return static_cast<unsigned short>(static_cast<int>(__val));
3369 else if constexpr (is_same_v<_Tp, month> || is_same_v<_Tp, day>)
3370 return static_cast<unsigned char>(static_cast<unsigned>(__val));
3371 else if constexpr (is_same_v<_Tp, weekday>)
3372 return static_cast<unsigned char>(__val.c_encoding());
3373 else if constexpr (is_same_v<_Tp, weekday_indexed>)
3374 return __pack_ints(chrono::__as_int(__val.weekday()),
3375 static_cast<unsigned char>(__val.index()));
3376 else if constexpr (is_same_v<_Tp, weekday_last>)
3377 return chrono::__as_int(__val.weekday());
3378 else
3379 static_assert(false);
3380 }
3381
3382 template<typename _Arg, typename... _Args>
3383 size_t
3384 __int_hash(_Arg __arg, _Args... __args)
3385 {
3386 static_assert((is_integral_v<_Arg> && ... && is_integral_v<_Args>));
3387
3388 // TODO consider using a better quality hasher
3389 using _Hasher = _Hash_impl;
3390 size_t __result = _Hasher::hash(__arg);
3391 ((__result = _Hasher::__hash_combine(__args, __result)), ...);
3392 return __result;
3393 }
3394
3395 template<typename... _Tps>
3396 [[__gnu__::__always_inline__]]
3397 inline size_t
3398 __hash(_Tps... __vals)
3399 {
3400 if constexpr (sizeof...(_Tps) == 1)
3401 return chrono::__int_hash(chrono::__as_int(__vals)...);
3402 else
3403 {
3404 auto __res = chrono::__pack_ints(chrono::__as_int(__vals)...);
3405 return chrono::__int_hash(__res);
3406 }
3407 }
3408 } // namespace chrono
3409
3410 // duration
3411 template<typename _Rep, typename _Period>
3412 requires __is_hash_enabled_for<_Rep>
3413 struct hash<chrono::duration<_Rep, _Period>>
3414 {
3415 size_t
3416 operator()(const chrono::duration<_Rep, _Period>& __val) const
3417 noexcept(__is_nothrow_copy_hashable<_Rep>)
3418 {
3419 if constexpr (is_integral_v<_Rep>)
3420 return chrono::__int_hash(__val.count());
3421 else
3422 return hash<_Rep>{}(__val.count());
3423 }
3424 };
3425
3426 template<typename _Rep, typename _Period>
3427 struct __is_fast_hash<hash<chrono::duration<_Rep, _Period>>>
3428 : __is_fast_hash<hash<_Rep>>
3429 {};
3430
3431 // time_point
3432 template<typename _Clock, typename _Dur>
3433 requires __is_hash_enabled_for<_Dur>
3434 struct hash<chrono::time_point<_Clock, _Dur>>
3435 {
3436 size_t
3437 operator()(const chrono::time_point<_Clock, _Dur>& __val) const
3438 noexcept(__is_nothrow_copy_hashable<_Dur>)
3439 { return hash<_Dur>{}(__val.time_since_epoch()); }
3440 };
3441
3442 template<typename _Clock, typename _Dur>
3443 struct __is_fast_hash<hash<chrono::time_point<_Clock, _Dur>>>
3444 : __is_fast_hash<hash<_Dur>>
3445 {};
3446
3447 // day
3448 template<>
3449 struct hash<chrono::day>
3450 {
3451 size_t
3452 operator()(chrono::day __val) const noexcept
3453 { return chrono::__hash(__val); }
3454 };
3455
3456 // month
3457 template<>
3458 struct hash<chrono::month>
3459 {
3460 size_t
3461 operator()(chrono::month __val) const noexcept
3462 { return chrono::__hash(__val); }
3463 };
3464
3465 // year
3466 template<>
3467 struct hash<chrono::year>
3468 {
3469 size_t
3470 operator()(chrono::year __val) const noexcept
3471 { return chrono::__hash(__val); }
3472 };
3473
3474 // weekday
3475 template<>
3476 struct hash<chrono::weekday>
3477 {
3478 size_t
3479 operator()(chrono::weekday __val) const noexcept
3480 { return chrono::__hash(__val); }
3481 };
3482
3483 // weekday_indexed
3484 template<>
3485 struct hash<chrono::weekday_indexed>
3486 {
3487 size_t
3488 operator()(chrono::weekday_indexed __val) const noexcept
3489 { return chrono::__hash(__val); }
3490 };
3491
3492 // weekday_last
3493 template<>
3494 struct hash<chrono::weekday_last>
3495 {
3496 size_t
3497 operator()(chrono::weekday_last __val) const noexcept
3498 { return chrono::__hash(__val); }
3499 };
3500
3501 // month_day
3502 template<>
3503 struct hash<chrono::month_day>
3504 {
3505 size_t
3506 operator()(chrono::month_day __val) const noexcept
3507 { return chrono::__hash(__val.month(), __val.day()); }
3508 };
3509
3510 // month_day_last
3511 template<>
3512 struct hash<chrono::month_day_last>
3513 {
3514 size_t operator()(chrono::month_day_last __val) const noexcept
3515 { return chrono::__hash(__val.month()); }
3516 };
3517
3518 // month_weekday
3519 template<>
3520 struct hash<chrono::month_weekday>
3521 {
3522 size_t
3523 operator()(chrono::month_weekday __val) const noexcept
3524 { return chrono::__hash(__val.month(), __val.weekday_indexed()); }
3525 };
3526
3527 // month_weekday_last
3528 template<>
3529 struct hash<chrono::month_weekday_last>
3530 {
3531 size_t
3532 operator()(chrono::month_weekday_last __val) const noexcept
3533 { return chrono::__hash(__val.month(), __val.weekday_last()); }
3534 };
3535
3536 // year_month
3537 template<>
3538 struct hash<chrono::year_month>
3539 {
3540 size_t
3541 operator()(chrono::year_month __val) const noexcept
3542 { return chrono::__hash(__val.year(), __val.month()); }
3543 };
3544
3545 // year_month_day
3546 template<>
3547 struct hash<chrono::year_month_day>
3548 {
3549 size_t
3550 operator()(chrono::year_month_day __val) const noexcept
3551 { return chrono::__hash(__val.year(), __val.month(), __val.day()); }
3552 };
3553
3554 // year_month_day_last
3555 template<>
3556 struct hash<chrono::year_month_day_last>
3557 {
3558 size_t
3559 operator()(chrono::year_month_day_last __val) const noexcept
3560 { return chrono::__hash(__val.year(), __val.month()); }
3561 };
3562
3563 // year_month_weekday
3564 template<>
3565 struct hash<chrono::year_month_weekday>
3566 {
3567 size_t
3568 operator()(chrono::year_month_weekday __val) const noexcept
3569 {
3570 return chrono::__hash(__val.year(), __val.month(),
3571 __val.weekday_indexed());
3572 }
3573 };
3574
3575 // year_month_weekday_last
3576 template<>
3577 struct hash<chrono::year_month_weekday_last>
3578 {
3579 size_t
3580 operator()(chrono::year_month_weekday_last __val) const noexcept
3581 {
3582 return chrono::__hash(__val.year(), __val.month(),
3583 __val.weekday_last());
3584 }
3585 };
3586
3587#if _GLIBCXX_HOSTED
3588#if _GLIBCXX_USE_CXX11_ABI || ! _GLIBCXX_USE_DUAL_ABI
3589 // zoned_time
3590 template<typename _Duration, typename _TimeZonePtr>
3591 requires __is_hash_enabled_for<
3592 typename chrono::zoned_time<_Duration, _TimeZonePtr>::duration>
3593 && __is_hash_enabled_for<_TimeZonePtr>
3594 struct hash<chrono::zoned_time<_Duration, _TimeZonePtr>>
3595 {
3596 private:
3597 using _ActualDuration =
3598 typename chrono::zoned_time<_Duration, _TimeZonePtr>::duration;
3599
3600 public:
3601 size_t
3602 operator()(const chrono::zoned_time<_Duration, _TimeZonePtr>& __val) const
3603 noexcept(__is_nothrow_copy_hashable<_ActualDuration>
3604 && __is_nothrow_copy_hashable<_TimeZonePtr>)
3605 {
3606 const auto __iduration = [&] {
3607 const _ActualDuration __sd = __val.get_sys_time().time_since_epoch();
3608 if constexpr (is_integral_v<typename _ActualDuration::rep>)
3609 return __sd.count();
3610 else
3611 return hash<_ActualDuration>{}(__sd);
3612 }();
3613
3614 const auto __izone = [&] {
3615 const _TimeZonePtr __tz = __val.get_time_zone();
3616 if constexpr (is_same_v<_TimeZonePtr, const chrono::time_zone*>)
3617 return reinterpret_cast<uintptr_t>(__tz);
3618 else
3619 return hash<_TimeZonePtr>{}(__tz);
3620 }();
3621
3622 return chrono::__int_hash(__iduration, __izone);
3623 }
3624 };
3625
3626 template<typename _Duration, typename _TimeZonePtr>
3627 struct __is_fast_hash<hash<chrono::zoned_time<_Duration, _TimeZonePtr>>>
3628 : __and_<__is_fast_hash<hash<
3629 typename chrono::zoned_time<_Duration, _TimeZonePtr>::duration>>,
3630 __is_fast_hash<hash<_TimeZonePtr>>>
3631 {};
3632
3633 // leap_second
3634 template<>
3635 struct hash<chrono::leap_second>
3636 {
3637 size_t
3638 operator()(chrono::leap_second __val) const noexcept
3639 {
3640 return chrono::__int_hash(
3641 __val.date().time_since_epoch().count(),
3642 __val.value().count());
3643 }
3644 };
3645#endif // _GLIBCXX_USE_CXX11_ABI || ! _GLIBCXX_USE_DUAL_ABI
3646#endif // _GLIBCXX_HOSTED
3647#endif // __glibcxx_chrono_cxx20 >= 202306
3648
3649#ifdef __glibcxx_chrono_cxx20
3650 inline namespace literals
3651 {
3652 inline namespace chrono_literals
3653 {
3654 /// @addtogroup chrono
3655 /// @{
3656#pragma GCC diagnostic push
3657#pragma GCC diagnostic ignored "-Wliteral-suffix"
3658 /// Literal suffix for creating chrono::day objects.
3659 /// @since C++20
3660 constexpr chrono::day
3661 operator""d(unsigned long long __d) noexcept
3662 { return chrono::day{static_cast<unsigned>(__d)}; }
3663
3664 /// Literal suffix for creating chrono::year objects.
3665 /// @since C++20
3666 constexpr chrono::year
3667 operator""y(unsigned long long __y) noexcept
3668 { return chrono::year{static_cast<int>(__y)}; }
3669#pragma GCC diagnostic pop
3670 /// @}
3671 } // inline namespace chrono_literals
3672 } // inline namespace literals
3673#endif // C++20
3674
3675_GLIBCXX_END_NAMESPACE_VERSION
3676} // namespace std
3677
3678#if defined __glibcxx_chrono_cxx20 && _GLIBCXX_HOSTED
3679# include <bits/chrono_io.h>
3680#endif
3681
3682#endif // C++11
3683
3684#endif //_GLIBCXX_CHRONO
constexpr __enable_if_is_duration< _ToDur > floor(const duration< _Rep, _Period > &__d)
Definition chrono.h:392
duration< int64_t, ratio< 2629746 > > months
months
Definition chrono.h:920
duration< int64_t, ratio< 86400 > > days
days
Definition chrono.h:911
duration< int64_t, ratio< 31556952 > > years
years
Definition chrono.h:917
constexpr bool operator<=(const duration< _Rep1, _Period1 > &__lhs, const duration< _Rep2, _Period2 > &__rhs)
Definition chrono.h:859
duration< int64_t, ratio< 3600 > > hours
hours
Definition chrono.h:907
constexpr bool operator>=(const duration< _Rep1, _Period1 > &__lhs, const duration< _Rep2, _Period2 > &__rhs)
Definition chrono.h:873
auto clock_cast(const time_point< _SourceClock, _Duration > &__t)
Convert a time point to a different clock.
Definition chrono:419
constexpr enable_if_t< numeric_limits< _Rep >::is_signed, duration< _Rep, _Period > > abs(duration< _Rep, _Period > __d)
Definition chrono.h:463
duration< int64_t, ratio< 60 > > minutes
minutes
Definition chrono.h:904
constexpr time_point< _Clock, typename common_type< duration< _Rep1, _Period1 >, _Dur2 >::type > operator+(const duration< _Rep1, _Period1 > &__lhs, const time_point< _Clock, _Dur2 > &__rhs)
Adjust a time point forwards by the given duration.
Definition chrono.h:1119
constexpr bool operator<(const duration< _Rep1, _Period1 > &__lhs, const duration< _Rep2, _Period2 > &__rhs)
Definition chrono.h:826
duration< int64_t > seconds
seconds
Definition chrono.h:901
constexpr bool operator>(const duration< _Rep1, _Period1 > &__lhs, const duration< _Rep2, _Period2 > &__rhs)
Definition chrono.h:866
constexpr common_type< duration< _Rep1, _Period1 >, duration< _Rep2, _Period2 > >::type operator-(const duration< _Rep1, _Period1 > &__lhs, const duration< _Rep2, _Period2 > &__rhs)
The difference between two durations.
Definition chrono.h:714
constexpr duration< __common_rep_t< _Rep1, __disable_if_is_duration< _Rep2 > >, _Period > operator/(const duration< _Rep1, _Period > &__d, const _Rep2 &__s)
Definition chrono.h:760
constexpr __enable_if_is_duration< _ToDur > duration_cast(const duration< _Rep, _Period > &__d)
Definition chrono.h:279
constexpr complex< _Tp > operator-(const complex< _Tp > &__x, const complex< _Tp > &__y)
Return new complex value x minus y.
Definition complex:404
constexpr complex< _Tp > operator+(const complex< _Tp > &__x, const complex< _Tp > &__y)
Return new complex value x plus y.
Definition complex:374
constexpr complex< _Tp > operator/(const complex< _Tp > &__x, const complex< _Tp > &__y)
Return new complex value x divided by y.
Definition complex:464
basic_ostringstream< char > ostringstream
Class for char output memory streams.
Definition iosfwd:157
__bool_constant< true > true_type
The type used as a compile-time boolean with true value.
Definition type_traits:118
typename common_type< _Tp... >::type common_type_t
Alias template for common_type.
Definition type_traits:2906
typename make_unsigned< _Tp >::type make_unsigned_t
Alias template for make_unsigned.
Definition type_traits:2204
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
Definition move.h:138
_Tp * end(valarray< _Tp > &__va) noexcept
Return an iterator pointing to one past the last element of the valarray.
Definition valarray:1251
_Tp * begin(valarray< _Tp > &__va) noexcept
Return an iterator pointing to the first element of the valarray.
Definition valarray:1229
ISO C++ entities toplevel namespace is std.
constexpr auto cend(const _Container &__cont) noexcept(noexcept(std::end(__cont))) -> decltype(std::end(__cont))
Return an iterator pointing to one past the last element of the const container.
constexpr auto size(const _Container &__cont) noexcept(noexcept(__cont.size())) -> decltype(__cont.size())
Return the size of a container.
constexpr auto cbegin(const _Container &__cont) noexcept(noexcept(std::begin(__cont))) -> decltype(std::begin(__cont))
Return an iterator pointing to the first element of the const container.
ISO C++ inline namespace for literal suffixes.
ISO C++ 2011 namespace for date and time utilities.
const tzdb & front() const noexcept
const_iterator erase_after(const_iterator __p)
static constexpr bool is_signed
Definition limits:230
Provides compile-time rational arithmetic.
Definition ratio:272
Primary class template hash.
is_unsigned
Definition type_traits:1020
chrono::duration represents a distance between two points in time
Definition chrono.h:516
chrono::time_point represents a point in time as measured by a clock
Definition chrono.h:927
A smart pointer with reference-counted copy semantics.
Forward iterators support a superset of input iterator operations.
constexpr iterator begin() noexcept
Definition stl_vector.h:988