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