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