3// Copyright (C) 2008-2025 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/>.
27/** @file include/chrono
28 * This is a Standard C++ Library header.
32#ifndef _GLIBCXX_CHRONO
33#define _GLIBCXX_CHRONO 1
36#pragma GCC system_header
39#ifdef _GLIBCXX_NO_FREESTANDING_CHRONO
40# include <bits/requires_hosted.h> // for <ctime> and clocks
43#if __cplusplus < 201103L
44# include <bits/c++0x_warning.h>
47#define __glibcxx_want_chrono
48#define __glibcxx_want_chrono_udls
49#include <bits/version.h>
51#include <bits/chrono.h>
53#if __cpp_lib_bitops >= 201907L
54# include <bit> // __countr_zero
56#ifdef __glibcxx_chrono_cxx20
57# include <bits/stl_algo.h> // upper_bound
58# include <bits/range_access.h> // begin/end for arrays
60#if __cpp_lib_chrono >= 201803L // C++20 && HOSTED && USE_CXX11_ABI
64# include <bits/shared_ptr.h>
65# include <bits/unique_ptr.h>
68namespace std _GLIBCXX_VISIBILITY(default)
70_GLIBCXX_BEGIN_NAMESPACE_VERSION
73 * @defgroup chrono Time
76 * Classes and functions for time.
81 /** @namespace std::chrono
82 * @brief ISO C++ 2011 namespace for date and time utilities
87#ifdef __glibcxx_chrono_cxx20
88 /// @addtogroup chrono
91 template<typename _Duration>
92 using local_time = time_point<local_t, _Duration>;
93 using local_seconds = local_time<seconds>;
94 using local_days = local_time<days>;
101 template<typename _Duration>
102 using utc_time = time_point<utc_clock, _Duration>;
103 using utc_seconds = utc_time<seconds>;
105 template<typename _Duration>
106 using tai_time = time_point<tai_clock, _Duration>;
107 using tai_seconds = tai_time<seconds>;
109 template<typename _Duration>
110 using gps_time = time_point<gps_clock, _Duration>;
111 using gps_seconds = gps_time<seconds>;
113 template<> struct is_clock<utc_clock> : true_type { };
114 template<> struct is_clock<tai_clock> : true_type { };
115 template<> struct is_clock<gps_clock> : true_type { };
117 template<> inline constexpr bool is_clock_v<utc_clock> = true;
118 template<> inline constexpr bool is_clock_v<tai_clock> = true;
119 template<> inline constexpr bool is_clock_v<gps_clock> = true;
121 struct leap_second_info
127 template<typename _Duration>
129 get_leap_second_info(const utc_time<_Duration>& __ut);
131 /** A clock that measures Universal Coordinated Time (UTC).
133 * The epoch is 1970-01-01 00:00:00.
140 using rep = system_clock::rep;
141 using period = system_clock::period;
142 using duration = chrono::duration<rep, period>;
143 using time_point = chrono::time_point<utc_clock>;
144 static constexpr bool is_steady = false;
149 { return from_sys(system_clock::now()); }
151 template<typename _Duration>
153 static sys_time<common_type_t<_Duration, seconds>>
154 to_sys(const utc_time<_Duration>& __t)
156 using _CDur = common_type_t<_Duration, seconds>;
157 const auto __li = chrono::get_leap_second_info(__t);
158 sys_time<_CDur> __s{__t.time_since_epoch() - __li.elapsed};
159 if (__li.is_leap_second)
160 __s = chrono::floor<seconds>(__s) + seconds{1} - _CDur{1};
164 template<typename _Duration>
166 static utc_time<common_type_t<_Duration, seconds>>
167 from_sys(const sys_time<_Duration>& __t);
170 /** A clock that measures International Atomic Time.
172 * The epoch is 1958-01-01 00:00:00.
179 using rep = system_clock::rep;
180 using period = system_clock::period;
181 using duration = chrono::duration<rep, period>;
182 using time_point = chrono::time_point<tai_clock>;
183 static constexpr bool is_steady = false;
187 now(); // in src/c++20/clock.cc
189 template<typename _Duration>
191 static utc_time<common_type_t<_Duration, seconds>>
192 to_utc(const tai_time<_Duration>& __t)
194 using _CDur = common_type_t<_Duration, seconds>;
195 return utc_time<_CDur>{__t.time_since_epoch()} - 378691210s;
198 template<typename _Duration>
200 static tai_time<common_type_t<_Duration, seconds>>
201 from_utc(const utc_time<_Duration>& __t)
203 using _CDur = common_type_t<_Duration, seconds>;
204 return tai_time<_CDur>{__t.time_since_epoch()} + 378691210s;
208 /** A clock that measures GPS time.
210 * The epoch is 1980-01-06 00:00:00.
217 using rep = system_clock::rep;
218 using period = system_clock::period;
219 using duration = chrono::duration<rep, period>;
220 using time_point = chrono::time_point<gps_clock>;
221 static constexpr bool is_steady = false;
225 now(); // in src/c++20/clock.cc
227 template<typename _Duration>
229 static utc_time<common_type_t<_Duration, seconds>>
230 to_utc(const gps_time<_Duration>& __t)
232 using _CDur = common_type_t<_Duration, seconds>;
233 return utc_time<_CDur>{__t.time_since_epoch()} + 315964809s;
236 template<typename _Duration>
238 static gps_time<common_type_t<_Duration, seconds>>
239 from_utc(const utc_time<_Duration>& __t)
241 using _CDur = common_type_t<_Duration, seconds>;
242 return gps_time<_CDur>{__t.time_since_epoch()} - 315964809s;
245#endif // _GLIBCXX_HOSTED
247 template<typename _DestClock, typename _SourceClock>
248 struct clock_time_conversion
251 // Identity conversions
253 template<typename _Clock>
254 struct clock_time_conversion<_Clock, _Clock>
256 template<typename _Duration>
257 time_point<_Clock, _Duration>
258 operator()(const time_point<_Clock, _Duration>& __t) const
264 struct clock_time_conversion<system_clock, system_clock>
266 template<typename _Duration>
268 operator()(const sys_time<_Duration>& __t) const
273 struct clock_time_conversion<utc_clock, utc_clock>
275 template<typename _Duration>
277 operator()(const utc_time<_Duration>& __t) const
281 // Conversions between system_clock and utc_clock
284 struct clock_time_conversion<utc_clock, system_clock>
286 template<typename _Duration>
287 utc_time<common_type_t<_Duration, seconds>>
288 operator()(const sys_time<_Duration>& __t) const
289 { return utc_clock::from_sys(__t); }
293 struct clock_time_conversion<system_clock, utc_clock>
295 template<typename _Duration>
296 sys_time<common_type_t<_Duration, seconds>>
297 operator()(const utc_time<_Duration>& __t) const
298 { return utc_clock::to_sys(__t); }
301 template<typename _Tp, typename _Clock>
302 inline constexpr bool __is_time_point_for_v = false;
304 template<typename _Clock, typename _Duration>
305 inline constexpr bool
306 __is_time_point_for_v<time_point<_Clock, _Duration>, _Clock> = true;
308 // Conversions between system_clock and other clocks
310 template<typename _SourceClock>
311 struct clock_time_conversion<system_clock, _SourceClock>
313 template<typename _Duration, typename _Src = _SourceClock>
315 operator()(const time_point<_SourceClock, _Duration>& __t) const
316 -> decltype(_Src::to_sys(__t))
318 using _Ret = decltype(_SourceClock::to_sys(__t));
319 static_assert(__is_time_point_for_v<_Ret, system_clock>);
320 return _SourceClock::to_sys(__t);
324 template<typename _DestClock>
325 struct clock_time_conversion<_DestClock, system_clock>
327 template<typename _Duration, typename _Dest = _DestClock>
329 operator()(const sys_time<_Duration>& __t) const
330 -> decltype(_Dest::from_sys(__t))
332 using _Ret = decltype(_DestClock::from_sys(__t));
333 static_assert(__is_time_point_for_v<_Ret, _DestClock>);
334 return _DestClock::from_sys(__t);
338 // Conversions between utc_clock and other clocks
340 template<typename _SourceClock>
341 struct clock_time_conversion<utc_clock, _SourceClock>
343 template<typename _Duration, typename _Src = _SourceClock>
345 operator()(const time_point<_SourceClock, _Duration>& __t) const
346 -> decltype(_Src::to_utc(__t))
348 using _Ret = decltype(_SourceClock::to_utc(__t));
349 static_assert(__is_time_point_for_v<_Ret, utc_clock>);
350 return _SourceClock::to_utc(__t);
354 template<typename _DestClock>
355 struct clock_time_conversion<_DestClock, utc_clock>
357 template<typename _Duration, typename _Dest = _DestClock>
359 operator()(const utc_time<_Duration>& __t) const
360 -> decltype(_Dest::from_utc(__t))
362 using _Ret = decltype(_DestClock::from_utc(__t));
363 static_assert(__is_time_point_for_v<_Ret, _DestClock>);
364 return _DestClock::from_utc(__t);
367#endif // _GLIBCXX_HOSTED
369 /// @cond undocumented
372 template<typename _DestClock, typename _SourceClock, typename _Duration>
373 concept __clock_convs
374 = requires (const time_point<_SourceClock, _Duration>& __t) {
375 clock_time_conversion<_DestClock, _SourceClock>{}(__t);
379 template<typename _DestClock, typename _SourceClock, typename _Duration>
380 concept __clock_convs_sys
381 = requires (const time_point<_SourceClock, _Duration>& __t) {
382 clock_time_conversion<_DestClock, system_clock>{}(
383 clock_time_conversion<system_clock, _SourceClock>{}(__t));
386 template<typename _DestClock, typename _SourceClock, typename _Duration>
387 concept __clock_convs_utc
388 = requires (const time_point<_SourceClock, _Duration>& __t) {
389 clock_time_conversion<_DestClock, utc_clock>{}(
390 clock_time_conversion<utc_clock, _SourceClock>{}(__t));
393 template<typename _DestClock, typename _SourceClock, typename _Duration>
394 concept __clock_convs_sys_utc
395 = requires (const time_point<_SourceClock, _Duration>& __t) {
396 clock_time_conversion<_DestClock, utc_clock>{}(
397 clock_time_conversion<utc_clock, system_clock>{}(
398 clock_time_conversion<system_clock, _SourceClock>{}(__t)));
401 template<typename _DestClock, typename _SourceClock, typename _Duration>
402 concept __clock_convs_utc_sys
403 = requires (const time_point<_SourceClock, _Duration>& __t) {
404 clock_time_conversion<_DestClock, system_clock>{}(
405 clock_time_conversion<system_clock, utc_clock>{}(
406 clock_time_conversion<utc_clock, _SourceClock>{}(__t)));
408#endif // _GLIBCXX_HOSTED
409 } // namespace __detail
412 /// Convert a time point to a different clock.
413 template<typename _DestClock, typename _SourceClock, typename _Duration>
416 clock_cast(const time_point<_SourceClock, _Duration>& __t)
417 requires __detail::__clock_convs<_DestClock, _SourceClock, _Duration>
419 || __detail::__clock_convs_sys<_DestClock, _SourceClock, _Duration>
420 || __detail::__clock_convs_utc<_DestClock, _SourceClock, _Duration>
421 || __detail::__clock_convs_sys_utc<_DestClock, _SourceClock, _Duration>
422 || __detail::__clock_convs_utc_sys<_DestClock, _SourceClock, _Duration>
423#endif // _GLIBCXX_HOSTED
425 constexpr bool __direct
426 = __detail::__clock_convs<_DestClock, _SourceClock, _Duration>;
427 if constexpr (__direct)
429 return clock_time_conversion<_DestClock, _SourceClock>{}(__t);
434 constexpr bool __convert_via_sys_clock
435 = __detail::__clock_convs_sys<_DestClock, _SourceClock, _Duration>;
436 constexpr bool __convert_via_utc_clock
437 = __detail::__clock_convs_utc<_DestClock, _SourceClock, _Duration>;
438 if constexpr (__convert_via_sys_clock)
440 static_assert(!__convert_via_utc_clock,
441 "clock_cast requires a unique best conversion, but "
442 "conversion is possible via system_clock and also via"
444 return clock_time_conversion<_DestClock, system_clock>{}(
445 clock_time_conversion<system_clock, _SourceClock>{}(__t));
447 else if constexpr (__convert_via_utc_clock)
449 return clock_time_conversion<_DestClock, utc_clock>{}(
450 clock_time_conversion<utc_clock, _SourceClock>{}(__t));
454 constexpr bool __convert_via_sys_and_utc_clocks
455 = __detail::__clock_convs_sys_utc<_DestClock,
459 if constexpr (__convert_via_sys_and_utc_clocks)
461 constexpr bool __convert_via_utc_and_sys_clocks
462 = __detail::__clock_convs_utc_sys<_DestClock,
465 static_assert(!__convert_via_utc_and_sys_clocks,
466 "clock_cast requires a unique best conversion, but "
467 "conversion is possible via system_clock followed by "
468 "utc_clock, and also via utc_clock followed by "
470 return clock_time_conversion<_DestClock, utc_clock>{}(
471 clock_time_conversion<utc_clock, system_clock>{}(
472 clock_time_conversion<system_clock, _SourceClock>{}(__t)));
476 return clock_time_conversion<_DestClock, system_clock>{}(
477 clock_time_conversion<system_clock, utc_clock>{}(
478 clock_time_conversion<utc_clock, _SourceClock>{}(__t)));
482#endif // _GLIBCXX_HOSTED
487 // CLASS DECLARATIONS
492 class weekday_indexed;
495 class month_day_last;
497 class month_weekday_last;
499 class year_month_day;
500 class year_month_day_last;
501 class year_month_weekday;
502 class year_month_weekday_last;
506 explicit last_spec() = default;
508 friend constexpr month_day_last
509 operator/(int __m, last_spec) noexcept;
511 friend constexpr month_day_last
512 operator/(last_spec, int __m) noexcept;
515 inline constexpr last_spec last{};
519 // Helper to __add_modulo and __sub_modulo.
520 template <unsigned __d, typename _Tp>
524 using _Up = make_unsigned_t<_Tp>;
525 auto constexpr __a = _Up(-1) - _Up(255 + __d - 2);
526 auto constexpr __b = _Up(__d * (__a / __d) - 1);
527 // Notice: b <= a - 1 <= _Up(-1) - (255 + d - 1) and b % d = d - 1.
528 return _Up(-1) - __b; // >= 255 + d - 1
531 // Compute the remainder of the Euclidean division of __x + __y divided by
532 // __d without overflowing. Typically, __x <= 255 + d - 1 is sum of
533 // weekday/month with a shift in [0, d - 1] and __y is a duration count.
534 template <unsigned __d, typename _Tp>
536 __add_modulo(unsigned __x, _Tp __y)
538 using _Up = make_unsigned_t<_Tp>;
539 // For __y >= 0, _Up(__y) has the same mathematical value as __y and
540 // this function simply returns (__x + _Up(__y)) % d. Typically, this
541 // doesn't overflow since the range of _Up contains many more positive
542 // values than _Tp's. For __y < 0, _Up(__y) has a mathematical value in
543 // the upper-half range of _Up so that adding a positive value to it
544 // might overflow. Moreover, most likely, _Up(__y) != __y mod d. To
545 // fix both issues we subtract from _Up(__y) an __offset >=
546 // 255 + d - 1 to make room for the addition to __x and shift the modulo
547 // to the correct value.
548 auto const __offset = __y >= 0 ? _Up(0) : __modulo_offset<__d, _Tp>();
549 return (__x + _Up(__y) - __offset) % __d;
552 // Similar to __add_modulo but for __x - __y.
553 template <unsigned __d, typename _Tp>
555 __sub_modulo(unsigned __x, _Tp __y)
557 using _Up = make_unsigned_t<_Tp>;
558 auto const __offset = __y <= 0 ? _Up(0) : __modulo_offset<__d, _Tp>();
559 return (__x - _Up(__y) - __offset) % __d;
562 inline constexpr unsigned __days_per_month[12]
563 = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
577 day(unsigned __d) noexcept
582 operator++() noexcept
589 operator++(int) noexcept
597 operator--() noexcept
604 operator--(int) noexcept
612 operator+=(const days& __d) noexcept
619 operator-=(const days& __d) noexcept
626 operator unsigned() const noexcept
631 { return 1 <= _M_d && _M_d <= 31; }
633 friend constexpr bool
634 operator==(const day& __x, const day& __y) noexcept
635 { return unsigned{__x} == unsigned{__y}; }
637 friend constexpr strong_ordering
638 operator<=>(const day& __x, const day& __y) noexcept
639 { return unsigned{__x} <=> unsigned{__y}; }
642 operator+(const day& __x, const days& __y) noexcept
643 { return day(unsigned{__x} + __y.count()); }
646 operator+(const days& __x, const day& __y) noexcept
647 { return __y + __x; }
650 operator-(const day& __x, const days& __y) noexcept
651 { return __x + -__y; }
653 friend constexpr days
654 operator-(const day& __x, const day& __y) noexcept
655 { return days{int(unsigned{__x}) - int(unsigned{__y})}; }
657 friend constexpr month_day
658 operator/(const month& __m, const day& __d) noexcept;
660 friend constexpr month_day
661 operator/(int __m, const day& __d) noexcept;
663 friend constexpr month_day
664 operator/(const day& __d, const month& __m) noexcept;
666 friend constexpr month_day
667 operator/(const day& __d, int __m) noexcept;
669 friend constexpr year_month_day
670 operator/(const year_month& __ym, const day& __d) noexcept;
684 month(unsigned __m) noexcept
689 operator++() noexcept
696 operator++(int) noexcept
704 operator--() noexcept
711 operator--(int) noexcept
719 operator+=(const months& __m) noexcept
726 operator-=(const months& __m) noexcept
733 operator unsigned() const noexcept
738 { return 1 <= _M_m && _M_m <= 12; }
740 friend constexpr bool
741 operator==(const month& __x, const month& __y) noexcept
742 { return unsigned{__x} == unsigned{__y}; }
744 friend constexpr strong_ordering
745 operator<=>(const month& __x, const month& __y) noexcept
746 { return unsigned{__x} <=> unsigned{__y}; }
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::__add_modulo<12>(
754 unsigned{__x} + 11, __y.count())};
757 friend constexpr month
758 operator+(const months& __x, const month& __y) noexcept
759 { return __y + __x; }
761 friend constexpr month
762 operator-(const month& __x, const months& __y) noexcept
764 // modulo(x + (-y - 1), 12) = modulo(x + (-y - 1) + 12, 12)
765 // = modulo((x + 11) - y , 12)
766 return month{1 + __detail::__sub_modulo<12>(
767 unsigned{__x} + 11, __y.count())};
770 friend constexpr months
771 operator-(const month& __x, const month& __y) noexcept
773 const auto __dm = int(unsigned(__x)) - int(unsigned(__y));
774 return months{__dm < 0 ? 12 + __dm : __dm};
777 friend constexpr year_month
778 operator/(const year& __y, const month& __m) noexcept;
780 friend constexpr month_day
781 operator/(const month& __m, int __d) noexcept;
783 friend constexpr month_day_last
784 operator/(const month& __m, last_spec) noexcept;
786 friend constexpr month_day_last
787 operator/(last_spec, const month& __m) noexcept;
789 friend constexpr month_weekday
790 operator/(const month& __m, const weekday_indexed& __wdi) noexcept;
792 friend constexpr month_weekday
793 operator/(const weekday_indexed& __wdi, const month& __m) noexcept;
795 friend constexpr month_weekday_last
796 operator/(const month& __m, const weekday_last& __wdl) noexcept;
798 friend constexpr month_weekday_last
799 operator/(const weekday_last& __wdl, const month& __m) noexcept;
802 inline constexpr month January{1};
803 inline constexpr month February{2};
804 inline constexpr month March{3};
805 inline constexpr month April{4};
806 inline constexpr month May{5};
807 inline constexpr month June{6};
808 inline constexpr month July{7};
809 inline constexpr month August{8};
810 inline constexpr month September{9};
811 inline constexpr month October{10};
812 inline constexpr month November{11};
813 inline constexpr month December{12};
826 year(int __y) noexcept
827 : _M_y{static_cast<short>(__y)}
830 static constexpr year
832 { return year{-32767}; }
834 static constexpr year
836 { return year{32767}; }
839 operator++() noexcept
846 operator++(int) noexcept
854 operator--() noexcept
861 operator--(int) noexcept
869 operator+=(const years& __y) noexcept
876 operator-=(const years& __y) noexcept
883 operator+() const noexcept
887 operator-() const noexcept
888 { return year{-_M_y}; }
891 is_leap() const noexcept
893 // Testing divisibility by 100 first gives better performance [1], i.e.,
894 // return _M_y % 100 == 0 ? _M_y % 400 == 0 : _M_y % 16 == 0;
895 // Furthermore, if _M_y % 100 == 0, then _M_y % 400 == 0 is equivalent
896 // to _M_y % 16 == 0, so we can simplify it to
897 // return _M_y % 100 == 0 ? _M_y % 16 == 0 : _M_y % 4 == 0. // #1
898 // Similarly, we can replace 100 with 25 (which is good since
899 // _M_y % 25 == 0 requires one fewer instruction than _M_y % 100 == 0
901 // return _M_y % 25 == 0 ? _M_y % 16 == 0 : _M_y % 4 == 0. // #2
902 // Indeed, first assume _M_y % 4 != 0. Then _M_y % 16 != 0 and hence,
903 // _M_y % 4 == 0 and _M_y % 16 == 0 are both false. Therefore, #2
904 // returns false as it should (regardless of _M_y % 25.) Now assume
905 // _M_y % 4 == 0. In this case, _M_y % 25 == 0 if, and only if,
906 // _M_y % 100 == 0, that is, #1 and #2 are equivalent. Finally, #2 is
908 // return (_M_y & (_M_y % 25 == 0 ? 15 : 3)) == 0.
911 // [1] https://github.com/cassioneri/calendar
912 // [2] https://godbolt.org/z/55G8rn77e
913 // [3] https://gcc.gnu.org/pipermail/libstdc++/2021-June/052815.html
915 return (_M_y & (_M_y % 25 == 0 ? 15 : 3)) == 0;
919 operator int() const noexcept
924 { return min()._M_y <= _M_y && _M_y <= max()._M_y; }
926 friend constexpr bool
927 operator==(const year& __x, const year& __y) noexcept
928 { return int{__x} == int{__y}; }
930 friend constexpr strong_ordering
931 operator<=>(const year& __x, const year& __y) noexcept
932 { return int{__x} <=> int{__y}; }
934 friend constexpr year
935 operator+(const year& __x, const years& __y) noexcept
936 { return year{int{__x} + static_cast<int>(__y.count())}; }
938 friend constexpr year
939 operator+(const years& __x, const year& __y) noexcept
940 { return __y + __x; }
942 friend constexpr year
943 operator-(const year& __x, const years& __y) noexcept
944 { return __x + -__y; }
946 friend constexpr years
947 operator-(const year& __x, const year& __y) noexcept
948 { return years{int{__x} - int{__y}}; }
950 friend constexpr year_month
951 operator/(const year& __y, int __m) noexcept;
953 friend constexpr year_month_day
954 operator/(const year& __y, const month_day& __md) noexcept;
956 friend constexpr year_month_day
957 operator/(const month_day& __md, const year& __y) noexcept;
959 friend constexpr year_month_day_last
960 operator/(const year& __y, const month_day_last& __mdl) noexcept;
962 friend constexpr year_month_day_last
963 operator/(const month_day_last& __mdl, const year& __y) noexcept;
965 friend constexpr year_month_weekday
966 operator/(const year& __y, const month_weekday& __mwd) noexcept;
968 friend constexpr year_month_weekday
969 operator/(const month_weekday& __mwd, const year& __y) noexcept;
971 friend constexpr year_month_weekday_last
972 operator/(const year& __y, const month_weekday_last& __mwdl) noexcept;
974 friend constexpr year_month_weekday_last
975 operator/(const month_weekday_last& __mwdl, const year& __y) noexcept;
985 static constexpr weekday
986 _S_from_days(const days& __d)
988 return weekday{__detail::__add_modulo<7>(4, __d.count())};
995 weekday(unsigned __wd) noexcept
996 : _M_wd(__wd == 7 ? 0 : __wd) // __wd % 7 ?
1000 weekday(const sys_days& __dp) noexcept
1001 : weekday{_S_from_days(__dp.time_since_epoch())}
1005 weekday(const local_days& __dp) noexcept
1006 : weekday{sys_days{__dp.time_since_epoch()}}
1010 operator++() noexcept
1017 operator++(int) noexcept
1025 operator--() noexcept
1032 operator--(int) noexcept
1040 operator+=(const days& __d) noexcept
1042 *this = *this + __d;
1047 operator-=(const days& __d) noexcept
1049 *this = *this - __d;
1054 c_encoding() const noexcept
1058 iso_encoding() const noexcept
1059 { return _M_wd == 0u ? 7u : _M_wd; }
1063 { return _M_wd <= 6; }
1065 constexpr weekday_indexed
1066 operator[](unsigned __index) const noexcept;
1068 constexpr weekday_last
1069 operator[](last_spec) const noexcept;
1071 friend constexpr bool
1072 operator==(const weekday& __x, const weekday& __y) noexcept
1073 { return __x._M_wd == __y._M_wd; }
1075 friend constexpr weekday
1076 operator+(const weekday& __x, const days& __y) noexcept
1078 return weekday{__detail::__add_modulo<7>(__x._M_wd, __y.count())};
1081 friend constexpr weekday
1082 operator+(const days& __x, const weekday& __y) noexcept
1083 { return __y + __x; }
1085 friend constexpr weekday
1086 operator-(const weekday& __x, const days& __y) noexcept
1088 return weekday{__detail::__sub_modulo<7>(__x._M_wd, __y.count())};
1091 friend constexpr days
1092 operator-(const weekday& __x, const weekday& __y) noexcept
1094 const auto __n = __x.c_encoding() - __y.c_encoding();
1095 return static_cast<int>(__n) >= 0 ? days{__n} : days{__n + 7};
1099 inline constexpr weekday Sunday{0};
1100 inline constexpr weekday Monday{1};
1101 inline constexpr weekday Tuesday{2};
1102 inline constexpr weekday Wednesday{3};
1103 inline constexpr weekday Thursday{4};
1104 inline constexpr weekday Friday{5};
1105 inline constexpr weekday Saturday{6};
1109 class weekday_indexed
1112 chrono::weekday _M_wd;
1113 unsigned char _M_index;
1116 weekday_indexed() = default;
1119 weekday_indexed(const chrono::weekday& __wd, unsigned __index) noexcept
1120 : _M_wd(__wd), _M_index(__index)
1123 constexpr chrono::weekday
1124 weekday() const noexcept
1128 index() const noexcept
1129 { return _M_index; };
1133 { return _M_wd.ok() && 1 <= _M_index && _M_index <= 5; }
1135 friend constexpr bool
1136 operator==(const weekday_indexed& __x, const weekday_indexed& __y) noexcept
1137 { return __x.weekday() == __y.weekday() && __x.index() == __y.index(); }
1139 friend constexpr month_weekday
1140 operator/(const month& __m, const weekday_indexed& __wdi) noexcept;
1142 friend constexpr month_weekday
1143 operator/(int __m, const weekday_indexed& __wdi) noexcept;
1145 friend constexpr month_weekday
1146 operator/(const weekday_indexed& __wdi, const month& __m) noexcept;
1148 friend constexpr month_weekday
1149 operator/(const weekday_indexed& __wdi, int __m) noexcept;
1151 friend constexpr year_month_weekday
1152 operator/(const year_month& __ym, const weekday_indexed& __wdi) noexcept;
1155 constexpr weekday_indexed
1156 weekday::operator[](unsigned __index) const noexcept
1157 { return {*this, __index}; }
1164 chrono::weekday _M_wd;
1168 weekday_last(const chrono::weekday& __wd) noexcept
1172 constexpr chrono::weekday
1173 weekday() const noexcept
1178 { return _M_wd.ok(); }
1180 friend constexpr bool
1181 operator==(const weekday_last& __x, const weekday_last& __y) noexcept
1182 { return __x.weekday() == __y.weekday(); }
1184 friend constexpr month_weekday_last
1185 operator/(int __m, const weekday_last& __wdl) noexcept;
1187 friend constexpr month_weekday_last
1188 operator/(const weekday_last& __wdl, int __m) noexcept;
1190 friend constexpr year_month_weekday_last
1191 operator/(const year_month& __ym, const weekday_last& __wdl) noexcept;
1194 constexpr weekday_last
1195 weekday::operator[](last_spec) const noexcept
1196 { return weekday_last{*this}; }
1207 month_day() = default;
1210 month_day(const chrono::month& __m, const chrono::day& __d) noexcept
1211 : _M_m{__m}, _M_d{__d}
1214 constexpr chrono::month
1215 month() const noexcept
1218 constexpr chrono::day
1219 day() const noexcept
1226 && 1u <= unsigned(_M_d)
1227 && unsigned(_M_d) <= __detail::__days_per_month[unsigned(_M_m) - 1];
1230 friend constexpr bool
1231 operator==(const month_day& __x, const month_day& __y) noexcept
1232 { return __x.month() == __y.month() && __x.day() == __y.day(); }
1234 friend constexpr strong_ordering
1235 operator<=>(const month_day& __x, const month_day& __y) noexcept
1238 friend constexpr month_day
1239 operator/(const chrono::month& __m, const chrono::day& __d) noexcept
1240 { return {__m, __d}; }
1242 friend constexpr month_day
1243 operator/(const chrono::month& __m, int __d) noexcept
1244 { return {__m, chrono::day(unsigned(__d))}; }
1246 friend constexpr month_day
1247 operator/(int __m, const chrono::day& __d) noexcept
1248 { return {chrono::month(unsigned(__m)), __d}; }
1250 friend constexpr month_day
1251 operator/(const chrono::day& __d, const chrono::month& __m) noexcept
1252 { return {__m, __d}; }
1254 friend constexpr month_day
1255 operator/(const chrono::day& __d, int __m) noexcept
1256 { return {chrono::month(unsigned(__m)), __d}; }
1258 friend constexpr year_month_day
1259 operator/(int __y, const month_day& __md) noexcept;
1261 friend constexpr year_month_day
1262 operator/(const month_day& __md, int __y) noexcept;
1267 class month_day_last
1274 month_day_last(const chrono::month& __m) noexcept
1278 constexpr chrono::month
1279 month() const noexcept
1284 { return _M_m.ok(); }
1286 friend constexpr bool
1287 operator==(const month_day_last& __x, const month_day_last& __y) noexcept
1288 { return __x.month() == __y.month(); }
1290 friend constexpr strong_ordering
1291 operator<=>(const month_day_last& __x, const month_day_last& __y) noexcept
1294 friend constexpr month_day_last
1295 operator/(const chrono::month& __m, last_spec) noexcept
1296 { return month_day_last{__m}; }
1298 friend constexpr month_day_last
1299 operator/(int __m, last_spec) noexcept
1300 { return chrono::month(unsigned(__m)) / last; }
1302 friend constexpr month_day_last
1303 operator/(last_spec, const chrono::month& __m) noexcept
1304 { return __m / last; }
1306 friend constexpr month_day_last
1307 operator/(last_spec, int __m) noexcept
1308 { return __m / last; }
1310 friend constexpr year_month_day_last
1311 operator/(int __y, const month_day_last& __mdl) noexcept;
1313 friend constexpr year_month_day_last
1314 operator/(const month_day_last& __mdl, int __y) noexcept;
1323 chrono::weekday_indexed _M_wdi;
1327 month_weekday(const chrono::month& __m,
1328 const chrono::weekday_indexed& __wdi) noexcept
1329 : _M_m{__m}, _M_wdi{__wdi}
1332 constexpr chrono::month
1333 month() const noexcept
1336 constexpr chrono::weekday_indexed
1337 weekday_indexed() const noexcept
1342 { return _M_m.ok() && _M_wdi.ok(); }
1344 friend constexpr bool
1345 operator==(const month_weekday& __x, const month_weekday& __y) noexcept
1347 return __x.month() == __y.month()
1348 && __x.weekday_indexed() == __y.weekday_indexed();
1351 friend constexpr month_weekday
1352 operator/(const chrono::month& __m,
1353 const chrono::weekday_indexed& __wdi) noexcept
1354 { return {__m, __wdi}; }
1356 friend constexpr month_weekday
1357 operator/(int __m, const chrono::weekday_indexed& __wdi) noexcept
1358 { return chrono::month(unsigned(__m)) / __wdi; }
1360 friend constexpr month_weekday
1361 operator/(const chrono::weekday_indexed& __wdi,
1362 const chrono::month& __m) noexcept
1363 { return __m / __wdi; }
1365 friend constexpr month_weekday
1366 operator/(const chrono::weekday_indexed& __wdi, int __m) noexcept
1367 { return __m / __wdi; }
1369 friend constexpr year_month_weekday
1370 operator/(int __y, const month_weekday& __mwd) noexcept;
1372 friend constexpr year_month_weekday
1373 operator/(const month_weekday& __mwd, int __y) noexcept;
1376 // MONTH_WEEKDAY_LAST
1378 class month_weekday_last
1382 chrono::weekday_last _M_wdl;
1386 month_weekday_last(const chrono::month& __m,
1387 const chrono::weekday_last& __wdl) noexcept
1388 :_M_m{__m}, _M_wdl{__wdl}
1391 constexpr chrono::month
1392 month() const noexcept
1395 constexpr chrono::weekday_last
1396 weekday_last() const noexcept
1401 { return _M_m.ok() && _M_wdl.ok(); }
1403 friend constexpr bool
1404 operator==(const month_weekday_last& __x,
1405 const month_weekday_last& __y) noexcept
1407 return __x.month() == __y.month()
1408 && __x.weekday_last() == __y.weekday_last();
1411 friend constexpr month_weekday_last
1412 operator/(const chrono::month& __m,
1413 const chrono::weekday_last& __wdl) noexcept
1414 { return {__m, __wdl}; }
1416 friend constexpr month_weekday_last
1417 operator/(int __m, const chrono::weekday_last& __wdl) noexcept
1418 { return chrono::month(unsigned(__m)) / __wdl; }
1420 friend constexpr month_weekday_last
1421 operator/(const chrono::weekday_last& __wdl,
1422 const chrono::month& __m) noexcept
1423 { return __m / __wdl; }
1425 friend constexpr month_weekday_last
1426 operator/(const chrono::weekday_last& __wdl, int __m) noexcept
1427 { return chrono::month(unsigned(__m)) / __wdl; }
1429 friend constexpr year_month_weekday_last
1430 operator/(int __y, const month_weekday_last& __mwdl) noexcept;
1432 friend constexpr year_month_weekday_last
1433 operator/(const month_weekday_last& __mwdl, int __y) noexcept;
1440 // [time.cal.ym], [time.cal.ymd], etc constrain the 'months'-based
1441 // addition/subtraction operator overloads like so:
1443 // Constraints: if the argument supplied by the caller for the months
1444 // parameter is convertible to years, its implicit conversion sequence
1445 // to years is worse than its implicit conversion sequence to months.
1447 // We realize this constraint by templatizing the 'months'-based
1448 // overloads (using a dummy defaulted template parameter), so that
1449 // overload resolution doesn't select the 'months'-based overload unless
1450 // the implicit conversion sequence to 'months' is better than that to
1452 using __months_years_conversion_disambiguator = void;
1462 year_month() = default;
1465 year_month(const chrono::year& __y, const chrono::month& __m) noexcept
1466 : _M_y{__y}, _M_m{__m}
1469 constexpr chrono::year
1470 year() const noexcept
1473 constexpr chrono::month
1474 month() const noexcept
1477 template<typename = __detail::__months_years_conversion_disambiguator>
1478 constexpr year_month&
1479 operator+=(const months& __dm) noexcept
1481 *this = *this + __dm;
1485 template<typename = __detail::__months_years_conversion_disambiguator>
1486 constexpr year_month&
1487 operator-=(const months& __dm) noexcept
1489 *this = *this - __dm;
1493 constexpr year_month&
1494 operator+=(const years& __dy) noexcept
1496 *this = *this + __dy;
1500 constexpr year_month&
1501 operator-=(const years& __dy) noexcept
1503 *this = *this - __dy;
1509 { return _M_y.ok() && _M_m.ok(); }
1511 friend constexpr bool
1512 operator==(const year_month& __x, const year_month& __y) noexcept
1513 { return __x.year() == __y.year() && __x.month() == __y.month(); }
1515 friend constexpr strong_ordering
1516 operator<=>(const year_month& __x, const year_month& __y) noexcept
1519 template<typename = __detail::__months_years_conversion_disambiguator>
1520 friend constexpr year_month
1521 operator+(const year_month& __ym, const months& __dm) noexcept
1524 auto __m = __ym.month() + __dm;
1525 auto __i = int(unsigned(__ym.month())) - 1 + __dm.count();
1527 ? __ym.year() + years{(__i - 11) / 12}
1528 : __ym.year() + years{__i / 12});
1532 template<typename = __detail::__months_years_conversion_disambiguator>
1533 friend constexpr year_month
1534 operator+(const months& __dm, const year_month& __ym) noexcept
1535 { return __ym + __dm; }
1537 template<typename = __detail::__months_years_conversion_disambiguator>
1538 friend constexpr year_month
1539 operator-(const year_month& __ym, const months& __dm) noexcept
1540 { return __ym + -__dm; }
1542 friend constexpr months
1543 operator-(const year_month& __x, const year_month& __y) noexcept
1545 return (__x.year() - __y.year()
1546 + months{static_cast<int>(unsigned{__x.month()})
1547 - static_cast<int>(unsigned{__y.month()})});
1550 friend constexpr year_month
1551 operator+(const year_month& __ym, const years& __dy) noexcept
1552 { return (__ym.year() + __dy) / __ym.month(); }
1554 friend constexpr year_month
1555 operator+(const years& __dy, const year_month& __ym) noexcept
1556 { return __ym + __dy; }
1558 friend constexpr year_month
1559 operator-(const year_month& __ym, const years& __dy) noexcept
1560 { return __ym + -__dy; }
1562 friend constexpr year_month
1563 operator/(const chrono::year& __y, const chrono::month& __m) noexcept
1564 { return {__y, __m}; }
1566 friend constexpr year_month
1567 operator/(const chrono::year& __y, int __m) noexcept
1568 { return {__y, chrono::month(unsigned(__m))}; }
1570 friend constexpr year_month_day
1571 operator/(const year_month& __ym, int __d) noexcept;
1573 friend constexpr year_month_day_last
1574 operator/(const year_month& __ym, last_spec) noexcept;
1579 class year_month_day
1586 static constexpr year_month_day _S_from_days(const days& __dp) noexcept;
1588 constexpr days _M_days_since_epoch() const noexcept;
1591 year_month_day() = default;
1594 year_month_day(const chrono::year& __y, const chrono::month& __m,
1595 const chrono::day& __d) noexcept
1596 : _M_y{__y}, _M_m{__m}, _M_d{__d}
1600 year_month_day(const year_month_day_last& __ymdl) noexcept;
1603 year_month_day(const sys_days& __dp) noexcept
1604 : year_month_day(_S_from_days(__dp.time_since_epoch()))
1608 year_month_day(const local_days& __dp) noexcept
1609 : year_month_day(sys_days{__dp.time_since_epoch()})
1612 template<typename = __detail::__months_years_conversion_disambiguator>
1613 constexpr year_month_day&
1614 operator+=(const months& __m) noexcept
1616 *this = *this + __m;
1620 template<typename = __detail::__months_years_conversion_disambiguator>
1621 constexpr year_month_day&
1622 operator-=(const months& __m) noexcept
1624 *this = *this - __m;
1628 constexpr year_month_day&
1629 operator+=(const years& __y) noexcept
1631 *this = *this + __y;
1635 constexpr year_month_day&
1636 operator-=(const years& __y) noexcept
1638 *this = *this - __y;
1642 constexpr chrono::year
1643 year() const noexcept
1646 constexpr chrono::month
1647 month() const noexcept
1650 constexpr chrono::day
1651 day() const noexcept
1655 operator sys_days() const noexcept
1656 { return sys_days{_M_days_since_epoch()}; }
1659 operator local_days() const noexcept
1660 { return local_days{sys_days{*this}.time_since_epoch()}; }
1662 constexpr bool ok() const noexcept;
1664 friend constexpr bool
1665 operator==(const year_month_day& __x, const year_month_day& __y) noexcept
1667 return __x.year() == __y.year()
1668 && __x.month() == __y.month()
1669 && __x.day() == __y.day();
1672 friend constexpr strong_ordering
1673 operator<=>(const year_month_day& __x, const year_month_day& __y) noexcept
1676 template<typename = __detail::__months_years_conversion_disambiguator>
1677 friend constexpr year_month_day
1678 operator+(const year_month_day& __ymd, const months& __dm) noexcept
1679 { return (__ymd.year() / __ymd.month() + __dm) / __ymd.day(); }
1681 template<typename = __detail::__months_years_conversion_disambiguator>
1682 friend constexpr year_month_day
1683 operator+(const months& __dm, const year_month_day& __ymd) 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.year() + __dy) / __ymd.month() / __ymd.day(); }
1690 friend constexpr year_month_day
1691 operator+(const years& __dy, const year_month_day& __ymd) noexcept
1692 { return __ymd + __dy; }
1694 template<typename = __detail::__months_years_conversion_disambiguator>
1695 friend constexpr year_month_day
1696 operator-(const year_month_day& __ymd, const months& __dm) noexcept
1697 { return __ymd + -__dm; }
1699 friend constexpr year_month_day
1700 operator-(const year_month_day& __ymd, const years& __dy) noexcept
1701 { return __ymd + -__dy; }
1703 friend constexpr year_month_day
1704 operator/(const year_month& __ym, const chrono::day& __d) noexcept
1705 { return {__ym.year(), __ym.month(), __d}; }
1707 friend constexpr year_month_day
1708 operator/(const year_month& __ym, int __d) noexcept
1709 { return __ym / chrono::day{unsigned(__d)}; }
1711 friend constexpr year_month_day
1712 operator/(const chrono::year& __y, const month_day& __md) noexcept
1713 { return __y / __md.month() / __md.day(); }
1715 friend constexpr year_month_day
1716 operator/(int __y, const month_day& __md) noexcept
1717 { return chrono::year{__y} / __md; }
1719 friend constexpr year_month_day
1720 operator/(const month_day& __md, const chrono::year& __y) noexcept
1721 { return __y / __md; }
1723 friend constexpr year_month_day
1724 operator/(const month_day& __md, int __y) noexcept
1725 { return chrono::year(__y) / __md; }
1728 // Construct from days since 1970/01/01.
1729 // Proposition 6.3 of Neri and Schneider,
1730 // "Euclidean Affine Functions and Applications to Calendar Algorithms".
1731 // https://arxiv.org/abs/2102.06959
1732 constexpr year_month_day
1733 year_month_day::_S_from_days(const days& __dp) noexcept
1735 constexpr auto __z2 = static_cast<uint32_t>(-1468000);
1736 constexpr auto __r2_e3 = static_cast<uint32_t>(536895458);
1738 const auto __r0 = static_cast<uint32_t>(__dp.count()) + __r2_e3;
1740 const auto __n1 = 4 * __r0 + 3;
1741 const auto __q1 = __n1 / 146097;
1742 const auto __r1 = __n1 % 146097 / 4;
1744 constexpr auto __p32 = static_cast<uint64_t>(1) << 32;
1745 const auto __n2 = 4 * __r1 + 3;
1746 const auto __u2 = static_cast<uint64_t>(2939745) * __n2;
1747 const auto __q2 = static_cast<uint32_t>(__u2 / __p32);
1748 const auto __r2 = static_cast<uint32_t>(__u2 % __p32) / 2939745 / 4;
1750 constexpr auto __p16 = static_cast<uint32_t>(1) << 16;
1751 const auto __n3 = 2141 * __r2 + 197913;
1752 const auto __q3 = __n3 / __p16;
1753 const auto __r3 = __n3 % __p16 / 2141;
1755 const auto __y0 = 100 * __q1 + __q2;
1756 const auto __m0 = __q3;
1757 const auto __d0 = __r3;
1759 const auto __j = __r2 >= 306;
1760 const auto __y1 = __y0 + __j;
1761 const auto __m1 = __j ? __m0 - 12 : __m0;
1762 const auto __d1 = __d0 + 1;
1764 return year_month_day{chrono::year{static_cast<int>(__y1 + __z2)},
1765 chrono::month{__m1}, chrono::day{__d1}};
1768 // Days since 1970/01/01.
1769 // Proposition 6.2 of Neri and Schneider,
1770 // "Euclidean Affine Functions and Applications to Calendar Algorithms".
1771 // https://arxiv.org/abs/2102.06959
1773 year_month_day::_M_days_since_epoch() const noexcept
1775 auto constexpr __z2 = static_cast<uint32_t>(-1468000);
1776 auto constexpr __r2_e3 = static_cast<uint32_t>(536895458);
1778 const auto __y1 = static_cast<uint32_t>(static_cast<int>(_M_y)) - __z2;
1779 const auto __m1 = static_cast<uint32_t>(static_cast<unsigned>(_M_m));
1780 const auto __d1 = static_cast<uint32_t>(static_cast<unsigned>(_M_d));
1782 const auto __j = static_cast<uint32_t>(__m1 < 3);
1783 const auto __y0 = __y1 - __j;
1784 const auto __m0 = __j ? __m1 + 12 : __m1;
1785 const auto __d0 = __d1 - 1;
1787 const auto __q1 = __y0 / 100;
1788 const auto __yc = 1461 * __y0 / 4 - __q1 + __q1 / 4;
1789 const auto __mc = (979 *__m0 - 2919) / 32;
1790 const auto __dc = __d0;
1792 return days{static_cast<int32_t>(__yc + __mc + __dc - __r2_e3)};
1795 // YEAR_MONTH_DAY_LAST
1797 class year_month_day_last
1801 chrono::month_day_last _M_mdl;
1805 year_month_day_last(const chrono::year& __y,
1806 const chrono::month_day_last& __mdl) noexcept
1807 : _M_y{__y}, _M_mdl{__mdl}
1810 template<typename = __detail::__months_years_conversion_disambiguator>
1811 constexpr year_month_day_last&
1812 operator+=(const months& __m) noexcept
1814 *this = *this + __m;
1818 template<typename = __detail::__months_years_conversion_disambiguator>
1819 constexpr year_month_day_last&
1820 operator-=(const months& __m) noexcept
1822 *this = *this - __m;
1826 constexpr year_month_day_last&
1827 operator+=(const years& __y) noexcept
1829 *this = *this + __y;
1833 constexpr year_month_day_last&
1834 operator-=(const years& __y) noexcept
1836 *this = *this - __y;
1840 constexpr chrono::year
1841 year() const noexcept
1844 constexpr chrono::month
1845 month() const noexcept
1846 { return _M_mdl.month(); }
1848 constexpr chrono::month_day_last
1849 month_day_last() const noexcept
1852 // Return A day representing the last day of this year, month pair.
1853 constexpr chrono::day
1854 day() const noexcept
1856 const auto __m = static_cast<unsigned>(month());
1858 // The result is unspecified if __m < 1 or __m > 12. Hence, assume
1859 // 1 <= __m <= 12. For __m != 2, day() == 30 or day() == 31 or, in
1860 // other words, day () == 30 | b, where b is in {0, 1}.
1862 // If __m in {1, 3, 4, 5, 6, 7}, then b is 1 if, and only if, __m is
1863 // odd. Hence, b = __m & 1 = (__m ^ 0) & 1.
1865 // If __m in {8, 9, 10, 11, 12}, then b is 1 if, and only if, __m is
1866 // even. Hence, b = (__m ^ 1) & 1.
1868 // Therefore, b = (__m ^ c) & 1, where c = 0, if __m < 8, or c = 1 if
1869 // __m >= 8, that is, c = __m >> 3.
1871 // Since 30 = (11110)_2 and __m <= 31 = (11111)_2, the "& 1" in b's
1872 // calculation is unnecessary.
1874 // The performance of this implementation does not depend on look-up
1875 // tables being on the L1 cache.
1876 return chrono::day{__m != 2 ? (__m ^ (__m >> 3)) | 30
1877 : _M_y.is_leap() ? 29 : 28};
1881 operator sys_days() const noexcept
1882 { return sys_days{year() / month() / day()}; }
1885 operator local_days() const noexcept
1886 { return local_days{sys_days{*this}.time_since_epoch()}; }
1890 { return _M_y.ok() && _M_mdl.ok(); }
1892 friend constexpr bool
1893 operator==(const year_month_day_last& __x,
1894 const year_month_day_last& __y) noexcept
1896 return __x.year() == __y.year()
1897 && __x.month_day_last() == __y.month_day_last();
1900 friend constexpr strong_ordering
1901 operator<=>(const year_month_day_last& __x,
1902 const year_month_day_last& __y) noexcept
1905 template<typename = __detail::__months_years_conversion_disambiguator>
1906 friend constexpr year_month_day_last
1907 operator+(const year_month_day_last& __ymdl,
1908 const months& __dm) noexcept
1909 { return (__ymdl.year() / __ymdl.month() + __dm) / last; }
1911 template<typename = __detail::__months_years_conversion_disambiguator>
1912 friend constexpr year_month_day_last
1913 operator+(const months& __dm,
1914 const year_month_day_last& __ymdl) noexcept
1915 { return __ymdl + __dm; }
1917 template<typename = __detail::__months_years_conversion_disambiguator>
1918 friend constexpr year_month_day_last
1919 operator-(const year_month_day_last& __ymdl,
1920 const months& __dm) noexcept
1921 { return __ymdl + -__dm; }
1923 friend constexpr year_month_day_last
1924 operator+(const year_month_day_last& __ymdl,
1925 const years& __dy) noexcept
1926 { return {__ymdl.year() + __dy, __ymdl.month_day_last()}; }
1928 friend constexpr year_month_day_last
1929 operator+(const years& __dy,
1930 const year_month_day_last& __ymdl) noexcept
1931 { return __ymdl + __dy; }
1933 friend constexpr year_month_day_last
1934 operator-(const year_month_day_last& __ymdl,
1935 const years& __dy) noexcept
1936 { return __ymdl + -__dy; }
1938 friend constexpr year_month_day_last
1939 operator/(const year_month& __ym, last_spec) noexcept
1940 { return {__ym.year(), chrono::month_day_last{__ym.month()}}; }
1942 friend constexpr year_month_day_last
1943 operator/(const chrono::year& __y,
1944 const chrono::month_day_last& __mdl) noexcept
1945 { return {__y, __mdl}; }
1947 friend constexpr year_month_day_last
1948 operator/(int __y, const chrono::month_day_last& __mdl) noexcept
1949 { return chrono::year(__y) / __mdl; }
1951 friend constexpr year_month_day_last
1952 operator/(const chrono::month_day_last& __mdl,
1953 const chrono::year& __y) noexcept
1954 { return __y / __mdl; }
1956 friend constexpr year_month_day_last
1957 operator/(const chrono::month_day_last& __mdl, int __y) noexcept
1958 { return chrono::year(__y) / __mdl; }
1961 // year_month_day ctor from year_month_day_last
1963 year_month_day::year_month_day(const year_month_day_last& __ymdl) noexcept
1964 : _M_y{__ymdl.year()}, _M_m{__ymdl.month()}, _M_d{__ymdl.day()}
1968 year_month_day::ok() const noexcept
1970 if (!_M_y.ok() || !_M_m.ok())
1972 return chrono::day{1} <= _M_d && _M_d <= (_M_y / _M_m / last).day();
1975 // YEAR_MONTH_WEEKDAY
1977 class year_month_weekday
1982 chrono::weekday_indexed _M_wdi;
1984 static constexpr year_month_weekday
1985 _S_from_sys_days(const sys_days& __dp)
1987 year_month_day __ymd{__dp};
1988 chrono::weekday __wd{__dp};
1989 auto __index = __wd[(unsigned{__ymd.day()} - 1) / 7 + 1];
1990 return {__ymd.year(), __ymd.month(), __index};
1994 year_month_weekday() = default;
1997 year_month_weekday(const chrono::year& __y, const chrono::month& __m,
1998 const chrono::weekday_indexed& __wdi) noexcept
1999 : _M_y{__y}, _M_m{__m}, _M_wdi{__wdi}
2003 year_month_weekday(const sys_days& __dp) noexcept
2004 : year_month_weekday{_S_from_sys_days(__dp)}
2008 year_month_weekday(const local_days& __dp) noexcept
2009 : year_month_weekday{sys_days{__dp.time_since_epoch()}}
2012 template<typename = __detail::__months_years_conversion_disambiguator>
2013 constexpr year_month_weekday&
2014 operator+=(const months& __m) noexcept
2016 *this = *this + __m;
2020 template<typename = __detail::__months_years_conversion_disambiguator>
2021 constexpr year_month_weekday&
2022 operator-=(const months& __m) noexcept
2024 *this = *this - __m;
2028 constexpr year_month_weekday&
2029 operator+=(const years& __y) noexcept
2031 *this = *this + __y;
2035 constexpr year_month_weekday&
2036 operator-=(const years& __y) noexcept
2038 *this = *this - __y;
2042 constexpr chrono::year
2043 year() const noexcept
2046 constexpr chrono::month
2047 month() const noexcept
2050 constexpr chrono::weekday
2051 weekday() const noexcept
2052 { return _M_wdi.weekday(); }
2055 index() const noexcept
2056 { return _M_wdi.index(); }
2058 constexpr chrono::weekday_indexed
2059 weekday_indexed() const noexcept
2063 operator sys_days() const noexcept
2065 auto __d = sys_days{year() / month() / 1};
2066 return __d + (weekday() - chrono::weekday(__d)
2067 + days{(static_cast<int>(index())-1)*7});
2071 operator local_days() const noexcept
2072 { return local_days{sys_days{*this}.time_since_epoch()}; }
2077 if (!_M_y.ok() || !_M_m.ok() || !_M_wdi.ok())
2079 if (_M_wdi.index() <= 4)
2081 days __d = (_M_wdi.weekday()
2082 - chrono::weekday{sys_days{_M_y / _M_m / 1}}
2083 + days((_M_wdi.index()-1)*7 + 1));
2084 __glibcxx_assert(__d.count() >= 1);
2085 return (unsigned)__d.count() <= (unsigned)(_M_y / _M_m / last).day();
2088 friend constexpr bool
2089 operator==(const year_month_weekday& __x,
2090 const year_month_weekday& __y) noexcept
2092 return __x.year() == __y.year()
2093 && __x.month() == __y.month()
2094 && __x.weekday_indexed() == __y.weekday_indexed();
2097 template<typename = __detail::__months_years_conversion_disambiguator>
2098 friend constexpr year_month_weekday
2099 operator+(const year_month_weekday& __ymwd, const months& __dm) noexcept
2101 return ((__ymwd.year() / __ymwd.month() + __dm)
2102 / __ymwd.weekday_indexed());
2105 template<typename = __detail::__months_years_conversion_disambiguator>
2106 friend constexpr year_month_weekday
2107 operator+(const months& __dm, const year_month_weekday& __ymwd) 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.year() + __dy, __ymwd.month(), __ymwd.weekday_indexed()}; }
2114 friend constexpr year_month_weekday
2115 operator+(const years& __dy, const year_month_weekday& __ymwd) noexcept
2116 { return __ymwd + __dy; }
2118 template<typename = __detail::__months_years_conversion_disambiguator>
2119 friend constexpr year_month_weekday
2120 operator-(const year_month_weekday& __ymwd, const months& __dm) noexcept
2121 { return __ymwd + -__dm; }
2123 friend constexpr year_month_weekday
2124 operator-(const year_month_weekday& __ymwd, const years& __dy) noexcept
2125 { return __ymwd + -__dy; }
2127 friend constexpr year_month_weekday
2128 operator/(const year_month& __ym,
2129 const chrono::weekday_indexed& __wdi) noexcept
2130 { return {__ym.year(), __ym.month(), __wdi}; }
2132 friend constexpr year_month_weekday
2133 operator/(const chrono::year& __y, const month_weekday& __mwd) noexcept
2134 { return {__y, __mwd.month(), __mwd.weekday_indexed()}; }
2136 friend constexpr year_month_weekday
2137 operator/(int __y, const month_weekday& __mwd) noexcept
2138 { return chrono::year(__y) / __mwd; }
2140 friend constexpr year_month_weekday
2141 operator/(const month_weekday& __mwd, const chrono::year& __y) noexcept
2142 { return __y / __mwd; }
2144 friend constexpr year_month_weekday
2145 operator/(const month_weekday& __mwd, int __y) noexcept
2146 { return chrono::year(__y) / __mwd; }
2149 // YEAR_MONTH_WEEKDAY_LAST
2151 class year_month_weekday_last
2156 chrono::weekday_last _M_wdl;
2160 year_month_weekday_last(const chrono::year& __y, const chrono::month& __m,
2161 const chrono::weekday_last& __wdl) noexcept
2162 : _M_y{__y}, _M_m{__m}, _M_wdl{__wdl}
2165 template<typename = __detail::__months_years_conversion_disambiguator>
2166 constexpr year_month_weekday_last&
2167 operator+=(const months& __m) noexcept
2169 *this = *this + __m;
2173 template<typename = __detail::__months_years_conversion_disambiguator>
2174 constexpr year_month_weekday_last&
2175 operator-=(const months& __m) noexcept
2177 *this = *this - __m;
2181 constexpr year_month_weekday_last&
2182 operator+=(const years& __y) noexcept
2184 *this = *this + __y;
2188 constexpr year_month_weekday_last&
2189 operator-=(const years& __y) noexcept
2191 *this = *this - __y;
2195 constexpr chrono::year
2196 year() const noexcept
2199 constexpr chrono::month
2200 month() const noexcept
2203 constexpr chrono::weekday
2204 weekday() const noexcept
2205 { return _M_wdl.weekday(); }
2207 constexpr chrono::weekday_last
2208 weekday_last() const noexcept
2212 operator sys_days() const noexcept
2214 const auto __d = sys_days{_M_y / _M_m / last};
2215 return sys_days{(__d - (chrono::weekday{__d}
2216 - _M_wdl.weekday())).time_since_epoch()};
2220 operator local_days() const noexcept
2221 { return local_days{sys_days{*this}.time_since_epoch()}; }
2225 { return _M_y.ok() && _M_m.ok() && _M_wdl.ok(); }
2227 friend constexpr bool
2228 operator==(const year_month_weekday_last& __x,
2229 const year_month_weekday_last& __y) noexcept
2231 return __x.year() == __y.year()
2232 && __x.month() == __y.month()
2233 && __x.weekday_last() == __y.weekday_last();
2236 template<typename = __detail::__months_years_conversion_disambiguator>
2237 friend constexpr year_month_weekday_last
2238 operator+(const year_month_weekday_last& __ymwdl,
2239 const months& __dm) noexcept
2241 return ((__ymwdl.year() / __ymwdl.month() + __dm)
2242 / __ymwdl.weekday_last());
2245 template<typename = __detail::__months_years_conversion_disambiguator>
2246 friend constexpr year_month_weekday_last
2247 operator+(const months& __dm,
2248 const year_month_weekday_last& __ymwdl) noexcept
2249 { return __ymwdl + __dm; }
2251 friend constexpr year_month_weekday_last
2252 operator+(const year_month_weekday_last& __ymwdl,
2253 const years& __dy) noexcept
2254 { return {__ymwdl.year() + __dy, __ymwdl.month(), __ymwdl.weekday_last()}; }
2256 friend constexpr year_month_weekday_last
2257 operator+(const years& __dy,
2258 const year_month_weekday_last& __ymwdl) noexcept
2259 { return __ymwdl + __dy; }
2261 template<typename = __detail::__months_years_conversion_disambiguator>
2262 friend constexpr year_month_weekday_last
2263 operator-(const year_month_weekday_last& __ymwdl,
2264 const months& __dm) noexcept
2265 { return __ymwdl + -__dm; }
2267 friend constexpr year_month_weekday_last
2268 operator-(const year_month_weekday_last& __ymwdl,
2269 const years& __dy) noexcept
2270 { return __ymwdl + -__dy; }
2272 friend constexpr year_month_weekday_last
2273 operator/(const year_month& __ym,
2274 const chrono::weekday_last& __wdl) noexcept
2275 { return {__ym.year(), __ym.month(), __wdl}; }
2277 friend constexpr year_month_weekday_last
2278 operator/(const chrono::year& __y,
2279 const chrono::month_weekday_last& __mwdl) noexcept
2280 { return {__y, __mwdl.month(), __mwdl.weekday_last()}; }
2282 friend constexpr year_month_weekday_last
2283 operator/(int __y, const chrono::month_weekday_last& __mwdl) noexcept
2284 { return chrono::year(__y) / __mwdl; }
2286 friend constexpr year_month_weekday_last
2287 operator/(const chrono::month_weekday_last& __mwdl,
2288 const chrono::year& __y) noexcept
2289 { return __y / __mwdl; }
2291 friend constexpr year_month_weekday_last
2292 operator/(const chrono::month_weekday_last& __mwdl, int __y) noexcept
2293 { return chrono::year(__y) / __mwdl; }
2298 /// @cond undocumented
2302 __pow10(unsigned __n)
2312 /** Utility for splitting a duration into hours, minutes, and seconds
2314 * This is a convenience type that provides accessors for the constituent
2315 * parts (hours, minutes, seconds and subseconds) of a duration.
2319 template<typename _Duration>
2322 static_assert( __is_duration<_Duration>::value );
2325 static consteval int
2326 _S_fractional_width()
2328 auto __den = _Duration::period::den;
2329 const int __multiplicity_2 = std::__countr_zero((uintmax_t)__den);
2330 __den >>= __multiplicity_2;
2331 int __multiplicity_5 = 0;
2332 while ((__den % 5) == 0)
2340 int __width = (__multiplicity_2 > __multiplicity_5
2341 ? __multiplicity_2 : __multiplicity_5);
2348 hh_mm_ss(_Duration __d, bool __is_neg)
2349 : _M_h (duration_cast<chrono::hours>(__d)),
2350 _M_m (duration_cast<chrono::minutes>(__d - hours())),
2351 _M_s (duration_cast<chrono::seconds>(__d - hours() - minutes())),
2354 auto __ss = __d - hours() - minutes() - seconds();
2355 if constexpr (treat_as_floating_point_v<typename precision::rep>)
2356 _M_ss._M_r = __ss.count();
2357 else if constexpr (precision::period::den != 1)
2358 _M_ss._M_r = duration_cast<precision>(__ss).count();
2361 static constexpr _Duration
2362 _S_abs(_Duration __d)
2364 if constexpr (numeric_limits<typename _Duration::rep>::is_signed)
2365 return chrono::abs(__d);
2371 static constexpr unsigned fractional_width = {_S_fractional_width()};
2374 = duration<common_type_t<typename _Duration::rep,
2375 chrono::seconds::rep>,
2376 ratio<1, __detail::__pow10(fractional_width)>>;
2378 constexpr hh_mm_ss() noexcept = default;
2381 hh_mm_ss(_Duration __d)
2382 : hh_mm_ss(_S_abs(__d), __d < _Duration::zero())
2386 is_negative() const noexcept
2388 if constexpr (!_S_is_unsigned)
2394 constexpr chrono::hours
2395 hours() const noexcept
2398 constexpr chrono::minutes
2399 minutes() const noexcept
2402 constexpr chrono::seconds
2403 seconds() const noexcept
2407 subseconds() const noexcept
2408 { return static_cast<precision>(_M_ss); }
2411 operator precision() const noexcept
2412 { return to_duration(); }
2415 to_duration() const noexcept
2417 if constexpr (!_S_is_unsigned)
2419 return -(_M_h + _M_m + _M_s + subseconds());
2420 return _M_h + _M_m + _M_s + subseconds();
2424 static constexpr bool _S_is_unsigned
2425 = __and_v<is_integral<typename _Duration::rep>,
2426 is_unsigned<typename _Duration::rep>>;
2428 template<typename _Ratio>
2429 using __byte_duration = duration<unsigned char, _Ratio>;
2431 // The type of the _M_ss member that holds the subsecond precision.
2432 template<typename _Dur>
2435 typename _Dur::rep _M_r{};
2438 operator _Dur() const noexcept
2439 { return _Dur(_M_r); }
2442 // An empty class if this precision doesn't need subseconds.
2443 template<typename _Rep>
2444 requires (!treat_as_floating_point_v<_Rep>)
2445 struct __subseconds<duration<_Rep, ratio<1>>>
2448 operator duration<_Rep, ratio<1>>() const noexcept
2452 template<typename _Rep, typename _Period>
2453 requires (!treat_as_floating_point_v<_Rep>)
2454 && ratio_less_v<_Period, ratio<1, 1>>
2455 && ratio_greater_equal_v<_Period, ratio<1, 250>>
2456 struct __subseconds<duration<_Rep, _Period>>
2458 unsigned char _M_r{};
2461 operator duration<_Rep, _Period>() const noexcept
2462 { return duration<_Rep, _Period>(_M_r); }
2465 template<typename _Rep, typename _Period>
2466 requires (!treat_as_floating_point_v<_Rep>)
2467 && ratio_less_v<_Period, ratio<1, 250>>
2468 && ratio_greater_equal_v<_Period, ratio<1, 4000000000>>
2469 struct __subseconds<duration<_Rep, _Period>>
2471 uint_least32_t _M_r{};
2474 operator duration<_Rep, _Period>() const noexcept
2475 { return duration<_Rep, _Period>(_M_r); }
2478 chrono::hours _M_h{};
2479 __byte_duration<ratio<60>> _M_m{};
2480 __byte_duration<ratio<1>> _M_s{};
2482 __subseconds<precision> _M_ss{};
2485 // 12/24 HOURS FUNCTIONS
2488 is_am(const hours& __h) noexcept
2489 { return 0h <= __h && __h <= 11h; }
2492 is_pm(const hours& __h) noexcept
2493 { return 12h <= __h && __h <= 23h; }
2496 make12(const hours& __h) noexcept
2506 make24(const hours& __h, bool __is_pm) noexcept
2525#if _GLIBCXX_USE_CXX11_ABI || ! _GLIBCXX_USE_DUAL_ABI
2526 // C++20 [time.zones] Time zones
2541 static constexpr int unique = 0;
2542 static constexpr int nonexistent = 1;
2543 static constexpr int ambiguous = 2;
2550 class nonexistent_local_time : public runtime_error
2553 template<typename _Duration>
2554 nonexistent_local_time(const local_time<_Duration>& __tp,
2555 const local_info& __i)
2556 : runtime_error(_S_make_what_str(__tp, __i))
2557 { __glibcxx_assert(__i.result == local_info::nonexistent); }
2560 template<typename _Duration>
2562 _S_make_what_str(const local_time<_Duration>& __tp,
2563 const local_info& __i)
2565 std::ostringstream __os;
2566 __os << __tp << " is in a gap between\n"
2567 << local_seconds(__i.first.end.time_since_epoch())
2568 + __i.first.offset << ' ' << __i.first.abbrev << " and\n"
2569 << local_seconds(__i.second.begin.time_since_epoch())
2570 + __i.second.offset << ' ' << __i.second.abbrev
2571 << " which are both equivalent to\n"
2572 << __i.first.end << " UTC";
2573 return std::move(__os).str();
2577 class ambiguous_local_time : public runtime_error
2580 template<typename _Duration>
2581 ambiguous_local_time(const local_time<_Duration>& __tp,
2582 const local_info& __i)
2583 : runtime_error(_S_make_what_str(__tp, __i))
2584 { __glibcxx_assert(__i.result == local_info::ambiguous); }
2587 template<typename _Duration>
2589 _S_make_what_str(const local_time<_Duration>& __tp,
2590 const local_info& __i)
2592 std::ostringstream __os;
2593 __os << __tp << " is ambiguous. It could be\n"
2594 << __tp << ' ' << __i.first.abbrev << " == "
2595 << __tp - __i.first.offset << " UTC or\n"
2596 << __tp << ' ' << __i.second.abbrev << " == "
2597 << __tp - __i.second.offset << " UTC";
2598 return std::move(__os).str();
2602 template<typename _Duration>
2604 __throw_bad_local_time(const local_time<_Duration>& __tp,
2605 const local_info& __i)
2608 if (__i.result == local_info::nonexistent)
2609 throw nonexistent_local_time(__tp, __i);
2610 throw ambiguous_local_time(__tp, __i);
2616 enum class choose { earliest, latest };
2621 time_zone(time_zone&&) = default;
2622 time_zone& operator=(time_zone&&) = default;
2627 string_view name() const noexcept { return _M_name; }
2629 template<typename _Duration>
2631 get_info(const sys_time<_Duration>& __st) const
2632 { return _M_get_sys_info(chrono::floor<seconds>(__st)); }
2634 template<typename _Duration>
2636 get_info(const local_time<_Duration>& __tp) const
2637 { return _M_get_local_info(chrono::floor<seconds>(__tp)); }
2639 template<typename _Duration>
2640 sys_time<common_type_t<_Duration, seconds>>
2641 to_sys(const local_time<_Duration>& __tp) const
2643 local_info __info = get_info(__tp);
2645 if (__info.result != local_info::unique)
2646 __throw_bad_local_time(__tp, __info);
2648 return sys_time<_Duration>(__tp.time_since_epoch())
2649 - __info.first.offset;
2652 template<typename _Duration>
2653 sys_time<common_type_t<_Duration, seconds>>
2654 to_sys(const local_time<_Duration>& __tp, choose __z) const
2656 local_info __info = get_info(__tp);
2658 if (__info.result == local_info::nonexistent)
2659 return __info.first.end; // Last second of the previous sys_info.
2661 sys_time<_Duration> __st(__tp.time_since_epoch());
2663 if (__info.result == local_info::ambiguous && __z == choose::latest)
2664 return __st - __info.second.offset; // Time in the later sys_info.
2665 // else if __z == earliest, use __info.first.offset as below:
2667 return __st - __info.first.offset;
2670 template<typename _Duration>
2671 local_time<common_type_t<_Duration, seconds>>
2672 to_local(const sys_time<_Duration>& __tp) const
2674 auto __d = (__tp + get_info(__tp).offset).time_since_epoch();
2675 return local_time<common_type_t<_Duration, seconds>>(__d);
2678 [[nodiscard]] friend bool
2679 operator==(const time_zone& __x, const time_zone& __y) noexcept
2680 { return __x._M_name == __y._M_name; }
2682 [[nodiscard]] friend strong_ordering
2683 operator<=>(const time_zone& __x, const time_zone& __y) noexcept
2684 { return __x._M_name <=> __y._M_name; }
2687 sys_info _M_get_sys_info(sys_seconds) const;
2688 local_info _M_get_local_info(local_seconds) const;
2690 friend const tzdb& reload_tzdb();
2692 friend class tzdb_list;
2696 explicit time_zone(unique_ptr<_Impl> __p);
2698 unique_ptr<_Impl> _M_impl;
2701 const time_zone* locate_zone(string_view __tz_name);
2702 const time_zone* current_zone();
2704 /** The list of `chrono::tzdb` objects
2706 * A single object of this type is constructed by the C++ runtime,
2707 * and can be accessed by calling `chrono::get_tzdb_list()`.
2709 * The front of the list is the current `tzdb` object and can be accessed
2710 * via `chrono::get_tzdb_list().front()` or `chrono::get_tzdb()` or
2711 * `*chrono::get_tzdb_list().begin()`.
2713 * The `chrono::reload_tzdb()` function will check for a newer version
2714 * and if found, insert it at the front of the list.
2723 tzdb_list(const tzdb_list&) = delete;
2724 tzdb_list& operator=(const tzdb_list&) = delete;
2726 /** An iterator into the `tzdb_list`
2728 * As a extension, in libstdc++ each `tzdb` is reference-counted
2729 * and the `const_iterator` type shares ownership of the object it
2730 * refers to. This ensures that a `tzdb` erased from the list will
2731 * not be destroyed while there is an iterator that refers to it.
2733 class const_iterator
2736 using value_type = tzdb;
2737 using reference = const tzdb&;
2738 using pointer = const tzdb*;
2739 using difference_type = ptrdiff_t;
2740 using iterator_category = forward_iterator_tag;
2742 constexpr const_iterator() = default;
2743 const_iterator(const const_iterator&) = default;
2744 const_iterator(const_iterator&&) = default;
2745 const_iterator& operator=(const const_iterator&) = default;
2746 const_iterator& operator=(const_iterator&&) = default;
2748 reference operator*() const noexcept;
2749 pointer operator->() const noexcept { return &**this; }
2750 const_iterator& operator++();
2751 const_iterator operator++(int);
2753 bool operator==(const const_iterator&) const noexcept = default;
2756 explicit const_iterator(const shared_ptr<_Node>&) noexcept;
2758 friend class tzdb_list;
2760 shared_ptr<_Node> _M_node;
2761 void* _M_reserved = nullptr;
2764 /** Access the current `tzdb` at the front of the list.
2766 * This returns a reference to the same object as `chrono::get_tzdb()`.
2768 * @returns A reference to the current tzdb object.
2771 const tzdb& front() const noexcept;
2773 /** Remove the tzdb object _after_ the one the iterator refers to.
2775 * Calling this function concurrently with any of `front()`, `begin()`,
2776 * or `end()` does not cause a data race, but in general this function
2777 * is not thread-safe. The behaviour may be undefined if erasing an
2778 * element from the list while another thread is calling the same
2779 * function, or incrementing an iterator into the list, or accessing
2780 * the element being erased (unless it is accessed through an iterator).
2782 * @param __p A dereferenceable iterator.
2783 * @returns An iterator the element after the one that was erased
2784 * (or `end()` if there is no such element).
2787 const_iterator erase_after(const_iterator __p);
2789 const_iterator begin() const noexcept;
2790 const_iterator end() const noexcept { return {}; }
2791 const_iterator cbegin() const noexcept { return begin(); }
2792 const_iterator cend() const noexcept { return end(); }
2795 constexpr explicit tzdb_list(nullptr_t);
2797 friend tzdb_list& get_tzdb_list();
2798 friend const tzdb& get_tzdb();
2799 friend const tzdb& reload_tzdb();
2801 friend class leap_second;
2802 friend struct time_zone::_Impl;
2803 friend class time_zone_link;
2806 class time_zone_link
2809 time_zone_link(time_zone_link&&) = default;
2810 time_zone_link& operator=(time_zone_link&&) = default;
2812 string_view name() const noexcept { return _M_name; }
2813 string_view target() const noexcept { return _M_target; }
2816 operator==(const time_zone_link& __x, const time_zone_link& __y) noexcept
2817 { return __x.name() == __y.name(); }
2819 friend strong_ordering
2820 operator<=>(const time_zone_link& __x, const time_zone_link& __y) noexcept
2821 { return __x.name() <=> __y.name(); }
2824 friend const tzdb& reload_tzdb();
2825 friend struct tzdb_list::_Node;
2827 explicit time_zone_link(nullptr_t) { }
2836 leap_second(const leap_second&) = default;
2837 leap_second& operator=(const leap_second&) = default;
2840 constexpr sys_seconds
2841 date() const noexcept
2843 if (_M_s >= _M_s.zero()) [[likely]]
2844 return sys_seconds(_M_s);
2845 return sys_seconds(-_M_s);
2850 value() const noexcept
2852 if (_M_s >= _M_s.zero()) [[likely]]
2857 // This can be defaulted because the database will never contain two
2858 // leap_second objects with the same date but different signs.
2859 [[nodiscard]] friend constexpr bool
2860 operator==(const leap_second&, const leap_second&) noexcept = default;
2862 [[nodiscard]] friend constexpr strong_ordering
2863 operator<=>(const leap_second& __x, const leap_second& __y) noexcept
2864 { return __x.date() <=> __y.date(); }
2866 template<typename _Duration>
2867 [[nodiscard]] friend constexpr bool
2868 operator==(const leap_second& __x,
2869 const sys_time<_Duration>& __y) noexcept
2870 { return __x.date() == __y; }
2872 template<typename _Duration>
2873 [[nodiscard]] friend constexpr bool
2874 operator<(const leap_second& __x,
2875 const sys_time<_Duration>& __y) noexcept
2876 { return __x.date() < __y; }
2878 template<typename _Duration>
2879 [[nodiscard]] friend constexpr bool
2880 operator<(const sys_time<_Duration>& __x,
2881 const leap_second& __y) noexcept
2882 { return __x < __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 __y < __x.date(); }
2890 template<typename _Duration>
2891 [[nodiscard]] friend constexpr bool
2892 operator>(const sys_time<_Duration>& __x,
2893 const leap_second& __y) noexcept
2894 { return __y.date() < __x; }
2896 template<typename _Duration>
2897 [[nodiscard]] friend constexpr bool
2898 operator<=(const leap_second& __x,
2899 const sys_time<_Duration>& __y) noexcept
2900 { return !(__y < __x.date()); }
2902 template<typename _Duration>
2903 [[nodiscard]] friend constexpr bool
2904 operator<=(const sys_time<_Duration>& __x,
2905 const leap_second& __y) noexcept
2906 { return !(__y.date() < __x); }
2908 template<typename _Duration>
2909 [[nodiscard]] friend constexpr bool
2910 operator>=(const leap_second& __x,
2911 const sys_time<_Duration>& __y) noexcept
2912 { return !(__x.date() < __y); }
2914 template<typename _Duration>
2915 [[nodiscard]] friend constexpr bool
2916 operator>=(const sys_time<_Duration>& __x,
2917 const leap_second& __y) noexcept
2918 { return !(__x < __y.date()); }
2920 // This is a simplified form of the constraint specified in the standard,
2921 // three_way_comparable_with<sys_seconds, sys_time<_Duration>>.
2922 template<three_way_comparable_with<seconds> _Duration>
2923 [[nodiscard]] friend constexpr auto
2924 operator<=>(const leap_second& __x,
2925 const sys_time<_Duration>& __y) noexcept
2926 { return __x.date() <=> __y; }
2929 explicit leap_second(seconds::rep __s) : _M_s(__s) { }
2931 friend struct tzdb_list::_Node;
2933 friend const tzdb& reload_tzdb();
2935 template<typename _Duration>
2936 friend leap_second_info
2937 get_leap_second_info(const utc_time<_Duration>&);
2939 seconds _M_s; // == date().time_since_epoch() * value().count()
2942 template<class _Tp> struct zoned_traits { };
2945 struct zoned_traits<const time_zone*>
2947 static const time_zone*
2949 { return std::chrono::locate_zone("UTC"); }
2951 static const time_zone*
2952 locate_zone(string_view __name)
2953 { return std::chrono::locate_zone(__name); }
2959 _GLIBCXX_STD_C::vector<time_zone> zones;
2960 _GLIBCXX_STD_C::vector<time_zone_link> links;
2961 _GLIBCXX_STD_C::vector<leap_second> leap_seconds;
2964 locate_zone(string_view __tz_name) const;
2967 current_zone() const;
2970 friend const tzdb& reload_tzdb();
2971 friend class time_zone;
2972 friend struct tzdb_list::_Node;
2975 tzdb_list& get_tzdb_list();
2976 const tzdb& get_tzdb();
2978 const tzdb& reload_tzdb();
2979 string remote_version();
2981 template<typename _Duration, typename _TimeZonePtr = const time_zone*>
2984 static_assert(__is_duration_v<_Duration>);
2986 using _Traits = zoned_traits<_TimeZonePtr>;
2988 // Every constructor that accepts a string_view as its first parameter
2989 // does not participate in class template argument deduction.
2990 using string_view = type_identity_t<std::string_view>;
2993 using duration = common_type_t<_Duration, seconds>;
2995 zoned_time() requires requires { _Traits::default_zone(); }
2998 zoned_time(const zoned_time&) = default;
2999 zoned_time& operator=(const zoned_time&) = default;
3001 zoned_time(const sys_time<_Duration>& __st)
3002 requires requires { _Traits::default_zone(); }
3007 zoned_time(_TimeZonePtr __z) : _M_zone(std::move(__z)) { }
3010 zoned_time(string_view __name)
3012 _TimeZonePtr{_Traits::locate_zone(std::string_view{})};
3014 : _M_zone(_Traits::locate_zone(__name))
3017 template<typename _Duration2>
3018 zoned_time(const zoned_time<_Duration2, _TimeZonePtr>& __zt)
3019 requires is_convertible_v<sys_time<_Duration2>, sys_time<_Duration>>
3020 : _M_zone(__zt._M_zone), _M_tp(__zt._M_tp)
3023 zoned_time(_TimeZonePtr __z, const sys_time<_Duration>& __st)
3024 : _M_zone(std::move(__z)), _M_tp(__st)
3027 zoned_time(string_view __name, const sys_time<_Duration>& __st)
3028 : zoned_time(_Traits::locate_zone(__name), __st)
3031 zoned_time(_TimeZonePtr __z, const local_time<_Duration>& __tp)
3033 { __z->to_sys(__tp) } -> convertible_to<sys_time<_Duration>>;
3035 : _M_zone(std::move(__z)), _M_tp(_M_zone->to_sys(__tp))
3038 zoned_time(string_view __name, const local_time<_Duration>& __tp)
3039 requires requires (_TimeZonePtr __z) {
3040 { _Traits::locate_zone(__name) } -> convertible_to<_TimeZonePtr>;
3041 { __z->to_sys(__tp) } -> convertible_to<sys_time<_Duration>>;
3043 : zoned_time(_Traits::locate_zone(__name), __tp)
3046 zoned_time(_TimeZonePtr __z, const local_time<_Duration>& __tp,
3049 { __z->to_sys(__tp, __c) } -> convertible_to<sys_time<_Duration>>;
3051 : _M_zone(std::move(__z)), _M_tp(_M_zone->to_sys(__tp, __c))
3054 zoned_time(string_view __name, const local_time<_Duration>& __tp,
3056 requires requires (_TimeZonePtr __z) {
3057 { _Traits::locate_zone(__name) } -> convertible_to<_TimeZonePtr>;
3058 { __z->to_sys(__tp, __c) } -> convertible_to<sys_time<_Duration>>;
3060 : _M_zone(_Traits::locate_zone(__name)),
3061 _M_tp(_M_zone->to_sys(__tp, __c))
3064 template<typename _Duration2, typename _TimeZonePtr2>
3065 zoned_time(_TimeZonePtr __z,
3066 const zoned_time<_Duration2, _TimeZonePtr2>& __zt)
3067 requires is_convertible_v<sys_time<_Duration2>, sys_time<_Duration>>
3068 : _M_zone(__z), _M_tp(__zt._M_tp)
3071 template<typename _Duration2, typename _TimeZonePtr2>
3072 zoned_time(_TimeZonePtr __z,
3073 const zoned_time<_Duration2, _TimeZonePtr2>& __zt,
3075 requires is_convertible_v<sys_time<_Duration2>, sys_time<_Duration>>
3076 : _M_zone(__z), _M_tp(__zt._M_tp)
3079 template<typename _Duration2, typename _TimeZonePtr2>
3080 zoned_time(string_view __name,
3081 const zoned_time<_Duration2, _TimeZonePtr2>& __zt)
3082 requires is_convertible_v<sys_time<_Duration2>, sys_time<_Duration>>
3084 { _Traits::locate_zone(__name) } -> convertible_to<_TimeZonePtr>;
3086 : _M_zone(_Traits::locate_zone(__name)), _M_tp(__zt._M_tp)
3089 template<typename _Duration2, typename _TimeZonePtr2>
3090 zoned_time(string_view __name,
3091 const zoned_time<_Duration2, _TimeZonePtr2>& __zt,
3093 requires is_convertible_v<sys_time<_Duration2>, sys_time<_Duration>>
3095 { _Traits::locate_zone(__name) } -> convertible_to<_TimeZonePtr>;
3097 : _M_zone(_Traits::locate_zone(__name)), _M_tp(__zt._M_tp)
3101 operator=(const sys_time<_Duration>& __st)
3108 operator=(const local_time<_Duration>& __lt)
3110 _M_tp = _M_zone->to_sys(__lt);
3115 operator sys_time<duration>() const { return _M_tp; }
3118 explicit operator local_time<duration>() const
3119 { return get_local_time(); }
3123 get_time_zone() const
3127 local_time<duration>
3128 get_local_time() const
3129 { return _M_zone->to_local(_M_tp); }
3133 get_sys_time() const
3139 { return _M_zone->get_info(_M_tp); }
3141 [[nodiscard]] friend bool
3142 operator==(const zoned_time&, const zoned_time&) = default;
3145 _TimeZonePtr _M_zone{ _Traits::default_zone() };
3146 sys_time<duration> _M_tp{};
3148 template<typename _Duration2, typename _TimeZonePtr2>
3149 friend class zoned_time;
3152 zoned_time() -> zoned_time<seconds>;
3154 template<typename _Duration>
3155 zoned_time(sys_time<_Duration>)
3156 -> zoned_time<common_type_t<_Duration, seconds>>;
3158 /// @cond undocumented
3159 template<typename _TimeZonePtrOrName>
3160 using __time_zone_representation
3161 = __conditional_t<is_convertible_v<_TimeZonePtrOrName, string_view>,
3163 remove_cvref_t<_TimeZonePtrOrName>>;
3166 template<typename _TimeZonePtrOrName>
3167 zoned_time(_TimeZonePtrOrName&&)
3168 -> zoned_time<seconds, __time_zone_representation<_TimeZonePtrOrName>>;
3170 template<typename _TimeZonePtrOrName, typename _Duration>
3171 zoned_time(_TimeZonePtrOrName&&, sys_time<_Duration>)
3172 -> zoned_time<common_type_t<_Duration, seconds>,
3173 __time_zone_representation<_TimeZonePtrOrName>>;
3175 template<typename _TimeZonePtrOrName, typename _Duration>
3176 zoned_time(_TimeZonePtrOrName&&, local_time<_Duration>,
3177 choose = choose::earliest)
3178 -> zoned_time<common_type_t<_Duration, seconds>,
3179 __time_zone_representation<_TimeZonePtrOrName>>;
3181 template<typename _Duration, typename _TimeZonePtrOrName,
3182 typename _TimeZonePtr2>
3183 zoned_time(_TimeZonePtrOrName&&, zoned_time<_Duration, _TimeZonePtr2>,
3184 choose = choose::earliest)
3185 -> zoned_time<common_type_t<_Duration, seconds>,
3186 __time_zone_representation<_TimeZonePtrOrName>>;
3188 template<typename _Dur1, typename _TZPtr1, typename _Dur2, typename _TZPtr2>
3191 operator==(const zoned_time<_Dur1, _TZPtr1>& __x,
3192 const zoned_time<_Dur2, _TZPtr2>& __y)
3194 return __x.get_time_zone() == __y.get_time_zone()
3195 && __x.get_sys_time() == __y.get_sys_time();
3198 using zoned_seconds = zoned_time<seconds>;
3199#endif // _GLIBCXX_USE_CXX11_ABI || ! _GLIBCXX_USE_DUAL_ABI
3203 inline leap_second_info
3204 __get_leap_second_info(sys_seconds __ss, bool __is_utc)
3206 if (__ss < sys_seconds{}) [[unlikely]]
3209 const seconds::rep __leaps[] {
3210 78796800, // 1 Jul 1972
3211 94694400, // 1 Jan 1973
3212 126230400, // 1 Jan 1974
3213 157766400, // 1 Jan 1975
3214 189302400, // 1 Jan 1976
3215 220924800, // 1 Jan 1977
3216 252460800, // 1 Jan 1978
3217 283996800, // 1 Jan 1979
3218 315532800, // 1 Jan 1980
3219 362793600, // 1 Jul 1981
3220 394329600, // 1 Jul 1982
3221 425865600, // 1 Jul 1983
3222 489024000, // 1 Jul 1985
3223 567993600, // 1 Jan 1988
3224 631152000, // 1 Jan 1990
3225 662688000, // 1 Jan 1991
3226 709948800, // 1 Jul 1992
3227 741484800, // 1 Jul 1993
3228 773020800, // 1 Jul 1994
3229 820454400, // 1 Jan 1996
3230 867715200, // 1 Jul 1997
3231 915148800, // 1 Jan 1999
3232 1136073600, // 1 Jan 2006
3233 1230768000, // 1 Jan 2009
3234 1341100800, // 1 Jul 2012
3235 1435708800, // 1 Jul 2015
3236 1483228800, // 1 Jan 2017
3238 // The list above is known to be valid until (at least) this date
3239 // and only contains positive leap seconds.
3240 const sys_seconds __expires(1766880000s); // 2025-12-28 00:00:00 UTC
3242#if _GLIBCXX_USE_CXX11_ABI || ! _GLIBCXX_USE_DUAL_ABI
3243 if (__ss > __expires)
3245 // Use updated leap_seconds from tzdb.
3246 size_t __n = std::size(__leaps);
3248 auto __db = get_tzdb_list().begin();
3249 auto __first = __db->leap_seconds.begin() + __n;
3250 auto __last = __db->leap_seconds.end();
3251 auto __pos = std::upper_bound(__first, __last, __ss);
3252 seconds __elapsed(__n);
3253 for (auto __i = __first; __i != __pos; ++__i)
3254 __elapsed += __i->value();
3258 // Convert utc_time to sys_time:
3260 // See if that sys_time is before (or during) previous leap sec:
3261 if (__pos != __first && __ss < __pos[-1])
3263 if ((__ss + 1s) >= __pos[-1])
3264 return {true, __elapsed};
3265 __elapsed -= __pos[-1].value();
3268 return {false, __elapsed};
3273 seconds::rep __s = __ss.time_since_epoch().count();
3274 const seconds::rep* __first = std::begin(__leaps);
3275 const seconds::rep* __last = std::end(__leaps);
3277 // Don't bother searching the list if we're after the last one.
3278 if (__s > (__last[-1] + (__last - __first) + 1))
3279 return { false, seconds(__last - __first) };
3281 auto __pos = std::upper_bound(__first, __last, __s);
3282 seconds __elapsed{__pos - __first};
3285 // Convert utc_time to sys_time:
3286 __s -= __elapsed.count();
3287 // See if that sys_time is before (or during) previous leap sec:
3288 if (__pos != __first && __s < __pos[-1])
3290 if ((__s + 1) >= __pos[-1])
3291 return {true, __elapsed};
3295 return {false, __elapsed};
3298} // namespace __detail
3300 template<typename _Duration>
3302 inline leap_second_info
3303 get_leap_second_info(const utc_time<_Duration>& __ut)
3305 auto __s = chrono::duration_cast<seconds>(__ut.time_since_epoch());
3306 return __detail::__get_leap_second_info(sys_seconds(__s), true);
3309 template<typename _Duration>
3311 inline utc_time<common_type_t<_Duration, seconds>>
3312 utc_clock::from_sys(const sys_time<_Duration>& __t)
3314 using _CDur = common_type_t<_Duration, seconds>;
3315 auto __s = chrono::time_point_cast<seconds>(__t);
3316 const auto __li = __detail::__get_leap_second_info(__s, false);
3317 return utc_time<_CDur>{__t.time_since_epoch()} + __li.elapsed;
3319#endif // _GLIBCXX_HOSTED
3323 } // namespace chrono
3325#ifdef __glibcxx_chrono_cxx20
3326 inline namespace literals
3328 inline namespace chrono_literals
3330 /// @addtogroup chrono
3332#pragma GCC diagnostic push
3333#pragma GCC diagnostic ignored "-Wliteral-suffix"
3334 /// Literal suffix for creating chrono::day objects.
3336 constexpr chrono::day
3337 operator""d(unsigned long long __d) noexcept
3338 { return chrono::day{static_cast<unsigned>(__d)}; }
3340 /// Literal suffix for creating chrono::year objects.
3342 constexpr chrono::year
3343 operator""y(unsigned long long __y) noexcept
3344 { return chrono::year{static_cast<int>(__y)}; }
3345#pragma GCC diagnostic pop
3347 } // inline namespace chrono_literals
3348 } // inline namespace literals
3351_GLIBCXX_END_NAMESPACE_VERSION
3354#if defined __glibcxx_chrono_cxx20 && _GLIBCXX_HOSTED
3355# include <bits/chrono_io.h>
3360#endif //_GLIBCXX_CHRONO