1 // <chrono> -*- C++ -*-
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
33 #ifdef _GLIBCXX_SYSHDR
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>
63 namespace 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)
2311 /** Utility for splitting a duration into hours, minutes, and seconds
2313 * This is a convenience type that provides accessors for the constituent
2314 * parts (hours, minutes, seconds and subseconds) of a duration.
2318 template<typename _Duration>
2321 static_assert( __is_duration<_Duration>::value );
2324 static consteval int
2325 _S_fractional_width()
2327 auto __den = _Duration::period::den;
2328 const int __multiplicity_2 = std::__countr_zero((uintmax_t)__den);
2329 __den >>= __multiplicity_2;
2330 int __multiplicity_5 = 0;
2331 while ((__den % 5) == 0)
2339 int __width = (__multiplicity_2 > __multiplicity_5
2340 ? __multiplicity_2 : __multiplicity_5);
2347 hh_mm_ss(_Duration __d, bool __is_neg)
2348 : _M_h (duration_cast<chrono::hours>(__d)),
2349 _M_m (duration_cast<chrono::minutes>(__d - hours())),
2350 _M_s (duration_cast<chrono::seconds>(__d - hours() - minutes())),
2353 auto __ss = __d - hours() - minutes() - seconds();
2354 if constexpr (treat_as_floating_point_v<typename precision::rep>)
2355 _M_ss._M_r = __ss.count();
2356 else if constexpr (precision::period::den != 1)
2357 _M_ss._M_r = duration_cast<precision>(__ss).count();
2360 static constexpr _Duration
2361 _S_abs(_Duration __d)
2363 if constexpr (numeric_limits<typename _Duration::rep>::is_signed)
2364 return chrono::abs(__d);
2370 static constexpr unsigned fractional_width = {_S_fractional_width()};
2373 = duration<common_type_t<typename _Duration::rep,
2374 chrono::seconds::rep>,
2375 ratio<1, __detail::__pow10(fractional_width)>>;
2377 constexpr hh_mm_ss() noexcept = default;
2380 hh_mm_ss(_Duration __d)
2381 : hh_mm_ss(_S_abs(__d), __d < _Duration::zero())
2385 is_negative() const noexcept
2387 if constexpr (!_S_is_unsigned)
2393 constexpr chrono::hours
2394 hours() const noexcept
2397 constexpr chrono::minutes
2398 minutes() const noexcept
2401 constexpr chrono::seconds
2402 seconds() const noexcept
2406 subseconds() const noexcept
2407 { return static_cast<precision>(_M_ss); }
2410 operator precision() const noexcept
2411 { return to_duration(); }
2414 to_duration() const noexcept
2416 if constexpr (!_S_is_unsigned)
2418 return -(_M_h + _M_m + _M_s + subseconds());
2419 return _M_h + _M_m + _M_s + subseconds();
2423 static constexpr bool _S_is_unsigned
2424 = __and_v<is_integral<typename _Duration::rep>,
2425 is_unsigned<typename _Duration::rep>>;
2427 template<typename _Ratio>
2428 using __byte_duration = duration<unsigned char, _Ratio>;
2430 // The type of the _M_ss member that holds the subsecond precision.
2431 template<typename _Dur>
2434 typename _Dur::rep _M_r{};
2437 operator _Dur() const noexcept
2438 { return _Dur(_M_r); }
2441 // An empty class if this precision doesn't need subseconds.
2442 template<typename _Rep>
2443 requires (!treat_as_floating_point_v<_Rep>)
2444 struct __subseconds<duration<_Rep, ratio<1>>>
2447 operator duration<_Rep, ratio<1>>() const noexcept
2451 template<typename _Rep, typename _Period>
2452 requires (!treat_as_floating_point_v<_Rep>)
2453 && ratio_less_v<_Period, ratio<1, 1>>
2454 && ratio_greater_equal_v<_Period, ratio<1, 250>>
2455 struct __subseconds<duration<_Rep, _Period>>
2457 unsigned char _M_r{};
2460 operator duration<_Rep, _Period>() const noexcept
2461 { return duration<_Rep, _Period>(_M_r); }
2464 template<typename _Rep, typename _Period>
2465 requires (!treat_as_floating_point_v<_Rep>)
2466 && ratio_less_v<_Period, ratio<1, 250>>
2467 && ratio_greater_equal_v<_Period, ratio<1, 4000000000>>
2468 struct __subseconds<duration<_Rep, _Period>>
2470 uint_least32_t _M_r{};
2473 operator duration<_Rep, _Period>() const noexcept
2474 { return duration<_Rep, _Period>(_M_r); }
2477 chrono::hours _M_h{};
2478 __byte_duration<ratio<60>> _M_m{};
2479 __byte_duration<ratio<1>> _M_s{};
2481 __subseconds<precision> _M_ss{};
2484 // 12/24 HOURS FUNCTIONS
2487 is_am(const hours& __h) noexcept
2488 { return 0h <= __h && __h <= 11h; }
2491 is_pm(const hours& __h) noexcept
2492 { return 12h <= __h && __h <= 23h; }
2495 make12(const hours& __h) noexcept
2505 make24(const hours& __h, bool __is_pm) noexcept
2524 #if _GLIBCXX_USE_CXX11_ABI || ! _GLIBCXX_USE_DUAL_ABI
2525 // C++20 [time.zones] Time zones
2540 static constexpr int unique = 0;
2541 static constexpr int nonexistent = 1;
2542 static constexpr int ambiguous = 2;
2549 class nonexistent_local_time : public runtime_error
2552 template<typename _Duration>
2553 nonexistent_local_time(const local_time<_Duration>& __tp,
2554 const local_info& __i)
2555 : runtime_error(_S_make_what_str(__tp, __i))
2556 { __glibcxx_assert(__i.result == local_info::nonexistent); }
2559 template<typename _Duration>
2561 _S_make_what_str(const local_time<_Duration>& __tp,
2562 const local_info& __i)
2564 std::ostringstream __os;
2565 __os << __tp << " is in a gap between\n"
2566 << local_seconds(__i.first.end.time_since_epoch())
2567 + __i.first.offset << ' ' << __i.first.abbrev << " and\n"
2568 << local_seconds(__i.second.begin.time_since_epoch())
2569 + __i.second.offset << ' ' << __i.second.abbrev
2570 << " which are both equivalent to\n"
2571 << __i.first.end << " UTC";
2572 return std::move(__os).str();
2576 class ambiguous_local_time : public runtime_error
2579 template<typename _Duration>
2580 ambiguous_local_time(const local_time<_Duration>& __tp,
2581 const local_info& __i)
2582 : runtime_error(_S_make_what_str(__tp, __i))
2583 { __glibcxx_assert(__i.result == local_info::ambiguous); }
2586 template<typename _Duration>
2588 _S_make_what_str(const local_time<_Duration>& __tp,
2589 const local_info& __i)
2591 std::ostringstream __os;
2592 __os << __tp << " is ambiguous. It could be\n"
2593 << __tp << ' ' << __i.first.abbrev << " == "
2594 << __tp - __i.first.offset << " UTC or\n"
2595 << __tp << ' ' << __i.second.abbrev << " == "
2596 << __tp - __i.second.offset << " UTC";
2597 return std::move(__os).str();
2601 template<typename _Duration>
2603 __throw_bad_local_time(const local_time<_Duration>& __tp,
2604 const local_info& __i)
2606 #if __cpp_exceptions
2607 if (__i.result == local_info::nonexistent)
2608 throw nonexistent_local_time(__tp, __i);
2609 throw ambiguous_local_time(__tp, __i);
2615 enum class choose { earliest, latest };
2620 time_zone(time_zone&&) = default;
2621 time_zone& operator=(time_zone&&) = default;
2626 string_view name() const noexcept { return _M_name; }
2628 template<typename _Duration>
2630 get_info(const sys_time<_Duration>& __st) const
2631 { return _M_get_sys_info(chrono::floor<seconds>(__st)); }
2633 template<typename _Duration>
2635 get_info(const local_time<_Duration>& __tp) const
2636 { return _M_get_local_info(chrono::floor<seconds>(__tp)); }
2638 template<typename _Duration>
2639 sys_time<common_type_t<_Duration, seconds>>
2640 to_sys(const local_time<_Duration>& __tp) const
2642 local_info __info = get_info(__tp);
2644 if (__info.result != local_info::unique)
2645 __throw_bad_local_time(__tp, __info);
2647 return sys_time<_Duration>(__tp.time_since_epoch())
2648 - __info.first.offset;
2651 template<typename _Duration>
2652 sys_time<common_type_t<_Duration, seconds>>
2653 to_sys(const local_time<_Duration>& __tp, choose __z) const
2655 local_info __info = get_info(__tp);
2657 if (__info.result == local_info::nonexistent)
2658 return __info.first.end; // Last second of the previous sys_info.
2660 sys_time<_Duration> __st(__tp.time_since_epoch());
2662 if (__info.result == local_info::ambiguous && __z == choose::latest)
2663 return __st - __info.second.offset; // Time in the later sys_info.
2664 // else if __z == earliest, use __info.first.offset as below:
2666 return __st - __info.first.offset;
2669 template<typename _Duration>
2670 local_time<common_type_t<_Duration, seconds>>
2671 to_local(const sys_time<_Duration>& __tp) const
2673 auto __d = (__tp + get_info(__tp).offset).time_since_epoch();
2674 return local_time<common_type_t<_Duration, seconds>>(__d);
2677 [[nodiscard]] friend bool
2678 operator==(const time_zone& __x, const time_zone& __y) noexcept
2679 { return __x._M_name == __y._M_name; }
2681 [[nodiscard]] friend strong_ordering
2682 operator<=>(const time_zone& __x, const time_zone& __y) noexcept
2683 { return __x._M_name <=> __y._M_name; }
2686 sys_info _M_get_sys_info(sys_seconds) const;
2687 local_info _M_get_local_info(local_seconds) const;
2689 friend const tzdb& reload_tzdb();
2691 friend class tzdb_list;
2695 explicit time_zone(unique_ptr<_Impl> __p);
2697 unique_ptr<_Impl> _M_impl;
2700 const time_zone* locate_zone(string_view __tz_name);
2701 const time_zone* current_zone();
2703 /** The list of `chrono::tzdb` objects
2705 * A single object of this type is constructed by the C++ runtime,
2706 * and can be accessed by calling `chrono::get_tzdb_list()`.
2708 * The front of the list is the current `tzdb` object and can be accessed
2709 * via `chrono::get_tzdb_list().front()` or `chrono::get_tzdb()` or
2710 * `*chrono::get_tzdb_list().begin()`.
2712 * The `chrono::reload_tzdb()` function will check for a newer version
2713 * and if found, insert it at the front of the list.
2722 tzdb_list(const tzdb_list&) = delete;
2723 tzdb_list& operator=(const tzdb_list&) = delete;
2725 /** An iterator into the `tzdb_list`
2727 * As a extension, in libstdc++ each `tzdb` is reference-counted
2728 * and the `const_iterator` type shares ownership of the object it
2729 * refers to. This ensures that a `tzdb` erased from the list will
2730 * not be destroyed while there is an iterator that refers to it.
2732 class const_iterator
2735 using value_type = tzdb;
2736 using reference = const tzdb&;
2737 using pointer = const tzdb*;
2738 using difference_type = ptrdiff_t;
2739 using iterator_category = forward_iterator_tag;
2741 constexpr const_iterator() = default;
2742 const_iterator(const const_iterator&) = default;
2743 const_iterator(const_iterator&&) = default;
2744 const_iterator& operator=(const const_iterator&) = default;
2745 const_iterator& operator=(const_iterator&&) = default;
2747 reference operator*() const noexcept;
2748 pointer operator->() const noexcept { return &**this; }
2749 const_iterator& operator++();
2750 const_iterator operator++(int);
2752 bool operator==(const const_iterator&) const noexcept = default;
2755 explicit const_iterator(const shared_ptr<_Node>&) noexcept;
2757 friend class tzdb_list;
2759 shared_ptr<_Node> _M_node;
2760 void* _M_reserved = nullptr;
2763 /** Access the current `tzdb` at the front of the list.
2765 * This returns a reference to the same object as `chrono::get_tzdb()`.
2767 * @returns A reference to the current tzdb object.
2770 const tzdb& front() const noexcept;
2772 /** Remove the tzdb object _after_ the one the iterator refers to.
2774 * Calling this function concurrently with any of `front()`, `begin()`,
2775 * or `end()` does not cause a data race, but in general this function
2776 * is not thread-safe. The behaviour may be undefined if erasing an
2777 * element from the list while another thread is calling the same
2778 * function, or incrementing an iterator into the list, or accessing
2779 * the element being erased (unless it is accessed through an iterator).
2781 * @param __p A dereferenceable iterator.
2782 * @returns An iterator the element after the one that was erased
2783 * (or `end()` if there is no such element).
2786 const_iterator erase_after(const_iterator __p);
2788 const_iterator begin() const noexcept;
2789 const_iterator end() const noexcept { return {}; }
2790 const_iterator cbegin() const noexcept { return begin(); }
2791 const_iterator cend() const noexcept { return end(); }
2794 constexpr explicit tzdb_list(nullptr_t);
2796 friend tzdb_list& get_tzdb_list();
2797 friend const tzdb& get_tzdb();
2798 friend const tzdb& reload_tzdb();
2800 friend class leap_second;
2801 friend struct time_zone::_Impl;
2802 friend class time_zone_link;
2805 class time_zone_link
2808 time_zone_link(time_zone_link&&) = default;
2809 time_zone_link& operator=(time_zone_link&&) = default;
2811 string_view name() const noexcept { return _M_name; }
2812 string_view target() const noexcept { return _M_target; }
2815 operator==(const time_zone_link& __x, const time_zone_link& __y) noexcept
2816 { return __x.name() == __y.name(); }
2818 friend strong_ordering
2819 operator<=>(const time_zone_link& __x, const time_zone_link& __y) noexcept
2820 { return __x.name() <=> __y.name(); }
2823 friend const tzdb& reload_tzdb();
2824 friend struct tzdb_list::_Node;
2826 explicit time_zone_link(nullptr_t) { }
2835 leap_second(const leap_second&) = default;
2836 leap_second& operator=(const leap_second&) = default;
2839 constexpr sys_seconds
2840 date() const noexcept
2842 if (_M_s >= _M_s.zero()) [[likely]]
2843 return sys_seconds(_M_s);
2844 return sys_seconds(-_M_s);
2849 value() const noexcept
2851 if (_M_s >= _M_s.zero()) [[likely]]
2856 // This can be defaulted because the database will never contain two
2857 // leap_second objects with the same date but different signs.
2858 [[nodiscard]] friend constexpr bool
2859 operator==(const leap_second&, const leap_second&) noexcept = default;
2861 [[nodiscard]] friend constexpr strong_ordering
2862 operator<=>(const leap_second& __x, const leap_second& __y) noexcept
2863 { return __x.date() <=> __y.date(); }
2865 template<typename _Duration>
2866 [[nodiscard]] friend constexpr bool
2867 operator==(const leap_second& __x,
2868 const sys_time<_Duration>& __y) noexcept
2869 { return __x.date() == __y; }
2871 template<typename _Duration>
2872 [[nodiscard]] friend constexpr bool
2873 operator<(const leap_second& __x,
2874 const sys_time<_Duration>& __y) noexcept
2875 { return __x.date() < __y; }
2877 template<typename _Duration>
2878 [[nodiscard]] friend constexpr bool
2879 operator<(const sys_time<_Duration>& __x,
2880 const leap_second& __y) noexcept
2881 { return __x < __y.date(); }
2883 template<typename _Duration>
2884 [[nodiscard]] friend constexpr bool
2885 operator>(const leap_second& __x,
2886 const sys_time<_Duration>& __y) noexcept
2887 { return __y < __x.date(); }
2889 template<typename _Duration>
2890 [[nodiscard]] friend constexpr bool
2891 operator>(const sys_time<_Duration>& __x,
2892 const leap_second& __y) noexcept
2893 { return __y.date() < __x; }
2895 template<typename _Duration>
2896 [[nodiscard]] friend constexpr bool
2897 operator<=(const leap_second& __x,
2898 const sys_time<_Duration>& __y) noexcept
2899 { return !(__y < __x.date()); }
2901 template<typename _Duration>
2902 [[nodiscard]] friend constexpr bool
2903 operator<=(const sys_time<_Duration>& __x,
2904 const leap_second& __y) noexcept
2905 { return !(__y.date() < __x); }
2907 template<typename _Duration>
2908 [[nodiscard]] friend constexpr bool
2909 operator>=(const leap_second& __x,
2910 const sys_time<_Duration>& __y) noexcept
2911 { return !(__x.date() < __y); }
2913 template<typename _Duration>
2914 [[nodiscard]] friend constexpr bool
2915 operator>=(const sys_time<_Duration>& __x,
2916 const leap_second& __y) noexcept
2917 { return !(__x < __y.date()); }
2919 // This is a simplified form of the constraint specified in the standard,
2920 // three_way_comparable_with<sys_seconds, sys_time<_Duration>>.
2921 template<three_way_comparable_with<seconds> _Duration>
2922 [[nodiscard]] friend constexpr auto
2923 operator<=>(const leap_second& __x,
2924 const sys_time<_Duration>& __y) noexcept
2925 { return __x.date() <=> __y; }
2928 explicit leap_second(seconds::rep __s) : _M_s(__s) { }
2930 friend struct tzdb_list::_Node;
2932 friend const tzdb& reload_tzdb();
2934 template<typename _Duration>
2935 friend leap_second_info
2936 get_leap_second_info(const utc_time<_Duration>&);
2938 seconds _M_s; // == date().time_since_epoch() * value().count()
2941 template<class _Tp> struct zoned_traits { };
2944 struct zoned_traits<const time_zone*>
2946 static const time_zone*
2948 { return std::chrono::locate_zone("UTC"); }
2950 static const time_zone*
2951 locate_zone(string_view __name)
2952 { return std::chrono::locate_zone(__name); }
2958 _GLIBCXX_STD_C::vector<time_zone> zones;
2959 _GLIBCXX_STD_C::vector<time_zone_link> links;
2960 _GLIBCXX_STD_C::vector<leap_second> leap_seconds;
2963 locate_zone(string_view __tz_name) const;
2966 current_zone() const;
2969 friend const tzdb& reload_tzdb();
2970 friend class time_zone;
2971 friend struct tzdb_list::_Node;
2974 tzdb_list& get_tzdb_list();
2975 const tzdb& get_tzdb();
2977 const tzdb& reload_tzdb();
2978 string remote_version();
2980 template<typename _Duration, typename _TimeZonePtr = const time_zone*>
2983 static_assert(__is_duration_v<_Duration>);
2985 using _Traits = zoned_traits<_TimeZonePtr>;
2987 // Every constructor that accepts a string_view as its first parameter
2988 // does not participate in class template argument deduction.
2989 using string_view = type_identity_t<std::string_view>;
2992 using duration = common_type_t<_Duration, seconds>;
2994 zoned_time() requires requires { _Traits::default_zone(); }
2997 zoned_time(const zoned_time&) = default;
2998 zoned_time& operator=(const zoned_time&) = default;
3000 zoned_time(const sys_time<_Duration>& __st)
3001 requires requires { _Traits::default_zone(); }
3006 zoned_time(_TimeZonePtr __z) : _M_zone(std::move(__z)) { }
3009 zoned_time(string_view __name)
3011 _TimeZonePtr{_Traits::locate_zone(std::string_view{})};
3013 : _M_zone(_Traits::locate_zone(__name))
3016 template<typename _Duration2>
3017 zoned_time(const zoned_time<_Duration2, _TimeZonePtr>& __zt)
3018 requires is_convertible_v<sys_time<_Duration2>, sys_time<_Duration>>
3019 : _M_zone(__zt._M_zone), _M_tp(__zt._M_tp)
3022 zoned_time(_TimeZonePtr __z, const sys_time<_Duration>& __st)
3023 : _M_zone(std::move(__z)), _M_tp(__st)
3026 zoned_time(string_view __name, const sys_time<_Duration>& __st)
3027 : zoned_time(_Traits::locate_zone(__name), __st)
3030 zoned_time(_TimeZonePtr __z, const local_time<_Duration>& __tp)
3032 { __z->to_sys(__tp) } -> convertible_to<sys_time<_Duration>>;
3034 : _M_zone(std::move(__z)), _M_tp(_M_zone->to_sys(__tp))
3037 zoned_time(string_view __name, const local_time<_Duration>& __tp)
3038 requires requires (_TimeZonePtr __z) {
3039 { _Traits::locate_zone(__name) } -> convertible_to<_TimeZonePtr>;
3040 { __z->to_sys(__tp) } -> convertible_to<sys_time<_Duration>>;
3042 : zoned_time(_Traits::locate_zone(__name), __tp)
3045 zoned_time(_TimeZonePtr __z, const local_time<_Duration>& __tp,
3048 { __z->to_sys(__tp, __c) } -> convertible_to<sys_time<_Duration>>;
3050 : _M_zone(std::move(__z)), _M_tp(_M_zone->to_sys(__tp, __c))
3053 zoned_time(string_view __name, const local_time<_Duration>& __tp,
3055 requires requires (_TimeZonePtr __z) {
3056 { _Traits::locate_zone(__name) } -> convertible_to<_TimeZonePtr>;
3057 { __z->to_sys(__tp, __c) } -> convertible_to<sys_time<_Duration>>;
3059 : _M_zone(_Traits::locate_zone(__name)),
3060 _M_tp(_M_zone->to_sys(__tp, __c))
3063 template<typename _Duration2, typename _TimeZonePtr2>
3064 zoned_time(_TimeZonePtr __z,
3065 const zoned_time<_Duration2, _TimeZonePtr2>& __zt)
3066 requires is_convertible_v<sys_time<_Duration2>, sys_time<_Duration>>
3067 : _M_zone(__z), _M_tp(__zt._M_tp)
3070 template<typename _Duration2, typename _TimeZonePtr2>
3071 zoned_time(_TimeZonePtr __z,
3072 const zoned_time<_Duration2, _TimeZonePtr2>& __zt,
3074 requires is_convertible_v<sys_time<_Duration2>, sys_time<_Duration>>
3075 : _M_zone(__z), _M_tp(__zt._M_tp)
3078 template<typename _Duration2, typename _TimeZonePtr2>
3079 zoned_time(string_view __name,
3080 const zoned_time<_Duration2, _TimeZonePtr2>& __zt)
3081 requires is_convertible_v<sys_time<_Duration2>, sys_time<_Duration>>
3083 { _Traits::locate_zone(__name) } -> convertible_to<_TimeZonePtr>;
3085 : _M_zone(_Traits::locate_zone(__name)), _M_tp(__zt._M_tp)
3088 template<typename _Duration2, typename _TimeZonePtr2>
3089 zoned_time(string_view __name,
3090 const zoned_time<_Duration2, _TimeZonePtr2>& __zt,
3092 requires is_convertible_v<sys_time<_Duration2>, sys_time<_Duration>>
3094 { _Traits::locate_zone(__name) } -> convertible_to<_TimeZonePtr>;
3096 : _M_zone(_Traits::locate_zone(__name)), _M_tp(__zt._M_tp)
3100 operator=(const sys_time<_Duration>& __st)
3107 operator=(const local_time<_Duration>& __lt)
3109 _M_tp = _M_zone->to_sys(__lt);
3114 operator sys_time<duration>() const { return _M_tp; }
3117 explicit operator local_time<duration>() const
3118 { return get_local_time(); }
3122 get_time_zone() const
3126 local_time<duration>
3127 get_local_time() const
3128 { return _M_zone->to_local(_M_tp); }
3132 get_sys_time() const
3138 { return _M_zone->get_info(_M_tp); }
3140 [[nodiscard]] friend bool
3141 operator==(const zoned_time&, const zoned_time&) = default;
3144 _TimeZonePtr _M_zone{ _Traits::default_zone() };
3145 sys_time<duration> _M_tp{};
3147 template<typename _Duration2, typename _TimeZonePtr2>
3148 friend class zoned_time;
3151 zoned_time() -> zoned_time<seconds>;
3153 template<typename _Duration>
3154 zoned_time(sys_time<_Duration>)
3155 -> zoned_time<common_type_t<_Duration, seconds>>;
3157 /// @cond undocumented
3158 template<typename _TimeZonePtrOrName>
3159 using __time_zone_representation
3160 = __conditional_t<is_convertible_v<_TimeZonePtrOrName, string_view>,
3162 remove_cvref_t<_TimeZonePtrOrName>>;
3165 template<typename _TimeZonePtrOrName>
3166 zoned_time(_TimeZonePtrOrName&&)
3167 -> zoned_time<seconds, __time_zone_representation<_TimeZonePtrOrName>>;
3169 template<typename _TimeZonePtrOrName, typename _Duration>
3170 zoned_time(_TimeZonePtrOrName&&, sys_time<_Duration>)
3171 -> zoned_time<common_type_t<_Duration, seconds>,
3172 __time_zone_representation<_TimeZonePtrOrName>>;
3174 template<typename _TimeZonePtrOrName, typename _Duration>
3175 zoned_time(_TimeZonePtrOrName&&, local_time<_Duration>,
3176 choose = choose::earliest)
3177 -> zoned_time<common_type_t<_Duration, seconds>,
3178 __time_zone_representation<_TimeZonePtrOrName>>;
3180 template<typename _Duration, typename _TimeZonePtrOrName,
3181 typename _TimeZonePtr2>
3182 zoned_time(_TimeZonePtrOrName&&, zoned_time<_Duration, _TimeZonePtr2>,
3183 choose = choose::earliest)
3184 -> zoned_time<common_type_t<_Duration, seconds>,
3185 __time_zone_representation<_TimeZonePtrOrName>>;
3187 template<typename _Dur1, typename _TZPtr1, typename _Dur2, typename _TZPtr2>
3190 operator==(const zoned_time<_Dur1, _TZPtr1>& __x,
3191 const zoned_time<_Dur2, _TZPtr2>& __y)
3193 return __x.get_time_zone() == __y.get_time_zone()
3194 && __x.get_sys_time() == __y.get_sys_time();
3197 using zoned_seconds = zoned_time<seconds>;
3198 #endif // _GLIBCXX_USE_CXX11_ABI || ! _GLIBCXX_USE_DUAL_ABI
3202 inline leap_second_info
3203 __get_leap_second_info(sys_seconds __ss, bool __is_utc)
3205 if (__ss < sys_seconds{}) [[unlikely]]
3208 const seconds::rep __leaps[] {
3209 78796800, // 1 Jul 1972
3210 94694400, // 1 Jan 1973
3211 126230400, // 1 Jan 1974
3212 157766400, // 1 Jan 1975
3213 189302400, // 1 Jan 1976
3214 220924800, // 1 Jan 1977
3215 252460800, // 1 Jan 1978
3216 283996800, // 1 Jan 1979
3217 315532800, // 1 Jan 1980
3218 362793600, // 1 Jul 1981
3219 394329600, // 1 Jul 1982
3220 425865600, // 1 Jul 1983
3221 489024000, // 1 Jul 1985
3222 567993600, // 1 Jan 1988
3223 631152000, // 1 Jan 1990
3224 662688000, // 1 Jan 1991
3225 709948800, // 1 Jul 1992
3226 741484800, // 1 Jul 1993
3227 773020800, // 1 Jul 1994
3228 820454400, // 1 Jan 1996
3229 867715200, // 1 Jul 1997
3230 915148800, // 1 Jan 1999
3231 1136073600, // 1 Jan 2006
3232 1230768000, // 1 Jan 2009
3233 1341100800, // 1 Jul 2012
3234 1435708800, // 1 Jul 2015
3235 1483228800, // 1 Jan 2017
3237 // The list above is known to be valid until (at least) this date
3238 // and only contains positive leap seconds.
3239 const sys_seconds __expires(1766880000s); // 2025-12-28 00:00:00 UTC
3241 #if _GLIBCXX_USE_CXX11_ABI || ! _GLIBCXX_USE_DUAL_ABI
3242 if (__ss > __expires)
3244 // Use updated leap_seconds from tzdb.
3245 size_t __n = std::size(__leaps);
3247 auto __db = get_tzdb_list().begin();
3248 auto __first = __db->leap_seconds.begin() + __n;
3249 auto __last = __db->leap_seconds.end();
3250 auto __pos = std::upper_bound(__first, __last, __ss);
3251 seconds __elapsed(__n);
3252 for (auto __i = __first; __i != __pos; ++__i)
3253 __elapsed += __i->value();
3257 // Convert utc_time to sys_time:
3259 // See if that sys_time is before (or during) previous leap sec:
3260 if (__pos != __first && __ss < __pos[-1])
3262 if ((__ss + 1s) >= __pos[-1])
3263 return {true, __elapsed};
3264 __elapsed -= __pos[-1].value();
3267 return {false, __elapsed};
3272 seconds::rep __s = __ss.time_since_epoch().count();
3273 const seconds::rep* __first = std::begin(__leaps);
3274 const seconds::rep* __last = std::end(__leaps);
3276 // Don't bother searching the list if we're after the last one.
3277 if (__s > (__last[-1] + (__last - __first) + 1))
3278 return { false, seconds(__last - __first) };
3280 auto __pos = std::upper_bound(__first, __last, __s);
3281 seconds __elapsed{__pos - __first};
3284 // Convert utc_time to sys_time:
3285 __s -= __elapsed.count();
3286 // See if that sys_time is before (or during) previous leap sec:
3287 if (__pos != __first && __s < __pos[-1])
3289 if ((__s + 1) >= __pos[-1])
3290 return {true, __elapsed};
3294 return {false, __elapsed};
3297 } // namespace __detail
3299 template<typename _Duration>
3301 inline leap_second_info
3302 get_leap_second_info(const utc_time<_Duration>& __ut)
3304 auto __s = chrono::duration_cast<seconds>(__ut.time_since_epoch());
3305 return __detail::__get_leap_second_info(sys_seconds(__s), true);
3308 template<typename _Duration>
3310 inline utc_time<common_type_t<_Duration, seconds>>
3311 utc_clock::from_sys(const sys_time<_Duration>& __t)
3313 using _CDur = common_type_t<_Duration, seconds>;
3314 auto __s = chrono::time_point_cast<seconds>(__t);
3315 const auto __li = __detail::__get_leap_second_info(__s, false);
3316 return utc_time<_CDur>{__t.time_since_epoch()} + __li.elapsed;
3318 #endif // _GLIBCXX_HOSTED
3322 } // namespace chrono
3324 #if __cplusplus >= 202002L
3325 inline namespace literals
3327 inline namespace chrono_literals
3329 /// @addtogroup chrono
3331 #pragma GCC diagnostic push
3332 #pragma GCC diagnostic ignored "-Wliteral-suffix"
3333 /// Literal suffix for creating chrono::day objects.
3335 constexpr chrono::day
3336 operator""d(unsigned long long __d) noexcept
3337 { return chrono::day{static_cast<unsigned>(__d)}; }
3339 /// Literal suffix for creating chrono::year objects.
3341 constexpr chrono::year
3342 operator""y(unsigned long long __y) noexcept
3343 { return chrono::year{static_cast<int>(__y)}; }
3344 #pragma GCC diagnostic pop
3346 } // inline namespace chrono_literals
3347 } // inline namespace literals
3350 _GLIBCXX_END_NAMESPACE_VERSION
3353 #if __cplusplus >= 202002L && _GLIBCXX_HOSTED
3354 # include <bits/chrono_io.h>
3359 #endif //_GLIBCXX_CHRONO