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 // Compute the remainder of the Euclidean division of __n divided by __d.
507 // Euclidean division truncates toward negative infinity and always
508 // produces a remainder in the range of [0,__d-1] (whereas standard
509 // division truncates toward zero and yields a nonpositive remainder
510 // for negative __n).
512 __modulo(long long __n, unsigned __d)
517 return (__d + (__n % __d)) % __d;
520 inline constexpr unsigned __days_per_month[12]
521 = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
535 day(unsigned __d) noexcept
540 operator++() noexcept
547 operator++(int) noexcept
555 operator--() noexcept
562 operator--(int) noexcept
570 operator+=(const days& __d) noexcept
577 operator-=(const days& __d) noexcept
584 operator unsigned() const noexcept
589 { return 1 <= _M_d && _M_d <= 31; }
591 friend constexpr bool
592 operator==(const day& __x, const day& __y) noexcept
593 { return unsigned{__x} == unsigned{__y}; }
595 friend constexpr strong_ordering
596 operator<=>(const day& __x, const day& __y) noexcept
597 { return unsigned{__x} <=> unsigned{__y}; }
600 operator+(const day& __x, const days& __y) noexcept
601 { return day(unsigned{__x} + __y.count()); }
604 operator+(const days& __x, const day& __y) noexcept
605 { return __y + __x; }
608 operator-(const day& __x, const days& __y) noexcept
609 { return __x + -__y; }
611 friend constexpr days
612 operator-(const day& __x, const day& __y) noexcept
613 { return days{int(unsigned{__x}) - int(unsigned{__y})}; }
615 friend constexpr month_day
616 operator/(const month& __m, const day& __d) noexcept;
618 friend constexpr month_day
619 operator/(int __m, const day& __d) noexcept;
621 friend constexpr month_day
622 operator/(const day& __d, const month& __m) noexcept;
624 friend constexpr month_day
625 operator/(const day& __d, int __m) noexcept;
627 friend constexpr year_month_day
628 operator/(const year_month& __ym, const day& __d) noexcept;
642 month(unsigned __m) noexcept
647 operator++() noexcept
654 operator++(int) noexcept
662 operator--() noexcept
669 operator--(int) noexcept
677 operator+=(const months& __m) noexcept
684 operator-=(const months& __m) noexcept
691 operator unsigned() const noexcept
696 { return 1 <= _M_m && _M_m <= 12; }
698 friend constexpr bool
699 operator==(const month& __x, const month& __y) noexcept
700 { return unsigned{__x} == unsigned{__y}; }
702 friend constexpr strong_ordering
703 operator<=>(const month& __x, const month& __y) noexcept
704 { return unsigned{__x} <=> unsigned{__y}; }
706 friend constexpr month
707 operator+(const month& __x, const months& __y) noexcept
709 auto __n = static_cast<long long>(unsigned{__x}) + (__y.count() - 1);
710 return month{__detail::__modulo(__n, 12) + 1};
713 friend constexpr month
714 operator+(const months& __x, const month& __y) noexcept
715 { return __y + __x; }
717 friend constexpr month
718 operator-(const month& __x, const months& __y) noexcept
719 { return __x + -__y; }
721 friend constexpr months
722 operator-(const month& __x, const month& __y) noexcept
724 const auto __dm = int(unsigned(__x)) - int(unsigned(__y));
725 return months{__dm < 0 ? 12 + __dm : __dm};
728 friend constexpr year_month
729 operator/(const year& __y, const month& __m) noexcept;
731 friend constexpr month_day
732 operator/(const month& __m, int __d) noexcept;
734 friend constexpr month_day_last
735 operator/(const month& __m, last_spec) noexcept;
737 friend constexpr month_day_last
738 operator/(last_spec, const month& __m) noexcept;
740 friend constexpr month_weekday
741 operator/(const month& __m, const weekday_indexed& __wdi) noexcept;
743 friend constexpr month_weekday
744 operator/(const weekday_indexed& __wdi, const month& __m) noexcept;
746 friend constexpr month_weekday_last
747 operator/(const month& __m, const weekday_last& __wdl) noexcept;
749 friend constexpr month_weekday_last
750 operator/(const weekday_last& __wdl, const month& __m) noexcept;
753 inline constexpr month January{1};
754 inline constexpr month February{2};
755 inline constexpr month March{3};
756 inline constexpr month April{4};
757 inline constexpr month May{5};
758 inline constexpr month June{6};
759 inline constexpr month July{7};
760 inline constexpr month August{8};
761 inline constexpr month September{9};
762 inline constexpr month October{10};
763 inline constexpr month November{11};
764 inline constexpr month December{12};
777 year(int __y) noexcept
778 : _M_y{static_cast<short>(__y)}
781 static constexpr year
783 { return year{-32767}; }
785 static constexpr year
787 { return year{32767}; }
790 operator++() noexcept
797 operator++(int) noexcept
805 operator--() noexcept
812 operator--(int) noexcept
820 operator+=(const years& __y) noexcept
827 operator-=(const years& __y) noexcept
834 operator+() const noexcept
838 operator-() const noexcept
839 { return year{-_M_y}; }
842 is_leap() const noexcept
844 // Testing divisibility by 100 first gives better performance, that is,
845 // return (_M_y % 100 != 0 || _M_y % 400 == 0) && _M_y % 4 == 0;
847 // It gets even faster if _M_y is in [-536870800, 536870999]
848 // (which is the case here) and _M_y % 100 is replaced by
849 // __is_multiple_of_100 below.
852 // [1] https://github.com/cassioneri/calendar
853 // [2] https://accu.org/journals/overload/28/155/overload155.pdf#page=16
855 // Furthermore, if y%100 == 0, then y%400==0 is equivalent to y%16==0,
856 // so we can simplify it to (!mult_100 && y % 4 == 0) || y % 16 == 0,
857 // which is equivalent to (y & (mult_100 ? 15 : 3)) == 0.
858 // See https://gcc.gnu.org/pipermail/libstdc++/2021-June/052815.html
860 constexpr uint32_t __multiplier = 42949673;
861 constexpr uint32_t __bound = 42949669;
862 constexpr uint32_t __max_dividend = 1073741799;
863 constexpr uint32_t __offset = __max_dividend / 2 / 100 * 100;
864 const bool __is_multiple_of_100
865 = __multiplier * (_M_y + __offset) < __bound;
866 return (_M_y & (__is_multiple_of_100 ? 15 : 3)) == 0;
870 operator int() const noexcept
875 { return min()._M_y <= _M_y && _M_y <= max()._M_y; }
877 friend constexpr bool
878 operator==(const year& __x, const year& __y) noexcept
879 { return int{__x} == int{__y}; }
881 friend constexpr strong_ordering
882 operator<=>(const year& __x, const year& __y) noexcept
883 { return int{__x} <=> int{__y}; }
885 friend constexpr year
886 operator+(const year& __x, const years& __y) noexcept
887 { return year{int{__x} + static_cast<int>(__y.count())}; }
889 friend constexpr year
890 operator+(const years& __x, const year& __y) noexcept
891 { return __y + __x; }
893 friend constexpr year
894 operator-(const year& __x, const years& __y) noexcept
895 { return __x + -__y; }
897 friend constexpr years
898 operator-(const year& __x, const year& __y) noexcept
899 { return years{int{__x} - int{__y}}; }
901 friend constexpr year_month
902 operator/(const year& __y, int __m) noexcept;
904 friend constexpr year_month_day
905 operator/(const year& __y, const month_day& __md) noexcept;
907 friend constexpr year_month_day
908 operator/(const month_day& __md, const year& __y) noexcept;
910 friend constexpr year_month_day_last
911 operator/(const year& __y, const month_day_last& __mdl) noexcept;
913 friend constexpr year_month_day_last
914 operator/(const month_day_last& __mdl, const year& __y) noexcept;
916 friend constexpr year_month_weekday
917 operator/(const year& __y, const month_weekday& __mwd) noexcept;
919 friend constexpr year_month_weekday
920 operator/(const month_weekday& __mwd, const year& __y) noexcept;
922 friend constexpr year_month_weekday_last
923 operator/(const year& __y, const month_weekday_last& __mwdl) noexcept;
925 friend constexpr year_month_weekday_last
926 operator/(const month_weekday_last& __mwdl, const year& __y) noexcept;
936 static constexpr weekday
937 _S_from_days(const days& __d)
939 auto __n = __d.count();
940 return weekday(__n >= -4 ? (__n + 4) % 7 : (__n + 5) % 7 + 6);
947 weekday(unsigned __wd) noexcept
948 : _M_wd(__wd == 7 ? 0 : __wd) // __wd % 7 ?
952 weekday(const sys_days& __dp) noexcept
953 : weekday{_S_from_days(__dp.time_since_epoch())}
957 weekday(const local_days& __dp) noexcept
958 : weekday{sys_days{__dp.time_since_epoch()}}
962 operator++() noexcept
969 operator++(int) noexcept
977 operator--() noexcept
984 operator--(int) noexcept
992 operator+=(const days& __d) noexcept
999 operator-=(const days& __d) noexcept
1001 *this = *this - __d;
1006 c_encoding() const noexcept
1010 iso_encoding() const noexcept
1011 { return _M_wd == 0u ? 7u : _M_wd; }
1015 { return _M_wd <= 6; }
1017 constexpr weekday_indexed
1018 operator[](unsigned __index) const noexcept;
1020 constexpr weekday_last
1021 operator[](last_spec) const noexcept;
1023 friend constexpr bool
1024 operator==(const weekday& __x, const weekday& __y) noexcept
1025 { return __x._M_wd == __y._M_wd; }
1027 friend constexpr weekday
1028 operator+(const weekday& __x, const days& __y) noexcept
1030 auto __n = static_cast<long long>(__x._M_wd) + __y.count();
1031 return weekday{__detail::__modulo(__n, 7)};
1034 friend constexpr weekday
1035 operator+(const days& __x, const weekday& __y) noexcept
1036 { return __y + __x; }
1038 friend constexpr weekday
1039 operator-(const weekday& __x, const days& __y) noexcept
1040 { return __x + -__y; }
1042 friend constexpr days
1043 operator-(const weekday& __x, const weekday& __y) noexcept
1045 auto __n = static_cast<long long>(__x._M_wd) - __y._M_wd;
1046 return days{__detail::__modulo(__n, 7)};
1050 inline constexpr weekday Sunday{0};
1051 inline constexpr weekday Monday{1};
1052 inline constexpr weekday Tuesday{2};
1053 inline constexpr weekday Wednesday{3};
1054 inline constexpr weekday Thursday{4};
1055 inline constexpr weekday Friday{5};
1056 inline constexpr weekday Saturday{6};
1060 class weekday_indexed
1063 chrono::weekday _M_wd;
1064 unsigned char _M_index;
1067 weekday_indexed() = default;
1070 weekday_indexed(const chrono::weekday& __wd, unsigned __index) noexcept
1071 : _M_wd(__wd), _M_index(__index)
1074 constexpr chrono::weekday
1075 weekday() const noexcept
1079 index() const noexcept
1080 { return _M_index; };
1084 { return _M_wd.ok() && 1 <= _M_index && _M_index <= 5; }
1086 friend constexpr bool
1087 operator==(const weekday_indexed& __x, const weekday_indexed& __y) noexcept
1088 { return __x.weekday() == __y.weekday() && __x.index() == __y.index(); }
1090 friend constexpr month_weekday
1091 operator/(const month& __m, const weekday_indexed& __wdi) noexcept;
1093 friend constexpr month_weekday
1094 operator/(int __m, const weekday_indexed& __wdi) noexcept;
1096 friend constexpr month_weekday
1097 operator/(const weekday_indexed& __wdi, const month& __m) noexcept;
1099 friend constexpr month_weekday
1100 operator/(const weekday_indexed& __wdi, int __m) noexcept;
1102 friend constexpr year_month_weekday
1103 operator/(const year_month& __ym, const weekday_indexed& __wdi) noexcept;
1106 constexpr weekday_indexed
1107 weekday::operator[](unsigned __index) const noexcept
1108 { return {*this, __index}; }
1115 chrono::weekday _M_wd;
1119 weekday_last(const chrono::weekday& __wd) noexcept
1123 constexpr chrono::weekday
1124 weekday() const noexcept
1129 { return _M_wd.ok(); }
1131 friend constexpr bool
1132 operator==(const weekday_last& __x, const weekday_last& __y) noexcept
1133 { return __x.weekday() == __y.weekday(); }
1135 friend constexpr month_weekday_last
1136 operator/(int __m, const weekday_last& __wdl) noexcept;
1138 friend constexpr month_weekday_last
1139 operator/(const weekday_last& __wdl, int __m) noexcept;
1141 friend constexpr year_month_weekday_last
1142 operator/(const year_month& __ym, const weekday_last& __wdl) noexcept;
1145 constexpr weekday_last
1146 weekday::operator[](last_spec) const noexcept
1147 { return weekday_last{*this}; }
1158 month_day() = default;
1161 month_day(const chrono::month& __m, const chrono::day& __d) noexcept
1162 : _M_m{__m}, _M_d{__d}
1165 constexpr chrono::month
1166 month() const noexcept
1169 constexpr chrono::day
1170 day() const noexcept
1177 && 1u <= unsigned(_M_d)
1178 && unsigned(_M_d) <= __detail::__days_per_month[unsigned(_M_m) - 1];
1181 friend constexpr bool
1182 operator==(const month_day& __x, const month_day& __y) noexcept
1183 { return __x.month() == __y.month() && __x.day() == __y.day(); }
1185 friend constexpr strong_ordering
1186 operator<=>(const month_day& __x, const month_day& __y) noexcept
1189 friend constexpr month_day
1190 operator/(const chrono::month& __m, const chrono::day& __d) noexcept
1191 { return {__m, __d}; }
1193 friend constexpr month_day
1194 operator/(const chrono::month& __m, int __d) noexcept
1195 { return {__m, chrono::day(unsigned(__d))}; }
1197 friend constexpr month_day
1198 operator/(int __m, const chrono::day& __d) noexcept
1199 { return {chrono::month(unsigned(__m)), __d}; }
1201 friend constexpr month_day
1202 operator/(const chrono::day& __d, const chrono::month& __m) noexcept
1203 { return {__m, __d}; }
1205 friend constexpr month_day
1206 operator/(const chrono::day& __d, int __m) noexcept
1207 { return {chrono::month(unsigned(__m)), __d}; }
1209 friend constexpr year_month_day
1210 operator/(int __y, const month_day& __md) noexcept;
1212 friend constexpr year_month_day
1213 operator/(const month_day& __md, int __y) noexcept;
1218 class month_day_last
1225 month_day_last(const chrono::month& __m) noexcept
1229 constexpr chrono::month
1230 month() const noexcept
1235 { return _M_m.ok(); }
1237 friend constexpr bool
1238 operator==(const month_day_last& __x, const month_day_last& __y) noexcept
1239 { return __x.month() == __y.month(); }
1241 friend constexpr strong_ordering
1242 operator<=>(const month_day_last& __x, const month_day_last& __y) noexcept
1245 friend constexpr month_day_last
1246 operator/(const chrono::month& __m, last_spec) noexcept
1247 { return month_day_last{__m}; }
1249 friend constexpr month_day_last
1250 operator/(int __m, last_spec) noexcept
1251 { return chrono::month(unsigned(__m)) / last; }
1253 friend constexpr month_day_last
1254 operator/(last_spec, const chrono::month& __m) noexcept
1255 { return __m / last; }
1257 friend constexpr month_day_last
1258 operator/(last_spec, int __m) noexcept
1259 { return __m / last; }
1261 friend constexpr year_month_day_last
1262 operator/(int __y, const month_day_last& __mdl) noexcept;
1264 friend constexpr year_month_day_last
1265 operator/(const month_day_last& __mdl, int __y) noexcept;
1274 chrono::weekday_indexed _M_wdi;
1278 month_weekday(const chrono::month& __m,
1279 const chrono::weekday_indexed& __wdi) noexcept
1280 : _M_m{__m}, _M_wdi{__wdi}
1283 constexpr chrono::month
1284 month() const noexcept
1287 constexpr chrono::weekday_indexed
1288 weekday_indexed() const noexcept
1293 { return _M_m.ok() && _M_wdi.ok(); }
1295 friend constexpr bool
1296 operator==(const month_weekday& __x, const month_weekday& __y) noexcept
1298 return __x.month() == __y.month()
1299 && __x.weekday_indexed() == __y.weekday_indexed();
1302 friend constexpr month_weekday
1303 operator/(const chrono::month& __m,
1304 const chrono::weekday_indexed& __wdi) noexcept
1305 { return {__m, __wdi}; }
1307 friend constexpr month_weekday
1308 operator/(int __m, const chrono::weekday_indexed& __wdi) noexcept
1309 { return chrono::month(unsigned(__m)) / __wdi; }
1311 friend constexpr month_weekday
1312 operator/(const chrono::weekday_indexed& __wdi,
1313 const chrono::month& __m) noexcept
1314 { return __m / __wdi; }
1316 friend constexpr month_weekday
1317 operator/(const chrono::weekday_indexed& __wdi, int __m) noexcept
1318 { return __m / __wdi; }
1320 friend constexpr year_month_weekday
1321 operator/(int __y, const month_weekday& __mwd) noexcept;
1323 friend constexpr year_month_weekday
1324 operator/(const month_weekday& __mwd, int __y) noexcept;
1327 // MONTH_WEEKDAY_LAST
1329 class month_weekday_last
1333 chrono::weekday_last _M_wdl;
1337 month_weekday_last(const chrono::month& __m,
1338 const chrono::weekday_last& __wdl) noexcept
1339 :_M_m{__m}, _M_wdl{__wdl}
1342 constexpr chrono::month
1343 month() const noexcept
1346 constexpr chrono::weekday_last
1347 weekday_last() const noexcept
1352 { return _M_m.ok() && _M_wdl.ok(); }
1354 friend constexpr bool
1355 operator==(const month_weekday_last& __x,
1356 const month_weekday_last& __y) noexcept
1358 return __x.month() == __y.month()
1359 && __x.weekday_last() == __y.weekday_last();
1362 friend constexpr month_weekday_last
1363 operator/(const chrono::month& __m,
1364 const chrono::weekday_last& __wdl) noexcept
1365 { return {__m, __wdl}; }
1367 friend constexpr month_weekday_last
1368 operator/(int __m, const chrono::weekday_last& __wdl) noexcept
1369 { return chrono::month(unsigned(__m)) / __wdl; }
1371 friend constexpr month_weekday_last
1372 operator/(const chrono::weekday_last& __wdl,
1373 const chrono::month& __m) noexcept
1374 { return __m / __wdl; }
1376 friend constexpr month_weekday_last
1377 operator/(const chrono::weekday_last& __wdl, int __m) noexcept
1378 { return chrono::month(unsigned(__m)) / __wdl; }
1380 friend constexpr year_month_weekday_last
1381 operator/(int __y, const month_weekday_last& __mwdl) noexcept;
1383 friend constexpr year_month_weekday_last
1384 operator/(const month_weekday_last& __mwdl, int __y) noexcept;
1391 // [time.cal.ym], [time.cal.ymd], etc constrain the 'months'-based
1392 // addition/subtraction operator overloads like so:
1394 // Constraints: if the argument supplied by the caller for the months
1395 // parameter is convertible to years, its implicit conversion sequence
1396 // to years is worse than its implicit conversion sequence to months.
1398 // We realize this constraint by templatizing the 'months'-based
1399 // overloads (using a dummy defaulted template parameter), so that
1400 // overload resolution doesn't select the 'months'-based overload unless
1401 // the implicit conversion sequence to 'months' is better than that to
1403 using __months_years_conversion_disambiguator = void;
1413 year_month() = default;
1416 year_month(const chrono::year& __y, const chrono::month& __m) noexcept
1417 : _M_y{__y}, _M_m{__m}
1420 constexpr chrono::year
1421 year() const noexcept
1424 constexpr chrono::month
1425 month() const noexcept
1428 template<typename = __detail::__months_years_conversion_disambiguator>
1429 constexpr year_month&
1430 operator+=(const months& __dm) noexcept
1432 *this = *this + __dm;
1436 template<typename = __detail::__months_years_conversion_disambiguator>
1437 constexpr year_month&
1438 operator-=(const months& __dm) noexcept
1440 *this = *this - __dm;
1444 constexpr year_month&
1445 operator+=(const years& __dy) noexcept
1447 *this = *this + __dy;
1451 constexpr year_month&
1452 operator-=(const years& __dy) noexcept
1454 *this = *this - __dy;
1460 { return _M_y.ok() && _M_m.ok(); }
1462 friend constexpr bool
1463 operator==(const year_month& __x, const year_month& __y) noexcept
1464 { return __x.year() == __y.year() && __x.month() == __y.month(); }
1466 friend constexpr strong_ordering
1467 operator<=>(const year_month& __x, const year_month& __y) noexcept
1470 template<typename = __detail::__months_years_conversion_disambiguator>
1471 friend constexpr year_month
1472 operator+(const year_month& __ym, const months& __dm) noexcept
1475 auto __m = __ym.month() + __dm;
1476 auto __i = int(unsigned(__ym.month())) - 1 + __dm.count();
1478 ? __ym.year() + years{(__i - 11) / 12}
1479 : __ym.year() + years{__i / 12});
1483 template<typename = __detail::__months_years_conversion_disambiguator>
1484 friend constexpr year_month
1485 operator+(const months& __dm, const year_month& __ym) noexcept
1486 { return __ym + __dm; }
1488 template<typename = __detail::__months_years_conversion_disambiguator>
1489 friend constexpr year_month
1490 operator-(const year_month& __ym, const months& __dm) noexcept
1491 { return __ym + -__dm; }
1493 friend constexpr months
1494 operator-(const year_month& __x, const year_month& __y) noexcept
1496 return (__x.year() - __y.year()
1497 + months{static_cast<int>(unsigned{__x.month()})
1498 - static_cast<int>(unsigned{__y.month()})});
1501 friend constexpr year_month
1502 operator+(const year_month& __ym, const years& __dy) noexcept
1503 { return (__ym.year() + __dy) / __ym.month(); }
1505 friend constexpr year_month
1506 operator+(const years& __dy, const year_month& __ym) noexcept
1507 { return __ym + __dy; }
1509 friend constexpr year_month
1510 operator-(const year_month& __ym, const years& __dy) noexcept
1511 { return __ym + -__dy; }
1513 friend constexpr year_month
1514 operator/(const chrono::year& __y, const chrono::month& __m) noexcept
1515 { return {__y, __m}; }
1517 friend constexpr year_month
1518 operator/(const chrono::year& __y, int __m) noexcept
1519 { return {__y, chrono::month(unsigned(__m))}; }
1521 friend constexpr year_month_day
1522 operator/(const year_month& __ym, int __d) noexcept;
1524 friend constexpr year_month_day_last
1525 operator/(const year_month& __ym, last_spec) noexcept;
1530 class year_month_day
1537 static constexpr year_month_day _S_from_days(const days& __dp) noexcept;
1539 constexpr days _M_days_since_epoch() const noexcept;
1542 year_month_day() = default;
1545 year_month_day(const chrono::year& __y, const chrono::month& __m,
1546 const chrono::day& __d) noexcept
1547 : _M_y{__y}, _M_m{__m}, _M_d{__d}
1551 year_month_day(const year_month_day_last& __ymdl) noexcept;
1554 year_month_day(const sys_days& __dp) noexcept
1555 : year_month_day(_S_from_days(__dp.time_since_epoch()))
1559 year_month_day(const local_days& __dp) noexcept
1560 : year_month_day(sys_days{__dp.time_since_epoch()})
1563 template<typename = __detail::__months_years_conversion_disambiguator>
1564 constexpr year_month_day&
1565 operator+=(const months& __m) noexcept
1567 *this = *this + __m;
1571 template<typename = __detail::__months_years_conversion_disambiguator>
1572 constexpr year_month_day&
1573 operator-=(const months& __m) noexcept
1575 *this = *this - __m;
1579 constexpr year_month_day&
1580 operator+=(const years& __y) noexcept
1582 *this = *this + __y;
1586 constexpr year_month_day&
1587 operator-=(const years& __y) noexcept
1589 *this = *this - __y;
1593 constexpr chrono::year
1594 year() const noexcept
1597 constexpr chrono::month
1598 month() const noexcept
1601 constexpr chrono::day
1602 day() const noexcept
1606 operator sys_days() const noexcept
1607 { return sys_days{_M_days_since_epoch()}; }
1610 operator local_days() const noexcept
1611 { return local_days{sys_days{*this}.time_since_epoch()}; }
1613 constexpr bool ok() const noexcept;
1615 friend constexpr bool
1616 operator==(const year_month_day& __x, const year_month_day& __y) noexcept
1618 return __x.year() == __y.year()
1619 && __x.month() == __y.month()
1620 && __x.day() == __y.day();
1623 friend constexpr strong_ordering
1624 operator<=>(const year_month_day& __x, const year_month_day& __y) noexcept
1627 template<typename = __detail::__months_years_conversion_disambiguator>
1628 friend constexpr year_month_day
1629 operator+(const year_month_day& __ymd, const months& __dm) noexcept
1630 { return (__ymd.year() / __ymd.month() + __dm) / __ymd.day(); }
1632 template<typename = __detail::__months_years_conversion_disambiguator>
1633 friend constexpr year_month_day
1634 operator+(const months& __dm, const year_month_day& __ymd) noexcept
1635 { return __ymd + __dm; }
1637 friend constexpr year_month_day
1638 operator+(const year_month_day& __ymd, const years& __dy) noexcept
1639 { return (__ymd.year() + __dy) / __ymd.month() / __ymd.day(); }
1641 friend constexpr year_month_day
1642 operator+(const years& __dy, const year_month_day& __ymd) noexcept
1643 { return __ymd + __dy; }
1645 template<typename = __detail::__months_years_conversion_disambiguator>
1646 friend constexpr year_month_day
1647 operator-(const year_month_day& __ymd, const months& __dm) noexcept
1648 { return __ymd + -__dm; }
1650 friend constexpr year_month_day
1651 operator-(const year_month_day& __ymd, const years& __dy) noexcept
1652 { return __ymd + -__dy; }
1654 friend constexpr year_month_day
1655 operator/(const year_month& __ym, const chrono::day& __d) noexcept
1656 { return {__ym.year(), __ym.month(), __d}; }
1658 friend constexpr year_month_day
1659 operator/(const year_month& __ym, int __d) noexcept
1660 { return __ym / chrono::day{unsigned(__d)}; }
1662 friend constexpr year_month_day
1663 operator/(const chrono::year& __y, const month_day& __md) noexcept
1664 { return __y / __md.month() / __md.day(); }
1666 friend constexpr year_month_day
1667 operator/(int __y, const month_day& __md) noexcept
1668 { return chrono::year{__y} / __md; }
1670 friend constexpr year_month_day
1671 operator/(const month_day& __md, const chrono::year& __y) noexcept
1672 { return __y / __md; }
1674 friend constexpr year_month_day
1675 operator/(const month_day& __md, int __y) noexcept
1676 { return chrono::year(__y) / __md; }
1679 // Construct from days since 1970/01/01.
1680 // Proposition 6.3 of Neri and Schneider,
1681 // "Euclidean Affine Functions and Applications to Calendar Algorithms".
1682 // https://arxiv.org/abs/2102.06959
1683 constexpr year_month_day
1684 year_month_day::_S_from_days(const days& __dp) noexcept
1686 constexpr auto __z2 = static_cast<uint32_t>(-1468000);
1687 constexpr auto __r2_e3 = static_cast<uint32_t>(536895458);
1689 const auto __r0 = static_cast<uint32_t>(__dp.count()) + __r2_e3;
1691 const auto __n1 = 4 * __r0 + 3;
1692 const auto __q1 = __n1 / 146097;
1693 const auto __r1 = __n1 % 146097 / 4;
1695 constexpr auto __p32 = static_cast<uint64_t>(1) << 32;
1696 const auto __n2 = 4 * __r1 + 3;
1697 const auto __u2 = static_cast<uint64_t>(2939745) * __n2;
1698 const auto __q2 = static_cast<uint32_t>(__u2 / __p32);
1699 const auto __r2 = static_cast<uint32_t>(__u2 % __p32) / 2939745 / 4;
1701 constexpr auto __p16 = static_cast<uint32_t>(1) << 16;
1702 const auto __n3 = 2141 * __r2 + 197913;
1703 const auto __q3 = __n3 / __p16;
1704 const auto __r3 = __n3 % __p16 / 2141;
1706 const auto __y0 = 100 * __q1 + __q2;
1707 const auto __m0 = __q3;
1708 const auto __d0 = __r3;
1710 const auto __j = __r2 >= 306;
1711 const auto __y1 = __y0 + __j;
1712 const auto __m1 = __j ? __m0 - 12 : __m0;
1713 const auto __d1 = __d0 + 1;
1715 return year_month_day{chrono::year{static_cast<int>(__y1 + __z2)},
1716 chrono::month{__m1}, chrono::day{__d1}};
1719 // Days since 1970/01/01.
1720 // Proposition 6.2 of Neri and Schneider,
1721 // "Euclidean Affine Functions and Applications to Calendar Algorithms".
1722 // https://arxiv.org/abs/2102.06959
1724 year_month_day::_M_days_since_epoch() const noexcept
1726 auto constexpr __z2 = static_cast<uint32_t>(-1468000);
1727 auto constexpr __r2_e3 = static_cast<uint32_t>(536895458);
1729 const auto __y1 = static_cast<uint32_t>(static_cast<int>(_M_y)) - __z2;
1730 const auto __m1 = static_cast<uint32_t>(static_cast<unsigned>(_M_m));
1731 const auto __d1 = static_cast<uint32_t>(static_cast<unsigned>(_M_d));
1733 const auto __j = static_cast<uint32_t>(__m1 < 3);
1734 const auto __y0 = __y1 - __j;
1735 const auto __m0 = __j ? __m1 + 12 : __m1;
1736 const auto __d0 = __d1 - 1;
1738 const auto __q1 = __y0 / 100;
1739 const auto __yc = 1461 * __y0 / 4 - __q1 + __q1 / 4;
1740 const auto __mc = (979 *__m0 - 2919) / 32;
1741 const auto __dc = __d0;
1743 return days{static_cast<int32_t>(__yc + __mc + __dc - __r2_e3)};
1746 // YEAR_MONTH_DAY_LAST
1748 class year_month_day_last
1752 chrono::month_day_last _M_mdl;
1756 year_month_day_last(const chrono::year& __y,
1757 const chrono::month_day_last& __mdl) noexcept
1758 : _M_y{__y}, _M_mdl{__mdl}
1761 template<typename = __detail::__months_years_conversion_disambiguator>
1762 constexpr year_month_day_last&
1763 operator+=(const months& __m) noexcept
1765 *this = *this + __m;
1769 template<typename = __detail::__months_years_conversion_disambiguator>
1770 constexpr year_month_day_last&
1771 operator-=(const months& __m) noexcept
1773 *this = *this - __m;
1777 constexpr year_month_day_last&
1778 operator+=(const years& __y) noexcept
1780 *this = *this + __y;
1784 constexpr year_month_day_last&
1785 operator-=(const years& __y) noexcept
1787 *this = *this - __y;
1791 constexpr chrono::year
1792 year() const noexcept
1795 constexpr chrono::month
1796 month() const noexcept
1797 { return _M_mdl.month(); }
1799 constexpr chrono::month_day_last
1800 month_day_last() const noexcept
1803 // Return A day representing the last day of this year, month pair.
1804 constexpr chrono::day
1805 day() const noexcept
1807 const auto __m = static_cast<unsigned>(month());
1809 // Excluding February, the last day of month __m is either 30 or 31 or,
1810 // in another words, it is 30 + b = 30 | b, where b is in {0, 1}.
1812 // If __m in {1, 3, 4, 5, 6, 7}, then b is 1 if, and only if __m is odd.
1813 // Hence, b = __m & 1 = (__m ^ 0) & 1.
1815 // If __m in {8, 9, 10, 11, 12}, then b is 1 if, and only if __m is even.
1816 // Hence, b = (__m ^ 1) & 1.
1818 // Therefore, b = (__m ^ c) & 1, where c = 0, if __m < 8, or c = 1 if
1819 // __m >= 8, that is, c = __m >> 3.
1821 // The above mathematically justifies this implementation whose
1822 // performance does not depend on look-up tables being on the L1 cache.
1823 return chrono::day{__m != 2 ? ((__m ^ (__m >> 3)) & 1) | 30
1824 : _M_y.is_leap() ? 29 : 28};
1828 operator sys_days() const noexcept
1829 { return sys_days{year() / month() / day()}; }
1832 operator local_days() const noexcept
1833 { return local_days{sys_days{*this}.time_since_epoch()}; }
1837 { return _M_y.ok() && _M_mdl.ok(); }
1839 friend constexpr bool
1840 operator==(const year_month_day_last& __x,
1841 const year_month_day_last& __y) noexcept
1843 return __x.year() == __y.year()
1844 && __x.month_day_last() == __y.month_day_last();
1847 friend constexpr strong_ordering
1848 operator<=>(const year_month_day_last& __x,
1849 const year_month_day_last& __y) noexcept
1852 template<typename = __detail::__months_years_conversion_disambiguator>
1853 friend constexpr year_month_day_last
1854 operator+(const year_month_day_last& __ymdl,
1855 const months& __dm) noexcept
1856 { return (__ymdl.year() / __ymdl.month() + __dm) / last; }
1858 template<typename = __detail::__months_years_conversion_disambiguator>
1859 friend constexpr year_month_day_last
1860 operator+(const months& __dm,
1861 const year_month_day_last& __ymdl) noexcept
1862 { return __ymdl + __dm; }
1864 template<typename = __detail::__months_years_conversion_disambiguator>
1865 friend constexpr year_month_day_last
1866 operator-(const year_month_day_last& __ymdl,
1867 const months& __dm) noexcept
1868 { return __ymdl + -__dm; }
1870 friend constexpr year_month_day_last
1871 operator+(const year_month_day_last& __ymdl,
1872 const years& __dy) noexcept
1873 { return {__ymdl.year() + __dy, __ymdl.month_day_last()}; }
1875 friend constexpr year_month_day_last
1876 operator+(const years& __dy,
1877 const year_month_day_last& __ymdl) noexcept
1878 { return __ymdl + __dy; }
1880 friend constexpr year_month_day_last
1881 operator-(const year_month_day_last& __ymdl,
1882 const years& __dy) noexcept
1883 { return __ymdl + -__dy; }
1885 friend constexpr year_month_day_last
1886 operator/(const year_month& __ym, last_spec) noexcept
1887 { return {__ym.year(), chrono::month_day_last{__ym.month()}}; }
1889 friend constexpr year_month_day_last
1890 operator/(const chrono::year& __y,
1891 const chrono::month_day_last& __mdl) noexcept
1892 { return {__y, __mdl}; }
1894 friend constexpr year_month_day_last
1895 operator/(int __y, const chrono::month_day_last& __mdl) noexcept
1896 { return chrono::year(__y) / __mdl; }
1898 friend constexpr year_month_day_last
1899 operator/(const chrono::month_day_last& __mdl,
1900 const chrono::year& __y) noexcept
1901 { return __y / __mdl; }
1903 friend constexpr year_month_day_last
1904 operator/(const chrono::month_day_last& __mdl, int __y) noexcept
1905 { return chrono::year(__y) / __mdl; }
1908 // year_month_day ctor from year_month_day_last
1910 year_month_day::year_month_day(const year_month_day_last& __ymdl) noexcept
1911 : _M_y{__ymdl.year()}, _M_m{__ymdl.month()}, _M_d{__ymdl.day()}
1915 year_month_day::ok() const noexcept
1917 if (!_M_y.ok() || !_M_m.ok())
1919 return chrono::day{1} <= _M_d && _M_d <= (_M_y / _M_m / last).day();
1922 // YEAR_MONTH_WEEKDAY
1924 class year_month_weekday
1929 chrono::weekday_indexed _M_wdi;
1931 static constexpr year_month_weekday
1932 _S_from_sys_days(const sys_days& __dp)
1934 year_month_day __ymd{__dp};
1935 chrono::weekday __wd{__dp};
1936 auto __index = __wd[(unsigned{__ymd.day()} - 1) / 7 + 1];
1937 return {__ymd.year(), __ymd.month(), __index};
1941 year_month_weekday() = default;
1944 year_month_weekday(const chrono::year& __y, const chrono::month& __m,
1945 const chrono::weekday_indexed& __wdi) noexcept
1946 : _M_y{__y}, _M_m{__m}, _M_wdi{__wdi}
1950 year_month_weekday(const sys_days& __dp) noexcept
1951 : year_month_weekday{_S_from_sys_days(__dp)}
1955 year_month_weekday(const local_days& __dp) noexcept
1956 : year_month_weekday{sys_days{__dp.time_since_epoch()}}
1959 template<typename = __detail::__months_years_conversion_disambiguator>
1960 constexpr year_month_weekday&
1961 operator+=(const months& __m) noexcept
1963 *this = *this + __m;
1967 template<typename = __detail::__months_years_conversion_disambiguator>
1968 constexpr year_month_weekday&
1969 operator-=(const months& __m) noexcept
1971 *this = *this - __m;
1975 constexpr year_month_weekday&
1976 operator+=(const years& __y) noexcept
1978 *this = *this + __y;
1982 constexpr year_month_weekday&
1983 operator-=(const years& __y) noexcept
1985 *this = *this - __y;
1989 constexpr chrono::year
1990 year() const noexcept
1993 constexpr chrono::month
1994 month() const noexcept
1997 constexpr chrono::weekday
1998 weekday() const noexcept
1999 { return _M_wdi.weekday(); }
2002 index() const noexcept
2003 { return _M_wdi.index(); }
2005 constexpr chrono::weekday_indexed
2006 weekday_indexed() const noexcept
2010 operator sys_days() const noexcept
2012 auto __d = sys_days{year() / month() / 1};
2013 return __d + (weekday() - chrono::weekday(__d)
2014 + days{(static_cast<int>(index())-1)*7});
2018 operator local_days() const noexcept
2019 { return local_days{sys_days{*this}.time_since_epoch()}; }
2024 if (!_M_y.ok() || !_M_m.ok() || !_M_wdi.ok())
2026 if (_M_wdi.index() <= 4)
2028 days __d = (_M_wdi.weekday()
2029 - chrono::weekday{sys_days{_M_y / _M_m / 1}}
2030 + days((_M_wdi.index()-1)*7 + 1));
2031 __glibcxx_assert(__d.count() >= 1);
2032 return (unsigned)__d.count() <= (unsigned)(_M_y / _M_m / last).day();
2035 friend constexpr bool
2036 operator==(const year_month_weekday& __x,
2037 const year_month_weekday& __y) noexcept
2039 return __x.year() == __y.year()
2040 && __x.month() == __y.month()
2041 && __x.weekday_indexed() == __y.weekday_indexed();
2044 template<typename = __detail::__months_years_conversion_disambiguator>
2045 friend constexpr year_month_weekday
2046 operator+(const year_month_weekday& __ymwd, const months& __dm) noexcept
2048 return ((__ymwd.year() / __ymwd.month() + __dm)
2049 / __ymwd.weekday_indexed());
2052 template<typename = __detail::__months_years_conversion_disambiguator>
2053 friend constexpr year_month_weekday
2054 operator+(const months& __dm, const year_month_weekday& __ymwd) noexcept
2055 { return __ymwd + __dm; }
2057 friend constexpr year_month_weekday
2058 operator+(const year_month_weekday& __ymwd, const years& __dy) noexcept
2059 { return {__ymwd.year() + __dy, __ymwd.month(), __ymwd.weekday_indexed()}; }
2061 friend constexpr year_month_weekday
2062 operator+(const years& __dy, const year_month_weekday& __ymwd) noexcept
2063 { return __ymwd + __dy; }
2065 template<typename = __detail::__months_years_conversion_disambiguator>
2066 friend constexpr year_month_weekday
2067 operator-(const year_month_weekday& __ymwd, const months& __dm) noexcept
2068 { return __ymwd + -__dm; }
2070 friend constexpr year_month_weekday
2071 operator-(const year_month_weekday& __ymwd, const years& __dy) noexcept
2072 { return __ymwd + -__dy; }
2074 friend constexpr year_month_weekday
2075 operator/(const year_month& __ym,
2076 const chrono::weekday_indexed& __wdi) noexcept
2077 { return {__ym.year(), __ym.month(), __wdi}; }
2079 friend constexpr year_month_weekday
2080 operator/(const chrono::year& __y, const month_weekday& __mwd) noexcept
2081 { return {__y, __mwd.month(), __mwd.weekday_indexed()}; }
2083 friend constexpr year_month_weekday
2084 operator/(int __y, const month_weekday& __mwd) noexcept
2085 { return chrono::year(__y) / __mwd; }
2087 friend constexpr year_month_weekday
2088 operator/(const month_weekday& __mwd, const chrono::year& __y) noexcept
2089 { return __y / __mwd; }
2091 friend constexpr year_month_weekday
2092 operator/(const month_weekday& __mwd, int __y) noexcept
2093 { return chrono::year(__y) / __mwd; }
2096 // YEAR_MONTH_WEEKDAY_LAST
2098 class year_month_weekday_last
2103 chrono::weekday_last _M_wdl;
2107 year_month_weekday_last(const chrono::year& __y, const chrono::month& __m,
2108 const chrono::weekday_last& __wdl) noexcept
2109 : _M_y{__y}, _M_m{__m}, _M_wdl{__wdl}
2112 template<typename = __detail::__months_years_conversion_disambiguator>
2113 constexpr year_month_weekday_last&
2114 operator+=(const months& __m) noexcept
2116 *this = *this + __m;
2120 template<typename = __detail::__months_years_conversion_disambiguator>
2121 constexpr year_month_weekday_last&
2122 operator-=(const months& __m) noexcept
2124 *this = *this - __m;
2128 constexpr year_month_weekday_last&
2129 operator+=(const years& __y) noexcept
2131 *this = *this + __y;
2135 constexpr year_month_weekday_last&
2136 operator-=(const years& __y) noexcept
2138 *this = *this - __y;
2142 constexpr chrono::year
2143 year() const noexcept
2146 constexpr chrono::month
2147 month() const noexcept
2150 constexpr chrono::weekday
2151 weekday() const noexcept
2152 { return _M_wdl.weekday(); }
2154 constexpr chrono::weekday_last
2155 weekday_last() const noexcept
2159 operator sys_days() const noexcept
2161 const auto __d = sys_days{_M_y / _M_m / last};
2162 return sys_days{(__d - (chrono::weekday{__d}
2163 - _M_wdl.weekday())).time_since_epoch()};
2167 operator local_days() const noexcept
2168 { return local_days{sys_days{*this}.time_since_epoch()}; }
2172 { return _M_y.ok() && _M_m.ok() && _M_wdl.ok(); }
2174 friend constexpr bool
2175 operator==(const year_month_weekday_last& __x,
2176 const year_month_weekday_last& __y) noexcept
2178 return __x.year() == __y.year()
2179 && __x.month() == __y.month()
2180 && __x.weekday_last() == __y.weekday_last();
2183 template<typename = __detail::__months_years_conversion_disambiguator>
2184 friend constexpr year_month_weekday_last
2185 operator+(const year_month_weekday_last& __ymwdl,
2186 const months& __dm) noexcept
2188 return ((__ymwdl.year() / __ymwdl.month() + __dm)
2189 / __ymwdl.weekday_last());
2192 template<typename = __detail::__months_years_conversion_disambiguator>
2193 friend constexpr year_month_weekday_last
2194 operator+(const months& __dm,
2195 const year_month_weekday_last& __ymwdl) noexcept
2196 { return __ymwdl + __dm; }
2198 friend constexpr year_month_weekday_last
2199 operator+(const year_month_weekday_last& __ymwdl,
2200 const years& __dy) noexcept
2201 { return {__ymwdl.year() + __dy, __ymwdl.month(), __ymwdl.weekday_last()}; }
2203 friend constexpr year_month_weekday_last
2204 operator+(const years& __dy,
2205 const year_month_weekday_last& __ymwdl) noexcept
2206 { return __ymwdl + __dy; }
2208 template<typename = __detail::__months_years_conversion_disambiguator>
2209 friend constexpr year_month_weekday_last
2210 operator-(const year_month_weekday_last& __ymwdl,
2211 const months& __dm) noexcept
2212 { return __ymwdl + -__dm; }
2214 friend constexpr year_month_weekday_last
2215 operator-(const year_month_weekday_last& __ymwdl,
2216 const years& __dy) noexcept
2217 { return __ymwdl + -__dy; }
2219 friend constexpr year_month_weekday_last
2220 operator/(const year_month& __ym,
2221 const chrono::weekday_last& __wdl) noexcept
2222 { return {__ym.year(), __ym.month(), __wdl}; }
2224 friend constexpr year_month_weekday_last
2225 operator/(const chrono::year& __y,
2226 const chrono::month_weekday_last& __mwdl) noexcept
2227 { return {__y, __mwdl.month(), __mwdl.weekday_last()}; }
2229 friend constexpr year_month_weekday_last
2230 operator/(int __y, const chrono::month_weekday_last& __mwdl) noexcept
2231 { return chrono::year(__y) / __mwdl; }
2233 friend constexpr year_month_weekday_last
2234 operator/(const chrono::month_weekday_last& __mwdl,
2235 const chrono::year& __y) noexcept
2236 { return __y / __mwdl; }
2238 friend constexpr year_month_weekday_last
2239 operator/(const chrono::month_weekday_last& __mwdl, int __y) noexcept
2240 { return chrono::year(__y) / __mwdl; }
2245 /// @cond undocumented
2249 __pow10(unsigned __n)
2257 template<typename _Duration> struct __utc_leap_second;
2261 /** Utility for splitting a duration into hours, minutes, and seconds
2263 * This is a convenience type that provides accessors for the constituent
2264 * parts (hours, minutes, seconds and subseconds) of a duration.
2268 template<typename _Duration>
2271 static_assert( __is_duration<_Duration>::value );
2274 static consteval int
2275 _S_fractional_width()
2277 auto __den = _Duration::period::den;
2278 const int __multiplicity_2 = std::__countr_zero((uintmax_t)__den);
2279 __den >>= __multiplicity_2;
2280 int __multiplicity_5 = 0;
2281 while ((__den % 5) == 0)
2289 int __width = (__multiplicity_2 > __multiplicity_5
2290 ? __multiplicity_2 : __multiplicity_5);
2297 hh_mm_ss(_Duration __d, bool __is_neg)
2298 : _M_h (duration_cast<chrono::hours>(__d)),
2299 _M_m (duration_cast<chrono::minutes>(__d - hours())),
2300 _M_s (duration_cast<chrono::seconds>(__d - hours() - minutes())),
2303 auto __ss = __d - hours() - minutes() - seconds();
2304 if constexpr (treat_as_floating_point_v<typename precision::rep>)
2305 _M_ss._M_r = __ss.count();
2306 else if constexpr (precision::period::den != 1)
2307 _M_ss._M_r = duration_cast<precision>(__ss).count();
2310 static constexpr _Duration
2311 _S_abs(_Duration __d)
2313 if constexpr (numeric_limits<typename _Duration::rep>::is_signed)
2314 return chrono::abs(__d);
2320 static constexpr unsigned fractional_width = {_S_fractional_width()};
2323 = duration<common_type_t<typename _Duration::rep,
2324 chrono::seconds::rep>,
2325 ratio<1, __detail::__pow10(fractional_width)>>;
2327 constexpr hh_mm_ss() noexcept = default;
2330 hh_mm_ss(_Duration __d)
2331 : hh_mm_ss(_S_abs(__d), __d < _Duration::zero())
2335 is_negative() const noexcept
2337 if constexpr (!_S_is_unsigned)
2343 constexpr chrono::hours
2344 hours() const noexcept
2347 constexpr chrono::minutes
2348 minutes() const noexcept
2351 constexpr chrono::seconds
2352 seconds() const noexcept
2356 subseconds() const noexcept
2357 { return static_cast<precision>(_M_ss); }
2360 operator precision() const noexcept
2361 { return to_duration(); }
2364 to_duration() const noexcept
2366 if constexpr (!_S_is_unsigned)
2368 return -(_M_h + _M_m + _M_s + subseconds());
2369 return _M_h + _M_m + _M_s + subseconds();
2373 static constexpr bool _S_is_unsigned
2374 = __and_v<is_integral<typename _Duration::rep>,
2375 is_unsigned<typename _Duration::rep>>;
2377 template<typename _Ratio>
2378 using __byte_duration = duration<unsigned char, _Ratio>;
2380 // The type of the _M_ss member that holds the subsecond precision.
2381 template<typename _Dur>
2384 typename _Dur::rep _M_r{};
2387 operator _Dur() const noexcept
2388 { return _Dur(_M_r); }
2391 // An empty class if this precision doesn't need subseconds.
2392 template<typename _Rep>
2393 requires (!treat_as_floating_point_v<_Rep>)
2394 struct __subseconds<duration<_Rep, ratio<1>>>
2397 operator duration<_Rep, ratio<1>>() const noexcept
2401 // True if the maximum constructor argument can be represented in _Tp.
2402 template<typename _Tp>
2403 static constexpr bool __fits
2404 = duration_values<typename _Duration::rep>::max()
2405 <= duration_values<_Tp>::max();
2407 template<typename _Rep, typename _Period>
2408 requires (!treat_as_floating_point_v<_Rep>)
2409 && ratio_less_v<_Period, ratio<1, 1>>
2410 && (ratio_greater_equal_v<_Period, ratio<1, 250>>
2411 || __fits<unsigned char>)
2412 struct __subseconds<duration<_Rep, _Period>>
2414 unsigned char _M_r{};
2417 operator duration<_Rep, _Period>() const noexcept
2418 { return duration<_Rep, _Period>(_M_r); }
2421 template<typename _Rep, typename _Period>
2422 requires (!treat_as_floating_point_v<_Rep>)
2423 && ratio_less_v<_Period, ratio<1, 250>>
2424 && (ratio_greater_equal_v<_Period, ratio<1, 4000000000>>
2425 || __fits<uint_least32_t>)
2426 struct __subseconds<duration<_Rep, _Period>>
2428 uint_least32_t _M_r{};
2431 operator duration<_Rep, _Period>() const noexcept
2432 { return duration<_Rep, _Period>(_M_r); }
2435 chrono::hours _M_h{};
2436 __byte_duration<ratio<60>> _M_m{};
2437 __byte_duration<ratio<1>> _M_s{};
2439 __subseconds<precision> _M_ss{};
2441 template<typename> friend struct __detail::__utc_leap_second;
2444 /// @cond undocumented
2447 // Represents a time that is within a leap second insertion.
2448 template<typename _Duration>
2449 struct __utc_leap_second
2452 __utc_leap_second(const sys_time<_Duration>& __s)
2453 : _M_date(chrono::floor<days>(__s)), _M_time(__s - _M_date)
2459 hh_mm_ss<common_type_t<_Duration, days>> _M_time;
2464 // 12/24 HOURS FUNCTIONS
2467 is_am(const hours& __h) noexcept
2468 { return 0h <= __h && __h <= 11h; }
2471 is_pm(const hours& __h) noexcept
2472 { return 12h <= __h && __h <= 23h; }
2475 make12(const hours& __h) noexcept
2485 make24(const hours& __h, bool __is_pm) noexcept
2503#if _GLIBCXX_USE_CXX11_ABI || ! _GLIBCXX_USE_DUAL_ABI
2504 // C++20 [time.zones] Time zones
2519 static constexpr int unique = 0;
2520 static constexpr int nonexistent = 1;
2521 static constexpr int ambiguous = 2;
2528 class nonexistent_local_time : public runtime_error
2531 template<typename _Duration>
2532 nonexistent_local_time(const local_time<_Duration>& __tp,
2533 const local_info& __i)
2534 : runtime_error(_S_make_what_str(__tp, __i))
2535 { __glibcxx_assert(__i.result == local_info::nonexistent); }
2538 template<typename _Duration>
2540 _S_make_what_str(const local_time<_Duration>& __tp,
2541 const local_info& __i)
2543 std::ostringstream __os;
2544 __os << __tp << " is in a gap between\n"
2545 << local_seconds(__i.first.end.time_since_epoch())
2546 + __i.first.offset << ' ' << __i.first.abbrev << " and\n"
2547 << local_seconds(__i.second.begin.time_since_epoch())
2548 + __i.second.offset << ' ' << __i.second.abbrev
2549 << " which are both equivalent to\n"
2550 << __i.first.end << " UTC";
2551 return std::move(__os).str();
2555 class ambiguous_local_time : public runtime_error
2558 template<typename _Duration>
2559 ambiguous_local_time(const local_time<_Duration>& __tp,
2560 const local_info& __i)
2561 : runtime_error(_S_make_what_str(__tp, __i))
2562 { __glibcxx_assert(__i.result == local_info::ambiguous); }
2565 template<typename _Duration>
2567 _S_make_what_str(const local_time<_Duration>& __tp,
2568 const local_info& __i)
2570 std::ostringstream __os;
2571 __os << __tp << " is ambiguous. It could be\n"
2572 << __tp << ' ' << __i.first.abbrev << " == "
2573 << __tp - __i.first.offset << " UTC or\n"
2574 << __tp << ' ' << __i.second.abbrev << " == "
2575 << __tp - __i.second.offset << " UTC";
2576 return std::move(__os).str();
2580 template<typename _Duration>
2582 __throw_bad_local_time(const local_time<_Duration>& __tp,
2583 const local_info& __i)
2586 if (__i.result == local_info::nonexistent)
2587 throw nonexistent_local_time(__tp, __i);
2588 throw ambiguous_local_time(__tp, __i);
2594 enum class choose { earliest, latest };
2599 time_zone(time_zone&&) = default;
2600 time_zone& operator=(time_zone&&) = default;
2605 string_view name() const noexcept { return _M_name; }
2607 template<typename _Duration>
2609 get_info(const sys_time<_Duration>& __st) const
2610 { return _M_get_sys_info(chrono::floor<seconds>(__st)); }
2612 template<typename _Duration>
2614 get_info(const local_time<_Duration>& __tp) const
2615 { return _M_get_local_info(chrono::floor<seconds>(__tp)); }
2617 template<typename _Duration>
2618 sys_time<common_type_t<_Duration, seconds>>
2619 to_sys(const local_time<_Duration>& __tp) const
2621 local_info __info = get_info(__tp);
2623 if (__info.result != local_info::unique)
2624 __throw_bad_local_time(__tp, __info);
2626 return sys_time<_Duration>(__tp.time_since_epoch())
2627 - __info.first.offset;
2630 template<typename _Duration>
2631 sys_time<common_type_t<_Duration, seconds>>
2632 to_sys(const local_time<_Duration>& __tp, choose __z) const
2634 local_info __info = get_info(__tp);
2636 if (__info.result == local_info::nonexistent)
2637 return __info.first.end; // Last second of the previous sys_info.
2639 sys_time<_Duration> __st(__tp.time_since_epoch());
2641 if (__info.result == local_info::ambiguous && __z == choose::latest)
2642 return __st - __info.second.offset; // Time in the later sys_info.
2643 // else if __z == earliest, use __info.first.offset as below:
2645 return __st - __info.first.offset;
2648 template<typename _Duration>
2649 local_time<common_type_t<_Duration, seconds>>
2650 to_local(const sys_time<_Duration>& __tp) const
2652 auto __d = (__tp + get_info(__tp).offset).time_since_epoch();
2653 return local_time<common_type_t<_Duration, seconds>>(__d);
2656 [[nodiscard]] friend bool
2657 operator==(const time_zone& __x, const time_zone& __y) noexcept
2658 { return __x._M_name == __y._M_name; }
2660 [[nodiscard]] friend strong_ordering
2661 operator<=>(const time_zone& __x, const time_zone& __y) noexcept
2662 { return __x._M_name <=> __y._M_name; }
2665 sys_info _M_get_sys_info(sys_seconds) const;
2666 local_info _M_get_local_info(local_seconds) const;
2668 friend const tzdb& reload_tzdb();
2670 friend class tzdb_list;
2674 explicit time_zone(unique_ptr<_Impl> __p);
2676 unique_ptr<_Impl> _M_impl;
2679 const time_zone* locate_zone(string_view __tz_name);
2680 const time_zone* current_zone();
2682 /** The list of `chrono::tzdb` objects
2684 * A single object of this type is constructed by the C++ runtime,
2685 * and can be accessed by calling `chrono::get_tzdb_list()`.
2687 * The front of the list is the current `tzdb` object and can be accessed
2688 * via `chrono::get_tzdb_list().front()` or `chrono::get_tzdb()` or
2689 * `*chrono::get_tzdb_list().begin()`.
2691 * The `chrono::reload_tzdb()` function will check for a newer version
2692 * and if found, insert it at the front of the list.
2701 tzdb_list(const tzdb_list&) = delete;
2702 tzdb_list& operator=(const tzdb_list&) = delete;
2704 /** An iterator into the `tzdb_list`
2706 * As a extension, in libstdc++ each `tzdb` is reference-counted
2707 * and the `const_iterator` type shares ownership of the object it
2708 * refers to. This ensures that a `tzdb` erased from the list will
2709 * not be destroyed while there is an iterator that refers to it.
2711 class const_iterator
2714 using value_type = tzdb;
2715 using reference = const tzdb&;
2716 using pointer = const tzdb*;
2717 using difference_type = ptrdiff_t;
2718 using iterator_category = forward_iterator_tag;
2720 constexpr const_iterator() = default;
2721 const_iterator(const const_iterator&) = default;
2722 const_iterator(const_iterator&&) = default;
2723 const_iterator& operator=(const const_iterator&) = default;
2724 const_iterator& operator=(const_iterator&&) = default;
2726 reference operator*() const noexcept;
2727 pointer operator->() const noexcept { return &**this; }
2728 const_iterator& operator++();
2729 const_iterator operator++(int);
2731 bool operator==(const const_iterator&) const noexcept = default;
2734 explicit const_iterator(const shared_ptr<_Node>&) noexcept;
2736 friend class tzdb_list;
2738 shared_ptr<_Node> _M_node;
2739 void* _M_reserved = nullptr;
2742 /** Access the current `tzdb` at the front of the list.
2744 * This returns a reference to the same object as `chrono::get_tzdb()`.
2746 * @returns A reference to the current tzdb object.
2749 const tzdb& front() const noexcept;
2751 /** Remove the tzdb object _after_ the one the iterator refers to.
2753 * Calling this function concurently with any of `front()`, `begin()`,
2754 * or `end()` does not cause a data race, but in general this function
2755 * is not thread-safe. The behaviour may be undefined if erasing an
2756 * element from the list while another thread is calling the same
2757 * function, or incrementing an iterator into the list, or accessing
2758 * the element being erased (unless it is accessed through an iterator).
2760 * @param __p A dereferenceable iterator.
2761 * @returns An iterator the element after the one that was erased
2762 * (or `end()` if there is no such element).
2765 const_iterator erase_after(const_iterator __p);
2767 const_iterator begin() const noexcept;
2768 const_iterator end() const noexcept { return {}; }
2769 const_iterator cbegin() const noexcept { return begin(); }
2770 const_iterator cend() const noexcept { return end(); }
2773 constexpr explicit tzdb_list(nullptr_t);
2775 friend tzdb_list& get_tzdb_list();
2776 friend const tzdb& get_tzdb();
2777 friend const tzdb& reload_tzdb();
2779 friend class leap_second;
2780 friend struct time_zone::_Impl;
2781 friend class time_zone_link;
2784 class time_zone_link
2787 time_zone_link(time_zone_link&&) = default;
2788 time_zone_link& operator=(time_zone_link&&) = default;
2790 string_view name() const noexcept { return _M_name; }
2791 string_view target() const noexcept { return _M_target; }
2794 operator==(const time_zone_link& __x, const time_zone_link& __y) noexcept
2795 { return __x.name() == __y.name(); }
2797 friend strong_ordering
2798 operator<=>(const time_zone_link& __x, const time_zone_link& __y) noexcept
2799 { return __x.name() <=> __y.name(); }
2802 friend const tzdb& reload_tzdb();
2803 friend class tzdb_list::_Node;
2805 explicit time_zone_link(nullptr_t) { }
2814 leap_second(const leap_second&) = default;
2815 leap_second& operator=(const leap_second&) = default;
2818 constexpr sys_seconds
2819 date() const noexcept
2821 if (_M_s >= _M_s.zero()) [[likely]]
2822 return sys_seconds(_M_s);
2823 return sys_seconds(-_M_s);
2828 value() const noexcept
2830 if (_M_s >= _M_s.zero()) [[likely]]
2835 // This can be defaulted because the database will never contain two
2836 // leap_second objects with the same date but different signs.
2837 [[nodiscard]] friend constexpr bool
2838 operator==(const leap_second&, const leap_second&) noexcept = default;
2840 [[nodiscard]] friend constexpr strong_ordering
2841 operator<=>(const leap_second& __x, const leap_second& __y) noexcept
2842 { return __x.date() <=> __y.date(); }
2844 template<typename _Duration>
2845 [[nodiscard]] friend constexpr bool
2846 operator==(const leap_second& __x,
2847 const sys_time<_Duration>& __y) noexcept
2848 { return __x.date() == __y; }
2850 template<typename _Duration>
2851 [[nodiscard]] friend constexpr bool
2852 operator<(const leap_second& __x,
2853 const sys_time<_Duration>& __y) noexcept
2854 { return __x.date() < __y; }
2856 template<typename _Duration>
2857 [[nodiscard]] friend constexpr bool
2858 operator<(const sys_time<_Duration>& __x,
2859 const leap_second& __y) noexcept
2860 { return __x < __y.date(); }
2862 template<typename _Duration>
2863 [[nodiscard]] friend constexpr bool
2864 operator>(const leap_second& __x,
2865 const sys_time<_Duration>& __y) noexcept
2866 { return __y < __x.date(); }
2868 template<typename _Duration>
2869 [[nodiscard]] friend constexpr bool
2870 operator>(const sys_time<_Duration>& __x,
2871 const leap_second& __y) noexcept
2872 { return __y.date() < __x; }
2874 template<typename _Duration>
2875 [[nodiscard]] friend constexpr bool
2876 operator<=(const leap_second& __x,
2877 const sys_time<_Duration>& __y) noexcept
2878 { return !(__y < __x.date()); }
2880 template<typename _Duration>
2881 [[nodiscard]] friend constexpr bool
2882 operator<=(const sys_time<_Duration>& __x,
2883 const leap_second& __y) noexcept
2884 { return !(__y.date() < __x); }
2886 template<typename _Duration>
2887 [[nodiscard]] friend constexpr bool
2888 operator>=(const leap_second& __x,
2889 const sys_time<_Duration>& __y) noexcept
2890 { return !(__x.date() < __y); }
2892 template<typename _Duration>
2893 [[nodiscard]] friend constexpr bool
2894 operator>=(const sys_time<_Duration>& __x,
2895 const leap_second& __y) noexcept
2896 { return !(__x < __y.date()); }
2898 template<three_way_comparable_with<seconds> _Duration>
2899 [[nodiscard]] friend constexpr auto
2900 operator<=>(const leap_second& __x,
2901 const sys_time<_Duration>& __y) noexcept
2902 { return __x.date() <=> __y; }
2905 explicit leap_second(seconds::rep __s) : _M_s(__s) { }
2907 friend class tzdb_list::_Node;
2909 friend const tzdb& reload_tzdb();
2911 template<typename _Duration>
2912 friend leap_second_info
2913 get_leap_second_info(const utc_time<_Duration>&);
2915 seconds _M_s; // == date().time_since_epoch() * value().count()
2918 template<class _Tp> struct zoned_traits { };
2921 struct zoned_traits<const time_zone*>
2923 static const time_zone*
2925 { return std::chrono::locate_zone("UTC"); }
2927 static const time_zone*
2928 locate_zone(string_view __name)
2929 { return std::chrono::locate_zone(__name); }
2935 _GLIBCXX_STD_C::vector<time_zone> zones;
2936 _GLIBCXX_STD_C::vector<time_zone_link> links;
2937 _GLIBCXX_STD_C::vector<leap_second> leap_seconds;
2940 locate_zone(string_view __tz_name) const;
2943 current_zone() const;
2946 friend const tzdb& reload_tzdb();
2947 friend class time_zone;
2948 friend class tzdb_list::_Node;
2951 tzdb_list& get_tzdb_list();
2952 const tzdb& get_tzdb();
2954 const tzdb& reload_tzdb();
2955 string remote_version();
2957 template<typename _Duration, typename _TimeZonePtr = const time_zone*>
2960 static_assert(__is_duration_v<_Duration>);
2962 using _Traits = zoned_traits<_TimeZonePtr>;
2964 // Every constructor that accepts a string_view as its first parameter
2965 // does not participate in class template argument deduction.
2966 using string_view = type_identity_t<std::string_view>;
2969 using duration = common_type_t<_Duration, seconds>;
2971 zoned_time() requires requires { _Traits::default_zone(); }
2974 zoned_time(const zoned_time&) = default;
2975 zoned_time& operator=(const zoned_time&) = default;
2977 zoned_time(const sys_time<_Duration>& __st)
2978 requires requires { _Traits::default_zone(); }
2983 zoned_time(_TimeZonePtr __z) : _M_zone(std::move(__z)) { }
2986 zoned_time(string_view __name)
2988 _TimeZonePtr{_Traits::locate_zone(std::string_view{})};
2990 : _M_zone(_Traits::locate_zone(__name))
2993 template<typename _Duration2>
2994 zoned_time(const zoned_time<_Duration2, _TimeZonePtr>& __zt)
2995 requires is_convertible_v<sys_time<_Duration2>, sys_time<_Duration>>
2996 : _M_zone(__zt._M_zone), _M_tp(__zt._M_tp)
2999 zoned_time(_TimeZonePtr __z, const sys_time<_Duration>& __st)
3000 : _M_zone(std::move(__z)), _M_tp(__st)
3003 zoned_time(string_view __name, const sys_time<_Duration>& __st)
3004 : zoned_time(_Traits::locate_zone(__name), __st)
3007 zoned_time(_TimeZonePtr __z, const local_time<_Duration>& __tp)
3009 { __z->to_sys(__tp) } -> convertible_to<sys_time<_Duration>>;
3011 : _M_zone(std::move(__z)), _M_tp(_M_zone->to_sys(__tp))
3014 zoned_time(string_view __name, const local_time<_Duration>& __tp)
3015 requires requires (_TimeZonePtr __z) {
3016 { _Traits::locate_zone(__name) } -> convertible_to<_TimeZonePtr>;
3017 { __z->to_sys(__tp) } -> convertible_to<sys_time<_Duration>>;
3019 : zoned_time(_Traits::locate_zone(__name), __tp)
3022 zoned_time(_TimeZonePtr __z, const local_time<_Duration>& __tp,
3025 { __z->to_sys(__tp, __c) } -> convertible_to<sys_time<_Duration>>;
3027 : _M_zone(std::move(__z)), _M_tp(_M_zone->to_sys(__tp, __c))
3030 zoned_time(string_view __name, const local_time<_Duration>& __tp,
3032 requires requires (_TimeZonePtr __z) {
3033 { _Traits::locate_zone(__name) } -> convertible_to<_TimeZonePtr>;
3034 { __z->to_sys(__tp, __c) } -> convertible_to<sys_time<_Duration>>;
3036 : _M_zone(_Traits::locate_zone(__name)),
3037 _M_tp(_M_zone->to_sys(__tp, __c))
3040 template<typename _Duration2, typename _TimeZonePtr2>
3041 zoned_time(_TimeZonePtr __z,
3042 const zoned_time<_Duration2, _TimeZonePtr2>& __zt)
3043 requires is_convertible_v<sys_time<_Duration2>, sys_time<_Duration>>
3044 : _M_zone(__z), _M_tp(__zt._M_tp)
3047 template<typename _Duration2, typename _TimeZonePtr2>
3048 zoned_time(_TimeZonePtr __z,
3049 const zoned_time<_Duration2, _TimeZonePtr2>& __zt,
3051 requires is_convertible_v<sys_time<_Duration2>, sys_time<_Duration>>
3052 : _M_zone(__z), _M_tp(__zt._M_tp)
3055 template<typename _Duration2, typename _TimeZonePtr2>
3056 zoned_time(string_view __name,
3057 const zoned_time<_Duration2, _TimeZonePtr2>& __zt)
3058 requires is_convertible_v<sys_time<_Duration2>, sys_time<_Duration>>
3060 { _Traits::locate_zone(__name) } -> convertible_to<_TimeZonePtr>;
3062 : _M_zone(_Traits::locate_zone(__name)), _M_tp(__zt._M_tp)
3065 template<typename _Duration2, typename _TimeZonePtr2>
3066 zoned_time(string_view __name,
3067 const zoned_time<_Duration2, _TimeZonePtr2>& __zt,
3069 requires is_convertible_v<sys_time<_Duration2>, sys_time<_Duration>>
3071 { _Traits::locate_zone(__name) } -> convertible_to<_TimeZonePtr>;
3073 : _M_zone(_Traits::locate_zone(__name)), _M_tp(__zt._M_tp)
3077 operator=(const sys_time<_Duration>& __st)
3084 operator=(const local_time<_Duration>& __lt)
3086 _M_tp = _M_zone->to_sys(__lt);
3091 operator sys_time<duration>() const { return _M_tp; }
3094 explicit operator local_time<duration>() const
3095 { return get_local_time(); }
3099 get_time_zone() const
3103 local_time<duration>
3104 get_local_time() const
3105 { return _M_zone->to_local(_M_tp); }
3109 get_sys_time() const
3115 { return _M_zone->get_info(_M_tp); }
3117 [[nodiscard]] friend bool
3118 operator==(const zoned_time&, const zoned_time&) = default;
3121 _TimeZonePtr _M_zone{ _Traits::default_zone() };
3122 sys_time<duration> _M_tp{};
3124 template<typename _Duration2, typename _TimeZonePtr2>
3125 friend class zoned_time;
3128 zoned_time() -> zoned_time<seconds>;
3130 template<typename _Duration>
3131 zoned_time(sys_time<_Duration>)
3132 -> zoned_time<common_type_t<_Duration, seconds>>;
3134 /// @cond undocumented
3135 template<typename _TimeZonePtrOrName>
3136 using __time_zone_representation
3137 = __conditional_t<is_convertible_v<_TimeZonePtrOrName, string_view>,
3139 remove_cvref_t<_TimeZonePtrOrName>>;
3142 template<typename _TimeZonePtrOrName>
3143 zoned_time(_TimeZonePtrOrName&&)
3144 -> zoned_time<seconds, __time_zone_representation<_TimeZonePtrOrName>>;
3146 template<typename _TimeZonePtrOrName, typename _Duration>
3147 zoned_time(_TimeZonePtrOrName&&, sys_time<_Duration>)
3148 -> zoned_time<common_type_t<_Duration, seconds>,
3149 __time_zone_representation<_TimeZonePtrOrName>>;
3151 template<typename _TimeZonePtrOrName, typename _Duration>
3152 zoned_time(_TimeZonePtrOrName&&, local_time<_Duration>,
3153 choose = choose::earliest)
3154 -> zoned_time<common_type_t<_Duration, seconds>,
3155 __time_zone_representation<_TimeZonePtrOrName>>;
3157 template<typename _Duration, typename _TimeZonePtrOrName,
3158 typename _TimeZonePtr2>
3159 zoned_time(_TimeZonePtrOrName&&, zoned_time<_Duration, _TimeZonePtr2>,
3160 choose = choose::earliest)
3161 -> zoned_time<common_type_t<_Duration, seconds>,
3162 __time_zone_representation<_TimeZonePtrOrName>>;
3164 template<typename _Dur1, typename _TZPtr1, typename _Dur2, typename _TZPtr2>
3167 operator==(const zoned_time<_Dur1, _TZPtr1>& __x,
3168 const zoned_time<_Dur2, _TZPtr2>& __y)
3170 return __x.get_time_zone() == __y.get_time_zone()
3171 && __x.get_sys_time() == __y.get_sys_time();
3174 using zoned_seconds = zoned_time<seconds>;
3175#endif // _GLIBCXX_USE_CXX11_ABI || ! _GLIBCXX_USE_DUAL_ABI
3179 inline leap_second_info
3180 __get_leap_second_info(sys_seconds __ss, bool __is_utc)
3182 if (__ss < sys_seconds{}) [[unlikely]]
3185 const seconds::rep __leaps[] {
3186 78796800, // 1 Jul 1972
3187 94694400, // 1 Jan 1973
3188 126230400, // 1 Jan 1974
3189 157766400, // 1 Jan 1975
3190 189302400, // 1 Jan 1976
3191 220924800, // 1 Jan 1977
3192 252460800, // 1 Jan 1978
3193 283996800, // 1 Jan 1979
3194 315532800, // 1 Jan 1980
3195 362793600, // 1 Jul 1981
3196 394329600, // 1 Jul 1982
3197 425865600, // 1 Jul 1983
3198 489024000, // 1 Jul 1985
3199 567993600, // 1 Jan 1988
3200 631152000, // 1 Jan 1990
3201 662688000, // 1 Jan 1991
3202 709948800, // 1 Jul 1992
3203 741484800, // 1 Jul 1993
3204 773020800, // 1 Jul 1994
3205 820454400, // 1 Jan 1996
3206 867715200, // 1 Jul 1997
3207 915148800, // 1 Jan 1999
3208 1136073600, // 1 Jan 2006
3209 1230768000, // 1 Jan 2009
3210 1341100800, // 1 Jul 2012
3211 1435708800, // 1 Jul 2015
3212 1483228800, // 1 Jan 2017
3214 // The list above is known to be valid until (at least) this date
3215 // and only contains positive leap seconds.
3216 const sys_seconds __expires(1703721600s); // 2023-12-28 00:00:00 UTC
3218#if _GLIBCXX_USE_CXX11_ABI || ! _GLIBCXX_USE_DUAL_ABI
3219 if (__ss > __expires)
3221 // Use updated leap_seconds from tzdb.
3222 size_t __n = std::size(__leaps);
3224 auto __db = get_tzdb_list().begin();
3225 auto __first = __db->leap_seconds.begin() + __n;
3226 auto __last = __db->leap_seconds.end();
3227 auto __pos = std::upper_bound(__first, __last, __ss);
3228 seconds __elapsed(__n);
3229 for (auto __i = __first; __i != __pos; ++__i)
3230 __elapsed += __i->value();
3234 // Convert utc_time to sys_time:
3236 // See if that sys_time is before (or during) previous leap sec:
3237 if (__pos != __first && __ss < __pos[-1])
3239 if ((__ss + 1s) >= __pos[-1])
3240 return {true, __elapsed};
3241 __elapsed -= __pos[-1].value();
3244 return {false, __elapsed};
3249 seconds::rep __s = __ss.time_since_epoch().count();
3250 const seconds::rep* __first = std::begin(__leaps);
3251 const seconds::rep* __last = std::end(__leaps);
3253 // Don't bother searching the list if we're after the last one.
3254 if (__s > (__last[-1] + (__last - __first) + 1))
3255 return { false, seconds(__last - __first) };
3257 auto __pos = std::upper_bound(__first, __last, __s);
3258 seconds __elapsed{__pos - __first};
3261 // Convert utc_time to sys_time:
3262 __s -= __elapsed.count();
3263 // See if that sys_time is before (or during) previous leap sec:
3264 if (__pos != __first && __s < __pos[-1])
3266 if ((__s + 1) >= __pos[-1])
3267 return {true, __elapsed};
3271 return {false, __elapsed};
3274} // namespace __detail
3276 template<typename _Duration>
3278 inline leap_second_info
3279 get_leap_second_info(const utc_time<_Duration>& __ut)
3281 auto __s = chrono::duration_cast<seconds>(__ut.time_since_epoch());
3282 return __detail::__get_leap_second_info(sys_seconds(__s), true);
3285 template<typename _Duration>
3287 inline utc_time<common_type_t<_Duration, seconds>>
3288 utc_clock::from_sys(const sys_time<_Duration>& __t)
3290 using _CDur = common_type_t<_Duration, seconds>;
3291 auto __s = chrono::time_point_cast<seconds>(__t);
3292 const auto __li = __detail::__get_leap_second_info(__s, false);
3293 return utc_time<_CDur>{__t.time_since_epoch()} + __li.elapsed;
3298 } // namespace chrono
3300#if __cplusplus >= 202002L
3301 inline namespace literals
3303 inline namespace chrono_literals
3305 /// @addtogroup chrono
3307#pragma GCC diagnostic push
3308#pragma GCC diagnostic ignored "-Wliteral-suffix"
3309 /// Literal suffix for creating chrono::day objects.
3311 constexpr chrono::day
3312 operator""d(unsigned long long __d) noexcept
3313 { return chrono::day{static_cast<unsigned>(__d)}; }
3315 /// Literal suffix for creating chrono::year objects.
3317 constexpr chrono::year
3318 operator""y(unsigned long long __y) noexcept
3319 { return chrono::year{static_cast<int>(__y)}; }
3320#pragma GCC diagnostic pop
3322 } // inline namespace chrono_literals
3323 } // inline namespace literals
3326_GLIBCXX_END_NAMESPACE_VERSION
3329#if __cplusplus >= 202002L
3330# include <bits/chrono_io.h>
3335#endif //_GLIBCXX_CHRONO