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