3// Copyright (C) 2008-2023 Free Software Foundation, Inc.
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)
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.
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.
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/>.
25/** @file include/chrono
26 * This is a Standard C++ Library header.
30#ifndef _GLIBCXX_CHRONO
31#define _GLIBCXX_CHRONO 1
33#pragma GCC system_header
35#include <bits/requires_hosted.h> // for <ctime> and clocks
37#if __cplusplus < 201103L
38# include <bits/c++0x_warning.h>
41#include <bits/chrono.h>
43#if __cplusplus >= 202002L
48# include <bits/stl_algo.h> // upper_bound
49# include <bits/shared_ptr.h>
50# include <bits/unique_ptr.h>
53#if __cplusplus >= 202002L
54// TODO formatting and parsing
55// # undef __cpp_lib_chrono
56// # define __cpp_lib_chrono 201907L
59namespace std _GLIBCXX_VISIBILITY(default)
61_GLIBCXX_BEGIN_NAMESPACE_VERSION
64 * @defgroup chrono Time
67 * Classes and functions for time.
72 /** @namespace std::chrono
73 * @brief ISO C++ 2011 namespace for date and time utilities
78#if __cplusplus >= 202002L
79 /// @addtogroup chrono
82 template<typename _Duration>
83 using local_time = time_point<local_t, _Duration>;
84 using local_seconds = local_time<seconds>;
85 using local_days = local_time<days>;
91 template<typename _Duration>
92 using utc_time = time_point<utc_clock, _Duration>;
93 using utc_seconds = utc_time<seconds>;
95 template<typename _Duration>
96 using tai_time = time_point<tai_clock, _Duration>;
97 using tai_seconds = tai_time<seconds>;
99 template<typename _Duration>
100 using gps_time = time_point<gps_clock, _Duration>;
101 using gps_seconds = gps_time<seconds>;
103 template<> struct is_clock<utc_clock> : true_type { };
104 template<> struct is_clock<tai_clock> : true_type { };
105 template<> struct is_clock<gps_clock> : true_type { };
107 template<> inline constexpr bool is_clock_v<utc_clock> = true;
108 template<> inline constexpr bool is_clock_v<tai_clock> = true;
109 template<> inline constexpr bool is_clock_v<gps_clock> = true;
111 struct leap_second_info
117 template<typename _Duration>
119 get_leap_second_info(const utc_time<_Duration>& __ut);
121 /** A clock that measures Universal Coordinated Time (UTC).
123 * The epoch is 1970-01-01 00:00:00.
130 using rep = system_clock::rep;
131 using period = system_clock::period;
132 using duration = chrono::duration<rep, period>;
133 using time_point = chrono::time_point<utc_clock>;
134 static constexpr bool is_steady = false;
139 { return from_sys(system_clock::now()); }
141 template<typename _Duration>
143 static sys_time<common_type_t<_Duration, seconds>>
144 to_sys(const utc_time<_Duration>& __t)
146 using _CDur = common_type_t<_Duration, seconds>;
147 const auto __li = chrono::get_leap_second_info(__t);
148 sys_time<_CDur> __s{__t.time_since_epoch() - __li.elapsed};
149 if (__li.is_leap_second)
150 __s = chrono::floor<seconds>(__s) + seconds{1} - _CDur{1};
154 template<typename _Duration>
156 static utc_time<common_type_t<_Duration, seconds>>
157 from_sys(const sys_time<_Duration>& __t);
160 /** A clock that measures International Atomic Time.
162 * The epoch is 1958-01-01 00:00:00.
169 using rep = system_clock::rep;
170 using period = system_clock::period;
171 using duration = chrono::duration<rep, period>;
172 using time_point = chrono::time_point<tai_clock>;
173 static constexpr bool is_steady = false; // XXX true for CLOCK_TAI?
175 // TODO move into lib, use CLOCK_TAI on linux, add extension point.
179 { return from_utc(utc_clock::now()); }
181 template<typename _Duration>
183 static utc_time<common_type_t<_Duration, seconds>>
184 to_utc(const tai_time<_Duration>& __t)
186 using _CDur = common_type_t<_Duration, seconds>;
187 return utc_time<_CDur>{__t.time_since_epoch()} - 378691210s;
190 template<typename _Duration>
192 static tai_time<common_type_t<_Duration, seconds>>
193 from_utc(const utc_time<_Duration>& __t)
195 using _CDur = common_type_t<_Duration, seconds>;
196 return tai_time<_CDur>{__t.time_since_epoch()} + 378691210s;
200 /** A clock that measures GPS time.
202 * The epoch is 1980-01-06 00:00:00.
209 using rep = system_clock::rep;
210 using period = system_clock::period;
211 using duration = chrono::duration<rep, period>;
212 using time_point = chrono::time_point<gps_clock>;
213 static constexpr bool is_steady = false; // XXX
215 // TODO move into lib, add extension point.
219 { return from_utc(utc_clock::now()); }
221 template<typename _Duration>
223 static utc_time<common_type_t<_Duration, seconds>>
224 to_utc(const gps_time<_Duration>& __t)
226 using _CDur = common_type_t<_Duration, seconds>;
227 return utc_time<_CDur>{__t.time_since_epoch()} + 315964809s;
230 template<typename _Duration>
232 static gps_time<common_type_t<_Duration, seconds>>
233 from_utc(const utc_time<_Duration>& __t)
235 using _CDur = common_type_t<_Duration, seconds>;
236 return gps_time<_CDur>{__t.time_since_epoch()} - 315964809s;
241 template<typename _DestClock, typename _SourceClock>
242 struct clock_time_conversion
245 // Identity conversions
247 template<typename _Clock>
248 struct clock_time_conversion<_Clock, _Clock>
250 template<typename _Duration>
251 time_point<_Clock, _Duration>
252 operator()(const time_point<_Clock, _Duration>& __t) const
257 struct clock_time_conversion<system_clock, system_clock>
259 template<typename _Duration>
261 operator()(const sys_time<_Duration>& __t) const
266 struct clock_time_conversion<utc_clock, utc_clock>
268 template<typename _Duration>
270 operator()(const utc_time<_Duration>& __t) const
274 // Conversions between system_clock and utc_clock
277 struct clock_time_conversion<utc_clock, system_clock>
279 template<typename _Duration>
280 utc_time<common_type_t<_Duration, seconds>>
281 operator()(const sys_time<_Duration>& __t) const
282 { return utc_clock::from_sys(__t); }
286 struct clock_time_conversion<system_clock, utc_clock>
288 template<typename _Duration>
289 sys_time<common_type_t<_Duration, seconds>>
290 operator()(const utc_time<_Duration>& __t) const
291 { return utc_clock::to_sys(__t); }
294 template<typename _Tp, typename _Clock>
295 inline constexpr bool __is_time_point_for_v = false;
297 template<typename _Clock, typename _Duration>
298 inline constexpr bool
299 __is_time_point_for_v<time_point<_Clock, _Duration>, _Clock> = true;
301 // Conversions between system_clock and other clocks
303 template<typename _SourceClock>
304 struct clock_time_conversion<system_clock, _SourceClock>
306 template<typename _Duration, typename _Src = _SourceClock>
308 operator()(const time_point<_SourceClock, _Duration>& __t) const
309 -> decltype(_Src::to_sys(__t))
311 using _Ret = decltype(_SourceClock::to_sys(__t));
312 static_assert(__is_time_point_for_v<_Ret, system_clock>);
313 return _SourceClock::to_sys(__t);
317 template<typename _DestClock>
318 struct clock_time_conversion<_DestClock, system_clock>
320 template<typename _Duration, typename _Dest = _DestClock>
322 operator()(const sys_time<_Duration>& __t) const
323 -> decltype(_Dest::from_sys(__t))
325 using _Ret = decltype(_DestClock::from_sys(__t));
326 static_assert(__is_time_point_for_v<_Ret, _DestClock>);
327 return _DestClock::from_sys(__t);
331 // Conversions between utc_clock and other clocks
333 template<typename _SourceClock>
334 struct clock_time_conversion<utc_clock, _SourceClock>
336 template<typename _Duration, typename _Src = _SourceClock>
338 operator()(const time_point<_SourceClock, _Duration>& __t) const
339 -> decltype(_Src::to_utc(__t))
341 using _Ret = decltype(_SourceClock::to_utc(__t));
342 static_assert(__is_time_point_for_v<_Ret, utc_clock>);
343 return _SourceClock::to_utc(__t);
347 template<typename _DestClock>
348 struct clock_time_conversion<_DestClock, utc_clock>
350 template<typename _Duration, typename _Dest = _DestClock>
352 operator()(const utc_time<_Duration>& __t) const
353 -> decltype(_Dest::from_utc(__t))
355 using _Ret = decltype(_DestClock::from_utc(__t));
356 static_assert(__is_time_point_for_v<_Ret, _DestClock>);
357 return _DestClock::from_utc(__t);
361 /// @cond undocumented
364 template<typename _DestClock, typename _SourceClock, typename _Duration>
365 concept __clock_convs
366 = requires (const time_point<_SourceClock, _Duration>& __t) {
367 clock_time_conversion<_DestClock, _SourceClock>{}(__t);
370 template<typename _DestClock, typename _SourceClock, typename _Duration>
371 concept __clock_convs_sys
372 = requires (const time_point<_SourceClock, _Duration>& __t) {
373 clock_time_conversion<_DestClock, system_clock>{}(
374 clock_time_conversion<system_clock, _SourceClock>{}(__t));
377 template<typename _DestClock, typename _SourceClock, typename _Duration>
378 concept __clock_convs_utc
379 = requires (const time_point<_SourceClock, _Duration>& __t) {
380 clock_time_conversion<_DestClock, utc_clock>{}(
381 clock_time_conversion<utc_clock, _SourceClock>{}(__t));
384 template<typename _DestClock, typename _SourceClock, typename _Duration>
385 concept __clock_convs_sys_utc
386 = requires (const time_point<_SourceClock, _Duration>& __t) {
387 clock_time_conversion<_DestClock, utc_clock>{}(
388 clock_time_conversion<utc_clock, system_clock>{}(
389 clock_time_conversion<system_clock, _SourceClock>{}(__t)));
392 template<typename _DestClock, typename _SourceClock, typename _Duration>
393 concept __clock_convs_utc_sys
394 = requires (const time_point<_SourceClock, _Duration>& __t) {
395 clock_time_conversion<_DestClock, system_clock>{}(
396 clock_time_conversion<system_clock, utc_clock>{}(
397 clock_time_conversion<utc_clock, _SourceClock>{}(__t)));
400 } // namespace __detail
403 /// Convert a time point to a different clock.
404 template<typename _DestClock, typename _SourceClock, typename _Duration>
407 clock_cast(const time_point<_SourceClock, _Duration>& __t)
408 requires __detail::__clock_convs<_DestClock, _SourceClock, _Duration>
409 || __detail::__clock_convs_sys<_DestClock, _SourceClock, _Duration>
410 || __detail::__clock_convs_utc<_DestClock, _SourceClock, _Duration>
411 || __detail::__clock_convs_sys_utc<_DestClock, _SourceClock, _Duration>
412 || __detail::__clock_convs_utc_sys<_DestClock, _SourceClock, _Duration>
414 constexpr bool __direct
415 = __detail::__clock_convs<_DestClock, _SourceClock, _Duration>;
416 if constexpr (__direct)
418 return clock_time_conversion<_DestClock, _SourceClock>{}(__t);
422 constexpr bool __convert_via_sys_clock
423 = __detail::__clock_convs_sys<_DestClock, _SourceClock, _Duration>;
424 constexpr bool __convert_via_utc_clock
425 = __detail::__clock_convs_utc<_DestClock, _SourceClock, _Duration>;
426 if constexpr (__convert_via_sys_clock)
428 static_assert(!__convert_via_utc_clock,
429 "clock_cast requires a unique best conversion, but "
430 "conversion is possible via system_clock and also via"
432 return clock_time_conversion<_DestClock, system_clock>{}(
433 clock_time_conversion<system_clock, _SourceClock>{}(__t));
435 else if constexpr (__convert_via_utc_clock)
437 return clock_time_conversion<_DestClock, utc_clock>{}(
438 clock_time_conversion<utc_clock, _SourceClock>{}(__t));
442 constexpr bool __convert_via_sys_and_utc_clocks
443 = __detail::__clock_convs_sys_utc<_DestClock,
447 if constexpr (__convert_via_sys_and_utc_clocks)
449 constexpr bool __convert_via_utc_and_sys_clocks
450 = __detail::__clock_convs_utc_sys<_DestClock,
453 static_assert(!__convert_via_utc_and_sys_clocks,
454 "clock_cast requires a unique best conversion, but "
455 "conversion is possible via system_clock followed by "
456 "utc_clock, and also via utc_clock followed by "
458 return clock_time_conversion<_DestClock, utc_clock>{}(
459 clock_time_conversion<utc_clock, system_clock>{}(
460 clock_time_conversion<system_clock, _SourceClock>{}(__t)));
464 return clock_time_conversion<_DestClock, system_clock>{}(
465 clock_time_conversion<system_clock, utc_clock>{}(
466 clock_time_conversion<utc_clock, _SourceClock>{}(__t)));
474 // CLASS DECLARATIONS
479 class weekday_indexed;
482 class month_day_last;
484 class month_weekday_last;
486 class year_month_day;
487 class year_month_day_last;
488 class year_month_weekday;
489 class year_month_weekday_last;
493 explicit last_spec() = default;
495 friend constexpr month_day_last
496 operator/(int __m, last_spec) noexcept;
498 friend constexpr month_day_last
499 operator/(last_spec, int __m) noexcept;
502 inline constexpr last_spec last{};
506 // Helper to __add_modulo and __sub_modulo.
507 template <unsigned __d, typename _Tp>
511 using _Up = make_unsigned_t<_Tp>;
512 auto constexpr __a = _Up(-1) - _Up(255 + __d - 2);
513 auto constexpr __b = _Up(__d * (__a / __d) - 1);
514 // Notice: b <= a - 1 <= _Up(-1) - (255 + d - 1) and b % d = d - 1.
515 return _Up(-1) - __b; // >= 255 + d - 1
518 // Compute the remainder of the Euclidean division of __x + __y divided by
519 // __d without overflowing. Typically, __x <= 255 + d - 1 is sum of
520 // weekday/month with a shift in [0, d - 1] and __y is a duration count.
521 template <unsigned __d, typename _Tp>
523 __add_modulo(unsigned __x, _Tp __y)
525 using _Up = make_unsigned_t<_Tp>;
526 // For __y >= 0, _Up(__y) has the same mathematical value as __y and
527 // this function simply returns (__x + _Up(__y)) % d. Typically, this
528 // doesn't overflow since the range of _Up contains many more positive
529 // values than _Tp's. For __y < 0, _Up(__y) has a mathematical value in
530 // the upper-half range of _Up so that adding a positive value to it
531 // might overflow. Moreover, most likely, _Up(__y) != __y mod d. To
532 // fix both issues we subtract from _Up(__y) an __offset >=
533 // 255 + d - 1 to make room for the addition to __x and shift the modulo
534 // to the correct value.
535 auto const __offset = __y >= 0 ? _Up(0) : __modulo_offset<__d, _Tp>();
536 return (__x + _Up(__y) - __offset) % __d;
539 // Similar to __add_modulo but for __x - __y.
540 template <unsigned __d, typename _Tp>
542 __sub_modulo(unsigned __x, _Tp __y)
544 using _Up = make_unsigned_t<_Tp>;
545 auto const __offset = __y <= 0 ? _Up(0) : __modulo_offset<__d, _Tp>();
546 return (__x - _Up(__y) - __offset) % __d;
549 inline constexpr unsigned __days_per_month[12]
550 = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
564 day(unsigned __d) noexcept
569 operator++() noexcept
576 operator++(int) noexcept
584 operator--() noexcept
591 operator--(int) noexcept
599 operator+=(const days& __d) noexcept
606 operator-=(const days& __d) noexcept
613 operator unsigned() const noexcept
618 { return 1 <= _M_d && _M_d <= 31; }
620 friend constexpr bool
621 operator==(const day& __x, const day& __y) noexcept
622 { return unsigned{__x} == unsigned{__y}; }
624 friend constexpr strong_ordering
625 operator<=>(const day& __x, const day& __y) noexcept
626 { return unsigned{__x} <=> unsigned{__y}; }
629 operator+(const day& __x, const days& __y) noexcept
630 { return day(unsigned{__x} + __y.count()); }
633 operator+(const days& __x, const day& __y) noexcept
634 { return __y + __x; }
637 operator-(const day& __x, const days& __y) noexcept
638 { return __x + -__y; }
640 friend constexpr days
641 operator-(const day& __x, const day& __y) noexcept
642 { return days{int(unsigned{__x}) - int(unsigned{__y})}; }
644 friend constexpr month_day
645 operator/(const month& __m, const day& __d) noexcept;
647 friend constexpr month_day
648 operator/(int __m, const day& __d) noexcept;
650 friend constexpr month_day
651 operator/(const day& __d, const month& __m) noexcept;
653 friend constexpr month_day
654 operator/(const day& __d, int __m) noexcept;
656 friend constexpr year_month_day
657 operator/(const year_month& __ym, const day& __d) noexcept;
671 month(unsigned __m) noexcept
676 operator++() noexcept
683 operator++(int) noexcept
691 operator--() noexcept
698 operator--(int) noexcept
706 operator+=(const months& __m) noexcept
713 operator-=(const months& __m) noexcept
720 operator unsigned() const noexcept
725 { return 1 <= _M_m && _M_m <= 12; }
727 friend constexpr bool
728 operator==(const month& __x, const month& __y) noexcept
729 { return unsigned{__x} == unsigned{__y}; }
731 friend constexpr strong_ordering
732 operator<=>(const month& __x, const month& __y) noexcept
733 { return unsigned{__x} <=> unsigned{__y}; }
735 friend constexpr month
736 operator+(const month& __x, const months& __y) noexcept
738 // modulo(x + (y - 1), 12) = modulo(x + (y - 1) + 12, 12)
739 // = modulo((x + 11) + y , 12)
740 return month{1 + __detail::__add_modulo<12>(
741 unsigned{__x} + 11, __y.count())};
744 friend constexpr month
745 operator+(const months& __x, const month& __y) noexcept
746 { return __y + __x; }
748 friend constexpr month
749 operator-(const month& __x, const months& __y) noexcept
751 // modulo(x + (-y - 1), 12) = modulo(x + (-y - 1) + 12, 12)
752 // = modulo((x + 11) - y , 12)
753 return month{1 + __detail::__sub_modulo<12>(
754 unsigned{__x} + 11, __y.count())};
757 friend constexpr months
758 operator-(const month& __x, const month& __y) noexcept
760 const auto __dm = int(unsigned(__x)) - int(unsigned(__y));
761 return months{__dm < 0 ? 12 + __dm : __dm};
764 friend constexpr year_month
765 operator/(const year& __y, const month& __m) noexcept;
767 friend constexpr month_day
768 operator/(const month& __m, int __d) noexcept;
770 friend constexpr month_day_last
771 operator/(const month& __m, last_spec) noexcept;
773 friend constexpr month_day_last
774 operator/(last_spec, const month& __m) noexcept;
776 friend constexpr month_weekday
777 operator/(const month& __m, const weekday_indexed& __wdi) noexcept;
779 friend constexpr month_weekday
780 operator/(const weekday_indexed& __wdi, const month& __m) noexcept;
782 friend constexpr month_weekday_last
783 operator/(const month& __m, const weekday_last& __wdl) noexcept;
785 friend constexpr month_weekday_last
786 operator/(const weekday_last& __wdl, const month& __m) noexcept;
789 inline constexpr month January{1};
790 inline constexpr month February{2};
791 inline constexpr month March{3};
792 inline constexpr month April{4};
793 inline constexpr month May{5};
794 inline constexpr month June{6};
795 inline constexpr month July{7};
796 inline constexpr month August{8};
797 inline constexpr month September{9};
798 inline constexpr month October{10};
799 inline constexpr month November{11};
800 inline constexpr month December{12};
813 year(int __y) noexcept
814 : _M_y{static_cast<short>(__y)}
817 static constexpr year
819 { return year{-32767}; }
821 static constexpr year
823 { return year{32767}; }
826 operator++() noexcept
833 operator++(int) noexcept
841 operator--() noexcept
848 operator--(int) noexcept
856 operator+=(const years& __y) noexcept
863 operator-=(const years& __y) noexcept
870 operator+() const noexcept
874 operator-() const noexcept
875 { return year{-_M_y}; }
878 is_leap() const noexcept
880 // Testing divisibility by 100 first gives better performance [1], i.e.,
881 // return _M_y % 100 == 0 ? _M_y % 400 == 0 : _M_y % 16 == 0;
882 // Furthermore, if _M_y % 100 == 0, then _M_y % 400 == 0 is equivalent
883 // to _M_y % 16 == 0, so we can simplify it to
884 // return _M_y % 100 == 0 ? _M_y % 16 == 0 : _M_y % 4 == 0. // #1
885 // Similarly, we can replace 100 with 25 (which is good since
886 // _M_y % 25 == 0 requires one fewer instruction than _M_y % 100 == 0
888 // return _M_y % 25 == 0 ? _M_y % 16 == 0 : _M_y % 4 == 0. // #2
889 // Indeed, first assume _M_y % 4 != 0. Then _M_y % 16 != 0 and hence,
890 // _M_y % 4 == 0 and _M_y % 16 == 0 are both false. Therefore, #2
891 // returns false as it should (regardless of _M_y % 25.) Now assume
892 // _M_y % 4 == 0. In this case, _M_y % 25 == 0 if, and only if,
893 // _M_y % 100 == 0, that is, #1 and #2 are equivalent. Finally, #2 is
895 // return (_M_y & (_M_y % 25 == 0 ? 15 : 3)) == 0.
898 // [1] https://github.com/cassioneri/calendar
899 // [2] https://godbolt.org/z/55G8rn77e
900 // [3] https://gcc.gnu.org/pipermail/libstdc++/2021-June/052815.html
902 return (_M_y & (_M_y % 25 == 0 ? 15 : 3)) == 0;
906 operator int() const noexcept
911 { return min()._M_y <= _M_y && _M_y <= max()._M_y; }
913 friend constexpr bool
914 operator==(const year& __x, const year& __y) noexcept
915 { return int{__x} == int{__y}; }
917 friend constexpr strong_ordering
918 operator<=>(const year& __x, const year& __y) noexcept
919 { return int{__x} <=> int{__y}; }
921 friend constexpr year
922 operator+(const year& __x, const years& __y) noexcept
923 { return year{int{__x} + static_cast<int>(__y.count())}; }
925 friend constexpr year
926 operator+(const years& __x, const year& __y) noexcept
927 { return __y + __x; }
929 friend constexpr year
930 operator-(const year& __x, const years& __y) noexcept
931 { return __x + -__y; }
933 friend constexpr years
934 operator-(const year& __x, const year& __y) noexcept
935 { return years{int{__x} - int{__y}}; }
937 friend constexpr year_month
938 operator/(const year& __y, int __m) noexcept;
940 friend constexpr year_month_day
941 operator/(const year& __y, const month_day& __md) noexcept;
943 friend constexpr year_month_day
944 operator/(const month_day& __md, const year& __y) noexcept;
946 friend constexpr year_month_day_last
947 operator/(const year& __y, const month_day_last& __mdl) noexcept;
949 friend constexpr year_month_day_last
950 operator/(const month_day_last& __mdl, const year& __y) noexcept;
952 friend constexpr year_month_weekday
953 operator/(const year& __y, const month_weekday& __mwd) noexcept;
955 friend constexpr year_month_weekday
956 operator/(const month_weekday& __mwd, const year& __y) noexcept;
958 friend constexpr year_month_weekday_last
959 operator/(const year& __y, const month_weekday_last& __mwdl) noexcept;
961 friend constexpr year_month_weekday_last
962 operator/(const month_weekday_last& __mwdl, const year& __y) noexcept;
972 static constexpr weekday
973 _S_from_days(const days& __d)
975 return weekday{__detail::__add_modulo<7>(4, __d.count())};
982 weekday(unsigned __wd) noexcept
983 : _M_wd(__wd == 7 ? 0 : __wd) // __wd % 7 ?
987 weekday(const sys_days& __dp) noexcept
988 : weekday{_S_from_days(__dp.time_since_epoch())}
992 weekday(const local_days& __dp) noexcept
993 : weekday{sys_days{__dp.time_since_epoch()}}
997 operator++() noexcept
1004 operator++(int) noexcept
1012 operator--() noexcept
1019 operator--(int) noexcept
1027 operator+=(const days& __d) noexcept
1029 *this = *this + __d;
1034 operator-=(const days& __d) noexcept
1036 *this = *this - __d;
1041 c_encoding() const noexcept
1045 iso_encoding() const noexcept
1046 { return _M_wd == 0u ? 7u : _M_wd; }
1050 { return _M_wd <= 6; }
1052 constexpr weekday_indexed
1053 operator[](unsigned __index) const noexcept;
1055 constexpr weekday_last
1056 operator[](last_spec) const noexcept;
1058 friend constexpr bool
1059 operator==(const weekday& __x, const weekday& __y) noexcept
1060 { return __x._M_wd == __y._M_wd; }
1062 friend constexpr weekday
1063 operator+(const weekday& __x, const days& __y) noexcept
1065 return weekday{__detail::__add_modulo<7>(__x._M_wd, __y.count())};
1068 friend constexpr weekday
1069 operator+(const days& __x, const weekday& __y) noexcept
1070 { return __y + __x; }
1072 friend constexpr weekday
1073 operator-(const weekday& __x, const days& __y) noexcept
1075 return weekday{__detail::__sub_modulo<7>(__x._M_wd, __y.count())};
1078 friend constexpr days
1079 operator-(const weekday& __x, const weekday& __y) noexcept
1081 const auto __n = __x.c_encoding() - __y.c_encoding();
1082 return static_cast<int>(__n) >= 0 ? days{__n} : days{__n + 7};
1086 inline constexpr weekday Sunday{0};
1087 inline constexpr weekday Monday{1};
1088 inline constexpr weekday Tuesday{2};
1089 inline constexpr weekday Wednesday{3};
1090 inline constexpr weekday Thursday{4};
1091 inline constexpr weekday Friday{5};
1092 inline constexpr weekday Saturday{6};
1096 class weekday_indexed
1099 chrono::weekday _M_wd;
1100 unsigned char _M_index;
1103 weekday_indexed() = default;
1106 weekday_indexed(const chrono::weekday& __wd, unsigned __index) noexcept
1107 : _M_wd(__wd), _M_index(__index)
1110 constexpr chrono::weekday
1111 weekday() const noexcept
1115 index() const noexcept
1116 { return _M_index; };
1120 { return _M_wd.ok() && 1 <= _M_index && _M_index <= 5; }
1122 friend constexpr bool
1123 operator==(const weekday_indexed& __x, const weekday_indexed& __y) noexcept
1124 { return __x.weekday() == __y.weekday() && __x.index() == __y.index(); }
1126 friend constexpr month_weekday
1127 operator/(const month& __m, const weekday_indexed& __wdi) noexcept;
1129 friend constexpr month_weekday
1130 operator/(int __m, const weekday_indexed& __wdi) noexcept;
1132 friend constexpr month_weekday
1133 operator/(const weekday_indexed& __wdi, const month& __m) noexcept;
1135 friend constexpr month_weekday
1136 operator/(const weekday_indexed& __wdi, int __m) noexcept;
1138 friend constexpr year_month_weekday
1139 operator/(const year_month& __ym, const weekday_indexed& __wdi) noexcept;
1142 constexpr weekday_indexed
1143 weekday::operator[](unsigned __index) const noexcept
1144 { return {*this, __index}; }
1151 chrono::weekday _M_wd;
1155 weekday_last(const chrono::weekday& __wd) noexcept
1159 constexpr chrono::weekday
1160 weekday() const noexcept
1165 { return _M_wd.ok(); }
1167 friend constexpr bool
1168 operator==(const weekday_last& __x, const weekday_last& __y) noexcept
1169 { return __x.weekday() == __y.weekday(); }
1171 friend constexpr month_weekday_last
1172 operator/(int __m, const weekday_last& __wdl) noexcept;
1174 friend constexpr month_weekday_last
1175 operator/(const weekday_last& __wdl, int __m) noexcept;
1177 friend constexpr year_month_weekday_last
1178 operator/(const year_month& __ym, const weekday_last& __wdl) noexcept;
1181 constexpr weekday_last
1182 weekday::operator[](last_spec) const noexcept
1183 { return weekday_last{*this}; }
1194 month_day() = default;
1197 month_day(const chrono::month& __m, const chrono::day& __d) noexcept
1198 : _M_m{__m}, _M_d{__d}
1201 constexpr chrono::month
1202 month() const noexcept
1205 constexpr chrono::day
1206 day() const noexcept
1213 && 1u <= unsigned(_M_d)
1214 && unsigned(_M_d) <= __detail::__days_per_month[unsigned(_M_m) - 1];
1217 friend constexpr bool
1218 operator==(const month_day& __x, const month_day& __y) noexcept
1219 { return __x.month() == __y.month() && __x.day() == __y.day(); }
1221 friend constexpr strong_ordering
1222 operator<=>(const month_day& __x, const month_day& __y) noexcept
1225 friend constexpr month_day
1226 operator/(const chrono::month& __m, const chrono::day& __d) noexcept
1227 { return {__m, __d}; }
1229 friend constexpr month_day
1230 operator/(const chrono::month& __m, int __d) noexcept
1231 { return {__m, chrono::day(unsigned(__d))}; }
1233 friend constexpr month_day
1234 operator/(int __m, const chrono::day& __d) noexcept
1235 { return {chrono::month(unsigned(__m)), __d}; }
1237 friend constexpr month_day
1238 operator/(const chrono::day& __d, const chrono::month& __m) noexcept
1239 { return {__m, __d}; }
1241 friend constexpr month_day
1242 operator/(const chrono::day& __d, int __m) noexcept
1243 { return {chrono::month(unsigned(__m)), __d}; }
1245 friend constexpr year_month_day
1246 operator/(int __y, const month_day& __md) noexcept;
1248 friend constexpr year_month_day
1249 operator/(const month_day& __md, int __y) noexcept;
1254 class month_day_last
1261 month_day_last(const chrono::month& __m) noexcept
1265 constexpr chrono::month
1266 month() const noexcept
1271 { return _M_m.ok(); }
1273 friend constexpr bool
1274 operator==(const month_day_last& __x, const month_day_last& __y) noexcept
1275 { return __x.month() == __y.month(); }
1277 friend constexpr strong_ordering
1278 operator<=>(const month_day_last& __x, const month_day_last& __y) noexcept
1281 friend constexpr month_day_last
1282 operator/(const chrono::month& __m, last_spec) noexcept
1283 { return month_day_last{__m}; }
1285 friend constexpr month_day_last
1286 operator/(int __m, last_spec) noexcept
1287 { return chrono::month(unsigned(__m)) / last; }
1289 friend constexpr month_day_last
1290 operator/(last_spec, const chrono::month& __m) noexcept
1291 { return __m / last; }
1293 friend constexpr month_day_last
1294 operator/(last_spec, int __m) noexcept
1295 { return __m / last; }
1297 friend constexpr year_month_day_last
1298 operator/(int __y, const month_day_last& __mdl) noexcept;
1300 friend constexpr year_month_day_last
1301 operator/(const month_day_last& __mdl, int __y) noexcept;
1310 chrono::weekday_indexed _M_wdi;
1314 month_weekday(const chrono::month& __m,
1315 const chrono::weekday_indexed& __wdi) noexcept
1316 : _M_m{__m}, _M_wdi{__wdi}
1319 constexpr chrono::month
1320 month() const noexcept
1323 constexpr chrono::weekday_indexed
1324 weekday_indexed() const noexcept
1329 { return _M_m.ok() && _M_wdi.ok(); }
1331 friend constexpr bool
1332 operator==(const month_weekday& __x, const month_weekday& __y) noexcept
1334 return __x.month() == __y.month()
1335 && __x.weekday_indexed() == __y.weekday_indexed();
1338 friend constexpr month_weekday
1339 operator/(const chrono::month& __m,
1340 const chrono::weekday_indexed& __wdi) noexcept
1341 { return {__m, __wdi}; }
1343 friend constexpr month_weekday
1344 operator/(int __m, const chrono::weekday_indexed& __wdi) noexcept
1345 { return chrono::month(unsigned(__m)) / __wdi; }
1347 friend constexpr month_weekday
1348 operator/(const chrono::weekday_indexed& __wdi,
1349 const chrono::month& __m) noexcept
1350 { return __m / __wdi; }
1352 friend constexpr month_weekday
1353 operator/(const chrono::weekday_indexed& __wdi, int __m) noexcept
1354 { return __m / __wdi; }
1356 friend constexpr year_month_weekday
1357 operator/(int __y, const month_weekday& __mwd) noexcept;
1359 friend constexpr year_month_weekday
1360 operator/(const month_weekday& __mwd, int __y) noexcept;
1363 // MONTH_WEEKDAY_LAST
1365 class month_weekday_last
1369 chrono::weekday_last _M_wdl;
1373 month_weekday_last(const chrono::month& __m,
1374 const chrono::weekday_last& __wdl) noexcept
1375 :_M_m{__m}, _M_wdl{__wdl}
1378 constexpr chrono::month
1379 month() const noexcept
1382 constexpr chrono::weekday_last
1383 weekday_last() const noexcept
1388 { return _M_m.ok() && _M_wdl.ok(); }
1390 friend constexpr bool
1391 operator==(const month_weekday_last& __x,
1392 const month_weekday_last& __y) noexcept
1394 return __x.month() == __y.month()
1395 && __x.weekday_last() == __y.weekday_last();
1398 friend constexpr month_weekday_last
1399 operator/(const chrono::month& __m,
1400 const chrono::weekday_last& __wdl) noexcept
1401 { return {__m, __wdl}; }
1403 friend constexpr month_weekday_last
1404 operator/(int __m, const chrono::weekday_last& __wdl) noexcept
1405 { return chrono::month(unsigned(__m)) / __wdl; }
1407 friend constexpr month_weekday_last
1408 operator/(const chrono::weekday_last& __wdl,
1409 const chrono::month& __m) noexcept
1410 { return __m / __wdl; }
1412 friend constexpr month_weekday_last
1413 operator/(const chrono::weekday_last& __wdl, int __m) noexcept
1414 { return chrono::month(unsigned(__m)) / __wdl; }
1416 friend constexpr year_month_weekday_last
1417 operator/(int __y, const month_weekday_last& __mwdl) noexcept;
1419 friend constexpr year_month_weekday_last
1420 operator/(const month_weekday_last& __mwdl, int __y) noexcept;
1427 // [time.cal.ym], [time.cal.ymd], etc constrain the 'months'-based
1428 // addition/subtraction operator overloads like so:
1430 // Constraints: if the argument supplied by the caller for the months
1431 // parameter is convertible to years, its implicit conversion sequence
1432 // to years is worse than its implicit conversion sequence to months.
1434 // We realize this constraint by templatizing the 'months'-based
1435 // overloads (using a dummy defaulted template parameter), so that
1436 // overload resolution doesn't select the 'months'-based overload unless
1437 // the implicit conversion sequence to 'months' is better than that to
1439 using __months_years_conversion_disambiguator = void;
1449 year_month() = default;
1452 year_month(const chrono::year& __y, const chrono::month& __m) noexcept
1453 : _M_y{__y}, _M_m{__m}
1456 constexpr chrono::year
1457 year() const noexcept
1460 constexpr chrono::month
1461 month() const noexcept
1464 template<typename = __detail::__months_years_conversion_disambiguator>
1465 constexpr year_month&
1466 operator+=(const months& __dm) noexcept
1468 *this = *this + __dm;
1472 template<typename = __detail::__months_years_conversion_disambiguator>
1473 constexpr year_month&
1474 operator-=(const months& __dm) noexcept
1476 *this = *this - __dm;
1480 constexpr year_month&
1481 operator+=(const years& __dy) noexcept
1483 *this = *this + __dy;
1487 constexpr year_month&
1488 operator-=(const years& __dy) noexcept
1490 *this = *this - __dy;
1496 { return _M_y.ok() && _M_m.ok(); }
1498 friend constexpr bool
1499 operator==(const year_month& __x, const year_month& __y) noexcept
1500 { return __x.year() == __y.year() && __x.month() == __y.month(); }
1502 friend constexpr strong_ordering
1503 operator<=>(const year_month& __x, const year_month& __y) noexcept
1506 template<typename = __detail::__months_years_conversion_disambiguator>
1507 friend constexpr year_month
1508 operator+(const year_month& __ym, const months& __dm) noexcept
1511 auto __m = __ym.month() + __dm;
1512 auto __i = int(unsigned(__ym.month())) - 1 + __dm.count();
1514 ? __ym.year() + years{(__i - 11) / 12}
1515 : __ym.year() + years{__i / 12});
1519 template<typename = __detail::__months_years_conversion_disambiguator>
1520 friend constexpr year_month
1521 operator+(const months& __dm, const year_month& __ym) noexcept
1522 { return __ym + __dm; }
1524 template<typename = __detail::__months_years_conversion_disambiguator>
1525 friend constexpr year_month
1526 operator-(const year_month& __ym, const months& __dm) noexcept
1527 { return __ym + -__dm; }
1529 friend constexpr months
1530 operator-(const year_month& __x, const year_month& __y) noexcept
1532 return (__x.year() - __y.year()
1533 + months{static_cast<int>(unsigned{__x.month()})
1534 - static_cast<int>(unsigned{__y.month()})});
1537 friend constexpr year_month
1538 operator+(const year_month& __ym, const years& __dy) noexcept
1539 { return (__ym.year() + __dy) / __ym.month(); }
1541 friend constexpr year_month
1542 operator+(const years& __dy, const year_month& __ym) noexcept
1543 { return __ym + __dy; }
1545 friend constexpr year_month
1546 operator-(const year_month& __ym, const years& __dy) noexcept
1547 { return __ym + -__dy; }
1549 friend constexpr year_month
1550 operator/(const chrono::year& __y, const chrono::month& __m) noexcept
1551 { return {__y, __m}; }
1553 friend constexpr year_month
1554 operator/(const chrono::year& __y, int __m) noexcept
1555 { return {__y, chrono::month(unsigned(__m))}; }
1557 friend constexpr year_month_day
1558 operator/(const year_month& __ym, int __d) noexcept;
1560 friend constexpr year_month_day_last
1561 operator/(const year_month& __ym, last_spec) noexcept;
1566 class year_month_day
1573 static constexpr year_month_day _S_from_days(const days& __dp) noexcept;
1575 constexpr days _M_days_since_epoch() const noexcept;
1578 year_month_day() = default;
1581 year_month_day(const chrono::year& __y, const chrono::month& __m,
1582 const chrono::day& __d) noexcept
1583 : _M_y{__y}, _M_m{__m}, _M_d{__d}
1587 year_month_day(const year_month_day_last& __ymdl) noexcept;
1590 year_month_day(const sys_days& __dp) noexcept
1591 : year_month_day(_S_from_days(__dp.time_since_epoch()))
1595 year_month_day(const local_days& __dp) noexcept
1596 : year_month_day(sys_days{__dp.time_since_epoch()})
1599 template<typename = __detail::__months_years_conversion_disambiguator>
1600 constexpr year_month_day&
1601 operator+=(const months& __m) noexcept
1603 *this = *this + __m;
1607 template<typename = __detail::__months_years_conversion_disambiguator>
1608 constexpr year_month_day&
1609 operator-=(const months& __m) noexcept
1611 *this = *this - __m;
1615 constexpr year_month_day&
1616 operator+=(const years& __y) noexcept
1618 *this = *this + __y;
1622 constexpr year_month_day&
1623 operator-=(const years& __y) noexcept
1625 *this = *this - __y;
1629 constexpr chrono::year
1630 year() const noexcept
1633 constexpr chrono::month
1634 month() const noexcept
1637 constexpr chrono::day
1638 day() const noexcept
1642 operator sys_days() const noexcept
1643 { return sys_days{_M_days_since_epoch()}; }
1646 operator local_days() const noexcept
1647 { return local_days{sys_days{*this}.time_since_epoch()}; }
1649 constexpr bool ok() const noexcept;
1651 friend constexpr bool
1652 operator==(const year_month_day& __x, const year_month_day& __y) noexcept
1654 return __x.year() == __y.year()
1655 && __x.month() == __y.month()
1656 && __x.day() == __y.day();
1659 friend constexpr strong_ordering
1660 operator<=>(const year_month_day& __x, const year_month_day& __y) noexcept
1663 template<typename = __detail::__months_years_conversion_disambiguator>
1664 friend constexpr year_month_day
1665 operator+(const year_month_day& __ymd, const months& __dm) noexcept
1666 { return (__ymd.year() / __ymd.month() + __dm) / __ymd.day(); }
1668 template<typename = __detail::__months_years_conversion_disambiguator>
1669 friend constexpr year_month_day
1670 operator+(const months& __dm, const year_month_day& __ymd) noexcept
1671 { return __ymd + __dm; }
1673 friend constexpr year_month_day
1674 operator+(const year_month_day& __ymd, const years& __dy) noexcept
1675 { return (__ymd.year() + __dy) / __ymd.month() / __ymd.day(); }
1677 friend constexpr year_month_day
1678 operator+(const years& __dy, const year_month_day& __ymd) noexcept
1679 { return __ymd + __dy; }
1681 template<typename = __detail::__months_years_conversion_disambiguator>
1682 friend constexpr year_month_day
1683 operator-(const year_month_day& __ymd, const months& __dm) noexcept
1684 { return __ymd + -__dm; }
1686 friend constexpr year_month_day
1687 operator-(const year_month_day& __ymd, const years& __dy) noexcept
1688 { return __ymd + -__dy; }
1690 friend constexpr year_month_day
1691 operator/(const year_month& __ym, const chrono::day& __d) noexcept
1692 { return {__ym.year(), __ym.month(), __d}; }
1694 friend constexpr year_month_day
1695 operator/(const year_month& __ym, int __d) noexcept
1696 { return __ym / chrono::day{unsigned(__d)}; }
1698 friend constexpr year_month_day
1699 operator/(const chrono::year& __y, const month_day& __md) noexcept
1700 { return __y / __md.month() / __md.day(); }
1702 friend constexpr year_month_day
1703 operator/(int __y, const month_day& __md) noexcept
1704 { return chrono::year{__y} / __md; }
1706 friend constexpr year_month_day
1707 operator/(const month_day& __md, const chrono::year& __y) noexcept
1708 { return __y / __md; }
1710 friend constexpr year_month_day
1711 operator/(const month_day& __md, int __y) noexcept
1712 { return chrono::year(__y) / __md; }
1715 // Construct from days since 1970/01/01.
1716 // Proposition 6.3 of Neri and Schneider,
1717 // "Euclidean Affine Functions and Applications to Calendar Algorithms".
1718 // https://arxiv.org/abs/2102.06959
1719 constexpr year_month_day
1720 year_month_day::_S_from_days(const days& __dp) noexcept
1722 constexpr auto __z2 = static_cast<uint32_t>(-1468000);
1723 constexpr auto __r2_e3 = static_cast<uint32_t>(536895458);
1725 const auto __r0 = static_cast<uint32_t>(__dp.count()) + __r2_e3;
1727 const auto __n1 = 4 * __r0 + 3;
1728 const auto __q1 = __n1 / 146097;
1729 const auto __r1 = __n1 % 146097 / 4;
1731 constexpr auto __p32 = static_cast<uint64_t>(1) << 32;
1732 const auto __n2 = 4 * __r1 + 3;
1733 const auto __u2 = static_cast<uint64_t>(2939745) * __n2;
1734 const auto __q2 = static_cast<uint32_t>(__u2 / __p32);
1735 const auto __r2 = static_cast<uint32_t>(__u2 % __p32) / 2939745 / 4;
1737 constexpr auto __p16 = static_cast<uint32_t>(1) << 16;
1738 const auto __n3 = 2141 * __r2 + 197913;
1739 const auto __q3 = __n3 / __p16;
1740 const auto __r3 = __n3 % __p16 / 2141;
1742 const auto __y0 = 100 * __q1 + __q2;
1743 const auto __m0 = __q3;
1744 const auto __d0 = __r3;
1746 const auto __j = __r2 >= 306;
1747 const auto __y1 = __y0 + __j;
1748 const auto __m1 = __j ? __m0 - 12 : __m0;
1749 const auto __d1 = __d0 + 1;
1751 return year_month_day{chrono::year{static_cast<int>(__y1 + __z2)},
1752 chrono::month{__m1}, chrono::day{__d1}};
1755 // Days since 1970/01/01.
1756 // Proposition 6.2 of Neri and Schneider,
1757 // "Euclidean Affine Functions and Applications to Calendar Algorithms".
1758 // https://arxiv.org/abs/2102.06959
1760 year_month_day::_M_days_since_epoch() const noexcept
1762 auto constexpr __z2 = static_cast<uint32_t>(-1468000);
1763 auto constexpr __r2_e3 = static_cast<uint32_t>(536895458);
1765 const auto __y1 = static_cast<uint32_t>(static_cast<int>(_M_y)) - __z2;
1766 const auto __m1 = static_cast<uint32_t>(static_cast<unsigned>(_M_m));
1767 const auto __d1 = static_cast<uint32_t>(static_cast<unsigned>(_M_d));
1769 const auto __j = static_cast<uint32_t>(__m1 < 3);
1770 const auto __y0 = __y1 - __j;
1771 const auto __m0 = __j ? __m1 + 12 : __m1;
1772 const auto __d0 = __d1 - 1;
1774 const auto __q1 = __y0 / 100;
1775 const auto __yc = 1461 * __y0 / 4 - __q1 + __q1 / 4;
1776 const auto __mc = (979 *__m0 - 2919) / 32;
1777 const auto __dc = __d0;
1779 return days{static_cast<int32_t>(__yc + __mc + __dc - __r2_e3)};
1782 // YEAR_MONTH_DAY_LAST
1784 class year_month_day_last
1788 chrono::month_day_last _M_mdl;
1792 year_month_day_last(const chrono::year& __y,
1793 const chrono::month_day_last& __mdl) noexcept
1794 : _M_y{__y}, _M_mdl{__mdl}
1797 template<typename = __detail::__months_years_conversion_disambiguator>
1798 constexpr year_month_day_last&
1799 operator+=(const months& __m) noexcept
1801 *this = *this + __m;
1805 template<typename = __detail::__months_years_conversion_disambiguator>
1806 constexpr year_month_day_last&
1807 operator-=(const months& __m) noexcept
1809 *this = *this - __m;
1813 constexpr year_month_day_last&
1814 operator+=(const years& __y) noexcept
1816 *this = *this + __y;
1820 constexpr year_month_day_last&
1821 operator-=(const years& __y) noexcept
1823 *this = *this - __y;
1827 constexpr chrono::year
1828 year() const noexcept
1831 constexpr chrono::month
1832 month() const noexcept
1833 { return _M_mdl.month(); }
1835 constexpr chrono::month_day_last
1836 month_day_last() const noexcept
1839 // Return A day representing the last day of this year, month pair.
1840 constexpr chrono::day
1841 day() const noexcept
1843 const auto __m = static_cast<unsigned>(month());
1845 // The result is unspecified if __m < 1 or __m > 12. Hence, assume
1846 // 1 <= __m <= 12. For __m != 2, day() == 30 or day() == 31 or, in
1847 // other words, day () == 30 | b, where b is in {0, 1}.
1849 // If __m in {1, 3, 4, 5, 6, 7}, then b is 1 if, and only if, __m is
1850 // odd. Hence, b = __m & 1 = (__m ^ 0) & 1.
1852 // If __m in {8, 9, 10, 11, 12}, then b is 1 if, and only if, __m is
1853 // even. Hence, b = (__m ^ 1) & 1.
1855 // Therefore, b = (__m ^ c) & 1, where c = 0, if __m < 8, or c = 1 if
1856 // __m >= 8, that is, c = __m >> 3.
1858 // Since 30 = (11110)_2 and __m <= 31 = (11111)_2, the "& 1" in b's
1859 // calculation is unnecessary.
1861 // The performance of this implementation does not depend on look-up
1862 // tables being on the L1 cache.
1863 return chrono::day{__m != 2 ? (__m ^ (__m >> 3)) | 30
1864 : _M_y.is_leap() ? 29 : 28};
1868 operator sys_days() const noexcept
1869 { return sys_days{year() / month() / day()}; }
1872 operator local_days() const noexcept
1873 { return local_days{sys_days{*this}.time_since_epoch()}; }
1877 { return _M_y.ok() && _M_mdl.ok(); }
1879 friend constexpr bool
1880 operator==(const year_month_day_last& __x,
1881 const year_month_day_last& __y) noexcept
1883 return __x.year() == __y.year()
1884 && __x.month_day_last() == __y.month_day_last();
1887 friend constexpr strong_ordering
1888 operator<=>(const year_month_day_last& __x,
1889 const year_month_day_last& __y) noexcept
1892 template<typename = __detail::__months_years_conversion_disambiguator>
1893 friend constexpr year_month_day_last
1894 operator+(const year_month_day_last& __ymdl,
1895 const months& __dm) noexcept
1896 { return (__ymdl.year() / __ymdl.month() + __dm) / last; }
1898 template<typename = __detail::__months_years_conversion_disambiguator>
1899 friend constexpr year_month_day_last
1900 operator+(const months& __dm,
1901 const year_month_day_last& __ymdl) noexcept
1902 { return __ymdl + __dm; }
1904 template<typename = __detail::__months_years_conversion_disambiguator>
1905 friend constexpr year_month_day_last
1906 operator-(const year_month_day_last& __ymdl,
1907 const months& __dm) noexcept
1908 { return __ymdl + -__dm; }
1910 friend constexpr year_month_day_last
1911 operator+(const year_month_day_last& __ymdl,
1912 const years& __dy) noexcept
1913 { return {__ymdl.year() + __dy, __ymdl.month_day_last()}; }
1915 friend constexpr year_month_day_last
1916 operator+(const years& __dy,
1917 const year_month_day_last& __ymdl) noexcept
1918 { return __ymdl + __dy; }
1920 friend constexpr year_month_day_last
1921 operator-(const year_month_day_last& __ymdl,
1922 const years& __dy) noexcept
1923 { return __ymdl + -__dy; }
1925 friend constexpr year_month_day_last
1926 operator/(const year_month& __ym, last_spec) noexcept
1927 { return {__ym.year(), chrono::month_day_last{__ym.month()}}; }
1929 friend constexpr year_month_day_last
1930 operator/(const chrono::year& __y,
1931 const chrono::month_day_last& __mdl) noexcept
1932 { return {__y, __mdl}; }
1934 friend constexpr year_month_day_last
1935 operator/(int __y, const chrono::month_day_last& __mdl) noexcept
1936 { return chrono::year(__y) / __mdl; }
1938 friend constexpr year_month_day_last
1939 operator/(const chrono::month_day_last& __mdl,
1940 const chrono::year& __y) noexcept
1941 { return __y / __mdl; }
1943 friend constexpr year_month_day_last
1944 operator/(const chrono::month_day_last& __mdl, int __y) noexcept
1945 { return chrono::year(__y) / __mdl; }
1948 // year_month_day ctor from year_month_day_last
1950 year_month_day::year_month_day(const year_month_day_last& __ymdl) noexcept
1951 : _M_y{__ymdl.year()}, _M_m{__ymdl.month()}, _M_d{__ymdl.day()}
1955 year_month_day::ok() const noexcept
1957 if (!_M_y.ok() || !_M_m.ok())
1959 return chrono::day{1} <= _M_d && _M_d <= (_M_y / _M_m / last).day();
1962 // YEAR_MONTH_WEEKDAY
1964 class year_month_weekday
1969 chrono::weekday_indexed _M_wdi;
1971 static constexpr year_month_weekday
1972 _S_from_sys_days(const sys_days& __dp)
1974 year_month_day __ymd{__dp};
1975 chrono::weekday __wd{__dp};
1976 auto __index = __wd[(unsigned{__ymd.day()} - 1) / 7 + 1];
1977 return {__ymd.year(), __ymd.month(), __index};
1981 year_month_weekday() = default;
1984 year_month_weekday(const chrono::year& __y, const chrono::month& __m,
1985 const chrono::weekday_indexed& __wdi) noexcept
1986 : _M_y{__y}, _M_m{__m}, _M_wdi{__wdi}
1990 year_month_weekday(const sys_days& __dp) noexcept
1991 : year_month_weekday{_S_from_sys_days(__dp)}
1995 year_month_weekday(const local_days& __dp) noexcept
1996 : year_month_weekday{sys_days{__dp.time_since_epoch()}}
1999 template<typename = __detail::__months_years_conversion_disambiguator>
2000 constexpr year_month_weekday&
2001 operator+=(const months& __m) noexcept
2003 *this = *this + __m;
2007 template<typename = __detail::__months_years_conversion_disambiguator>
2008 constexpr year_month_weekday&
2009 operator-=(const months& __m) noexcept
2011 *this = *this - __m;
2015 constexpr year_month_weekday&
2016 operator+=(const years& __y) noexcept
2018 *this = *this + __y;
2022 constexpr year_month_weekday&
2023 operator-=(const years& __y) noexcept
2025 *this = *this - __y;
2029 constexpr chrono::year
2030 year() const noexcept
2033 constexpr chrono::month
2034 month() const noexcept
2037 constexpr chrono::weekday
2038 weekday() const noexcept
2039 { return _M_wdi.weekday(); }
2042 index() const noexcept
2043 { return _M_wdi.index(); }
2045 constexpr chrono::weekday_indexed
2046 weekday_indexed() const noexcept
2050 operator sys_days() const noexcept
2052 auto __d = sys_days{year() / month() / 1};
2053 return __d + (weekday() - chrono::weekday(__d)
2054 + days{(static_cast<int>(index())-1)*7});
2058 operator local_days() const noexcept
2059 { return local_days{sys_days{*this}.time_since_epoch()}; }
2064 if (!_M_y.ok() || !_M_m.ok() || !_M_wdi.ok())
2066 if (_M_wdi.index() <= 4)
2068 days __d = (_M_wdi.weekday()
2069 - chrono::weekday{sys_days{_M_y / _M_m / 1}}
2070 + days((_M_wdi.index()-1)*7 + 1));
2071 __glibcxx_assert(__d.count() >= 1);
2072 return (unsigned)__d.count() <= (unsigned)(_M_y / _M_m / last).day();
2075 friend constexpr bool
2076 operator==(const year_month_weekday& __x,
2077 const year_month_weekday& __y) noexcept
2079 return __x.year() == __y.year()
2080 && __x.month() == __y.month()
2081 && __x.weekday_indexed() == __y.weekday_indexed();
2084 template<typename = __detail::__months_years_conversion_disambiguator>
2085 friend constexpr year_month_weekday
2086 operator+(const year_month_weekday& __ymwd, const months& __dm) noexcept
2088 return ((__ymwd.year() / __ymwd.month() + __dm)
2089 / __ymwd.weekday_indexed());
2092 template<typename = __detail::__months_years_conversion_disambiguator>
2093 friend constexpr year_month_weekday
2094 operator+(const months& __dm, const year_month_weekday& __ymwd) noexcept
2095 { return __ymwd + __dm; }
2097 friend constexpr year_month_weekday
2098 operator+(const year_month_weekday& __ymwd, const years& __dy) noexcept
2099 { return {__ymwd.year() + __dy, __ymwd.month(), __ymwd.weekday_indexed()}; }
2101 friend constexpr year_month_weekday
2102 operator+(const years& __dy, const year_month_weekday& __ymwd) noexcept
2103 { return __ymwd + __dy; }
2105 template<typename = __detail::__months_years_conversion_disambiguator>
2106 friend constexpr year_month_weekday
2107 operator-(const year_month_weekday& __ymwd, const months& __dm) noexcept
2108 { return __ymwd + -__dm; }
2110 friend constexpr year_month_weekday
2111 operator-(const year_month_weekday& __ymwd, const years& __dy) noexcept
2112 { return __ymwd + -__dy; }
2114 friend constexpr year_month_weekday
2115 operator/(const year_month& __ym,
2116 const chrono::weekday_indexed& __wdi) noexcept
2117 { return {__ym.year(), __ym.month(), __wdi}; }
2119 friend constexpr year_month_weekday
2120 operator/(const chrono::year& __y, const month_weekday& __mwd) noexcept
2121 { return {__y, __mwd.month(), __mwd.weekday_indexed()}; }
2123 friend constexpr year_month_weekday
2124 operator/(int __y, const month_weekday& __mwd) noexcept
2125 { return chrono::year(__y) / __mwd; }
2127 friend constexpr year_month_weekday
2128 operator/(const month_weekday& __mwd, const chrono::year& __y) noexcept
2129 { return __y / __mwd; }
2131 friend constexpr year_month_weekday
2132 operator/(const month_weekday& __mwd, int __y) noexcept
2133 { return chrono::year(__y) / __mwd; }
2136 // YEAR_MONTH_WEEKDAY_LAST
2138 class year_month_weekday_last
2143 chrono::weekday_last _M_wdl;
2147 year_month_weekday_last(const chrono::year& __y, const chrono::month& __m,
2148 const chrono::weekday_last& __wdl) noexcept
2149 : _M_y{__y}, _M_m{__m}, _M_wdl{__wdl}
2152 template<typename = __detail::__months_years_conversion_disambiguator>
2153 constexpr year_month_weekday_last&
2154 operator+=(const months& __m) noexcept
2156 *this = *this + __m;
2160 template<typename = __detail::__months_years_conversion_disambiguator>
2161 constexpr year_month_weekday_last&
2162 operator-=(const months& __m) noexcept
2164 *this = *this - __m;
2168 constexpr year_month_weekday_last&
2169 operator+=(const years& __y) noexcept
2171 *this = *this + __y;
2175 constexpr year_month_weekday_last&
2176 operator-=(const years& __y) noexcept
2178 *this = *this - __y;
2182 constexpr chrono::year
2183 year() const noexcept
2186 constexpr chrono::month
2187 month() const noexcept
2190 constexpr chrono::weekday
2191 weekday() const noexcept
2192 { return _M_wdl.weekday(); }
2194 constexpr chrono::weekday_last
2195 weekday_last() const noexcept
2199 operator sys_days() const noexcept
2201 const auto __d = sys_days{_M_y / _M_m / last};
2202 return sys_days{(__d - (chrono::weekday{__d}
2203 - _M_wdl.weekday())).time_since_epoch()};
2207 operator local_days() const noexcept
2208 { return local_days{sys_days{*this}.time_since_epoch()}; }
2212 { return _M_y.ok() && _M_m.ok() && _M_wdl.ok(); }
2214 friend constexpr bool
2215 operator==(const year_month_weekday_last& __x,
2216 const year_month_weekday_last& __y) noexcept
2218 return __x.year() == __y.year()
2219 && __x.month() == __y.month()
2220 && __x.weekday_last() == __y.weekday_last();
2223 template<typename = __detail::__months_years_conversion_disambiguator>
2224 friend constexpr year_month_weekday_last
2225 operator+(const year_month_weekday_last& __ymwdl,
2226 const months& __dm) noexcept
2228 return ((__ymwdl.year() / __ymwdl.month() + __dm)
2229 / __ymwdl.weekday_last());
2232 template<typename = __detail::__months_years_conversion_disambiguator>
2233 friend constexpr year_month_weekday_last
2234 operator+(const months& __dm,
2235 const year_month_weekday_last& __ymwdl) noexcept
2236 { return __ymwdl + __dm; }
2238 friend constexpr year_month_weekday_last
2239 operator+(const year_month_weekday_last& __ymwdl,
2240 const years& __dy) noexcept
2241 { return {__ymwdl.year() + __dy, __ymwdl.month(), __ymwdl.weekday_last()}; }
2243 friend constexpr year_month_weekday_last
2244 operator+(const years& __dy,
2245 const year_month_weekday_last& __ymwdl) noexcept
2246 { return __ymwdl + __dy; }
2248 template<typename = __detail::__months_years_conversion_disambiguator>
2249 friend constexpr year_month_weekday_last
2250 operator-(const year_month_weekday_last& __ymwdl,
2251 const months& __dm) noexcept
2252 { return __ymwdl + -__dm; }
2254 friend constexpr year_month_weekday_last
2255 operator-(const year_month_weekday_last& __ymwdl,
2256 const years& __dy) noexcept
2257 { return __ymwdl + -__dy; }
2259 friend constexpr year_month_weekday_last
2260 operator/(const year_month& __ym,
2261 const chrono::weekday_last& __wdl) noexcept
2262 { return {__ym.year(), __ym.month(), __wdl}; }
2264 friend constexpr year_month_weekday_last
2265 operator/(const chrono::year& __y,
2266 const chrono::month_weekday_last& __mwdl) noexcept
2267 { return {__y, __mwdl.month(), __mwdl.weekday_last()}; }
2269 friend constexpr year_month_weekday_last
2270 operator/(int __y, const chrono::month_weekday_last& __mwdl) noexcept
2271 { return chrono::year(__y) / __mwdl; }
2273 friend constexpr year_month_weekday_last
2274 operator/(const chrono::month_weekday_last& __mwdl,
2275 const chrono::year& __y) noexcept
2276 { return __y / __mwdl; }
2278 friend constexpr year_month_weekday_last
2279 operator/(const chrono::month_weekday_last& __mwdl, int __y) noexcept
2280 { return chrono::year(__y) / __mwdl; }
2285 /// @cond undocumented
2289 __pow10(unsigned __n)
2297 template<typename _Duration> struct __utc_leap_second;
2301 /** Utility for splitting a duration into hours, minutes, and seconds
2303 * This is a convenience type that provides accessors for the constituent
2304 * parts (hours, minutes, seconds and subseconds) of a duration.
2308 template<typename _Duration>
2311 static_assert( __is_duration<_Duration>::value );
2314 static consteval int
2315 _S_fractional_width()
2317 auto __den = _Duration::period::den;
2318 const int __multiplicity_2 = std::__countr_zero((uintmax_t)__den);
2319 __den >>= __multiplicity_2;
2320 int __multiplicity_5 = 0;
2321 while ((__den % 5) == 0)
2329 int __width = (__multiplicity_2 > __multiplicity_5
2330 ? __multiplicity_2 : __multiplicity_5);
2337 hh_mm_ss(_Duration __d, bool __is_neg)
2338 : _M_h (duration_cast<chrono::hours>(__d)),
2339 _M_m (duration_cast<chrono::minutes>(__d - hours())),
2340 _M_s (duration_cast<chrono::seconds>(__d - hours() - minutes())),
2343 auto __ss = __d - hours() - minutes() - seconds();
2344 if constexpr (treat_as_floating_point_v<typename precision::rep>)
2345 _M_ss._M_r = __ss.count();
2346 else if constexpr (precision::period::den != 1)
2347 _M_ss._M_r = duration_cast<precision>(__ss).count();
2350 static constexpr _Duration
2351 _S_abs(_Duration __d)
2353 if constexpr (numeric_limits<typename _Duration::rep>::is_signed)
2354 return chrono::abs(__d);
2360 static constexpr unsigned fractional_width = {_S_fractional_width()};
2363 = duration<common_type_t<typename _Duration::rep,
2364 chrono::seconds::rep>,
2365 ratio<1, __detail::__pow10(fractional_width)>>;
2367 constexpr hh_mm_ss() noexcept = default;
2370 hh_mm_ss(_Duration __d)
2371 : hh_mm_ss(_S_abs(__d), __d < _Duration::zero())
2375 is_negative() const noexcept
2377 if constexpr (!_S_is_unsigned)
2383 constexpr chrono::hours
2384 hours() const noexcept
2387 constexpr chrono::minutes
2388 minutes() const noexcept
2391 constexpr chrono::seconds
2392 seconds() const noexcept
2396 subseconds() const noexcept
2397 { return static_cast<precision>(_M_ss); }
2400 operator precision() const noexcept
2401 { return to_duration(); }
2404 to_duration() const noexcept
2406 if constexpr (!_S_is_unsigned)
2408 return -(_M_h + _M_m + _M_s + subseconds());
2409 return _M_h + _M_m + _M_s + subseconds();
2413 static constexpr bool _S_is_unsigned
2414 = __and_v<is_integral<typename _Duration::rep>,
2415 is_unsigned<typename _Duration::rep>>;
2417 template<typename _Ratio>
2418 using __byte_duration = duration<unsigned char, _Ratio>;
2420 // The type of the _M_ss member that holds the subsecond precision.
2421 template<typename _Dur>
2424 typename _Dur::rep _M_r{};
2427 operator _Dur() const noexcept
2428 { return _Dur(_M_r); }
2431 // An empty class if this precision doesn't need subseconds.
2432 template<typename _Rep>
2433 requires (!treat_as_floating_point_v<_Rep>)
2434 struct __subseconds<duration<_Rep, ratio<1>>>
2437 operator duration<_Rep, ratio<1>>() const noexcept
2441 // True if the maximum constructor argument can be represented in _Tp.
2442 template<typename _Tp>
2443 static constexpr bool __fits
2444 = duration_values<typename _Duration::rep>::max()
2445 <= duration_values<_Tp>::max();
2447 template<typename _Rep, typename _Period>
2448 requires (!treat_as_floating_point_v<_Rep>)
2449 && ratio_less_v<_Period, ratio<1, 1>>
2450 && (ratio_greater_equal_v<_Period, ratio<1, 250>>
2451 || __fits<unsigned char>)
2452 struct __subseconds<duration<_Rep, _Period>>
2454 unsigned char _M_r{};
2457 operator duration<_Rep, _Period>() const noexcept
2458 { return duration<_Rep, _Period>(_M_r); }
2461 template<typename _Rep, typename _Period>
2462 requires (!treat_as_floating_point_v<_Rep>)
2463 && ratio_less_v<_Period, ratio<1, 250>>
2464 && (ratio_greater_equal_v<_Period, ratio<1, 4000000000>>
2465 || __fits<uint_least32_t>)
2466 struct __subseconds<duration<_Rep, _Period>>
2468 uint_least32_t _M_r{};
2471 operator duration<_Rep, _Period>() const noexcept
2472 { return duration<_Rep, _Period>(_M_r); }
2475 chrono::hours _M_h{};
2476 __byte_duration<ratio<60>> _M_m{};
2477 __byte_duration<ratio<1>> _M_s{};
2479 __subseconds<precision> _M_ss{};
2481 template<typename> friend struct __detail::__utc_leap_second;
2484 /// @cond undocumented
2487 // Represents a time that is within a leap second insertion.
2488 template<typename _Duration>
2489 struct __utc_leap_second
2492 __utc_leap_second(const sys_time<_Duration>& __s)
2493 : _M_date(chrono::floor<days>(__s)), _M_time(__s - _M_date)
2499 hh_mm_ss<common_type_t<_Duration, days>> _M_time;
2504 // 12/24 HOURS FUNCTIONS
2507 is_am(const hours& __h) noexcept
2508 { return 0h <= __h && __h <= 11h; }
2511 is_pm(const hours& __h) noexcept
2512 { return 12h <= __h && __h <= 23h; }
2515 make12(const hours& __h) noexcept
2525 make24(const hours& __h, bool __is_pm) noexcept
2543#if _GLIBCXX_USE_CXX11_ABI || ! _GLIBCXX_USE_DUAL_ABI
2544 // C++20 [time.zones] Time zones
2559 static constexpr int unique = 0;
2560 static constexpr int nonexistent = 1;
2561 static constexpr int ambiguous = 2;
2568 class nonexistent_local_time : public runtime_error
2571 template<typename _Duration>
2572 nonexistent_local_time(const local_time<_Duration>& __tp,
2573 const local_info& __i)
2574 : runtime_error(_S_make_what_str(__tp, __i))
2575 { __glibcxx_assert(__i.result == local_info::nonexistent); }
2578 template<typename _Duration>
2580 _S_make_what_str(const local_time<_Duration>& __tp,
2581 const local_info& __i)
2583 std::ostringstream __os;
2584 __os << __tp << " is in a gap between\n"
2585 << local_seconds(__i.first.end.time_since_epoch())
2586 + __i.first.offset << ' ' << __i.first.abbrev << " and\n"
2587 << local_seconds(__i.second.begin.time_since_epoch())
2588 + __i.second.offset << ' ' << __i.second.abbrev
2589 << " which are both equivalent to\n"
2590 << __i.first.end << " UTC";
2591 return std::move(__os).str();
2595 class ambiguous_local_time : public runtime_error
2598 template<typename _Duration>
2599 ambiguous_local_time(const local_time<_Duration>& __tp,
2600 const local_info& __i)
2601 : runtime_error(_S_make_what_str(__tp, __i))
2602 { __glibcxx_assert(__i.result == local_info::ambiguous); }
2605 template<typename _Duration>
2607 _S_make_what_str(const local_time<_Duration>& __tp,
2608 const local_info& __i)
2610 std::ostringstream __os;
2611 __os << __tp << " is ambiguous. It could be\n"
2612 << __tp << ' ' << __i.first.abbrev << " == "
2613 << __tp - __i.first.offset << " UTC or\n"
2614 << __tp << ' ' << __i.second.abbrev << " == "
2615 << __tp - __i.second.offset << " UTC";
2616 return std::move(__os).str();
2620 template<typename _Duration>
2622 __throw_bad_local_time(const local_time<_Duration>& __tp,
2623 const local_info& __i)
2626 if (__i.result == local_info::nonexistent)
2627 throw nonexistent_local_time(__tp, __i);
2628 throw ambiguous_local_time(__tp, __i);
2634 enum class choose { earliest, latest };
2639 time_zone(time_zone&&) = default;
2640 time_zone& operator=(time_zone&&) = default;
2645 string_view name() const noexcept { return _M_name; }
2647 template<typename _Duration>
2649 get_info(const sys_time<_Duration>& __st) const
2650 { return _M_get_sys_info(chrono::floor<seconds>(__st)); }
2652 template<typename _Duration>
2654 get_info(const local_time<_Duration>& __tp) const
2655 { return _M_get_local_info(chrono::floor<seconds>(__tp)); }
2657 template<typename _Duration>
2658 sys_time<common_type_t<_Duration, seconds>>
2659 to_sys(const local_time<_Duration>& __tp) const
2661 local_info __info = get_info(__tp);
2663 if (__info.result != local_info::unique)
2664 __throw_bad_local_time(__tp, __info);
2666 return sys_time<_Duration>(__tp.time_since_epoch())
2667 - __info.first.offset;
2670 template<typename _Duration>
2671 sys_time<common_type_t<_Duration, seconds>>
2672 to_sys(const local_time<_Duration>& __tp, choose __z) const
2674 local_info __info = get_info(__tp);
2676 if (__info.result == local_info::nonexistent)
2677 return __info.first.end; // Last second of the previous sys_info.
2679 sys_time<_Duration> __st(__tp.time_since_epoch());
2681 if (__info.result == local_info::ambiguous && __z == choose::latest)
2682 return __st - __info.second.offset; // Time in the later sys_info.
2683 // else if __z == earliest, use __info.first.offset as below:
2685 return __st - __info.first.offset;
2688 template<typename _Duration>
2689 local_time<common_type_t<_Duration, seconds>>
2690 to_local(const sys_time<_Duration>& __tp) const
2692 auto __d = (__tp + get_info(__tp).offset).time_since_epoch();
2693 return local_time<common_type_t<_Duration, seconds>>(__d);
2696 [[nodiscard]] friend bool
2697 operator==(const time_zone& __x, const time_zone& __y) noexcept
2698 { return __x._M_name == __y._M_name; }
2700 [[nodiscard]] friend strong_ordering
2701 operator<=>(const time_zone& __x, const time_zone& __y) noexcept
2702 { return __x._M_name <=> __y._M_name; }
2705 sys_info _M_get_sys_info(sys_seconds) const;
2706 local_info _M_get_local_info(local_seconds) const;
2708 friend const tzdb& reload_tzdb();
2710 friend class tzdb_list;
2714 explicit time_zone(unique_ptr<_Impl> __p);
2716 unique_ptr<_Impl> _M_impl;
2719 const time_zone* locate_zone(string_view __tz_name);
2720 const time_zone* current_zone();
2722 /** The list of `chrono::tzdb` objects
2724 * A single object of this type is constructed by the C++ runtime,
2725 * and can be accessed by calling `chrono::get_tzdb_list()`.
2727 * The front of the list is the current `tzdb` object and can be accessed
2728 * via `chrono::get_tzdb_list().front()` or `chrono::get_tzdb()` or
2729 * `*chrono::get_tzdb_list().begin()`.
2731 * The `chrono::reload_tzdb()` function will check for a newer version
2732 * and if found, insert it at the front of the list.
2741 tzdb_list(const tzdb_list&) = delete;
2742 tzdb_list& operator=(const tzdb_list&) = delete;
2744 /** An iterator into the `tzdb_list`
2746 * As a extension, in libstdc++ each `tzdb` is reference-counted
2747 * and the `const_iterator` type shares ownership of the object it
2748 * refers to. This ensures that a `tzdb` erased from the list will
2749 * not be destroyed while there is an iterator that refers to it.
2751 class const_iterator
2754 using value_type = tzdb;
2755 using reference = const tzdb&;
2756 using pointer = const tzdb*;
2757 using difference_type = ptrdiff_t;
2758 using iterator_category = forward_iterator_tag;
2760 constexpr const_iterator() = default;
2761 const_iterator(const const_iterator&) = default;
2762 const_iterator(const_iterator&&) = default;
2763 const_iterator& operator=(const const_iterator&) = default;
2764 const_iterator& operator=(const_iterator&&) = default;
2766 reference operator*() const noexcept;
2767 pointer operator->() const noexcept { return &**this; }
2768 const_iterator& operator++();
2769 const_iterator operator++(int);
2771 bool operator==(const const_iterator&) const noexcept = default;
2774 explicit const_iterator(const shared_ptr<_Node>&) noexcept;
2776 friend class tzdb_list;
2778 shared_ptr<_Node> _M_node;
2779 void* _M_reserved = nullptr;
2782 /** Access the current `tzdb` at the front of the list.
2784 * This returns a reference to the same object as `chrono::get_tzdb()`.
2786 * @returns A reference to the current tzdb object.
2789 const tzdb& front() const noexcept;
2791 /** Remove the tzdb object _after_ the one the iterator refers to.
2793 * Calling this function concurently with any of `front()`, `begin()`,
2794 * or `end()` does not cause a data race, but in general this function
2795 * is not thread-safe. The behaviour may be undefined if erasing an
2796 * element from the list while another thread is calling the same
2797 * function, or incrementing an iterator into the list, or accessing
2798 * the element being erased (unless it is accessed through an iterator).
2800 * @param __p A dereferenceable iterator.
2801 * @returns An iterator the element after the one that was erased
2802 * (or `end()` if there is no such element).
2805 const_iterator erase_after(const_iterator __p);
2807 const_iterator begin() const noexcept;
2808 const_iterator end() const noexcept { return {}; }
2809 const_iterator cbegin() const noexcept { return begin(); }
2810 const_iterator cend() const noexcept { return end(); }
2813 constexpr explicit tzdb_list(nullptr_t);
2815 friend tzdb_list& get_tzdb_list();
2816 friend const tzdb& get_tzdb();
2817 friend const tzdb& reload_tzdb();
2819 friend class leap_second;
2820 friend struct time_zone::_Impl;
2821 friend class time_zone_link;
2824 class time_zone_link
2827 time_zone_link(time_zone_link&&) = default;
2828 time_zone_link& operator=(time_zone_link&&) = default;
2830 string_view name() const noexcept { return _M_name; }
2831 string_view target() const noexcept { return _M_target; }
2834 operator==(const time_zone_link& __x, const time_zone_link& __y) noexcept
2835 { return __x.name() == __y.name(); }
2837 friend strong_ordering
2838 operator<=>(const time_zone_link& __x, const time_zone_link& __y) noexcept
2839 { return __x.name() <=> __y.name(); }
2842 friend const tzdb& reload_tzdb();
2843 friend struct tzdb_list::_Node;
2845 explicit time_zone_link(nullptr_t) { }
2854 leap_second(const leap_second&) = default;
2855 leap_second& operator=(const leap_second&) = default;
2858 constexpr sys_seconds
2859 date() const noexcept
2861 if (_M_s >= _M_s.zero()) [[likely]]
2862 return sys_seconds(_M_s);
2863 return sys_seconds(-_M_s);
2868 value() const noexcept
2870 if (_M_s >= _M_s.zero()) [[likely]]
2875 // This can be defaulted because the database will never contain two
2876 // leap_second objects with the same date but different signs.
2877 [[nodiscard]] friend constexpr bool
2878 operator==(const leap_second&, const leap_second&) noexcept = default;
2880 [[nodiscard]] friend constexpr strong_ordering
2881 operator<=>(const leap_second& __x, const leap_second& __y) noexcept
2882 { return __x.date() <=> __y.date(); }
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; }
2890 template<typename _Duration>
2891 [[nodiscard]] friend constexpr bool
2892 operator<(const leap_second& __x,
2893 const sys_time<_Duration>& __y) noexcept
2894 { return __x.date() < __y; }
2896 template<typename _Duration>
2897 [[nodiscard]] friend constexpr bool
2898 operator<(const sys_time<_Duration>& __x,
2899 const leap_second& __y) noexcept
2900 { return __x < __y.date(); }
2902 template<typename _Duration>
2903 [[nodiscard]] friend constexpr bool
2904 operator>(const leap_second& __x,
2905 const sys_time<_Duration>& __y) noexcept
2906 { return __y < __x.date(); }
2908 template<typename _Duration>
2909 [[nodiscard]] friend constexpr bool
2910 operator>(const sys_time<_Duration>& __x,
2911 const leap_second& __y) noexcept
2912 { return __y.date() < __x; }
2914 template<typename _Duration>
2915 [[nodiscard]] friend constexpr bool
2916 operator<=(const leap_second& __x,
2917 const sys_time<_Duration>& __y) noexcept
2918 { return !(__y < __x.date()); }
2920 template<typename _Duration>
2921 [[nodiscard]] friend constexpr bool
2922 operator<=(const sys_time<_Duration>& __x,
2923 const leap_second& __y) noexcept
2924 { return !(__y.date() < __x); }
2926 template<typename _Duration>
2927 [[nodiscard]] friend constexpr bool
2928 operator>=(const leap_second& __x,
2929 const sys_time<_Duration>& __y) noexcept
2930 { return !(__x.date() < __y); }
2932 template<typename _Duration>
2933 [[nodiscard]] friend constexpr bool
2934 operator>=(const sys_time<_Duration>& __x,
2935 const leap_second& __y) noexcept
2936 { return !(__x < __y.date()); }
2938 template<three_way_comparable_with<seconds> _Duration>
2939 [[nodiscard]] friend constexpr auto
2940 operator<=>(const leap_second& __x,
2941 const sys_time<_Duration>& __y) noexcept
2942 { return __x.date() <=> __y; }
2945 explicit leap_second(seconds::rep __s) : _M_s(__s) { }
2947 friend struct tzdb_list::_Node;
2949 friend const tzdb& reload_tzdb();
2951 template<typename _Duration>
2952 friend leap_second_info
2953 get_leap_second_info(const utc_time<_Duration>&);
2955 seconds _M_s; // == date().time_since_epoch() * value().count()
2958 template<class _Tp> struct zoned_traits { };
2961 struct zoned_traits<const time_zone*>
2963 static const time_zone*
2965 { return std::chrono::locate_zone("UTC"); }
2967 static const time_zone*
2968 locate_zone(string_view __name)
2969 { return std::chrono::locate_zone(__name); }
2975 _GLIBCXX_STD_C::vector<time_zone> zones;
2976 _GLIBCXX_STD_C::vector<time_zone_link> links;
2977 _GLIBCXX_STD_C::vector<leap_second> leap_seconds;
2980 locate_zone(string_view __tz_name) const;
2983 current_zone() const;
2986 friend const tzdb& reload_tzdb();
2987 friend class time_zone;
2988 friend struct tzdb_list::_Node;
2991 tzdb_list& get_tzdb_list();
2992 const tzdb& get_tzdb();
2994 const tzdb& reload_tzdb();
2995 string remote_version();
2997 template<typename _Duration, typename _TimeZonePtr = const time_zone*>
3000 static_assert(__is_duration_v<_Duration>);
3002 using _Traits = zoned_traits<_TimeZonePtr>;
3004 // Every constructor that accepts a string_view as its first parameter
3005 // does not participate in class template argument deduction.
3006 using string_view = type_identity_t<std::string_view>;
3009 using duration = common_type_t<_Duration, seconds>;
3011 zoned_time() requires requires { _Traits::default_zone(); }
3014 zoned_time(const zoned_time&) = default;
3015 zoned_time& operator=(const zoned_time&) = default;
3017 zoned_time(const sys_time<_Duration>& __st)
3018 requires requires { _Traits::default_zone(); }
3023 zoned_time(_TimeZonePtr __z) : _M_zone(std::move(__z)) { }
3026 zoned_time(string_view __name)
3028 _TimeZonePtr{_Traits::locate_zone(std::string_view{})};
3030 : _M_zone(_Traits::locate_zone(__name))
3033 template<typename _Duration2>
3034 zoned_time(const zoned_time<_Duration2, _TimeZonePtr>& __zt)
3035 requires is_convertible_v<sys_time<_Duration2>, sys_time<_Duration>>
3036 : _M_zone(__zt._M_zone), _M_tp(__zt._M_tp)
3039 zoned_time(_TimeZonePtr __z, const sys_time<_Duration>& __st)
3040 : _M_zone(std::move(__z)), _M_tp(__st)
3043 zoned_time(string_view __name, const sys_time<_Duration>& __st)
3044 : zoned_time(_Traits::locate_zone(__name), __st)
3047 zoned_time(_TimeZonePtr __z, const local_time<_Duration>& __tp)
3049 { __z->to_sys(__tp) } -> convertible_to<sys_time<_Duration>>;
3051 : _M_zone(std::move(__z)), _M_tp(_M_zone->to_sys(__tp))
3054 zoned_time(string_view __name, const local_time<_Duration>& __tp)
3055 requires requires (_TimeZonePtr __z) {
3056 { _Traits::locate_zone(__name) } -> convertible_to<_TimeZonePtr>;
3057 { __z->to_sys(__tp) } -> convertible_to<sys_time<_Duration>>;
3059 : zoned_time(_Traits::locate_zone(__name), __tp)
3062 zoned_time(_TimeZonePtr __z, const local_time<_Duration>& __tp,
3065 { __z->to_sys(__tp, __c) } -> convertible_to<sys_time<_Duration>>;
3067 : _M_zone(std::move(__z)), _M_tp(_M_zone->to_sys(__tp, __c))
3070 zoned_time(string_view __name, const local_time<_Duration>& __tp,
3072 requires requires (_TimeZonePtr __z) {
3073 { _Traits::locate_zone(__name) } -> convertible_to<_TimeZonePtr>;
3074 { __z->to_sys(__tp, __c) } -> convertible_to<sys_time<_Duration>>;
3076 : _M_zone(_Traits::locate_zone(__name)),
3077 _M_tp(_M_zone->to_sys(__tp, __c))
3080 template<typename _Duration2, typename _TimeZonePtr2>
3081 zoned_time(_TimeZonePtr __z,
3082 const zoned_time<_Duration2, _TimeZonePtr2>& __zt)
3083 requires is_convertible_v<sys_time<_Duration2>, sys_time<_Duration>>
3084 : _M_zone(__z), _M_tp(__zt._M_tp)
3087 template<typename _Duration2, typename _TimeZonePtr2>
3088 zoned_time(_TimeZonePtr __z,
3089 const zoned_time<_Duration2, _TimeZonePtr2>& __zt,
3091 requires is_convertible_v<sys_time<_Duration2>, sys_time<_Duration>>
3092 : _M_zone(__z), _M_tp(__zt._M_tp)
3095 template<typename _Duration2, typename _TimeZonePtr2>
3096 zoned_time(string_view __name,
3097 const zoned_time<_Duration2, _TimeZonePtr2>& __zt)
3098 requires is_convertible_v<sys_time<_Duration2>, sys_time<_Duration>>
3100 { _Traits::locate_zone(__name) } -> convertible_to<_TimeZonePtr>;
3102 : _M_zone(_Traits::locate_zone(__name)), _M_tp(__zt._M_tp)
3105 template<typename _Duration2, typename _TimeZonePtr2>
3106 zoned_time(string_view __name,
3107 const zoned_time<_Duration2, _TimeZonePtr2>& __zt,
3109 requires is_convertible_v<sys_time<_Duration2>, sys_time<_Duration>>
3111 { _Traits::locate_zone(__name) } -> convertible_to<_TimeZonePtr>;
3113 : _M_zone(_Traits::locate_zone(__name)), _M_tp(__zt._M_tp)
3117 operator=(const sys_time<_Duration>& __st)
3124 operator=(const local_time<_Duration>& __lt)
3126 _M_tp = _M_zone->to_sys(__lt);
3131 operator sys_time<duration>() const { return _M_tp; }
3134 explicit operator local_time<duration>() const
3135 { return get_local_time(); }
3139 get_time_zone() const
3143 local_time<duration>
3144 get_local_time() const
3145 { return _M_zone->to_local(_M_tp); }
3149 get_sys_time() const
3155 { return _M_zone->get_info(_M_tp); }
3157 [[nodiscard]] friend bool
3158 operator==(const zoned_time&, const zoned_time&) = default;
3161 _TimeZonePtr _M_zone{ _Traits::default_zone() };
3162 sys_time<duration> _M_tp{};
3164 template<typename _Duration2, typename _TimeZonePtr2>
3165 friend class zoned_time;
3168 zoned_time() -> zoned_time<seconds>;
3170 template<typename _Duration>
3171 zoned_time(sys_time<_Duration>)
3172 -> zoned_time<common_type_t<_Duration, seconds>>;
3174 /// @cond undocumented
3175 template<typename _TimeZonePtrOrName>
3176 using __time_zone_representation
3177 = __conditional_t<is_convertible_v<_TimeZonePtrOrName, string_view>,
3179 remove_cvref_t<_TimeZonePtrOrName>>;
3182 template<typename _TimeZonePtrOrName>
3183 zoned_time(_TimeZonePtrOrName&&)
3184 -> zoned_time<seconds, __time_zone_representation<_TimeZonePtrOrName>>;
3186 template<typename _TimeZonePtrOrName, typename _Duration>
3187 zoned_time(_TimeZonePtrOrName&&, sys_time<_Duration>)
3188 -> zoned_time<common_type_t<_Duration, seconds>,
3189 __time_zone_representation<_TimeZonePtrOrName>>;
3191 template<typename _TimeZonePtrOrName, typename _Duration>
3192 zoned_time(_TimeZonePtrOrName&&, local_time<_Duration>,
3193 choose = choose::earliest)
3194 -> zoned_time<common_type_t<_Duration, seconds>,
3195 __time_zone_representation<_TimeZonePtrOrName>>;
3197 template<typename _Duration, typename _TimeZonePtrOrName,
3198 typename _TimeZonePtr2>
3199 zoned_time(_TimeZonePtrOrName&&, zoned_time<_Duration, _TimeZonePtr2>,
3200 choose = choose::earliest)
3201 -> zoned_time<common_type_t<_Duration, seconds>,
3202 __time_zone_representation<_TimeZonePtrOrName>>;
3204 template<typename _Dur1, typename _TZPtr1, typename _Dur2, typename _TZPtr2>
3207 operator==(const zoned_time<_Dur1, _TZPtr1>& __x,
3208 const zoned_time<_Dur2, _TZPtr2>& __y)
3210 return __x.get_time_zone() == __y.get_time_zone()
3211 && __x.get_sys_time() == __y.get_sys_time();
3214 using zoned_seconds = zoned_time<seconds>;
3215#endif // _GLIBCXX_USE_CXX11_ABI || ! _GLIBCXX_USE_DUAL_ABI
3219 inline leap_second_info
3220 __get_leap_second_info(sys_seconds __ss, bool __is_utc)
3222 if (__ss < sys_seconds{}) [[unlikely]]
3225 const seconds::rep __leaps[] {
3226 78796800, // 1 Jul 1972
3227 94694400, // 1 Jan 1973
3228 126230400, // 1 Jan 1974
3229 157766400, // 1 Jan 1975
3230 189302400, // 1 Jan 1976
3231 220924800, // 1 Jan 1977
3232 252460800, // 1 Jan 1978
3233 283996800, // 1 Jan 1979
3234 315532800, // 1 Jan 1980
3235 362793600, // 1 Jul 1981
3236 394329600, // 1 Jul 1982
3237 425865600, // 1 Jul 1983
3238 489024000, // 1 Jul 1985
3239 567993600, // 1 Jan 1988
3240 631152000, // 1 Jan 1990
3241 662688000, // 1 Jan 1991
3242 709948800, // 1 Jul 1992
3243 741484800, // 1 Jul 1993
3244 773020800, // 1 Jul 1994
3245 820454400, // 1 Jan 1996
3246 867715200, // 1 Jul 1997
3247 915148800, // 1 Jan 1999
3248 1136073600, // 1 Jan 2006
3249 1230768000, // 1 Jan 2009
3250 1341100800, // 1 Jul 2012
3251 1435708800, // 1 Jul 2015
3252 1483228800, // 1 Jan 2017
3254 // The list above is known to be valid until (at least) this date
3255 // and only contains positive leap seconds.
3256 const sys_seconds __expires(1735344000s); // 2024-12-28 00:00:00 UTC
3258#if _GLIBCXX_USE_CXX11_ABI || ! _GLIBCXX_USE_DUAL_ABI
3259 if (__ss > __expires)
3261 // Use updated leap_seconds from tzdb.
3262 size_t __n = std::size(__leaps);
3264 auto __db = get_tzdb_list().begin();
3265 auto __first = __db->leap_seconds.begin() + __n;
3266 auto __last = __db->leap_seconds.end();
3267 auto __pos = std::upper_bound(__first, __last, __ss);
3268 seconds __elapsed(__n);
3269 for (auto __i = __first; __i != __pos; ++__i)
3270 __elapsed += __i->value();
3274 // Convert utc_time to sys_time:
3276 // See if that sys_time is before (or during) previous leap sec:
3277 if (__pos != __first && __ss < __pos[-1])
3279 if ((__ss + 1s) >= __pos[-1])
3280 return {true, __elapsed};
3281 __elapsed -= __pos[-1].value();
3284 return {false, __elapsed};
3289 seconds::rep __s = __ss.time_since_epoch().count();
3290 const seconds::rep* __first = std::begin(__leaps);
3291 const seconds::rep* __last = std::end(__leaps);
3293 // Don't bother searching the list if we're after the last one.
3294 if (__s > (__last[-1] + (__last - __first) + 1))
3295 return { false, seconds(__last - __first) };
3297 auto __pos = std::upper_bound(__first, __last, __s);
3298 seconds __elapsed{__pos - __first};
3301 // Convert utc_time to sys_time:
3302 __s -= __elapsed.count();
3303 // See if that sys_time is before (or during) previous leap sec:
3304 if (__pos != __first && __s < __pos[-1])
3306 if ((__s + 1) >= __pos[-1])
3307 return {true, __elapsed};
3311 return {false, __elapsed};
3314} // namespace __detail
3316 template<typename _Duration>
3318 inline leap_second_info
3319 get_leap_second_info(const utc_time<_Duration>& __ut)
3321 auto __s = chrono::duration_cast<seconds>(__ut.time_since_epoch());
3322 return __detail::__get_leap_second_info(sys_seconds(__s), true);
3325 template<typename _Duration>
3327 inline utc_time<common_type_t<_Duration, seconds>>
3328 utc_clock::from_sys(const sys_time<_Duration>& __t)
3330 using _CDur = common_type_t<_Duration, seconds>;
3331 auto __s = chrono::time_point_cast<seconds>(__t);
3332 const auto __li = __detail::__get_leap_second_info(__s, false);
3333 return utc_time<_CDur>{__t.time_since_epoch()} + __li.elapsed;
3338 } // namespace chrono
3340#if __cplusplus >= 202002L
3341 inline namespace literals
3343 inline namespace chrono_literals
3345 /// @addtogroup chrono
3347#pragma GCC diagnostic push
3348#pragma GCC diagnostic ignored "-Wliteral-suffix"
3349 /// Literal suffix for creating chrono::day objects.
3351 constexpr chrono::day
3352 operator""d(unsigned long long __d) noexcept
3353 { return chrono::day{static_cast<unsigned>(__d)}; }
3355 /// Literal suffix for creating chrono::year objects.
3357 constexpr chrono::year
3358 operator""y(unsigned long long __y) noexcept
3359 { return chrono::year{static_cast<int>(__y)}; }
3360#pragma GCC diagnostic pop
3362 } // inline namespace chrono_literals
3363 } // inline namespace literals
3366_GLIBCXX_END_NAMESPACE_VERSION
3369#if __cplusplus >= 202002L
3370# include <bits/chrono_io.h>
3375#endif //_GLIBCXX_CHRONO