blob: 0691101010fbfb5fa206e59a0c319452752bc5c3 [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001// -*- C++ -*-
2//===---------------------------- chrono ----------------------------------===//
3//
Howard Hinnantc566dc32010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnantc51e1022010-05-11 19:42:16 +00005//
Howard Hinnantee11c312010-11-16 22:09:02 +00006// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantc51e1022010-05-11 19:42:16 +00008//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP_CHRONO
12#define _LIBCPP_CHRONO
13
14/*
15 chrono synopsis
16
17namespace std
18{
19namespace chrono
20{
21
22template <class ToDuration, class Rep, class Period>
Howard Hinnantcf3143c2012-07-13 19:17:27 +000023constexpr
Howard Hinnantc51e1022010-05-11 19:42:16 +000024ToDuration
25duration_cast(const duration<Rep, Period>& fd);
26
27template <class Rep> struct treat_as_floating_point : is_floating_point<Rep> {};
28
Marshall Clowf1bf62f2018-01-02 17:17:01 +000029template <class Rep> inline constexpr bool treat_as_floating_point_v
Marshall Clow59581f02015-11-30 05:39:30 +000030 = treat_as_floating_point<Rep>::value; // C++17
31
Howard Hinnantc51e1022010-05-11 19:42:16 +000032template <class Rep>
33struct duration_values
34{
35public:
Howard Hinnantcf3143c2012-07-13 19:17:27 +000036 static constexpr Rep zero();
37 static constexpr Rep max();
38 static constexpr Rep min();
Howard Hinnantc51e1022010-05-11 19:42:16 +000039};
40
41// duration
42
43template <class Rep, class Period = ratio<1>>
44class duration
45{
46 static_assert(!__is_duration<Rep>::value, "A duration representation can not be a duration");
47 static_assert(__is_ratio<Period>::value, "Second template parameter of duration must be a std::ratio");
48 static_assert(Period::num > 0, "duration period must be positive");
49public:
50 typedef Rep rep;
Marshall Clow76200b22017-03-21 18:38:57 +000051 typedef typename _Period::type period;
Howard Hinnantc51e1022010-05-11 19:42:16 +000052
Howard Hinnantcf3143c2012-07-13 19:17:27 +000053 constexpr duration() = default;
Howard Hinnantc51e1022010-05-11 19:42:16 +000054 template <class Rep2>
Howard Hinnantcf3143c2012-07-13 19:17:27 +000055 constexpr explicit duration(const Rep2& r,
Howard Hinnantc51e1022010-05-11 19:42:16 +000056 typename enable_if
57 <
58 is_convertible<Rep2, rep>::value &&
59 (treat_as_floating_point<rep>::value ||
60 !treat_as_floating_point<rep>::value && !treat_as_floating_point<Rep2>::value)
61 >::type* = 0);
62
63 // conversions
64 template <class Rep2, class Period2>
Howard Hinnantcf3143c2012-07-13 19:17:27 +000065 constexpr duration(const duration<Rep2, Period2>& d,
Howard Hinnantc51e1022010-05-11 19:42:16 +000066 typename enable_if
67 <
68 treat_as_floating_point<rep>::value ||
69 ratio_divide<Period2, period>::type::den == 1
70 >::type* = 0);
71
72 // observer
73
Howard Hinnantcf3143c2012-07-13 19:17:27 +000074 constexpr rep count() const;
Howard Hinnantc51e1022010-05-11 19:42:16 +000075
76 // arithmetic
77
Marshall Clow76200b22017-03-21 18:38:57 +000078 constexpr common_type<duration>::type operator+() const;
79 constexpr common_type<duration>::type operator-() const;
Marshall Clowdf247eb2018-08-29 23:02:15 +000080 constexpr duration& operator++(); // constexpr in C++17
81 constexpr duration operator++(int); // constexpr in C++17
82 constexpr duration& operator--(); // constexpr in C++17
83 constexpr duration operator--(int); // constexpr in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +000084
Marshall Clowdf247eb2018-08-29 23:02:15 +000085 constexpr duration& operator+=(const duration& d); // constexpr in C++17
86 constexpr duration& operator-=(const duration& d); // constexpr in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +000087
Marshall Clowdf247eb2018-08-29 23:02:15 +000088 duration& operator*=(const rep& rhs); // constexpr in C++17
89 duration& operator/=(const rep& rhs); // constexpr in C++17
90 duration& operator%=(const rep& rhs); // constexpr in C++17
91 duration& operator%=(const duration& rhs); // constexpr in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +000092
93 // special values
94
Howard Hinnantcf3143c2012-07-13 19:17:27 +000095 static constexpr duration zero();
96 static constexpr duration min();
97 static constexpr duration max();
Howard Hinnantc51e1022010-05-11 19:42:16 +000098};
99
100typedef duration<long long, nano> nanoseconds;
101typedef duration<long long, micro> microseconds;
102typedef duration<long long, milli> milliseconds;
103typedef duration<long long > seconds;
104typedef duration< long, ratio< 60> > minutes;
105typedef duration< long, ratio<3600> > hours;
106
107template <class Clock, class Duration = typename Clock::duration>
108class time_point
109{
110public:
111 typedef Clock clock;
112 typedef Duration duration;
113 typedef typename duration::rep rep;
114 typedef typename duration::period period;
115private:
116 duration d_; // exposition only
117
118public:
Marshall Clow5c459c92013-07-31 19:32:19 +0000119 time_point(); // has value "epoch" // constexpr in C++14
120 explicit time_point(const duration& d); // same as time_point() + d // constexpr in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000121
122 // conversions
123 template <class Duration2>
Marshall Clow5c459c92013-07-31 19:32:19 +0000124 time_point(const time_point<clock, Duration2>& t); // constexpr in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000125
126 // observer
127
Marshall Clow5c459c92013-07-31 19:32:19 +0000128 duration time_since_epoch() const; // constexpr in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000129
130 // arithmetic
131
Marshall Clowdf247eb2018-08-29 23:02:15 +0000132 time_point& operator+=(const duration& d); // constexpr in C++17
133 time_point& operator-=(const duration& d); // constexpr in C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000134
135 // special values
136
137 static constexpr time_point min();
138 static constexpr time_point max();
139};
140
141} // chrono
142
143// common_type traits
144template <class Rep1, class Period1, class Rep2, class Period2>
145 struct common_type<chrono::duration<Rep1, Period1>, chrono::duration<Rep2, Period2>>;
146
147template <class Clock, class Duration1, class Duration2>
148 struct common_type<chrono::time_point<Clock, Duration1>, chrono::time_point<Clock, Duration2>>;
149
150namespace chrono {
151
Marshall Clow40ca0c12018-07-18 17:37:51 +0000152
153template<class T> struct is_clock; // C++20
154template<class T> inline constexpr bool is_clock_v = is_clock<T>::value; // C++20
155
156
Howard Hinnantc51e1022010-05-11 19:42:16 +0000157// duration arithmetic
158template <class Rep1, class Period1, class Rep2, class Period2>
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000159 constexpr
Howard Hinnantc51e1022010-05-11 19:42:16 +0000160 typename common_type<duration<Rep1, Period1>, duration<Rep2, Period2>>::type
161 operator+(const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs);
162template <class Rep1, class Period1, class Rep2, class Period2>
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000163 constexpr
Howard Hinnantc51e1022010-05-11 19:42:16 +0000164 typename common_type<duration<Rep1, Period1>, duration<Rep2, Period2>>::type
165 operator-(const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs);
166template <class Rep1, class Period, class Rep2>
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000167 constexpr
Howard Hinnantc51e1022010-05-11 19:42:16 +0000168 duration<typename common_type<Rep1, Rep2>::type, Period>
169 operator*(const duration<Rep1, Period>& d, const Rep2& s);
170template <class Rep1, class Period, class Rep2>
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000171 constexpr
Howard Hinnantc51e1022010-05-11 19:42:16 +0000172 duration<typename common_type<Rep1, Rep2>::type, Period>
173 operator*(const Rep1& s, const duration<Rep2, Period>& d);
174template <class Rep1, class Period, class Rep2>
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000175 constexpr
Howard Hinnantc51e1022010-05-11 19:42:16 +0000176 duration<typename common_type<Rep1, Rep2>::type, Period>
177 operator/(const duration<Rep1, Period>& d, const Rep2& s);
178template <class Rep1, class Period1, class Rep2, class Period2>
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000179 constexpr
Howard Hinnantc51e1022010-05-11 19:42:16 +0000180 typename common_type<Rep1, Rep2>::type
181 operator/(const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs);
182
183// duration comparisons
184template <class Rep1, class Period1, class Rep2, class Period2>
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000185 constexpr
Howard Hinnantc51e1022010-05-11 19:42:16 +0000186 bool operator==(const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs);
187template <class Rep1, class Period1, class Rep2, class Period2>
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000188 constexpr
Howard Hinnantc51e1022010-05-11 19:42:16 +0000189 bool operator!=(const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs);
190template <class Rep1, class Period1, class Rep2, class Period2>
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000191 constexpr
Howard Hinnantc51e1022010-05-11 19:42:16 +0000192 bool operator< (const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs);
193template <class Rep1, class Period1, class Rep2, class Period2>
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000194 constexpr
Howard Hinnantc51e1022010-05-11 19:42:16 +0000195 bool operator<=(const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs);
196template <class Rep1, class Period1, class Rep2, class Period2>
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000197 constexpr
Howard Hinnantc51e1022010-05-11 19:42:16 +0000198 bool operator> (const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs);
199template <class Rep1, class Period1, class Rep2, class Period2>
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000200 constexpr
Howard Hinnantc51e1022010-05-11 19:42:16 +0000201 bool operator>=(const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs);
202
203// duration_cast
204template <class ToDuration, class Rep, class Period>
205 ToDuration duration_cast(const duration<Rep, Period>& d);
206
Marshall Clowf2eabaf2015-11-05 19:33:59 +0000207template <class ToDuration, class Rep, class Period>
208 constexpr ToDuration floor(const duration<Rep, Period>& d); // C++17
209template <class ToDuration, class Rep, class Period>
210 constexpr ToDuration ceil(const duration<Rep, Period>& d); // C++17
211template <class ToDuration, class Rep, class Period>
212 constexpr ToDuration round(const duration<Rep, Period>& d); // C++17
213
Marshall Clow40ca0c12018-07-18 17:37:51 +0000214// duration I/O is elsewhere
215
Marshall Clow5c459c92013-07-31 19:32:19 +0000216// time_point arithmetic (all constexpr in C++14)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000217template <class Clock, class Duration1, class Rep2, class Period2>
218 time_point<Clock, typename common_type<Duration1, duration<Rep2, Period2>>::type>
219 operator+(const time_point<Clock, Duration1>& lhs, const duration<Rep2, Period2>& rhs);
220template <class Rep1, class Period1, class Clock, class Duration2>
221 time_point<Clock, typename common_type<duration<Rep1, Period1>, Duration2>::type>
222 operator+(const duration<Rep1, Period1>& lhs, const time_point<Clock, Duration2>& rhs);
223template <class Clock, class Duration1, class Rep2, class Period2>
224 time_point<Clock, typename common_type<Duration1, duration<Rep2, Period2>>::type>
225 operator-(const time_point<Clock, Duration1>& lhs, const duration<Rep2, Period2>& rhs);
226template <class Clock, class Duration1, class Duration2>
227 typename common_type<Duration1, Duration2>::type
228 operator-(const time_point<Clock, Duration1>& lhs, const time_point<Clock, Duration2>& rhs);
229
Marshall Clow5c459c92013-07-31 19:32:19 +0000230// time_point comparisons (all constexpr in C++14)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000231template <class Clock, class Duration1, class Duration2>
232 bool operator==(const time_point<Clock, Duration1>& lhs, const time_point<Clock, Duration2>& rhs);
233template <class Clock, class Duration1, class Duration2>
234 bool operator!=(const time_point<Clock, Duration1>& lhs, const time_point<Clock, Duration2>& rhs);
235template <class Clock, class Duration1, class Duration2>
236 bool operator< (const time_point<Clock, Duration1>& lhs, const time_point<Clock, Duration2>& rhs);
237template <class Clock, class Duration1, class Duration2>
238 bool operator<=(const time_point<Clock, Duration1>& lhs, const time_point<Clock, Duration2>& rhs);
239template <class Clock, class Duration1, class Duration2>
240 bool operator> (const time_point<Clock, Duration1>& lhs, const time_point<Clock, Duration2>& rhs);
241template <class Clock, class Duration1, class Duration2>
242 bool operator>=(const time_point<Clock, Duration1>& lhs, const time_point<Clock, Duration2>& rhs);
243
Marshall Clow5c459c92013-07-31 19:32:19 +0000244// time_point_cast (constexpr in C++14)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000245
246template <class ToDuration, class Clock, class Duration>
247 time_point<Clock, ToDuration> time_point_cast(const time_point<Clock, Duration>& t);
248
Marshall Clowf2eabaf2015-11-05 19:33:59 +0000249template <class ToDuration, class Clock, class Duration>
250 constexpr time_point<Clock, ToDuration>
251 floor(const time_point<Clock, Duration>& tp); // C++17
252
253template <class ToDuration, class Clock, class Duration>
254 constexpr time_point<Clock, ToDuration>
255 ceil(const time_point<Clock, Duration>& tp); // C++17
256
257template <class ToDuration, class Clock, class Duration>
258 constexpr time_point<Clock, ToDuration>
259 round(const time_point<Clock, Duration>& tp); // C++17
260
261template <class Rep, class Period>
262 constexpr duration<Rep, Period> abs(duration<Rep, Period> d); // C++17
Marshall Clow40ca0c12018-07-18 17:37:51 +0000263
Howard Hinnantc51e1022010-05-11 19:42:16 +0000264// Clocks
265
266class system_clock
267{
268public:
269 typedef microseconds duration;
270 typedef duration::rep rep;
271 typedef duration::period period;
272 typedef chrono::time_point<system_clock> time_point;
Marshall Clow5c459c92013-07-31 19:32:19 +0000273 static const bool is_steady = false; // constexpr in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000274
Howard Hinnantaa54ac42011-05-28 18:34:36 +0000275 static time_point now() noexcept;
276 static time_t to_time_t (const time_point& __t) noexcept;
277 static time_point from_time_t(time_t __t) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000278};
279
Marshall Clow40ca0c12018-07-18 17:37:51 +0000280template <class Duration>
281 using sys_time = time_point<system_clock, Duration>; // C++20
282using sys_seconds = sys_time<seconds>; // C++20
283using sys_days = sys_time<days>; // C++20
284
285class utc_clock; // C++20
286
287template <class Duration>
288 using utc_time = time_point<utc_clock, Duration>; // C++20
289using utc_seconds = utc_time<seconds>; // C++20
290
291class tai_clock; // C++20
292
293template <class Duration>
294 using tai_time = time_point<tai_clock, Duration>; // C++20
295using tai_seconds = tai_time<seconds>; // C++20
296
297class file_clock; // C++20
298
299template<class Duration>
300 using file_time = time_point<file_clock, Duration>; // C++20
301
Howard Hinnantc8dbd222010-11-20 19:16:30 +0000302class steady_clock
Howard Hinnantc51e1022010-05-11 19:42:16 +0000303{
304public:
305 typedef nanoseconds duration;
306 typedef duration::rep rep;
307 typedef duration::period period;
Howard Hinnantc8dbd222010-11-20 19:16:30 +0000308 typedef chrono::time_point<steady_clock, duration> time_point;
Marshall Clow5c459c92013-07-31 19:32:19 +0000309 static const bool is_steady = true; // constexpr in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000310
Howard Hinnantaa54ac42011-05-28 18:34:36 +0000311 static time_point now() noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000312};
313
Howard Hinnantc8dbd222010-11-20 19:16:30 +0000314typedef steady_clock high_resolution_clock;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000315
Marshall Clow40ca0c12018-07-18 17:37:51 +0000316// 25.7.8, local time // C++20
317struct local_t {};
318template<class Duration>
319 using local_time = time_point<local_t, Duration>;
320using local_seconds = local_time<seconds>;
321using local_days = local_time<days>;
322
323// 25.7.9, time_point conversions template<class DestClock, class SourceClock> // C++20
324struct clock_time_conversion;
325
326template<class DestClock, class SourceClock, class Duration>
327 auto clock_cast(const time_point<SourceClock, Duration>& t);
Louis Dionne44bcff92018-08-03 22:36:53 +0000328
Marshall Clow40ca0c12018-07-18 17:37:51 +0000329// 25.8.2, class last_spec // C++20
330struct last_spec;
331
332// 25.8.3, class day // C++20
333
334class day;
335constexpr bool operator==(const day& x, const day& y) noexcept;
336constexpr bool operator!=(const day& x, const day& y) noexcept;
337constexpr bool operator< (const day& x, const day& y) noexcept;
338constexpr bool operator> (const day& x, const day& y) noexcept;
339constexpr bool operator<=(const day& x, const day& y) noexcept;
340constexpr bool operator>=(const day& x, const day& y) noexcept;
341constexpr day operator+(const day& x, const days& y) noexcept;
342constexpr day operator+(const days& x, const day& y) noexcept;
343constexpr day operator-(const day& x, const days& y) noexcept;
344constexpr days operator-(const day& x, const day& y) noexcept;
345
346// 25.8.4, class month // C++20
347class month;
348constexpr bool operator==(const month& x, const month& y) noexcept;
349constexpr bool operator!=(const month& x, const month& y) noexcept;
350constexpr bool operator< (const month& x, const month& y) noexcept;
351constexpr bool operator> (const month& x, const month& y) noexcept;
352constexpr bool operator<=(const month& x, const month& y) noexcept;
353constexpr bool operator>=(const month& x, const month& y) noexcept;
354constexpr month operator+(const month& x, const months& y) noexcept;
355constexpr month operator+(const months& x, const month& y) noexcept;
356constexpr month operator-(const month& x, const months& y) noexcept;
357constexpr months operator-(const month& x, const month& y) noexcept;
358
359// 25.8.5, class year // C++20
360class year;
361constexpr bool operator==(const year& x, const year& y) noexcept;
362constexpr bool operator!=(const year& x, const year& y) noexcept;
363constexpr bool operator< (const year& x, const year& y) noexcept;
364constexpr bool operator> (const year& x, const year& y) noexcept;
365constexpr bool operator<=(const year& x, const year& y) noexcept;
366constexpr bool operator>=(const year& x, const year& y) noexcept;
367constexpr year operator+(const year& x, const years& y) noexcept;
368constexpr year operator+(const years& x, const year& y) noexcept;
369constexpr year operator-(const year& x, const years& y) noexcept;
370constexpr years operator-(const year& x, const year& y) noexcept;
371
372// 25.8.6, class weekday // C++20
373class weekday;
374
375constexpr bool operator==(const weekday& x, const weekday& y) noexcept;
376constexpr bool operator!=(const weekday& x, const weekday& y) noexcept;
377constexpr weekday operator+(const weekday& x, const days& y) noexcept;
378constexpr weekday operator+(const days& x, const weekday& y) noexcept;
379constexpr weekday operator-(const weekday& x, const days& y) noexcept;
380constexpr days operator-(const weekday& x, const weekday& y) noexcept;
381
382// 25.8.7, class weekday_indexed // C++20
383
384class weekday_indexed;
385constexpr bool operator==(const weekday_indexed& x, const weekday_indexed& y) noexcept;
386constexpr bool operator!=(const weekday_indexed& x, const weekday_indexed& y) noexcept;
387
388// 25.8.8, class weekday_last // C++20
389class weekday_last;
390
391constexpr bool operator==(const weekday_last& x, const weekday_last& y) noexcept;
392constexpr bool operator!=(const weekday_last& x, const weekday_last& y) noexcept;
393
394// 25.8.9, class month_day // C++20
395class month_day;
396
397constexpr bool operator==(const month_day& x, const month_day& y) noexcept;
398constexpr bool operator!=(const month_day& x, const month_day& y) noexcept;
399constexpr bool operator< (const month_day& x, const month_day& y) noexcept;
400constexpr bool operator> (const month_day& x, const month_day& y) noexcept;
401constexpr bool operator<=(const month_day& x, const month_day& y) noexcept;
402constexpr bool operator>=(const month_day& x, const month_day& y) noexcept;
403
404
405// 25.8.10, class month_day_last // C++20
406class month_day_last;
407
408constexpr bool operator==(const month_day_last& x, const month_day_last& y) noexcept;
409constexpr bool operator!=(const month_day_last& x, const month_day_last& y) noexcept;
410constexpr bool operator< (const month_day_last& x, const month_day_last& y) noexcept;
411constexpr bool operator> (const month_day_last& x, const month_day_last& y) noexcept;
412constexpr bool operator<=(const month_day_last& x, const month_day_last& y) noexcept;
413constexpr bool operator>=(const month_day_last& x, const month_day_last& y) noexcept;
414
415// 25.8.11, class month_weekday // C++20
416class month_weekday;
417
418constexpr bool operator==(const month_weekday& x, const month_weekday& y) noexcept;
419constexpr bool operator!=(const month_weekday& x, const month_weekday& y) noexcept;
420
421// 25.8.12, class month_weekday_last // C++20
422class month_weekday_last;
423
424constexpr bool operator==(const month_weekday_last& x, const month_weekday_last& y) noexcept;
425constexpr bool operator!=(const month_weekday_last& x, const month_weekday_last& y) noexcept;
426
427
428// 25.8.13, class year_month // C++20
429class year_month;
430
431constexpr bool operator==(const year_month& x, const year_month& y) noexcept;
432constexpr bool operator!=(const year_month& x, const year_month& y) noexcept;
433constexpr bool operator< (const year_month& x, const year_month& y) noexcept;
434constexpr bool operator> (const year_month& x, const year_month& y) noexcept;
435constexpr bool operator<=(const year_month& x, const year_month& y) noexcept;
436constexpr bool operator>=(const year_month& x, const year_month& y) noexcept;
437
438constexpr year_month operator+(const year_month& ym, const months& dm) noexcept;
439constexpr year_month operator+(const months& dm, const year_month& ym) noexcept;
440constexpr year_month operator-(const year_month& ym, const months& dm) noexcept;
441constexpr months operator-(const year_month& x, const year_month& y) noexcept;
442constexpr year_month operator+(const year_month& ym, const years& dy) noexcept;
443constexpr year_month operator+(const years& dy, const year_month& ym) noexcept;
444constexpr year_month operator-(const year_month& ym, const years& dy) noexcept;
445
446// 25.8.14, class year_month_day class // C++20
447year_month_day;
448
449constexpr bool operator==(const year_month_day& x, const year_month_day& y) noexcept;
450constexpr bool operator!=(const year_month_day& x, const year_month_day& y) noexcept;
451constexpr bool operator< (const year_month_day& x, const year_month_day& y) noexcept;
452constexpr bool operator> (const year_month_day& x, const year_month_day& y) noexcept;
453constexpr bool operator<=(const year_month_day& x, const year_month_day& y) noexcept;
454constexpr bool operator>=(const year_month_day& x, const year_month_day& y) noexcept;
455
456constexpr year_month_day operator+(const year_month_day& ymd, const months& dm) noexcept;
457constexpr year_month_day operator+(const months& dm, const year_month_day& ymd) noexcept;
458constexpr year_month_day operator+(const year_month_day& ymd, const years& dy) noexcept;
459constexpr year_month_day operator+(const years& dy, const year_month_day& ymd) noexcept;
460constexpr year_month_day operator-(const year_month_day& ymd, const months& dm) noexcept;
461constexpr year_month_day operator-(const year_month_day& ymd, const years& dy) noexcept;
462
463
464// 25.8.15, class year_month_day_last // C++20
465class year_month_day_last;
466
467constexpr bool operator==(const year_month_day_last& x,
468 const year_month_day_last& y) noexcept;
469constexpr bool operator!=(const year_month_day_last& x,
470 const year_month_day_last& y) noexcept;
471constexpr bool operator< (const year_month_day_last& x,
472 const year_month_day_last& y) noexcept;
473constexpr bool operator> (const year_month_day_last& x,
474 const year_month_day_last& y) noexcept;
475constexpr bool operator<=(const year_month_day_last& x,
476 const year_month_day_last& y) noexcept;
477constexpr bool operator>=(const year_month_day_last& x,
478 const year_month_day_last& y) noexcept;
479
480constexpr year_month_day_last
481 operator+(const year_month_day_last& ymdl, const months& dm) noexcept;
482constexpr year_month_day_last
483 operator+(const months& dm, const year_month_day_last& ymdl) noexcept;
484constexpr year_month_day_last
485 operator+(const year_month_day_last& ymdl, const years& dy) noexcept;
486constexpr year_month_day_last
487 operator+(const years& dy, const year_month_day_last& ymdl) noexcept;
488constexpr year_month_day_last
489 operator-(const year_month_day_last& ymdl, const months& dm) noexcept;
490constexpr year_month_day_last
491 operator-(const year_month_day_last& ymdl, const years& dy) noexcept;
492
493// 25.8.16, class year_month_weekday // C++20
494class year_month_weekday;
495
496constexpr bool operator==(const year_month_weekday& x,
497 const year_month_weekday& y) noexcept;
498constexpr bool operator!=(const year_month_weekday& x,
499 const year_month_weekday& y) noexcept;
500
501constexpr year_month_weekday
502 operator+(const year_month_weekday& ymwd, const months& dm) noexcept;
503constexpr year_month_weekday
504 operator+(const months& dm, const year_month_weekday& ymwd) noexcept;
505constexpr year_month_weekday
506 operator+(const year_month_weekday& ymwd, const years& dy) noexcept;
507constexpr year_month_weekday
508 operator+(const years& dy, const year_month_weekday& ymwd) noexcept;
509constexpr year_month_weekday
510 operator-(const year_month_weekday& ymwd, const months& dm) noexcept;
511constexpr year_month_weekday
512 operator-(const year_month_weekday& ymwd, const years& dy) noexcept;
513
514// 25.8.17, class year_month_weekday_last // C++20
515class year_month_weekday_last;
516
517constexpr bool operator==(const year_month_weekday_last& x,
518 const year_month_weekday_last& y) noexcept;
519constexpr bool operator!=(const year_month_weekday_last& x,
520 const year_month_weekday_last& y) noexcept;
521constexpr year_month_weekday_last
522 operator+(const year_month_weekday_last& ymwdl, const months& dm) noexcept;
523constexpr year_month_weekday_last
524 operator+(const months& dm, const year_month_weekday_last& ymwdl) noexcept;
525constexpr year_month_weekday_last
526 operator+(const year_month_weekday_last& ymwdl, const years& dy) noexcept;
527constexpr year_month_weekday_last
528 operator+(const years& dy, const year_month_weekday_last& ymwdl) noexcept;
529constexpr year_month_weekday_last
530 operator-(const year_month_weekday_last& ymwdl, const months& dm) noexcept;
531constexpr year_month_weekday_last
532 operator-(const year_month_weekday_last& ymwdl, const years& dy) noexcept;
Louis Dionne44bcff92018-08-03 22:36:53 +0000533
Marshall Clow40ca0c12018-07-18 17:37:51 +0000534// 25.8.18, civil calendar conventional syntax operators // C++20
535constexpr year_month
536 operator/(const year& y, const month& m) noexcept;
537constexpr year_month
538 operator/(const year& y, int m) noexcept;
539constexpr month_day
540 operator/(const month& m, const day& d) noexcept;
541constexpr month_day
542 operator/(const month& m, int d) noexcept;
543constexpr month_day
544 operator/(int m, const day& d) noexcept;
545constexpr month_day
546 operator/(const day& d, const month& m) noexcept;
547constexpr month_day
548 operator/(const day& d, int m) noexcept;
549constexpr month_day_last
550 operator/(const month& m, last_spec) noexcept;
551constexpr month_day_last
552 operator/(int m, last_spec) noexcept;
553constexpr month_day_last
554 operator/(last_spec, const month& m) noexcept;
555constexpr month_day_last
556 operator/(last_spec, int m) noexcept;
557constexpr month_weekday
558 operator/(const month& m, const weekday_indexed& wdi) noexcept;
559constexpr month_weekday
560 operator/(int m, const weekday_indexed& wdi) noexcept;
561constexpr month_weekday
562 operator/(const weekday_indexed& wdi, const month& m) noexcept;
563constexpr month_weekday
564 operator/(const weekday_indexed& wdi, int m) noexcept;
565constexpr month_weekday_last
566 operator/(const month& m, const weekday_last& wdl) noexcept;
567constexpr month_weekday_last
568 operator/(int m, const weekday_last& wdl) noexcept;
569constexpr month_weekday_last
570 operator/(const weekday_last& wdl, const month& m) noexcept;
571constexpr month_weekday_last
572 operator/(const weekday_last& wdl, int m) noexcept;
573constexpr year_month_day
574 operator/(const year_month& ym, const day& d) noexcept;
575constexpr year_month_day
576 operator/(const year_month& ym, int d) noexcept;
577constexpr year_month_day
578 operator/(const year& y, const month_day& md) noexcept;
579constexpr year_month_day
580 operator/(int y, const month_day& md) noexcept;
581constexpr year_month_day
582 operator/(const month_day& md, const year& y) noexcept;
583constexpr year_month_day
584 operator/(const month_day& md, int y) noexcept;
585constexpr year_month_day_last
586 operator/(const year_month& ym, last_spec) noexcept;
587constexpr year_month_day_last
588 operator/(const year& y, const month_day_last& mdl) noexcept;
589constexpr year_month_day_last
590 operator/(int y, const month_day_last& mdl) noexcept;
591constexpr year_month_day_last
592 operator/(const month_day_last& mdl, const year& y) noexcept;
593constexpr year_month_day_last
594 operator/(const month_day_last& mdl, int y) noexcept;
595constexpr year_month_weekday
596 operator/(const year_month& ym, const weekday_indexed& wdi) noexcept;
597constexpr year_month_weekday
598 operator/(const year& y, const month_weekday& mwd) noexcept;
599constexpr year_month_weekday
600 operator/(int y, const month_weekday& mwd) noexcept;
601constexpr year_month_weekday
602 operator/(const month_weekday& mwd, const year& y) noexcept;
603constexpr year_month_weekday
604 operator/(const month_weekday& mwd, int y) noexcept;
605constexpr year_month_weekday_last
606 operator/(const year_month& ym, const weekday_last& wdl) noexcept;
607constexpr year_month_weekday_last
608 operator/(const year& y, const month_weekday_last& mwdl) noexcept;
609constexpr year_month_weekday_last
610 operator/(int y, const month_weekday_last& mwdl) noexcept;
611constexpr year_month_weekday_last
612 operator/(const month_weekday_last& mwdl, const year& y) noexcept;
613constexpr year_month_weekday_last
Louis Dionne44bcff92018-08-03 22:36:53 +0000614 operator/(const month_weekday_last& mwdl, int y) noexcept;
Marshall Clow40ca0c12018-07-18 17:37:51 +0000615
616// 25.9, class template time_of_day // C++20
617template<class Duration> class time_of_day;
618
619template<> class time_of_day<hours>;
620template<> class time_of_day<minutes>;
621template<> class time_of_day<seconds>;
622template<class Rep, class Period> class time_of_day<duration<Rep, Period>>;
623
624// 25.10.2, time zone database // C++20
625struct tzdb;
626class tzdb_list;
627
628// 25.10.2.3, time zone database access // C++20
629const tzdb& get_tzdb();
630tzdb_list& get_tzdb_list();
631const time_zone* locate_zone(string_view tz_name);
632const time_zone* current_zone();
633
634// 25.10.2.4, remote time zone database support // C++20
635const tzdb& reload_tzdb();
636string remote_version();
637
638// 25.10.3, exception classes // C++20
639class nonexistent_local_time;
640class ambiguous_local_time;
641
642// 25.10.4, information classes // C++20
643struct sys_info;
644struct local_info;
Louis Dionne44bcff92018-08-03 22:36:53 +0000645
Marshall Clow40ca0c12018-07-18 17:37:51 +0000646// 25.10.5, class time_zone // C++20
647enum class choose {earliest, latest};
648class time_zone;
649bool operator==(const time_zone& x, const time_zone& y) noexcept;
650bool operator!=(const time_zone& x, const time_zone& y) noexcept;
651bool operator<(const time_zone& x, const time_zone& y) noexcept;
652bool operator>(const time_zone& x, const time_zone& y) noexcept;
653bool operator<=(const time_zone& x, const time_zone& y) noexcept;
654bool operator>=(const time_zone& x, const time_zone& y) noexcept;
Louis Dionne44bcff92018-08-03 22:36:53 +0000655
Marshall Clow40ca0c12018-07-18 17:37:51 +0000656// 25.10.6, class template zoned_traits // C++20
657template<class T> struct zoned_traits;
658
659// 25.10.7, class template zoned_time // C++20
660template<class Duration, class TimeZonePtr = const time_zone*> class zoned_time;
661using zoned_seconds = zoned_time<seconds>;
662
663template<class Duration1, class Duration2, class TimeZonePtr>
664 bool operator==(const zoned_time<Duration1, TimeZonePtr>& x,
665 const zoned_time<Duration2, TimeZonePtr>& y);
666template<class Duration1, class Duration2, class TimeZonePtr>
667 bool operator!=(const zoned_time<Duration1, TimeZonePtr>& x,
668 const zoned_time<Duration2, TimeZonePtr>& y);
669
670// 25.10.8, leap second support // C++20
671class leap;
672
673bool operator==(const leap& x, const leap& y);
674bool operator!=(const leap& x, const leap& y);
675bool operator< (const leap& x, const leap& y);
676bool operator> (const leap& x, const leap& y);
677bool operator<=(const leap& x, const leap& y);
678bool operator>=(const leap& x, const leap& y);
679template<class Duration>
680 bool operator==(const leap& x, const sys_time<Duration>& y);
681template<class Duration>
682 bool operator==(const sys_time<Duration>& x, const leap& y);
683template<class Duration>
684 bool operator!=(const leap& x, const sys_time<Duration>& y);
685template<class Duration>
686 bool operator!=(const sys_time<Duration>& x, const leap& y);
687template<class Duration>
688 bool operator< (const leap& x, const sys_time<Duration>& y);
689template<class Duration>
690 bool operator< (const sys_time<Duration>& x, const leap& y);
691template<class Duration>
692 bool operator> (const leap& x, const sys_time<Duration>& y);
693template<class Duration>
694 bool operator> (const sys_time<Duration>& x, const leap& y);
695template<class Duration>
696 bool operator<=(const leap& x, const sys_time<Duration>& y);
697template<class Duration>
698 bool operator<=(const sys_time<Duration>& x, const leap& y);
699template<class Duration>
700 bool operator>=(const leap& x, const sys_time<Duration>& y);
701template<class Duration>
702 bool operator>=(const sys_time<Duration>& x, const leap& y);
703
704// 25.10.9, class link // C++20
705class link;
706bool operator==(const link& x, const link& y);
707bool operator!=(const link& x, const link& y);
708bool operator< (const link& x, const link& y);
709bool operator> (const link& x, const link& y);
710bool operator<=(const link& x, const link& y);
711bool operator>=(const link& x, const link& y);
712
713// 25.11, formatting // C++20
714template<class charT, class Streamable>
715 basic_string<charT>
716 format(const charT* fmt, const Streamable& s);
717
718template<class charT, class Streamable>
719 basic_string<charT>
720 format(const locale& loc, const charT* fmt, const Streamable& s);
721
722template<class charT, class traits, class Alloc, class Streamable>
723 basic_string<charT, traits, Alloc>
724 format(const basic_string<charT, traits, Alloc>& fmt, const Streamable& s);
725
726template<class charT, class traits, class Alloc, class Streamable>
727 basic_string<charT, traits, Alloc>
728 format(const locale& loc, const basic_string<charT, traits, Alloc>& fmt,
Louis Dionne44bcff92018-08-03 22:36:53 +0000729 const Streamable& s);
Marshall Clow40ca0c12018-07-18 17:37:51 +0000730
731// 25.12, parsing // C++20
732template<class charT, class traits, class Alloc, class Parsable>
733unspecified
734 parse(const basic_string<charT, traits, Alloc>& format, Parsable& tp);
735
736template<class charT, class traits, class Alloc, class Parsable>
737unspecified
738 parse(const basic_string<charT, traits, Alloc>& format, Parsable& tp,
739 basic_string<charT, traits, Alloc>& abbrev);
740
741template<class charT, class traits, class Alloc, class Parsable>
742unspecified
743 parse(const basic_string<charT, traits, Alloc>& format, Parsable& tp,
744 minutes& offset);
745
746template<class charT, class traits, class Alloc, class Parsable>
747unspecified
748 parse(const basic_string<charT, traits, Alloc>& format, Parsable& tp,
749 basic_string<charT, traits, Alloc>& abbrev, minutes& offset);
750
Louis Dionne44bcff92018-08-03 22:36:53 +0000751inline constexpr last_spec last{}; // C++20
Marshall Clow40ca0c12018-07-18 17:37:51 +0000752inline constexpr chrono::weekday Sunday{0}; // C++20
753inline constexpr chrono::weekday Monday{1}; // C++20
754inline constexpr chrono::weekday Tuesday{2}; // C++20
755inline constexpr chrono::weekday Wednesday{3}; // C++20
756inline constexpr chrono::weekday Thursday{4}; // C++20
757inline constexpr chrono::weekday Friday{5}; // C++20
758inline constexpr chrono::weekday Saturday{6}; // C++20
759
760inline constexpr chrono::month January{1}; // C++20
761inline constexpr chrono::month February{2}; // C++20
762inline constexpr chrono::month March{3}; // C++20
763inline constexpr chrono::month April{4}; // C++20
764inline constexpr chrono::month May{5}; // C++20
765inline constexpr chrono::month June{6}; // C++20
766inline constexpr chrono::month July{7}; // C++20
767inline constexpr chrono::month August{8}; // C++20
768inline constexpr chrono::month September{9}; // C++20
769inline constexpr chrono::month October{10}; // C++20
770inline constexpr chrono::month November{11}; // C++20
771inline constexpr chrono::month December{12}; // C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000772} // chrono
773
Marshall Clow40ca0c12018-07-18 17:37:51 +0000774inline namespace literals {
775 inline namespace chrono_literals {
Marshall Clow2ac805a2017-08-09 15:42:50 +0000776constexpr chrono::hours operator ""h(unsigned long long); // C++14
777constexpr chrono::duration<unspecified , ratio<3600,1>> operator ""h(long double); // C++14
778constexpr chrono::minutes operator ""min(unsigned long long); // C++14
779constexpr chrono::duration<unspecified , ratio<60,1>> operator ""min(long double); // C++14
780constexpr chrono::seconds operator ""s(unsigned long long); // C++14
781constexpr chrono::duration<unspecified > operator ""s(long double); // C++14
782constexpr chrono::milliseconds operator ""ms(unsigned long long); // C++14
783constexpr chrono::duration<unspecified , milli> operator ""ms(long double); // C++14
784constexpr chrono::microseconds operator ""us(unsigned long long); // C++14
785constexpr chrono::duration<unspecified , micro> operator ""us(long double); // C++14
786constexpr chrono::nanoseconds operator ""ns(unsigned long long); // C++14
787constexpr chrono::duration<unspecified , nano> operator ""ns(long double); // C++14
Marshall Clow40ca0c12018-07-18 17:37:51 +0000788constexpr chrono::day operator ""d(unsigned long long d) noexcept; // C++20
789constexpr chrono::year operator ""y(unsigned long long y) noexcept; // C++20
790} // chrono_literals
791} // literals
Marshall Cloweb41e5c2013-07-24 21:18:14 +0000792
Howard Hinnantc51e1022010-05-11 19:42:16 +0000793} // std
794*/
795
796#include <__config>
797#include <ctime>
798#include <type_traits>
799#include <ratio>
800#include <limits>
Marshall Clow0a1e7502018-09-12 19:41:40 +0000801#include <version>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000802
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000803#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000804#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000805#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000806
Eric Fiselierf4433a32017-05-31 22:07:49 +0000807_LIBCPP_PUSH_MACROS
808#include <__undef_macros>
809
810
Howard Hinnantc51e1022010-05-11 19:42:16 +0000811_LIBCPP_BEGIN_NAMESPACE_STD
812
813namespace chrono
814{
815
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000816template <class _Rep, class _Period = ratio<1> > class _LIBCPP_TEMPLATE_VIS duration;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000817
Howard Hinnantf39463b2010-05-18 17:32:30 +0000818template <class _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000819struct __is_duration : false_type {};
820
821template <class _Rep, class _Period>
822struct __is_duration<duration<_Rep, _Period> > : true_type {};
823
824template <class _Rep, class _Period>
825struct __is_duration<const duration<_Rep, _Period> > : true_type {};
826
827template <class _Rep, class _Period>
828struct __is_duration<volatile duration<_Rep, _Period> > : true_type {};
829
830template <class _Rep, class _Period>
831struct __is_duration<const volatile duration<_Rep, _Period> > : true_type {};
832
833} // chrono
834
835template <class _Rep1, class _Period1, class _Rep2, class _Period2>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000836struct _LIBCPP_TEMPLATE_VIS common_type<chrono::duration<_Rep1, _Period1>,
Howard Hinnanta37d3cf2013-08-12 18:38:34 +0000837 chrono::duration<_Rep2, _Period2> >
Howard Hinnantc51e1022010-05-11 19:42:16 +0000838{
839 typedef chrono::duration<typename common_type<_Rep1, _Rep2>::type,
840 typename __ratio_gcd<_Period1, _Period2>::type> type;
841};
842
843namespace chrono {
844
845// duration_cast
846
847template <class _FromDuration, class _ToDuration,
848 class _Period = typename ratio_divide<typename _FromDuration::period, typename _ToDuration::period>::type,
849 bool = _Period::num == 1,
850 bool = _Period::den == 1>
851struct __duration_cast;
852
853template <class _FromDuration, class _ToDuration, class _Period>
854struct __duration_cast<_FromDuration, _ToDuration, _Period, true, true>
855{
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000856 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnantc51e1022010-05-11 19:42:16 +0000857 _ToDuration operator()(const _FromDuration& __fd) const
858 {
859 return _ToDuration(static_cast<typename _ToDuration::rep>(__fd.count()));
860 }
861};
862
863template <class _FromDuration, class _ToDuration, class _Period>
864struct __duration_cast<_FromDuration, _ToDuration, _Period, true, false>
865{
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000866 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnantc51e1022010-05-11 19:42:16 +0000867 _ToDuration operator()(const _FromDuration& __fd) const
868 {
869 typedef typename common_type<typename _ToDuration::rep, typename _FromDuration::rep, intmax_t>::type _Ct;
870 return _ToDuration(static_cast<typename _ToDuration::rep>(
871 static_cast<_Ct>(__fd.count()) / static_cast<_Ct>(_Period::den)));
872 }
873};
874
875template <class _FromDuration, class _ToDuration, class _Period>
876struct __duration_cast<_FromDuration, _ToDuration, _Period, false, true>
877{
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000878 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnantc51e1022010-05-11 19:42:16 +0000879 _ToDuration operator()(const _FromDuration& __fd) const
880 {
881 typedef typename common_type<typename _ToDuration::rep, typename _FromDuration::rep, intmax_t>::type _Ct;
882 return _ToDuration(static_cast<typename _ToDuration::rep>(
883 static_cast<_Ct>(__fd.count()) * static_cast<_Ct>(_Period::num)));
884 }
885};
886
887template <class _FromDuration, class _ToDuration, class _Period>
888struct __duration_cast<_FromDuration, _ToDuration, _Period, false, false>
889{
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000890 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnantc51e1022010-05-11 19:42:16 +0000891 _ToDuration operator()(const _FromDuration& __fd) const
892 {
893 typedef typename common_type<typename _ToDuration::rep, typename _FromDuration::rep, intmax_t>::type _Ct;
894 return _ToDuration(static_cast<typename _ToDuration::rep>(
895 static_cast<_Ct>(__fd.count()) * static_cast<_Ct>(_Period::num)
896 / static_cast<_Ct>(_Period::den)));
897 }
898};
899
900template <class _ToDuration, class _Rep, class _Period>
901inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000902_LIBCPP_CONSTEXPR
Howard Hinnantc51e1022010-05-11 19:42:16 +0000903typename enable_if
904<
905 __is_duration<_ToDuration>::value,
906 _ToDuration
907>::type
908duration_cast(const duration<_Rep, _Period>& __fd)
909{
910 return __duration_cast<duration<_Rep, _Period>, _ToDuration>()(__fd);
911}
912
Howard Hinnant874ad9a2010-09-21 21:28:23 +0000913template <class _Rep>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000914struct _LIBCPP_TEMPLATE_VIS treat_as_floating_point : is_floating_point<_Rep> {};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000915
Marshall Clow59581f02015-11-30 05:39:30 +0000916#if _LIBCPP_STD_VER > 14 && !defined(_LIBCPP_HAS_NO_VARIABLE_TEMPLATES)
Marshall Clowf1bf62f2018-01-02 17:17:01 +0000917template <class _Rep>
918_LIBCPP_INLINE_VAR _LIBCPP_CONSTEXPR bool treat_as_floating_point_v
Marshall Clow59581f02015-11-30 05:39:30 +0000919 = treat_as_floating_point<_Rep>::value;
920#endif
921
Howard Hinnantc51e1022010-05-11 19:42:16 +0000922template <class _Rep>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000923struct _LIBCPP_TEMPLATE_VIS duration_values
Howard Hinnantc51e1022010-05-11 19:42:16 +0000924{
925public:
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000926 _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR _Rep zero() {return _Rep(0);}
927 _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR _Rep max() {return numeric_limits<_Rep>::max();}
928 _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR _Rep min() {return numeric_limits<_Rep>::lowest();}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000929};
930
Marshall Clowf2eabaf2015-11-05 19:33:59 +0000931#if _LIBCPP_STD_VER > 14
932template <class _ToDuration, class _Rep, class _Period>
933inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
934typename enable_if
935<
936 __is_duration<_ToDuration>::value,
937 _ToDuration
938>::type
939floor(const duration<_Rep, _Period>& __d)
940{
941 _ToDuration __t = duration_cast<_ToDuration>(__d);
942 if (__t > __d)
943 __t = __t - _ToDuration{1};
944 return __t;
945}
946
947template <class _ToDuration, class _Rep, class _Period>
948inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
949typename enable_if
950<
951 __is_duration<_ToDuration>::value,
952 _ToDuration
953>::type
954ceil(const duration<_Rep, _Period>& __d)
955{
956 _ToDuration __t = duration_cast<_ToDuration>(__d);
957 if (__t < __d)
958 __t = __t + _ToDuration{1};
959 return __t;
960}
961
962template <class _ToDuration, class _Rep, class _Period>
963inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
964typename enable_if
965<
966 __is_duration<_ToDuration>::value,
967 _ToDuration
968>::type
969round(const duration<_Rep, _Period>& __d)
970{
971 _ToDuration __lower = floor<_ToDuration>(__d);
972 _ToDuration __upper = __lower + _ToDuration{1};
973 auto __lowerDiff = __d - __lower;
974 auto __upperDiff = __upper - __d;
975 if (__lowerDiff < __upperDiff)
976 return __lower;
977 if (__lowerDiff > __upperDiff)
978 return __upper;
979 return __lower.count() & 1 ? __upper : __lower;
980}
981#endif
982
Howard Hinnantc51e1022010-05-11 19:42:16 +0000983// duration
984
985template <class _Rep, class _Period>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000986class _LIBCPP_TEMPLATE_VIS duration
Howard Hinnantc51e1022010-05-11 19:42:16 +0000987{
988 static_assert(!__is_duration<_Rep>::value, "A duration representation can not be a duration");
989 static_assert(__is_ratio<_Period>::value, "Second template parameter of duration must be a std::ratio");
990 static_assert(_Period::num > 0, "duration period must be positive");
Howard Hinnant0a51e152013-08-31 16:51:56 +0000991
992 template <class _R1, class _R2>
993 struct __no_overflow
994 {
995 private:
996 static const intmax_t __gcd_n1_n2 = __static_gcd<_R1::num, _R2::num>::value;
997 static const intmax_t __gcd_d1_d2 = __static_gcd<_R1::den, _R2::den>::value;
998 static const intmax_t __n1 = _R1::num / __gcd_n1_n2;
999 static const intmax_t __d1 = _R1::den / __gcd_d1_d2;
1000 static const intmax_t __n2 = _R2::num / __gcd_n1_n2;
1001 static const intmax_t __d2 = _R2::den / __gcd_d1_d2;
1002 static const intmax_t max = -((intmax_t(1) << (sizeof(intmax_t) * CHAR_BIT - 1)) + 1);
1003
1004 template <intmax_t _Xp, intmax_t _Yp, bool __overflow>
1005 struct __mul // __overflow == false
1006 {
1007 static const intmax_t value = _Xp * _Yp;
1008 };
1009
1010 template <intmax_t _Xp, intmax_t _Yp>
1011 struct __mul<_Xp, _Yp, true>
1012 {
1013 static const intmax_t value = 1;
1014 };
1015
1016 public:
1017 static const bool value = (__n1 <= max / __d2) && (__n2 <= max / __d1);
1018 typedef ratio<__mul<__n1, __d2, !value>::value,
1019 __mul<__n2, __d1, !value>::value> type;
1020 };
Louis Dionne44bcff92018-08-03 22:36:53 +00001021
Howard Hinnantc51e1022010-05-11 19:42:16 +00001022public:
1023 typedef _Rep rep;
Marshall Clow76200b22017-03-21 18:38:57 +00001024 typedef typename _Period::type period;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001025private:
1026 rep __rep_;
1027public:
1028
Marshall Clow5c459c92013-07-31 19:32:19 +00001029 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Eric Fiselier07b2b552016-11-18 06:42:17 +00001030#ifndef _LIBCPP_CXX03_LANG
Marshall Clow5c459c92013-07-31 19:32:19 +00001031 duration() = default;
Marshall Clow3dbcf132013-07-31 19:39:37 +00001032#else
1033 duration() {}
Marshall Clow5c459c92013-07-31 19:32:19 +00001034#endif
1035
Howard Hinnantc51e1022010-05-11 19:42:16 +00001036 template <class _Rep2>
Howard Hinnantcf3143c2012-07-13 19:17:27 +00001037 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnantc51e1022010-05-11 19:42:16 +00001038 explicit duration(const _Rep2& __r,
1039 typename enable_if
1040 <
1041 is_convertible<_Rep2, rep>::value &&
1042 (treat_as_floating_point<rep>::value ||
1043 !treat_as_floating_point<_Rep2>::value)
1044 >::type* = 0)
1045 : __rep_(__r) {}
1046
1047 // conversions
1048 template <class _Rep2, class _Period2>
Howard Hinnantcf3143c2012-07-13 19:17:27 +00001049 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnantc51e1022010-05-11 19:42:16 +00001050 duration(const duration<_Rep2, _Period2>& __d,
1051 typename enable_if
1052 <
Howard Hinnant0a51e152013-08-31 16:51:56 +00001053 __no_overflow<_Period2, period>::value && (
Howard Hinnantc51e1022010-05-11 19:42:16 +00001054 treat_as_floating_point<rep>::value ||
Howard Hinnant0a51e152013-08-31 16:51:56 +00001055 (__no_overflow<_Period2, period>::type::den == 1 &&
1056 !treat_as_floating_point<_Rep2>::value))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001057 >::type* = 0)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001058 : __rep_(_VSTD::chrono::duration_cast<duration>(__d).count()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001059
1060 // observer
1061
Howard Hinnantcf3143c2012-07-13 19:17:27 +00001062 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR rep count() const {return __rep_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001063
1064 // arithmetic
1065
Marshall Clow76200b22017-03-21 18:38:57 +00001066 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR typename common_type<duration>::type operator+() const {return typename common_type<duration>::type(*this);}
1067 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR typename common_type<duration>::type operator-() const {return typename common_type<duration>::type(-__rep_);}
Marshall Clow5c87ad02017-01-04 23:03:24 +00001068 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 duration& operator++() {++__rep_; return *this;}
1069 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 duration operator++(int) {return duration(__rep_++);}
1070 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 duration& operator--() {--__rep_; return *this;}
1071 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 duration operator--(int) {return duration(__rep_--);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001072
Marshall Clow5c87ad02017-01-04 23:03:24 +00001073 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 duration& operator+=(const duration& __d) {__rep_ += __d.count(); return *this;}
1074 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 duration& operator-=(const duration& __d) {__rep_ -= __d.count(); return *this;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001075
Marshall Clow5c87ad02017-01-04 23:03:24 +00001076 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 duration& operator*=(const rep& rhs) {__rep_ *= rhs; return *this;}
1077 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 duration& operator/=(const rep& rhs) {__rep_ /= rhs; return *this;}
1078 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 duration& operator%=(const rep& rhs) {__rep_ %= rhs; return *this;}
1079 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 duration& operator%=(const duration& rhs) {__rep_ %= rhs.count(); return *this;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001080
1081 // special values
1082
Howard Hinnantcf3143c2012-07-13 19:17:27 +00001083 _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR duration zero() {return duration(duration_values<rep>::zero());}
1084 _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR duration min() {return duration(duration_values<rep>::min());}
1085 _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR duration max() {return duration(duration_values<rep>::max());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001086};
1087
1088typedef duration<long long, nano> nanoseconds;
1089typedef duration<long long, micro> microseconds;
1090typedef duration<long long, milli> milliseconds;
1091typedef duration<long long > seconds;
1092typedef duration< long, ratio< 60> > minutes;
1093typedef duration< long, ratio<3600> > hours;
Artem Dergachev7846aaf2018-10-16 02:40:42 +00001094
Howard Hinnantc51e1022010-05-11 19:42:16 +00001095// Duration ==
1096
1097template <class _LhsDuration, class _RhsDuration>
1098struct __duration_eq
1099{
Howard Hinnantcf3143c2012-07-13 19:17:27 +00001100 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnant12967fb2013-06-28 18:09:35 +00001101 bool operator()(const _LhsDuration& __lhs, const _RhsDuration& __rhs) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00001102 {
1103 typedef typename common_type<_LhsDuration, _RhsDuration>::type _Ct;
1104 return _Ct(__lhs).count() == _Ct(__rhs).count();
1105 }
1106};
1107
1108template <class _LhsDuration>
1109struct __duration_eq<_LhsDuration, _LhsDuration>
1110{
Howard Hinnantcf3143c2012-07-13 19:17:27 +00001111 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnant12967fb2013-06-28 18:09:35 +00001112 bool operator()(const _LhsDuration& __lhs, const _LhsDuration& __rhs) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00001113 {return __lhs.count() == __rhs.count();}
1114};
1115
1116template <class _Rep1, class _Period1, class _Rep2, class _Period2>
1117inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantcf3143c2012-07-13 19:17:27 +00001118_LIBCPP_CONSTEXPR
Howard Hinnantc51e1022010-05-11 19:42:16 +00001119bool
1120operator==(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
1121{
1122 return __duration_eq<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >()(__lhs, __rhs);
1123}
1124
1125// Duration !=
1126
1127template <class _Rep1, class _Period1, class _Rep2, class _Period2>
1128inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantcf3143c2012-07-13 19:17:27 +00001129_LIBCPP_CONSTEXPR
Howard Hinnantc51e1022010-05-11 19:42:16 +00001130bool
1131operator!=(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
1132{
1133 return !(__lhs == __rhs);
1134}
1135
1136// Duration <
1137
1138template <class _LhsDuration, class _RhsDuration>
1139struct __duration_lt
1140{
Howard Hinnantcf3143c2012-07-13 19:17:27 +00001141 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnant12967fb2013-06-28 18:09:35 +00001142 bool operator()(const _LhsDuration& __lhs, const _RhsDuration& __rhs) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00001143 {
1144 typedef typename common_type<_LhsDuration, _RhsDuration>::type _Ct;
1145 return _Ct(__lhs).count() < _Ct(__rhs).count();
1146 }
1147};
1148
1149template <class _LhsDuration>
1150struct __duration_lt<_LhsDuration, _LhsDuration>
1151{
Howard Hinnantcf3143c2012-07-13 19:17:27 +00001152 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnant12967fb2013-06-28 18:09:35 +00001153 bool operator()(const _LhsDuration& __lhs, const _LhsDuration& __rhs) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00001154 {return __lhs.count() < __rhs.count();}
1155};
1156
1157template <class _Rep1, class _Period1, class _Rep2, class _Period2>
1158inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantcf3143c2012-07-13 19:17:27 +00001159_LIBCPP_CONSTEXPR
Howard Hinnantc51e1022010-05-11 19:42:16 +00001160bool
1161operator< (const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
1162{
1163 return __duration_lt<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >()(__lhs, __rhs);
1164}
1165
1166// Duration >
1167
1168template <class _Rep1, class _Period1, class _Rep2, class _Period2>
1169inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantcf3143c2012-07-13 19:17:27 +00001170_LIBCPP_CONSTEXPR
Howard Hinnantc51e1022010-05-11 19:42:16 +00001171bool
1172operator> (const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
1173{
1174 return __rhs < __lhs;
1175}
1176
1177// Duration <=
1178
1179template <class _Rep1, class _Period1, class _Rep2, class _Period2>
1180inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantcf3143c2012-07-13 19:17:27 +00001181_LIBCPP_CONSTEXPR
Howard Hinnantc51e1022010-05-11 19:42:16 +00001182bool
1183operator<=(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
1184{
1185 return !(__rhs < __lhs);
1186}
1187
1188// Duration >=
1189
1190template <class _Rep1, class _Period1, class _Rep2, class _Period2>
1191inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantcf3143c2012-07-13 19:17:27 +00001192_LIBCPP_CONSTEXPR
Howard Hinnantc51e1022010-05-11 19:42:16 +00001193bool
1194operator>=(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
1195{
1196 return !(__lhs < __rhs);
1197}
1198
1199// Duration +
1200
1201template <class _Rep1, class _Period1, class _Rep2, class _Period2>
1202inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantcf3143c2012-07-13 19:17:27 +00001203_LIBCPP_CONSTEXPR
Howard Hinnantc51e1022010-05-11 19:42:16 +00001204typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type
1205operator+(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
1206{
Howard Hinnantcf3143c2012-07-13 19:17:27 +00001207 typedef typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type _Cd;
1208 return _Cd(_Cd(__lhs).count() + _Cd(__rhs).count());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001209}
1210
1211// Duration -
1212
1213template <class _Rep1, class _Period1, class _Rep2, class _Period2>
1214inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantcf3143c2012-07-13 19:17:27 +00001215_LIBCPP_CONSTEXPR
Howard Hinnantc51e1022010-05-11 19:42:16 +00001216typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type
1217operator-(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
1218{
Howard Hinnantcf3143c2012-07-13 19:17:27 +00001219 typedef typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type _Cd;
1220 return _Cd(_Cd(__lhs).count() - _Cd(__rhs).count());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001221}
1222
1223// Duration *
1224
1225template <class _Rep1, class _Period, class _Rep2>
1226inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantcf3143c2012-07-13 19:17:27 +00001227_LIBCPP_CONSTEXPR
Howard Hinnantc51e1022010-05-11 19:42:16 +00001228typename enable_if
1229<
1230 is_convertible<_Rep2, typename common_type<_Rep1, _Rep2>::type>::value,
1231 duration<typename common_type<_Rep1, _Rep2>::type, _Period>
1232>::type
1233operator*(const duration<_Rep1, _Period>& __d, const _Rep2& __s)
1234{
1235 typedef typename common_type<_Rep1, _Rep2>::type _Cr;
Howard Hinnantcf3143c2012-07-13 19:17:27 +00001236 typedef duration<_Cr, _Period> _Cd;
1237 return _Cd(_Cd(__d).count() * static_cast<_Cr>(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001238}
1239
1240template <class _Rep1, class _Period, class _Rep2>
1241inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantcf3143c2012-07-13 19:17:27 +00001242_LIBCPP_CONSTEXPR
Howard Hinnantc51e1022010-05-11 19:42:16 +00001243typename enable_if
1244<
1245 is_convertible<_Rep1, typename common_type<_Rep1, _Rep2>::type>::value,
1246 duration<typename common_type<_Rep1, _Rep2>::type, _Period>
1247>::type
1248operator*(const _Rep1& __s, const duration<_Rep2, _Period>& __d)
1249{
1250 return __d * __s;
1251}
1252
1253// Duration /
1254
1255template <class _Duration, class _Rep, bool = __is_duration<_Rep>::value>
1256struct __duration_divide_result
1257{
1258};
1259
1260template <class _Duration, class _Rep2,
1261 bool = is_convertible<_Rep2,
1262 typename common_type<typename _Duration::rep, _Rep2>::type>::value>
1263struct __duration_divide_imp
1264{
1265};
1266
1267template <class _Rep1, class _Period, class _Rep2>
1268struct __duration_divide_imp<duration<_Rep1, _Period>, _Rep2, true>
1269{
1270 typedef duration<typename common_type<_Rep1, _Rep2>::type, _Period> type;
1271};
1272
1273template <class _Rep1, class _Period, class _Rep2>
1274struct __duration_divide_result<duration<_Rep1, _Period>, _Rep2, false>
1275 : __duration_divide_imp<duration<_Rep1, _Period>, _Rep2>
1276{
1277};
1278
1279template <class _Rep1, class _Period, class _Rep2>
1280inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantcf3143c2012-07-13 19:17:27 +00001281_LIBCPP_CONSTEXPR
Howard Hinnantc51e1022010-05-11 19:42:16 +00001282typename __duration_divide_result<duration<_Rep1, _Period>, _Rep2>::type
1283operator/(const duration<_Rep1, _Period>& __d, const _Rep2& __s)
1284{
1285 typedef typename common_type<_Rep1, _Rep2>::type _Cr;
Howard Hinnantcf3143c2012-07-13 19:17:27 +00001286 typedef duration<_Cr, _Period> _Cd;
1287 return _Cd(_Cd(__d).count() / static_cast<_Cr>(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001288}
1289
1290template <class _Rep1, class _Period1, class _Rep2, class _Period2>
1291inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantcf3143c2012-07-13 19:17:27 +00001292_LIBCPP_CONSTEXPR
Howard Hinnantc51e1022010-05-11 19:42:16 +00001293typename common_type<_Rep1, _Rep2>::type
1294operator/(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
1295{
1296 typedef typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type _Ct;
1297 return _Ct(__lhs).count() / _Ct(__rhs).count();
1298}
1299
1300// Duration %
1301
1302template <class _Rep1, class _Period, class _Rep2>
1303inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantcf3143c2012-07-13 19:17:27 +00001304_LIBCPP_CONSTEXPR
Howard Hinnantc51e1022010-05-11 19:42:16 +00001305typename __duration_divide_result<duration<_Rep1, _Period>, _Rep2>::type
1306operator%(const duration<_Rep1, _Period>& __d, const _Rep2& __s)
1307{
1308 typedef typename common_type<_Rep1, _Rep2>::type _Cr;
Howard Hinnantcf3143c2012-07-13 19:17:27 +00001309 typedef duration<_Cr, _Period> _Cd;
1310 return _Cd(_Cd(__d).count() % static_cast<_Cr>(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001311}
1312
1313template <class _Rep1, class _Period1, class _Rep2, class _Period2>
1314inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantcf3143c2012-07-13 19:17:27 +00001315_LIBCPP_CONSTEXPR
Howard Hinnantc51e1022010-05-11 19:42:16 +00001316typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type
1317operator%(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
1318{
Howard Hinnantcf3143c2012-07-13 19:17:27 +00001319 typedef typename common_type<_Rep1, _Rep2>::type _Cr;
1320 typedef typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type _Cd;
1321 return _Cd(static_cast<_Cr>(_Cd(__lhs).count()) % static_cast<_Cr>(_Cd(__rhs).count()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001322}
1323
1324//////////////////////////////////////////////////////////
1325///////////////////// time_point /////////////////////////
1326//////////////////////////////////////////////////////////
1327
1328template <class _Clock, class _Duration = typename _Clock::duration>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001329class _LIBCPP_TEMPLATE_VIS time_point
Howard Hinnantc51e1022010-05-11 19:42:16 +00001330{
1331 static_assert(__is_duration<_Duration>::value,
1332 "Second template parameter of time_point must be a std::chrono::duration");
1333public:
1334 typedef _Clock clock;
1335 typedef _Duration duration;
1336 typedef typename duration::rep rep;
1337 typedef typename duration::period period;
1338private:
1339 duration __d_;
1340
1341public:
Marshall Clow5c459c92013-07-31 19:32:19 +00001342 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 time_point() : __d_(duration::zero()) {}
1343 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 explicit time_point(const duration& __d) : __d_(__d) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001344
1345 // conversions
1346 template <class _Duration2>
Marshall Clow5c459c92013-07-31 19:32:19 +00001347 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantc51e1022010-05-11 19:42:16 +00001348 time_point(const time_point<clock, _Duration2>& t,
1349 typename enable_if
1350 <
1351 is_convertible<_Duration2, duration>::value
1352 >::type* = 0)
1353 : __d_(t.time_since_epoch()) {}
1354
1355 // observer
1356
Marshall Clow5c459c92013-07-31 19:32:19 +00001357 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 duration time_since_epoch() const {return __d_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001358
1359 // arithmetic
1360
Marshall Clowdf247eb2018-08-29 23:02:15 +00001361 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 time_point& operator+=(const duration& __d) {__d_ += __d; return *this;}
1362 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 time_point& operator-=(const duration& __d) {__d_ -= __d; return *this;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001363
1364 // special values
1365
Howard Hinnantcf3143c2012-07-13 19:17:27 +00001366 _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR time_point min() {return time_point(duration::min());}
1367 _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR time_point max() {return time_point(duration::max());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001368};
1369
1370} // chrono
1371
1372template <class _Clock, class _Duration1, class _Duration2>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001373struct _LIBCPP_TEMPLATE_VIS common_type<chrono::time_point<_Clock, _Duration1>,
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00001374 chrono::time_point<_Clock, _Duration2> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001375{
1376 typedef chrono::time_point<_Clock, typename common_type<_Duration1, _Duration2>::type> type;
1377};
1378
1379namespace chrono {
1380
1381template <class _ToDuration, class _Clock, class _Duration>
Marshall Clow5c459c92013-07-31 19:32:19 +00001382inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantc51e1022010-05-11 19:42:16 +00001383time_point<_Clock, _ToDuration>
1384time_point_cast(const time_point<_Clock, _Duration>& __t)
1385{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001386 return time_point<_Clock, _ToDuration>(_VSTD::chrono::duration_cast<_ToDuration>(__t.time_since_epoch()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001387}
1388
Marshall Clowf2eabaf2015-11-05 19:33:59 +00001389#if _LIBCPP_STD_VER > 14
1390template <class _ToDuration, class _Clock, class _Duration>
1391inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
1392typename enable_if
1393<
1394 __is_duration<_ToDuration>::value,
1395 time_point<_Clock, _ToDuration>
1396>::type
1397floor(const time_point<_Clock, _Duration>& __t)
1398{
1399 return time_point<_Clock, _ToDuration>{floor<_ToDuration>(__t.time_since_epoch())};
1400}
1401
1402template <class _ToDuration, class _Clock, class _Duration>
1403inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
1404typename enable_if
1405<
1406 __is_duration<_ToDuration>::value,
1407 time_point<_Clock, _ToDuration>
1408>::type
1409ceil(const time_point<_Clock, _Duration>& __t)
1410{
1411 return time_point<_Clock, _ToDuration>{ceil<_ToDuration>(__t.time_since_epoch())};
1412}
1413
1414template <class _ToDuration, class _Clock, class _Duration>
1415inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
1416typename enable_if
1417<
1418 __is_duration<_ToDuration>::value,
1419 time_point<_Clock, _ToDuration>
1420>::type
1421round(const time_point<_Clock, _Duration>& __t)
1422{
1423 return time_point<_Clock, _ToDuration>{round<_ToDuration>(__t.time_since_epoch())};
1424}
1425
1426template <class _Rep, class _Period>
1427inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
1428typename enable_if
1429<
1430 numeric_limits<_Rep>::is_signed,
1431 duration<_Rep, _Period>
1432>::type
1433abs(duration<_Rep, _Period> __d)
1434{
1435 return __d >= __d.zero() ? __d : -__d;
1436}
1437#endif
1438
Howard Hinnantc51e1022010-05-11 19:42:16 +00001439// time_point ==
1440
1441template <class _Clock, class _Duration1, class _Duration2>
Marshall Clow5c459c92013-07-31 19:32:19 +00001442inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantc51e1022010-05-11 19:42:16 +00001443bool
1444operator==(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs)
1445{
1446 return __lhs.time_since_epoch() == __rhs.time_since_epoch();
1447}
1448
1449// time_point !=
1450
1451template <class _Clock, class _Duration1, class _Duration2>
Marshall Clow5c459c92013-07-31 19:32:19 +00001452inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantc51e1022010-05-11 19:42:16 +00001453bool
1454operator!=(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs)
1455{
1456 return !(__lhs == __rhs);
1457}
1458
1459// time_point <
1460
1461template <class _Clock, class _Duration1, class _Duration2>
Marshall Clow5c459c92013-07-31 19:32:19 +00001462inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantc51e1022010-05-11 19:42:16 +00001463bool
1464operator<(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs)
1465{
1466 return __lhs.time_since_epoch() < __rhs.time_since_epoch();
1467}
1468
1469// time_point >
1470
1471template <class _Clock, class _Duration1, class _Duration2>
Marshall Clow5c459c92013-07-31 19:32:19 +00001472inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantc51e1022010-05-11 19:42:16 +00001473bool
1474operator>(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs)
1475{
1476 return __rhs < __lhs;
1477}
1478
1479// time_point <=
1480
1481template <class _Clock, class _Duration1, class _Duration2>
Marshall Clow5c459c92013-07-31 19:32:19 +00001482inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantc51e1022010-05-11 19:42:16 +00001483bool
1484operator<=(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs)
1485{
1486 return !(__rhs < __lhs);
1487}
1488
1489// time_point >=
1490
1491template <class _Clock, class _Duration1, class _Duration2>
Marshall Clow5c459c92013-07-31 19:32:19 +00001492inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantc51e1022010-05-11 19:42:16 +00001493bool
1494operator>=(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs)
1495{
1496 return !(__lhs < __rhs);
1497}
1498
1499// time_point operator+(time_point x, duration y);
1500
1501template <class _Clock, class _Duration1, class _Rep2, class _Period2>
Marshall Clow5c459c92013-07-31 19:32:19 +00001502inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantc51e1022010-05-11 19:42:16 +00001503time_point<_Clock, typename common_type<_Duration1, duration<_Rep2, _Period2> >::type>
1504operator+(const time_point<_Clock, _Duration1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
1505{
1506 typedef time_point<_Clock, typename common_type<_Duration1, duration<_Rep2, _Period2> >::type> _Tr;
Marshall Clow5c459c92013-07-31 19:32:19 +00001507 return _Tr (__lhs.time_since_epoch() + __rhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001508}
1509
1510// time_point operator+(duration x, time_point y);
1511
1512template <class _Rep1, class _Period1, class _Clock, class _Duration2>
Marshall Clow5c459c92013-07-31 19:32:19 +00001513inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantc51e1022010-05-11 19:42:16 +00001514time_point<_Clock, typename common_type<duration<_Rep1, _Period1>, _Duration2>::type>
1515operator+(const duration<_Rep1, _Period1>& __lhs, const time_point<_Clock, _Duration2>& __rhs)
1516{
1517 return __rhs + __lhs;
1518}
1519
1520// time_point operator-(time_point x, duration y);
1521
1522template <class _Clock, class _Duration1, class _Rep2, class _Period2>
Marshall Clow5c459c92013-07-31 19:32:19 +00001523inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantc51e1022010-05-11 19:42:16 +00001524time_point<_Clock, typename common_type<_Duration1, duration<_Rep2, _Period2> >::type>
1525operator-(const time_point<_Clock, _Duration1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
1526{
Marshall Clow78dbe462016-11-14 18:22:19 +00001527 typedef time_point<_Clock, typename common_type<_Duration1, duration<_Rep2, _Period2> >::type> _Ret;
1528 return _Ret(__lhs.time_since_epoch() -__rhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001529}
1530
1531// duration operator-(time_point x, time_point y);
1532
1533template <class _Clock, class _Duration1, class _Duration2>
Marshall Clow5c459c92013-07-31 19:32:19 +00001534inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantc51e1022010-05-11 19:42:16 +00001535typename common_type<_Duration1, _Duration2>::type
1536operator-(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs)
1537{
1538 return __lhs.time_since_epoch() - __rhs.time_since_epoch();
1539}
1540
1541//////////////////////////////////////////////////////////
1542/////////////////////// clocks ///////////////////////////
1543//////////////////////////////////////////////////////////
1544
Howard Hinnant8331b762013-03-06 23:30:19 +00001545class _LIBCPP_TYPE_VIS system_clock
Howard Hinnantc51e1022010-05-11 19:42:16 +00001546{
1547public:
1548 typedef microseconds duration;
1549 typedef duration::rep rep;
1550 typedef duration::period period;
1551 typedef chrono::time_point<system_clock> time_point;
Marshall Clow5c459c92013-07-31 19:32:19 +00001552 static _LIBCPP_CONSTEXPR_AFTER_CXX11 const bool is_steady = false;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001553
Howard Hinnantaa54ac42011-05-28 18:34:36 +00001554 static time_point now() _NOEXCEPT;
1555 static time_t to_time_t (const time_point& __t) _NOEXCEPT;
1556 static time_point from_time_t(time_t __t) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001557};
1558
Jonathan Roelofscce96eb2014-09-02 21:14:38 +00001559#ifndef _LIBCPP_HAS_NO_MONOTONIC_CLOCK
Howard Hinnant8331b762013-03-06 23:30:19 +00001560class _LIBCPP_TYPE_VIS steady_clock
Howard Hinnantc51e1022010-05-11 19:42:16 +00001561{
1562public:
1563 typedef nanoseconds duration;
1564 typedef duration::rep rep;
1565 typedef duration::period period;
Howard Hinnantc8dbd222010-11-20 19:16:30 +00001566 typedef chrono::time_point<steady_clock, duration> time_point;
Marshall Clow5c459c92013-07-31 19:32:19 +00001567 static _LIBCPP_CONSTEXPR_AFTER_CXX11 const bool is_steady = true;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001568
Howard Hinnantaa54ac42011-05-28 18:34:36 +00001569 static time_point now() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001570};
1571
Howard Hinnantc8dbd222010-11-20 19:16:30 +00001572typedef steady_clock high_resolution_clock;
Jonathan Roelofscce96eb2014-09-02 21:14:38 +00001573#else
1574typedef system_clock high_resolution_clock;
1575#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001576
1577} // chrono
1578
Marshall Clowac868372013-10-05 21:18:32 +00001579#if _LIBCPP_STD_VER > 11
1580// Suffixes for duration literals [time.duration.literals]
1581inline namespace literals
Louis Dionne44bcff92018-08-03 22:36:53 +00001582{
Marshall Cloweb41e5c2013-07-24 21:18:14 +00001583 inline namespace chrono_literals
1584 {
1585
Marshall Clow2ac805a2017-08-09 15:42:50 +00001586 constexpr chrono::hours operator""h(unsigned long long __h)
Howard Hinnant5c167562013-08-07 19:39:48 +00001587 {
1588 return chrono::hours(static_cast<chrono::hours::rep>(__h));
1589 }
Marshall Cloweb41e5c2013-07-24 21:18:14 +00001590
Marshall Clow2ac805a2017-08-09 15:42:50 +00001591 constexpr chrono::duration<long double, ratio<3600,1>> operator""h(long double __h)
Howard Hinnant5c167562013-08-07 19:39:48 +00001592 {
1593 return chrono::duration<long double, ratio<3600,1>>(__h);
1594 }
Marshall Cloweb41e5c2013-07-24 21:18:14 +00001595
1596
Marshall Clow2ac805a2017-08-09 15:42:50 +00001597 constexpr chrono::minutes operator""min(unsigned long long __m)
Howard Hinnant5c167562013-08-07 19:39:48 +00001598 {
1599 return chrono::minutes(static_cast<chrono::minutes::rep>(__m));
1600 }
Marshall Cloweb41e5c2013-07-24 21:18:14 +00001601
Marshall Clow2ac805a2017-08-09 15:42:50 +00001602 constexpr chrono::duration<long double, ratio<60,1>> operator""min(long double __m)
Howard Hinnant5c167562013-08-07 19:39:48 +00001603 {
1604 return chrono::duration<long double, ratio<60,1>> (__m);
1605 }
Marshall Cloweb41e5c2013-07-24 21:18:14 +00001606
1607
Marshall Clow2ac805a2017-08-09 15:42:50 +00001608 constexpr chrono::seconds operator""s(unsigned long long __s)
Howard Hinnant5c167562013-08-07 19:39:48 +00001609 {
1610 return chrono::seconds(static_cast<chrono::seconds::rep>(__s));
1611 }
Marshall Cloweb41e5c2013-07-24 21:18:14 +00001612
Marshall Clow2ac805a2017-08-09 15:42:50 +00001613 constexpr chrono::duration<long double> operator""s(long double __s)
Howard Hinnant5c167562013-08-07 19:39:48 +00001614 {
1615 return chrono::duration<long double> (__s);
1616 }
Marshall Cloweb41e5c2013-07-24 21:18:14 +00001617
1618
Marshall Clow2ac805a2017-08-09 15:42:50 +00001619 constexpr chrono::milliseconds operator""ms(unsigned long long __ms)
Howard Hinnant5c167562013-08-07 19:39:48 +00001620 {
1621 return chrono::milliseconds(static_cast<chrono::milliseconds::rep>(__ms));
1622 }
Marshall Cloweb41e5c2013-07-24 21:18:14 +00001623
Marshall Clow2ac805a2017-08-09 15:42:50 +00001624 constexpr chrono::duration<long double, milli> operator""ms(long double __ms)
Howard Hinnant5c167562013-08-07 19:39:48 +00001625 {
1626 return chrono::duration<long double, milli>(__ms);
1627 }
Marshall Cloweb41e5c2013-07-24 21:18:14 +00001628
1629
Marshall Clow2ac805a2017-08-09 15:42:50 +00001630 constexpr chrono::microseconds operator""us(unsigned long long __us)
Howard Hinnant5c167562013-08-07 19:39:48 +00001631 {
1632 return chrono::microseconds(static_cast<chrono::microseconds::rep>(__us));
1633 }
Marshall Cloweb41e5c2013-07-24 21:18:14 +00001634
Marshall Clow2ac805a2017-08-09 15:42:50 +00001635 constexpr chrono::duration<long double, micro> operator""us(long double __us)
Howard Hinnant5c167562013-08-07 19:39:48 +00001636 {
1637 return chrono::duration<long double, micro> (__us);
1638 }
Louis Dionne44bcff92018-08-03 22:36:53 +00001639
Marshall Cloweb41e5c2013-07-24 21:18:14 +00001640
Marshall Clow2ac805a2017-08-09 15:42:50 +00001641 constexpr chrono::nanoseconds operator""ns(unsigned long long __ns)
Howard Hinnant5c167562013-08-07 19:39:48 +00001642 {
1643 return chrono::nanoseconds(static_cast<chrono::nanoseconds::rep>(__ns));
1644 }
Marshall Cloweb41e5c2013-07-24 21:18:14 +00001645
Marshall Clow2ac805a2017-08-09 15:42:50 +00001646 constexpr chrono::duration<long double, nano> operator""ns(long double __ns)
Howard Hinnant5c167562013-08-07 19:39:48 +00001647 {
1648 return chrono::duration<long double, nano> (__ns);
1649 }
Marshall Cloweb41e5c2013-07-24 21:18:14 +00001650
1651}}
Marshall Clowac868372013-10-05 21:18:32 +00001652
1653namespace chrono { // hoist the literals into namespace std::chrono
1654 using namespace literals::chrono_literals;
1655}
1656
Marshall Cloweb41e5c2013-07-24 21:18:14 +00001657#endif
1658
Howard Hinnantc51e1022010-05-11 19:42:16 +00001659_LIBCPP_END_NAMESPACE_STD
1660
Eric Fiselierf4433a32017-05-31 22:07:49 +00001661_LIBCPP_POP_MACROS
1662
Howard Hinnantc51e1022010-05-11 19:42:16 +00001663#endif // _LIBCPP_CHRONO