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