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/>.
25/** @file include/chrono
26 * This is a Standard C++ Library header.
30#ifndef _GLIBCXX_CHRONO
31#define _GLIBCXX_CHRONO 1
34#pragma GCC system_header
37#ifdef _GLIBCXX_NO_FREESTANDING_CHRONO
38# include <bits/requires_hosted.h> // for <ctime> and clocks
41#if __cplusplus < 201103L
42# include <bits/c++0x_warning.h>
45#include <bits/chrono.h>
47#if __cplusplus >= 202002L
48# include <bit> // __countr_zero
50#if __cplusplus >= 202002L && _GLIBCXX_HOSTED
54# include <bits/stl_algo.h> // upper_bound
55# include <bits/shared_ptr.h>
56# include <bits/unique_ptr.h>
59#define __glibcxx_want_chrono
60#define __glibcxx_want_chrono_udls
61#include <bits/version.h>
63namespace std _GLIBCXX_VISIBILITY(default)
65_GLIBCXX_BEGIN_NAMESPACE_VERSION
68 * @defgroup chrono Time
71 * Classes and functions for time.
76 /** @namespace std::chrono
77 * @brief ISO C++ 2011 namespace for date and time utilities
82#if __cplusplus >= 202002L
83 /// @addtogroup chrono
86 template<typename _Duration>
87 using local_time = time_point<local_t, _Duration>;
88 using local_seconds = local_time<seconds>;
89 using local_days = local_time<days>;
96 template<typename _Duration>
97 using utc_time = time_point<utc_clock, _Duration>;
98 using utc_seconds = utc_time<seconds>;
100 template<typename _Duration>
101 using tai_time = time_point<tai_clock, _Duration>;
102 using tai_seconds = tai_time<seconds>;
104 template<typename _Duration>
105 using gps_time = time_point<gps_clock, _Duration>;
106 using gps_seconds = gps_time<seconds>;
108 template<> struct is_clock<utc_clock> : true_type { };
109 template<> struct is_clock<tai_clock> : true_type { };
110 template<> struct is_clock<gps_clock> : true_type { };
112 template<> inline constexpr bool is_clock_v<utc_clock> = true;
113 template<> inline constexpr bool is_clock_v<tai_clock> = true;
114 template<> inline constexpr bool is_clock_v<gps_clock> = true;
116 struct leap_second_info
122 template<typename _Duration>
124 get_leap_second_info(const utc_time<_Duration>& __ut);
126 /** A clock that measures Universal Coordinated Time (UTC).
128 * The epoch is 1970-01-01 00:00:00.
135 using rep = system_clock::rep;
136 using period = system_clock::period;
137 using duration = chrono::duration<rep, period>;
138 using time_point = chrono::time_point<utc_clock>;
139 static constexpr bool is_steady = false;
144 { return from_sys(system_clock::now()); }
146 template<typename _Duration>
148 static sys_time<common_type_t<_Duration, seconds>>
149 to_sys(const utc_time<_Duration>& __t)
151 using _CDur = common_type_t<_Duration, seconds>;
152 const auto __li = chrono::get_leap_second_info(__t);
153 sys_time<_CDur> __s{__t.time_since_epoch() - __li.elapsed};
154 if (__li.is_leap_second)
155 __s = chrono::floor<seconds>(__s) + seconds{1} - _CDur{1};
159 template<typename _Duration>
161 static utc_time<common_type_t<_Duration, seconds>>
162 from_sys(const sys_time<_Duration>& __t);
165 /** A clock that measures International Atomic Time.
167 * The epoch is 1958-01-01 00:00:00.
174 using rep = system_clock::rep;
175 using period = system_clock::period;
176 using duration = chrono::duration<rep, period>;
177 using time_point = chrono::time_point<tai_clock>;
178 static constexpr bool is_steady = false; // XXX true for CLOCK_TAI?
180 // TODO move into lib, use CLOCK_TAI on linux, add extension point.
184 { return from_utc(utc_clock::now()); }
186 template<typename _Duration>
188 static utc_time<common_type_t<_Duration, seconds>>
189 to_utc(const tai_time<_Duration>& __t)
191 using _CDur = common_type_t<_Duration, seconds>;
192 return utc_time<_CDur>{__t.time_since_epoch()} - 378691210s;
195 template<typename _Duration>
197 static tai_time<common_type_t<_Duration, seconds>>
198 from_utc(const utc_time<_Duration>& __t)
200 using _CDur = common_type_t<_Duration, seconds>;
201 return tai_time<_CDur>{__t.time_since_epoch()} + 378691210s;
205 /** A clock that measures GPS time.
207 * The epoch is 1980-01-06 00:00:00.
214 using rep = system_clock::rep;
215 using period = system_clock::period;
216 using duration = chrono::duration<rep, period>;
217 using time_point = chrono::time_point<gps_clock>;
218 static constexpr bool is_steady = false; // XXX
220 // TODO move into lib, add extension point.
224 { return from_utc(utc_clock::now()); }
226 template<typename _Duration>
228 static utc_time<common_type_t<_Duration, seconds>>
229 to_utc(const gps_time<_Duration>& __t)
231 using _CDur = common_type_t<_Duration, seconds>;
232 return utc_time<_CDur>{__t.time_since_epoch()} + 315964809s;
235 template<typename _Duration>
237 static gps_time<common_type_t<_Duration, seconds>>
238 from_utc(const utc_time<_Duration>& __t)
240 using _CDur = common_type_t<_Duration, seconds>;
241 return gps_time<_CDur>{__t.time_since_epoch()} - 315964809s;
244#endif // _GLIBCXX_HOSTED
246 template<typename _DestClock, typename _SourceClock>
247 struct clock_time_conversion
250 // Identity conversions
252 template<typename _Clock>
253 struct clock_time_conversion<_Clock, _Clock>
255 template<typename _Duration>
256 time_point<_Clock, _Duration>
257 operator()(const time_point<_Clock, _Duration>& __t) const
263 struct clock_time_conversion<system_clock, system_clock>
265 template<typename _Duration>
267 operator()(const sys_time<_Duration>& __t) const
272 struct clock_time_conversion<utc_clock, utc_clock>
274 template<typename _Duration>
276 operator()(const utc_time<_Duration>& __t) const
280 // Conversions between system_clock and utc_clock
283 struct clock_time_conversion<utc_clock, system_clock>
285 template<typename _Duration>
286 utc_time<common_type_t<_Duration, seconds>>
287 operator()(const sys_time<_Duration>& __t) const
288 { return utc_clock::from_sys(__t); }
292 struct clock_time_conversion<system_clock, utc_clock>
294 template<typename _Duration>
295 sys_time<common_type_t<_Duration, seconds>>
296 operator()(const utc_time<_Duration>& __t) const
297 { return utc_clock::to_sys(__t); }
300 template<typename _Tp, typename _Clock>
301 inline constexpr bool __is_time_point_for_v = false;
303 template<typename _Clock, typename _Duration>
304 inline constexpr bool
305 __is_time_point_for_v<time_point<_Clock, _Duration>, _Clock> = true;
307 // Conversions between system_clock and other clocks
309 template<typename _SourceClock>
310 struct clock_time_conversion<system_clock, _SourceClock>
312 template<typename _Duration, typename _Src = _SourceClock>
314 operator()(const time_point<_SourceClock, _Duration>& __t) const
315 -> decltype(_Src::to_sys(__t))
317 using _Ret = decltype(_SourceClock::to_sys(__t));
318 static_assert(__is_time_point_for_v<_Ret, system_clock>);
319 return _SourceClock::to_sys(__t);
323 template<typename _DestClock>
324 struct clock_time_conversion<_DestClock, system_clock>
326 template<typename _Duration, typename _Dest = _DestClock>
328 operator()(const sys_time<_Duration>& __t) const
329 -> decltype(_Dest::from_sys(__t))
331 using _Ret = decltype(_DestClock::from_sys(__t));
332 static_assert(__is_time_point_for_v<_Ret, _DestClock>);
333 return _DestClock::from_sys(__t);
337 // Conversions between utc_clock and other clocks
339 template<typename _SourceClock>
340 struct clock_time_conversion<utc_clock, _SourceClock>
342 template<typename _Duration, typename _Src = _SourceClock>
344 operator()(const time_point<_SourceClock, _Duration>& __t) const
345 -> decltype(_Src::to_utc(__t))
347 using _Ret = decltype(_SourceClock::to_utc(__t));
348 static_assert(__is_time_point_for_v<_Ret, utc_clock>);
349 return _SourceClock::to_utc(__t);
353 template<typename _DestClock>
354 struct clock_time_conversion<_DestClock, utc_clock>
356 template<typename _Duration, typename _Dest = _DestClock>
358 operator()(const utc_time<_Duration>& __t) const
359 -> decltype(_Dest::from_utc(__t))
361 using _Ret = decltype(_DestClock::from_utc(__t));
362 static_assert(__is_time_point_for_v<_Ret, _DestClock>);
363 return _DestClock::from_utc(__t);
366#endif // _GLIBCXX_HOSTED
368 /// @cond undocumented
371 template<typename _DestClock, typename _SourceClock, typename _Duration>
372 concept __clock_convs
373 = requires (const time_point<_SourceClock, _Duration>& __t) {
374 clock_time_conversion<_DestClock, _SourceClock>{}(__t);
378 template<typename _DestClock, typename _SourceClock, typename _Duration>
379 concept __clock_convs_sys
380 = requires (const time_point<_SourceClock, _Duration>& __t) {
381 clock_time_conversion<_DestClock, system_clock>{}(
382 clock_time_conversion<system_clock, _SourceClock>{}(__t));
385 template<typename _DestClock, typename _SourceClock, typename _Duration>
386 concept __clock_convs_utc
387 = requires (const time_point<_SourceClock, _Duration>& __t) {
388 clock_time_conversion<_DestClock, utc_clock>{}(
389 clock_time_conversion<utc_clock, _SourceClock>{}(__t));
392 template<typename _DestClock, typename _SourceClock, typename _Duration>
393 concept __clock_convs_sys_utc
394 = requires (const time_point<_SourceClock, _Duration>& __t) {
395 clock_time_conversion<_DestClock, utc_clock>{}(
396 clock_time_conversion<utc_clock, system_clock>{}(
397 clock_time_conversion<system_clock, _SourceClock>{}(__t)));
400 template<typename _DestClock, typename _SourceClock, typename _Duration>
401 concept __clock_convs_utc_sys
402 = requires (const time_point<_SourceClock, _Duration>& __t) {
403 clock_time_conversion<_DestClock, system_clock>{}(
404 clock_time_conversion<system_clock, utc_clock>{}(
405 clock_time_conversion<utc_clock, _SourceClock>{}(__t)));
407#endif // _GLIBCXX_HOSTED
408 } // namespace __detail
411 /// Convert a time point to a different clock.
412 template<typename _DestClock, typename _SourceClock, typename _Duration>
415 clock_cast(const time_point<_SourceClock, _Duration>& __t)
416 requires __detail::__clock_convs<_DestClock, _SourceClock, _Duration>
418 || __detail::__clock_convs_sys<_DestClock, _SourceClock, _Duration>
419 || __detail::__clock_convs_utc<_DestClock, _SourceClock, _Duration>
420 || __detail::__clock_convs_sys_utc<_DestClock, _SourceClock, _Duration>
421 || __detail::__clock_convs_utc_sys<_DestClock, _SourceClock, _Duration>
422#endif // _GLIBCXX_HOSTED
424 constexpr bool __direct
425 = __detail::__clock_convs<_DestClock, _SourceClock, _Duration>;
426 if constexpr (__direct)
428 return clock_time_conversion<_DestClock, _SourceClock>{}(__t);
433 constexpr bool __convert_via_sys_clock
434 = __detail::__clock_convs_sys<_DestClock, _SourceClock, _Duration>;
435 constexpr bool __convert_via_utc_clock
436 = __detail::__clock_convs_utc<_DestClock, _SourceClock, _Duration>;
437 if constexpr (__convert_via_sys_clock)
439 static_assert(!__convert_via_utc_clock,
440 "clock_cast requires a unique best conversion, but "
441 "conversion is possible via system_clock and also via"
443 return clock_time_conversion<_DestClock, system_clock>{}(
444 clock_time_conversion<system_clock, _SourceClock>{}(__t));
446 else if constexpr (__convert_via_utc_clock)
448 return clock_time_conversion<_DestClock, utc_clock>{}(
449 clock_time_conversion<utc_clock, _SourceClock>{}(__t));
453 constexpr bool __convert_via_sys_and_utc_clocks
454 = __detail::__clock_convs_sys_utc<_DestClock,
458 if constexpr (__convert_via_sys_and_utc_clocks)
460 constexpr bool __convert_via_utc_and_sys_clocks
461 = __detail::__clock_convs_utc_sys<_DestClock,
464 static_assert(!__convert_via_utc_and_sys_clocks,
465 "clock_cast requires a unique best conversion, but "
466 "conversion is possible via system_clock followed by "
467 "utc_clock, and also via utc_clock followed by "
469 return clock_time_conversion<_DestClock, utc_clock>{}(
470 clock_time_conversion<utc_clock, system_clock>{}(
471 clock_time_conversion<system_clock, _SourceClock>{}(__t)));
475 return clock_time_conversion<_DestClock, system_clock>{}(
476 clock_time_conversion<system_clock, utc_clock>{}(
477 clock_time_conversion<utc_clock, _SourceClock>{}(__t)));
481#endif // _GLIBCXX_HOSTED
486 // CLASS DECLARATIONS
491 class weekday_indexed;
494 class month_day_last;
496 class month_weekday_last;
498 class year_month_day;
499 class year_month_day_last;
500 class year_month_weekday;
501 class year_month_weekday_last;
505 explicit last_spec() = default;
507 friend constexpr month_day_last
508 operator/(int __m, last_spec) noexcept;
510 friend constexpr month_day_last
511 operator/(last_spec, int __m) noexcept;
514 inline constexpr last_spec last{};
518 // Helper to __add_modulo and __sub_modulo.
519 template <unsigned __d, typename _Tp>
523 using _Up = make_unsigned_t<_Tp>;
524 auto constexpr __a = _Up(-1) - _Up(255 + __d - 2);
525 auto constexpr __b = _Up(__d * (__a / __d) - 1);
526 // Notice: b <= a - 1 <= _Up(-1) - (255 + d - 1) and b % d = d - 1.
527 return _Up(-1) - __b; // >= 255 + d - 1
530 // Compute the remainder of the Euclidean division of __x + __y divided by
531 // __d without overflowing. Typically, __x <= 255 + d - 1 is sum of
532 // weekday/month with a shift in [0, d - 1] and __y is a duration count.
533 template <unsigned __d, typename _Tp>
535 __add_modulo(unsigned __x, _Tp __y)
537 using _Up = make_unsigned_t<_Tp>;
538 // For __y >= 0, _Up(__y) has the same mathematical value as __y and
539 // this function simply returns (__x + _Up(__y)) % d. Typically, this
540 // doesn't overflow since the range of _Up contains many more positive
541 // values than _Tp's. For __y < 0, _Up(__y) has a mathematical value in
542 // the upper-half range of _Up so that adding a positive value to it
543 // might overflow. Moreover, most likely, _Up(__y) != __y mod d. To
544 // fix both issues we subtract from _Up(__y) an __offset >=
545 // 255 + d - 1 to make room for the addition to __x and shift the modulo
546 // to the correct value.
547 auto const __offset = __y >= 0 ? _Up(0) : __modulo_offset<__d, _Tp>();
548 return (__x + _Up(__y) - __offset) % __d;
551 // Similar to __add_modulo but for __x - __y.
552 template <unsigned __d, typename _Tp>
554 __sub_modulo(unsigned __x, _Tp __y)
556 using _Up = make_unsigned_t<_Tp>;
557 auto const __offset = __y <= 0 ? _Up(0) : __modulo_offset<__d, _Tp>();
558 return (__x - _Up(__y) - __offset) % __d;
561 inline constexpr unsigned __days_per_month[12]
562 = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
576 day(unsigned __d) noexcept
581 operator++() noexcept
588 operator++(int) noexcept
596 operator--() noexcept
603 operator--(int) noexcept
611 operator+=(const days& __d) noexcept
618 operator-=(const days& __d) noexcept
625 operator unsigned() const noexcept
630 { return 1 <= _M_d && _M_d <= 31; }
632 friend constexpr bool
633 operator==(const day& __x, const day& __y) noexcept
634 { return unsigned{__x} == unsigned{__y}; }
636 friend constexpr strong_ordering
637 operator<=>(const day& __x, const day& __y) noexcept
638 { return unsigned{__x} <=> unsigned{__y}; }
641 operator+(const day& __x, const days& __y) noexcept
642 { return day(unsigned{__x} + __y.count()); }
645 operator+(const days& __x, const day& __y) noexcept
646 { return __y + __x; }
649 operator-(const day& __x, const days& __y) noexcept
650 { return __x + -__y; }
652 friend constexpr days
653 operator-(const day& __x, const day& __y) noexcept
654 { return days{int(unsigned{__x}) - int(unsigned{__y})}; }
656 friend constexpr month_day
657 operator/(const month& __m, const day& __d) noexcept;
659 friend constexpr month_day
660 operator/(int __m, const day& __d) noexcept;
662 friend constexpr month_day
663 operator/(const day& __d, const month& __m) noexcept;
665 friend constexpr month_day
666 operator/(const day& __d, int __m) noexcept;
668 friend constexpr year_month_day
669 operator/(const year_month& __ym, const day& __d) noexcept;
683 month(unsigned __m) noexcept
688 operator++() noexcept
695 operator++(int) noexcept
703 operator--() noexcept
710 operator--(int) noexcept
718 operator+=(const months& __m) noexcept
725 operator-=(const months& __m) noexcept
732 operator unsigned() const noexcept
737 { return 1 <= _M_m && _M_m <= 12; }
739 friend constexpr bool
740 operator==(const month& __x, const month& __y) noexcept
741 { return unsigned{__x} == unsigned{__y}; }
743 friend constexpr strong_ordering
744 operator<=>(const month& __x, const month& __y) noexcept
745 { return unsigned{__x} <=> unsigned{__y}; }
747 friend constexpr month
748 operator+(const month& __x, const months& __y) noexcept
750 // modulo(x + (y - 1), 12) = modulo(x + (y - 1) + 12, 12)
751 // = modulo((x + 11) + y , 12)
752 return month{1 + __detail::__add_modulo<12>(
753 unsigned{__x} + 11, __y.count())};
756 friend constexpr month
757 operator+(const months& __x, const month& __y) noexcept
758 { return __y + __x; }
760 friend constexpr month
761 operator-(const month& __x, const months& __y) noexcept
763 // modulo(x + (-y - 1), 12) = modulo(x + (-y - 1) + 12, 12)
764 // = modulo((x + 11) - y , 12)
765 return month{1 + __detail::__sub_modulo<12>(
766 unsigned{__x} + 11, __y.count())};
769 friend constexpr months
770 operator-(const month& __x, const month& __y) noexcept
772 const auto __dm = int(unsigned(__x)) - int(unsigned(__y));
773 return months{__dm < 0 ? 12 + __dm : __dm};
776 friend constexpr year_month
777 operator/(const year& __y, const month& __m) noexcept;
779 friend constexpr month_day
780 operator/(const month& __m, int __d) noexcept;
782 friend constexpr month_day_last
783 operator/(const month& __m, last_spec) noexcept;
785 friend constexpr month_day_last
786 operator/(last_spec, const month& __m) noexcept;
788 friend constexpr month_weekday
789 operator/(const month& __m, const weekday_indexed& __wdi) noexcept;
791 friend constexpr month_weekday
792 operator/(const weekday_indexed& __wdi, const month& __m) noexcept;
794 friend constexpr month_weekday_last
795 operator/(const month& __m, const weekday_last& __wdl) noexcept;
797 friend constexpr month_weekday_last
798 operator/(const weekday_last& __wdl, const month& __m) noexcept;
801 inline constexpr month January{1};
802 inline constexpr month February{2};
803 inline constexpr month March{3};
804 inline constexpr month April{4};
805 inline constexpr month May{5};
806 inline constexpr month June{6};
807 inline constexpr month July{7};
808 inline constexpr month August{8};
809 inline constexpr month September{9};
810 inline constexpr month October{10};
811 inline constexpr month November{11};
812 inline constexpr month December{12};
825 year(int __y) noexcept
826 : _M_y{static_cast<short>(__y)}
829 static constexpr year
831 { return year{-32767}; }
833 static constexpr year
835 { return year{32767}; }
838 operator++() noexcept
845 operator++(int) noexcept
853 operator--() noexcept
860 operator--(int) noexcept
868 operator+=(const years& __y) noexcept
875 operator-=(const years& __y) noexcept
882 operator+() const noexcept
886 operator-() const noexcept
887 { return year{-_M_y}; }
890 is_leap() const noexcept
892 // Testing divisibility by 100 first gives better performance [1], i.e.,
893 // return _M_y % 100 == 0 ? _M_y % 400 == 0 : _M_y % 16 == 0;
894 // Furthermore, if _M_y % 100 == 0, then _M_y % 400 == 0 is equivalent
895 // to _M_y % 16 == 0, so we can simplify it to
896 // return _M_y % 100 == 0 ? _M_y % 16 == 0 : _M_y % 4 == 0. // #1
897 // Similarly, we can replace 100 with 25 (which is good since
898 // _M_y % 25 == 0 requires one fewer instruction than _M_y % 100 == 0
900 // return _M_y % 25 == 0 ? _M_y % 16 == 0 : _M_y % 4 == 0. // #2
901 // Indeed, first assume _M_y % 4 != 0. Then _M_y % 16 != 0 and hence,
902 // _M_y % 4 == 0 and _M_y % 16 == 0 are both false. Therefore, #2
903 // returns false as it should (regardless of _M_y % 25.) Now assume
904 // _M_y % 4 == 0. In this case, _M_y % 25 == 0 if, and only if,
905 // _M_y % 100 == 0, that is, #1 and #2 are equivalent. Finally, #2 is
907 // return (_M_y & (_M_y % 25 == 0 ? 15 : 3)) == 0.
910 // [1] https://github.com/cassioneri/calendar
911 // [2] https://godbolt.org/z/55G8rn77e
912 // [3] https://gcc.gnu.org/pipermail/libstdc++/2021-June/052815.html
914 return (_M_y & (_M_y % 25 == 0 ? 15 : 3)) == 0;
918 operator int() const noexcept
923 { return min()._M_y <= _M_y && _M_y <= max()._M_y; }
925 friend constexpr bool
926 operator==(const year& __x, const year& __y) noexcept
927 { return int{__x} == int{__y}; }
929 friend constexpr strong_ordering
930 operator<=>(const year& __x, const year& __y) noexcept
931 { return int{__x} <=> int{__y}; }
933 friend constexpr year
934 operator+(const year& __x, const years& __y) noexcept
935 { return year{int{__x} + static_cast<int>(__y.count())}; }
937 friend constexpr year
938 operator+(const years& __x, const year& __y) noexcept
939 { return __y + __x; }
941 friend constexpr year
942 operator-(const year& __x, const years& __y) noexcept
943 { return __x + -__y; }
945 friend constexpr years
946 operator-(const year& __x, const year& __y) noexcept
947 { return years{int{__x} - int{__y}}; }
949 friend constexpr year_month
950 operator/(const year& __y, int __m) noexcept;
952 friend constexpr year_month_day
953 operator/(const year& __y, const month_day& __md) noexcept;
955 friend constexpr year_month_day
956 operator/(const month_day& __md, const year& __y) noexcept;
958 friend constexpr year_month_day_last
959 operator/(const year& __y, const month_day_last& __mdl) noexcept;
961 friend constexpr year_month_day_last
962 operator/(const month_day_last& __mdl, const year& __y) noexcept;
964 friend constexpr year_month_weekday
965 operator/(const year& __y, const month_weekday& __mwd) noexcept;
967 friend constexpr year_month_weekday
968 operator/(const month_weekday& __mwd, const year& __y) noexcept;
970 friend constexpr year_month_weekday_last
971 operator/(const year& __y, const month_weekday_last& __mwdl) noexcept;
973 friend constexpr year_month_weekday_last
974 operator/(const month_weekday_last& __mwdl, const year& __y) noexcept;
984 static constexpr weekday
985 _S_from_days(const days& __d)
987 return weekday{__detail::__add_modulo<7>(4, __d.count())};
994 weekday(unsigned __wd) noexcept
995 : _M_wd(__wd == 7 ? 0 : __wd) // __wd % 7 ?
999 weekday(const sys_days& __dp) noexcept
1000 : weekday{_S_from_days(__dp.time_since_epoch())}
1004 weekday(const local_days& __dp) noexcept
1005 : weekday{sys_days{__dp.time_since_epoch()}}
1009 operator++() noexcept
1016 operator++(int) noexcept
1024 operator--() noexcept
1031 operator--(int) noexcept
1039 operator+=(const days& __d) noexcept
1041 *this = *this + __d;
1046 operator-=(const days& __d) noexcept
1048 *this = *this - __d;
1053 c_encoding() const noexcept
1057 iso_encoding() const noexcept
1058 { return _M_wd == 0u ? 7u : _M_wd; }
1062 { return _M_wd <= 6; }
1064 constexpr weekday_indexed
1065 operator[](unsigned __index) const noexcept;
1067 constexpr weekday_last
1068 operator[](last_spec) const noexcept;
1070 friend constexpr bool
1071 operator==(const weekday& __x, const weekday& __y) noexcept
1072 { return __x._M_wd == __y._M_wd; }
1074 friend constexpr weekday
1075 operator+(const weekday& __x, const days& __y) noexcept
1077 return weekday{__detail::__add_modulo<7>(__x._M_wd, __y.count())};
1080 friend constexpr weekday
1081 operator+(const days& __x, const weekday& __y) noexcept
1082 { return __y + __x; }
1084 friend constexpr weekday
1085 operator-(const weekday& __x, const days& __y) noexcept
1087 return weekday{__detail::__sub_modulo<7>(__x._M_wd, __y.count())};
1090 friend constexpr days
1091 operator-(const weekday& __x, const weekday& __y) noexcept
1093 const auto __n = __x.c_encoding() - __y.c_encoding();
1094 return static_cast<int>(__n) >= 0 ? days{__n} : days{__n + 7};
1098 inline constexpr weekday Sunday{0};
1099 inline constexpr weekday Monday{1};
1100 inline constexpr weekday Tuesday{2};
1101 inline constexpr weekday Wednesday{3};
1102 inline constexpr weekday Thursday{4};
1103 inline constexpr weekday Friday{5};
1104 inline constexpr weekday Saturday{6};
1108 class weekday_indexed
1111 chrono::weekday _M_wd;
1112 unsigned char _M_index;
1115 weekday_indexed() = default;
1118 weekday_indexed(const chrono::weekday& __wd, unsigned __index) noexcept
1119 : _M_wd(__wd), _M_index(__index)
1122 constexpr chrono::weekday
1123 weekday() const noexcept
1127 index() const noexcept
1128 { return _M_index; };
1132 { return _M_wd.ok() && 1 <= _M_index && _M_index <= 5; }
1134 friend constexpr bool
1135 operator==(const weekday_indexed& __x, const weekday_indexed& __y) noexcept
1136 { return __x.weekday() == __y.weekday() && __x.index() == __y.index(); }
1138 friend constexpr month_weekday
1139 operator/(const month& __m, const weekday_indexed& __wdi) noexcept;
1141 friend constexpr month_weekday
1142 operator/(int __m, const weekday_indexed& __wdi) noexcept;
1144 friend constexpr month_weekday
1145 operator/(const weekday_indexed& __wdi, const month& __m) noexcept;
1147 friend constexpr month_weekday
1148 operator/(const weekday_indexed& __wdi, int __m) noexcept;
1150 friend constexpr year_month_weekday
1151 operator/(const year_month& __ym, const weekday_indexed& __wdi) noexcept;
1154 constexpr weekday_indexed
1155 weekday::operator[](unsigned __index) const noexcept
1156 { return {*this, __index}; }
1163 chrono::weekday _M_wd;
1167 weekday_last(const chrono::weekday& __wd) noexcept
1171 constexpr chrono::weekday
1172 weekday() const noexcept
1177 { return _M_wd.ok(); }
1179 friend constexpr bool
1180 operator==(const weekday_last& __x, const weekday_last& __y) noexcept
1181 { return __x.weekday() == __y.weekday(); }
1183 friend constexpr month_weekday_last
1184 operator/(int __m, const weekday_last& __wdl) noexcept;
1186 friend constexpr month_weekday_last
1187 operator/(const weekday_last& __wdl, int __m) noexcept;
1189 friend constexpr year_month_weekday_last
1190 operator/(const year_month& __ym, const weekday_last& __wdl) noexcept;
1193 constexpr weekday_last
1194 weekday::operator[](last_spec) const noexcept
1195 { return weekday_last{*this}; }
1206 month_day() = default;
1209 month_day(const chrono::month& __m, const chrono::day& __d) noexcept
1210 : _M_m{__m}, _M_d{__d}
1213 constexpr chrono::month
1214 month() const noexcept
1217 constexpr chrono::day
1218 day() const noexcept
1225 && 1u <= unsigned(_M_d)
1226 && unsigned(_M_d) <= __detail::__days_per_month[unsigned(_M_m) - 1];
1229 friend constexpr bool
1230 operator==(const month_day& __x, const month_day& __y) noexcept
1231 { return __x.month() == __y.month() && __x.day() == __y.day(); }
1233 friend constexpr strong_ordering
1234 operator<=>(const month_day& __x, const month_day& __y) noexcept
1237 friend constexpr month_day
1238 operator/(const chrono::month& __m, const chrono::day& __d) noexcept
1239 { return {__m, __d}; }
1241 friend constexpr month_day
1242 operator/(const chrono::month& __m, int __d) noexcept
1243 { return {__m, chrono::day(unsigned(__d))}; }
1245 friend constexpr month_day
1246 operator/(int __m, const chrono::day& __d) noexcept
1247 { return {chrono::month(unsigned(__m)), __d}; }
1249 friend constexpr month_day
1250 operator/(const chrono::day& __d, const chrono::month& __m) noexcept
1251 { return {__m, __d}; }
1253 friend constexpr month_day
1254 operator/(const chrono::day& __d, int __m) noexcept
1255 { return {chrono::month(unsigned(__m)), __d}; }
1257 friend constexpr year_month_day
1258 operator/(int __y, const month_day& __md) noexcept;
1260 friend constexpr year_month_day
1261 operator/(const month_day& __md, int __y) noexcept;
1266 class month_day_last
1273 month_day_last(const chrono::month& __m) noexcept
1277 constexpr chrono::month
1278 month() const noexcept
1283 { return _M_m.ok(); }
1285 friend constexpr bool
1286 operator==(const month_day_last& __x, const month_day_last& __y) noexcept
1287 { return __x.month() == __y.month(); }
1289 friend constexpr strong_ordering
1290 operator<=>(const month_day_last& __x, const month_day_last& __y) noexcept
1293 friend constexpr month_day_last
1294 operator/(const chrono::month& __m, last_spec) noexcept
1295 { return month_day_last{__m}; }
1297 friend constexpr month_day_last
1298 operator/(int __m, last_spec) noexcept
1299 { return chrono::month(unsigned(__m)) / last; }
1301 friend constexpr month_day_last
1302 operator/(last_spec, const chrono::month& __m) noexcept
1303 { return __m / last; }
1305 friend constexpr month_day_last
1306 operator/(last_spec, int __m) noexcept
1307 { return __m / last; }
1309 friend constexpr year_month_day_last
1310 operator/(int __y, const month_day_last& __mdl) noexcept;
1312 friend constexpr year_month_day_last
1313 operator/(const month_day_last& __mdl, int __y) noexcept;
1322 chrono::weekday_indexed _M_wdi;
1326 month_weekday(const chrono::month& __m,
1327 const chrono::weekday_indexed& __wdi) noexcept
1328 : _M_m{__m}, _M_wdi{__wdi}
1331 constexpr chrono::month
1332 month() const noexcept
1335 constexpr chrono::weekday_indexed
1336 weekday_indexed() const noexcept
1341 { return _M_m.ok() && _M_wdi.ok(); }
1343 friend constexpr bool
1344 operator==(const month_weekday& __x, const month_weekday& __y) noexcept
1346 return __x.month() == __y.month()
1347 && __x.weekday_indexed() == __y.weekday_indexed();
1350 friend constexpr month_weekday
1351 operator/(const chrono::month& __m,
1352 const chrono::weekday_indexed& __wdi) noexcept
1353 { return {__m, __wdi}; }
1355 friend constexpr month_weekday
1356 operator/(int __m, const chrono::weekday_indexed& __wdi) noexcept
1357 { return chrono::month(unsigned(__m)) / __wdi; }
1359 friend constexpr month_weekday
1360 operator/(const chrono::weekday_indexed& __wdi,
1361 const chrono::month& __m) noexcept
1362 { return __m / __wdi; }
1364 friend constexpr month_weekday
1365 operator/(const chrono::weekday_indexed& __wdi, int __m) noexcept
1366 { return __m / __wdi; }
1368 friend constexpr year_month_weekday
1369 operator/(int __y, const month_weekday& __mwd) noexcept;
1371 friend constexpr year_month_weekday
1372 operator/(const month_weekday& __mwd, int __y) noexcept;
1375 // MONTH_WEEKDAY_LAST
1377 class month_weekday_last
1381 chrono::weekday_last _M_wdl;
1385 month_weekday_last(const chrono::month& __m,
1386 const chrono::weekday_last& __wdl) noexcept
1387 :_M_m{__m}, _M_wdl{__wdl}
1390 constexpr chrono::month
1391 month() const noexcept
1394 constexpr chrono::weekday_last
1395 weekday_last() const noexcept
1400 { return _M_m.ok() && _M_wdl.ok(); }
1402 friend constexpr bool
1403 operator==(const month_weekday_last& __x,
1404 const month_weekday_last& __y) noexcept
1406 return __x.month() == __y.month()
1407 && __x.weekday_last() == __y.weekday_last();
1410 friend constexpr month_weekday_last
1411 operator/(const chrono::month& __m,
1412 const chrono::weekday_last& __wdl) noexcept
1413 { return {__m, __wdl}; }
1415 friend constexpr month_weekday_last
1416 operator/(int __m, const chrono::weekday_last& __wdl) noexcept
1417 { return chrono::month(unsigned(__m)) / __wdl; }
1419 friend constexpr month_weekday_last
1420 operator/(const chrono::weekday_last& __wdl,
1421 const chrono::month& __m) noexcept
1422 { return __m / __wdl; }
1424 friend constexpr month_weekday_last
1425 operator/(const chrono::weekday_last& __wdl, int __m) noexcept
1426 { return chrono::month(unsigned(__m)) / __wdl; }
1428 friend constexpr year_month_weekday_last
1429 operator/(int __y, const month_weekday_last& __mwdl) noexcept;
1431 friend constexpr year_month_weekday_last
1432 operator/(const month_weekday_last& __mwdl, int __y) noexcept;
1439 // [time.cal.ym], [time.cal.ymd], etc constrain the 'months'-based
1440 // addition/subtraction operator overloads like so:
1442 // Constraints: if the argument supplied by the caller for the months
1443 // parameter is convertible to years, its implicit conversion sequence
1444 // to years is worse than its implicit conversion sequence to months.
1446 // We realize this constraint by templatizing the 'months'-based
1447 // overloads (using a dummy defaulted template parameter), so that
1448 // overload resolution doesn't select the 'months'-based overload unless
1449 // the implicit conversion sequence to 'months' is better than that to
1451 using __months_years_conversion_disambiguator = void;
1461 year_month() = default;
1464 year_month(const chrono::year& __y, const chrono::month& __m) noexcept
1465 : _M_y{__y}, _M_m{__m}
1468 constexpr chrono::year
1469 year() const noexcept
1472 constexpr chrono::month
1473 month() const noexcept
1476 template<typename = __detail::__months_years_conversion_disambiguator>
1477 constexpr year_month&
1478 operator+=(const months& __dm) noexcept
1480 *this = *this + __dm;
1484 template<typename = __detail::__months_years_conversion_disambiguator>
1485 constexpr year_month&
1486 operator-=(const months& __dm) noexcept
1488 *this = *this - __dm;
1492 constexpr year_month&
1493 operator+=(const years& __dy) noexcept
1495 *this = *this + __dy;
1499 constexpr year_month&
1500 operator-=(const years& __dy) noexcept
1502 *this = *this - __dy;
1508 { return _M_y.ok() && _M_m.ok(); }
1510 friend constexpr bool
1511 operator==(const year_month& __x, const year_month& __y) noexcept
1512 { return __x.year() == __y.year() && __x.month() == __y.month(); }
1514 friend constexpr strong_ordering
1515 operator<=>(const year_month& __x, const year_month& __y) noexcept
1518 template<typename = __detail::__months_years_conversion_disambiguator>
1519 friend constexpr year_month
1520 operator+(const year_month& __ym, const months& __dm) noexcept
1523 auto __m = __ym.month() + __dm;
1524 auto __i = int(unsigned(__ym.month())) - 1 + __dm.count();
1526 ? __ym.year() + years{(__i - 11) / 12}
1527 : __ym.year() + years{__i / 12});
1531 template<typename = __detail::__months_years_conversion_disambiguator>
1532 friend constexpr year_month
1533 operator+(const months& __dm, const year_month& __ym) noexcept
1534 { return __ym + __dm; }
1536 template<typename = __detail::__months_years_conversion_disambiguator>
1537 friend constexpr year_month
1538 operator-(const year_month& __ym, const months& __dm) noexcept
1539 { return __ym + -__dm; }
1541 friend constexpr months
1542 operator-(const year_month& __x, const year_month& __y) noexcept
1544 return (__x.year() - __y.year()
1545 + months{static_cast<int>(unsigned{__x.month()})
1546 - static_cast<int>(unsigned{__y.month()})});
1549 friend constexpr year_month
1550 operator+(const year_month& __ym, const years& __dy) noexcept
1551 { return (__ym.year() + __dy) / __ym.month(); }
1553 friend constexpr year_month
1554 operator+(const years& __dy, const year_month& __ym) noexcept
1555 { return __ym + __dy; }
1557 friend constexpr year_month
1558 operator-(const year_month& __ym, const years& __dy) noexcept
1559 { return __ym + -__dy; }
1561 friend constexpr year_month
1562 operator/(const chrono::year& __y, const chrono::month& __m) noexcept
1563 { return {__y, __m}; }
1565 friend constexpr year_month
1566 operator/(const chrono::year& __y, int __m) noexcept
1567 { return {__y, chrono::month(unsigned(__m))}; }
1569 friend constexpr year_month_day
1570 operator/(const year_month& __ym, int __d) noexcept;
1572 friend constexpr year_month_day_last
1573 operator/(const year_month& __ym, last_spec) noexcept;
1578 class year_month_day
1585 static constexpr year_month_day _S_from_days(const days& __dp) noexcept;
1587 constexpr days _M_days_since_epoch() const noexcept;
1590 year_month_day() = default;
1593 year_month_day(const chrono::year& __y, const chrono::month& __m,
1594 const chrono::day& __d) noexcept
1595 : _M_y{__y}, _M_m{__m}, _M_d{__d}
1599 year_month_day(const year_month_day_last& __ymdl) noexcept;
1602 year_month_day(const sys_days& __dp) noexcept
1603 : year_month_day(_S_from_days(__dp.time_since_epoch()))
1607 year_month_day(const local_days& __dp) noexcept
1608 : year_month_day(sys_days{__dp.time_since_epoch()})
1611 template<typename = __detail::__months_years_conversion_disambiguator>
1612 constexpr year_month_day&
1613 operator+=(const months& __m) noexcept
1615 *this = *this + __m;
1619 template<typename = __detail::__months_years_conversion_disambiguator>
1620 constexpr year_month_day&
1621 operator-=(const months& __m) noexcept
1623 *this = *this - __m;
1627 constexpr year_month_day&
1628 operator+=(const years& __y) noexcept
1630 *this = *this + __y;
1634 constexpr year_month_day&
1635 operator-=(const years& __y) noexcept
1637 *this = *this - __y;
1641 constexpr chrono::year
1642 year() const noexcept
1645 constexpr chrono::month
1646 month() const noexcept
1649 constexpr chrono::day
1650 day() const noexcept
1654 operator sys_days() const noexcept
1655 { return sys_days{_M_days_since_epoch()}; }
1658 operator local_days() const noexcept
1659 { return local_days{sys_days{*this}.time_since_epoch()}; }
1661 constexpr bool ok() const noexcept;
1663 friend constexpr bool
1664 operator==(const year_month_day& __x, const year_month_day& __y) noexcept
1666 return __x.year() == __y.year()
1667 && __x.month() == __y.month()
1668 && __x.day() == __y.day();
1671 friend constexpr strong_ordering
1672 operator<=>(const year_month_day& __x, const year_month_day& __y) noexcept
1675 template<typename = __detail::__months_years_conversion_disambiguator>
1676 friend constexpr year_month_day
1677 operator+(const year_month_day& __ymd, const months& __dm) noexcept
1678 { return (__ymd.year() / __ymd.month() + __dm) / __ymd.day(); }
1680 template<typename = __detail::__months_years_conversion_disambiguator>
1681 friend constexpr year_month_day
1682 operator+(const months& __dm, const year_month_day& __ymd) noexcept
1683 { return __ymd + __dm; }
1685 friend constexpr year_month_day
1686 operator+(const year_month_day& __ymd, const years& __dy) noexcept
1687 { return (__ymd.year() + __dy) / __ymd.month() / __ymd.day(); }
1689 friend constexpr year_month_day
1690 operator+(const years& __dy, const year_month_day& __ymd) noexcept
1691 { return __ymd + __dy; }
1693 template<typename = __detail::__months_years_conversion_disambiguator>
1694 friend constexpr year_month_day
1695 operator-(const year_month_day& __ymd, const months& __dm) noexcept
1696 { return __ymd + -__dm; }
1698 friend constexpr year_month_day
1699 operator-(const year_month_day& __ymd, const years& __dy) noexcept
1700 { return __ymd + -__dy; }
1702 friend constexpr year_month_day
1703 operator/(const year_month& __ym, const chrono::day& __d) noexcept
1704 { return {__ym.year(), __ym.month(), __d}; }
1706 friend constexpr year_month_day
1707 operator/(const year_month& __ym, int __d) noexcept
1708 { return __ym / chrono::day{unsigned(__d)}; }
1710 friend constexpr year_month_day
1711 operator/(const chrono::year& __y, const month_day& __md) noexcept
1712 { return __y / __md.month() / __md.day(); }
1714 friend constexpr year_month_day
1715 operator/(int __y, const month_day& __md) noexcept
1716 { return chrono::year{__y} / __md; }
1718 friend constexpr year_month_day
1719 operator/(const month_day& __md, const chrono::year& __y) noexcept
1720 { return __y / __md; }
1722 friend constexpr year_month_day
1723 operator/(const month_day& __md, int __y) noexcept
1724 { return chrono::year(__y) / __md; }
1727 // Construct from days since 1970/01/01.
1728 // Proposition 6.3 of Neri and Schneider,
1729 // "Euclidean Affine Functions and Applications to Calendar Algorithms".
1730 // https://arxiv.org/abs/2102.06959
1731 constexpr year_month_day
1732 year_month_day::_S_from_days(const days& __dp) noexcept
1734 constexpr auto __z2 = static_cast<uint32_t>(-1468000);
1735 constexpr auto __r2_e3 = static_cast<uint32_t>(536895458);
1737 const auto __r0 = static_cast<uint32_t>(__dp.count()) + __r2_e3;
1739 const auto __n1 = 4 * __r0 + 3;
1740 const auto __q1 = __n1 / 146097;
1741 const auto __r1 = __n1 % 146097 / 4;
1743 constexpr auto __p32 = static_cast<uint64_t>(1) << 32;
1744 const auto __n2 = 4 * __r1 + 3;
1745 const auto __u2 = static_cast<uint64_t>(2939745) * __n2;
1746 const auto __q2 = static_cast<uint32_t>(__u2 / __p32);
1747 const auto __r2 = static_cast<uint32_t>(__u2 % __p32) / 2939745 / 4;
1749 constexpr auto __p16 = static_cast<uint32_t>(1) << 16;
1750 const auto __n3 = 2141 * __r2 + 197913;
1751 const auto __q3 = __n3 / __p16;
1752 const auto __r3 = __n3 % __p16 / 2141;
1754 const auto __y0 = 100 * __q1 + __q2;
1755 const auto __m0 = __q3;
1756 const auto __d0 = __r3;
1758 const auto __j = __r2 >= 306;
1759 const auto __y1 = __y0 + __j;
1760 const auto __m1 = __j ? __m0 - 12 : __m0;
1761 const auto __d1 = __d0 + 1;
1763 return year_month_day{chrono::year{static_cast<int>(__y1 + __z2)},
1764 chrono::month{__m1}, chrono::day{__d1}};
1767 // Days since 1970/01/01.
1768 // Proposition 6.2 of Neri and Schneider,
1769 // "Euclidean Affine Functions and Applications to Calendar Algorithms".
1770 // https://arxiv.org/abs/2102.06959
1772 year_month_day::_M_days_since_epoch() const noexcept
1774 auto constexpr __z2 = static_cast<uint32_t>(-1468000);
1775 auto constexpr __r2_e3 = static_cast<uint32_t>(536895458);
1777 const auto __y1 = static_cast<uint32_t>(static_cast<int>(_M_y)) - __z2;
1778 const auto __m1 = static_cast<uint32_t>(static_cast<unsigned>(_M_m));
1779 const auto __d1 = static_cast<uint32_t>(static_cast<unsigned>(_M_d));
1781 const auto __j = static_cast<uint32_t>(__m1 < 3);
1782 const auto __y0 = __y1 - __j;
1783 const auto __m0 = __j ? __m1 + 12 : __m1;
1784 const auto __d0 = __d1 - 1;
1786 const auto __q1 = __y0 / 100;
1787 const auto __yc = 1461 * __y0 / 4 - __q1 + __q1 / 4;
1788 const auto __mc = (979 *__m0 - 2919) / 32;
1789 const auto __dc = __d0;
1791 return days{static_cast<int32_t>(__yc + __mc + __dc - __r2_e3)};
1794 // YEAR_MONTH_DAY_LAST
1796 class year_month_day_last
1800 chrono::month_day_last _M_mdl;
1804 year_month_day_last(const chrono::year& __y,
1805 const chrono::month_day_last& __mdl) noexcept
1806 : _M_y{__y}, _M_mdl{__mdl}
1809 template<typename = __detail::__months_years_conversion_disambiguator>
1810 constexpr year_month_day_last&
1811 operator+=(const months& __m) noexcept
1813 *this = *this + __m;
1817 template<typename = __detail::__months_years_conversion_disambiguator>
1818 constexpr year_month_day_last&
1819 operator-=(const months& __m) noexcept
1821 *this = *this - __m;
1825 constexpr year_month_day_last&
1826 operator+=(const years& __y) noexcept
1828 *this = *this + __y;
1832 constexpr year_month_day_last&
1833 operator-=(const years& __y) noexcept
1835 *this = *this - __y;
1839 constexpr chrono::year
1840 year() const noexcept
1843 constexpr chrono::month
1844 month() const noexcept
1845 { return _M_mdl.month(); }
1847 constexpr chrono::month_day_last
1848 month_day_last() const noexcept
1851 // Return A day representing the last day of this year, month pair.
1852 constexpr chrono::day
1853 day() const noexcept
1855 const auto __m = static_cast<unsigned>(month());
1857 // The result is unspecified if __m < 1 or __m > 12. Hence, assume
1858 // 1 <= __m <= 12. For __m != 2, day() == 30 or day() == 31 or, in
1859 // other words, day () == 30 | b, where b is in {0, 1}.
1861 // If __m in {1, 3, 4, 5, 6, 7}, then b is 1 if, and only if, __m is
1862 // odd. Hence, b = __m & 1 = (__m ^ 0) & 1.
1864 // If __m in {8, 9, 10, 11, 12}, then b is 1 if, and only if, __m is
1865 // even. Hence, b = (__m ^ 1) & 1.
1867 // Therefore, b = (__m ^ c) & 1, where c = 0, if __m < 8, or c = 1 if
1868 // __m >= 8, that is, c = __m >> 3.
1870 // Since 30 = (11110)_2 and __m <= 31 = (11111)_2, the "& 1" in b's
1871 // calculation is unnecessary.
1873 // The performance of this implementation does not depend on look-up
1874 // tables being on the L1 cache.
1875 return chrono::day{__m != 2 ? (__m ^ (__m >> 3)) | 30
1876 : _M_y.is_leap() ? 29 : 28};
1880 operator sys_days() const noexcept
1881 { return sys_days{year() / month() / day()}; }
1884 operator local_days() const noexcept
1885 { return local_days{sys_days{*this}.time_since_epoch()}; }
1889 { return _M_y.ok() && _M_mdl.ok(); }
1891 friend constexpr bool
1892 operator==(const year_month_day_last& __x,
1893 const year_month_day_last& __y) noexcept
1895 return __x.year() == __y.year()
1896 && __x.month_day_last() == __y.month_day_last();
1899 friend constexpr strong_ordering
1900 operator<=>(const year_month_day_last& __x,
1901 const year_month_day_last& __y) noexcept
1904 template<typename = __detail::__months_years_conversion_disambiguator>
1905 friend constexpr year_month_day_last
1906 operator+(const year_month_day_last& __ymdl,
1907 const months& __dm) noexcept
1908 { return (__ymdl.year() / __ymdl.month() + __dm) / last; }
1910 template<typename = __detail::__months_years_conversion_disambiguator>
1911 friend constexpr year_month_day_last
1912 operator+(const months& __dm,
1913 const year_month_day_last& __ymdl) noexcept
1914 { return __ymdl + __dm; }
1916 template<typename = __detail::__months_years_conversion_disambiguator>
1917 friend constexpr year_month_day_last
1918 operator-(const year_month_day_last& __ymdl,
1919 const months& __dm) noexcept
1920 { return __ymdl + -__dm; }
1922 friend constexpr year_month_day_last
1923 operator+(const year_month_day_last& __ymdl,
1924 const years& __dy) noexcept
1925 { return {__ymdl.year() + __dy, __ymdl.month_day_last()}; }
1927 friend constexpr year_month_day_last
1928 operator+(const years& __dy,
1929 const year_month_day_last& __ymdl) noexcept
1930 { return __ymdl + __dy; }
1932 friend constexpr year_month_day_last
1933 operator-(const year_month_day_last& __ymdl,
1934 const years& __dy) noexcept
1935 { return __ymdl + -__dy; }
1937 friend constexpr year_month_day_last
1938 operator/(const year_month& __ym, last_spec) noexcept
1939 { return {__ym.year(), chrono::month_day_last{__ym.month()}}; }
1941 friend constexpr year_month_day_last
1942 operator/(const chrono::year& __y,
1943 const chrono::month_day_last& __mdl) noexcept
1944 { return {__y, __mdl}; }
1946 friend constexpr year_month_day_last
1947 operator/(int __y, const chrono::month_day_last& __mdl) noexcept
1948 { return chrono::year(__y) / __mdl; }
1950 friend constexpr year_month_day_last
1951 operator/(const chrono::month_day_last& __mdl,
1952 const chrono::year& __y) noexcept
1953 { return __y / __mdl; }
1955 friend constexpr year_month_day_last
1956 operator/(const chrono::month_day_last& __mdl, int __y) noexcept
1957 { return chrono::year(__y) / __mdl; }
1960 // year_month_day ctor from year_month_day_last
1962 year_month_day::year_month_day(const year_month_day_last& __ymdl) noexcept
1963 : _M_y{__ymdl.year()}, _M_m{__ymdl.month()}, _M_d{__ymdl.day()}
1967 year_month_day::ok() const noexcept
1969 if (!_M_y.ok() || !_M_m.ok())
1971 return chrono::day{1} <= _M_d && _M_d <= (_M_y / _M_m / last).day();
1974 // YEAR_MONTH_WEEKDAY
1976 class year_month_weekday
1981 chrono::weekday_indexed _M_wdi;
1983 static constexpr year_month_weekday
1984 _S_from_sys_days(const sys_days& __dp)
1986 year_month_day __ymd{__dp};
1987 chrono::weekday __wd{__dp};
1988 auto __index = __wd[(unsigned{__ymd.day()} - 1) / 7 + 1];
1989 return {__ymd.year(), __ymd.month(), __index};
1993 year_month_weekday() = default;
1996 year_month_weekday(const chrono::year& __y, const chrono::month& __m,
1997 const chrono::weekday_indexed& __wdi) noexcept
1998 : _M_y{__y}, _M_m{__m}, _M_wdi{__wdi}
2002 year_month_weekday(const sys_days& __dp) noexcept
2003 : year_month_weekday{_S_from_sys_days(__dp)}
2007 year_month_weekday(const local_days& __dp) noexcept
2008 : year_month_weekday{sys_days{__dp.time_since_epoch()}}
2011 template<typename = __detail::__months_years_conversion_disambiguator>
2012 constexpr year_month_weekday&
2013 operator+=(const months& __m) noexcept
2015 *this = *this + __m;
2019 template<typename = __detail::__months_years_conversion_disambiguator>
2020 constexpr year_month_weekday&
2021 operator-=(const months& __m) noexcept
2023 *this = *this - __m;
2027 constexpr year_month_weekday&
2028 operator+=(const years& __y) noexcept
2030 *this = *this + __y;
2034 constexpr year_month_weekday&
2035 operator-=(const years& __y) noexcept
2037 *this = *this - __y;
2041 constexpr chrono::year
2042 year() const noexcept
2045 constexpr chrono::month
2046 month() const noexcept
2049 constexpr chrono::weekday
2050 weekday() const noexcept
2051 { return _M_wdi.weekday(); }
2054 index() const noexcept
2055 { return _M_wdi.index(); }
2057 constexpr chrono::weekday_indexed
2058 weekday_indexed() const noexcept
2062 operator sys_days() const noexcept
2064 auto __d = sys_days{year() / month() / 1};
2065 return __d + (weekday() - chrono::weekday(__d)
2066 + days{(static_cast<int>(index())-1)*7});
2070 operator local_days() const noexcept
2071 { return local_days{sys_days{*this}.time_since_epoch()}; }
2076 if (!_M_y.ok() || !_M_m.ok() || !_M_wdi.ok())
2078 if (_M_wdi.index() <= 4)
2080 days __d = (_M_wdi.weekday()
2081 - chrono::weekday{sys_days{_M_y / _M_m / 1}}
2082 + days((_M_wdi.index()-1)*7 + 1));
2083 __glibcxx_assert(__d.count() >= 1);
2084 return (unsigned)__d.count() <= (unsigned)(_M_y / _M_m / last).day();
2087 friend constexpr bool
2088 operator==(const year_month_weekday& __x,
2089 const year_month_weekday& __y) noexcept
2091 return __x.year() == __y.year()
2092 && __x.month() == __y.month()
2093 && __x.weekday_indexed() == __y.weekday_indexed();
2096 template<typename = __detail::__months_years_conversion_disambiguator>
2097 friend constexpr year_month_weekday
2098 operator+(const year_month_weekday& __ymwd, const months& __dm) noexcept
2100 return ((__ymwd.year() / __ymwd.month() + __dm)
2101 / __ymwd.weekday_indexed());
2104 template<typename = __detail::__months_years_conversion_disambiguator>
2105 friend constexpr year_month_weekday
2106 operator+(const months& __dm, const year_month_weekday& __ymwd) noexcept
2107 { return __ymwd + __dm; }
2109 friend constexpr year_month_weekday
2110 operator+(const year_month_weekday& __ymwd, const years& __dy) noexcept
2111 { return {__ymwd.year() + __dy, __ymwd.month(), __ymwd.weekday_indexed()}; }
2113 friend constexpr year_month_weekday
2114 operator+(const years& __dy, const year_month_weekday& __ymwd) noexcept
2115 { return __ymwd + __dy; }
2117 template<typename = __detail::__months_years_conversion_disambiguator>
2118 friend constexpr year_month_weekday
2119 operator-(const year_month_weekday& __ymwd, const months& __dm) noexcept
2120 { return __ymwd + -__dm; }
2122 friend constexpr year_month_weekday
2123 operator-(const year_month_weekday& __ymwd, const years& __dy) noexcept
2124 { return __ymwd + -__dy; }
2126 friend constexpr year_month_weekday
2127 operator/(const year_month& __ym,
2128 const chrono::weekday_indexed& __wdi) noexcept
2129 { return {__ym.year(), __ym.month(), __wdi}; }
2131 friend constexpr year_month_weekday
2132 operator/(const chrono::year& __y, const month_weekday& __mwd) noexcept
2133 { return {__y, __mwd.month(), __mwd.weekday_indexed()}; }
2135 friend constexpr year_month_weekday
2136 operator/(int __y, const month_weekday& __mwd) noexcept
2137 { return chrono::year(__y) / __mwd; }
2139 friend constexpr year_month_weekday
2140 operator/(const month_weekday& __mwd, const chrono::year& __y) noexcept
2141 { return __y / __mwd; }
2143 friend constexpr year_month_weekday
2144 operator/(const month_weekday& __mwd, int __y) noexcept
2145 { return chrono::year(__y) / __mwd; }
2148 // YEAR_MONTH_WEEKDAY_LAST
2150 class year_month_weekday_last
2155 chrono::weekday_last _M_wdl;
2159 year_month_weekday_last(const chrono::year& __y, const chrono::month& __m,
2160 const chrono::weekday_last& __wdl) noexcept
2161 : _M_y{__y}, _M_m{__m}, _M_wdl{__wdl}
2164 template<typename = __detail::__months_years_conversion_disambiguator>
2165 constexpr year_month_weekday_last&
2166 operator+=(const months& __m) noexcept
2168 *this = *this + __m;
2172 template<typename = __detail::__months_years_conversion_disambiguator>
2173 constexpr year_month_weekday_last&
2174 operator-=(const months& __m) noexcept
2176 *this = *this - __m;
2180 constexpr year_month_weekday_last&
2181 operator+=(const years& __y) noexcept
2183 *this = *this + __y;
2187 constexpr year_month_weekday_last&
2188 operator-=(const years& __y) noexcept
2190 *this = *this - __y;
2194 constexpr chrono::year
2195 year() const noexcept
2198 constexpr chrono::month
2199 month() const noexcept
2202 constexpr chrono::weekday
2203 weekday() const noexcept
2204 { return _M_wdl.weekday(); }
2206 constexpr chrono::weekday_last
2207 weekday_last() const noexcept
2211 operator sys_days() const noexcept
2213 const auto __d = sys_days{_M_y / _M_m / last};
2214 return sys_days{(__d - (chrono::weekday{__d}
2215 - _M_wdl.weekday())).time_since_epoch()};
2219 operator local_days() const noexcept
2220 { return local_days{sys_days{*this}.time_since_epoch()}; }
2224 { return _M_y.ok() && _M_m.ok() && _M_wdl.ok(); }
2226 friend constexpr bool
2227 operator==(const year_month_weekday_last& __x,
2228 const year_month_weekday_last& __y) noexcept
2230 return __x.year() == __y.year()
2231 && __x.month() == __y.month()
2232 && __x.weekday_last() == __y.weekday_last();
2235 template<typename = __detail::__months_years_conversion_disambiguator>
2236 friend constexpr year_month_weekday_last
2237 operator+(const year_month_weekday_last& __ymwdl,
2238 const months& __dm) noexcept
2240 return ((__ymwdl.year() / __ymwdl.month() + __dm)
2241 / __ymwdl.weekday_last());
2244 template<typename = __detail::__months_years_conversion_disambiguator>
2245 friend constexpr year_month_weekday_last
2246 operator+(const months& __dm,
2247 const year_month_weekday_last& __ymwdl) noexcept
2248 { return __ymwdl + __dm; }
2250 friend constexpr year_month_weekday_last
2251 operator+(const year_month_weekday_last& __ymwdl,
2252 const years& __dy) noexcept
2253 { return {__ymwdl.year() + __dy, __ymwdl.month(), __ymwdl.weekday_last()}; }
2255 friend constexpr year_month_weekday_last
2256 operator+(const years& __dy,
2257 const year_month_weekday_last& __ymwdl) noexcept
2258 { return __ymwdl + __dy; }
2260 template<typename = __detail::__months_years_conversion_disambiguator>
2261 friend constexpr year_month_weekday_last
2262 operator-(const year_month_weekday_last& __ymwdl,
2263 const months& __dm) noexcept
2264 { return __ymwdl + -__dm; }
2266 friend constexpr year_month_weekday_last
2267 operator-(const year_month_weekday_last& __ymwdl,
2268 const years& __dy) noexcept
2269 { return __ymwdl + -__dy; }
2271 friend constexpr year_month_weekday_last
2272 operator/(const year_month& __ym,
2273 const chrono::weekday_last& __wdl) noexcept
2274 { return {__ym.year(), __ym.month(), __wdl}; }
2276 friend constexpr year_month_weekday_last
2277 operator/(const chrono::year& __y,
2278 const chrono::month_weekday_last& __mwdl) noexcept
2279 { return {__y, __mwdl.month(), __mwdl.weekday_last()}; }
2281 friend constexpr year_month_weekday_last
2282 operator/(int __y, const chrono::month_weekday_last& __mwdl) noexcept
2283 { return chrono::year(__y) / __mwdl; }
2285 friend constexpr year_month_weekday_last
2286 operator/(const chrono::month_weekday_last& __mwdl,
2287 const chrono::year& __y) noexcept
2288 { return __y / __mwdl; }
2290 friend constexpr year_month_weekday_last
2291 operator/(const chrono::month_weekday_last& __mwdl, int __y) noexcept
2292 { return chrono::year(__y) / __mwdl; }
2297 /// @cond undocumented
2301 __pow10(unsigned __n)
2309 template<typename _Duration> struct __utc_leap_second;
2313 /** Utility for splitting a duration into hours, minutes, and seconds
2315 * This is a convenience type that provides accessors for the constituent
2316 * parts (hours, minutes, seconds and subseconds) of a duration.
2320 template<typename _Duration>
2323 static_assert( __is_duration<_Duration>::value );
2326 static consteval int
2327 _S_fractional_width()
2329 auto __den = _Duration::period::den;
2330 const int __multiplicity_2 = std::__countr_zero((uintmax_t)__den);
2331 __den >>= __multiplicity_2;
2332 int __multiplicity_5 = 0;
2333 while ((__den % 5) == 0)
2341 int __width = (__multiplicity_2 > __multiplicity_5
2342 ? __multiplicity_2 : __multiplicity_5);
2349 hh_mm_ss(_Duration __d, bool __is_neg)
2350 : _M_h (duration_cast<chrono::hours>(__d)),
2351 _M_m (duration_cast<chrono::minutes>(__d - hours())),
2352 _M_s (duration_cast<chrono::seconds>(__d - hours() - minutes())),
2355 auto __ss = __d - hours() - minutes() - seconds();
2356 if constexpr (treat_as_floating_point_v<typename precision::rep>)
2357 _M_ss._M_r = __ss.count();
2358 else if constexpr (precision::period::den != 1)
2359 _M_ss._M_r = duration_cast<precision>(__ss).count();
2362 static constexpr _Duration
2363 _S_abs(_Duration __d)
2365 if constexpr (numeric_limits<typename _Duration::rep>::is_signed)
2366 return chrono::abs(__d);
2372 static constexpr unsigned fractional_width = {_S_fractional_width()};
2375 = duration<common_type_t<typename _Duration::rep,
2376 chrono::seconds::rep>,
2377 ratio<1, __detail::__pow10(fractional_width)>>;
2379 constexpr hh_mm_ss() noexcept = default;
2382 hh_mm_ss(_Duration __d)
2383 : hh_mm_ss(_S_abs(__d), __d < _Duration::zero())
2387 is_negative() const noexcept
2389 if constexpr (!_S_is_unsigned)
2395 constexpr chrono::hours
2396 hours() const noexcept
2399 constexpr chrono::minutes
2400 minutes() const noexcept
2403 constexpr chrono::seconds
2404 seconds() const noexcept
2408 subseconds() const noexcept
2409 { return static_cast<precision>(_M_ss); }
2412 operator precision() const noexcept
2413 { return to_duration(); }
2416 to_duration() const noexcept
2418 if constexpr (!_S_is_unsigned)
2420 return -(_M_h + _M_m + _M_s + subseconds());
2421 return _M_h + _M_m + _M_s + subseconds();
2425 static constexpr bool _S_is_unsigned
2426 = __and_v<is_integral<typename _Duration::rep>,
2427 is_unsigned<typename _Duration::rep>>;
2429 template<typename _Ratio>
2430 using __byte_duration = duration<unsigned char, _Ratio>;
2432 // The type of the _M_ss member that holds the subsecond precision.
2433 template<typename _Dur>
2436 typename _Dur::rep _M_r{};
2439 operator _Dur() const noexcept
2440 { return _Dur(_M_r); }
2443 // An empty class if this precision doesn't need subseconds.
2444 template<typename _Rep>
2445 requires (!treat_as_floating_point_v<_Rep>)
2446 struct __subseconds<duration<_Rep, ratio<1>>>
2449 operator duration<_Rep, ratio<1>>() const noexcept
2453 template<typename _Rep, typename _Period>
2454 requires (!treat_as_floating_point_v<_Rep>)
2455 && ratio_less_v<_Period, ratio<1, 1>>
2456 && ratio_greater_equal_v<_Period, ratio<1, 250>>
2457 struct __subseconds<duration<_Rep, _Period>>
2459 unsigned char _M_r{};
2462 operator duration<_Rep, _Period>() const noexcept
2463 { return duration<_Rep, _Period>(_M_r); }
2466 template<typename _Rep, typename _Period>
2467 requires (!treat_as_floating_point_v<_Rep>)
2468 && ratio_less_v<_Period, ratio<1, 250>>
2469 && ratio_greater_equal_v<_Period, ratio<1, 4000000000>>
2470 struct __subseconds<duration<_Rep, _Period>>
2472 uint_least32_t _M_r{};
2475 operator duration<_Rep, _Period>() const noexcept
2476 { return duration<_Rep, _Period>(_M_r); }
2479 chrono::hours _M_h{};
2480 __byte_duration<ratio<60>> _M_m{};
2481 __byte_duration<ratio<1>> _M_s{};
2483 __subseconds<precision> _M_ss{};
2485 template<typename> friend struct __detail::__utc_leap_second;
2488 /// @cond undocumented
2491 // Represents a time that is within a leap second insertion.
2492 template<typename _Duration>
2493 struct __utc_leap_second
2496 __utc_leap_second(const sys_time<_Duration>& __s)
2497 : _M_date(chrono::floor<days>(__s)), _M_time(__s - _M_date)
2503 hh_mm_ss<common_type_t<_Duration, days>> _M_time;
2508 // 12/24 HOURS FUNCTIONS
2511 is_am(const hours& __h) noexcept
2512 { return 0h <= __h && __h <= 11h; }
2515 is_pm(const hours& __h) noexcept
2516 { return 12h <= __h && __h <= 23h; }
2519 make12(const hours& __h) noexcept
2529 make24(const hours& __h, bool __is_pm) noexcept
2548#if _GLIBCXX_USE_CXX11_ABI || ! _GLIBCXX_USE_DUAL_ABI
2549 // C++20 [time.zones] Time zones
2564 static constexpr int unique = 0;
2565 static constexpr int nonexistent = 1;
2566 static constexpr int ambiguous = 2;
2573 class nonexistent_local_time : public runtime_error
2576 template<typename _Duration>
2577 nonexistent_local_time(const local_time<_Duration>& __tp,
2578 const local_info& __i)
2579 : runtime_error(_S_make_what_str(__tp, __i))
2580 { __glibcxx_assert(__i.result == local_info::nonexistent); }
2583 template<typename _Duration>
2585 _S_make_what_str(const local_time<_Duration>& __tp,
2586 const local_info& __i)
2588 std::ostringstream __os;
2589 __os << __tp << " is in a gap between\n"
2590 << local_seconds(__i.first.end.time_since_epoch())
2591 + __i.first.offset << ' ' << __i.first.abbrev << " and\n"
2592 << local_seconds(__i.second.begin.time_since_epoch())
2593 + __i.second.offset << ' ' << __i.second.abbrev
2594 << " which are both equivalent to\n"
2595 << __i.first.end << " UTC";
2596 return std::move(__os).str();
2600 class ambiguous_local_time : public runtime_error
2603 template<typename _Duration>
2604 ambiguous_local_time(const local_time<_Duration>& __tp,
2605 const local_info& __i)
2606 : runtime_error(_S_make_what_str(__tp, __i))
2607 { __glibcxx_assert(__i.result == local_info::ambiguous); }
2610 template<typename _Duration>
2612 _S_make_what_str(const local_time<_Duration>& __tp,
2613 const local_info& __i)
2615 std::ostringstream __os;
2616 __os << __tp << " is ambiguous. It could be\n"
2617 << __tp << ' ' << __i.first.abbrev << " == "
2618 << __tp - __i.first.offset << " UTC or\n"
2619 << __tp << ' ' << __i.second.abbrev << " == "
2620 << __tp - __i.second.offset << " UTC";
2621 return std::move(__os).str();
2625 template<typename _Duration>
2627 __throw_bad_local_time(const local_time<_Duration>& __tp,
2628 const local_info& __i)
2631 if (__i.result == local_info::nonexistent)
2632 throw nonexistent_local_time(__tp, __i);
2633 throw ambiguous_local_time(__tp, __i);
2639 enum class choose { earliest, latest };
2644 time_zone(time_zone&&) = default;
2645 time_zone& operator=(time_zone&&) = default;
2650 string_view name() const noexcept { return _M_name; }
2652 template<typename _Duration>
2654 get_info(const sys_time<_Duration>& __st) const
2655 { return _M_get_sys_info(chrono::floor<seconds>(__st)); }
2657 template<typename _Duration>
2659 get_info(const local_time<_Duration>& __tp) const
2660 { return _M_get_local_info(chrono::floor<seconds>(__tp)); }
2662 template<typename _Duration>
2663 sys_time<common_type_t<_Duration, seconds>>
2664 to_sys(const local_time<_Duration>& __tp) const
2666 local_info __info = get_info(__tp);
2668 if (__info.result != local_info::unique)
2669 __throw_bad_local_time(__tp, __info);
2671 return sys_time<_Duration>(__tp.time_since_epoch())
2672 - __info.first.offset;
2675 template<typename _Duration>
2676 sys_time<common_type_t<_Duration, seconds>>
2677 to_sys(const local_time<_Duration>& __tp, choose __z) const
2679 local_info __info = get_info(__tp);
2681 if (__info.result == local_info::nonexistent)
2682 return __info.first.end; // Last second of the previous sys_info.
2684 sys_time<_Duration> __st(__tp.time_since_epoch());
2686 if (__info.result == local_info::ambiguous && __z == choose::latest)
2687 return __st - __info.second.offset; // Time in the later sys_info.
2688 // else if __z == earliest, use __info.first.offset as below:
2690 return __st - __info.first.offset;
2693 template<typename _Duration>
2694 local_time<common_type_t<_Duration, seconds>>
2695 to_local(const sys_time<_Duration>& __tp) const
2697 auto __d = (__tp + get_info(__tp).offset).time_since_epoch();
2698 return local_time<common_type_t<_Duration, seconds>>(__d);
2701 [[nodiscard]] friend bool
2702 operator==(const time_zone& __x, const time_zone& __y) noexcept
2703 { return __x._M_name == __y._M_name; }
2705 [[nodiscard]] friend strong_ordering
2706 operator<=>(const time_zone& __x, const time_zone& __y) noexcept
2707 { return __x._M_name <=> __y._M_name; }
2710 sys_info _M_get_sys_info(sys_seconds) const;
2711 local_info _M_get_local_info(local_seconds) const;
2713 friend const tzdb& reload_tzdb();
2715 friend class tzdb_list;
2719 explicit time_zone(unique_ptr<_Impl> __p);
2721 unique_ptr<_Impl> _M_impl;
2724 const time_zone* locate_zone(string_view __tz_name);
2725 const time_zone* current_zone();
2727 /** The list of `chrono::tzdb` objects
2729 * A single object of this type is constructed by the C++ runtime,
2730 * and can be accessed by calling `chrono::get_tzdb_list()`.
2732 * The front of the list is the current `tzdb` object and can be accessed
2733 * via `chrono::get_tzdb_list().front()` or `chrono::get_tzdb()` or
2734 * `*chrono::get_tzdb_list().begin()`.
2736 * The `chrono::reload_tzdb()` function will check for a newer version
2737 * and if found, insert it at the front of the list.
2746 tzdb_list(const tzdb_list&) = delete;
2747 tzdb_list& operator=(const tzdb_list&) = delete;
2749 /** An iterator into the `tzdb_list`
2751 * As a extension, in libstdc++ each `tzdb` is reference-counted
2752 * and the `const_iterator` type shares ownership of the object it
2753 * refers to. This ensures that a `tzdb` erased from the list will
2754 * not be destroyed while there is an iterator that refers to it.
2756 class const_iterator
2759 using value_type = tzdb;
2760 using reference = const tzdb&;
2761 using pointer = const tzdb*;
2762 using difference_type = ptrdiff_t;
2763 using iterator_category = forward_iterator_tag;
2765 constexpr const_iterator() = default;
2766 const_iterator(const const_iterator&) = default;
2767 const_iterator(const_iterator&&) = default;
2768 const_iterator& operator=(const const_iterator&) = default;
2769 const_iterator& operator=(const_iterator&&) = default;
2771 reference operator*() const noexcept;
2772 pointer operator->() const noexcept { return &**this; }
2773 const_iterator& operator++();
2774 const_iterator operator++(int);
2776 bool operator==(const const_iterator&) const noexcept = default;
2779 explicit const_iterator(const shared_ptr<_Node>&) noexcept;
2781 friend class tzdb_list;
2783 shared_ptr<_Node> _M_node;
2784 void* _M_reserved = nullptr;
2787 /** Access the current `tzdb` at the front of the list.
2789 * This returns a reference to the same object as `chrono::get_tzdb()`.
2791 * @returns A reference to the current tzdb object.
2794 const tzdb& front() const noexcept;
2796 /** Remove the tzdb object _after_ the one the iterator refers to.
2798 * Calling this function concurrently with any of `front()`, `begin()`,
2799 * or `end()` does not cause a data race, but in general this function
2800 * is not thread-safe. The behaviour may be undefined if erasing an
2801 * element from the list while another thread is calling the same
2802 * function, or incrementing an iterator into the list, or accessing
2803 * the element being erased (unless it is accessed through an iterator).
2805 * @param __p A dereferenceable iterator.
2806 * @returns An iterator the element after the one that was erased
2807 * (or `end()` if there is no such element).
2810 const_iterator erase_after(const_iterator __p);
2812 const_iterator begin() const noexcept;
2813 const_iterator end() const noexcept { return {}; }
2814 const_iterator cbegin() const noexcept { return begin(); }
2815 const_iterator cend() const noexcept { return end(); }
2818 constexpr explicit tzdb_list(nullptr_t);
2820 friend tzdb_list& get_tzdb_list();
2821 friend const tzdb& get_tzdb();
2822 friend const tzdb& reload_tzdb();
2824 friend class leap_second;
2825 friend struct time_zone::_Impl;
2826 friend class time_zone_link;
2829 class time_zone_link
2832 time_zone_link(time_zone_link&&) = default;
2833 time_zone_link& operator=(time_zone_link&&) = default;
2835 string_view name() const noexcept { return _M_name; }
2836 string_view target() const noexcept { return _M_target; }
2839 operator==(const time_zone_link& __x, const time_zone_link& __y) noexcept
2840 { return __x.name() == __y.name(); }
2842 friend strong_ordering
2843 operator<=>(const time_zone_link& __x, const time_zone_link& __y) noexcept
2844 { return __x.name() <=> __y.name(); }
2847 friend const tzdb& reload_tzdb();
2848 friend struct tzdb_list::_Node;
2850 explicit time_zone_link(nullptr_t) { }
2859 leap_second(const leap_second&) = default;
2860 leap_second& operator=(const leap_second&) = default;
2863 constexpr sys_seconds
2864 date() const noexcept
2866 if (_M_s >= _M_s.zero()) [[likely]]
2867 return sys_seconds(_M_s);
2868 return sys_seconds(-_M_s);
2873 value() const noexcept
2875 if (_M_s >= _M_s.zero()) [[likely]]
2880 // This can be defaulted because the database will never contain two
2881 // leap_second objects with the same date but different signs.
2882 [[nodiscard]] friend constexpr bool
2883 operator==(const leap_second&, const leap_second&) noexcept = default;
2885 [[nodiscard]] friend constexpr strong_ordering
2886 operator<=>(const leap_second& __x, const leap_second& __y) noexcept
2887 { return __x.date() <=> __y.date(); }
2889 template<typename _Duration>
2890 [[nodiscard]] friend constexpr bool
2891 operator==(const leap_second& __x,
2892 const sys_time<_Duration>& __y) noexcept
2893 { return __x.date() == __y; }
2895 template<typename _Duration>
2896 [[nodiscard]] friend constexpr bool
2897 operator<(const leap_second& __x,
2898 const sys_time<_Duration>& __y) noexcept
2899 { return __x.date() < __y; }
2901 template<typename _Duration>
2902 [[nodiscard]] friend constexpr bool
2903 operator<(const sys_time<_Duration>& __x,
2904 const leap_second& __y) noexcept
2905 { return __x < __y.date(); }
2907 template<typename _Duration>
2908 [[nodiscard]] friend constexpr bool
2909 operator>(const leap_second& __x,
2910 const sys_time<_Duration>& __y) noexcept
2911 { return __y < __x.date(); }
2913 template<typename _Duration>
2914 [[nodiscard]] friend constexpr bool
2915 operator>(const sys_time<_Duration>& __x,
2916 const leap_second& __y) noexcept
2917 { return __y.date() < __x; }
2919 template<typename _Duration>
2920 [[nodiscard]] friend constexpr bool
2921 operator<=(const leap_second& __x,
2922 const sys_time<_Duration>& __y) noexcept
2923 { return !(__y < __x.date()); }
2925 template<typename _Duration>
2926 [[nodiscard]] friend constexpr bool
2927 operator<=(const sys_time<_Duration>& __x,
2928 const leap_second& __y) noexcept
2929 { return !(__y.date() < __x); }
2931 template<typename _Duration>
2932 [[nodiscard]] friend constexpr bool
2933 operator>=(const leap_second& __x,
2934 const sys_time<_Duration>& __y) noexcept
2935 { return !(__x.date() < __y); }
2937 template<typename _Duration>
2938 [[nodiscard]] friend constexpr bool
2939 operator>=(const sys_time<_Duration>& __x,
2940 const leap_second& __y) noexcept
2941 { return !(__x < __y.date()); }
2943 // This is a simplified form of the constraint specified in the standard,
2944 // three_way_comparable_with<sys_seconds, sys_time<_Duration>>.
2945 template<three_way_comparable_with<seconds> _Duration>
2946 [[nodiscard]] friend constexpr auto
2947 operator<=>(const leap_second& __x,
2948 const sys_time<_Duration>& __y) noexcept
2949 { return __x.date() <=> __y; }
2952 explicit leap_second(seconds::rep __s) : _M_s(__s) { }
2954 friend struct tzdb_list::_Node;
2956 friend const tzdb& reload_tzdb();
2958 template<typename _Duration>
2959 friend leap_second_info
2960 get_leap_second_info(const utc_time<_Duration>&);
2962 seconds _M_s; // == date().time_since_epoch() * value().count()
2965 template<class _Tp> struct zoned_traits { };
2968 struct zoned_traits<const time_zone*>
2970 static const time_zone*
2972 { return std::chrono::locate_zone("UTC"); }
2974 static const time_zone*
2975 locate_zone(string_view __name)
2976 { return std::chrono::locate_zone(__name); }
2982 _GLIBCXX_STD_C::vector<time_zone> zones;
2983 _GLIBCXX_STD_C::vector<time_zone_link> links;
2984 _GLIBCXX_STD_C::vector<leap_second> leap_seconds;
2987 locate_zone(string_view __tz_name) const;
2990 current_zone() const;
2993 friend const tzdb& reload_tzdb();
2994 friend class time_zone;
2995 friend struct tzdb_list::_Node;
2998 tzdb_list& get_tzdb_list();
2999 const tzdb& get_tzdb();
3001 const tzdb& reload_tzdb();
3002 string remote_version();
3004 template<typename _Duration, typename _TimeZonePtr = const time_zone*>
3007 static_assert(__is_duration_v<_Duration>);
3009 using _Traits = zoned_traits<_TimeZonePtr>;
3011 // Every constructor that accepts a string_view as its first parameter
3012 // does not participate in class template argument deduction.
3013 using string_view = type_identity_t<std::string_view>;
3016 using duration = common_type_t<_Duration, seconds>;
3018 zoned_time() requires requires { _Traits::default_zone(); }
3021 zoned_time(const zoned_time&) = default;
3022 zoned_time& operator=(const zoned_time&) = default;
3024 zoned_time(const sys_time<_Duration>& __st)
3025 requires requires { _Traits::default_zone(); }
3030 zoned_time(_TimeZonePtr __z) : _M_zone(std::move(__z)) { }
3033 zoned_time(string_view __name)
3035 _TimeZonePtr{_Traits::locate_zone(std::string_view{})};
3037 : _M_zone(_Traits::locate_zone(__name))
3040 template<typename _Duration2>
3041 zoned_time(const zoned_time<_Duration2, _TimeZonePtr>& __zt)
3042 requires is_convertible_v<sys_time<_Duration2>, sys_time<_Duration>>
3043 : _M_zone(__zt._M_zone), _M_tp(__zt._M_tp)
3046 zoned_time(_TimeZonePtr __z, const sys_time<_Duration>& __st)
3047 : _M_zone(std::move(__z)), _M_tp(__st)
3050 zoned_time(string_view __name, const sys_time<_Duration>& __st)
3051 : zoned_time(_Traits::locate_zone(__name), __st)
3054 zoned_time(_TimeZonePtr __z, const local_time<_Duration>& __tp)
3056 { __z->to_sys(__tp) } -> convertible_to<sys_time<_Duration>>;
3058 : _M_zone(std::move(__z)), _M_tp(_M_zone->to_sys(__tp))
3061 zoned_time(string_view __name, const local_time<_Duration>& __tp)
3062 requires requires (_TimeZonePtr __z) {
3063 { _Traits::locate_zone(__name) } -> convertible_to<_TimeZonePtr>;
3064 { __z->to_sys(__tp) } -> convertible_to<sys_time<_Duration>>;
3066 : zoned_time(_Traits::locate_zone(__name), __tp)
3069 zoned_time(_TimeZonePtr __z, const local_time<_Duration>& __tp,
3072 { __z->to_sys(__tp, __c) } -> convertible_to<sys_time<_Duration>>;
3074 : _M_zone(std::move(__z)), _M_tp(_M_zone->to_sys(__tp, __c))
3077 zoned_time(string_view __name, const local_time<_Duration>& __tp,
3079 requires requires (_TimeZonePtr __z) {
3080 { _Traits::locate_zone(__name) } -> convertible_to<_TimeZonePtr>;
3081 { __z->to_sys(__tp, __c) } -> convertible_to<sys_time<_Duration>>;
3083 : _M_zone(_Traits::locate_zone(__name)),
3084 _M_tp(_M_zone->to_sys(__tp, __c))
3087 template<typename _Duration2, typename _TimeZonePtr2>
3088 zoned_time(_TimeZonePtr __z,
3089 const zoned_time<_Duration2, _TimeZonePtr2>& __zt)
3090 requires is_convertible_v<sys_time<_Duration2>, sys_time<_Duration>>
3091 : _M_zone(__z), _M_tp(__zt._M_tp)
3094 template<typename _Duration2, typename _TimeZonePtr2>
3095 zoned_time(_TimeZonePtr __z,
3096 const zoned_time<_Duration2, _TimeZonePtr2>& __zt,
3098 requires is_convertible_v<sys_time<_Duration2>, sys_time<_Duration>>
3099 : _M_zone(__z), _M_tp(__zt._M_tp)
3102 template<typename _Duration2, typename _TimeZonePtr2>
3103 zoned_time(string_view __name,
3104 const zoned_time<_Duration2, _TimeZonePtr2>& __zt)
3105 requires is_convertible_v<sys_time<_Duration2>, sys_time<_Duration>>
3107 { _Traits::locate_zone(__name) } -> convertible_to<_TimeZonePtr>;
3109 : _M_zone(_Traits::locate_zone(__name)), _M_tp(__zt._M_tp)
3112 template<typename _Duration2, typename _TimeZonePtr2>
3113 zoned_time(string_view __name,
3114 const zoned_time<_Duration2, _TimeZonePtr2>& __zt,
3116 requires is_convertible_v<sys_time<_Duration2>, sys_time<_Duration>>
3118 { _Traits::locate_zone(__name) } -> convertible_to<_TimeZonePtr>;
3120 : _M_zone(_Traits::locate_zone(__name)), _M_tp(__zt._M_tp)
3124 operator=(const sys_time<_Duration>& __st)
3131 operator=(const local_time<_Duration>& __lt)
3133 _M_tp = _M_zone->to_sys(__lt);
3138 operator sys_time<duration>() const { return _M_tp; }
3141 explicit operator local_time<duration>() const
3142 { return get_local_time(); }
3146 get_time_zone() const
3150 local_time<duration>
3151 get_local_time() const
3152 { return _M_zone->to_local(_M_tp); }
3156 get_sys_time() const
3162 { return _M_zone->get_info(_M_tp); }
3164 [[nodiscard]] friend bool
3165 operator==(const zoned_time&, const zoned_time&) = default;
3168 _TimeZonePtr _M_zone{ _Traits::default_zone() };
3169 sys_time<duration> _M_tp{};
3171 template<typename _Duration2, typename _TimeZonePtr2>
3172 friend class zoned_time;
3175 zoned_time() -> zoned_time<seconds>;
3177 template<typename _Duration>
3178 zoned_time(sys_time<_Duration>)
3179 -> zoned_time<common_type_t<_Duration, seconds>>;
3181 /// @cond undocumented
3182 template<typename _TimeZonePtrOrName>
3183 using __time_zone_representation
3184 = __conditional_t<is_convertible_v<_TimeZonePtrOrName, string_view>,
3186 remove_cvref_t<_TimeZonePtrOrName>>;
3189 template<typename _TimeZonePtrOrName>
3190 zoned_time(_TimeZonePtrOrName&&)
3191 -> zoned_time<seconds, __time_zone_representation<_TimeZonePtrOrName>>;
3193 template<typename _TimeZonePtrOrName, typename _Duration>
3194 zoned_time(_TimeZonePtrOrName&&, sys_time<_Duration>)
3195 -> zoned_time<common_type_t<_Duration, seconds>,
3196 __time_zone_representation<_TimeZonePtrOrName>>;
3198 template<typename _TimeZonePtrOrName, typename _Duration>
3199 zoned_time(_TimeZonePtrOrName&&, local_time<_Duration>,
3200 choose = choose::earliest)
3201 -> zoned_time<common_type_t<_Duration, seconds>,
3202 __time_zone_representation<_TimeZonePtrOrName>>;
3204 template<typename _Duration, typename _TimeZonePtrOrName,
3205 typename _TimeZonePtr2>
3206 zoned_time(_TimeZonePtrOrName&&, zoned_time<_Duration, _TimeZonePtr2>,
3207 choose = choose::earliest)
3208 -> zoned_time<common_type_t<_Duration, seconds>,
3209 __time_zone_representation<_TimeZonePtrOrName>>;
3211 template<typename _Dur1, typename _TZPtr1, typename _Dur2, typename _TZPtr2>
3214 operator==(const zoned_time<_Dur1, _TZPtr1>& __x,
3215 const zoned_time<_Dur2, _TZPtr2>& __y)
3217 return __x.get_time_zone() == __y.get_time_zone()
3218 && __x.get_sys_time() == __y.get_sys_time();
3221 using zoned_seconds = zoned_time<seconds>;
3222#endif // _GLIBCXX_USE_CXX11_ABI || ! _GLIBCXX_USE_DUAL_ABI
3226 inline leap_second_info
3227 __get_leap_second_info(sys_seconds __ss, bool __is_utc)
3229 if (__ss < sys_seconds{}) [[unlikely]]
3232 const seconds::rep __leaps[] {
3233 78796800, // 1 Jul 1972
3234 94694400, // 1 Jan 1973
3235 126230400, // 1 Jan 1974
3236 157766400, // 1 Jan 1975
3237 189302400, // 1 Jan 1976
3238 220924800, // 1 Jan 1977
3239 252460800, // 1 Jan 1978
3240 283996800, // 1 Jan 1979
3241 315532800, // 1 Jan 1980
3242 362793600, // 1 Jul 1981
3243 394329600, // 1 Jul 1982
3244 425865600, // 1 Jul 1983
3245 489024000, // 1 Jul 1985
3246 567993600, // 1 Jan 1988
3247 631152000, // 1 Jan 1990
3248 662688000, // 1 Jan 1991
3249 709948800, // 1 Jul 1992
3250 741484800, // 1 Jul 1993
3251 773020800, // 1 Jul 1994
3252 820454400, // 1 Jan 1996
3253 867715200, // 1 Jul 1997
3254 915148800, // 1 Jan 1999
3255 1136073600, // 1 Jan 2006
3256 1230768000, // 1 Jan 2009
3257 1341100800, // 1 Jul 2012
3258 1435708800, // 1 Jul 2015
3259 1483228800, // 1 Jan 2017
3261 // The list above is known to be valid until (at least) this date
3262 // and only contains positive leap seconds.
3263 const sys_seconds __expires(1735344000s); // 2024-12-28 00:00:00 UTC
3265#if _GLIBCXX_USE_CXX11_ABI || ! _GLIBCXX_USE_DUAL_ABI
3266 if (__ss > __expires)
3268 // Use updated leap_seconds from tzdb.
3269 size_t __n = std::size(__leaps);
3271 auto __db = get_tzdb_list().begin();
3272 auto __first = __db->leap_seconds.begin() + __n;
3273 auto __last = __db->leap_seconds.end();
3274 auto __pos = std::upper_bound(__first, __last, __ss);
3275 seconds __elapsed(__n);
3276 for (auto __i = __first; __i != __pos; ++__i)
3277 __elapsed += __i->value();
3281 // Convert utc_time to sys_time:
3283 // See if that sys_time is before (or during) previous leap sec:
3284 if (__pos != __first && __ss < __pos[-1])
3286 if ((__ss + 1s) >= __pos[-1])
3287 return {true, __elapsed};
3288 __elapsed -= __pos[-1].value();
3291 return {false, __elapsed};
3296 seconds::rep __s = __ss.time_since_epoch().count();
3297 const seconds::rep* __first = std::begin(__leaps);
3298 const seconds::rep* __last = std::end(__leaps);
3300 // Don't bother searching the list if we're after the last one.
3301 if (__s > (__last[-1] + (__last - __first) + 1))
3302 return { false, seconds(__last - __first) };
3304 auto __pos = std::upper_bound(__first, __last, __s);
3305 seconds __elapsed{__pos - __first};
3308 // Convert utc_time to sys_time:
3309 __s -= __elapsed.count();
3310 // See if that sys_time is before (or during) previous leap sec:
3311 if (__pos != __first && __s < __pos[-1])
3313 if ((__s + 1) >= __pos[-1])
3314 return {true, __elapsed};
3318 return {false, __elapsed};
3321} // namespace __detail
3323 template<typename _Duration>
3325 inline leap_second_info
3326 get_leap_second_info(const utc_time<_Duration>& __ut)
3328 auto __s = chrono::duration_cast<seconds>(__ut.time_since_epoch());
3329 return __detail::__get_leap_second_info(sys_seconds(__s), true);
3332 template<typename _Duration>
3334 inline utc_time<common_type_t<_Duration, seconds>>
3335 utc_clock::from_sys(const sys_time<_Duration>& __t)
3337 using _CDur = common_type_t<_Duration, seconds>;
3338 auto __s = chrono::time_point_cast<seconds>(__t);
3339 const auto __li = __detail::__get_leap_second_info(__s, false);
3340 return utc_time<_CDur>{__t.time_since_epoch()} + __li.elapsed;
3342#endif // _GLIBCXX_HOSTED
3346 } // namespace chrono
3348#if __cplusplus >= 202002L
3349 inline namespace literals
3351 inline namespace chrono_literals
3353 /// @addtogroup chrono
3355#pragma GCC diagnostic push
3356#pragma GCC diagnostic ignored "-Wliteral-suffix"
3357 /// Literal suffix for creating chrono::day objects.
3359 constexpr chrono::day
3360 operator""d(unsigned long long __d) noexcept
3361 { return chrono::day{static_cast<unsigned>(__d)}; }
3363 /// Literal suffix for creating chrono::year objects.
3365 constexpr chrono::year
3366 operator""y(unsigned long long __y) noexcept
3367 { return chrono::year{static_cast<int>(__y)}; }
3368#pragma GCC diagnostic pop
3370 } // inline namespace chrono_literals
3371 } // inline namespace literals
3374_GLIBCXX_END_NAMESPACE_VERSION
3377#if __cplusplus >= 202002L && _GLIBCXX_HOSTED
3378# include <bits/chrono_io.h>
3383#endif //_GLIBCXX_CHRONO