blob: b9709b373659500809af6f26abb154834a6e8712 [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>
801
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000802#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000803#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000804#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000805
Eric Fiselierf4433a32017-05-31 22:07:49 +0000806_LIBCPP_PUSH_MACROS
807#include <__undef_macros>
808
809
Howard Hinnantc51e1022010-05-11 19:42:16 +0000810_LIBCPP_BEGIN_NAMESPACE_STD
811
812namespace chrono
813{
814
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000815template <class _Rep, class _Period = ratio<1> > class _LIBCPP_TEMPLATE_VIS duration;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000816
Howard Hinnantf39463b2010-05-18 17:32:30 +0000817template <class _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000818struct __is_duration : false_type {};
819
820template <class _Rep, class _Period>
821struct __is_duration<duration<_Rep, _Period> > : true_type {};
822
823template <class _Rep, class _Period>
824struct __is_duration<const duration<_Rep, _Period> > : true_type {};
825
826template <class _Rep, class _Period>
827struct __is_duration<volatile duration<_Rep, _Period> > : true_type {};
828
829template <class _Rep, class _Period>
830struct __is_duration<const volatile duration<_Rep, _Period> > : true_type {};
831
832} // chrono
833
834template <class _Rep1, class _Period1, class _Rep2, class _Period2>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000835struct _LIBCPP_TEMPLATE_VIS common_type<chrono::duration<_Rep1, _Period1>,
Howard Hinnanta37d3cf2013-08-12 18:38:34 +0000836 chrono::duration<_Rep2, _Period2> >
Howard Hinnantc51e1022010-05-11 19:42:16 +0000837{
838 typedef chrono::duration<typename common_type<_Rep1, _Rep2>::type,
839 typename __ratio_gcd<_Period1, _Period2>::type> type;
840};
841
842namespace chrono {
843
844// duration_cast
845
846template <class _FromDuration, class _ToDuration,
847 class _Period = typename ratio_divide<typename _FromDuration::period, typename _ToDuration::period>::type,
848 bool = _Period::num == 1,
849 bool = _Period::den == 1>
850struct __duration_cast;
851
852template <class _FromDuration, class _ToDuration, class _Period>
853struct __duration_cast<_FromDuration, _ToDuration, _Period, true, true>
854{
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000855 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnantc51e1022010-05-11 19:42:16 +0000856 _ToDuration operator()(const _FromDuration& __fd) const
857 {
858 return _ToDuration(static_cast<typename _ToDuration::rep>(__fd.count()));
859 }
860};
861
862template <class _FromDuration, class _ToDuration, class _Period>
863struct __duration_cast<_FromDuration, _ToDuration, _Period, true, false>
864{
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000865 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnantc51e1022010-05-11 19:42:16 +0000866 _ToDuration operator()(const _FromDuration& __fd) const
867 {
868 typedef typename common_type<typename _ToDuration::rep, typename _FromDuration::rep, intmax_t>::type _Ct;
869 return _ToDuration(static_cast<typename _ToDuration::rep>(
870 static_cast<_Ct>(__fd.count()) / static_cast<_Ct>(_Period::den)));
871 }
872};
873
874template <class _FromDuration, class _ToDuration, class _Period>
875struct __duration_cast<_FromDuration, _ToDuration, _Period, false, true>
876{
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000877 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnantc51e1022010-05-11 19:42:16 +0000878 _ToDuration operator()(const _FromDuration& __fd) const
879 {
880 typedef typename common_type<typename _ToDuration::rep, typename _FromDuration::rep, intmax_t>::type _Ct;
881 return _ToDuration(static_cast<typename _ToDuration::rep>(
882 static_cast<_Ct>(__fd.count()) * static_cast<_Ct>(_Period::num)));
883 }
884};
885
886template <class _FromDuration, class _ToDuration, class _Period>
887struct __duration_cast<_FromDuration, _ToDuration, _Period, false, false>
888{
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000889 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnantc51e1022010-05-11 19:42:16 +0000890 _ToDuration operator()(const _FromDuration& __fd) const
891 {
892 typedef typename common_type<typename _ToDuration::rep, typename _FromDuration::rep, intmax_t>::type _Ct;
893 return _ToDuration(static_cast<typename _ToDuration::rep>(
894 static_cast<_Ct>(__fd.count()) * static_cast<_Ct>(_Period::num)
895 / static_cast<_Ct>(_Period::den)));
896 }
897};
898
899template <class _ToDuration, class _Rep, class _Period>
900inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000901_LIBCPP_CONSTEXPR
Howard Hinnantc51e1022010-05-11 19:42:16 +0000902typename enable_if
903<
904 __is_duration<_ToDuration>::value,
905 _ToDuration
906>::type
907duration_cast(const duration<_Rep, _Period>& __fd)
908{
909 return __duration_cast<duration<_Rep, _Period>, _ToDuration>()(__fd);
910}
911
Howard Hinnant874ad9a2010-09-21 21:28:23 +0000912template <class _Rep>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000913struct _LIBCPP_TEMPLATE_VIS treat_as_floating_point : is_floating_point<_Rep> {};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000914
Marshall Clow59581f02015-11-30 05:39:30 +0000915#if _LIBCPP_STD_VER > 14 && !defined(_LIBCPP_HAS_NO_VARIABLE_TEMPLATES)
Marshall Clowf1bf62f2018-01-02 17:17:01 +0000916template <class _Rep>
917_LIBCPP_INLINE_VAR _LIBCPP_CONSTEXPR bool treat_as_floating_point_v
Marshall Clow59581f02015-11-30 05:39:30 +0000918 = treat_as_floating_point<_Rep>::value;
919#endif
920
Howard Hinnantc51e1022010-05-11 19:42:16 +0000921template <class _Rep>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000922struct _LIBCPP_TEMPLATE_VIS duration_values
Howard Hinnantc51e1022010-05-11 19:42:16 +0000923{
924public:
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000925 _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR _Rep zero() {return _Rep(0);}
926 _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR _Rep max() {return numeric_limits<_Rep>::max();}
927 _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR _Rep min() {return numeric_limits<_Rep>::lowest();}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000928};
929
Marshall Clowf2eabaf2015-11-05 19:33:59 +0000930#if _LIBCPP_STD_VER > 14
931template <class _ToDuration, class _Rep, class _Period>
932inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
933typename enable_if
934<
935 __is_duration<_ToDuration>::value,
936 _ToDuration
937>::type
938floor(const duration<_Rep, _Period>& __d)
939{
940 _ToDuration __t = duration_cast<_ToDuration>(__d);
941 if (__t > __d)
942 __t = __t - _ToDuration{1};
943 return __t;
944}
945
946template <class _ToDuration, class _Rep, class _Period>
947inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
948typename enable_if
949<
950 __is_duration<_ToDuration>::value,
951 _ToDuration
952>::type
953ceil(const duration<_Rep, _Period>& __d)
954{
955 _ToDuration __t = duration_cast<_ToDuration>(__d);
956 if (__t < __d)
957 __t = __t + _ToDuration{1};
958 return __t;
959}
960
961template <class _ToDuration, class _Rep, class _Period>
962inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
963typename enable_if
964<
965 __is_duration<_ToDuration>::value,
966 _ToDuration
967>::type
968round(const duration<_Rep, _Period>& __d)
969{
970 _ToDuration __lower = floor<_ToDuration>(__d);
971 _ToDuration __upper = __lower + _ToDuration{1};
972 auto __lowerDiff = __d - __lower;
973 auto __upperDiff = __upper - __d;
974 if (__lowerDiff < __upperDiff)
975 return __lower;
976 if (__lowerDiff > __upperDiff)
977 return __upper;
978 return __lower.count() & 1 ? __upper : __lower;
979}
980#endif
981
Howard Hinnantc51e1022010-05-11 19:42:16 +0000982// duration
983
984template <class _Rep, class _Period>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000985class _LIBCPP_TEMPLATE_VIS duration
Howard Hinnantc51e1022010-05-11 19:42:16 +0000986{
987 static_assert(!__is_duration<_Rep>::value, "A duration representation can not be a duration");
988 static_assert(__is_ratio<_Period>::value, "Second template parameter of duration must be a std::ratio");
989 static_assert(_Period::num > 0, "duration period must be positive");
Howard Hinnant0a51e152013-08-31 16:51:56 +0000990
991 template <class _R1, class _R2>
992 struct __no_overflow
993 {
994 private:
995 static const intmax_t __gcd_n1_n2 = __static_gcd<_R1::num, _R2::num>::value;
996 static const intmax_t __gcd_d1_d2 = __static_gcd<_R1::den, _R2::den>::value;
997 static const intmax_t __n1 = _R1::num / __gcd_n1_n2;
998 static const intmax_t __d1 = _R1::den / __gcd_d1_d2;
999 static const intmax_t __n2 = _R2::num / __gcd_n1_n2;
1000 static const intmax_t __d2 = _R2::den / __gcd_d1_d2;
1001 static const intmax_t max = -((intmax_t(1) << (sizeof(intmax_t) * CHAR_BIT - 1)) + 1);
1002
1003 template <intmax_t _Xp, intmax_t _Yp, bool __overflow>
1004 struct __mul // __overflow == false
1005 {
1006 static const intmax_t value = _Xp * _Yp;
1007 };
1008
1009 template <intmax_t _Xp, intmax_t _Yp>
1010 struct __mul<_Xp, _Yp, true>
1011 {
1012 static const intmax_t value = 1;
1013 };
1014
1015 public:
1016 static const bool value = (__n1 <= max / __d2) && (__n2 <= max / __d1);
1017 typedef ratio<__mul<__n1, __d2, !value>::value,
1018 __mul<__n2, __d1, !value>::value> type;
1019 };
Louis Dionne44bcff92018-08-03 22:36:53 +00001020
Howard Hinnantc51e1022010-05-11 19:42:16 +00001021public:
1022 typedef _Rep rep;
Marshall Clow76200b22017-03-21 18:38:57 +00001023 typedef typename _Period::type period;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001024private:
1025 rep __rep_;
1026public:
1027
Marshall Clow5c459c92013-07-31 19:32:19 +00001028 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Eric Fiselier07b2b552016-11-18 06:42:17 +00001029#ifndef _LIBCPP_CXX03_LANG
Marshall Clow5c459c92013-07-31 19:32:19 +00001030 duration() = default;
Marshall Clow3dbcf132013-07-31 19:39:37 +00001031#else
1032 duration() {}
Marshall Clow5c459c92013-07-31 19:32:19 +00001033#endif
1034
Howard Hinnantc51e1022010-05-11 19:42:16 +00001035 template <class _Rep2>
Howard Hinnantcf3143c2012-07-13 19:17:27 +00001036 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnantc51e1022010-05-11 19:42:16 +00001037 explicit duration(const _Rep2& __r,
1038 typename enable_if
1039 <
1040 is_convertible<_Rep2, rep>::value &&
1041 (treat_as_floating_point<rep>::value ||
1042 !treat_as_floating_point<_Rep2>::value)
1043 >::type* = 0)
1044 : __rep_(__r) {}
1045
1046 // conversions
1047 template <class _Rep2, class _Period2>
Howard Hinnantcf3143c2012-07-13 19:17:27 +00001048 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnantc51e1022010-05-11 19:42:16 +00001049 duration(const duration<_Rep2, _Period2>& __d,
1050 typename enable_if
1051 <
Howard Hinnant0a51e152013-08-31 16:51:56 +00001052 __no_overflow<_Period2, period>::value && (
Howard Hinnantc51e1022010-05-11 19:42:16 +00001053 treat_as_floating_point<rep>::value ||
Howard Hinnant0a51e152013-08-31 16:51:56 +00001054 (__no_overflow<_Period2, period>::type::den == 1 &&
1055 !treat_as_floating_point<_Rep2>::value))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001056 >::type* = 0)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001057 : __rep_(_VSTD::chrono::duration_cast<duration>(__d).count()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001058
1059 // observer
1060
Howard Hinnantcf3143c2012-07-13 19:17:27 +00001061 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR rep count() const {return __rep_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001062
1063 // arithmetic
1064
Marshall Clow76200b22017-03-21 18:38:57 +00001065 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR typename common_type<duration>::type operator+() const {return typename common_type<duration>::type(*this);}
1066 _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 +00001067 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 duration& operator++() {++__rep_; return *this;}
1068 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 duration operator++(int) {return duration(__rep_++);}
1069 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 duration& operator--() {--__rep_; return *this;}
1070 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 duration operator--(int) {return duration(__rep_--);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001071
Marshall Clow5c87ad02017-01-04 23:03:24 +00001072 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 duration& operator+=(const duration& __d) {__rep_ += __d.count(); return *this;}
1073 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 duration& operator-=(const duration& __d) {__rep_ -= __d.count(); return *this;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001074
Marshall Clow5c87ad02017-01-04 23:03:24 +00001075 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 duration& operator*=(const rep& rhs) {__rep_ *= rhs; return *this;}
1076 _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 duration& rhs) {__rep_ %= rhs.count(); return *this;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001079
1080 // special values
1081
Howard Hinnantcf3143c2012-07-13 19:17:27 +00001082 _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR duration zero() {return duration(duration_values<rep>::zero());}
1083 _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR duration min() {return duration(duration_values<rep>::min());}
1084 _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR duration max() {return duration(duration_values<rep>::max());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001085};
1086
1087typedef duration<long long, nano> nanoseconds;
1088typedef duration<long long, micro> microseconds;
1089typedef duration<long long, milli> milliseconds;
1090typedef duration<long long > seconds;
1091typedef duration< long, ratio< 60> > minutes;
1092typedef duration< long, ratio<3600> > hours;
1093
1094// Duration ==
1095
1096template <class _LhsDuration, class _RhsDuration>
1097struct __duration_eq
1098{
Howard Hinnantcf3143c2012-07-13 19:17:27 +00001099 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnant12967fb2013-06-28 18:09:35 +00001100 bool operator()(const _LhsDuration& __lhs, const _RhsDuration& __rhs) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00001101 {
1102 typedef typename common_type<_LhsDuration, _RhsDuration>::type _Ct;
1103 return _Ct(__lhs).count() == _Ct(__rhs).count();
1104 }
1105};
1106
1107template <class _LhsDuration>
1108struct __duration_eq<_LhsDuration, _LhsDuration>
1109{
Howard Hinnantcf3143c2012-07-13 19:17:27 +00001110 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnant12967fb2013-06-28 18:09:35 +00001111 bool operator()(const _LhsDuration& __lhs, const _LhsDuration& __rhs) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00001112 {return __lhs.count() == __rhs.count();}
1113};
1114
1115template <class _Rep1, class _Period1, class _Rep2, class _Period2>
1116inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantcf3143c2012-07-13 19:17:27 +00001117_LIBCPP_CONSTEXPR
Howard Hinnantc51e1022010-05-11 19:42:16 +00001118bool
1119operator==(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
1120{
1121 return __duration_eq<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >()(__lhs, __rhs);
1122}
1123
1124// Duration !=
1125
1126template <class _Rep1, class _Period1, class _Rep2, class _Period2>
1127inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantcf3143c2012-07-13 19:17:27 +00001128_LIBCPP_CONSTEXPR
Howard Hinnantc51e1022010-05-11 19:42:16 +00001129bool
1130operator!=(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
1131{
1132 return !(__lhs == __rhs);
1133}
1134
1135// Duration <
1136
1137template <class _LhsDuration, class _RhsDuration>
1138struct __duration_lt
1139{
Howard Hinnantcf3143c2012-07-13 19:17:27 +00001140 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnant12967fb2013-06-28 18:09:35 +00001141 bool operator()(const _LhsDuration& __lhs, const _RhsDuration& __rhs) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00001142 {
1143 typedef typename common_type<_LhsDuration, _RhsDuration>::type _Ct;
1144 return _Ct(__lhs).count() < _Ct(__rhs).count();
1145 }
1146};
1147
1148template <class _LhsDuration>
1149struct __duration_lt<_LhsDuration, _LhsDuration>
1150{
Howard Hinnantcf3143c2012-07-13 19:17:27 +00001151 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnant12967fb2013-06-28 18:09:35 +00001152 bool operator()(const _LhsDuration& __lhs, const _LhsDuration& __rhs) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00001153 {return __lhs.count() < __rhs.count();}
1154};
1155
1156template <class _Rep1, class _Period1, class _Rep2, class _Period2>
1157inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantcf3143c2012-07-13 19:17:27 +00001158_LIBCPP_CONSTEXPR
Howard Hinnantc51e1022010-05-11 19:42:16 +00001159bool
1160operator< (const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
1161{
1162 return __duration_lt<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >()(__lhs, __rhs);
1163}
1164
1165// Duration >
1166
1167template <class _Rep1, class _Period1, class _Rep2, class _Period2>
1168inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantcf3143c2012-07-13 19:17:27 +00001169_LIBCPP_CONSTEXPR
Howard Hinnantc51e1022010-05-11 19:42:16 +00001170bool
1171operator> (const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
1172{
1173 return __rhs < __lhs;
1174}
1175
1176// Duration <=
1177
1178template <class _Rep1, class _Period1, class _Rep2, class _Period2>
1179inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantcf3143c2012-07-13 19:17:27 +00001180_LIBCPP_CONSTEXPR
Howard Hinnantc51e1022010-05-11 19:42:16 +00001181bool
1182operator<=(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
1183{
1184 return !(__rhs < __lhs);
1185}
1186
1187// Duration >=
1188
1189template <class _Rep1, class _Period1, class _Rep2, class _Period2>
1190inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantcf3143c2012-07-13 19:17:27 +00001191_LIBCPP_CONSTEXPR
Howard Hinnantc51e1022010-05-11 19:42:16 +00001192bool
1193operator>=(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
1194{
1195 return !(__lhs < __rhs);
1196}
1197
1198// Duration +
1199
1200template <class _Rep1, class _Period1, class _Rep2, class _Period2>
1201inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantcf3143c2012-07-13 19:17:27 +00001202_LIBCPP_CONSTEXPR
Howard Hinnantc51e1022010-05-11 19:42:16 +00001203typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type
1204operator+(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
1205{
Howard Hinnantcf3143c2012-07-13 19:17:27 +00001206 typedef typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type _Cd;
1207 return _Cd(_Cd(__lhs).count() + _Cd(__rhs).count());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001208}
1209
1210// Duration -
1211
1212template <class _Rep1, class _Period1, class _Rep2, class _Period2>
1213inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantcf3143c2012-07-13 19:17:27 +00001214_LIBCPP_CONSTEXPR
Howard Hinnantc51e1022010-05-11 19:42:16 +00001215typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type
1216operator-(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
1217{
Howard Hinnantcf3143c2012-07-13 19:17:27 +00001218 typedef typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type _Cd;
1219 return _Cd(_Cd(__lhs).count() - _Cd(__rhs).count());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001220}
1221
1222// Duration *
1223
1224template <class _Rep1, class _Period, class _Rep2>
1225inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantcf3143c2012-07-13 19:17:27 +00001226_LIBCPP_CONSTEXPR
Howard Hinnantc51e1022010-05-11 19:42:16 +00001227typename enable_if
1228<
1229 is_convertible<_Rep2, typename common_type<_Rep1, _Rep2>::type>::value,
1230 duration<typename common_type<_Rep1, _Rep2>::type, _Period>
1231>::type
1232operator*(const duration<_Rep1, _Period>& __d, const _Rep2& __s)
1233{
1234 typedef typename common_type<_Rep1, _Rep2>::type _Cr;
Howard Hinnantcf3143c2012-07-13 19:17:27 +00001235 typedef duration<_Cr, _Period> _Cd;
1236 return _Cd(_Cd(__d).count() * static_cast<_Cr>(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001237}
1238
1239template <class _Rep1, class _Period, class _Rep2>
1240inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantcf3143c2012-07-13 19:17:27 +00001241_LIBCPP_CONSTEXPR
Howard Hinnantc51e1022010-05-11 19:42:16 +00001242typename enable_if
1243<
1244 is_convertible<_Rep1, typename common_type<_Rep1, _Rep2>::type>::value,
1245 duration<typename common_type<_Rep1, _Rep2>::type, _Period>
1246>::type
1247operator*(const _Rep1& __s, const duration<_Rep2, _Period>& __d)
1248{
1249 return __d * __s;
1250}
1251
1252// Duration /
1253
1254template <class _Duration, class _Rep, bool = __is_duration<_Rep>::value>
1255struct __duration_divide_result
1256{
1257};
1258
1259template <class _Duration, class _Rep2,
1260 bool = is_convertible<_Rep2,
1261 typename common_type<typename _Duration::rep, _Rep2>::type>::value>
1262struct __duration_divide_imp
1263{
1264};
1265
1266template <class _Rep1, class _Period, class _Rep2>
1267struct __duration_divide_imp<duration<_Rep1, _Period>, _Rep2, true>
1268{
1269 typedef duration<typename common_type<_Rep1, _Rep2>::type, _Period> type;
1270};
1271
1272template <class _Rep1, class _Period, class _Rep2>
1273struct __duration_divide_result<duration<_Rep1, _Period>, _Rep2, false>
1274 : __duration_divide_imp<duration<_Rep1, _Period>, _Rep2>
1275{
1276};
1277
1278template <class _Rep1, class _Period, class _Rep2>
1279inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantcf3143c2012-07-13 19:17:27 +00001280_LIBCPP_CONSTEXPR
Howard Hinnantc51e1022010-05-11 19:42:16 +00001281typename __duration_divide_result<duration<_Rep1, _Period>, _Rep2>::type
1282operator/(const duration<_Rep1, _Period>& __d, const _Rep2& __s)
1283{
1284 typedef typename common_type<_Rep1, _Rep2>::type _Cr;
Howard Hinnantcf3143c2012-07-13 19:17:27 +00001285 typedef duration<_Cr, _Period> _Cd;
1286 return _Cd(_Cd(__d).count() / static_cast<_Cr>(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001287}
1288
1289template <class _Rep1, class _Period1, class _Rep2, class _Period2>
1290inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantcf3143c2012-07-13 19:17:27 +00001291_LIBCPP_CONSTEXPR
Howard Hinnantc51e1022010-05-11 19:42:16 +00001292typename common_type<_Rep1, _Rep2>::type
1293operator/(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
1294{
1295 typedef typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type _Ct;
1296 return _Ct(__lhs).count() / _Ct(__rhs).count();
1297}
1298
1299// Duration %
1300
1301template <class _Rep1, class _Period, class _Rep2>
1302inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantcf3143c2012-07-13 19:17:27 +00001303_LIBCPP_CONSTEXPR
Howard Hinnantc51e1022010-05-11 19:42:16 +00001304typename __duration_divide_result<duration<_Rep1, _Period>, _Rep2>::type
1305operator%(const duration<_Rep1, _Period>& __d, const _Rep2& __s)
1306{
1307 typedef typename common_type<_Rep1, _Rep2>::type _Cr;
Howard Hinnantcf3143c2012-07-13 19:17:27 +00001308 typedef duration<_Cr, _Period> _Cd;
1309 return _Cd(_Cd(__d).count() % static_cast<_Cr>(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001310}
1311
1312template <class _Rep1, class _Period1, class _Rep2, class _Period2>
1313inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantcf3143c2012-07-13 19:17:27 +00001314_LIBCPP_CONSTEXPR
Howard Hinnantc51e1022010-05-11 19:42:16 +00001315typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type
1316operator%(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
1317{
Howard Hinnantcf3143c2012-07-13 19:17:27 +00001318 typedef typename common_type<_Rep1, _Rep2>::type _Cr;
1319 typedef typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type _Cd;
1320 return _Cd(static_cast<_Cr>(_Cd(__lhs).count()) % static_cast<_Cr>(_Cd(__rhs).count()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001321}
1322
1323//////////////////////////////////////////////////////////
1324///////////////////// time_point /////////////////////////
1325//////////////////////////////////////////////////////////
1326
1327template <class _Clock, class _Duration = typename _Clock::duration>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001328class _LIBCPP_TEMPLATE_VIS time_point
Howard Hinnantc51e1022010-05-11 19:42:16 +00001329{
1330 static_assert(__is_duration<_Duration>::value,
1331 "Second template parameter of time_point must be a std::chrono::duration");
1332public:
1333 typedef _Clock clock;
1334 typedef _Duration duration;
1335 typedef typename duration::rep rep;
1336 typedef typename duration::period period;
1337private:
1338 duration __d_;
1339
1340public:
Marshall Clow5c459c92013-07-31 19:32:19 +00001341 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 time_point() : __d_(duration::zero()) {}
1342 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 explicit time_point(const duration& __d) : __d_(__d) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001343
1344 // conversions
1345 template <class _Duration2>
Marshall Clow5c459c92013-07-31 19:32:19 +00001346 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantc51e1022010-05-11 19:42:16 +00001347 time_point(const time_point<clock, _Duration2>& t,
1348 typename enable_if
1349 <
1350 is_convertible<_Duration2, duration>::value
1351 >::type* = 0)
1352 : __d_(t.time_since_epoch()) {}
1353
1354 // observer
1355
Marshall Clow5c459c92013-07-31 19:32:19 +00001356 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 duration time_since_epoch() const {return __d_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001357
1358 // arithmetic
1359
Marshall Clowdf247eb2018-08-29 23:02:15 +00001360 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 time_point& operator+=(const duration& __d) {__d_ += __d; return *this;}
1361 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 time_point& operator-=(const duration& __d) {__d_ -= __d; return *this;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001362
1363 // special values
1364
Howard Hinnantcf3143c2012-07-13 19:17:27 +00001365 _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR time_point min() {return time_point(duration::min());}
1366 _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR time_point max() {return time_point(duration::max());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001367};
1368
1369} // chrono
1370
1371template <class _Clock, class _Duration1, class _Duration2>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001372struct _LIBCPP_TEMPLATE_VIS common_type<chrono::time_point<_Clock, _Duration1>,
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00001373 chrono::time_point<_Clock, _Duration2> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001374{
1375 typedef chrono::time_point<_Clock, typename common_type<_Duration1, _Duration2>::type> type;
1376};
1377
1378namespace chrono {
1379
1380template <class _ToDuration, class _Clock, class _Duration>
Marshall Clow5c459c92013-07-31 19:32:19 +00001381inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantc51e1022010-05-11 19:42:16 +00001382time_point<_Clock, _ToDuration>
1383time_point_cast(const time_point<_Clock, _Duration>& __t)
1384{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001385 return time_point<_Clock, _ToDuration>(_VSTD::chrono::duration_cast<_ToDuration>(__t.time_since_epoch()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001386}
1387
Marshall Clowf2eabaf2015-11-05 19:33:59 +00001388#if _LIBCPP_STD_VER > 14
1389template <class _ToDuration, class _Clock, class _Duration>
1390inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
1391typename enable_if
1392<
1393 __is_duration<_ToDuration>::value,
1394 time_point<_Clock, _ToDuration>
1395>::type
1396floor(const time_point<_Clock, _Duration>& __t)
1397{
1398 return time_point<_Clock, _ToDuration>{floor<_ToDuration>(__t.time_since_epoch())};
1399}
1400
1401template <class _ToDuration, class _Clock, class _Duration>
1402inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
1403typename enable_if
1404<
1405 __is_duration<_ToDuration>::value,
1406 time_point<_Clock, _ToDuration>
1407>::type
1408ceil(const time_point<_Clock, _Duration>& __t)
1409{
1410 return time_point<_Clock, _ToDuration>{ceil<_ToDuration>(__t.time_since_epoch())};
1411}
1412
1413template <class _ToDuration, class _Clock, class _Duration>
1414inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
1415typename enable_if
1416<
1417 __is_duration<_ToDuration>::value,
1418 time_point<_Clock, _ToDuration>
1419>::type
1420round(const time_point<_Clock, _Duration>& __t)
1421{
1422 return time_point<_Clock, _ToDuration>{round<_ToDuration>(__t.time_since_epoch())};
1423}
1424
1425template <class _Rep, class _Period>
1426inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
1427typename enable_if
1428<
1429 numeric_limits<_Rep>::is_signed,
1430 duration<_Rep, _Period>
1431>::type
1432abs(duration<_Rep, _Period> __d)
1433{
1434 return __d >= __d.zero() ? __d : -__d;
1435}
1436#endif
1437
Howard Hinnantc51e1022010-05-11 19:42:16 +00001438// time_point ==
1439
1440template <class _Clock, class _Duration1, class _Duration2>
Marshall Clow5c459c92013-07-31 19:32:19 +00001441inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantc51e1022010-05-11 19:42:16 +00001442bool
1443operator==(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs)
1444{
1445 return __lhs.time_since_epoch() == __rhs.time_since_epoch();
1446}
1447
1448// time_point !=
1449
1450template <class _Clock, class _Duration1, class _Duration2>
Marshall Clow5c459c92013-07-31 19:32:19 +00001451inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantc51e1022010-05-11 19:42:16 +00001452bool
1453operator!=(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs)
1454{
1455 return !(__lhs == __rhs);
1456}
1457
1458// time_point <
1459
1460template <class _Clock, class _Duration1, class _Duration2>
Marshall Clow5c459c92013-07-31 19:32:19 +00001461inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantc51e1022010-05-11 19:42:16 +00001462bool
1463operator<(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs)
1464{
1465 return __lhs.time_since_epoch() < __rhs.time_since_epoch();
1466}
1467
1468// time_point >
1469
1470template <class _Clock, class _Duration1, class _Duration2>
Marshall Clow5c459c92013-07-31 19:32:19 +00001471inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantc51e1022010-05-11 19:42:16 +00001472bool
1473operator>(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs)
1474{
1475 return __rhs < __lhs;
1476}
1477
1478// time_point <=
1479
1480template <class _Clock, class _Duration1, class _Duration2>
Marshall Clow5c459c92013-07-31 19:32:19 +00001481inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantc51e1022010-05-11 19:42:16 +00001482bool
1483operator<=(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs)
1484{
1485 return !(__rhs < __lhs);
1486}
1487
1488// time_point >=
1489
1490template <class _Clock, class _Duration1, class _Duration2>
Marshall Clow5c459c92013-07-31 19:32:19 +00001491inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantc51e1022010-05-11 19:42:16 +00001492bool
1493operator>=(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs)
1494{
1495 return !(__lhs < __rhs);
1496}
1497
1498// time_point operator+(time_point x, duration y);
1499
1500template <class _Clock, class _Duration1, class _Rep2, class _Period2>
Marshall Clow5c459c92013-07-31 19:32:19 +00001501inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantc51e1022010-05-11 19:42:16 +00001502time_point<_Clock, typename common_type<_Duration1, duration<_Rep2, _Period2> >::type>
1503operator+(const time_point<_Clock, _Duration1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
1504{
1505 typedef time_point<_Clock, typename common_type<_Duration1, duration<_Rep2, _Period2> >::type> _Tr;
Marshall Clow5c459c92013-07-31 19:32:19 +00001506 return _Tr (__lhs.time_since_epoch() + __rhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001507}
1508
1509// time_point operator+(duration x, time_point y);
1510
1511template <class _Rep1, class _Period1, class _Clock, class _Duration2>
Marshall Clow5c459c92013-07-31 19:32:19 +00001512inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantc51e1022010-05-11 19:42:16 +00001513time_point<_Clock, typename common_type<duration<_Rep1, _Period1>, _Duration2>::type>
1514operator+(const duration<_Rep1, _Period1>& __lhs, const time_point<_Clock, _Duration2>& __rhs)
1515{
1516 return __rhs + __lhs;
1517}
1518
1519// time_point operator-(time_point x, duration y);
1520
1521template <class _Clock, class _Duration1, class _Rep2, class _Period2>
Marshall Clow5c459c92013-07-31 19:32:19 +00001522inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantc51e1022010-05-11 19:42:16 +00001523time_point<_Clock, typename common_type<_Duration1, duration<_Rep2, _Period2> >::type>
1524operator-(const time_point<_Clock, _Duration1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
1525{
Marshall Clow78dbe462016-11-14 18:22:19 +00001526 typedef time_point<_Clock, typename common_type<_Duration1, duration<_Rep2, _Period2> >::type> _Ret;
1527 return _Ret(__lhs.time_since_epoch() -__rhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001528}
1529
1530// duration operator-(time_point x, time_point y);
1531
1532template <class _Clock, class _Duration1, class _Duration2>
Marshall Clow5c459c92013-07-31 19:32:19 +00001533inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantc51e1022010-05-11 19:42:16 +00001534typename common_type<_Duration1, _Duration2>::type
1535operator-(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs)
1536{
1537 return __lhs.time_since_epoch() - __rhs.time_since_epoch();
1538}
1539
1540//////////////////////////////////////////////////////////
1541/////////////////////// clocks ///////////////////////////
1542//////////////////////////////////////////////////////////
1543
Howard Hinnant8331b762013-03-06 23:30:19 +00001544class _LIBCPP_TYPE_VIS system_clock
Howard Hinnantc51e1022010-05-11 19:42:16 +00001545{
1546public:
1547 typedef microseconds duration;
1548 typedef duration::rep rep;
1549 typedef duration::period period;
1550 typedef chrono::time_point<system_clock> time_point;
Marshall Clow5c459c92013-07-31 19:32:19 +00001551 static _LIBCPP_CONSTEXPR_AFTER_CXX11 const bool is_steady = false;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001552
Howard Hinnantaa54ac42011-05-28 18:34:36 +00001553 static time_point now() _NOEXCEPT;
1554 static time_t to_time_t (const time_point& __t) _NOEXCEPT;
1555 static time_point from_time_t(time_t __t) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001556};
1557
Jonathan Roelofscce96eb2014-09-02 21:14:38 +00001558#ifndef _LIBCPP_HAS_NO_MONOTONIC_CLOCK
Howard Hinnant8331b762013-03-06 23:30:19 +00001559class _LIBCPP_TYPE_VIS steady_clock
Howard Hinnantc51e1022010-05-11 19:42:16 +00001560{
1561public:
1562 typedef nanoseconds duration;
1563 typedef duration::rep rep;
1564 typedef duration::period period;
Howard Hinnantc8dbd222010-11-20 19:16:30 +00001565 typedef chrono::time_point<steady_clock, duration> time_point;
Marshall Clow5c459c92013-07-31 19:32:19 +00001566 static _LIBCPP_CONSTEXPR_AFTER_CXX11 const bool is_steady = true;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001567
Howard Hinnantaa54ac42011-05-28 18:34:36 +00001568 static time_point now() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001569};
1570
Howard Hinnantc8dbd222010-11-20 19:16:30 +00001571typedef steady_clock high_resolution_clock;
Jonathan Roelofscce96eb2014-09-02 21:14:38 +00001572#else
1573typedef system_clock high_resolution_clock;
1574#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001575
1576} // chrono
1577
Marshall Clowac868372013-10-05 21:18:32 +00001578#if _LIBCPP_STD_VER > 11
1579// Suffixes for duration literals [time.duration.literals]
1580inline namespace literals
Louis Dionne44bcff92018-08-03 22:36:53 +00001581{
Marshall Cloweb41e5c2013-07-24 21:18:14 +00001582 inline namespace chrono_literals
1583 {
1584
Marshall Clow2ac805a2017-08-09 15:42:50 +00001585 constexpr chrono::hours operator""h(unsigned long long __h)
Howard Hinnant5c167562013-08-07 19:39:48 +00001586 {
1587 return chrono::hours(static_cast<chrono::hours::rep>(__h));
1588 }
Marshall Cloweb41e5c2013-07-24 21:18:14 +00001589
Marshall Clow2ac805a2017-08-09 15:42:50 +00001590 constexpr chrono::duration<long double, ratio<3600,1>> operator""h(long double __h)
Howard Hinnant5c167562013-08-07 19:39:48 +00001591 {
1592 return chrono::duration<long double, ratio<3600,1>>(__h);
1593 }
Marshall Cloweb41e5c2013-07-24 21:18:14 +00001594
1595
Marshall Clow2ac805a2017-08-09 15:42:50 +00001596 constexpr chrono::minutes operator""min(unsigned long long __m)
Howard Hinnant5c167562013-08-07 19:39:48 +00001597 {
1598 return chrono::minutes(static_cast<chrono::minutes::rep>(__m));
1599 }
Marshall Cloweb41e5c2013-07-24 21:18:14 +00001600
Marshall Clow2ac805a2017-08-09 15:42:50 +00001601 constexpr chrono::duration<long double, ratio<60,1>> operator""min(long double __m)
Howard Hinnant5c167562013-08-07 19:39:48 +00001602 {
1603 return chrono::duration<long double, ratio<60,1>> (__m);
1604 }
Marshall Cloweb41e5c2013-07-24 21:18:14 +00001605
1606
Marshall Clow2ac805a2017-08-09 15:42:50 +00001607 constexpr chrono::seconds operator""s(unsigned long long __s)
Howard Hinnant5c167562013-08-07 19:39:48 +00001608 {
1609 return chrono::seconds(static_cast<chrono::seconds::rep>(__s));
1610 }
Marshall Cloweb41e5c2013-07-24 21:18:14 +00001611
Marshall Clow2ac805a2017-08-09 15:42:50 +00001612 constexpr chrono::duration<long double> operator""s(long double __s)
Howard Hinnant5c167562013-08-07 19:39:48 +00001613 {
1614 return chrono::duration<long double> (__s);
1615 }
Marshall Cloweb41e5c2013-07-24 21:18:14 +00001616
1617
Marshall Clow2ac805a2017-08-09 15:42:50 +00001618 constexpr chrono::milliseconds operator""ms(unsigned long long __ms)
Howard Hinnant5c167562013-08-07 19:39:48 +00001619 {
1620 return chrono::milliseconds(static_cast<chrono::milliseconds::rep>(__ms));
1621 }
Marshall Cloweb41e5c2013-07-24 21:18:14 +00001622
Marshall Clow2ac805a2017-08-09 15:42:50 +00001623 constexpr chrono::duration<long double, milli> operator""ms(long double __ms)
Howard Hinnant5c167562013-08-07 19:39:48 +00001624 {
1625 return chrono::duration<long double, milli>(__ms);
1626 }
Marshall Cloweb41e5c2013-07-24 21:18:14 +00001627
1628
Marshall Clow2ac805a2017-08-09 15:42:50 +00001629 constexpr chrono::microseconds operator""us(unsigned long long __us)
Howard Hinnant5c167562013-08-07 19:39:48 +00001630 {
1631 return chrono::microseconds(static_cast<chrono::microseconds::rep>(__us));
1632 }
Marshall Cloweb41e5c2013-07-24 21:18:14 +00001633
Marshall Clow2ac805a2017-08-09 15:42:50 +00001634 constexpr chrono::duration<long double, micro> operator""us(long double __us)
Howard Hinnant5c167562013-08-07 19:39:48 +00001635 {
1636 return chrono::duration<long double, micro> (__us);
1637 }
Louis Dionne44bcff92018-08-03 22:36:53 +00001638
Marshall Cloweb41e5c2013-07-24 21:18:14 +00001639
Marshall Clow2ac805a2017-08-09 15:42:50 +00001640 constexpr chrono::nanoseconds operator""ns(unsigned long long __ns)
Howard Hinnant5c167562013-08-07 19:39:48 +00001641 {
1642 return chrono::nanoseconds(static_cast<chrono::nanoseconds::rep>(__ns));
1643 }
Marshall Cloweb41e5c2013-07-24 21:18:14 +00001644
Marshall Clow2ac805a2017-08-09 15:42:50 +00001645 constexpr chrono::duration<long double, nano> operator""ns(long double __ns)
Howard Hinnant5c167562013-08-07 19:39:48 +00001646 {
1647 return chrono::duration<long double, nano> (__ns);
1648 }
Marshall Cloweb41e5c2013-07-24 21:18:14 +00001649
1650}}
Marshall Clowac868372013-10-05 21:18:32 +00001651
1652namespace chrono { // hoist the literals into namespace std::chrono
1653 using namespace literals::chrono_literals;
1654}
1655
Marshall Cloweb41e5c2013-07-24 21:18:14 +00001656#endif
1657
Howard Hinnantc51e1022010-05-11 19:42:16 +00001658_LIBCPP_END_NAMESPACE_STD
1659
Eric Fiselierf4433a32017-05-31 22:07:49 +00001660_LIBCPP_POP_MACROS
1661
Howard Hinnantc51e1022010-05-11 19:42:16 +00001662#endif // _LIBCPP_CHRONO