libstdc++
chrono
Go to the documentation of this file.
1// <chrono> -*- C++ -*-
2
3// Copyright (C) 2008-2026 Free Software Foundation, Inc.
4//
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)
9// any later version.
10
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.
15
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.
19
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/>.
24
25// [time]
26
27/** @file include/chrono
28 * This is a Standard C++ Library header.
29 * @ingroup chrono
30 */
31
32#ifndef _GLIBCXX_CHRONO
33#define _GLIBCXX_CHRONO 1
34
35#ifdef _GLIBCXX_SYSHDR
36#pragma GCC system_header
37#endif
38
39#ifdef _GLIBCXX_NO_FREESTANDING_CHRONO
40# include <bits/requires_hosted.h> // for <ctime> and clocks
41#endif
42
43#if __cplusplus < 201103L
44# include <bits/c++0x_warning.h>
45#else
46
47#define __glibcxx_want_chrono
48#define __glibcxx_want_chrono_udls
49#include <bits/version.h>
50
51#include <bits/chrono.h>
52
53#if __cpp_lib_bitops >= 201907L
54# include <bit> // __countr_zero
55#endif
56#ifdef __glibcxx_chrono_cxx20
57# include <bits/stl_algo.h> // upper_bound
58# include <bits/range_access.h> // begin/end for arrays
59#endif
60#if __cpp_lib_chrono >= 201803L // C++20 && HOSTED && USE_CXX11_ABI
61# include <sstream>
62# include <string>
63# include <vector>
64# include <bits/shared_ptr.h>
65# include <bits/unique_ptr.h>
66#endif
67#if __glibcxx_chrono_cxx20 >= 202306L // C++26
68# include <bits/functional_hash.h>
69#endif
70
71namespace std _GLIBCXX_VISIBILITY(default)
72{
73_GLIBCXX_BEGIN_NAMESPACE_VERSION
74
75 /**
76 * @defgroup chrono Time
77 * @ingroup utilities
78 *
79 * Classes and functions for time.
80 *
81 * @since C++11
82 */
83
84 /** @namespace std::chrono
85 * @brief ISO C++ 2011 namespace for date and time utilities
86 * @ingroup chrono
87 */
88 namespace chrono
89 {
90#ifdef __glibcxx_chrono_cxx20
91 /// @addtogroup chrono
92 /// @{
93 struct local_t { };
94 template<typename _Duration>
95 using local_time = time_point<local_t, _Duration>;
96 using local_seconds = local_time<seconds>;
97 using local_days = local_time<days>;
98
99#if _GLIBCXX_HOSTED
100 class utc_clock;
101 class tai_clock;
102 class gps_clock;
103
104 template<typename _Duration>
105 using utc_time = time_point<utc_clock, _Duration>;
106 using utc_seconds = utc_time<seconds>;
107
108 template<typename _Duration>
109 using tai_time = time_point<tai_clock, _Duration>;
110 using tai_seconds = tai_time<seconds>;
111
112 template<typename _Duration>
113 using gps_time = time_point<gps_clock, _Duration>;
114 using gps_seconds = gps_time<seconds>;
115
116 template<> struct is_clock<utc_clock> : true_type { };
117 template<> struct is_clock<tai_clock> : true_type { };
118 template<> struct is_clock<gps_clock> : true_type { };
119
120 template<> inline constexpr bool is_clock_v<utc_clock> = true;
121 template<> inline constexpr bool is_clock_v<tai_clock> = true;
122 template<> inline constexpr bool is_clock_v<gps_clock> = true;
123
124 struct leap_second_info
125 {
126 bool is_leap_second;
127 seconds elapsed;
128 };
129
130 template<typename _Duration>
131 leap_second_info
132 get_leap_second_info(const utc_time<_Duration>& __ut);
133
134 /** A clock that measures Universal Coordinated Time (UTC).
135 *
136 * The epoch is 1970-01-01 00:00:00.
137 *
138 * @since C++20
139 */
141 {
142 public:
143 using rep = system_clock::rep;
144 using period = system_clock::period;
145 using duration = chrono::duration<rep, period>;
146 using time_point = chrono::time_point<utc_clock>;
147 static constexpr bool is_steady = false;
148
149 [[nodiscard]]
150 static time_point
151 now()
152 { return from_sys(system_clock::now()); }
153
154 template<typename _Duration>
155 [[nodiscard]]
156 static sys_time<common_type_t<_Duration, seconds>>
157 to_sys(const utc_time<_Duration>& __t)
158 {
160 const auto __li = chrono::get_leap_second_info(__t);
161 sys_time<_CDur> __s{__t.time_since_epoch() - __li.elapsed};
162 if (__li.is_leap_second)
163 __s = chrono::floor<seconds>(__s) + seconds{1} - _CDur{1};
164 return __s;
165 }
166
167 template<typename _Duration>
168 [[nodiscard]]
169 static utc_time<common_type_t<_Duration, seconds>>
170 from_sys(const sys_time<_Duration>& __t);
171 };
172
173 /** A clock that measures International Atomic Time.
174 *
175 * The epoch is 1958-01-01 00:00:00.
176 *
177 * @since C++20
178 */
180 {
181 public:
182 using rep = system_clock::rep;
183 using period = system_clock::period;
184 using duration = chrono::duration<rep, period>;
185 using time_point = chrono::time_point<tai_clock>;
186 static constexpr bool is_steady = false;
187
188 [[nodiscard]]
189 static time_point
190 now(); // in src/c++20/clock.cc
191
192 template<typename _Duration>
193 [[nodiscard]]
194 static utc_time<common_type_t<_Duration, seconds>>
195 to_utc(const tai_time<_Duration>& __t)
196 {
198 return utc_time<_CDur>{__t.time_since_epoch()} - 378691210s;
199 }
200
201 template<typename _Duration>
202 [[nodiscard]]
203 static tai_time<common_type_t<_Duration, seconds>>
204 from_utc(const utc_time<_Duration>& __t)
205 {
207 return tai_time<_CDur>{__t.time_since_epoch()} + 378691210s;
208 }
209 };
210
211 /** A clock that measures GPS time.
212 *
213 * The epoch is 1980-01-06 00:00:00.
214 *
215 * @since C++20
216 */
218 {
219 public:
220 using rep = system_clock::rep;
221 using period = system_clock::period;
222 using duration = chrono::duration<rep, period>;
223 using time_point = chrono::time_point<gps_clock>;
224 static constexpr bool is_steady = false;
225
226 [[nodiscard]]
227 static time_point
228 now(); // in src/c++20/clock.cc
229
230 template<typename _Duration>
231 [[nodiscard]]
232 static utc_time<common_type_t<_Duration, seconds>>
233 to_utc(const gps_time<_Duration>& __t)
234 {
236 return utc_time<_CDur>{__t.time_since_epoch()} + 315964809s;
237 }
238
239 template<typename _Duration>
240 [[nodiscard]]
241 static gps_time<common_type_t<_Duration, seconds>>
242 from_utc(const utc_time<_Duration>& __t)
243 {
245 return gps_time<_CDur>{__t.time_since_epoch()} - 315964809s;
246 }
247 };
248#endif // _GLIBCXX_HOSTED
249
250 template<typename _DestClock, typename _SourceClock>
251 struct clock_time_conversion
252 { };
253
254 // Identity conversions
255
256 template<typename _Clock>
257 struct clock_time_conversion<_Clock, _Clock>
258 {
259 template<typename _Duration>
260 time_point<_Clock, _Duration>
261 operator()(const time_point<_Clock, _Duration>& __t) const
262 { return __t; }
263 };
264
265#if _GLIBCXX_HOSTED
266 template<>
267 struct clock_time_conversion<system_clock, system_clock>
268 {
269 template<typename _Duration>
270 sys_time<_Duration>
271 operator()(const sys_time<_Duration>& __t) const
272 { return __t; }
273 };
274
275 template<>
276 struct clock_time_conversion<utc_clock, utc_clock>
277 {
278 template<typename _Duration>
279 utc_time<_Duration>
280 operator()(const utc_time<_Duration>& __t) const
281 { return __t; }
282 };
283
284 // Conversions between system_clock and utc_clock
285
286 template<>
287 struct clock_time_conversion<utc_clock, system_clock>
288 {
289 template<typename _Duration>
290 utc_time<common_type_t<_Duration, seconds>>
291 operator()(const sys_time<_Duration>& __t) const
292 { return utc_clock::from_sys(__t); }
293 };
294
295 template<>
296 struct clock_time_conversion<system_clock, utc_clock>
297 {
298 template<typename _Duration>
299 sys_time<common_type_t<_Duration, seconds>>
300 operator()(const utc_time<_Duration>& __t) const
301 { return utc_clock::to_sys(__t); }
302 };
303
304 template<typename _Tp, typename _Clock>
305 inline constexpr bool __is_time_point_for_v = false;
306
307 template<typename _Clock, typename _Duration>
308 inline constexpr bool
309 __is_time_point_for_v<time_point<_Clock, _Duration>, _Clock> = true;
310
311 // Conversions between system_clock and other clocks
312
313 template<typename _SourceClock>
314 struct clock_time_conversion<system_clock, _SourceClock>
315 {
316 template<typename _Duration, typename _Src = _SourceClock>
317 auto
318 operator()(const time_point<_SourceClock, _Duration>& __t) const
319 -> decltype(_Src::to_sys(__t))
320 {
321 using _Ret = decltype(_SourceClock::to_sys(__t));
322 static_assert(__is_time_point_for_v<_Ret, system_clock>);
323 return _SourceClock::to_sys(__t);
324 }
325 };
326
327 template<typename _DestClock>
328 struct clock_time_conversion<_DestClock, system_clock>
329 {
330 template<typename _Duration, typename _Dest = _DestClock>
331 auto
332 operator()(const sys_time<_Duration>& __t) const
333 -> decltype(_Dest::from_sys(__t))
334 {
335 using _Ret = decltype(_DestClock::from_sys(__t));
336 static_assert(__is_time_point_for_v<_Ret, _DestClock>);
337 return _DestClock::from_sys(__t);
338 }
339 };
340
341 // Conversions between utc_clock and other clocks
342
343 template<typename _SourceClock>
344 struct clock_time_conversion<utc_clock, _SourceClock>
345 {
346 template<typename _Duration, typename _Src = _SourceClock>
347 auto
348 operator()(const time_point<_SourceClock, _Duration>& __t) const
349 -> decltype(_Src::to_utc(__t))
350 {
351 using _Ret = decltype(_SourceClock::to_utc(__t));
352 static_assert(__is_time_point_for_v<_Ret, utc_clock>);
353 return _SourceClock::to_utc(__t);
354 }
355 };
356
357 template<typename _DestClock>
358 struct clock_time_conversion<_DestClock, utc_clock>
359 {
360 template<typename _Duration, typename _Dest = _DestClock>
361 auto
362 operator()(const utc_time<_Duration>& __t) const
363 -> decltype(_Dest::from_utc(__t))
364 {
365 using _Ret = decltype(_DestClock::from_utc(__t));
366 static_assert(__is_time_point_for_v<_Ret, _DestClock>);
367 return _DestClock::from_utc(__t);
368 }
369 };
370#endif // _GLIBCXX_HOSTED
371
372 /// @cond undocumented
373 namespace __detail
374 {
375 template<typename _DestClock, typename _SourceClock, typename _Duration>
376 concept __clock_convs
377 = requires (const time_point<_SourceClock, _Duration>& __t) {
378 clock_time_conversion<_DestClock, _SourceClock>{}(__t);
379 };
380
381#if _GLIBCXX_HOSTED
382 template<typename _DestClock, typename _SourceClock, typename _Duration>
383 concept __clock_convs_sys
384 = requires (const time_point<_SourceClock, _Duration>& __t) {
385 clock_time_conversion<_DestClock, system_clock>{}(
386 clock_time_conversion<system_clock, _SourceClock>{}(__t));
387 };
388
389 template<typename _DestClock, typename _SourceClock, typename _Duration>
390 concept __clock_convs_utc
391 = requires (const time_point<_SourceClock, _Duration>& __t) {
392 clock_time_conversion<_DestClock, utc_clock>{}(
393 clock_time_conversion<utc_clock, _SourceClock>{}(__t));
394 };
395
396 template<typename _DestClock, typename _SourceClock, typename _Duration>
397 concept __clock_convs_sys_utc
398 = requires (const time_point<_SourceClock, _Duration>& __t) {
399 clock_time_conversion<_DestClock, utc_clock>{}(
400 clock_time_conversion<utc_clock, system_clock>{}(
401 clock_time_conversion<system_clock, _SourceClock>{}(__t)));
402 };
403
404 template<typename _DestClock, typename _SourceClock, typename _Duration>
405 concept __clock_convs_utc_sys
406 = requires (const time_point<_SourceClock, _Duration>& __t) {
407 clock_time_conversion<_DestClock, system_clock>{}(
408 clock_time_conversion<system_clock, utc_clock>{}(
409 clock_time_conversion<utc_clock, _SourceClock>{}(__t)));
410 };
411#endif // _GLIBCXX_HOSTED
412 } // namespace __detail
413 /// @endcond
414
415 /// Convert a time point to a different clock.
416 template<typename _DestClock, typename _SourceClock, typename _Duration>
417 [[nodiscard]]
418 inline auto
420 requires __detail::__clock_convs<_DestClock, _SourceClock, _Duration>
421#if _GLIBCXX_HOSTED
422 || __detail::__clock_convs_sys<_DestClock, _SourceClock, _Duration>
423 || __detail::__clock_convs_utc<_DestClock, _SourceClock, _Duration>
424 || __detail::__clock_convs_sys_utc<_DestClock, _SourceClock, _Duration>
425 || __detail::__clock_convs_utc_sys<_DestClock, _SourceClock, _Duration>
426#endif // _GLIBCXX_HOSTED
427 {
428 constexpr bool __direct
429 = __detail::__clock_convs<_DestClock, _SourceClock, _Duration>;
430 if constexpr (__direct)
431 {
432 return clock_time_conversion<_DestClock, _SourceClock>{}(__t);
433 }
434#if _GLIBCXX_HOSTED
435 else
436 {
437 constexpr bool __convert_via_sys_clock
438 = __detail::__clock_convs_sys<_DestClock, _SourceClock, _Duration>;
439 constexpr bool __convert_via_utc_clock
440 = __detail::__clock_convs_utc<_DestClock, _SourceClock, _Duration>;
441 if constexpr (__convert_via_sys_clock)
442 {
443 static_assert(!__convert_via_utc_clock,
444 "clock_cast requires a unique best conversion, but "
445 "conversion is possible via system_clock and also via"
446 "utc_clock");
447 return clock_time_conversion<_DestClock, system_clock>{}(
448 clock_time_conversion<system_clock, _SourceClock>{}(__t));
449 }
450 else if constexpr (__convert_via_utc_clock)
451 {
452 return clock_time_conversion<_DestClock, utc_clock>{}(
453 clock_time_conversion<utc_clock, _SourceClock>{}(__t));
454 }
455 else
456 {
457 constexpr bool __convert_via_sys_and_utc_clocks
458 = __detail::__clock_convs_sys_utc<_DestClock,
459 _SourceClock,
460 _Duration>;
461
462 if constexpr (__convert_via_sys_and_utc_clocks)
463 {
464 constexpr bool __convert_via_utc_and_sys_clocks
465 = __detail::__clock_convs_utc_sys<_DestClock,
466 _SourceClock,
467 _Duration>;
468 static_assert(!__convert_via_utc_and_sys_clocks,
469 "clock_cast requires a unique best conversion, but "
470 "conversion is possible via system_clock followed by "
471 "utc_clock, and also via utc_clock followed by "
472 "system_clock");
473 return clock_time_conversion<_DestClock, utc_clock>{}(
474 clock_time_conversion<utc_clock, system_clock>{}(
475 clock_time_conversion<system_clock, _SourceClock>{}(__t)));
476 }
477 else
478 {
479 return clock_time_conversion<_DestClock, system_clock>{}(
480 clock_time_conversion<system_clock, utc_clock>{}(
481 clock_time_conversion<utc_clock, _SourceClock>{}(__t)));
482 }
483 }
484 }
485#endif // _GLIBCXX_HOSTED
486 }
487
488 // CALENDRICAL TYPES
489
490 // CLASS DECLARATIONS
491 class day;
492 class month;
493 class year;
494 class weekday;
495 class weekday_indexed;
496 class weekday_last;
497 class month_day;
498 class month_day_last;
499 class month_weekday;
500 class month_weekday_last;
501 class year_month;
502 class year_month_day;
503 class year_month_day_last;
504 class year_month_weekday;
505 class year_month_weekday_last;
506
507 struct last_spec
508 {
509 explicit last_spec() = default;
510
511 friend constexpr month_day_last
512 operator/(int __m, last_spec) noexcept;
513
514 friend constexpr month_day_last
515 operator/(last_spec, int __m) noexcept;
516 };
517
518 inline constexpr last_spec last{};
519
520 namespace __detail
521 {
522 // Helper to __add_modulo and __sub_modulo.
523 template <unsigned __d, typename _Tp>
524 consteval auto
525 __modulo_offset()
526 {
527 using _Up = make_unsigned_t<_Tp>;
528 auto constexpr __a = _Up(-1) - _Up(255 + __d - 2);
529 auto constexpr __b = _Up(__d * (__a / __d) - 1);
530 // Notice: b <= a - 1 <= _Up(-1) - (255 + d - 1) and b % d = d - 1.
531 return _Up(-1) - __b; // >= 255 + d - 1
532 }
533
534 // Compute the remainder of the Euclidean division of __x + __y divided by
535 // __d without overflowing. Typically, __x <= 255 + d - 1 is sum of
536 // weekday/month with a shift in [0, d - 1] and __y is a duration count.
537 template <unsigned __d, typename _Tp>
538 constexpr unsigned
539 __add_modulo(unsigned __x, _Tp __y)
540 {
541 using _Up = make_unsigned_t<_Tp>;
542 // For __y >= 0, _Up(__y) has the same mathematical value as __y and
543 // this function simply returns (__x + _Up(__y)) % d. Typically, this
544 // doesn't overflow since the range of _Up contains many more positive
545 // values than _Tp's. For __y < 0, _Up(__y) has a mathematical value in
546 // the upper-half range of _Up so that adding a positive value to it
547 // might overflow. Moreover, most likely, _Up(__y) != __y mod d. To
548 // fix both issues we subtract from _Up(__y) an __offset >=
549 // 255 + d - 1 to make room for the addition to __x and shift the modulo
550 // to the correct value.
551 auto const __offset = __y >= 0 ? _Up(0) : __modulo_offset<__d, _Tp>();
552 return (__x + _Up(__y) - __offset) % __d;
553 }
554
555 // Similar to __add_modulo but for __x - __y.
556 template <unsigned __d, typename _Tp>
557 constexpr unsigned
558 __sub_modulo(unsigned __x, _Tp __y)
559 {
560 using _Up = make_unsigned_t<_Tp>;
561 auto const __offset = __y <= 0 ? _Up(0) : __modulo_offset<__d, _Tp>();
562 return (__x - _Up(__y) - __offset) % __d;
563 }
564
565 inline constexpr unsigned __days_per_month[12]
566 = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
567 }
568
569 // DAY
570
571 class day
572 {
573 private:
574 unsigned char _M_d;
575
576 public:
577 day() = default;
578
579 explicit constexpr
580 day(unsigned __d) noexcept
581 : _M_d(__d)
582 { }
583
584 constexpr day&
585 operator++() noexcept
586 {
587 ++_M_d;
588 return *this;
589 }
590
591 constexpr day
592 operator++(int) noexcept
593 {
594 auto __ret = *this;
595 ++(*this);
596 return __ret;
597 }
598
599 constexpr day&
600 operator--() noexcept
601 {
602 --_M_d;
603 return *this;
604 }
605
606 constexpr day
607 operator--(int) noexcept
608 {
609 auto __ret = *this;
610 --(*this);
611 return __ret;
612 }
613
614 constexpr day&
615 operator+=(const days& __d) noexcept
616 {
617 *this = *this + __d;
618 return *this;
619 }
620
621 constexpr day&
622 operator-=(const days& __d) noexcept
623 {
624 *this = *this - __d;
625 return *this;
626 }
627
628 constexpr explicit
629 operator unsigned() const noexcept
630 { return _M_d; }
631
632 constexpr bool
633 ok() const noexcept
634 { return 1 <= _M_d && _M_d <= 31; }
635
636 friend constexpr bool
637 operator==(const day& __x, const day& __y) noexcept
638 { return unsigned{__x} == unsigned{__y}; }
639
640 friend constexpr strong_ordering
641 operator<=>(const day& __x, const day& __y) noexcept
642 { return unsigned{__x} <=> unsigned{__y}; }
643
644 friend constexpr day
645 operator+(const day& __x, const days& __y) noexcept
646 { return day(unsigned{__x} + __y.count()); }
647
648 friend constexpr day
649 operator+(const days& __x, const day& __y) noexcept
650 { return __y + __x; }
651
652 friend constexpr day
653 operator-(const day& __x, const days& __y) noexcept
654 { return __x + -__y; }
655
656 friend constexpr days
657 operator-(const day& __x, const day& __y) noexcept
658 { return days{int(unsigned{__x}) - int(unsigned{__y})}; }
659
660 friend constexpr month_day
661 operator/(const month& __m, const day& __d) noexcept;
662
663 friend constexpr month_day
664 operator/(int __m, const day& __d) noexcept;
665
666 friend constexpr month_day
667 operator/(const day& __d, const month& __m) noexcept;
668
669 friend constexpr month_day
670 operator/(const day& __d, int __m) noexcept;
671
672 friend constexpr year_month_day
673 operator/(const year_month& __ym, const day& __d) noexcept;
674 };
675
676 // MONTH
677
678 class month
679 {
680 private:
681 unsigned char _M_m;
682
683 public:
684 month() = default;
685
686 explicit constexpr
687 month(unsigned __m) noexcept
688 : _M_m(__m)
689 { }
690
691 constexpr month&
692 operator++() noexcept
693 {
694 *this += months{1};
695 return *this;
696 }
697
698 constexpr month
699 operator++(int) noexcept
700 {
701 auto __ret = *this;
702 ++(*this);
703 return __ret;
704 }
705
706 constexpr month&
707 operator--() noexcept
708 {
709 *this -= months{1};
710 return *this;
711 }
712
713 constexpr month
714 operator--(int) noexcept
715 {
716 auto __ret = *this;
717 --(*this);
718 return __ret;
719 }
720
721 constexpr month&
722 operator+=(const months& __m) noexcept
723 {
724 *this = *this + __m;
725 return *this;
726 }
727
728 constexpr month&
729 operator-=(const months& __m) noexcept
730 {
731 *this = *this - __m;
732 return *this;
733 }
734
735 explicit constexpr
736 operator unsigned() const noexcept
737 { return _M_m; }
738
739 constexpr bool
740 ok() const noexcept
741 { return 1 <= _M_m && _M_m <= 12; }
742
743 friend constexpr bool
744 operator==(const month& __x, const month& __y) noexcept
745 { return unsigned{__x} == unsigned{__y}; }
746
747 friend constexpr strong_ordering
748 operator<=>(const month& __x, const month& __y) noexcept
749 { return unsigned{__x} <=> unsigned{__y}; }
750
751 friend constexpr month
752 operator+(const month& __x, const months& __y) noexcept
753 {
754 // modulo(x + (y - 1), 12) = modulo(x + (y - 1) + 12, 12)
755 // = modulo((x + 11) + y , 12)
756 return month{1 + __detail::__add_modulo<12>(
757 unsigned{__x} + 11, __y.count())};
758 }
759
760 friend constexpr month
761 operator+(const months& __x, const month& __y) noexcept
762 { return __y + __x; }
763
764 friend constexpr month
765 operator-(const month& __x, const months& __y) noexcept
766 {
767 // modulo(x + (-y - 1), 12) = modulo(x + (-y - 1) + 12, 12)
768 // = modulo((x + 11) - y , 12)
769 return month{1 + __detail::__sub_modulo<12>(
770 unsigned{__x} + 11, __y.count())};
771 }
772
773 friend constexpr months
774 operator-(const month& __x, const month& __y) noexcept
775 {
776 const auto __dm = int(unsigned(__x)) - int(unsigned(__y));
777 return months{__dm < 0 ? 12 + __dm : __dm};
778 }
779
780 friend constexpr year_month
781 operator/(const year& __y, const month& __m) noexcept;
782
783 friend constexpr month_day
784 operator/(const month& __m, int __d) noexcept;
785
786 friend constexpr month_day_last
787 operator/(const month& __m, last_spec) noexcept;
788
789 friend constexpr month_day_last
790 operator/(last_spec, const month& __m) noexcept;
791
792 friend constexpr month_weekday
793 operator/(const month& __m, const weekday_indexed& __wdi) noexcept;
794
795 friend constexpr month_weekday
796 operator/(const weekday_indexed& __wdi, const month& __m) noexcept;
797
798 friend constexpr month_weekday_last
799 operator/(const month& __m, const weekday_last& __wdl) noexcept;
800
801 friend constexpr month_weekday_last
802 operator/(const weekday_last& __wdl, const month& __m) noexcept;
803 };
804
805 inline constexpr month January{1};
806 inline constexpr month February{2};
807 inline constexpr month March{3};
808 inline constexpr month April{4};
809 inline constexpr month May{5};
810 inline constexpr month June{6};
811 inline constexpr month July{7};
812 inline constexpr month August{8};
813 inline constexpr month September{9};
814 inline constexpr month October{10};
815 inline constexpr month November{11};
816 inline constexpr month December{12};
817
818 // YEAR
819
820 class year
821 {
822 private:
823 short _M_y;
824
825 public:
826 year() = default;
827
828 explicit constexpr
829 year(int __y) noexcept
830 : _M_y{static_cast<short>(__y)}
831 { }
832
833 static constexpr year
834 min() noexcept
835 { return year{-32767}; }
836
837 static constexpr year
838 max() noexcept
839 { return year{32767}; }
840
841 constexpr year&
842 operator++() noexcept
843 {
844 ++_M_y;
845 return *this;
846 }
847
848 constexpr year
849 operator++(int) noexcept
850 {
851 auto __ret = *this;
852 ++(*this);
853 return __ret;
854 }
855
856 constexpr year&
857 operator--() noexcept
858 {
859 --_M_y;
860 return *this;
861 }
862
863 constexpr year
864 operator--(int) noexcept
865 {
866 auto __ret = *this;
867 --(*this);
868 return __ret;
869 }
870
871 constexpr year&
872 operator+=(const years& __y) noexcept
873 {
874 *this = *this + __y;
875 return *this;
876 }
877
878 constexpr year&
879 operator-=(const years& __y) noexcept
880 {
881 *this = *this - __y;
882 return *this;
883 }
884
885 constexpr year
886 operator+() const noexcept
887 { return *this; }
888
889 constexpr year
890 operator-() const noexcept
891 { return year{-_M_y}; }
892
893 constexpr bool
894 is_leap() const noexcept
895 {
896 // Testing divisibility by 100 first gives better performance [1], i.e.,
897 // return _M_y % 100 == 0 ? _M_y % 400 == 0 : _M_y % 16 == 0;
898 // Furthermore, if _M_y % 100 == 0, then _M_y % 400 == 0 is equivalent
899 // to _M_y % 16 == 0, so we can simplify it to
900 // return _M_y % 100 == 0 ? _M_y % 16 == 0 : _M_y % 4 == 0. // #1
901 // Similarly, we can replace 100 with 25 (which is good since
902 // _M_y % 25 == 0 requires one fewer instruction than _M_y % 100 == 0
903 // [2]):
904 // return _M_y % 25 == 0 ? _M_y % 16 == 0 : _M_y % 4 == 0. // #2
905 // Indeed, first assume _M_y % 4 != 0. Then _M_y % 16 != 0 and hence,
906 // _M_y % 4 == 0 and _M_y % 16 == 0 are both false. Therefore, #2
907 // returns false as it should (regardless of _M_y % 25.) Now assume
908 // _M_y % 4 == 0. In this case, _M_y % 25 == 0 if, and only if,
909 // _M_y % 100 == 0, that is, #1 and #2 are equivalent. Finally, #2 is
910 // equivalent to
911 // return (_M_y & (_M_y % 25 == 0 ? 15 : 3)) == 0.
912
913 // References:
914 // [1] https://github.com/cassioneri/calendar
915 // [2] https://godbolt.org/z/55G8rn77e
916 // [3] https://gcc.gnu.org/pipermail/libstdc++/2021-June/052815.html
917
918 return (_M_y & (_M_y % 25 == 0 ? 15 : 3)) == 0;
919 }
920
921 explicit constexpr
922 operator int() const noexcept
923 { return _M_y; }
924
925 constexpr bool
926 ok() const noexcept
927 { return min()._M_y <= _M_y && _M_y <= max()._M_y; }
928
929 friend constexpr bool
930 operator==(const year& __x, const year& __y) noexcept
931 { return int{__x} == int{__y}; }
932
933 friend constexpr strong_ordering
934 operator<=>(const year& __x, const year& __y) noexcept
935 { return int{__x} <=> int{__y}; }
936
937 friend constexpr year
938 operator+(const year& __x, const years& __y) noexcept
939 { return year{int{__x} + static_cast<int>(__y.count())}; }
940
941 friend constexpr year
942 operator+(const years& __x, const year& __y) noexcept
943 { return __y + __x; }
944
945 friend constexpr year
946 operator-(const year& __x, const years& __y) noexcept
947 { return __x + -__y; }
948
949 friend constexpr years
950 operator-(const year& __x, const year& __y) noexcept
951 { return years{int{__x} - int{__y}}; }
952
953 friend constexpr year_month
954 operator/(const year& __y, int __m) noexcept;
955
956 friend constexpr year_month_day
957 operator/(const year& __y, const month_day& __md) noexcept;
958
959 friend constexpr year_month_day
960 operator/(const month_day& __md, const year& __y) noexcept;
961
962 friend constexpr year_month_day_last
963 operator/(const year& __y, const month_day_last& __mdl) noexcept;
964
965 friend constexpr year_month_day_last
966 operator/(const month_day_last& __mdl, const year& __y) noexcept;
967
968 friend constexpr year_month_weekday
969 operator/(const year& __y, const month_weekday& __mwd) noexcept;
970
971 friend constexpr year_month_weekday
972 operator/(const month_weekday& __mwd, const year& __y) noexcept;
973
974 friend constexpr year_month_weekday_last
975 operator/(const year& __y, const month_weekday_last& __mwdl) noexcept;
976
977 friend constexpr year_month_weekday_last
978 operator/(const month_weekday_last& __mwdl, const year& __y) noexcept;
979 };
980
981 // WEEKDAY
982
983 class weekday
984 {
985 private:
986 unsigned char _M_wd;
987
988 static constexpr weekday
989 _S_from_days(const days& __d)
990 {
991 return weekday{__detail::__add_modulo<7>(4, __d.count())};
992 }
993
994 public:
995 weekday() = default;
996
997 explicit constexpr
998 weekday(unsigned __wd) noexcept
999 : _M_wd(__wd == 7 ? 0 : __wd) // __wd % 7 ?
1000 { }
1001
1002 constexpr
1003 weekday(const sys_days& __dp) noexcept
1004 : weekday{_S_from_days(__dp.time_since_epoch())}
1005 { }
1006
1007 explicit constexpr
1008 weekday(const local_days& __dp) noexcept
1009 : weekday{sys_days{__dp.time_since_epoch()}}
1010 { }
1011
1012 constexpr weekday&
1013 operator++() noexcept
1014 {
1015 *this += days{1};
1016 return *this;
1017 }
1018
1019 constexpr weekday
1020 operator++(int) noexcept
1021 {
1022 auto __ret = *this;
1023 ++(*this);
1024 return __ret;
1025 }
1026
1027 constexpr weekday&
1028 operator--() noexcept
1029 {
1030 *this -= days{1};
1031 return *this;
1032 }
1033
1034 constexpr weekday
1035 operator--(int) noexcept
1036 {
1037 auto __ret = *this;
1038 --(*this);
1039 return __ret;
1040 }
1041
1042 constexpr weekday&
1043 operator+=(const days& __d) noexcept
1044 {
1045 *this = *this + __d;
1046 return *this;
1047 }
1048
1049 constexpr weekday&
1050 operator-=(const days& __d) noexcept
1051 {
1052 *this = *this - __d;
1053 return *this;
1054 }
1055
1056 constexpr unsigned
1057 c_encoding() const noexcept
1058 { return _M_wd; }
1059
1060 constexpr unsigned
1061 iso_encoding() const noexcept
1062 { return _M_wd == 0u ? 7u : _M_wd; }
1063
1064 constexpr bool
1065 ok() const noexcept
1066 { return _M_wd <= 6; }
1067
1068 constexpr weekday_indexed
1069 operator[](unsigned __index) const noexcept;
1070
1071 constexpr weekday_last
1072 operator[](last_spec) const noexcept;
1073
1074 friend constexpr bool
1075 operator==(const weekday& __x, const weekday& __y) noexcept
1076 { return __x._M_wd == __y._M_wd; }
1077
1078 friend constexpr weekday
1079 operator+(const weekday& __x, const days& __y) noexcept
1080 {
1081 return weekday{__detail::__add_modulo<7>(__x._M_wd, __y.count())};
1082 }
1083
1084 friend constexpr weekday
1085 operator+(const days& __x, const weekday& __y) noexcept
1086 { return __y + __x; }
1087
1088 friend constexpr weekday
1089 operator-(const weekday& __x, const days& __y) noexcept
1090 {
1091 return weekday{__detail::__sub_modulo<7>(__x._M_wd, __y.count())};
1092 }
1093
1094 friend constexpr days
1095 operator-(const weekday& __x, const weekday& __y) noexcept
1096 {
1097 const auto __n = __x.c_encoding() - __y.c_encoding();
1098 return static_cast<int>(__n) >= 0 ? days{__n} : days{__n + 7};
1099 }
1100 };
1101
1102 inline constexpr weekday Sunday{0};
1103 inline constexpr weekday Monday{1};
1104 inline constexpr weekday Tuesday{2};
1105 inline constexpr weekday Wednesday{3};
1106 inline constexpr weekday Thursday{4};
1107 inline constexpr weekday Friday{5};
1108 inline constexpr weekday Saturday{6};
1109
1110 // WEEKDAY_INDEXED
1111
1112 class weekday_indexed
1113 {
1114 private:
1115 chrono::weekday _M_wd;
1116 unsigned char _M_index;
1117
1118 public:
1119 weekday_indexed() = default;
1120
1121 constexpr
1122 weekday_indexed(const chrono::weekday& __wd, unsigned __index) noexcept
1123 : _M_wd(__wd), _M_index(__index)
1124 { }
1125
1126 constexpr chrono::weekday
1127 weekday() const noexcept
1128 { return _M_wd; }
1129
1130 constexpr unsigned
1131 index() const noexcept
1132 { return _M_index; };
1133
1134 constexpr bool
1135 ok() const noexcept
1136 { return _M_wd.ok() && 1 <= _M_index && _M_index <= 5; }
1137
1138 friend constexpr bool
1139 operator==(const weekday_indexed& __x, const weekday_indexed& __y) noexcept
1140 { return __x.weekday() == __y.weekday() && __x.index() == __y.index(); }
1141
1142 friend constexpr month_weekday
1143 operator/(const month& __m, const weekday_indexed& __wdi) noexcept;
1144
1145 friend constexpr month_weekday
1146 operator/(int __m, const weekday_indexed& __wdi) noexcept;
1147
1148 friend constexpr month_weekday
1149 operator/(const weekday_indexed& __wdi, const month& __m) noexcept;
1150
1151 friend constexpr month_weekday
1152 operator/(const weekday_indexed& __wdi, int __m) noexcept;
1153
1154 friend constexpr year_month_weekday
1155 operator/(const year_month& __ym, const weekday_indexed& __wdi) noexcept;
1156 };
1157
1158 constexpr weekday_indexed
1159 weekday::operator[](unsigned __index) const noexcept
1160 { return {*this, __index}; }
1161
1162 // WEEKDAY_LAST
1163
1164 class weekday_last
1165 {
1166 private:
1167 chrono::weekday _M_wd;
1168
1169 public:
1170 explicit constexpr
1171 weekday_last(const chrono::weekday& __wd) noexcept
1172 : _M_wd{__wd}
1173 { }
1174
1175 constexpr chrono::weekday
1176 weekday() const noexcept
1177 { return _M_wd; }
1178
1179 constexpr bool
1180 ok() const noexcept
1181 { return _M_wd.ok(); }
1182
1183 friend constexpr bool
1184 operator==(const weekday_last& __x, const weekday_last& __y) noexcept
1185 { return __x.weekday() == __y.weekday(); }
1186
1187 friend constexpr month_weekday_last
1188 operator/(int __m, const weekday_last& __wdl) noexcept;
1189
1190 friend constexpr month_weekday_last
1191 operator/(const weekday_last& __wdl, int __m) noexcept;
1192
1193 friend constexpr year_month_weekday_last
1194 operator/(const year_month& __ym, const weekday_last& __wdl) noexcept;
1195 };
1196
1197 constexpr weekday_last
1198 weekday::operator[](last_spec) const noexcept
1199 { return weekday_last{*this}; }
1200
1201 // MONTH_DAY
1202
1203 class month_day
1204 {
1205 private:
1206 chrono::month _M_m;
1207 chrono::day _M_d;
1208
1209 public:
1210 month_day() = default;
1211
1212 constexpr
1213 month_day(const chrono::month& __m, const chrono::day& __d) noexcept
1214 : _M_m{__m}, _M_d{__d}
1215 { }
1216
1217 constexpr chrono::month
1218 month() const noexcept
1219 { return _M_m; }
1220
1221 constexpr chrono::day
1222 day() const noexcept
1223 { return _M_d; }
1224
1225 constexpr bool
1226 ok() const noexcept
1227 {
1228 return _M_m.ok()
1229 && 1u <= unsigned(_M_d)
1230 && unsigned(_M_d) <= __detail::__days_per_month[unsigned(_M_m) - 1];
1231 }
1232
1233 friend constexpr bool
1234 operator==(const month_day& __x, const month_day& __y) noexcept
1235 { return __x.month() == __y.month() && __x.day() == __y.day(); }
1236
1237 friend constexpr strong_ordering
1238 operator<=>(const month_day& __x, const month_day& __y) noexcept
1239 = default;
1240
1241 friend constexpr month_day
1242 operator/(const chrono::month& __m, const chrono::day& __d) noexcept
1243 { return {__m, __d}; }
1244
1245 friend constexpr month_day
1246 operator/(const chrono::month& __m, int __d) noexcept
1247 { return {__m, chrono::day(unsigned(__d))}; }
1248
1249 friend constexpr month_day
1250 operator/(int __m, const chrono::day& __d) noexcept
1251 { return {chrono::month(unsigned(__m)), __d}; }
1252
1253 friend constexpr month_day
1254 operator/(const chrono::day& __d, const chrono::month& __m) noexcept
1255 { return {__m, __d}; }
1256
1257 friend constexpr month_day
1258 operator/(const chrono::day& __d, int __m) noexcept
1259 { return {chrono::month(unsigned(__m)), __d}; }
1260
1261 friend constexpr year_month_day
1262 operator/(int __y, const month_day& __md) noexcept;
1263
1264 friend constexpr year_month_day
1265 operator/(const month_day& __md, int __y) noexcept;
1266 };
1267
1268 // MONTH_DAY_LAST
1269
1270 class month_day_last
1271 {
1272 private:
1273 chrono::month _M_m;
1274
1275 public:
1276 explicit constexpr
1277 month_day_last(const chrono::month& __m) noexcept
1278 : _M_m{__m}
1279 { }
1280
1281 constexpr chrono::month
1282 month() const noexcept
1283 { return _M_m; }
1284
1285 constexpr bool
1286 ok() const noexcept
1287 { return _M_m.ok(); }
1288
1289 friend constexpr bool
1290 operator==(const month_day_last& __x, const month_day_last& __y) noexcept
1291 { return __x.month() == __y.month(); }
1292
1293 friend constexpr strong_ordering
1294 operator<=>(const month_day_last& __x, const month_day_last& __y) noexcept
1295 = default;
1296
1297 friend constexpr month_day_last
1298 operator/(const chrono::month& __m, last_spec) noexcept
1299 { return month_day_last{__m}; }
1300
1301 friend constexpr month_day_last
1302 operator/(int __m, last_spec) noexcept
1303 { return chrono::month(unsigned(__m)) / last; }
1304
1305 friend constexpr month_day_last
1306 operator/(last_spec, const chrono::month& __m) noexcept
1307 { return __m / last; }
1308
1309 friend constexpr month_day_last
1310 operator/(last_spec, int __m) noexcept
1311 { return __m / last; }
1312
1313 friend constexpr year_month_day_last
1314 operator/(int __y, const month_day_last& __mdl) noexcept;
1315
1316 friend constexpr year_month_day_last
1317 operator/(const month_day_last& __mdl, int __y) noexcept;
1318 };
1319
1320 // MONTH_WEEKDAY
1321
1322 class month_weekday
1323 {
1324 private:
1325 chrono::month _M_m;
1326 chrono::weekday_indexed _M_wdi;
1327
1328 public:
1329 constexpr
1330 month_weekday(const chrono::month& __m,
1331 const chrono::weekday_indexed& __wdi) noexcept
1332 : _M_m{__m}, _M_wdi{__wdi}
1333 { }
1334
1335 constexpr chrono::month
1336 month() const noexcept
1337 { return _M_m; }
1338
1339 constexpr chrono::weekday_indexed
1340 weekday_indexed() const noexcept
1341 { return _M_wdi; }
1342
1343 constexpr bool
1344 ok() const noexcept
1345 { return _M_m.ok() && _M_wdi.ok(); }
1346
1347 friend constexpr bool
1348 operator==(const month_weekday& __x, const month_weekday& __y) noexcept
1349 {
1350 return __x.month() == __y.month()
1351 && __x.weekday_indexed() == __y.weekday_indexed();
1352 }
1353
1354 friend constexpr month_weekday
1355 operator/(const chrono::month& __m,
1356 const chrono::weekday_indexed& __wdi) noexcept
1357 { return {__m, __wdi}; }
1358
1359 friend constexpr month_weekday
1360 operator/(int __m, const chrono::weekday_indexed& __wdi) noexcept
1361 { return chrono::month(unsigned(__m)) / __wdi; }
1362
1363 friend constexpr month_weekday
1364 operator/(const chrono::weekday_indexed& __wdi,
1365 const chrono::month& __m) noexcept
1366 { return __m / __wdi; }
1367
1368 friend constexpr month_weekday
1369 operator/(const chrono::weekday_indexed& __wdi, int __m) noexcept
1370 { return __m / __wdi; }
1371
1372 friend constexpr year_month_weekday
1373 operator/(int __y, const month_weekday& __mwd) noexcept;
1374
1375 friend constexpr year_month_weekday
1376 operator/(const month_weekday& __mwd, int __y) noexcept;
1377 };
1378
1379 // MONTH_WEEKDAY_LAST
1380
1381 class month_weekday_last
1382 {
1383 private:
1384 chrono::month _M_m;
1385 chrono::weekday_last _M_wdl;
1386
1387 public:
1388 constexpr
1389 month_weekday_last(const chrono::month& __m,
1390 const chrono::weekday_last& __wdl) noexcept
1391 :_M_m{__m}, _M_wdl{__wdl}
1392 { }
1393
1394 constexpr chrono::month
1395 month() const noexcept
1396 { return _M_m; }
1397
1398 constexpr chrono::weekday_last
1399 weekday_last() const noexcept
1400 { return _M_wdl; }
1401
1402 constexpr bool
1403 ok() const noexcept
1404 { return _M_m.ok() && _M_wdl.ok(); }
1405
1406 friend constexpr bool
1407 operator==(const month_weekday_last& __x,
1408 const month_weekday_last& __y) noexcept
1409 {
1410 return __x.month() == __y.month()
1411 && __x.weekday_last() == __y.weekday_last();
1412 }
1413
1414 friend constexpr month_weekday_last
1415 operator/(const chrono::month& __m,
1416 const chrono::weekday_last& __wdl) noexcept
1417 { return {__m, __wdl}; }
1418
1419 friend constexpr month_weekday_last
1420 operator/(int __m, const chrono::weekday_last& __wdl) noexcept
1421 { return chrono::month(unsigned(__m)) / __wdl; }
1422
1423 friend constexpr month_weekday_last
1424 operator/(const chrono::weekday_last& __wdl,
1425 const chrono::month& __m) noexcept
1426 { return __m / __wdl; }
1427
1428 friend constexpr month_weekday_last
1429 operator/(const chrono::weekday_last& __wdl, int __m) noexcept
1430 { return chrono::month(unsigned(__m)) / __wdl; }
1431
1432 friend constexpr year_month_weekday_last
1433 operator/(int __y, const month_weekday_last& __mwdl) noexcept;
1434
1435 friend constexpr year_month_weekday_last
1436 operator/(const month_weekday_last& __mwdl, int __y) noexcept;
1437 };
1438
1439 // YEAR_MONTH
1440
1441 namespace __detail
1442 {
1443 // [time.cal.ym], [time.cal.ymd], etc constrain the 'months'-based
1444 // addition/subtraction operator overloads like so:
1445 //
1446 // Constraints: if the argument supplied by the caller for the months
1447 // parameter is convertible to years, its implicit conversion sequence
1448 // to years is worse than its implicit conversion sequence to months.
1449 //
1450 // We realize this constraint by templatizing the 'months'-based
1451 // overloads (using a dummy defaulted template parameter), so that
1452 // overload resolution doesn't select the 'months'-based overload unless
1453 // the implicit conversion sequence to 'months' is better than that to
1454 // 'years'.
1455 using __months_years_conversion_disambiguator = void;
1456 }
1457
1458 class year_month
1459 {
1460 private:
1461 chrono::year _M_y;
1462 chrono::month _M_m;
1463
1464 public:
1465 year_month() = default;
1466
1467 constexpr
1468 year_month(const chrono::year& __y, const chrono::month& __m) noexcept
1469 : _M_y{__y}, _M_m{__m}
1470 { }
1471
1472 constexpr chrono::year
1473 year() const noexcept
1474 { return _M_y; }
1475
1476 constexpr chrono::month
1477 month() const noexcept
1478 { return _M_m; }
1479
1480 template<typename = __detail::__months_years_conversion_disambiguator>
1481 constexpr year_month&
1482 operator+=(const months& __dm) noexcept
1483 {
1484 *this = *this + __dm;
1485 return *this;
1486 }
1487
1488 template<typename = __detail::__months_years_conversion_disambiguator>
1489 constexpr year_month&
1490 operator-=(const months& __dm) noexcept
1491 {
1492 *this = *this - __dm;
1493 return *this;
1494 }
1495
1496 constexpr year_month&
1497 operator+=(const years& __dy) noexcept
1498 {
1499 *this = *this + __dy;
1500 return *this;
1501 }
1502
1503 constexpr year_month&
1504 operator-=(const years& __dy) noexcept
1505 {
1506 *this = *this - __dy;
1507 return *this;
1508 }
1509
1510 constexpr bool
1511 ok() const noexcept
1512 { return _M_y.ok() && _M_m.ok(); }
1513
1514 friend constexpr bool
1515 operator==(const year_month& __x, const year_month& __y) noexcept
1516 { return __x.year() == __y.year() && __x.month() == __y.month(); }
1517
1518 friend constexpr strong_ordering
1519 operator<=>(const year_month& __x, const year_month& __y) noexcept
1520 = default;
1521
1522 template<typename = __detail::__months_years_conversion_disambiguator>
1523 friend constexpr year_month
1524 operator+(const year_month& __ym, const months& __dm) noexcept
1525 {
1526 // TODO: Optimize?
1527 auto __m = __ym.month() + __dm;
1528 auto __i = int(unsigned(__ym.month())) - 1 + __dm.count();
1529 auto __y = (__i < 0
1530 ? __ym.year() + years{(__i - 11) / 12}
1531 : __ym.year() + years{__i / 12});
1532 return __y / __m;
1533 }
1534
1535 template<typename = __detail::__months_years_conversion_disambiguator>
1536 friend constexpr year_month
1537 operator+(const months& __dm, const year_month& __ym) noexcept
1538 { return __ym + __dm; }
1539
1540 template<typename = __detail::__months_years_conversion_disambiguator>
1541 friend constexpr year_month
1542 operator-(const year_month& __ym, const months& __dm) noexcept
1543 { return __ym + -__dm; }
1544
1545 friend constexpr months
1546 operator-(const year_month& __x, const year_month& __y) noexcept
1547 {
1548 return (__x.year() - __y.year()
1549 + months{static_cast<int>(unsigned{__x.month()})
1550 - static_cast<int>(unsigned{__y.month()})});
1551 }
1552
1553 friend constexpr year_month
1554 operator+(const year_month& __ym, const years& __dy) noexcept
1555 { return (__ym.year() + __dy) / __ym.month(); }
1556
1557 friend constexpr year_month
1558 operator+(const years& __dy, const year_month& __ym) noexcept
1559 { return __ym + __dy; }
1560
1561 friend constexpr year_month
1562 operator-(const year_month& __ym, const years& __dy) noexcept
1563 { return __ym + -__dy; }
1564
1565 friend constexpr year_month
1566 operator/(const chrono::year& __y, const chrono::month& __m) noexcept
1567 { return {__y, __m}; }
1568
1569 friend constexpr year_month
1570 operator/(const chrono::year& __y, int __m) noexcept
1571 { return {__y, chrono::month(unsigned(__m))}; }
1572
1573 friend constexpr year_month_day
1574 operator/(const year_month& __ym, int __d) noexcept;
1575
1576 friend constexpr year_month_day_last
1577 operator/(const year_month& __ym, last_spec) noexcept;
1578 };
1579
1580 // YEAR_MONTH_DAY
1581
1582 class year_month_day
1583 {
1584 private:
1585 chrono::year _M_y;
1586 chrono::month _M_m;
1587 chrono::day _M_d;
1588
1589 static constexpr year_month_day _S_from_days(const days& __dp) noexcept;
1590
1591 constexpr days _M_days_since_epoch() const noexcept;
1592
1593 public:
1594 year_month_day() = default;
1595
1596 constexpr
1597 year_month_day(const chrono::year& __y, const chrono::month& __m,
1598 const chrono::day& __d) noexcept
1599 : _M_y{__y}, _M_m{__m}, _M_d{__d}
1600 { }
1601
1602 constexpr
1603 year_month_day(const year_month_day_last& __ymdl) noexcept;
1604
1605 constexpr
1606 year_month_day(const sys_days& __dp) noexcept
1607 : year_month_day(_S_from_days(__dp.time_since_epoch()))
1608 { }
1609
1610 explicit constexpr
1611 year_month_day(const local_days& __dp) noexcept
1612 : year_month_day(sys_days{__dp.time_since_epoch()})
1613 { }
1614
1615 template<typename = __detail::__months_years_conversion_disambiguator>
1616 constexpr year_month_day&
1617 operator+=(const months& __m) noexcept
1618 {
1619 *this = *this + __m;
1620 return *this;
1621 }
1622
1623 template<typename = __detail::__months_years_conversion_disambiguator>
1624 constexpr year_month_day&
1625 operator-=(const months& __m) noexcept
1626 {
1627 *this = *this - __m;
1628 return *this;
1629 }
1630
1631 constexpr year_month_day&
1632 operator+=(const years& __y) noexcept
1633 {
1634 *this = *this + __y;
1635 return *this;
1636 }
1637
1638 constexpr year_month_day&
1639 operator-=(const years& __y) noexcept
1640 {
1641 *this = *this - __y;
1642 return *this;
1643 }
1644
1645 constexpr chrono::year
1646 year() const noexcept
1647 { return _M_y; }
1648
1649 constexpr chrono::month
1650 month() const noexcept
1651 { return _M_m; }
1652
1653 constexpr chrono::day
1654 day() const noexcept
1655 { return _M_d; }
1656
1657 constexpr
1658 operator sys_days() const noexcept
1659 { return sys_days{_M_days_since_epoch()}; }
1660
1661 explicit constexpr
1662 operator local_days() const noexcept
1663 { return local_days{sys_days{*this}.time_since_epoch()}; }
1664
1665 constexpr bool ok() const noexcept;
1666
1667 friend constexpr bool
1668 operator==(const year_month_day& __x, const year_month_day& __y) noexcept
1669 {
1670 return __x.year() == __y.year()
1671 && __x.month() == __y.month()
1672 && __x.day() == __y.day();
1673 }
1674
1675 friend constexpr strong_ordering
1676 operator<=>(const year_month_day& __x, const year_month_day& __y) noexcept
1677 = default;
1678
1679 template<typename = __detail::__months_years_conversion_disambiguator>
1680 friend constexpr year_month_day
1681 operator+(const year_month_day& __ymd, const months& __dm) noexcept
1682 { return (__ymd.year() / __ymd.month() + __dm) / __ymd.day(); }
1683
1684 template<typename = __detail::__months_years_conversion_disambiguator>
1685 friend constexpr year_month_day
1686 operator+(const months& __dm, const year_month_day& __ymd) noexcept
1687 { return __ymd + __dm; }
1688
1689 friend constexpr year_month_day
1690 operator+(const year_month_day& __ymd, const years& __dy) noexcept
1691 { return (__ymd.year() + __dy) / __ymd.month() / __ymd.day(); }
1692
1693 friend constexpr year_month_day
1694 operator+(const years& __dy, const year_month_day& __ymd) noexcept
1695 { return __ymd + __dy; }
1696
1697 template<typename = __detail::__months_years_conversion_disambiguator>
1698 friend constexpr year_month_day
1699 operator-(const year_month_day& __ymd, const months& __dm) noexcept
1700 { return __ymd + -__dm; }
1701
1702 friend constexpr year_month_day
1703 operator-(const year_month_day& __ymd, const years& __dy) noexcept
1704 { return __ymd + -__dy; }
1705
1706 friend constexpr year_month_day
1707 operator/(const year_month& __ym, const chrono::day& __d) noexcept
1708 { return {__ym.year(), __ym.month(), __d}; }
1709
1710 friend constexpr year_month_day
1711 operator/(const year_month& __ym, int __d) noexcept
1712 { return __ym / chrono::day{unsigned(__d)}; }
1713
1714 friend constexpr year_month_day
1715 operator/(const chrono::year& __y, const month_day& __md) noexcept
1716 { return __y / __md.month() / __md.day(); }
1717
1718 friend constexpr year_month_day
1719 operator/(int __y, const month_day& __md) noexcept
1720 { return chrono::year{__y} / __md; }
1721
1722 friend constexpr year_month_day
1723 operator/(const month_day& __md, const chrono::year& __y) noexcept
1724 { return __y / __md; }
1725
1726 friend constexpr year_month_day
1727 operator/(const month_day& __md, int __y) noexcept
1728 { return chrono::year(__y) / __md; }
1729 };
1730
1731 // Construct from days since 1970/01/01.
1732 // Proposition 6.3 of Neri and Schneider,
1733 // "Euclidean Affine Functions and Applications to Calendar Algorithms".
1734 // https://arxiv.org/abs/2102.06959
1735 constexpr year_month_day
1736 year_month_day::_S_from_days(const days& __dp) noexcept
1737 {
1738 constexpr auto __z2 = static_cast<uint32_t>(-1468000);
1739 constexpr auto __r2_e3 = static_cast<uint32_t>(536895458);
1740
1741 const auto __r0 = static_cast<uint32_t>(__dp.count()) + __r2_e3;
1742
1743 const auto __n1 = 4 * __r0 + 3;
1744 const auto __q1 = __n1 / 146097;
1745 const auto __r1 = __n1 % 146097 / 4;
1746
1747 constexpr auto __p32 = static_cast<uint64_t>(1) << 32;
1748 const auto __n2 = 4 * __r1 + 3;
1749 const auto __u2 = static_cast<uint64_t>(2939745) * __n2;
1750 const auto __q2 = static_cast<uint32_t>(__u2 / __p32);
1751 const auto __r2 = static_cast<uint32_t>(__u2 % __p32) / 2939745 / 4;
1752
1753 constexpr auto __p16 = static_cast<uint32_t>(1) << 16;
1754 const auto __n3 = 2141 * __r2 + 197913;
1755 const auto __q3 = __n3 / __p16;
1756 const auto __r3 = __n3 % __p16 / 2141;
1757
1758 const auto __y0 = 100 * __q1 + __q2;
1759 const auto __m0 = __q3;
1760 const auto __d0 = __r3;
1761
1762 const auto __j = __r2 >= 306;
1763 const auto __y1 = __y0 + __j;
1764 const auto __m1 = __j ? __m0 - 12 : __m0;
1765 const auto __d1 = __d0 + 1;
1766
1767 return year_month_day{chrono::year{static_cast<int>(__y1 + __z2)},
1768 chrono::month{__m1}, chrono::day{__d1}};
1769 }
1770
1771 // Days since 1970/01/01.
1772 // Proposition 6.2 of Neri and Schneider,
1773 // "Euclidean Affine Functions and Applications to Calendar Algorithms".
1774 // https://arxiv.org/abs/2102.06959
1775 constexpr days
1776 year_month_day::_M_days_since_epoch() const noexcept
1777 {
1778 auto constexpr __z2 = static_cast<uint32_t>(-1468000);
1779 auto constexpr __r2_e3 = static_cast<uint32_t>(536895458);
1780
1781 const auto __y1 = static_cast<uint32_t>(static_cast<int>(_M_y)) - __z2;
1782 const auto __m1 = static_cast<uint32_t>(static_cast<unsigned>(_M_m));
1783 const auto __d1 = static_cast<uint32_t>(static_cast<unsigned>(_M_d));
1784
1785 const auto __j = static_cast<uint32_t>(__m1 < 3);
1786 const auto __y0 = __y1 - __j;
1787 const auto __m0 = __j ? __m1 + 12 : __m1;
1788 const auto __d0 = __d1 - 1;
1789
1790 const auto __q1 = __y0 / 100;
1791 const auto __yc = 1461 * __y0 / 4 - __q1 + __q1 / 4;
1792 const auto __mc = (979 *__m0 - 2919) / 32;
1793 const auto __dc = __d0;
1794
1795 return days{static_cast<int32_t>(__yc + __mc + __dc - __r2_e3)};
1796 }
1797
1798 // YEAR_MONTH_DAY_LAST
1799
1800 class year_month_day_last
1801 {
1802 private:
1803 chrono::year _M_y;
1804 chrono::month_day_last _M_mdl;
1805
1806 public:
1807 constexpr
1808 year_month_day_last(const chrono::year& __y,
1809 const chrono::month_day_last& __mdl) noexcept
1810 : _M_y{__y}, _M_mdl{__mdl}
1811 { }
1812
1813 template<typename = __detail::__months_years_conversion_disambiguator>
1814 constexpr year_month_day_last&
1815 operator+=(const months& __m) noexcept
1816 {
1817 *this = *this + __m;
1818 return *this;
1819 }
1820
1821 template<typename = __detail::__months_years_conversion_disambiguator>
1822 constexpr year_month_day_last&
1823 operator-=(const months& __m) noexcept
1824 {
1825 *this = *this - __m;
1826 return *this;
1827 }
1828
1829 constexpr year_month_day_last&
1830 operator+=(const years& __y) noexcept
1831 {
1832 *this = *this + __y;
1833 return *this;
1834 }
1835
1836 constexpr year_month_day_last&
1837 operator-=(const years& __y) noexcept
1838 {
1839 *this = *this - __y;
1840 return *this;
1841 }
1842
1843 constexpr chrono::year
1844 year() const noexcept
1845 { return _M_y; }
1846
1847 constexpr chrono::month
1848 month() const noexcept
1849 { return _M_mdl.month(); }
1850
1851 constexpr chrono::month_day_last
1852 month_day_last() const noexcept
1853 { return _M_mdl; }
1854
1855 // Return A day representing the last day of this year, month pair.
1856 constexpr chrono::day
1857 day() const noexcept
1858 {
1859 const auto __m = static_cast<unsigned>(month());
1860
1861 // The result is unspecified if __m < 1 or __m > 12. Hence, assume
1862 // 1 <= __m <= 12. For __m != 2, day() == 30 or day() == 31 or, in
1863 // other words, day () == 30 | b, where b is in {0, 1}.
1864
1865 // If __m in {1, 3, 4, 5, 6, 7}, then b is 1 if, and only if, __m is
1866 // odd. Hence, b = __m & 1 = (__m ^ 0) & 1.
1867
1868 // If __m in {8, 9, 10, 11, 12}, then b is 1 if, and only if, __m is
1869 // even. Hence, b = (__m ^ 1) & 1.
1870
1871 // Therefore, b = (__m ^ c) & 1, where c = 0, if __m < 8, or c = 1 if
1872 // __m >= 8, that is, c = __m >> 3.
1873
1874 // Since 30 = (11110)_2 and __m <= 31 = (11111)_2, the "& 1" in b's
1875 // calculation is unnecessary.
1876
1877 // The performance of this implementation does not depend on look-up
1878 // tables being on the L1 cache.
1879 return chrono::day{__m != 2 ? (__m ^ (__m >> 3)) | 30
1880 : _M_y.is_leap() ? 29 : 28};
1881 }
1882
1883 constexpr
1884 operator sys_days() const noexcept
1885 { return sys_days{year() / month() / day()}; }
1886
1887 explicit constexpr
1888 operator local_days() const noexcept
1889 { return local_days{sys_days{*this}.time_since_epoch()}; }
1890
1891 constexpr bool
1892 ok() const noexcept
1893 { return _M_y.ok() && _M_mdl.ok(); }
1894
1895 friend constexpr bool
1896 operator==(const year_month_day_last& __x,
1897 const year_month_day_last& __y) noexcept
1898 {
1899 return __x.year() == __y.year()
1900 && __x.month_day_last() == __y.month_day_last();
1901 }
1902
1903 friend constexpr strong_ordering
1904 operator<=>(const year_month_day_last& __x,
1905 const year_month_day_last& __y) noexcept
1906 = default;
1907
1908 template<typename = __detail::__months_years_conversion_disambiguator>
1909 friend constexpr year_month_day_last
1910 operator+(const year_month_day_last& __ymdl,
1911 const months& __dm) noexcept
1912 { return (__ymdl.year() / __ymdl.month() + __dm) / last; }
1913
1914 template<typename = __detail::__months_years_conversion_disambiguator>
1915 friend constexpr year_month_day_last
1916 operator+(const months& __dm,
1917 const year_month_day_last& __ymdl) noexcept
1918 { return __ymdl + __dm; }
1919
1920 template<typename = __detail::__months_years_conversion_disambiguator>
1921 friend constexpr year_month_day_last
1922 operator-(const year_month_day_last& __ymdl,
1923 const months& __dm) noexcept
1924 { return __ymdl + -__dm; }
1925
1926 friend constexpr year_month_day_last
1927 operator+(const year_month_day_last& __ymdl,
1928 const years& __dy) noexcept
1929 { return {__ymdl.year() + __dy, __ymdl.month_day_last()}; }
1930
1931 friend constexpr year_month_day_last
1932 operator+(const years& __dy,
1933 const year_month_day_last& __ymdl) noexcept
1934 { return __ymdl + __dy; }
1935
1936 friend constexpr year_month_day_last
1937 operator-(const year_month_day_last& __ymdl,
1938 const years& __dy) noexcept
1939 { return __ymdl + -__dy; }
1940
1941 friend constexpr year_month_day_last
1942 operator/(const year_month& __ym, last_spec) noexcept
1943 { return {__ym.year(), chrono::month_day_last{__ym.month()}}; }
1944
1945 friend constexpr year_month_day_last
1946 operator/(const chrono::year& __y,
1947 const chrono::month_day_last& __mdl) noexcept
1948 { return {__y, __mdl}; }
1949
1950 friend constexpr year_month_day_last
1951 operator/(int __y, const chrono::month_day_last& __mdl) noexcept
1952 { return chrono::year(__y) / __mdl; }
1953
1954 friend constexpr year_month_day_last
1955 operator/(const chrono::month_day_last& __mdl,
1956 const chrono::year& __y) noexcept
1957 { return __y / __mdl; }
1958
1959 friend constexpr year_month_day_last
1960 operator/(const chrono::month_day_last& __mdl, int __y) noexcept
1961 { return chrono::year(__y) / __mdl; }
1962 };
1963
1964 // year_month_day ctor from year_month_day_last
1965 constexpr
1966 year_month_day::year_month_day(const year_month_day_last& __ymdl) noexcept
1967 : _M_y{__ymdl.year()}, _M_m{__ymdl.month()}, _M_d{__ymdl.day()}
1968 { }
1969
1970 constexpr bool
1971 year_month_day::ok() const noexcept
1972 {
1973 if (!_M_y.ok() || !_M_m.ok())
1974 return false;
1975 return chrono::day{1} <= _M_d && _M_d <= (_M_y / _M_m / last).day();
1976 }
1977
1978 // YEAR_MONTH_WEEKDAY
1979
1980 class year_month_weekday
1981 {
1982 private:
1983 chrono::year _M_y;
1984 chrono::month _M_m;
1985 chrono::weekday_indexed _M_wdi;
1986
1987 static constexpr year_month_weekday
1988 _S_from_sys_days(const sys_days& __dp)
1989 {
1990 year_month_day __ymd{__dp};
1991 chrono::weekday __wd{__dp};
1992 auto __index = __wd[(unsigned{__ymd.day()} - 1) / 7 + 1];
1993 return {__ymd.year(), __ymd.month(), __index};
1994 }
1995
1996 public:
1997 year_month_weekday() = default;
1998
1999 constexpr
2000 year_month_weekday(const chrono::year& __y, const chrono::month& __m,
2001 const chrono::weekday_indexed& __wdi) noexcept
2002 : _M_y{__y}, _M_m{__m}, _M_wdi{__wdi}
2003 { }
2004
2005 constexpr
2006 year_month_weekday(const sys_days& __dp) noexcept
2007 : year_month_weekday{_S_from_sys_days(__dp)}
2008 { }
2009
2010 explicit constexpr
2011 year_month_weekday(const local_days& __dp) noexcept
2012 : year_month_weekday{sys_days{__dp.time_since_epoch()}}
2013 { }
2014
2015 template<typename = __detail::__months_years_conversion_disambiguator>
2016 constexpr year_month_weekday&
2017 operator+=(const months& __m) noexcept
2018 {
2019 *this = *this + __m;
2020 return *this;
2021 }
2022
2023 template<typename = __detail::__months_years_conversion_disambiguator>
2024 constexpr year_month_weekday&
2025 operator-=(const months& __m) noexcept
2026 {
2027 *this = *this - __m;
2028 return *this;
2029 }
2030
2031 constexpr year_month_weekday&
2032 operator+=(const years& __y) noexcept
2033 {
2034 *this = *this + __y;
2035 return *this;
2036 }
2037
2038 constexpr year_month_weekday&
2039 operator-=(const years& __y) noexcept
2040 {
2041 *this = *this - __y;
2042 return *this;
2043 }
2044
2045 constexpr chrono::year
2046 year() const noexcept
2047 { return _M_y; }
2048
2049 constexpr chrono::month
2050 month() const noexcept
2051 { return _M_m; }
2052
2053 constexpr chrono::weekday
2054 weekday() const noexcept
2055 { return _M_wdi.weekday(); }
2056
2057 constexpr unsigned
2058 index() const noexcept
2059 { return _M_wdi.index(); }
2060
2061 constexpr chrono::weekday_indexed
2062 weekday_indexed() const noexcept
2063 { return _M_wdi; }
2064
2065 constexpr
2066 operator sys_days() const noexcept
2067 {
2068 auto __d = sys_days{year() / month() / 1};
2069 return __d + (weekday() - chrono::weekday(__d)
2070 + days{(static_cast<int>(index())-1)*7});
2071 }
2072
2073 explicit constexpr
2074 operator local_days() const noexcept
2075 { return local_days{sys_days{*this}.time_since_epoch()}; }
2076
2077 constexpr bool
2078 ok() const noexcept
2079 {
2080 if (!_M_y.ok() || !_M_m.ok() || !_M_wdi.ok())
2081 return false;
2082 if (_M_wdi.index() <= 4)
2083 return true;
2084 days __d = (_M_wdi.weekday()
2085 - chrono::weekday{sys_days{_M_y / _M_m / 1}}
2086 + days((_M_wdi.index()-1)*7 + 1));
2087 __glibcxx_assert(__d.count() >= 1);
2088 return (unsigned)__d.count() <= (unsigned)(_M_y / _M_m / last).day();
2089 }
2090
2091 friend constexpr bool
2092 operator==(const year_month_weekday& __x,
2093 const year_month_weekday& __y) noexcept
2094 {
2095 return __x.year() == __y.year()
2096 && __x.month() == __y.month()
2097 && __x.weekday_indexed() == __y.weekday_indexed();
2098 }
2099
2100 template<typename = __detail::__months_years_conversion_disambiguator>
2101 friend constexpr year_month_weekday
2102 operator+(const year_month_weekday& __ymwd, const months& __dm) noexcept
2103 {
2104 return ((__ymwd.year() / __ymwd.month() + __dm)
2105 / __ymwd.weekday_indexed());
2106 }
2107
2108 template<typename = __detail::__months_years_conversion_disambiguator>
2109 friend constexpr year_month_weekday
2110 operator+(const months& __dm, const year_month_weekday& __ymwd) noexcept
2111 { return __ymwd + __dm; }
2112
2113 friend constexpr year_month_weekday
2114 operator+(const year_month_weekday& __ymwd, const years& __dy) noexcept
2115 { return {__ymwd.year() + __dy, __ymwd.month(), __ymwd.weekday_indexed()}; }
2116
2117 friend constexpr year_month_weekday
2118 operator+(const years& __dy, const year_month_weekday& __ymwd) noexcept
2119 { return __ymwd + __dy; }
2120
2121 template<typename = __detail::__months_years_conversion_disambiguator>
2122 friend constexpr year_month_weekday
2123 operator-(const year_month_weekday& __ymwd, const months& __dm) noexcept
2124 { return __ymwd + -__dm; }
2125
2126 friend constexpr year_month_weekday
2127 operator-(const year_month_weekday& __ymwd, const years& __dy) noexcept
2128 { return __ymwd + -__dy; }
2129
2130 friend constexpr year_month_weekday
2131 operator/(const year_month& __ym,
2132 const chrono::weekday_indexed& __wdi) noexcept
2133 { return {__ym.year(), __ym.month(), __wdi}; }
2134
2135 friend constexpr year_month_weekday
2136 operator/(const chrono::year& __y, const month_weekday& __mwd) noexcept
2137 { return {__y, __mwd.month(), __mwd.weekday_indexed()}; }
2138
2139 friend constexpr year_month_weekday
2140 operator/(int __y, const month_weekday& __mwd) noexcept
2141 { return chrono::year(__y) / __mwd; }
2142
2143 friend constexpr year_month_weekday
2144 operator/(const month_weekday& __mwd, const chrono::year& __y) noexcept
2145 { return __y / __mwd; }
2146
2147 friend constexpr year_month_weekday
2148 operator/(const month_weekday& __mwd, int __y) noexcept
2149 { return chrono::year(__y) / __mwd; }
2150 };
2151
2152 // YEAR_MONTH_WEEKDAY_LAST
2153
2154 class year_month_weekday_last
2155 {
2156 private:
2157 chrono::year _M_y;
2158 chrono::month _M_m;
2159 chrono::weekday_last _M_wdl;
2160
2161 public:
2162 constexpr
2163 year_month_weekday_last(const chrono::year& __y, const chrono::month& __m,
2164 const chrono::weekday_last& __wdl) noexcept
2165 : _M_y{__y}, _M_m{__m}, _M_wdl{__wdl}
2166 { }
2167
2168 template<typename = __detail::__months_years_conversion_disambiguator>
2169 constexpr year_month_weekday_last&
2170 operator+=(const months& __m) noexcept
2171 {
2172 *this = *this + __m;
2173 return *this;
2174 }
2175
2176 template<typename = __detail::__months_years_conversion_disambiguator>
2177 constexpr year_month_weekday_last&
2178 operator-=(const months& __m) noexcept
2179 {
2180 *this = *this - __m;
2181 return *this;
2182 }
2183
2184 constexpr year_month_weekday_last&
2185 operator+=(const years& __y) noexcept
2186 {
2187 *this = *this + __y;
2188 return *this;
2189 }
2190
2191 constexpr year_month_weekday_last&
2192 operator-=(const years& __y) noexcept
2193 {
2194 *this = *this - __y;
2195 return *this;
2196 }
2197
2198 constexpr chrono::year
2199 year() const noexcept
2200 { return _M_y; }
2201
2202 constexpr chrono::month
2203 month() const noexcept
2204 { return _M_m; }
2205
2206 constexpr chrono::weekday
2207 weekday() const noexcept
2208 { return _M_wdl.weekday(); }
2209
2210 constexpr chrono::weekday_last
2211 weekday_last() const noexcept
2212 { return _M_wdl; }
2213
2214 constexpr
2215 operator sys_days() const noexcept
2216 {
2217 const auto __d = sys_days{_M_y / _M_m / last};
2218 return sys_days{(__d - (chrono::weekday{__d}
2219 - _M_wdl.weekday())).time_since_epoch()};
2220 }
2221
2222 explicit constexpr
2223 operator local_days() const noexcept
2224 { return local_days{sys_days{*this}.time_since_epoch()}; }
2225
2226 constexpr bool
2227 ok() const noexcept
2228 { return _M_y.ok() && _M_m.ok() && _M_wdl.ok(); }
2229
2230 friend constexpr bool
2231 operator==(const year_month_weekday_last& __x,
2232 const year_month_weekday_last& __y) noexcept
2233 {
2234 return __x.year() == __y.year()
2235 && __x.month() == __y.month()
2236 && __x.weekday_last() == __y.weekday_last();
2237 }
2238
2239 template<typename = __detail::__months_years_conversion_disambiguator>
2240 friend constexpr year_month_weekday_last
2241 operator+(const year_month_weekday_last& __ymwdl,
2242 const months& __dm) noexcept
2243 {
2244 return ((__ymwdl.year() / __ymwdl.month() + __dm)
2245 / __ymwdl.weekday_last());
2246 }
2247
2248 template<typename = __detail::__months_years_conversion_disambiguator>
2249 friend constexpr year_month_weekday_last
2250 operator+(const months& __dm,
2251 const year_month_weekday_last& __ymwdl) noexcept
2252 { return __ymwdl + __dm; }
2253
2254 friend constexpr year_month_weekday_last
2255 operator+(const year_month_weekday_last& __ymwdl,
2256 const years& __dy) noexcept
2257 { return {__ymwdl.year() + __dy, __ymwdl.month(), __ymwdl.weekday_last()}; }
2258
2259 friend constexpr year_month_weekday_last
2260 operator+(const years& __dy,
2261 const year_month_weekday_last& __ymwdl) noexcept
2262 { return __ymwdl + __dy; }
2263
2264 template<typename = __detail::__months_years_conversion_disambiguator>
2265 friend constexpr year_month_weekday_last
2266 operator-(const year_month_weekday_last& __ymwdl,
2267 const months& __dm) noexcept
2268 { return __ymwdl + -__dm; }
2269
2270 friend constexpr year_month_weekday_last
2271 operator-(const year_month_weekday_last& __ymwdl,
2272 const years& __dy) noexcept
2273 { return __ymwdl + -__dy; }
2274
2275 friend constexpr year_month_weekday_last
2276 operator/(const year_month& __ym,
2277 const chrono::weekday_last& __wdl) noexcept
2278 { return {__ym.year(), __ym.month(), __wdl}; }
2279
2280 friend constexpr year_month_weekday_last
2281 operator/(const chrono::year& __y,
2282 const chrono::month_weekday_last& __mwdl) noexcept
2283 { return {__y, __mwdl.month(), __mwdl.weekday_last()}; }
2284
2285 friend constexpr year_month_weekday_last
2286 operator/(int __y, const chrono::month_weekday_last& __mwdl) noexcept
2287 { return chrono::year(__y) / __mwdl; }
2288
2289 friend constexpr year_month_weekday_last
2290 operator/(const chrono::month_weekday_last& __mwdl,
2291 const chrono::year& __y) noexcept
2292 { return __y / __mwdl; }
2293
2294 friend constexpr year_month_weekday_last
2295 operator/(const chrono::month_weekday_last& __mwdl, int __y) noexcept
2296 { return chrono::year(__y) / __mwdl; }
2297 };
2298
2299 // HH_MM_SS
2300
2301 /// @cond undocumented
2302 namespace __detail
2303 {
2304 consteval long long
2305 __pow10(unsigned __n)
2306 {
2307 long long __r = 1;
2308 while (__n-- > 0)
2309 __r *= 10;
2310 return __r;
2311 }
2312 }
2313 /// @endcond
2314
2315 /** Utility for splitting a duration into hours, minutes, and seconds
2316 *
2317 * This is a convenience type that provides accessors for the constituent
2318 * parts (hours, minutes, seconds and subseconds) of a duration.
2319 *
2320 * @since C++20
2321 */
2322 template<typename _Duration>
2323 class hh_mm_ss
2324 {
2325 static_assert( __is_duration<_Duration>::value );
2326
2327 private:
2328 static consteval int
2329 _S_fractional_width()
2330 {
2331 auto __den = _Duration::period::den;
2332 const int __multiplicity_2 = std::__countr_zero((uintmax_t)__den);
2333 __den >>= __multiplicity_2;
2334 int __multiplicity_5 = 0;
2335 while ((__den % 5) == 0)
2336 {
2337 ++__multiplicity_5;
2338 __den /= 5;
2339 }
2340 if (__den != 1)
2341 return 6;
2342
2343 int __width = (__multiplicity_2 > __multiplicity_5
2344 ? __multiplicity_2 : __multiplicity_5);
2345 if (__width > 18)
2346 __width = 18;
2347 return __width;
2348 }
2349
2350 constexpr
2351 hh_mm_ss(_Duration __d, bool __is_neg)
2352 : _M_h (duration_cast<chrono::hours>(__d)),
2353 _M_m (duration_cast<chrono::minutes>(__d - hours())),
2354 _M_s (duration_cast<chrono::seconds>(__d - hours() - minutes())),
2355 _M_is_neg(__is_neg)
2356 {
2357 auto __ss = __d - hours() - minutes() - seconds();
2358 if constexpr (treat_as_floating_point_v<typename precision::rep>)
2359 _M_ss._M_r = __ss.count();
2360 else if constexpr (precision::period::den != 1)
2361 _M_ss._M_r = duration_cast<precision>(__ss).count();
2362 }
2363
2364 // _GLIBCXX_RESOLVE_LIB_DEFECTS
2365 // 4274. hh_mm_ss constructor is ill-formed for unsigned durations
2366 static constexpr _Duration
2367 _S_abs(_Duration __d)
2368 {
2370 return chrono::abs(__d);
2371 else
2372 return __d;
2373 }
2374
2375 public:
2376 static constexpr unsigned fractional_width = {_S_fractional_width()};
2377
2378 using precision
2379 = duration<common_type_t<typename _Duration::rep,
2380 chrono::seconds::rep>,
2381 ratio<1, __detail::__pow10(fractional_width)>>;
2382
2383 constexpr hh_mm_ss() noexcept = default;
2384
2385 constexpr explicit
2386 hh_mm_ss(_Duration __d)
2387 : hh_mm_ss(_S_abs(__d), __d < _Duration::zero())
2388 { }
2389
2390 constexpr bool
2391 is_negative() const noexcept
2392 {
2393 if constexpr (!_S_is_unsigned)
2394 return _M_is_neg;
2395 else
2396 return false;
2397 }
2398
2399 constexpr chrono::hours
2400 hours() const noexcept
2401 { return _M_h; }
2402
2403 constexpr chrono::minutes
2404 minutes() const noexcept
2405 { return _M_m; }
2406
2407 constexpr chrono::seconds
2408 seconds() const noexcept
2409 { return _M_s; }
2410
2411 constexpr precision
2412 subseconds() const noexcept
2413 { return static_cast<precision>(_M_ss); }
2414
2415 constexpr explicit
2416 operator precision() const noexcept
2417 { return to_duration(); }
2418
2419 constexpr precision
2420 to_duration() const noexcept
2421 {
2422 if constexpr (!_S_is_unsigned)
2423 if (_M_is_neg)
2424 return -(_M_h + _M_m + _M_s + subseconds());
2425 return _M_h + _M_m + _M_s + subseconds();
2426 }
2427
2428 private:
2429 static constexpr bool _S_is_unsigned
2430 = __and_v<is_integral<typename _Duration::rep>,
2432
2433 template<typename _Ratio>
2434 using __byte_duration = duration<unsigned char, _Ratio>;
2435
2436 // The type of the _M_ss member that holds the subsecond precision.
2437 template<typename _Dur>
2438 struct __subseconds
2439 {
2440 typename _Dur::rep _M_r{};
2441
2442 constexpr explicit
2443 operator _Dur() const noexcept
2444 { return _Dur(_M_r); }
2445 };
2446
2447 // An empty class if this precision doesn't need subseconds.
2448 template<typename _Rep>
2449 requires (!treat_as_floating_point_v<_Rep>)
2450 struct __subseconds<duration<_Rep, ratio<1>>>
2451 {
2452 constexpr explicit
2453 operator duration<_Rep, ratio<1>>() const noexcept
2454 { return {}; }
2455 };
2456
2457 template<typename _Rep, typename _Period>
2458 requires (!treat_as_floating_point_v<_Rep>)
2459 && ratio_less_v<_Period, ratio<1, 1>>
2460 && ratio_greater_equal_v<_Period, ratio<1, 250>>
2461 struct __subseconds<duration<_Rep, _Period>>
2462 {
2463 unsigned char _M_r{};
2464
2465 constexpr explicit
2466 operator duration<_Rep, _Period>() const noexcept
2467 { return duration<_Rep, _Period>(_M_r); }
2468 };
2469
2470 template<typename _Rep, typename _Period>
2471 requires (!treat_as_floating_point_v<_Rep>)
2472 && ratio_less_v<_Period, ratio<1, 250>>
2473 && ratio_greater_equal_v<_Period, ratio<1, 4000000000>>
2474 struct __subseconds<duration<_Rep, _Period>>
2475 {
2476 uint_least32_t _M_r{};
2477
2478 constexpr explicit
2479 operator duration<_Rep, _Period>() const noexcept
2480 { return duration<_Rep, _Period>(_M_r); }
2481 };
2482
2483 chrono::hours _M_h{};
2484 __byte_duration<ratio<60>> _M_m{};
2485 __byte_duration<ratio<1>> _M_s{};
2486 bool _M_is_neg{};
2487 __subseconds<precision> _M_ss{};
2488 };
2489
2490 // 12/24 HOURS FUNCTIONS
2491
2492 constexpr bool
2493 is_am(const hours& __h) noexcept
2494 { return 0h <= __h && __h <= 11h; }
2495
2496 constexpr bool
2497 is_pm(const hours& __h) noexcept
2498 { return 12h <= __h && __h <= 23h; }
2499
2500 constexpr hours
2501 make12(const hours& __h) noexcept
2502 {
2503 if (__h == 0h)
2504 return 12h;
2505 else if (__h > 12h)
2506 return __h - 12h;
2507 return __h;
2508 }
2509
2510 constexpr hours
2511 make24(const hours& __h, bool __is_pm) noexcept
2512 {
2513 if (!__is_pm)
2514 {
2515 if (__h == 12h)
2516 return 0h;
2517 else
2518 return __h;
2519 }
2520 else
2521 {
2522 if (__h == 12h)
2523 return __h;
2524 else
2525 return __h + 12h;
2526 }
2527 }
2528
2529#if _GLIBCXX_HOSTED
2530#if _GLIBCXX_USE_CXX11_ABI || ! _GLIBCXX_USE_DUAL_ABI
2531 // C++20 [time.zones] Time zones
2532
2533 struct tzdb;
2534
2535 struct sys_info
2536 {
2537 sys_seconds begin;
2538 sys_seconds end;
2539 seconds offset;
2540 minutes save;
2541 string abbrev;
2542 };
2543
2544 struct local_info
2545 {
2546 static constexpr int unique = 0;
2547 static constexpr int nonexistent = 1;
2548 static constexpr int ambiguous = 2;
2549
2550 int result;
2551 sys_info first;
2552 sys_info second;
2553 };
2554
2555 class nonexistent_local_time : public runtime_error
2556 {
2557 public:
2558 template<typename _Duration>
2559 nonexistent_local_time(const local_time<_Duration>& __tp,
2560 const local_info& __i)
2561 : runtime_error(_S_make_what_str(__tp, __i))
2562 { __glibcxx_assert(__i.result == local_info::nonexistent); }
2563
2564 private:
2565 template<typename _Duration>
2566 static string
2567 _S_make_what_str(const local_time<_Duration>& __tp,
2568 const local_info& __i)
2569 {
2570 std::ostringstream __os;
2571 __os << __tp << " is in a gap between\n"
2572 << local_seconds(__i.first.end.time_since_epoch())
2573 + __i.first.offset << ' ' << __i.first.abbrev << " and\n"
2574 << local_seconds(__i.second.begin.time_since_epoch())
2575 + __i.second.offset << ' ' << __i.second.abbrev
2576 << " which are both equivalent to\n"
2577 << __i.first.end << " UTC";
2578 return std::move(__os).str();
2579 }
2580 };
2581
2582 class ambiguous_local_time : public runtime_error
2583 {
2584 public:
2585 template<typename _Duration>
2586 ambiguous_local_time(const local_time<_Duration>& __tp,
2587 const local_info& __i)
2588 : runtime_error(_S_make_what_str(__tp, __i))
2589 { __glibcxx_assert(__i.result == local_info::ambiguous); }
2590
2591 private:
2592 template<typename _Duration>
2593 static string
2594 _S_make_what_str(const local_time<_Duration>& __tp,
2595 const local_info& __i)
2596 {
2597 std::ostringstream __os;
2598 __os << __tp << " is ambiguous. It could be\n"
2599 << __tp << ' ' << __i.first.abbrev << " == "
2600 << __tp - __i.first.offset << " UTC or\n"
2601 << __tp << ' ' << __i.second.abbrev << " == "
2602 << __tp - __i.second.offset << " UTC";
2603 return std::move(__os).str();
2604 }
2605 };
2606
2607 template<typename _Duration>
2608 [[noreturn]] void
2609 __throw_bad_local_time(const local_time<_Duration>& __tp,
2610 const local_info& __i)
2611 {
2612#if __cpp_exceptions
2613 if (__i.result == local_info::nonexistent)
2614 throw nonexistent_local_time(__tp, __i);
2615 throw ambiguous_local_time(__tp, __i);
2616#else
2617 __builtin_abort();
2618#endif
2619 }
2620
2621 enum class choose { earliest, latest };
2622
2623 class time_zone
2624 {
2625 public:
2626 time_zone(time_zone&&) = default;
2627 time_zone& operator=(time_zone&&) = default;
2628
2629 ~time_zone();
2630
2631 [[nodiscard]]
2632 string_view name() const noexcept { return _M_name; }
2633
2634 template<typename _Duration>
2635 sys_info
2636 get_info(const sys_time<_Duration>& __st) const
2637 { return _M_get_sys_info(chrono::floor<seconds>(__st)); }
2638
2639 template<typename _Duration>
2640 local_info
2641 get_info(const local_time<_Duration>& __tp) const
2642 { return _M_get_local_info(chrono::floor<seconds>(__tp)); }
2643
2644 template<typename _Duration>
2645 sys_time<common_type_t<_Duration, seconds>>
2646 to_sys(const local_time<_Duration>& __tp) const
2647 {
2648 local_info __info = get_info(__tp);
2649
2650 if (__info.result != local_info::unique)
2651 __throw_bad_local_time(__tp, __info);
2652
2653 return sys_time<_Duration>(__tp.time_since_epoch())
2654 - __info.first.offset;
2655 }
2656
2657 template<typename _Duration>
2658 sys_time<common_type_t<_Duration, seconds>>
2659 to_sys(const local_time<_Duration>& __tp, choose __z) const
2660 {
2661 local_info __info = get_info(__tp);
2662
2663 if (__info.result == local_info::nonexistent)
2664 return __info.first.end; // Last second of the previous sys_info.
2665
2666 sys_time<_Duration> __st(__tp.time_since_epoch());
2667
2668 if (__info.result == local_info::ambiguous && __z == choose::latest)
2669 return __st - __info.second.offset; // Time in the later sys_info.
2670 // else if __z == earliest, use __info.first.offset as below:
2671
2672 return __st - __info.first.offset;
2673 }
2674
2675 template<typename _Duration>
2676 local_time<common_type_t<_Duration, seconds>>
2677 to_local(const sys_time<_Duration>& __tp) const
2678 {
2679 auto __d = (__tp + get_info(__tp).offset).time_since_epoch();
2680 return local_time<common_type_t<_Duration, seconds>>(__d);
2681 }
2682
2683 [[nodiscard]] friend bool
2684 operator==(const time_zone& __x, const time_zone& __y) noexcept
2685 { return __x._M_name == __y._M_name; }
2686
2687 [[nodiscard]] friend strong_ordering
2688 operator<=>(const time_zone& __x, const time_zone& __y) noexcept
2689 { return __x._M_name <=> __y._M_name; }
2690
2691 private:
2692 sys_info _M_get_sys_info(sys_seconds) const;
2693 local_info _M_get_local_info(local_seconds) const;
2694
2695 friend const tzdb& reload_tzdb();
2696 friend struct tzdb;
2697 friend class tzdb_list;
2698
2699 struct _Impl;
2700
2701 explicit time_zone(unique_ptr<_Impl> __p);
2702 string _M_name;
2703 unique_ptr<_Impl> _M_impl;
2704 };
2705
2706 const time_zone* locate_zone(string_view __tz_name);
2707 const time_zone* current_zone();
2708
2709 /** The list of `chrono::tzdb` objects
2710 *
2711 * A single object of this type is constructed by the C++ runtime,
2712 * and can be accessed by calling `chrono::get_tzdb_list()`.
2713 *
2714 * The front of the list is the current `tzdb` object and can be accessed
2715 * via `chrono::get_tzdb_list().front()` or `chrono::get_tzdb()` or
2716 * `*chrono::get_tzdb_list().begin()`.
2717 *
2718 * The `chrono::reload_tzdb()` function will check for a newer version
2719 * and if found, insert it at the front of the list.
2720 *
2721 * @since C++20
2722 */
2723 class tzdb_list
2724 {
2725 struct _Node;
2726
2727 public:
2728 tzdb_list(const tzdb_list&) = delete;
2729 tzdb_list& operator=(const tzdb_list&) = delete;
2730
2731 /** An iterator into the `tzdb_list`
2732 *
2733 * As a extension, in libstdc++ each `tzdb` is reference-counted
2734 * and the `const_iterator` type shares ownership of the object it
2735 * refers to. This ensures that a `tzdb` erased from the list will
2736 * not be destroyed while there is an iterator that refers to it.
2737 */
2738 class const_iterator
2739 {
2740 public:
2741 using value_type = tzdb;
2742 using reference = const tzdb&;
2743 using pointer = const tzdb*;
2744 using difference_type = ptrdiff_t;
2745 using iterator_category = forward_iterator_tag;
2746
2747 constexpr const_iterator() = default;
2748 const_iterator(const const_iterator&) = default;
2749 const_iterator(const_iterator&&) = default;
2750 const_iterator& operator=(const const_iterator&) = default;
2751 const_iterator& operator=(const_iterator&&) = default;
2752
2753 reference operator*() const noexcept;
2754 pointer operator->() const noexcept { return &**this; }
2755 const_iterator& operator++();
2756 const_iterator operator++(int);
2757
2758 bool operator==(const const_iterator&) const noexcept = default;
2759
2760 private:
2761 explicit const_iterator(const shared_ptr<_Node>&) noexcept;
2762
2763 friend class tzdb_list;
2764
2765 shared_ptr<_Node> _M_node;
2766 void* _M_reserved = nullptr;
2767 };
2768
2769 /** Access the current `tzdb` at the front of the list.
2770 *
2771 * This returns a reference to the same object as `chrono::get_tzdb()`.
2772 *
2773 * @returns A reference to the current tzdb object.
2774 * @since C++20
2775 */
2776 const tzdb& front() const noexcept;
2777
2778 /** Remove the tzdb object _after_ the one the iterator refers to.
2779 *
2780 * Calling this function concurrently with any of `front()`, `begin()`,
2781 * or `end()` does not cause a data race, but in general this function
2782 * is not thread-safe. The behaviour may be undefined if erasing an
2783 * element from the list while another thread is calling the same
2784 * function, or incrementing an iterator into the list, or accessing
2785 * the element being erased (unless it is accessed through an iterator).
2786 *
2787 * @param __p A dereferenceable iterator.
2788 * @returns An iterator the element after the one that was erased
2789 * (or `end()` if there is no such element).
2790 * @since C++20
2791 */
2793
2794 const_iterator begin() const noexcept;
2795 const_iterator end() const noexcept { return {}; }
2796 const_iterator cbegin() const noexcept { return begin(); }
2797 const_iterator cend() const noexcept { return end(); }
2798
2799 private:
2800 constexpr explicit tzdb_list(nullptr_t);
2801
2802 friend tzdb_list& get_tzdb_list();
2803 friend const tzdb& get_tzdb();
2804 friend const tzdb& reload_tzdb();
2805 friend struct tzdb;
2806 friend class leap_second;
2807 friend struct time_zone::_Impl;
2808 friend class time_zone_link;
2809 };
2810
2811 class time_zone_link
2812 {
2813 public:
2814 time_zone_link(time_zone_link&&) = default;
2815 time_zone_link& operator=(time_zone_link&&) = default;
2816
2817 string_view name() const noexcept { return _M_name; }
2818 string_view target() const noexcept { return _M_target; }
2819
2820 friend bool
2821 operator==(const time_zone_link& __x, const time_zone_link& __y) noexcept
2822 { return __x.name() == __y.name(); }
2823
2824 friend strong_ordering
2825 operator<=>(const time_zone_link& __x, const time_zone_link& __y) noexcept
2826 { return __x.name() <=> __y.name(); }
2827
2828 private:
2829 friend const tzdb& reload_tzdb();
2830 friend struct tzdb_list::_Node;
2831
2832 explicit time_zone_link(nullptr_t) { }
2833
2834 string _M_name;
2835 string _M_target;
2836 };
2837
2838 class leap_second
2839 {
2840 public:
2841 leap_second(const leap_second&) = default;
2842 leap_second& operator=(const leap_second&) = default;
2843
2844 [[nodiscard]]
2845 constexpr sys_seconds
2846 date() const noexcept
2847 {
2848 if (_M_s >= _M_s.zero()) [[likely]]
2849 return sys_seconds(_M_s);
2850 return sys_seconds(-_M_s);
2851 }
2852
2853 [[nodiscard]]
2854 constexpr seconds
2855 value() const noexcept
2856 {
2857 if (_M_s >= _M_s.zero()) [[likely]]
2858 return seconds(1);
2859 return seconds(-1);
2860 }
2861
2862 // This can be defaulted because the database will never contain two
2863 // leap_second objects with the same date but different signs.
2864 [[nodiscard]] friend constexpr bool
2865 operator==(const leap_second&, const leap_second&) noexcept = default;
2866
2867 [[nodiscard]] friend constexpr strong_ordering
2868 operator<=>(const leap_second& __x, const leap_second& __y) noexcept
2869 { return __x.date() <=> __y.date(); }
2870
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; }
2876
2877 template<typename _Duration>
2878 [[nodiscard]] friend constexpr bool
2879 operator<(const leap_second& __x,
2880 const sys_time<_Duration>& __y) noexcept
2881 { return __x.date() < __y; }
2882
2883 template<typename _Duration>
2884 [[nodiscard]] friend constexpr bool
2885 operator<(const sys_time<_Duration>& __x,
2886 const leap_second& __y) noexcept
2887 { return __x < __y.date(); }
2888
2889 template<typename _Duration>
2890 [[nodiscard]] friend constexpr bool
2891 operator>(const leap_second& __x,
2892 const sys_time<_Duration>& __y) noexcept
2893 { return __y < __x.date(); }
2894
2895 template<typename _Duration>
2896 [[nodiscard]] friend constexpr bool
2897 operator>(const sys_time<_Duration>& __x,
2898 const leap_second& __y) noexcept
2899 { return __y.date() < __x; }
2900
2901 template<typename _Duration>
2902 [[nodiscard]] friend constexpr bool
2903 operator<=(const leap_second& __x,
2904 const sys_time<_Duration>& __y) noexcept
2905 { return !(__y < __x.date()); }
2906
2907 template<typename _Duration>
2908 [[nodiscard]] friend constexpr bool
2909 operator<=(const sys_time<_Duration>& __x,
2910 const leap_second& __y) noexcept
2911 { return !(__y.date() < __x); }
2912
2913 template<typename _Duration>
2914 [[nodiscard]] friend constexpr bool
2915 operator>=(const leap_second& __x,
2916 const sys_time<_Duration>& __y) noexcept
2917 { return !(__x.date() < __y); }
2918
2919 template<typename _Duration>
2920 [[nodiscard]] friend constexpr bool
2921 operator>=(const sys_time<_Duration>& __x,
2922 const leap_second& __y) noexcept
2923 { return !(__x < __y.date()); }
2924
2925 // This is a simplified form of the constraint specified in the standard,
2926 // three_way_comparable_with<sys_seconds, sys_time<_Duration>>.
2927 template<three_way_comparable_with<seconds> _Duration>
2928 [[nodiscard]] friend constexpr auto
2929 operator<=>(const leap_second& __x,
2930 const sys_time<_Duration>& __y) noexcept
2931 { return __x.date() <=> __y; }
2932
2933 private:
2934 explicit leap_second(seconds::rep __s) : _M_s(__s) { }
2935
2936 friend struct tzdb_list::_Node;
2937
2938 friend const tzdb& reload_tzdb();
2939
2940 template<typename _Duration>
2941 friend leap_second_info
2942 get_leap_second_info(const utc_time<_Duration>&);
2943
2944 seconds _M_s; // == date().time_since_epoch() * value().count()
2945 };
2946
2947 template<class _Tp> struct zoned_traits { };
2948
2949 template<>
2950 struct zoned_traits<const time_zone*>
2951 {
2952 static const time_zone*
2953 default_zone()
2954 { return std::chrono::locate_zone("UTC"); }
2955
2956 static const time_zone*
2957 locate_zone(string_view __name)
2958 { return std::chrono::locate_zone(__name); }
2959 };
2960
2961 struct tzdb
2962 {
2963 string version;
2964 _GLIBCXX_STD_C::vector<time_zone> zones;
2965 _GLIBCXX_STD_C::vector<time_zone_link> links;
2966 _GLIBCXX_STD_C::vector<leap_second> leap_seconds;
2967
2968 const time_zone*
2969 locate_zone(string_view __tz_name) const;
2970
2971 const time_zone*
2972 current_zone() const;
2973
2974 private:
2975 friend const tzdb& reload_tzdb();
2976 friend class time_zone;
2977 friend struct tzdb_list::_Node;
2978 };
2979
2980 tzdb_list& get_tzdb_list();
2981 const tzdb& get_tzdb();
2982
2983 const tzdb& reload_tzdb();
2984 string remote_version();
2985
2986 template<typename _Duration, typename _TimeZonePtr = const time_zone*>
2987 class zoned_time
2988 {
2989 static_assert(__is_duration_v<_Duration>);
2990
2991 using _Traits = zoned_traits<_TimeZonePtr>;
2992
2993 // Every constructor that accepts a string_view as its first parameter
2994 // does not participate in class template argument deduction.
2995 using string_view = type_identity_t<std::string_view>;
2996
2997 public:
2998 using duration = common_type_t<_Duration, seconds>;
2999
3000 zoned_time() requires requires { _Traits::default_zone(); }
3001 { }
3002
3003 zoned_time(const zoned_time&) = default;
3004 zoned_time& operator=(const zoned_time&) = default;
3005
3006 zoned_time(const sys_time<_Duration>& __st)
3007 requires requires { _Traits::default_zone(); }
3008 : _M_tp(__st)
3009 { }
3010
3011 explicit
3012 zoned_time(_TimeZonePtr __z) : _M_zone(std::move(__z)) { }
3013
3014 explicit
3015 zoned_time(string_view __name)
3016 requires requires {
3017 _TimeZonePtr{_Traits::locate_zone(std::string_view{})};
3018 }
3019 : _M_zone(_Traits::locate_zone(__name))
3020 { }
3021
3022 template<typename _Duration2>
3023 zoned_time(const zoned_time<_Duration2, _TimeZonePtr>& __zt)
3024 requires is_convertible_v<sys_time<_Duration2>, sys_time<_Duration>>
3025 : _M_zone(__zt._M_zone), _M_tp(__zt._M_tp)
3026 { }
3027
3028 zoned_time(_TimeZonePtr __z, const sys_time<_Duration>& __st)
3029 : _M_zone(std::move(__z)), _M_tp(__st)
3030 { }
3031
3032 zoned_time(string_view __name, const sys_time<_Duration>& __st)
3033 : zoned_time(_Traits::locate_zone(__name), __st)
3034 { }
3035
3036 zoned_time(_TimeZonePtr __z, const local_time<_Duration>& __tp)
3037 requires requires {
3038 { __z->to_sys(__tp) } -> convertible_to<sys_time<_Duration>>;
3039 }
3040 : _M_zone(std::move(__z)), _M_tp(_M_zone->to_sys(__tp))
3041 { }
3042
3043 zoned_time(string_view __name, const local_time<_Duration>& __tp)
3044 requires requires (_TimeZonePtr __z) {
3045 { _Traits::locate_zone(__name) } -> convertible_to<_TimeZonePtr>;
3046 { __z->to_sys(__tp) } -> convertible_to<sys_time<_Duration>>;
3047 }
3048 : zoned_time(_Traits::locate_zone(__name), __tp)
3049 { }
3050
3051 zoned_time(_TimeZonePtr __z, const local_time<_Duration>& __tp,
3052 choose __c)
3053 requires requires {
3054 { __z->to_sys(__tp, __c) } -> convertible_to<sys_time<_Duration>>;
3055 }
3056 : _M_zone(std::move(__z)), _M_tp(_M_zone->to_sys(__tp, __c))
3057 { }
3058
3059 zoned_time(string_view __name, const local_time<_Duration>& __tp,
3060 choose __c)
3061 requires requires (_TimeZonePtr __z) {
3062 { _Traits::locate_zone(__name) } -> convertible_to<_TimeZonePtr>;
3063 { __z->to_sys(__tp, __c) } -> convertible_to<sys_time<_Duration>>;
3064 }
3065 : _M_zone(_Traits::locate_zone(__name)),
3066 _M_tp(_M_zone->to_sys(__tp, __c))
3067 { }
3068
3069 template<typename _Duration2, typename _TimeZonePtr2>
3070 zoned_time(_TimeZonePtr __z,
3071 const zoned_time<_Duration2, _TimeZonePtr2>& __zt)
3072 requires is_convertible_v<sys_time<_Duration2>, sys_time<_Duration>>
3073 : _M_zone(__z), _M_tp(__zt._M_tp)
3074 { }
3075
3076 template<typename _Duration2, typename _TimeZonePtr2>
3077 zoned_time(_TimeZonePtr __z,
3078 const zoned_time<_Duration2, _TimeZonePtr2>& __zt,
3079 choose)
3080 requires is_convertible_v<sys_time<_Duration2>, sys_time<_Duration>>
3081 : _M_zone(__z), _M_tp(__zt._M_tp)
3082 { }
3083
3084 template<typename _Duration2, typename _TimeZonePtr2>
3085 zoned_time(string_view __name,
3086 const zoned_time<_Duration2, _TimeZonePtr2>& __zt)
3087 requires is_convertible_v<sys_time<_Duration2>, sys_time<_Duration>>
3088 && requires {
3089 { _Traits::locate_zone(__name) } -> convertible_to<_TimeZonePtr>;
3090 }
3091 : _M_zone(_Traits::locate_zone(__name)), _M_tp(__zt._M_tp)
3092 { }
3093
3094 template<typename _Duration2, typename _TimeZonePtr2>
3095 zoned_time(string_view __name,
3096 const zoned_time<_Duration2, _TimeZonePtr2>& __zt,
3097 choose)
3098 requires is_convertible_v<sys_time<_Duration2>, sys_time<_Duration>>
3099 && requires {
3100 { _Traits::locate_zone(__name) } -> convertible_to<_TimeZonePtr>;
3101 }
3102 : _M_zone(_Traits::locate_zone(__name)), _M_tp(__zt._M_tp)
3103 { }
3104
3105 zoned_time&
3106 operator=(const sys_time<_Duration>& __st)
3107 {
3108 _M_tp = __st;
3109 return *this;
3110 }
3111
3112 zoned_time&
3113 operator=(const local_time<_Duration>& __lt)
3114 {
3115 _M_tp = _M_zone->to_sys(__lt);
3116 return *this;
3117 }
3118
3119 [[nodiscard]]
3120 operator sys_time<duration>() const { return _M_tp; }
3121
3122 [[nodiscard]]
3123 explicit operator local_time<duration>() const
3124 { return get_local_time(); }
3125
3126 [[nodiscard]]
3127 _TimeZonePtr
3128 get_time_zone() const
3129 { return _M_zone; }
3130
3131 [[nodiscard]]
3132 local_time<duration>
3133 get_local_time() const
3134 { return _M_zone->to_local(_M_tp); }
3135
3136 [[nodiscard]]
3137 sys_time<duration>
3138 get_sys_time() const
3139 { return _M_tp; }
3140
3141 [[nodiscard]]
3142 sys_info
3143 get_info() const
3144 { return _M_zone->get_info(_M_tp); }
3145
3146 [[nodiscard]] friend bool
3147 operator==(const zoned_time&, const zoned_time&) = default;
3148
3149 private:
3150 _TimeZonePtr _M_zone{ _Traits::default_zone() };
3151 sys_time<duration> _M_tp{};
3152
3153 template<typename _Duration2, typename _TimeZonePtr2>
3154 friend class zoned_time;
3155 };
3156
3157 zoned_time() -> zoned_time<seconds>;
3158
3159 template<typename _Duration>
3160 zoned_time(sys_time<_Duration>)
3161 -> zoned_time<common_type_t<_Duration, seconds>>;
3162
3163 /// @cond undocumented
3164 template<typename _TimeZonePtrOrName>
3165 using __time_zone_representation
3166 = __conditional_t<is_convertible_v<_TimeZonePtrOrName, string_view>,
3167 const time_zone*,
3168 remove_cvref_t<_TimeZonePtrOrName>>;
3169 /// @endcond
3170
3171 template<typename _TimeZonePtrOrName>
3172 zoned_time(_TimeZonePtrOrName&&)
3173 -> zoned_time<seconds, __time_zone_representation<_TimeZonePtrOrName>>;
3174
3175 template<typename _TimeZonePtrOrName, typename _Duration>
3176 zoned_time(_TimeZonePtrOrName&&, sys_time<_Duration>)
3177 -> zoned_time<common_type_t<_Duration, seconds>,
3178 __time_zone_representation<_TimeZonePtrOrName>>;
3179
3180 template<typename _TimeZonePtrOrName, typename _Duration>
3181 zoned_time(_TimeZonePtrOrName&&, local_time<_Duration>,
3182 choose = choose::earliest)
3183 -> zoned_time<common_type_t<_Duration, seconds>,
3184 __time_zone_representation<_TimeZonePtrOrName>>;
3185
3186 template<typename _Duration, typename _TimeZonePtrOrName,
3187 typename _TimeZonePtr2>
3188 zoned_time(_TimeZonePtrOrName&&, zoned_time<_Duration, _TimeZonePtr2>,
3189 choose = choose::earliest)
3190 -> zoned_time<common_type_t<_Duration, seconds>,
3191 __time_zone_representation<_TimeZonePtrOrName>>;
3192
3193 template<typename _Dur1, typename _TZPtr1, typename _Dur2, typename _TZPtr2>
3194 [[nodiscard]]
3195 inline bool
3196 operator==(const zoned_time<_Dur1, _TZPtr1>& __x,
3197 const zoned_time<_Dur2, _TZPtr2>& __y)
3198 {
3199 return __x.get_time_zone() == __y.get_time_zone()
3200 && __x.get_sys_time() == __y.get_sys_time();
3201 }
3202
3203 using zoned_seconds = zoned_time<seconds>;
3204#endif // _GLIBCXX_USE_CXX11_ABI || ! _GLIBCXX_USE_DUAL_ABI
3205
3206namespace __detail
3207{
3208 inline leap_second_info
3209 __get_leap_second_info(sys_seconds __ss, bool __is_utc)
3210 {
3211 if (__ss < sys_seconds{}) [[unlikely]]
3212 return {};
3213
3214 const seconds::rep __leaps[] {
3215 78796800, // 1 Jul 1972
3216 94694400, // 1 Jan 1973
3217 126230400, // 1 Jan 1974
3218 157766400, // 1 Jan 1975
3219 189302400, // 1 Jan 1976
3220 220924800, // 1 Jan 1977
3221 252460800, // 1 Jan 1978
3222 283996800, // 1 Jan 1979
3223 315532800, // 1 Jan 1980
3224 362793600, // 1 Jul 1981
3225 394329600, // 1 Jul 1982
3226 425865600, // 1 Jul 1983
3227 489024000, // 1 Jul 1985
3228 567993600, // 1 Jan 1988
3229 631152000, // 1 Jan 1990
3230 662688000, // 1 Jan 1991
3231 709948800, // 1 Jul 1992
3232 741484800, // 1 Jul 1993
3233 773020800, // 1 Jul 1994
3234 820454400, // 1 Jan 1996
3235 867715200, // 1 Jul 1997
3236 915148800, // 1 Jan 1999
3237 1136073600, // 1 Jan 2006
3238 1230768000, // 1 Jan 2009
3239 1341100800, // 1 Jul 2012
3240 1435708800, // 1 Jul 2015
3241 1483228800, // 1 Jan 2017
3242 };
3243 // The list above is known to be valid until (at least) this date
3244 // and only contains positive leap seconds.
3245 constexpr sys_seconds __expires(1782604800s); // 2026-06-28 00:00:00 UTC
3246
3247#if _GLIBCXX_USE_CXX11_ABI || ! _GLIBCXX_USE_DUAL_ABI
3248 if (__ss > __expires)
3249 {
3250 // Use updated leap_seconds from tzdb.
3251 size_t __n = std::size(__leaps);
3252
3253 auto __db = get_tzdb_list().begin();
3254 auto __first = __db->leap_seconds.begin() + __n;
3255 auto __last = __db->leap_seconds.end();
3256 auto __pos = std::upper_bound(__first, __last, __ss);
3257 seconds __elapsed(__n);
3258 for (auto __i = __first; __i != __pos; ++__i)
3259 __elapsed += __i->value();
3260
3261 if (__is_utc)
3262 {
3263 // Convert utc_time to sys_time:
3264 __ss -= __elapsed;
3265 // See if that sys_time is before (or during) previous leap sec:
3266 if (__pos != __first && __ss < __pos[-1])
3267 {
3268 if ((__ss + 1s) >= __pos[-1])
3269 return {true, __elapsed};
3270 __elapsed -= __pos[-1].value();
3271 }
3272 }
3273 return {false, __elapsed};
3274 }
3275 else
3276#endif
3277 {
3278 seconds::rep __s = __ss.time_since_epoch().count();
3279 const seconds::rep* __first = std::begin(__leaps);
3280 const seconds::rep* __last = std::end(__leaps);
3281
3282 // Don't bother searching the list if we're after the last one.
3283 if (__s > (__last[-1] + (__last - __first) + 1))
3284 return { false, seconds(__last - __first) };
3285
3286 auto __pos = std::upper_bound(__first, __last, __s);
3287 seconds __elapsed{__pos - __first};
3288 if (__is_utc)
3289 {
3290 // Convert utc_time to sys_time:
3291 __s -= __elapsed.count();
3292 // See if that sys_time is before (or during) previous leap sec:
3293 if (__pos != __first && __s < __pos[-1])
3294 {
3295 if ((__s + 1) >= __pos[-1])
3296 return {true, __elapsed};
3297 --__elapsed;
3298 }
3299 }
3300 return {false, __elapsed};
3301 }
3302 }
3303} // namespace __detail
3304
3305 template<typename _Duration>
3306 [[nodiscard]]
3307 inline leap_second_info
3308 get_leap_second_info(const utc_time<_Duration>& __ut)
3309 {
3310 auto __s = chrono::duration_cast<seconds>(__ut.time_since_epoch());
3311 return __detail::__get_leap_second_info(sys_seconds(__s), true);
3312 }
3313
3314 template<typename _Duration>
3315 [[nodiscard]]
3316 inline utc_time<common_type_t<_Duration, seconds>>
3317 utc_clock::from_sys(const sys_time<_Duration>& __t)
3318 {
3319 using _CDur = common_type_t<_Duration, seconds>;
3320 auto __s = chrono::time_point_cast<seconds>(__t);
3321 const auto __li = __detail::__get_leap_second_info(__s, false);
3322 return utc_time<_CDur>{__t.time_since_epoch()} + __li.elapsed;
3323 }
3324#endif // _GLIBCXX_HOSTED
3325
3326 /// @} group chrono
3327#endif // C++20
3328 } // namespace chrono
3329
3330#if __glibcxx_chrono_cxx20 >= 202306 // C++26
3331 // Hash support [time.hash]
3332
3333 template<typename _Tp>
3334 concept __is_nothrow_copy_hashable = requires(const _Tp& __t) {
3335 { hash<_Tp>{}(_Tp(__t)) } noexcept -> same_as<size_t>;
3336 };
3337
3338 namespace chrono {
3339
3340 template<typename _T1, typename... _Ts>
3341 [[__gnu__::__always_inline__]]
3342 constexpr auto
3343 __pack_ints(_T1 __v1, _Ts... __vs)
3344 {
3345 using _ResT = decltype([] {
3346 constexpr size_t __tsize = (sizeof(_T1) + ... + sizeof(_Ts));
3347 if constexpr (__tsize <= 1)
3348 return static_cast<unsigned char>(0);
3349 else if constexpr (__tsize <= 2)
3350 return static_cast<__UINT16_TYPE__>(0);
3351 else if constexpr (__tsize <= 4)
3352 return static_cast<__UINT32_TYPE__>(0);
3353 else if constexpr (__tsize <= 8)
3354 return static_cast<__UINT64_TYPE__>(0);
3355 else
3356 static_assert(__tsize <= 8);
3357 }());
3358
3359 _ResT __res = __v1;
3360 ((__res = (__res << (sizeof(_Ts) * __CHAR_BIT__) | _ResT(__vs))), ...);
3361 return __res;
3362 }
3363
3364 template<typename _Tp>
3365 [[__gnu__::__always_inline__]]
3366 constexpr auto
3367 __as_int(_Tp __val)
3368 {
3369 if constexpr (is_same_v<_Tp, year>)
3370 return static_cast<unsigned short>(static_cast<int>(__val));
3371 else if constexpr (is_same_v<_Tp, month> || is_same_v<_Tp, day>)
3372 return static_cast<unsigned char>(static_cast<unsigned>(__val));
3373 else if constexpr (is_same_v<_Tp, weekday>)
3374 return static_cast<unsigned char>(__val.c_encoding());
3375 else if constexpr (is_same_v<_Tp, weekday_indexed>)
3376 return __pack_ints(chrono::__as_int(__val.weekday()),
3377 static_cast<unsigned char>(__val.index()));
3378 else if constexpr (is_same_v<_Tp, weekday_last>)
3379 return chrono::__as_int(__val.weekday());
3380 else
3381 static_assert(false);
3382 }
3383
3384 template<typename _Arg, typename... _Args>
3385 size_t
3386 __int_hash(_Arg __arg, _Args... __args)
3387 {
3388 static_assert((is_integral_v<_Arg> && ... && is_integral_v<_Args>));
3389
3390 // TODO consider using a better quality hasher
3391 using _Hasher = _Hash_impl;
3392 size_t __result = _Hasher::hash(__arg);
3393 ((__result = _Hasher::__hash_combine(__args, __result)), ...);
3394 return __result;
3395 }
3396
3397 template<typename... _Tps>
3398 [[__gnu__::__always_inline__]]
3399 inline size_t
3400 __hash(_Tps... __vals)
3401 {
3402 if constexpr (sizeof...(_Tps) == 1)
3403 return chrono::__int_hash(chrono::__as_int(__vals)...);
3404 else
3405 {
3406 auto __res = chrono::__pack_ints(chrono::__as_int(__vals)...);
3407 return chrono::__int_hash(__res);
3408 }
3409 }
3410 } // namespace chrono
3411
3412 // duration
3413 template<typename _Rep, typename _Period>
3414 requires __is_hash_enabled_for<_Rep>
3415 struct hash<chrono::duration<_Rep, _Period>>
3416 {
3417 size_t
3418 operator()(const chrono::duration<_Rep, _Period>& __val) const
3419 noexcept(__is_nothrow_copy_hashable<_Rep>)
3420 {
3421 if constexpr (is_integral_v<_Rep>)
3422 return chrono::__int_hash(__val.count());
3423 else
3424 return hash<_Rep>{}(__val.count());
3425 }
3426 };
3427
3428 template<typename _Rep, typename _Period>
3429 struct __is_fast_hash<hash<chrono::duration<_Rep, _Period>>>
3430 : __is_fast_hash<hash<_Rep>>
3431 {};
3432
3433 // time_point
3434 template<typename _Clock, typename _Dur>
3435 requires __is_hash_enabled_for<_Dur>
3436 struct hash<chrono::time_point<_Clock, _Dur>>
3437 {
3438 size_t
3439 operator()(const chrono::time_point<_Clock, _Dur>& __val) const
3440 noexcept(__is_nothrow_copy_hashable<_Dur>)
3441 { return hash<_Dur>{}(__val.time_since_epoch()); }
3442 };
3443
3444 template<typename _Clock, typename _Dur>
3445 struct __is_fast_hash<hash<chrono::time_point<_Clock, _Dur>>>
3446 : __is_fast_hash<hash<_Dur>>
3447 {};
3448
3449 // day
3450 template<>
3451 struct hash<chrono::day>
3452 {
3453 size_t
3454 operator()(chrono::day __val) const noexcept
3455 { return chrono::__hash(__val); }
3456 };
3457
3458 // month
3459 template<>
3460 struct hash<chrono::month>
3461 {
3462 size_t
3463 operator()(chrono::month __val) const noexcept
3464 { return chrono::__hash(__val); }
3465 };
3466
3467 // year
3468 template<>
3469 struct hash<chrono::year>
3470 {
3471 size_t
3472 operator()(chrono::year __val) const noexcept
3473 { return chrono::__hash(__val); }
3474 };
3475
3476 // weekday
3477 template<>
3478 struct hash<chrono::weekday>
3479 {
3480 size_t
3481 operator()(chrono::weekday __val) const noexcept
3482 { return chrono::__hash(__val); }
3483 };
3484
3485 // weekday_indexed
3486 template<>
3487 struct hash<chrono::weekday_indexed>
3488 {
3489 size_t
3490 operator()(chrono::weekday_indexed __val) const noexcept
3491 { return chrono::__hash(__val); }
3492 };
3493
3494 // weekday_last
3495 template<>
3496 struct hash<chrono::weekday_last>
3497 {
3498 size_t
3499 operator()(chrono::weekday_last __val) const noexcept
3500 { return chrono::__hash(__val); }
3501 };
3502
3503 // month_day
3504 template<>
3505 struct hash<chrono::month_day>
3506 {
3507 size_t
3508 operator()(chrono::month_day __val) const noexcept
3509 { return chrono::__hash(__val.month(), __val.day()); }
3510 };
3511
3512 // month_day_last
3513 template<>
3514 struct hash<chrono::month_day_last>
3515 {
3516 size_t operator()(chrono::month_day_last __val) const noexcept
3517 { return chrono::__hash(__val.month()); }
3518 };
3519
3520 // month_weekday
3521 template<>
3522 struct hash<chrono::month_weekday>
3523 {
3524 size_t
3525 operator()(chrono::month_weekday __val) const noexcept
3526 { return chrono::__hash(__val.month(), __val.weekday_indexed()); }
3527 };
3528
3529 // month_weekday_last
3530 template<>
3531 struct hash<chrono::month_weekday_last>
3532 {
3533 size_t
3534 operator()(chrono::month_weekday_last __val) const noexcept
3535 { return chrono::__hash(__val.month(), __val.weekday_last()); }
3536 };
3537
3538 // year_month
3539 template<>
3540 struct hash<chrono::year_month>
3541 {
3542 size_t
3543 operator()(chrono::year_month __val) const noexcept
3544 { return chrono::__hash(__val.year(), __val.month()); }
3545 };
3546
3547 // year_month_day
3548 template<>
3549 struct hash<chrono::year_month_day>
3550 {
3551 size_t
3552 operator()(chrono::year_month_day __val) const noexcept
3553 { return chrono::__hash(__val.year(), __val.month(), __val.day()); }
3554 };
3555
3556 // year_month_day_last
3557 template<>
3558 struct hash<chrono::year_month_day_last>
3559 {
3560 size_t
3561 operator()(chrono::year_month_day_last __val) const noexcept
3562 { return chrono::__hash(__val.year(), __val.month()); }
3563 };
3564
3565 // year_month_weekday
3566 template<>
3567 struct hash<chrono::year_month_weekday>
3568 {
3569 size_t
3570 operator()(chrono::year_month_weekday __val) const noexcept
3571 {
3572 return chrono::__hash(__val.year(), __val.month(),
3573 __val.weekday_indexed());
3574 }
3575 };
3576
3577 // year_month_weekday_last
3578 template<>
3579 struct hash<chrono::year_month_weekday_last>
3580 {
3581 size_t
3582 operator()(chrono::year_month_weekday_last __val) const noexcept
3583 {
3584 return chrono::__hash(__val.year(), __val.month(),
3585 __val.weekday_last());
3586 }
3587 };
3588
3589#if _GLIBCXX_HOSTED
3590#if _GLIBCXX_USE_CXX11_ABI || ! _GLIBCXX_USE_DUAL_ABI
3591 // zoned_time
3592 template<typename _Duration, typename _TimeZonePtr>
3593 requires __is_hash_enabled_for<
3594 typename chrono::zoned_time<_Duration, _TimeZonePtr>::duration>
3595 && __is_hash_enabled_for<_TimeZonePtr>
3596 struct hash<chrono::zoned_time<_Duration, _TimeZonePtr>>
3597 {
3598 private:
3599 using _ActualDuration =
3600 typename chrono::zoned_time<_Duration, _TimeZonePtr>::duration;
3601
3602 public:
3603 size_t
3604 operator()(const chrono::zoned_time<_Duration, _TimeZonePtr>& __val) const
3605 noexcept(__is_nothrow_copy_hashable<_ActualDuration>
3606 && __is_nothrow_copy_hashable<_TimeZonePtr>)
3607 {
3608 const auto __iduration = [&] {
3609 const _ActualDuration __sd = __val.get_sys_time().time_since_epoch();
3610 if constexpr (is_integral_v<typename _ActualDuration::rep>)
3611 return __sd.count();
3612 else
3613 return hash<_ActualDuration>{}(__sd);
3614 }();
3615
3616 const auto __izone = [&] {
3617 const _TimeZonePtr __tz = __val.get_time_zone();
3618 if constexpr (is_same_v<_TimeZonePtr, const chrono::time_zone*>)
3619 return reinterpret_cast<uintptr_t>(__tz);
3620 else
3621 return hash<_TimeZonePtr>{}(__tz);
3622 }();
3623
3624 return chrono::__int_hash(__iduration, __izone);
3625 }
3626 };
3627
3628 template<typename _Duration, typename _TimeZonePtr>
3629 struct __is_fast_hash<hash<chrono::zoned_time<_Duration, _TimeZonePtr>>>
3630 : __and_<__is_fast_hash<hash<
3631 typename chrono::zoned_time<_Duration, _TimeZonePtr>::duration>>,
3632 __is_fast_hash<hash<_TimeZonePtr>>>
3633 {};
3634
3635 // leap_second
3636 template<>
3637 struct hash<chrono::leap_second>
3638 {
3639 size_t
3640 operator()(chrono::leap_second __val) const noexcept
3641 {
3642 return chrono::__int_hash(
3643 __val.date().time_since_epoch().count(),
3644 __val.value().count());
3645 }
3646 };
3647#endif // _GLIBCXX_USE_CXX11_ABI || ! _GLIBCXX_USE_DUAL_ABI
3648#endif // _GLIBCXX_HOSTED
3649#endif // __glibcxx_chrono_cxx20 >= 202306
3650
3651#ifdef __glibcxx_chrono_cxx20
3652 inline namespace literals
3653 {
3654 inline namespace chrono_literals
3655 {
3656 /// @addtogroup chrono
3657 /// @{
3658#pragma GCC diagnostic push
3659#pragma GCC diagnostic ignored "-Wliteral-suffix"
3660 /// Literal suffix for creating chrono::day objects.
3661 /// @since C++20
3662 constexpr chrono::day
3663 operator""d(unsigned long long __d) noexcept
3664 { return chrono::day{static_cast<unsigned>(__d)}; }
3665
3666 /// Literal suffix for creating chrono::year objects.
3667 /// @since C++20
3668 constexpr chrono::year
3669 operator""y(unsigned long long __y) noexcept
3670 { return chrono::year{static_cast<int>(__y)}; }
3671#pragma GCC diagnostic pop
3672 /// @}
3673 } // inline namespace chrono_literals
3674 } // inline namespace literals
3675#endif // C++20
3676
3677_GLIBCXX_END_NAMESPACE_VERSION
3678} // namespace std
3679
3680#if defined __glibcxx_chrono_cxx20 && _GLIBCXX_HOSTED
3681# include <bits/chrono_io.h>
3682#endif
3683
3684#endif // C++11
3685
3686#endif //_GLIBCXX_CHRONO
constexpr __enable_if_is_duration< _ToDur > floor(const duration< _Rep, _Period > &__d)
Definition chrono.h:392
duration< int64_t, ratio< 2629746 > > months
months
Definition chrono.h:920
duration< int64_t, ratio< 86400 > > days
days
Definition chrono.h:911
duration< int64_t, ratio< 31556952 > > years
years
Definition chrono.h:917
constexpr bool operator<=(const duration< _Rep1, _Period1 > &__lhs, const duration< _Rep2, _Period2 > &__rhs)
Definition chrono.h:859
duration< int64_t, ratio< 3600 > > hours
hours
Definition chrono.h:907
constexpr bool operator>=(const duration< _Rep1, _Period1 > &__lhs, const duration< _Rep2, _Period2 > &__rhs)
Definition chrono.h:873
auto clock_cast(const time_point< _SourceClock, _Duration > &__t)
Convert a time point to a different clock.
Definition chrono:419
constexpr enable_if_t< numeric_limits< _Rep >::is_signed, duration< _Rep, _Period > > abs(duration< _Rep, _Period > __d)
Definition chrono.h:463
duration< int64_t, ratio< 60 > > minutes
minutes
Definition chrono.h:904
constexpr time_point< _Clock, typename common_type< duration< _Rep1, _Period1 >, _Dur2 >::type > operator+(const duration< _Rep1, _Period1 > &__lhs, const time_point< _Clock, _Dur2 > &__rhs)
Adjust a time point forwards by the given duration.
Definition chrono.h:1119
constexpr bool operator<(const duration< _Rep1, _Period1 > &__lhs, const duration< _Rep2, _Period2 > &__rhs)
Definition chrono.h:826
duration< int64_t > seconds
seconds
Definition chrono.h:901
constexpr bool operator>(const duration< _Rep1, _Period1 > &__lhs, const duration< _Rep2, _Period2 > &__rhs)
Definition chrono.h:866
constexpr common_type< duration< _Rep1, _Period1 >, duration< _Rep2, _Period2 > >::type operator-(const duration< _Rep1, _Period1 > &__lhs, const duration< _Rep2, _Period2 > &__rhs)
The difference between two durations.
Definition chrono.h:714
constexpr duration< __common_rep_t< _Rep1, __disable_if_is_duration< _Rep2 > >, _Period > operator/(const duration< _Rep1, _Period > &__d, const _Rep2 &__s)
Definition chrono.h:760
constexpr __enable_if_is_duration< _ToDur > duration_cast(const duration< _Rep, _Period > &__d)
Definition chrono.h:279
constexpr complex< _Tp > operator-(const complex< _Tp > &__x, const complex< _Tp > &__y)
Return new complex value x minus y.
Definition complex:404
constexpr complex< _Tp > operator+(const complex< _Tp > &__x, const complex< _Tp > &__y)
Return new complex value x plus y.
Definition complex:374
constexpr complex< _Tp > operator/(const complex< _Tp > &__x, const complex< _Tp > &__y)
Return new complex value x divided by y.
Definition complex:464
basic_ostringstream< char > ostringstream
Class for char output memory streams.
Definition iosfwd:157
__bool_constant< true > true_type
The type used as a compile-time boolean with true value.
Definition type_traits:119
typename common_type< _Tp... >::type common_type_t
Alias template for common_type.
Definition type_traits:2948
typename make_unsigned< _Tp >::type make_unsigned_t
Alias template for make_unsigned.
Definition type_traits:2246
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
Definition move.h:138
_Tp * end(valarray< _Tp > &__va) noexcept
Return an iterator pointing to one past the last element of the valarray.
Definition valarray:1251
_Tp * begin(valarray< _Tp > &__va) noexcept
Return an iterator pointing to the first element of the valarray.
Definition valarray:1229
ISO C++ entities toplevel namespace is std.
constexpr auto cend(const _Container &__cont) noexcept(noexcept(std::end(__cont))) -> decltype(std::end(__cont))
Return an iterator pointing to one past the last element of the const container.
constexpr auto size(const _Container &__cont) noexcept(noexcept(__cont.size())) -> decltype(__cont.size())
Return the size of a container.
constexpr auto cbegin(const _Container &__cont) noexcept(noexcept(std::begin(__cont))) -> decltype(std::begin(__cont))
Return an iterator pointing to the first element of the const container.
ISO C++ inline namespace for literal suffixes.
ISO C++ 2011 namespace for date and time utilities.
const tzdb & front() const noexcept
const_iterator erase_after(const_iterator __p)
static constexpr bool is_signed
Definition limits:230
Provides compile-time rational arithmetic.
Definition ratio:272
Primary class template hash.
is_unsigned
Definition type_traits:1062
chrono::duration represents a distance between two points in time
Definition chrono.h:516
chrono::time_point represents a point in time as measured by a clock
Definition chrono.h:927
A smart pointer with reference-counted copy semantics.
Forward iterators support a superset of input iterator operations.
constexpr iterator begin() noexcept
Definition stl_vector.h:988
[concept.same], concept same_as
Definition concepts:65