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