blob: f0bd6aff2b7e5115d03e5505ad4f32849f5f47a9 [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 Clow5c87ad02017-01-04 23:03:24 +000080 constexpr duration& operator++();
81 constexpr duration operator++(int);
82 constexpr duration& operator--();
83 constexpr duration operator--(int);
Howard Hinnantc51e1022010-05-11 19:42:16 +000084
Marshall Clow5c87ad02017-01-04 23:03:24 +000085 constexpr duration& operator+=(const duration& d);
86 constexpr duration& operator-=(const duration& d);
Howard Hinnantc51e1022010-05-11 19:42:16 +000087
88 duration& operator*=(const rep& rhs);
89 duration& operator/=(const rep& rhs);
90
91 // special values
92
Howard Hinnantcf3143c2012-07-13 19:17:27 +000093 static constexpr duration zero();
94 static constexpr duration min();
95 static constexpr duration max();
Howard Hinnantc51e1022010-05-11 19:42:16 +000096};
97
98typedef duration<long long, nano> nanoseconds;
99typedef duration<long long, micro> microseconds;
100typedef duration<long long, milli> milliseconds;
101typedef duration<long long > seconds;
102typedef duration< long, ratio< 60> > minutes;
103typedef duration< long, ratio<3600> > hours;
104
105template <class Clock, class Duration = typename Clock::duration>
106class time_point
107{
108public:
109 typedef Clock clock;
110 typedef Duration duration;
111 typedef typename duration::rep rep;
112 typedef typename duration::period period;
113private:
114 duration d_; // exposition only
115
116public:
Marshall Clow5c459c92013-07-31 19:32:19 +0000117 time_point(); // has value "epoch" // constexpr in C++14
118 explicit time_point(const duration& d); // same as time_point() + d // constexpr in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000119
120 // conversions
121 template <class Duration2>
Marshall Clow5c459c92013-07-31 19:32:19 +0000122 time_point(const time_point<clock, Duration2>& t); // constexpr in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000123
124 // observer
125
Marshall Clow5c459c92013-07-31 19:32:19 +0000126 duration time_since_epoch() const; // constexpr in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000127
128 // arithmetic
129
130 time_point& operator+=(const duration& d);
131 time_point& operator-=(const duration& d);
132
133 // special values
134
135 static constexpr time_point min();
136 static constexpr time_point max();
137};
138
139} // chrono
140
141// common_type traits
142template <class Rep1, class Period1, class Rep2, class Period2>
143 struct common_type<chrono::duration<Rep1, Period1>, chrono::duration<Rep2, Period2>>;
144
145template <class Clock, class Duration1, class Duration2>
146 struct common_type<chrono::time_point<Clock, Duration1>, chrono::time_point<Clock, Duration2>>;
147
148namespace chrono {
149
Marshall Clow40ca0c12018-07-18 17:37:51 +0000150
151template<class T> struct is_clock; // C++20
152template<class T> inline constexpr bool is_clock_v = is_clock<T>::value; // C++20
153
154
Howard Hinnantc51e1022010-05-11 19:42:16 +0000155// duration arithmetic
156template <class Rep1, class Period1, class Rep2, class Period2>
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000157 constexpr
Howard Hinnantc51e1022010-05-11 19:42:16 +0000158 typename common_type<duration<Rep1, Period1>, duration<Rep2, Period2>>::type
159 operator+(const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs);
160template <class Rep1, class Period1, class Rep2, class Period2>
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000161 constexpr
Howard Hinnantc51e1022010-05-11 19:42:16 +0000162 typename common_type<duration<Rep1, Period1>, duration<Rep2, Period2>>::type
163 operator-(const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs);
164template <class Rep1, class Period, class Rep2>
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000165 constexpr
Howard Hinnantc51e1022010-05-11 19:42:16 +0000166 duration<typename common_type<Rep1, Rep2>::type, Period>
167 operator*(const duration<Rep1, Period>& d, const Rep2& s);
168template <class Rep1, class Period, class Rep2>
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000169 constexpr
Howard Hinnantc51e1022010-05-11 19:42:16 +0000170 duration<typename common_type<Rep1, Rep2>::type, Period>
171 operator*(const Rep1& s, const duration<Rep2, Period>& d);
172template <class Rep1, class Period, class Rep2>
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000173 constexpr
Howard Hinnantc51e1022010-05-11 19:42:16 +0000174 duration<typename common_type<Rep1, Rep2>::type, Period>
175 operator/(const duration<Rep1, Period>& d, const Rep2& s);
176template <class Rep1, class Period1, class Rep2, class Period2>
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000177 constexpr
Howard Hinnantc51e1022010-05-11 19:42:16 +0000178 typename common_type<Rep1, Rep2>::type
179 operator/(const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs);
180
181// duration comparisons
182template <class Rep1, class Period1, class Rep2, class Period2>
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000183 constexpr
Howard Hinnantc51e1022010-05-11 19:42:16 +0000184 bool operator==(const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs);
185template <class Rep1, class Period1, class Rep2, class Period2>
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000186 constexpr
Howard Hinnantc51e1022010-05-11 19:42:16 +0000187 bool operator!=(const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs);
188template <class Rep1, class Period1, class Rep2, class Period2>
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000189 constexpr
Howard Hinnantc51e1022010-05-11 19:42:16 +0000190 bool operator< (const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs);
191template <class Rep1, class Period1, class Rep2, class Period2>
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000192 constexpr
Howard Hinnantc51e1022010-05-11 19:42:16 +0000193 bool operator<=(const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs);
194template <class Rep1, class Period1, class Rep2, class Period2>
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000195 constexpr
Howard Hinnantc51e1022010-05-11 19:42:16 +0000196 bool operator> (const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs);
197template <class Rep1, class Period1, class Rep2, class Period2>
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000198 constexpr
Howard Hinnantc51e1022010-05-11 19:42:16 +0000199 bool operator>=(const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs);
200
201// duration_cast
202template <class ToDuration, class Rep, class Period>
203 ToDuration duration_cast(const duration<Rep, Period>& d);
204
Marshall Clowf2eabaf2015-11-05 19:33:59 +0000205template <class ToDuration, class Rep, class Period>
206 constexpr ToDuration floor(const duration<Rep, Period>& d); // C++17
207template <class ToDuration, class Rep, class Period>
208 constexpr ToDuration ceil(const duration<Rep, Period>& d); // C++17
209template <class ToDuration, class Rep, class Period>
210 constexpr ToDuration round(const duration<Rep, Period>& d); // C++17
211
Marshall Clow40ca0c12018-07-18 17:37:51 +0000212// duration I/O is elsewhere
213
Marshall Clow5c459c92013-07-31 19:32:19 +0000214// time_point arithmetic (all constexpr in C++14)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000215template <class Clock, class Duration1, class Rep2, class Period2>
216 time_point<Clock, typename common_type<Duration1, duration<Rep2, Period2>>::type>
217 operator+(const time_point<Clock, Duration1>& lhs, const duration<Rep2, Period2>& rhs);
218template <class Rep1, class Period1, class Clock, class Duration2>
219 time_point<Clock, typename common_type<duration<Rep1, Period1>, Duration2>::type>
220 operator+(const duration<Rep1, Period1>& lhs, const time_point<Clock, Duration2>& rhs);
221template <class Clock, class Duration1, class Rep2, class Period2>
222 time_point<Clock, typename common_type<Duration1, duration<Rep2, Period2>>::type>
223 operator-(const time_point<Clock, Duration1>& lhs, const duration<Rep2, Period2>& rhs);
224template <class Clock, class Duration1, class Duration2>
225 typename common_type<Duration1, Duration2>::type
226 operator-(const time_point<Clock, Duration1>& lhs, const time_point<Clock, Duration2>& rhs);
227
Marshall Clow5c459c92013-07-31 19:32:19 +0000228// time_point comparisons (all constexpr in C++14)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000229template <class Clock, class Duration1, class Duration2>
230 bool operator==(const time_point<Clock, Duration1>& lhs, const time_point<Clock, Duration2>& rhs);
231template <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);
241
Marshall Clow5c459c92013-07-31 19:32:19 +0000242// time_point_cast (constexpr in C++14)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000243
244template <class ToDuration, class Clock, class Duration>
245 time_point<Clock, ToDuration> time_point_cast(const time_point<Clock, Duration>& t);
246
Marshall Clowf2eabaf2015-11-05 19:33:59 +0000247template <class ToDuration, class Clock, class Duration>
248 constexpr time_point<Clock, ToDuration>
249 floor(const time_point<Clock, Duration>& tp); // C++17
250
251template <class ToDuration, class Clock, class Duration>
252 constexpr time_point<Clock, ToDuration>
253 ceil(const time_point<Clock, Duration>& tp); // C++17
254
255template <class ToDuration, class Clock, class Duration>
256 constexpr time_point<Clock, ToDuration>
257 round(const time_point<Clock, Duration>& tp); // C++17
258
259template <class Rep, class Period>
260 constexpr duration<Rep, Period> abs(duration<Rep, Period> d); // C++17
Marshall Clow40ca0c12018-07-18 17:37:51 +0000261
Howard Hinnantc51e1022010-05-11 19:42:16 +0000262// Clocks
263
264class system_clock
265{
266public:
267 typedef microseconds duration;
268 typedef duration::rep rep;
269 typedef duration::period period;
270 typedef chrono::time_point<system_clock> time_point;
Marshall Clow5c459c92013-07-31 19:32:19 +0000271 static const bool is_steady = false; // constexpr in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000272
Howard Hinnantaa54ac42011-05-28 18:34:36 +0000273 static time_point now() noexcept;
274 static time_t to_time_t (const time_point& __t) noexcept;
275 static time_point from_time_t(time_t __t) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000276};
277
Marshall Clow40ca0c12018-07-18 17:37:51 +0000278template <class Duration>
279 using sys_time = time_point<system_clock, Duration>; // C++20
280using sys_seconds = sys_time<seconds>; // C++20
281using sys_days = sys_time<days>; // C++20
282
283class utc_clock; // C++20
284
285template <class Duration>
286 using utc_time = time_point<utc_clock, Duration>; // C++20
287using utc_seconds = utc_time<seconds>; // C++20
288
289class tai_clock; // C++20
290
291template <class Duration>
292 using tai_time = time_point<tai_clock, Duration>; // C++20
293using tai_seconds = tai_time<seconds>; // C++20
294
295class file_clock; // C++20
296
297template<class Duration>
298 using file_time = time_point<file_clock, Duration>; // C++20
299
Howard Hinnantc8dbd222010-11-20 19:16:30 +0000300class steady_clock
Howard Hinnantc51e1022010-05-11 19:42:16 +0000301{
302public:
303 typedef nanoseconds duration;
304 typedef duration::rep rep;
305 typedef duration::period period;
Howard Hinnantc8dbd222010-11-20 19:16:30 +0000306 typedef chrono::time_point<steady_clock, duration> time_point;
Marshall Clow5c459c92013-07-31 19:32:19 +0000307 static const bool is_steady = true; // constexpr in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000308
Howard Hinnantaa54ac42011-05-28 18:34:36 +0000309 static time_point now() noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000310};
311
Howard Hinnantc8dbd222010-11-20 19:16:30 +0000312typedef steady_clock high_resolution_clock;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000313
Marshall Clow40ca0c12018-07-18 17:37:51 +0000314// 25.7.8, local time // C++20
315struct local_t {};
316template<class Duration>
317 using local_time = time_point<local_t, Duration>;
318using local_seconds = local_time<seconds>;
319using local_days = local_time<days>;
320
321// 25.7.9, time_point conversions template<class DestClock, class SourceClock> // C++20
322struct clock_time_conversion;
323
324template<class DestClock, class SourceClock, class Duration>
325 auto clock_cast(const time_point<SourceClock, Duration>& t);
Louis Dionne44bcff92018-08-03 22:36:53 +0000326
Marshall Clow40ca0c12018-07-18 17:37:51 +0000327// 25.8.2, class last_spec // C++20
328struct last_spec;
329
330// 25.8.3, class day // C++20
331
332class day;
333constexpr bool operator==(const day& x, const day& y) noexcept;
334constexpr bool operator!=(const day& x, const day& y) noexcept;
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 day operator+(const day& x, const days& y) noexcept;
340constexpr day operator+(const days& x, const day& y) noexcept;
341constexpr day operator-(const day& x, const days& y) noexcept;
342constexpr days operator-(const day& x, const day& y) noexcept;
343
344// 25.8.4, class month // C++20
345class month;
346constexpr bool operator==(const month& x, const month& y) noexcept;
347constexpr bool operator!=(const month& x, const month& y) noexcept;
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 month operator+(const month& x, const months& y) noexcept;
353constexpr month operator+(const months& x, const month& y) noexcept;
354constexpr month operator-(const month& x, const months& y) noexcept;
355constexpr months operator-(const month& x, const month& y) noexcept;
356
357// 25.8.5, class year // C++20
358class year;
359constexpr bool operator==(const year& x, const year& y) noexcept;
360constexpr bool operator!=(const year& x, const year& y) noexcept;
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 year operator+(const year& x, const years& y) noexcept;
366constexpr year operator+(const years& x, const year& y) noexcept;
367constexpr year operator-(const year& x, const years& y) noexcept;
368constexpr years operator-(const year& x, const year& y) noexcept;
369
370// 25.8.6, class weekday // C++20
371class weekday;
372
373constexpr bool operator==(const weekday& x, const weekday& y) noexcept;
374constexpr bool operator!=(const weekday& x, const weekday& y) noexcept;
375constexpr weekday operator+(const weekday& x, const days& y) noexcept;
376constexpr weekday operator+(const days& x, const weekday& y) noexcept;
377constexpr weekday operator-(const weekday& x, const days& y) noexcept;
378constexpr days operator-(const weekday& x, const weekday& y) noexcept;
379
380// 25.8.7, class weekday_indexed // C++20
381
382class weekday_indexed;
383constexpr bool operator==(const weekday_indexed& x, const weekday_indexed& y) noexcept;
384constexpr bool operator!=(const weekday_indexed& x, const weekday_indexed& y) noexcept;
385
386// 25.8.8, class weekday_last // C++20
387class weekday_last;
388
389constexpr bool operator==(const weekday_last& x, const weekday_last& y) noexcept;
390constexpr bool operator!=(const weekday_last& x, const weekday_last& y) noexcept;
391
392// 25.8.9, class month_day // C++20
393class month_day;
394
395constexpr bool operator==(const month_day& x, const month_day& y) noexcept;
396constexpr bool operator!=(const month_day& x, const month_day& y) noexcept;
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;
401
402
403// 25.8.10, class month_day_last // C++20
404class month_day_last;
405
406constexpr bool operator==(const month_day_last& x, const month_day_last& y) noexcept;
407constexpr bool operator!=(const month_day_last& x, const month_day_last& y) noexcept;
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;
412
413// 25.8.11, class month_weekday // C++20
414class month_weekday;
415
416constexpr bool operator==(const month_weekday& x, const month_weekday& y) noexcept;
417constexpr bool operator!=(const month_weekday& x, const month_weekday& y) noexcept;
418
419// 25.8.12, class month_weekday_last // C++20
420class month_weekday_last;
421
422constexpr bool operator==(const month_weekday_last& x, const month_weekday_last& y) noexcept;
423constexpr bool operator!=(const month_weekday_last& x, const month_weekday_last& y) noexcept;
424
425
426// 25.8.13, class year_month // C++20
427class year_month;
428
429constexpr bool operator==(const year_month& x, const year_month& y) noexcept;
430constexpr bool operator!=(const year_month& x, const year_month& y) noexcept;
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;
435
436constexpr year_month operator+(const year_month& ym, const months& dm) noexcept;
437constexpr year_month operator+(const months& dm, const year_month& ym) noexcept;
438constexpr year_month operator-(const year_month& ym, const months& dm) noexcept;
439constexpr months operator-(const year_month& x, const year_month& y) noexcept;
440constexpr year_month operator+(const year_month& ym, const years& dy) noexcept;
441constexpr year_month operator+(const years& dy, const year_month& ym) noexcept;
442constexpr year_month operator-(const year_month& ym, const years& dy) noexcept;
443
444// 25.8.14, class year_month_day class // C++20
445year_month_day;
446
447constexpr bool operator==(const year_month_day& x, const year_month_day& y) noexcept;
448constexpr bool operator!=(const year_month_day& x, const year_month_day& y) noexcept;
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;
453
454constexpr year_month_day operator+(const year_month_day& ymd, const months& dm) noexcept;
455constexpr year_month_day operator+(const months& dm, const year_month_day& ymd) noexcept;
456constexpr year_month_day operator+(const year_month_day& ymd, const years& dy) noexcept;
457constexpr year_month_day operator+(const years& dy, const year_month_day& ymd) noexcept;
458constexpr year_month_day operator-(const year_month_day& ymd, const months& dm) noexcept;
459constexpr year_month_day operator-(const year_month_day& ymd, const years& dy) noexcept;
460
461
462// 25.8.15, class year_month_day_last // C++20
463class year_month_day_last;
464
465constexpr bool operator==(const year_month_day_last& x,
466 const year_month_day_last& y) noexcept;
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;
477
478constexpr year_month_day_last
479 operator+(const year_month_day_last& ymdl, const months& dm) noexcept;
480constexpr year_month_day_last
481 operator+(const months& dm, const year_month_day_last& ymdl) noexcept;
482constexpr year_month_day_last
483 operator+(const year_month_day_last& ymdl, const years& dy) noexcept;
484constexpr year_month_day_last
485 operator+(const years& dy, const year_month_day_last& ymdl) noexcept;
486constexpr year_month_day_last
487 operator-(const year_month_day_last& ymdl, const months& dm) noexcept;
488constexpr year_month_day_last
489 operator-(const year_month_day_last& ymdl, const years& dy) noexcept;
490
491// 25.8.16, class year_month_weekday // C++20
492class year_month_weekday;
493
494constexpr bool operator==(const year_month_weekday& x,
495 const year_month_weekday& y) noexcept;
496constexpr bool operator!=(const year_month_weekday& x,
497 const year_month_weekday& y) noexcept;
498
499constexpr year_month_weekday
500 operator+(const year_month_weekday& ymwd, const months& dm) noexcept;
501constexpr year_month_weekday
502 operator+(const months& dm, const year_month_weekday& ymwd) noexcept;
503constexpr year_month_weekday
504 operator+(const year_month_weekday& ymwd, const years& dy) noexcept;
505constexpr year_month_weekday
506 operator+(const years& dy, const year_month_weekday& ymwd) noexcept;
507constexpr year_month_weekday
508 operator-(const year_month_weekday& ymwd, const months& dm) noexcept;
509constexpr year_month_weekday
510 operator-(const year_month_weekday& ymwd, const years& dy) noexcept;
511
512// 25.8.17, class year_month_weekday_last // C++20
513class year_month_weekday_last;
514
515constexpr bool operator==(const year_month_weekday_last& x,
516 const year_month_weekday_last& y) noexcept;
517constexpr bool operator!=(const year_month_weekday_last& x,
518 const year_month_weekday_last& y) noexcept;
519constexpr year_month_weekday_last
520 operator+(const year_month_weekday_last& ymwdl, const months& dm) noexcept;
521constexpr year_month_weekday_last
522 operator+(const months& dm, const year_month_weekday_last& ymwdl) noexcept;
523constexpr year_month_weekday_last
524 operator+(const year_month_weekday_last& ymwdl, const years& dy) noexcept;
525constexpr year_month_weekday_last
526 operator+(const years& dy, const year_month_weekday_last& ymwdl) noexcept;
527constexpr year_month_weekday_last
528 operator-(const year_month_weekday_last& ymwdl, const months& dm) noexcept;
529constexpr year_month_weekday_last
530 operator-(const year_month_weekday_last& ymwdl, const years& dy) noexcept;
Louis Dionne44bcff92018-08-03 22:36:53 +0000531
Marshall Clow40ca0c12018-07-18 17:37:51 +0000532// 25.8.18, civil calendar conventional syntax operators // C++20
533constexpr year_month
534 operator/(const year& y, const month& m) noexcept;
535constexpr year_month
536 operator/(const year& y, int m) noexcept;
537constexpr month_day
538 operator/(const month& m, const day& d) noexcept;
539constexpr month_day
540 operator/(const month& m, int d) noexcept;
541constexpr month_day
542 operator/(int m, const day& d) noexcept;
543constexpr month_day
544 operator/(const day& d, const month& m) noexcept;
545constexpr month_day
546 operator/(const day& d, int m) noexcept;
547constexpr month_day_last
548 operator/(const month& m, last_spec) noexcept;
549constexpr month_day_last
550 operator/(int m, last_spec) noexcept;
551constexpr month_day_last
552 operator/(last_spec, const month& m) noexcept;
553constexpr month_day_last
554 operator/(last_spec, int m) noexcept;
555constexpr month_weekday
556 operator/(const month& m, const weekday_indexed& wdi) noexcept;
557constexpr month_weekday
558 operator/(int m, const weekday_indexed& wdi) noexcept;
559constexpr month_weekday
560 operator/(const weekday_indexed& wdi, const month& m) noexcept;
561constexpr month_weekday
562 operator/(const weekday_indexed& wdi, int m) noexcept;
563constexpr month_weekday_last
564 operator/(const month& m, const weekday_last& wdl) noexcept;
565constexpr month_weekday_last
566 operator/(int m, const weekday_last& wdl) noexcept;
567constexpr month_weekday_last
568 operator/(const weekday_last& wdl, const month& m) noexcept;
569constexpr month_weekday_last
570 operator/(const weekday_last& wdl, int m) noexcept;
571constexpr year_month_day
572 operator/(const year_month& ym, const day& d) noexcept;
573constexpr year_month_day
574 operator/(const year_month& ym, int d) noexcept;
575constexpr year_month_day
576 operator/(const year& y, const month_day& md) noexcept;
577constexpr year_month_day
578 operator/(int y, const month_day& md) noexcept;
579constexpr year_month_day
580 operator/(const month_day& md, const year& y) noexcept;
581constexpr year_month_day
582 operator/(const month_day& md, int y) noexcept;
583constexpr year_month_day_last
584 operator/(const year_month& ym, last_spec) noexcept;
585constexpr year_month_day_last
586 operator/(const year& y, const month_day_last& mdl) noexcept;
587constexpr year_month_day_last
588 operator/(int y, const month_day_last& mdl) noexcept;
589constexpr year_month_day_last
590 operator/(const month_day_last& mdl, const year& y) noexcept;
591constexpr year_month_day_last
592 operator/(const month_day_last& mdl, int y) noexcept;
593constexpr year_month_weekday
594 operator/(const year_month& ym, const weekday_indexed& wdi) noexcept;
595constexpr year_month_weekday
596 operator/(const year& y, const month_weekday& mwd) noexcept;
597constexpr year_month_weekday
598 operator/(int y, const month_weekday& mwd) noexcept;
599constexpr year_month_weekday
600 operator/(const month_weekday& mwd, const year& y) noexcept;
601constexpr year_month_weekday
602 operator/(const month_weekday& mwd, int y) noexcept;
603constexpr year_month_weekday_last
604 operator/(const year_month& ym, const weekday_last& wdl) noexcept;
605constexpr year_month_weekday_last
606 operator/(const year& y, const month_weekday_last& mwdl) noexcept;
607constexpr year_month_weekday_last
608 operator/(int y, const month_weekday_last& mwdl) noexcept;
609constexpr year_month_weekday_last
610 operator/(const month_weekday_last& mwdl, const year& y) noexcept;
611constexpr year_month_weekday_last
Louis Dionne44bcff92018-08-03 22:36:53 +0000612 operator/(const month_weekday_last& mwdl, int y) noexcept;
Marshall Clow40ca0c12018-07-18 17:37:51 +0000613
614// 25.9, class template time_of_day // C++20
615template<class Duration> class time_of_day;
616
617template<> class time_of_day<hours>;
618template<> class time_of_day<minutes>;
619template<> class time_of_day<seconds>;
620template<class Rep, class Period> class time_of_day<duration<Rep, Period>>;
621
622// 25.10.2, time zone database // C++20
623struct tzdb;
624class tzdb_list;
625
626// 25.10.2.3, time zone database access // C++20
627const tzdb& get_tzdb();
628tzdb_list& get_tzdb_list();
629const time_zone* locate_zone(string_view tz_name);
630const time_zone* current_zone();
631
632// 25.10.2.4, remote time zone database support // C++20
633const tzdb& reload_tzdb();
634string remote_version();
635
636// 25.10.3, exception classes // C++20
637class nonexistent_local_time;
638class ambiguous_local_time;
639
640// 25.10.4, information classes // C++20
641struct sys_info;
642struct local_info;
Louis Dionne44bcff92018-08-03 22:36:53 +0000643
Marshall Clow40ca0c12018-07-18 17:37:51 +0000644// 25.10.5, class time_zone // C++20
645enum class choose {earliest, latest};
646class time_zone;
647bool operator==(const time_zone& x, const time_zone& y) noexcept;
648bool operator!=(const time_zone& x, const time_zone& y) noexcept;
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;
Louis Dionne44bcff92018-08-03 22:36:53 +0000653
Marshall Clow40ca0c12018-07-18 17:37:51 +0000654// 25.10.6, class template zoned_traits // C++20
655template<class T> struct zoned_traits;
656
657// 25.10.7, class template zoned_time // C++20
658template<class Duration, class TimeZonePtr = const time_zone*> class zoned_time;
659using zoned_seconds = zoned_time<seconds>;
660
661template<class Duration1, class Duration2, class TimeZonePtr>
662 bool operator==(const zoned_time<Duration1, TimeZonePtr>& x,
663 const zoned_time<Duration2, TimeZonePtr>& y);
664template<class Duration1, class Duration2, class TimeZonePtr>
665 bool operator!=(const zoned_time<Duration1, TimeZonePtr>& x,
666 const zoned_time<Duration2, TimeZonePtr>& y);
667
668// 25.10.8, leap second support // C++20
669class leap;
670
671bool operator==(const leap& x, const leap& y);
672bool operator!=(const leap& x, const leap& y);
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);
677template<class Duration>
678 bool operator==(const leap& x, const sys_time<Duration>& y);
679template<class Duration>
680 bool operator==(const sys_time<Duration>& x, const leap& y);
681template<class Duration>
682 bool operator!=(const leap& x, const sys_time<Duration>& y);
683template<class Duration>
684 bool operator!=(const sys_time<Duration>& x, const leap& y);
685template<class Duration>
686 bool operator< (const leap& x, const sys_time<Duration>& y);
687template<class Duration>
688 bool operator< (const sys_time<Duration>& x, const leap& y);
689template<class Duration>
690 bool operator> (const leap& x, const sys_time<Duration>& y);
691template<class Duration>
692 bool operator> (const sys_time<Duration>& x, const leap& y);
693template<class Duration>
694 bool operator<=(const leap& x, const sys_time<Duration>& y);
695template<class Duration>
696 bool operator<=(const sys_time<Duration>& x, const leap& y);
697template<class Duration>
698 bool operator>=(const leap& x, const sys_time<Duration>& y);
699template<class Duration>
700 bool operator>=(const sys_time<Duration>& x, const leap& y);
701
702// 25.10.9, class link // C++20
703class link;
704bool operator==(const link& x, const link& y);
705bool operator!=(const link& x, const link& y);
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);
710
711// 25.11, formatting // C++20
712template<class charT, class Streamable>
713 basic_string<charT>
714 format(const charT* fmt, const Streamable& s);
715
716template<class charT, class Streamable>
717 basic_string<charT>
718 format(const locale& loc, const charT* fmt, const Streamable& s);
719
720template<class charT, class traits, class Alloc, class Streamable>
721 basic_string<charT, traits, Alloc>
722 format(const basic_string<charT, traits, Alloc>& fmt, const Streamable& s);
723
724template<class charT, class traits, class Alloc, class Streamable>
725 basic_string<charT, traits, Alloc>
726 format(const locale& loc, const basic_string<charT, traits, Alloc>& fmt,
Louis Dionne44bcff92018-08-03 22:36:53 +0000727 const Streamable& s);
Marshall Clow40ca0c12018-07-18 17:37:51 +0000728
729// 25.12, parsing // C++20
730template<class charT, class traits, class Alloc, class Parsable>
731unspecified
732 parse(const basic_string<charT, traits, Alloc>& format, Parsable& tp);
733
734template<class charT, class traits, class Alloc, class Parsable>
735unspecified
736 parse(const basic_string<charT, traits, Alloc>& format, Parsable& tp,
737 basic_string<charT, traits, Alloc>& abbrev);
738
739template<class charT, class traits, class Alloc, class Parsable>
740unspecified
741 parse(const basic_string<charT, traits, Alloc>& format, Parsable& tp,
742 minutes& offset);
743
744template<class charT, class traits, class Alloc, class Parsable>
745unspecified
746 parse(const basic_string<charT, traits, Alloc>& format, Parsable& tp,
747 basic_string<charT, traits, Alloc>& abbrev, minutes& offset);
748
Louis Dionne44bcff92018-08-03 22:36:53 +0000749inline constexpr last_spec last{}; // C++20
Marshall Clow40ca0c12018-07-18 17:37:51 +0000750inline constexpr chrono::weekday Sunday{0}; // C++20
751inline constexpr chrono::weekday Monday{1}; // C++20
752inline constexpr chrono::weekday Tuesday{2}; // C++20
753inline constexpr chrono::weekday Wednesday{3}; // C++20
754inline constexpr chrono::weekday Thursday{4}; // C++20
755inline constexpr chrono::weekday Friday{5}; // C++20
756inline constexpr chrono::weekday Saturday{6}; // C++20
757
758inline constexpr chrono::month January{1}; // C++20
759inline constexpr chrono::month February{2}; // C++20
760inline constexpr chrono::month March{3}; // C++20
761inline constexpr chrono::month April{4}; // C++20
762inline constexpr chrono::month May{5}; // C++20
763inline constexpr chrono::month June{6}; // C++20
764inline constexpr chrono::month July{7}; // C++20
765inline constexpr chrono::month August{8}; // C++20
766inline constexpr chrono::month September{9}; // C++20
767inline constexpr chrono::month October{10}; // C++20
768inline constexpr chrono::month November{11}; // C++20
769inline constexpr chrono::month December{12}; // C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000770} // chrono
771
Marshall Clow40ca0c12018-07-18 17:37:51 +0000772inline namespace literals {
773 inline namespace chrono_literals {
Marshall Clow2ac805a2017-08-09 15:42:50 +0000774constexpr chrono::hours operator ""h(unsigned long long); // C++14
775constexpr chrono::duration<unspecified , ratio<3600,1>> operator ""h(long double); // C++14
776constexpr chrono::minutes operator ""min(unsigned long long); // C++14
777constexpr chrono::duration<unspecified , ratio<60,1>> operator ""min(long double); // C++14
778constexpr chrono::seconds operator ""s(unsigned long long); // C++14
779constexpr chrono::duration<unspecified > operator ""s(long double); // C++14
780constexpr chrono::milliseconds operator ""ms(unsigned long long); // C++14
781constexpr chrono::duration<unspecified , milli> operator ""ms(long double); // C++14
782constexpr chrono::microseconds operator ""us(unsigned long long); // C++14
783constexpr chrono::duration<unspecified , micro> operator ""us(long double); // C++14
784constexpr chrono::nanoseconds operator ""ns(unsigned long long); // C++14
785constexpr chrono::duration<unspecified , nano> operator ""ns(long double); // C++14
Marshall Clow40ca0c12018-07-18 17:37:51 +0000786constexpr chrono::day operator ""d(unsigned long long d) noexcept; // C++20
787constexpr chrono::year operator ""y(unsigned long long y) noexcept; // C++20
788} // chrono_literals
789} // literals
Marshall Cloweb41e5c2013-07-24 21:18:14 +0000790
Howard Hinnantc51e1022010-05-11 19:42:16 +0000791} // std
792*/
793
794#include <__config>
795#include <ctime>
796#include <type_traits>
797#include <ratio>
798#include <limits>
799
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000800#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000801#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000802#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000803
Eric Fiselierf4433a32017-05-31 22:07:49 +0000804_LIBCPP_PUSH_MACROS
805#include <__undef_macros>
806
807
Howard Hinnantc51e1022010-05-11 19:42:16 +0000808_LIBCPP_BEGIN_NAMESPACE_STD
809
810namespace chrono
811{
812
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000813template <class _Rep, class _Period = ratio<1> > class _LIBCPP_TEMPLATE_VIS duration;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000814
Howard Hinnantf39463b2010-05-18 17:32:30 +0000815template <class _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000816struct __is_duration : false_type {};
817
818template <class _Rep, class _Period>
819struct __is_duration<duration<_Rep, _Period> > : true_type {};
820
821template <class _Rep, class _Period>
822struct __is_duration<const duration<_Rep, _Period> > : true_type {};
823
824template <class _Rep, class _Period>
825struct __is_duration<volatile duration<_Rep, _Period> > : true_type {};
826
827template <class _Rep, class _Period>
828struct __is_duration<const volatile duration<_Rep, _Period> > : true_type {};
829
830} // chrono
831
832template <class _Rep1, class _Period1, class _Rep2, class _Period2>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000833struct _LIBCPP_TEMPLATE_VIS common_type<chrono::duration<_Rep1, _Period1>,
Howard Hinnanta37d3cf2013-08-12 18:38:34 +0000834 chrono::duration<_Rep2, _Period2> >
Howard Hinnantc51e1022010-05-11 19:42:16 +0000835{
836 typedef chrono::duration<typename common_type<_Rep1, _Rep2>::type,
837 typename __ratio_gcd<_Period1, _Period2>::type> type;
838};
839
840namespace chrono {
841
842// duration_cast
843
844template <class _FromDuration, class _ToDuration,
845 class _Period = typename ratio_divide<typename _FromDuration::period, typename _ToDuration::period>::type,
846 bool = _Period::num == 1,
847 bool = _Period::den == 1>
848struct __duration_cast;
849
850template <class _FromDuration, class _ToDuration, class _Period>
851struct __duration_cast<_FromDuration, _ToDuration, _Period, true, true>
852{
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000853 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnantc51e1022010-05-11 19:42:16 +0000854 _ToDuration operator()(const _FromDuration& __fd) const
855 {
856 return _ToDuration(static_cast<typename _ToDuration::rep>(__fd.count()));
857 }
858};
859
860template <class _FromDuration, class _ToDuration, class _Period>
861struct __duration_cast<_FromDuration, _ToDuration, _Period, true, false>
862{
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000863 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnantc51e1022010-05-11 19:42:16 +0000864 _ToDuration operator()(const _FromDuration& __fd) const
865 {
866 typedef typename common_type<typename _ToDuration::rep, typename _FromDuration::rep, intmax_t>::type _Ct;
867 return _ToDuration(static_cast<typename _ToDuration::rep>(
868 static_cast<_Ct>(__fd.count()) / static_cast<_Ct>(_Period::den)));
869 }
870};
871
872template <class _FromDuration, class _ToDuration, class _Period>
873struct __duration_cast<_FromDuration, _ToDuration, _Period, false, true>
874{
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000875 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnantc51e1022010-05-11 19:42:16 +0000876 _ToDuration operator()(const _FromDuration& __fd) const
877 {
878 typedef typename common_type<typename _ToDuration::rep, typename _FromDuration::rep, intmax_t>::type _Ct;
879 return _ToDuration(static_cast<typename _ToDuration::rep>(
880 static_cast<_Ct>(__fd.count()) * static_cast<_Ct>(_Period::num)));
881 }
882};
883
884template <class _FromDuration, class _ToDuration, class _Period>
885struct __duration_cast<_FromDuration, _ToDuration, _Period, false, false>
886{
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000887 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnantc51e1022010-05-11 19:42:16 +0000888 _ToDuration operator()(const _FromDuration& __fd) const
889 {
890 typedef typename common_type<typename _ToDuration::rep, typename _FromDuration::rep, intmax_t>::type _Ct;
891 return _ToDuration(static_cast<typename _ToDuration::rep>(
892 static_cast<_Ct>(__fd.count()) * static_cast<_Ct>(_Period::num)
893 / static_cast<_Ct>(_Period::den)));
894 }
895};
896
897template <class _ToDuration, class _Rep, class _Period>
898inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000899_LIBCPP_CONSTEXPR
Howard Hinnantc51e1022010-05-11 19:42:16 +0000900typename enable_if
901<
902 __is_duration<_ToDuration>::value,
903 _ToDuration
904>::type
905duration_cast(const duration<_Rep, _Period>& __fd)
906{
907 return __duration_cast<duration<_Rep, _Period>, _ToDuration>()(__fd);
908}
909
Howard Hinnant874ad9a2010-09-21 21:28:23 +0000910template <class _Rep>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000911struct _LIBCPP_TEMPLATE_VIS treat_as_floating_point : is_floating_point<_Rep> {};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000912
Marshall Clow59581f02015-11-30 05:39:30 +0000913#if _LIBCPP_STD_VER > 14 && !defined(_LIBCPP_HAS_NO_VARIABLE_TEMPLATES)
Marshall Clowf1bf62f2018-01-02 17:17:01 +0000914template <class _Rep>
915_LIBCPP_INLINE_VAR _LIBCPP_CONSTEXPR bool treat_as_floating_point_v
Marshall Clow59581f02015-11-30 05:39:30 +0000916 = treat_as_floating_point<_Rep>::value;
917#endif
918
Howard Hinnantc51e1022010-05-11 19:42:16 +0000919template <class _Rep>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000920struct _LIBCPP_TEMPLATE_VIS duration_values
Howard Hinnantc51e1022010-05-11 19:42:16 +0000921{
922public:
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000923 _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR _Rep zero() {return _Rep(0);}
924 _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR _Rep max() {return numeric_limits<_Rep>::max();}
925 _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR _Rep min() {return numeric_limits<_Rep>::lowest();}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000926};
927
Marshall Clowf2eabaf2015-11-05 19:33:59 +0000928#if _LIBCPP_STD_VER > 14
929template <class _ToDuration, class _Rep, class _Period>
930inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
931typename enable_if
932<
933 __is_duration<_ToDuration>::value,
934 _ToDuration
935>::type
936floor(const duration<_Rep, _Period>& __d)
937{
938 _ToDuration __t = duration_cast<_ToDuration>(__d);
939 if (__t > __d)
940 __t = __t - _ToDuration{1};
941 return __t;
942}
943
944template <class _ToDuration, class _Rep, class _Period>
945inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
946typename enable_if
947<
948 __is_duration<_ToDuration>::value,
949 _ToDuration
950>::type
951ceil(const duration<_Rep, _Period>& __d)
952{
953 _ToDuration __t = duration_cast<_ToDuration>(__d);
954 if (__t < __d)
955 __t = __t + _ToDuration{1};
956 return __t;
957}
958
959template <class _ToDuration, class _Rep, class _Period>
960inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
961typename enable_if
962<
963 __is_duration<_ToDuration>::value,
964 _ToDuration
965>::type
966round(const duration<_Rep, _Period>& __d)
967{
968 _ToDuration __lower = floor<_ToDuration>(__d);
969 _ToDuration __upper = __lower + _ToDuration{1};
970 auto __lowerDiff = __d - __lower;
971 auto __upperDiff = __upper - __d;
972 if (__lowerDiff < __upperDiff)
973 return __lower;
974 if (__lowerDiff > __upperDiff)
975 return __upper;
976 return __lower.count() & 1 ? __upper : __lower;
977}
978#endif
979
Howard Hinnantc51e1022010-05-11 19:42:16 +0000980// duration
981
982template <class _Rep, class _Period>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000983class _LIBCPP_TEMPLATE_VIS duration
Howard Hinnantc51e1022010-05-11 19:42:16 +0000984{
985 static_assert(!__is_duration<_Rep>::value, "A duration representation can not be a duration");
986 static_assert(__is_ratio<_Period>::value, "Second template parameter of duration must be a std::ratio");
987 static_assert(_Period::num > 0, "duration period must be positive");
Howard Hinnant0a51e152013-08-31 16:51:56 +0000988
989 template <class _R1, class _R2>
990 struct __no_overflow
991 {
992 private:
993 static const intmax_t __gcd_n1_n2 = __static_gcd<_R1::num, _R2::num>::value;
994 static const intmax_t __gcd_d1_d2 = __static_gcd<_R1::den, _R2::den>::value;
995 static const intmax_t __n1 = _R1::num / __gcd_n1_n2;
996 static const intmax_t __d1 = _R1::den / __gcd_d1_d2;
997 static const intmax_t __n2 = _R2::num / __gcd_n1_n2;
998 static const intmax_t __d2 = _R2::den / __gcd_d1_d2;
999 static const intmax_t max = -((intmax_t(1) << (sizeof(intmax_t) * CHAR_BIT - 1)) + 1);
1000
1001 template <intmax_t _Xp, intmax_t _Yp, bool __overflow>
1002 struct __mul // __overflow == false
1003 {
1004 static const intmax_t value = _Xp * _Yp;
1005 };
1006
1007 template <intmax_t _Xp, intmax_t _Yp>
1008 struct __mul<_Xp, _Yp, true>
1009 {
1010 static const intmax_t value = 1;
1011 };
1012
1013 public:
1014 static const bool value = (__n1 <= max / __d2) && (__n2 <= max / __d1);
1015 typedef ratio<__mul<__n1, __d2, !value>::value,
1016 __mul<__n2, __d1, !value>::value> type;
1017 };
Louis Dionne44bcff92018-08-03 22:36:53 +00001018
Howard Hinnantc51e1022010-05-11 19:42:16 +00001019public:
1020 typedef _Rep rep;
Marshall Clow76200b22017-03-21 18:38:57 +00001021 typedef typename _Period::type period;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001022private:
1023 rep __rep_;
1024public:
1025
Marshall Clow5c459c92013-07-31 19:32:19 +00001026 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Eric Fiselier07b2b552016-11-18 06:42:17 +00001027#ifndef _LIBCPP_CXX03_LANG
Marshall Clow5c459c92013-07-31 19:32:19 +00001028 duration() = default;
Marshall Clow3dbcf132013-07-31 19:39:37 +00001029#else
1030 duration() {}
Marshall Clow5c459c92013-07-31 19:32:19 +00001031#endif
1032
Howard Hinnantc51e1022010-05-11 19:42:16 +00001033 template <class _Rep2>
Howard Hinnantcf3143c2012-07-13 19:17:27 +00001034 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnantc51e1022010-05-11 19:42:16 +00001035 explicit duration(const _Rep2& __r,
1036 typename enable_if
1037 <
1038 is_convertible<_Rep2, rep>::value &&
1039 (treat_as_floating_point<rep>::value ||
1040 !treat_as_floating_point<_Rep2>::value)
1041 >::type* = 0)
1042 : __rep_(__r) {}
1043
1044 // conversions
1045 template <class _Rep2, class _Period2>
Howard Hinnantcf3143c2012-07-13 19:17:27 +00001046 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnantc51e1022010-05-11 19:42:16 +00001047 duration(const duration<_Rep2, _Period2>& __d,
1048 typename enable_if
1049 <
Howard Hinnant0a51e152013-08-31 16:51:56 +00001050 __no_overflow<_Period2, period>::value && (
Howard Hinnantc51e1022010-05-11 19:42:16 +00001051 treat_as_floating_point<rep>::value ||
Howard Hinnant0a51e152013-08-31 16:51:56 +00001052 (__no_overflow<_Period2, period>::type::den == 1 &&
1053 !treat_as_floating_point<_Rep2>::value))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001054 >::type* = 0)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001055 : __rep_(_VSTD::chrono::duration_cast<duration>(__d).count()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001056
1057 // observer
1058
Howard Hinnantcf3143c2012-07-13 19:17:27 +00001059 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR rep count() const {return __rep_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001060
1061 // arithmetic
1062
Marshall Clow76200b22017-03-21 18:38:57 +00001063 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR typename common_type<duration>::type operator+() const {return typename common_type<duration>::type(*this);}
1064 _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 +00001065 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 duration& operator++() {++__rep_; return *this;}
1066 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 duration operator++(int) {return duration(__rep_++);}
1067 _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_--);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001069
Marshall Clow5c87ad02017-01-04 23:03:24 +00001070 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 duration& operator+=(const duration& __d) {__rep_ += __d.count(); return *this;}
1071 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 duration& operator-=(const duration& __d) {__rep_ -= __d.count(); return *this;}
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 rep& rhs) {__rep_ *= rhs; return *this;}
1074 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 duration& operator/=(const rep& rhs) {__rep_ /= rhs; return *this;}
1075 _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 duration& rhs) {__rep_ %= rhs.count(); return *this;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001077
1078 // special values
1079
Howard Hinnantcf3143c2012-07-13 19:17:27 +00001080 _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR duration zero() {return duration(duration_values<rep>::zero());}
1081 _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR duration min() {return duration(duration_values<rep>::min());}
1082 _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR duration max() {return duration(duration_values<rep>::max());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001083};
1084
1085typedef duration<long long, nano> nanoseconds;
1086typedef duration<long long, micro> microseconds;
1087typedef duration<long long, milli> milliseconds;
1088typedef duration<long long > seconds;
1089typedef duration< long, ratio< 60> > minutes;
1090typedef duration< long, ratio<3600> > hours;
1091
1092// Duration ==
1093
1094template <class _LhsDuration, class _RhsDuration>
1095struct __duration_eq
1096{
Howard Hinnantcf3143c2012-07-13 19:17:27 +00001097 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnant12967fb2013-06-28 18:09:35 +00001098 bool operator()(const _LhsDuration& __lhs, const _RhsDuration& __rhs) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00001099 {
1100 typedef typename common_type<_LhsDuration, _RhsDuration>::type _Ct;
1101 return _Ct(__lhs).count() == _Ct(__rhs).count();
1102 }
1103};
1104
1105template <class _LhsDuration>
1106struct __duration_eq<_LhsDuration, _LhsDuration>
1107{
Howard Hinnantcf3143c2012-07-13 19:17:27 +00001108 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnant12967fb2013-06-28 18:09:35 +00001109 bool operator()(const _LhsDuration& __lhs, const _LhsDuration& __rhs) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00001110 {return __lhs.count() == __rhs.count();}
1111};
1112
1113template <class _Rep1, class _Period1, class _Rep2, class _Period2>
1114inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantcf3143c2012-07-13 19:17:27 +00001115_LIBCPP_CONSTEXPR
Howard Hinnantc51e1022010-05-11 19:42:16 +00001116bool
1117operator==(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
1118{
1119 return __duration_eq<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >()(__lhs, __rhs);
1120}
1121
1122// Duration !=
1123
1124template <class _Rep1, class _Period1, class _Rep2, class _Period2>
1125inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantcf3143c2012-07-13 19:17:27 +00001126_LIBCPP_CONSTEXPR
Howard Hinnantc51e1022010-05-11 19:42:16 +00001127bool
1128operator!=(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
1129{
1130 return !(__lhs == __rhs);
1131}
1132
1133// Duration <
1134
1135template <class _LhsDuration, class _RhsDuration>
1136struct __duration_lt
1137{
Howard Hinnantcf3143c2012-07-13 19:17:27 +00001138 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnant12967fb2013-06-28 18:09:35 +00001139 bool operator()(const _LhsDuration& __lhs, const _RhsDuration& __rhs) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00001140 {
1141 typedef typename common_type<_LhsDuration, _RhsDuration>::type _Ct;
1142 return _Ct(__lhs).count() < _Ct(__rhs).count();
1143 }
1144};
1145
1146template <class _LhsDuration>
1147struct __duration_lt<_LhsDuration, _LhsDuration>
1148{
Howard Hinnantcf3143c2012-07-13 19:17:27 +00001149 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnant12967fb2013-06-28 18:09:35 +00001150 bool operator()(const _LhsDuration& __lhs, const _LhsDuration& __rhs) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00001151 {return __lhs.count() < __rhs.count();}
1152};
1153
1154template <class _Rep1, class _Period1, class _Rep2, class _Period2>
1155inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantcf3143c2012-07-13 19:17:27 +00001156_LIBCPP_CONSTEXPR
Howard Hinnantc51e1022010-05-11 19:42:16 +00001157bool
1158operator< (const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
1159{
1160 return __duration_lt<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >()(__lhs, __rhs);
1161}
1162
1163// Duration >
1164
1165template <class _Rep1, class _Period1, class _Rep2, class _Period2>
1166inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantcf3143c2012-07-13 19:17:27 +00001167_LIBCPP_CONSTEXPR
Howard Hinnantc51e1022010-05-11 19:42:16 +00001168bool
1169operator> (const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
1170{
1171 return __rhs < __lhs;
1172}
1173
1174// Duration <=
1175
1176template <class _Rep1, class _Period1, class _Rep2, class _Period2>
1177inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantcf3143c2012-07-13 19:17:27 +00001178_LIBCPP_CONSTEXPR
Howard Hinnantc51e1022010-05-11 19:42:16 +00001179bool
1180operator<=(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
1181{
1182 return !(__rhs < __lhs);
1183}
1184
1185// Duration >=
1186
1187template <class _Rep1, class _Period1, class _Rep2, class _Period2>
1188inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantcf3143c2012-07-13 19:17:27 +00001189_LIBCPP_CONSTEXPR
Howard Hinnantc51e1022010-05-11 19:42:16 +00001190bool
1191operator>=(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
1192{
1193 return !(__lhs < __rhs);
1194}
1195
1196// Duration +
1197
1198template <class _Rep1, class _Period1, class _Rep2, class _Period2>
1199inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantcf3143c2012-07-13 19:17:27 +00001200_LIBCPP_CONSTEXPR
Howard Hinnantc51e1022010-05-11 19:42:16 +00001201typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type
1202operator+(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
1203{
Howard Hinnantcf3143c2012-07-13 19:17:27 +00001204 typedef typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type _Cd;
1205 return _Cd(_Cd(__lhs).count() + _Cd(__rhs).count());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001206}
1207
1208// Duration -
1209
1210template <class _Rep1, class _Period1, class _Rep2, class _Period2>
1211inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantcf3143c2012-07-13 19:17:27 +00001212_LIBCPP_CONSTEXPR
Howard Hinnantc51e1022010-05-11 19:42:16 +00001213typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type
1214operator-(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
1215{
Howard Hinnantcf3143c2012-07-13 19:17:27 +00001216 typedef typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type _Cd;
1217 return _Cd(_Cd(__lhs).count() - _Cd(__rhs).count());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001218}
1219
1220// Duration *
1221
1222template <class _Rep1, class _Period, class _Rep2>
1223inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantcf3143c2012-07-13 19:17:27 +00001224_LIBCPP_CONSTEXPR
Howard Hinnantc51e1022010-05-11 19:42:16 +00001225typename enable_if
1226<
1227 is_convertible<_Rep2, typename common_type<_Rep1, _Rep2>::type>::value,
1228 duration<typename common_type<_Rep1, _Rep2>::type, _Period>
1229>::type
1230operator*(const duration<_Rep1, _Period>& __d, const _Rep2& __s)
1231{
1232 typedef typename common_type<_Rep1, _Rep2>::type _Cr;
Howard Hinnantcf3143c2012-07-13 19:17:27 +00001233 typedef duration<_Cr, _Period> _Cd;
1234 return _Cd(_Cd(__d).count() * static_cast<_Cr>(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001235}
1236
1237template <class _Rep1, class _Period, class _Rep2>
1238inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantcf3143c2012-07-13 19:17:27 +00001239_LIBCPP_CONSTEXPR
Howard Hinnantc51e1022010-05-11 19:42:16 +00001240typename enable_if
1241<
1242 is_convertible<_Rep1, typename common_type<_Rep1, _Rep2>::type>::value,
1243 duration<typename common_type<_Rep1, _Rep2>::type, _Period>
1244>::type
1245operator*(const _Rep1& __s, const duration<_Rep2, _Period>& __d)
1246{
1247 return __d * __s;
1248}
1249
1250// Duration /
1251
1252template <class _Duration, class _Rep, bool = __is_duration<_Rep>::value>
1253struct __duration_divide_result
1254{
1255};
1256
1257template <class _Duration, class _Rep2,
1258 bool = is_convertible<_Rep2,
1259 typename common_type<typename _Duration::rep, _Rep2>::type>::value>
1260struct __duration_divide_imp
1261{
1262};
1263
1264template <class _Rep1, class _Period, class _Rep2>
1265struct __duration_divide_imp<duration<_Rep1, _Period>, _Rep2, true>
1266{
1267 typedef duration<typename common_type<_Rep1, _Rep2>::type, _Period> type;
1268};
1269
1270template <class _Rep1, class _Period, class _Rep2>
1271struct __duration_divide_result<duration<_Rep1, _Period>, _Rep2, false>
1272 : __duration_divide_imp<duration<_Rep1, _Period>, _Rep2>
1273{
1274};
1275
1276template <class _Rep1, class _Period, class _Rep2>
1277inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantcf3143c2012-07-13 19:17:27 +00001278_LIBCPP_CONSTEXPR
Howard Hinnantc51e1022010-05-11 19:42:16 +00001279typename __duration_divide_result<duration<_Rep1, _Period>, _Rep2>::type
1280operator/(const duration<_Rep1, _Period>& __d, const _Rep2& __s)
1281{
1282 typedef typename common_type<_Rep1, _Rep2>::type _Cr;
Howard Hinnantcf3143c2012-07-13 19:17:27 +00001283 typedef duration<_Cr, _Period> _Cd;
1284 return _Cd(_Cd(__d).count() / static_cast<_Cr>(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001285}
1286
1287template <class _Rep1, class _Period1, class _Rep2, class _Period2>
1288inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantcf3143c2012-07-13 19:17:27 +00001289_LIBCPP_CONSTEXPR
Howard Hinnantc51e1022010-05-11 19:42:16 +00001290typename common_type<_Rep1, _Rep2>::type
1291operator/(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
1292{
1293 typedef typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type _Ct;
1294 return _Ct(__lhs).count() / _Ct(__rhs).count();
1295}
1296
1297// Duration %
1298
1299template <class _Rep1, class _Period, class _Rep2>
1300inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantcf3143c2012-07-13 19:17:27 +00001301_LIBCPP_CONSTEXPR
Howard Hinnantc51e1022010-05-11 19:42:16 +00001302typename __duration_divide_result<duration<_Rep1, _Period>, _Rep2>::type
1303operator%(const duration<_Rep1, _Period>& __d, const _Rep2& __s)
1304{
1305 typedef typename common_type<_Rep1, _Rep2>::type _Cr;
Howard Hinnantcf3143c2012-07-13 19:17:27 +00001306 typedef duration<_Cr, _Period> _Cd;
1307 return _Cd(_Cd(__d).count() % static_cast<_Cr>(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001308}
1309
1310template <class _Rep1, class _Period1, class _Rep2, class _Period2>
1311inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantcf3143c2012-07-13 19:17:27 +00001312_LIBCPP_CONSTEXPR
Howard Hinnantc51e1022010-05-11 19:42:16 +00001313typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type
1314operator%(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
1315{
Howard Hinnantcf3143c2012-07-13 19:17:27 +00001316 typedef typename common_type<_Rep1, _Rep2>::type _Cr;
1317 typedef typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type _Cd;
1318 return _Cd(static_cast<_Cr>(_Cd(__lhs).count()) % static_cast<_Cr>(_Cd(__rhs).count()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001319}
1320
1321//////////////////////////////////////////////////////////
1322///////////////////// time_point /////////////////////////
1323//////////////////////////////////////////////////////////
1324
1325template <class _Clock, class _Duration = typename _Clock::duration>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001326class _LIBCPP_TEMPLATE_VIS time_point
Howard Hinnantc51e1022010-05-11 19:42:16 +00001327{
1328 static_assert(__is_duration<_Duration>::value,
1329 "Second template parameter of time_point must be a std::chrono::duration");
1330public:
1331 typedef _Clock clock;
1332 typedef _Duration duration;
1333 typedef typename duration::rep rep;
1334 typedef typename duration::period period;
1335private:
1336 duration __d_;
1337
1338public:
Marshall Clow5c459c92013-07-31 19:32:19 +00001339 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 time_point() : __d_(duration::zero()) {}
1340 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 explicit time_point(const duration& __d) : __d_(__d) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001341
1342 // conversions
1343 template <class _Duration2>
Marshall Clow5c459c92013-07-31 19:32:19 +00001344 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantc51e1022010-05-11 19:42:16 +00001345 time_point(const time_point<clock, _Duration2>& t,
1346 typename enable_if
1347 <
1348 is_convertible<_Duration2, duration>::value
1349 >::type* = 0)
1350 : __d_(t.time_since_epoch()) {}
1351
1352 // observer
1353
Marshall Clow5c459c92013-07-31 19:32:19 +00001354 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 duration time_since_epoch() const {return __d_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001355
1356 // arithmetic
1357
Howard Hinnantecc7ba92013-07-08 21:06:38 +00001358 _LIBCPP_INLINE_VISIBILITY time_point& operator+=(const duration& __d) {__d_ += __d; return *this;}
1359 _LIBCPP_INLINE_VISIBILITY time_point& operator-=(const duration& __d) {__d_ -= __d; return *this;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001360
1361 // special values
1362
Howard Hinnantcf3143c2012-07-13 19:17:27 +00001363 _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR time_point min() {return time_point(duration::min());}
1364 _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR time_point max() {return time_point(duration::max());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001365};
1366
1367} // chrono
1368
1369template <class _Clock, class _Duration1, class _Duration2>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001370struct _LIBCPP_TEMPLATE_VIS common_type<chrono::time_point<_Clock, _Duration1>,
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00001371 chrono::time_point<_Clock, _Duration2> >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001372{
1373 typedef chrono::time_point<_Clock, typename common_type<_Duration1, _Duration2>::type> type;
1374};
1375
1376namespace chrono {
1377
1378template <class _ToDuration, class _Clock, class _Duration>
Marshall Clow5c459c92013-07-31 19:32:19 +00001379inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantc51e1022010-05-11 19:42:16 +00001380time_point<_Clock, _ToDuration>
1381time_point_cast(const time_point<_Clock, _Duration>& __t)
1382{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001383 return time_point<_Clock, _ToDuration>(_VSTD::chrono::duration_cast<_ToDuration>(__t.time_since_epoch()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001384}
1385
Marshall Clowf2eabaf2015-11-05 19:33:59 +00001386#if _LIBCPP_STD_VER > 14
1387template <class _ToDuration, class _Clock, class _Duration>
1388inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
1389typename enable_if
1390<
1391 __is_duration<_ToDuration>::value,
1392 time_point<_Clock, _ToDuration>
1393>::type
1394floor(const time_point<_Clock, _Duration>& __t)
1395{
1396 return time_point<_Clock, _ToDuration>{floor<_ToDuration>(__t.time_since_epoch())};
1397}
1398
1399template <class _ToDuration, class _Clock, class _Duration>
1400inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
1401typename enable_if
1402<
1403 __is_duration<_ToDuration>::value,
1404 time_point<_Clock, _ToDuration>
1405>::type
1406ceil(const time_point<_Clock, _Duration>& __t)
1407{
1408 return time_point<_Clock, _ToDuration>{ceil<_ToDuration>(__t.time_since_epoch())};
1409}
1410
1411template <class _ToDuration, class _Clock, class _Duration>
1412inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
1413typename enable_if
1414<
1415 __is_duration<_ToDuration>::value,
1416 time_point<_Clock, _ToDuration>
1417>::type
1418round(const time_point<_Clock, _Duration>& __t)
1419{
1420 return time_point<_Clock, _ToDuration>{round<_ToDuration>(__t.time_since_epoch())};
1421}
1422
1423template <class _Rep, class _Period>
1424inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
1425typename enable_if
1426<
1427 numeric_limits<_Rep>::is_signed,
1428 duration<_Rep, _Period>
1429>::type
1430abs(duration<_Rep, _Period> __d)
1431{
1432 return __d >= __d.zero() ? __d : -__d;
1433}
1434#endif
1435
Howard Hinnantc51e1022010-05-11 19:42:16 +00001436// time_point ==
1437
1438template <class _Clock, class _Duration1, class _Duration2>
Marshall Clow5c459c92013-07-31 19:32:19 +00001439inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantc51e1022010-05-11 19:42:16 +00001440bool
1441operator==(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs)
1442{
1443 return __lhs.time_since_epoch() == __rhs.time_since_epoch();
1444}
1445
1446// time_point !=
1447
1448template <class _Clock, class _Duration1, class _Duration2>
Marshall Clow5c459c92013-07-31 19:32:19 +00001449inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantc51e1022010-05-11 19:42:16 +00001450bool
1451operator!=(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs)
1452{
1453 return !(__lhs == __rhs);
1454}
1455
1456// time_point <
1457
1458template <class _Clock, class _Duration1, class _Duration2>
Marshall Clow5c459c92013-07-31 19:32:19 +00001459inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantc51e1022010-05-11 19:42:16 +00001460bool
1461operator<(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs)
1462{
1463 return __lhs.time_since_epoch() < __rhs.time_since_epoch();
1464}
1465
1466// time_point >
1467
1468template <class _Clock, class _Duration1, class _Duration2>
Marshall Clow5c459c92013-07-31 19:32:19 +00001469inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantc51e1022010-05-11 19:42:16 +00001470bool
1471operator>(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs)
1472{
1473 return __rhs < __lhs;
1474}
1475
1476// time_point <=
1477
1478template <class _Clock, class _Duration1, class _Duration2>
Marshall Clow5c459c92013-07-31 19:32:19 +00001479inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantc51e1022010-05-11 19:42:16 +00001480bool
1481operator<=(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs)
1482{
1483 return !(__rhs < __lhs);
1484}
1485
1486// time_point >=
1487
1488template <class _Clock, class _Duration1, class _Duration2>
Marshall Clow5c459c92013-07-31 19:32:19 +00001489inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantc51e1022010-05-11 19:42:16 +00001490bool
1491operator>=(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs)
1492{
1493 return !(__lhs < __rhs);
1494}
1495
1496// time_point operator+(time_point x, duration y);
1497
1498template <class _Clock, class _Duration1, class _Rep2, class _Period2>
Marshall Clow5c459c92013-07-31 19:32:19 +00001499inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantc51e1022010-05-11 19:42:16 +00001500time_point<_Clock, typename common_type<_Duration1, duration<_Rep2, _Period2> >::type>
1501operator+(const time_point<_Clock, _Duration1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
1502{
1503 typedef time_point<_Clock, typename common_type<_Duration1, duration<_Rep2, _Period2> >::type> _Tr;
Marshall Clow5c459c92013-07-31 19:32:19 +00001504 return _Tr (__lhs.time_since_epoch() + __rhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001505}
1506
1507// time_point operator+(duration x, time_point y);
1508
1509template <class _Rep1, class _Period1, class _Clock, class _Duration2>
Marshall Clow5c459c92013-07-31 19:32:19 +00001510inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantc51e1022010-05-11 19:42:16 +00001511time_point<_Clock, typename common_type<duration<_Rep1, _Period1>, _Duration2>::type>
1512operator+(const duration<_Rep1, _Period1>& __lhs, const time_point<_Clock, _Duration2>& __rhs)
1513{
1514 return __rhs + __lhs;
1515}
1516
1517// time_point operator-(time_point x, duration y);
1518
1519template <class _Clock, class _Duration1, class _Rep2, class _Period2>
Marshall Clow5c459c92013-07-31 19:32:19 +00001520inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantc51e1022010-05-11 19:42:16 +00001521time_point<_Clock, typename common_type<_Duration1, duration<_Rep2, _Period2> >::type>
1522operator-(const time_point<_Clock, _Duration1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
1523{
Marshall Clow78dbe462016-11-14 18:22:19 +00001524 typedef time_point<_Clock, typename common_type<_Duration1, duration<_Rep2, _Period2> >::type> _Ret;
1525 return _Ret(__lhs.time_since_epoch() -__rhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001526}
1527
1528// duration operator-(time_point x, time_point y);
1529
1530template <class _Clock, class _Duration1, class _Duration2>
Marshall Clow5c459c92013-07-31 19:32:19 +00001531inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantc51e1022010-05-11 19:42:16 +00001532typename common_type<_Duration1, _Duration2>::type
1533operator-(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs)
1534{
1535 return __lhs.time_since_epoch() - __rhs.time_since_epoch();
1536}
1537
1538//////////////////////////////////////////////////////////
1539/////////////////////// clocks ///////////////////////////
1540//////////////////////////////////////////////////////////
1541
Howard Hinnant8331b762013-03-06 23:30:19 +00001542class _LIBCPP_TYPE_VIS system_clock
Howard Hinnantc51e1022010-05-11 19:42:16 +00001543{
1544public:
1545 typedef microseconds duration;
1546 typedef duration::rep rep;
1547 typedef duration::period period;
1548 typedef chrono::time_point<system_clock> time_point;
Marshall Clow5c459c92013-07-31 19:32:19 +00001549 static _LIBCPP_CONSTEXPR_AFTER_CXX11 const bool is_steady = false;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001550
Howard Hinnantaa54ac42011-05-28 18:34:36 +00001551 static time_point now() _NOEXCEPT;
1552 static time_t to_time_t (const time_point& __t) _NOEXCEPT;
1553 static time_point from_time_t(time_t __t) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001554};
1555
Jonathan Roelofscce96eb2014-09-02 21:14:38 +00001556#ifndef _LIBCPP_HAS_NO_MONOTONIC_CLOCK
Howard Hinnant8331b762013-03-06 23:30:19 +00001557class _LIBCPP_TYPE_VIS steady_clock
Howard Hinnantc51e1022010-05-11 19:42:16 +00001558{
1559public:
1560 typedef nanoseconds duration;
1561 typedef duration::rep rep;
1562 typedef duration::period period;
Howard Hinnantc8dbd222010-11-20 19:16:30 +00001563 typedef chrono::time_point<steady_clock, duration> time_point;
Marshall Clow5c459c92013-07-31 19:32:19 +00001564 static _LIBCPP_CONSTEXPR_AFTER_CXX11 const bool is_steady = true;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001565
Howard Hinnantaa54ac42011-05-28 18:34:36 +00001566 static time_point now() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001567};
1568
Howard Hinnantc8dbd222010-11-20 19:16:30 +00001569typedef steady_clock high_resolution_clock;
Jonathan Roelofscce96eb2014-09-02 21:14:38 +00001570#else
1571typedef system_clock high_resolution_clock;
1572#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001573
1574} // chrono
1575
Marshall Clowac868372013-10-05 21:18:32 +00001576#if _LIBCPP_STD_VER > 11
1577// Suffixes for duration literals [time.duration.literals]
1578inline namespace literals
Louis Dionne44bcff92018-08-03 22:36:53 +00001579{
Marshall Cloweb41e5c2013-07-24 21:18:14 +00001580 inline namespace chrono_literals
1581 {
1582
Marshall Clow2ac805a2017-08-09 15:42:50 +00001583 constexpr chrono::hours operator""h(unsigned long long __h)
Howard Hinnant5c167562013-08-07 19:39:48 +00001584 {
1585 return chrono::hours(static_cast<chrono::hours::rep>(__h));
1586 }
Marshall Cloweb41e5c2013-07-24 21:18:14 +00001587
Marshall Clow2ac805a2017-08-09 15:42:50 +00001588 constexpr chrono::duration<long double, ratio<3600,1>> operator""h(long double __h)
Howard Hinnant5c167562013-08-07 19:39:48 +00001589 {
1590 return chrono::duration<long double, ratio<3600,1>>(__h);
1591 }
Marshall Cloweb41e5c2013-07-24 21:18:14 +00001592
1593
Marshall Clow2ac805a2017-08-09 15:42:50 +00001594 constexpr chrono::minutes operator""min(unsigned long long __m)
Howard Hinnant5c167562013-08-07 19:39:48 +00001595 {
1596 return chrono::minutes(static_cast<chrono::minutes::rep>(__m));
1597 }
Marshall Cloweb41e5c2013-07-24 21:18:14 +00001598
Marshall Clow2ac805a2017-08-09 15:42:50 +00001599 constexpr chrono::duration<long double, ratio<60,1>> operator""min(long double __m)
Howard Hinnant5c167562013-08-07 19:39:48 +00001600 {
1601 return chrono::duration<long double, ratio<60,1>> (__m);
1602 }
Marshall Cloweb41e5c2013-07-24 21:18:14 +00001603
1604
Marshall Clow2ac805a2017-08-09 15:42:50 +00001605 constexpr chrono::seconds operator""s(unsigned long long __s)
Howard Hinnant5c167562013-08-07 19:39:48 +00001606 {
1607 return chrono::seconds(static_cast<chrono::seconds::rep>(__s));
1608 }
Marshall Cloweb41e5c2013-07-24 21:18:14 +00001609
Marshall Clow2ac805a2017-08-09 15:42:50 +00001610 constexpr chrono::duration<long double> operator""s(long double __s)
Howard Hinnant5c167562013-08-07 19:39:48 +00001611 {
1612 return chrono::duration<long double> (__s);
1613 }
Marshall Cloweb41e5c2013-07-24 21:18:14 +00001614
1615
Marshall Clow2ac805a2017-08-09 15:42:50 +00001616 constexpr chrono::milliseconds operator""ms(unsigned long long __ms)
Howard Hinnant5c167562013-08-07 19:39:48 +00001617 {
1618 return chrono::milliseconds(static_cast<chrono::milliseconds::rep>(__ms));
1619 }
Marshall Cloweb41e5c2013-07-24 21:18:14 +00001620
Marshall Clow2ac805a2017-08-09 15:42:50 +00001621 constexpr chrono::duration<long double, milli> operator""ms(long double __ms)
Howard Hinnant5c167562013-08-07 19:39:48 +00001622 {
1623 return chrono::duration<long double, milli>(__ms);
1624 }
Marshall Cloweb41e5c2013-07-24 21:18:14 +00001625
1626
Marshall Clow2ac805a2017-08-09 15:42:50 +00001627 constexpr chrono::microseconds operator""us(unsigned long long __us)
Howard Hinnant5c167562013-08-07 19:39:48 +00001628 {
1629 return chrono::microseconds(static_cast<chrono::microseconds::rep>(__us));
1630 }
Marshall Cloweb41e5c2013-07-24 21:18:14 +00001631
Marshall Clow2ac805a2017-08-09 15:42:50 +00001632 constexpr chrono::duration<long double, micro> operator""us(long double __us)
Howard Hinnant5c167562013-08-07 19:39:48 +00001633 {
1634 return chrono::duration<long double, micro> (__us);
1635 }
Louis Dionne44bcff92018-08-03 22:36:53 +00001636
Marshall Cloweb41e5c2013-07-24 21:18:14 +00001637
Marshall Clow2ac805a2017-08-09 15:42:50 +00001638 constexpr chrono::nanoseconds operator""ns(unsigned long long __ns)
Howard Hinnant5c167562013-08-07 19:39:48 +00001639 {
1640 return chrono::nanoseconds(static_cast<chrono::nanoseconds::rep>(__ns));
1641 }
Marshall Cloweb41e5c2013-07-24 21:18:14 +00001642
Marshall Clow2ac805a2017-08-09 15:42:50 +00001643 constexpr chrono::duration<long double, nano> operator""ns(long double __ns)
Howard Hinnant5c167562013-08-07 19:39:48 +00001644 {
1645 return chrono::duration<long double, nano> (__ns);
1646 }
Marshall Cloweb41e5c2013-07-24 21:18:14 +00001647
1648}}
Marshall Clowac868372013-10-05 21:18:32 +00001649
1650namespace chrono { // hoist the literals into namespace std::chrono
1651 using namespace literals::chrono_literals;
1652}
1653
Marshall Cloweb41e5c2013-07-24 21:18:14 +00001654#endif
1655
Howard Hinnantc51e1022010-05-11 19:42:16 +00001656_LIBCPP_END_NAMESPACE_STD
1657
Eric Fiselierf4433a32017-05-31 22:07:49 +00001658_LIBCPP_POP_MACROS
1659
Howard Hinnantc51e1022010-05-11 19:42:16 +00001660#endif // _LIBCPP_CHRONO