Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1 | // -*- C++ -*- |
| 2 | //===---------------------------- chrono ----------------------------------===// |
| 3 | // |
Howard Hinnant | f5256e1 | 2010-05-11 21:36:01 +0000 | [diff] [blame] | 4 | // The LLVM Compiler Infrastructure |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5 | // |
Howard Hinnant | b64f8b0 | 2010-11-16 22:09:02 +0000 | [diff] [blame] | 6 | // This file is dual licensed under the MIT and the University of Illinois Open |
| 7 | // Source Licenses. See LICENSE.TXT for details. |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 8 | // |
| 9 | //===----------------------------------------------------------------------===// |
| 10 | |
| 11 | #ifndef _LIBCPP_CHRONO |
| 12 | #define _LIBCPP_CHRONO |
| 13 | |
| 14 | /* |
| 15 | chrono synopsis |
| 16 | |
| 17 | namespace std |
| 18 | { |
| 19 | namespace chrono |
| 20 | { |
| 21 | |
| 22 | template <class ToDuration, class Rep, class Period> |
Howard Hinnant | 473f838 | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 23 | constexpr |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 24 | ToDuration |
| 25 | duration_cast(const duration<Rep, Period>& fd); |
| 26 | |
| 27 | template <class Rep> struct treat_as_floating_point : is_floating_point<Rep> {}; |
| 28 | |
Marshall Clow | a3866e4 | 2015-11-30 05:39:30 +0000 | [diff] [blame] | 29 | template <class Rep> constexpr bool treat_as_floating_point_v |
| 30 | = treat_as_floating_point<Rep>::value; // C++17 |
| 31 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 32 | template <class Rep> |
| 33 | struct duration_values |
| 34 | { |
| 35 | public: |
Howard Hinnant | 473f838 | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 36 | static constexpr Rep zero(); |
| 37 | static constexpr Rep max(); |
| 38 | static constexpr Rep min(); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 39 | }; |
| 40 | |
| 41 | // duration |
| 42 | |
| 43 | template <class Rep, class Period = ratio<1>> |
| 44 | class 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"); |
| 49 | public: |
| 50 | typedef Rep rep; |
Marshall Clow | cf6e0db | 2017-03-21 18:38:57 +0000 | [diff] [blame] | 51 | typedef typename _Period::type period; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 52 | |
Howard Hinnant | 473f838 | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 53 | constexpr duration() = default; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 54 | template <class Rep2> |
Howard Hinnant | 473f838 | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 55 | constexpr explicit duration(const Rep2& r, |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 56 | 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 Hinnant | 473f838 | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 65 | constexpr duration(const duration<Rep2, Period2>& d, |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 66 | 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 Hinnant | 473f838 | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 74 | constexpr rep count() const; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 75 | |
| 76 | // arithmetic |
| 77 | |
Marshall Clow | cf6e0db | 2017-03-21 18:38:57 +0000 | [diff] [blame] | 78 | constexpr common_type<duration>::type operator+() const; |
| 79 | constexpr common_type<duration>::type operator-() const; |
Marshall Clow | 3df90c9 | 2017-01-04 23:03:24 +0000 | [diff] [blame] | 80 | constexpr duration& operator++(); |
| 81 | constexpr duration operator++(int); |
| 82 | constexpr duration& operator--(); |
| 83 | constexpr duration operator--(int); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 84 | |
Marshall Clow | 3df90c9 | 2017-01-04 23:03:24 +0000 | [diff] [blame] | 85 | constexpr duration& operator+=(const duration& d); |
| 86 | constexpr duration& operator-=(const duration& d); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 87 | |
| 88 | duration& operator*=(const rep& rhs); |
| 89 | duration& operator/=(const rep& rhs); |
| 90 | |
| 91 | // special values |
| 92 | |
Howard Hinnant | 473f838 | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 93 | static constexpr duration zero(); |
| 94 | static constexpr duration min(); |
| 95 | static constexpr duration max(); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 96 | }; |
| 97 | |
| 98 | typedef duration<long long, nano> nanoseconds; |
| 99 | typedef duration<long long, micro> microseconds; |
| 100 | typedef duration<long long, milli> milliseconds; |
| 101 | typedef duration<long long > seconds; |
| 102 | typedef duration< long, ratio< 60> > minutes; |
| 103 | typedef duration< long, ratio<3600> > hours; |
| 104 | |
| 105 | template <class Clock, class Duration = typename Clock::duration> |
| 106 | class time_point |
| 107 | { |
| 108 | public: |
| 109 | typedef Clock clock; |
| 110 | typedef Duration duration; |
| 111 | typedef typename duration::rep rep; |
| 112 | typedef typename duration::period period; |
| 113 | private: |
| 114 | duration d_; // exposition only |
| 115 | |
| 116 | public: |
Marshall Clow | 832b304 | 2013-07-31 19:32:19 +0000 | [diff] [blame] | 117 | 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 Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 119 | |
| 120 | // conversions |
| 121 | template <class Duration2> |
Marshall Clow | 832b304 | 2013-07-31 19:32:19 +0000 | [diff] [blame] | 122 | time_point(const time_point<clock, Duration2>& t); // constexpr in C++14 |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 123 | |
| 124 | // observer |
| 125 | |
Marshall Clow | 832b304 | 2013-07-31 19:32:19 +0000 | [diff] [blame] | 126 | duration time_since_epoch() const; // constexpr in C++14 |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 127 | |
| 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 |
| 142 | template <class Rep1, class Period1, class Rep2, class Period2> |
| 143 | struct common_type<chrono::duration<Rep1, Period1>, chrono::duration<Rep2, Period2>>; |
| 144 | |
| 145 | template <class Clock, class Duration1, class Duration2> |
| 146 | struct common_type<chrono::time_point<Clock, Duration1>, chrono::time_point<Clock, Duration2>>; |
| 147 | |
| 148 | namespace chrono { |
| 149 | |
| 150 | // duration arithmetic |
| 151 | template <class Rep1, class Period1, class Rep2, class Period2> |
Howard Hinnant | 473f838 | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 152 | constexpr |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 153 | typename common_type<duration<Rep1, Period1>, duration<Rep2, Period2>>::type |
| 154 | operator+(const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs); |
| 155 | template <class Rep1, class Period1, class Rep2, class Period2> |
Howard Hinnant | 473f838 | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 156 | constexpr |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 157 | typename common_type<duration<Rep1, Period1>, duration<Rep2, Period2>>::type |
| 158 | operator-(const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs); |
| 159 | template <class Rep1, class Period, class Rep2> |
Howard Hinnant | 473f838 | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 160 | constexpr |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 161 | duration<typename common_type<Rep1, Rep2>::type, Period> |
| 162 | operator*(const duration<Rep1, Period>& d, const Rep2& s); |
| 163 | template <class Rep1, class Period, class Rep2> |
Howard Hinnant | 473f838 | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 164 | constexpr |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 165 | duration<typename common_type<Rep1, Rep2>::type, Period> |
| 166 | operator*(const Rep1& s, const duration<Rep2, Period>& d); |
| 167 | template <class Rep1, class Period, class Rep2> |
Howard Hinnant | 473f838 | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 168 | constexpr |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 169 | duration<typename common_type<Rep1, Rep2>::type, Period> |
| 170 | operator/(const duration<Rep1, Period>& d, const Rep2& s); |
| 171 | template <class Rep1, class Period1, class Rep2, class Period2> |
Howard Hinnant | 473f838 | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 172 | constexpr |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 173 | typename common_type<Rep1, Rep2>::type |
| 174 | operator/(const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs); |
| 175 | |
| 176 | // duration comparisons |
| 177 | template <class Rep1, class Period1, class Rep2, class Period2> |
Howard Hinnant | 473f838 | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 178 | constexpr |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 179 | bool operator==(const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs); |
| 180 | template <class Rep1, class Period1, class Rep2, class Period2> |
Howard Hinnant | 473f838 | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 181 | constexpr |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 182 | bool operator!=(const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs); |
| 183 | template <class Rep1, class Period1, class Rep2, class Period2> |
Howard Hinnant | 473f838 | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 184 | constexpr |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 185 | bool operator< (const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs); |
| 186 | template <class Rep1, class Period1, class Rep2, class Period2> |
Howard Hinnant | 473f838 | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 187 | constexpr |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 188 | bool operator<=(const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs); |
| 189 | template <class Rep1, class Period1, class Rep2, class Period2> |
Howard Hinnant | 473f838 | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 190 | constexpr |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 191 | bool operator> (const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs); |
| 192 | template <class Rep1, class Period1, class Rep2, class Period2> |
Howard Hinnant | 473f838 | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 193 | constexpr |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 194 | bool operator>=(const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs); |
| 195 | |
| 196 | // duration_cast |
| 197 | template <class ToDuration, class Rep, class Period> |
| 198 | ToDuration duration_cast(const duration<Rep, Period>& d); |
| 199 | |
Marshall Clow | 223df2e | 2015-11-05 19:33:59 +0000 | [diff] [blame] | 200 | template <class ToDuration, class Rep, class Period> |
| 201 | constexpr ToDuration floor(const duration<Rep, Period>& d); // C++17 |
| 202 | template <class ToDuration, class Rep, class Period> |
| 203 | constexpr ToDuration ceil(const duration<Rep, Period>& d); // C++17 |
| 204 | template <class ToDuration, class Rep, class Period> |
| 205 | constexpr ToDuration round(const duration<Rep, Period>& d); // C++17 |
| 206 | |
Marshall Clow | 832b304 | 2013-07-31 19:32:19 +0000 | [diff] [blame] | 207 | // time_point arithmetic (all constexpr in C++14) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 208 | template <class Clock, class Duration1, class Rep2, class Period2> |
| 209 | time_point<Clock, typename common_type<Duration1, duration<Rep2, Period2>>::type> |
| 210 | operator+(const time_point<Clock, Duration1>& lhs, const duration<Rep2, Period2>& rhs); |
| 211 | template <class Rep1, class Period1, class Clock, class Duration2> |
| 212 | time_point<Clock, typename common_type<duration<Rep1, Period1>, Duration2>::type> |
| 213 | operator+(const duration<Rep1, Period1>& lhs, const time_point<Clock, Duration2>& rhs); |
| 214 | template <class Clock, class Duration1, class Rep2, class Period2> |
| 215 | time_point<Clock, typename common_type<Duration1, duration<Rep2, Period2>>::type> |
| 216 | operator-(const time_point<Clock, Duration1>& lhs, const duration<Rep2, Period2>& rhs); |
| 217 | template <class Clock, class Duration1, class Duration2> |
| 218 | typename common_type<Duration1, Duration2>::type |
| 219 | operator-(const time_point<Clock, Duration1>& lhs, const time_point<Clock, Duration2>& rhs); |
| 220 | |
Marshall Clow | 832b304 | 2013-07-31 19:32:19 +0000 | [diff] [blame] | 221 | // time_point comparisons (all constexpr in C++14) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 222 | template <class Clock, class Duration1, class Duration2> |
| 223 | bool operator==(const time_point<Clock, Duration1>& lhs, const time_point<Clock, Duration2>& rhs); |
| 224 | template <class Clock, class Duration1, class Duration2> |
| 225 | bool operator!=(const time_point<Clock, Duration1>& lhs, const time_point<Clock, Duration2>& rhs); |
| 226 | template <class Clock, class Duration1, class Duration2> |
| 227 | bool operator< (const time_point<Clock, Duration1>& lhs, const time_point<Clock, Duration2>& rhs); |
| 228 | template <class Clock, class Duration1, class Duration2> |
| 229 | bool operator<=(const time_point<Clock, Duration1>& lhs, const time_point<Clock, Duration2>& rhs); |
| 230 | template <class Clock, class Duration1, class Duration2> |
| 231 | bool operator> (const time_point<Clock, Duration1>& lhs, const time_point<Clock, Duration2>& rhs); |
| 232 | template <class Clock, class Duration1, class Duration2> |
| 233 | bool operator>=(const time_point<Clock, Duration1>& lhs, const time_point<Clock, Duration2>& rhs); |
| 234 | |
Marshall Clow | 832b304 | 2013-07-31 19:32:19 +0000 | [diff] [blame] | 235 | // time_point_cast (constexpr in C++14) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 236 | |
| 237 | template <class ToDuration, class Clock, class Duration> |
| 238 | time_point<Clock, ToDuration> time_point_cast(const time_point<Clock, Duration>& t); |
| 239 | |
Marshall Clow | 223df2e | 2015-11-05 19:33:59 +0000 | [diff] [blame] | 240 | template <class ToDuration, class Clock, class Duration> |
| 241 | constexpr time_point<Clock, ToDuration> |
| 242 | floor(const time_point<Clock, Duration>& tp); // C++17 |
| 243 | |
| 244 | template <class ToDuration, class Clock, class Duration> |
| 245 | constexpr time_point<Clock, ToDuration> |
| 246 | ceil(const time_point<Clock, Duration>& tp); // C++17 |
| 247 | |
| 248 | template <class ToDuration, class Clock, class Duration> |
| 249 | constexpr time_point<Clock, ToDuration> |
| 250 | round(const time_point<Clock, Duration>& tp); // C++17 |
| 251 | |
| 252 | template <class Rep, class Period> |
| 253 | constexpr duration<Rep, Period> abs(duration<Rep, Period> d); // C++17 |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 254 | // Clocks |
| 255 | |
| 256 | class system_clock |
| 257 | { |
| 258 | public: |
| 259 | typedef microseconds duration; |
| 260 | typedef duration::rep rep; |
| 261 | typedef duration::period period; |
| 262 | typedef chrono::time_point<system_clock> time_point; |
Marshall Clow | 832b304 | 2013-07-31 19:32:19 +0000 | [diff] [blame] | 263 | static const bool is_steady = false; // constexpr in C++14 |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 264 | |
Howard Hinnant | 756a176 | 2011-05-28 18:34:36 +0000 | [diff] [blame] | 265 | static time_point now() noexcept; |
| 266 | static time_t to_time_t (const time_point& __t) noexcept; |
| 267 | static time_point from_time_t(time_t __t) noexcept; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 268 | }; |
| 269 | |
Howard Hinnant | f8f8521 | 2010-11-20 19:16:30 +0000 | [diff] [blame] | 270 | class steady_clock |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 271 | { |
| 272 | public: |
| 273 | typedef nanoseconds duration; |
| 274 | typedef duration::rep rep; |
| 275 | typedef duration::period period; |
Howard Hinnant | f8f8521 | 2010-11-20 19:16:30 +0000 | [diff] [blame] | 276 | typedef chrono::time_point<steady_clock, duration> time_point; |
Marshall Clow | 832b304 | 2013-07-31 19:32:19 +0000 | [diff] [blame] | 277 | static const bool is_steady = true; // constexpr in C++14 |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 278 | |
Howard Hinnant | 756a176 | 2011-05-28 18:34:36 +0000 | [diff] [blame] | 279 | static time_point now() noexcept; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 280 | }; |
| 281 | |
Howard Hinnant | f8f8521 | 2010-11-20 19:16:30 +0000 | [diff] [blame] | 282 | typedef steady_clock high_resolution_clock; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 283 | |
| 284 | } // chrono |
| 285 | |
Marshall Clow | 3b53fab | 2017-08-09 15:42:50 +0000 | [diff] [blame] | 286 | constexpr chrono::hours operator ""h(unsigned long long); // C++14 |
| 287 | constexpr chrono::duration<unspecified , ratio<3600,1>> operator ""h(long double); // C++14 |
| 288 | constexpr chrono::minutes operator ""min(unsigned long long); // C++14 |
| 289 | constexpr chrono::duration<unspecified , ratio<60,1>> operator ""min(long double); // C++14 |
| 290 | constexpr chrono::seconds operator ""s(unsigned long long); // C++14 |
| 291 | constexpr chrono::duration<unspecified > operator ""s(long double); // C++14 |
| 292 | constexpr chrono::milliseconds operator ""ms(unsigned long long); // C++14 |
| 293 | constexpr chrono::duration<unspecified , milli> operator ""ms(long double); // C++14 |
| 294 | constexpr chrono::microseconds operator ""us(unsigned long long); // C++14 |
| 295 | constexpr chrono::duration<unspecified , micro> operator ""us(long double); // C++14 |
| 296 | constexpr chrono::nanoseconds operator ""ns(unsigned long long); // C++14 |
| 297 | constexpr chrono::duration<unspecified , nano> operator ""ns(long double); // C++14 |
Marshall Clow | f182038 | 2013-07-24 21:18:14 +0000 | [diff] [blame] | 298 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 299 | } // std |
| 300 | */ |
| 301 | |
| 302 | #include <__config> |
| 303 | #include <ctime> |
| 304 | #include <type_traits> |
| 305 | #include <ratio> |
| 306 | #include <limits> |
| 307 | |
Howard Hinnant | 08e1747 | 2011-10-17 20:05:10 +0000 | [diff] [blame] | 308 | #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 309 | #pragma GCC system_header |
Howard Hinnant | 08e1747 | 2011-10-17 20:05:10 +0000 | [diff] [blame] | 310 | #endif |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 311 | |
Eric Fiselier | 018a3d5 | 2017-05-31 22:07:49 +0000 | [diff] [blame] | 312 | _LIBCPP_PUSH_MACROS |
| 313 | #include <__undef_macros> |
| 314 | |
| 315 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 316 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 317 | |
| 318 | namespace chrono |
| 319 | { |
| 320 | |
Eric Fiselier | c3589a8 | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 321 | template <class _Rep, class _Period = ratio<1> > class _LIBCPP_TEMPLATE_VIS duration; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 322 | |
Howard Hinnant | d8bc09b | 2010-05-18 17:32:30 +0000 | [diff] [blame] | 323 | template <class _Tp> |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 324 | struct __is_duration : false_type {}; |
| 325 | |
| 326 | template <class _Rep, class _Period> |
| 327 | struct __is_duration<duration<_Rep, _Period> > : true_type {}; |
| 328 | |
| 329 | template <class _Rep, class _Period> |
| 330 | struct __is_duration<const duration<_Rep, _Period> > : true_type {}; |
| 331 | |
| 332 | template <class _Rep, class _Period> |
| 333 | struct __is_duration<volatile duration<_Rep, _Period> > : true_type {}; |
| 334 | |
| 335 | template <class _Rep, class _Period> |
| 336 | struct __is_duration<const volatile duration<_Rep, _Period> > : true_type {}; |
| 337 | |
| 338 | } // chrono |
| 339 | |
| 340 | template <class _Rep1, class _Period1, class _Rep2, class _Period2> |
Eric Fiselier | c3589a8 | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 341 | struct _LIBCPP_TEMPLATE_VIS common_type<chrono::duration<_Rep1, _Period1>, |
Howard Hinnant | 0f678bd | 2013-08-12 18:38:34 +0000 | [diff] [blame] | 342 | chrono::duration<_Rep2, _Period2> > |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 343 | { |
| 344 | typedef chrono::duration<typename common_type<_Rep1, _Rep2>::type, |
| 345 | typename __ratio_gcd<_Period1, _Period2>::type> type; |
| 346 | }; |
| 347 | |
| 348 | namespace chrono { |
| 349 | |
| 350 | // duration_cast |
| 351 | |
| 352 | template <class _FromDuration, class _ToDuration, |
| 353 | class _Period = typename ratio_divide<typename _FromDuration::period, typename _ToDuration::period>::type, |
| 354 | bool = _Period::num == 1, |
| 355 | bool = _Period::den == 1> |
| 356 | struct __duration_cast; |
| 357 | |
| 358 | template <class _FromDuration, class _ToDuration, class _Period> |
| 359 | struct __duration_cast<_FromDuration, _ToDuration, _Period, true, true> |
| 360 | { |
Howard Hinnant | 473f838 | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 361 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 362 | _ToDuration operator()(const _FromDuration& __fd) const |
| 363 | { |
| 364 | return _ToDuration(static_cast<typename _ToDuration::rep>(__fd.count())); |
| 365 | } |
| 366 | }; |
| 367 | |
| 368 | template <class _FromDuration, class _ToDuration, class _Period> |
| 369 | struct __duration_cast<_FromDuration, _ToDuration, _Period, true, false> |
| 370 | { |
Howard Hinnant | 473f838 | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 371 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 372 | _ToDuration operator()(const _FromDuration& __fd) const |
| 373 | { |
| 374 | typedef typename common_type<typename _ToDuration::rep, typename _FromDuration::rep, intmax_t>::type _Ct; |
| 375 | return _ToDuration(static_cast<typename _ToDuration::rep>( |
| 376 | static_cast<_Ct>(__fd.count()) / static_cast<_Ct>(_Period::den))); |
| 377 | } |
| 378 | }; |
| 379 | |
| 380 | template <class _FromDuration, class _ToDuration, class _Period> |
| 381 | struct __duration_cast<_FromDuration, _ToDuration, _Period, false, true> |
| 382 | { |
Howard Hinnant | 473f838 | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 383 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 384 | _ToDuration operator()(const _FromDuration& __fd) const |
| 385 | { |
| 386 | typedef typename common_type<typename _ToDuration::rep, typename _FromDuration::rep, intmax_t>::type _Ct; |
| 387 | return _ToDuration(static_cast<typename _ToDuration::rep>( |
| 388 | static_cast<_Ct>(__fd.count()) * static_cast<_Ct>(_Period::num))); |
| 389 | } |
| 390 | }; |
| 391 | |
| 392 | template <class _FromDuration, class _ToDuration, class _Period> |
| 393 | struct __duration_cast<_FromDuration, _ToDuration, _Period, false, false> |
| 394 | { |
Howard Hinnant | 473f838 | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 395 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 396 | _ToDuration operator()(const _FromDuration& __fd) const |
| 397 | { |
| 398 | typedef typename common_type<typename _ToDuration::rep, typename _FromDuration::rep, intmax_t>::type _Ct; |
| 399 | return _ToDuration(static_cast<typename _ToDuration::rep>( |
| 400 | static_cast<_Ct>(__fd.count()) * static_cast<_Ct>(_Period::num) |
| 401 | / static_cast<_Ct>(_Period::den))); |
| 402 | } |
| 403 | }; |
| 404 | |
| 405 | template <class _ToDuration, class _Rep, class _Period> |
| 406 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 473f838 | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 407 | _LIBCPP_CONSTEXPR |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 408 | typename enable_if |
| 409 | < |
| 410 | __is_duration<_ToDuration>::value, |
| 411 | _ToDuration |
| 412 | >::type |
| 413 | duration_cast(const duration<_Rep, _Period>& __fd) |
| 414 | { |
| 415 | return __duration_cast<duration<_Rep, _Period>, _ToDuration>()(__fd); |
| 416 | } |
| 417 | |
Howard Hinnant | 422a53f | 2010-09-21 21:28:23 +0000 | [diff] [blame] | 418 | template <class _Rep> |
Eric Fiselier | c3589a8 | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 419 | struct _LIBCPP_TEMPLATE_VIS treat_as_floating_point : is_floating_point<_Rep> {}; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 420 | |
Marshall Clow | a3866e4 | 2015-11-30 05:39:30 +0000 | [diff] [blame] | 421 | #if _LIBCPP_STD_VER > 14 && !defined(_LIBCPP_HAS_NO_VARIABLE_TEMPLATES) |
| 422 | template <class _Rep> _LIBCPP_CONSTEXPR bool treat_as_floating_point_v |
| 423 | = treat_as_floating_point<_Rep>::value; |
| 424 | #endif |
| 425 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 426 | template <class _Rep> |
Eric Fiselier | c3589a8 | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 427 | struct _LIBCPP_TEMPLATE_VIS duration_values |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 428 | { |
| 429 | public: |
Howard Hinnant | 473f838 | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 430 | _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR _Rep zero() {return _Rep(0);} |
| 431 | _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR _Rep max() {return numeric_limits<_Rep>::max();} |
| 432 | _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR _Rep min() {return numeric_limits<_Rep>::lowest();} |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 433 | }; |
| 434 | |
Marshall Clow | 223df2e | 2015-11-05 19:33:59 +0000 | [diff] [blame] | 435 | #if _LIBCPP_STD_VER > 14 |
| 436 | template <class _ToDuration, class _Rep, class _Period> |
| 437 | inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR |
| 438 | typename enable_if |
| 439 | < |
| 440 | __is_duration<_ToDuration>::value, |
| 441 | _ToDuration |
| 442 | >::type |
| 443 | floor(const duration<_Rep, _Period>& __d) |
| 444 | { |
| 445 | _ToDuration __t = duration_cast<_ToDuration>(__d); |
| 446 | if (__t > __d) |
| 447 | __t = __t - _ToDuration{1}; |
| 448 | return __t; |
| 449 | } |
| 450 | |
| 451 | template <class _ToDuration, class _Rep, class _Period> |
| 452 | inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR |
| 453 | typename enable_if |
| 454 | < |
| 455 | __is_duration<_ToDuration>::value, |
| 456 | _ToDuration |
| 457 | >::type |
| 458 | ceil(const duration<_Rep, _Period>& __d) |
| 459 | { |
| 460 | _ToDuration __t = duration_cast<_ToDuration>(__d); |
| 461 | if (__t < __d) |
| 462 | __t = __t + _ToDuration{1}; |
| 463 | return __t; |
| 464 | } |
| 465 | |
| 466 | template <class _ToDuration, class _Rep, class _Period> |
| 467 | inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR |
| 468 | typename enable_if |
| 469 | < |
| 470 | __is_duration<_ToDuration>::value, |
| 471 | _ToDuration |
| 472 | >::type |
| 473 | round(const duration<_Rep, _Period>& __d) |
| 474 | { |
| 475 | _ToDuration __lower = floor<_ToDuration>(__d); |
| 476 | _ToDuration __upper = __lower + _ToDuration{1}; |
| 477 | auto __lowerDiff = __d - __lower; |
| 478 | auto __upperDiff = __upper - __d; |
| 479 | if (__lowerDiff < __upperDiff) |
| 480 | return __lower; |
| 481 | if (__lowerDiff > __upperDiff) |
| 482 | return __upper; |
| 483 | return __lower.count() & 1 ? __upper : __lower; |
| 484 | } |
| 485 | #endif |
| 486 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 487 | // duration |
| 488 | |
| 489 | template <class _Rep, class _Period> |
Eric Fiselier | c3589a8 | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 490 | class _LIBCPP_TEMPLATE_VIS duration |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 491 | { |
| 492 | static_assert(!__is_duration<_Rep>::value, "A duration representation can not be a duration"); |
| 493 | static_assert(__is_ratio<_Period>::value, "Second template parameter of duration must be a std::ratio"); |
| 494 | static_assert(_Period::num > 0, "duration period must be positive"); |
Howard Hinnant | 713f4db | 2013-08-31 16:51:56 +0000 | [diff] [blame] | 495 | |
| 496 | template <class _R1, class _R2> |
| 497 | struct __no_overflow |
| 498 | { |
| 499 | private: |
| 500 | static const intmax_t __gcd_n1_n2 = __static_gcd<_R1::num, _R2::num>::value; |
| 501 | static const intmax_t __gcd_d1_d2 = __static_gcd<_R1::den, _R2::den>::value; |
| 502 | static const intmax_t __n1 = _R1::num / __gcd_n1_n2; |
| 503 | static const intmax_t __d1 = _R1::den / __gcd_d1_d2; |
| 504 | static const intmax_t __n2 = _R2::num / __gcd_n1_n2; |
| 505 | static const intmax_t __d2 = _R2::den / __gcd_d1_d2; |
| 506 | static const intmax_t max = -((intmax_t(1) << (sizeof(intmax_t) * CHAR_BIT - 1)) + 1); |
| 507 | |
| 508 | template <intmax_t _Xp, intmax_t _Yp, bool __overflow> |
| 509 | struct __mul // __overflow == false |
| 510 | { |
| 511 | static const intmax_t value = _Xp * _Yp; |
| 512 | }; |
| 513 | |
| 514 | template <intmax_t _Xp, intmax_t _Yp> |
| 515 | struct __mul<_Xp, _Yp, true> |
| 516 | { |
| 517 | static const intmax_t value = 1; |
| 518 | }; |
| 519 | |
| 520 | public: |
| 521 | static const bool value = (__n1 <= max / __d2) && (__n2 <= max / __d1); |
| 522 | typedef ratio<__mul<__n1, __d2, !value>::value, |
| 523 | __mul<__n2, __d1, !value>::value> type; |
| 524 | }; |
| 525 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 526 | public: |
| 527 | typedef _Rep rep; |
Marshall Clow | cf6e0db | 2017-03-21 18:38:57 +0000 | [diff] [blame] | 528 | typedef typename _Period::type period; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 529 | private: |
| 530 | rep __rep_; |
| 531 | public: |
| 532 | |
Marshall Clow | 832b304 | 2013-07-31 19:32:19 +0000 | [diff] [blame] | 533 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR |
Eric Fiselier | 8c57032 | 2016-11-18 06:42:17 +0000 | [diff] [blame] | 534 | #ifndef _LIBCPP_CXX03_LANG |
Marshall Clow | 832b304 | 2013-07-31 19:32:19 +0000 | [diff] [blame] | 535 | duration() = default; |
Marshall Clow | 0777473 | 2013-07-31 19:39:37 +0000 | [diff] [blame] | 536 | #else |
| 537 | duration() {} |
Marshall Clow | 832b304 | 2013-07-31 19:32:19 +0000 | [diff] [blame] | 538 | #endif |
| 539 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 540 | template <class _Rep2> |
Howard Hinnant | 473f838 | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 541 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 542 | explicit duration(const _Rep2& __r, |
| 543 | typename enable_if |
| 544 | < |
| 545 | is_convertible<_Rep2, rep>::value && |
| 546 | (treat_as_floating_point<rep>::value || |
| 547 | !treat_as_floating_point<_Rep2>::value) |
| 548 | >::type* = 0) |
| 549 | : __rep_(__r) {} |
| 550 | |
| 551 | // conversions |
| 552 | template <class _Rep2, class _Period2> |
Howard Hinnant | 473f838 | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 553 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 554 | duration(const duration<_Rep2, _Period2>& __d, |
| 555 | typename enable_if |
| 556 | < |
Howard Hinnant | 713f4db | 2013-08-31 16:51:56 +0000 | [diff] [blame] | 557 | __no_overflow<_Period2, period>::value && ( |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 558 | treat_as_floating_point<rep>::value || |
Howard Hinnant | 713f4db | 2013-08-31 16:51:56 +0000 | [diff] [blame] | 559 | (__no_overflow<_Period2, period>::type::den == 1 && |
| 560 | !treat_as_floating_point<_Rep2>::value)) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 561 | >::type* = 0) |
Howard Hinnant | 0949eed | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 562 | : __rep_(_VSTD::chrono::duration_cast<duration>(__d).count()) {} |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 563 | |
| 564 | // observer |
| 565 | |
Howard Hinnant | 473f838 | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 566 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR rep count() const {return __rep_;} |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 567 | |
| 568 | // arithmetic |
| 569 | |
Marshall Clow | cf6e0db | 2017-03-21 18:38:57 +0000 | [diff] [blame] | 570 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR typename common_type<duration>::type operator+() const {return typename common_type<duration>::type(*this);} |
| 571 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR typename common_type<duration>::type operator-() const {return typename common_type<duration>::type(-__rep_);} |
Marshall Clow | 3df90c9 | 2017-01-04 23:03:24 +0000 | [diff] [blame] | 572 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 duration& operator++() {++__rep_; return *this;} |
| 573 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 duration operator++(int) {return duration(__rep_++);} |
| 574 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 duration& operator--() {--__rep_; return *this;} |
| 575 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 duration operator--(int) {return duration(__rep_--);} |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 576 | |
Marshall Clow | 3df90c9 | 2017-01-04 23:03:24 +0000 | [diff] [blame] | 577 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 duration& operator+=(const duration& __d) {__rep_ += __d.count(); return *this;} |
| 578 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 duration& operator-=(const duration& __d) {__rep_ -= __d.count(); return *this;} |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 579 | |
Marshall Clow | 3df90c9 | 2017-01-04 23:03:24 +0000 | [diff] [blame] | 580 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 duration& operator*=(const rep& rhs) {__rep_ *= rhs; return *this;} |
| 581 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 duration& operator/=(const rep& rhs) {__rep_ /= rhs; return *this;} |
| 582 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 duration& operator%=(const rep& rhs) {__rep_ %= rhs; return *this;} |
| 583 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 duration& operator%=(const duration& rhs) {__rep_ %= rhs.count(); return *this;} |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 584 | |
| 585 | // special values |
| 586 | |
Howard Hinnant | 473f838 | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 587 | _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR duration zero() {return duration(duration_values<rep>::zero());} |
| 588 | _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR duration min() {return duration(duration_values<rep>::min());} |
| 589 | _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR duration max() {return duration(duration_values<rep>::max());} |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 590 | }; |
| 591 | |
| 592 | typedef duration<long long, nano> nanoseconds; |
| 593 | typedef duration<long long, micro> microseconds; |
| 594 | typedef duration<long long, milli> milliseconds; |
| 595 | typedef duration<long long > seconds; |
| 596 | typedef duration< long, ratio< 60> > minutes; |
| 597 | typedef duration< long, ratio<3600> > hours; |
| 598 | |
| 599 | // Duration == |
| 600 | |
| 601 | template <class _LhsDuration, class _RhsDuration> |
| 602 | struct __duration_eq |
| 603 | { |
Howard Hinnant | 473f838 | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 604 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR |
Howard Hinnant | b9d9fb4 | 2013-06-28 18:09:35 +0000 | [diff] [blame] | 605 | bool operator()(const _LhsDuration& __lhs, const _RhsDuration& __rhs) const |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 606 | { |
| 607 | typedef typename common_type<_LhsDuration, _RhsDuration>::type _Ct; |
| 608 | return _Ct(__lhs).count() == _Ct(__rhs).count(); |
| 609 | } |
| 610 | }; |
| 611 | |
| 612 | template <class _LhsDuration> |
| 613 | struct __duration_eq<_LhsDuration, _LhsDuration> |
| 614 | { |
Howard Hinnant | 473f838 | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 615 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR |
Howard Hinnant | b9d9fb4 | 2013-06-28 18:09:35 +0000 | [diff] [blame] | 616 | bool operator()(const _LhsDuration& __lhs, const _LhsDuration& __rhs) const |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 617 | {return __lhs.count() == __rhs.count();} |
| 618 | }; |
| 619 | |
| 620 | template <class _Rep1, class _Period1, class _Rep2, class _Period2> |
| 621 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 473f838 | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 622 | _LIBCPP_CONSTEXPR |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 623 | bool |
| 624 | operator==(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs) |
| 625 | { |
| 626 | return __duration_eq<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >()(__lhs, __rhs); |
| 627 | } |
| 628 | |
| 629 | // Duration != |
| 630 | |
| 631 | template <class _Rep1, class _Period1, class _Rep2, class _Period2> |
| 632 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 473f838 | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 633 | _LIBCPP_CONSTEXPR |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 634 | bool |
| 635 | operator!=(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs) |
| 636 | { |
| 637 | return !(__lhs == __rhs); |
| 638 | } |
| 639 | |
| 640 | // Duration < |
| 641 | |
| 642 | template <class _LhsDuration, class _RhsDuration> |
| 643 | struct __duration_lt |
| 644 | { |
Howard Hinnant | 473f838 | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 645 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR |
Howard Hinnant | b9d9fb4 | 2013-06-28 18:09:35 +0000 | [diff] [blame] | 646 | bool operator()(const _LhsDuration& __lhs, const _RhsDuration& __rhs) const |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 647 | { |
| 648 | typedef typename common_type<_LhsDuration, _RhsDuration>::type _Ct; |
| 649 | return _Ct(__lhs).count() < _Ct(__rhs).count(); |
| 650 | } |
| 651 | }; |
| 652 | |
| 653 | template <class _LhsDuration> |
| 654 | struct __duration_lt<_LhsDuration, _LhsDuration> |
| 655 | { |
Howard Hinnant | 473f838 | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 656 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR |
Howard Hinnant | b9d9fb4 | 2013-06-28 18:09:35 +0000 | [diff] [blame] | 657 | bool operator()(const _LhsDuration& __lhs, const _LhsDuration& __rhs) const |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 658 | {return __lhs.count() < __rhs.count();} |
| 659 | }; |
| 660 | |
| 661 | template <class _Rep1, class _Period1, class _Rep2, class _Period2> |
| 662 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 473f838 | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 663 | _LIBCPP_CONSTEXPR |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 664 | bool |
| 665 | operator< (const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs) |
| 666 | { |
| 667 | return __duration_lt<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >()(__lhs, __rhs); |
| 668 | } |
| 669 | |
| 670 | // Duration > |
| 671 | |
| 672 | template <class _Rep1, class _Period1, class _Rep2, class _Period2> |
| 673 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 473f838 | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 674 | _LIBCPP_CONSTEXPR |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 675 | bool |
| 676 | operator> (const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs) |
| 677 | { |
| 678 | return __rhs < __lhs; |
| 679 | } |
| 680 | |
| 681 | // Duration <= |
| 682 | |
| 683 | template <class _Rep1, class _Period1, class _Rep2, class _Period2> |
| 684 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 473f838 | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 685 | _LIBCPP_CONSTEXPR |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 686 | bool |
| 687 | operator<=(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs) |
| 688 | { |
| 689 | return !(__rhs < __lhs); |
| 690 | } |
| 691 | |
| 692 | // Duration >= |
| 693 | |
| 694 | template <class _Rep1, class _Period1, class _Rep2, class _Period2> |
| 695 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 473f838 | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 696 | _LIBCPP_CONSTEXPR |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 697 | bool |
| 698 | operator>=(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs) |
| 699 | { |
| 700 | return !(__lhs < __rhs); |
| 701 | } |
| 702 | |
| 703 | // Duration + |
| 704 | |
| 705 | template <class _Rep1, class _Period1, class _Rep2, class _Period2> |
| 706 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 473f838 | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 707 | _LIBCPP_CONSTEXPR |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 708 | typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type |
| 709 | operator+(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs) |
| 710 | { |
Howard Hinnant | 473f838 | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 711 | typedef typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type _Cd; |
| 712 | return _Cd(_Cd(__lhs).count() + _Cd(__rhs).count()); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 713 | } |
| 714 | |
| 715 | // Duration - |
| 716 | |
| 717 | template <class _Rep1, class _Period1, class _Rep2, class _Period2> |
| 718 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 473f838 | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 719 | _LIBCPP_CONSTEXPR |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 720 | typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type |
| 721 | operator-(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs) |
| 722 | { |
Howard Hinnant | 473f838 | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 723 | typedef typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type _Cd; |
| 724 | return _Cd(_Cd(__lhs).count() - _Cd(__rhs).count()); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 725 | } |
| 726 | |
| 727 | // Duration * |
| 728 | |
| 729 | template <class _Rep1, class _Period, class _Rep2> |
| 730 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 473f838 | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 731 | _LIBCPP_CONSTEXPR |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 732 | typename enable_if |
| 733 | < |
| 734 | is_convertible<_Rep2, typename common_type<_Rep1, _Rep2>::type>::value, |
| 735 | duration<typename common_type<_Rep1, _Rep2>::type, _Period> |
| 736 | >::type |
| 737 | operator*(const duration<_Rep1, _Period>& __d, const _Rep2& __s) |
| 738 | { |
| 739 | typedef typename common_type<_Rep1, _Rep2>::type _Cr; |
Howard Hinnant | 473f838 | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 740 | typedef duration<_Cr, _Period> _Cd; |
| 741 | return _Cd(_Cd(__d).count() * static_cast<_Cr>(__s)); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 742 | } |
| 743 | |
| 744 | template <class _Rep1, class _Period, class _Rep2> |
| 745 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 473f838 | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 746 | _LIBCPP_CONSTEXPR |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 747 | typename enable_if |
| 748 | < |
| 749 | is_convertible<_Rep1, typename common_type<_Rep1, _Rep2>::type>::value, |
| 750 | duration<typename common_type<_Rep1, _Rep2>::type, _Period> |
| 751 | >::type |
| 752 | operator*(const _Rep1& __s, const duration<_Rep2, _Period>& __d) |
| 753 | { |
| 754 | return __d * __s; |
| 755 | } |
| 756 | |
| 757 | // Duration / |
| 758 | |
| 759 | template <class _Duration, class _Rep, bool = __is_duration<_Rep>::value> |
| 760 | struct __duration_divide_result |
| 761 | { |
| 762 | }; |
| 763 | |
| 764 | template <class _Duration, class _Rep2, |
| 765 | bool = is_convertible<_Rep2, |
| 766 | typename common_type<typename _Duration::rep, _Rep2>::type>::value> |
| 767 | struct __duration_divide_imp |
| 768 | { |
| 769 | }; |
| 770 | |
| 771 | template <class _Rep1, class _Period, class _Rep2> |
| 772 | struct __duration_divide_imp<duration<_Rep1, _Period>, _Rep2, true> |
| 773 | { |
| 774 | typedef duration<typename common_type<_Rep1, _Rep2>::type, _Period> type; |
| 775 | }; |
| 776 | |
| 777 | template <class _Rep1, class _Period, class _Rep2> |
| 778 | struct __duration_divide_result<duration<_Rep1, _Period>, _Rep2, false> |
| 779 | : __duration_divide_imp<duration<_Rep1, _Period>, _Rep2> |
| 780 | { |
| 781 | }; |
| 782 | |
| 783 | template <class _Rep1, class _Period, class _Rep2> |
| 784 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 473f838 | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 785 | _LIBCPP_CONSTEXPR |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 786 | typename __duration_divide_result<duration<_Rep1, _Period>, _Rep2>::type |
| 787 | operator/(const duration<_Rep1, _Period>& __d, const _Rep2& __s) |
| 788 | { |
| 789 | typedef typename common_type<_Rep1, _Rep2>::type _Cr; |
Howard Hinnant | 473f838 | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 790 | typedef duration<_Cr, _Period> _Cd; |
| 791 | return _Cd(_Cd(__d).count() / static_cast<_Cr>(__s)); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 792 | } |
| 793 | |
| 794 | template <class _Rep1, class _Period1, class _Rep2, class _Period2> |
| 795 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 473f838 | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 796 | _LIBCPP_CONSTEXPR |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 797 | typename common_type<_Rep1, _Rep2>::type |
| 798 | operator/(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs) |
| 799 | { |
| 800 | typedef typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type _Ct; |
| 801 | return _Ct(__lhs).count() / _Ct(__rhs).count(); |
| 802 | } |
| 803 | |
| 804 | // Duration % |
| 805 | |
| 806 | template <class _Rep1, class _Period, class _Rep2> |
| 807 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 473f838 | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 808 | _LIBCPP_CONSTEXPR |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 809 | typename __duration_divide_result<duration<_Rep1, _Period>, _Rep2>::type |
| 810 | operator%(const duration<_Rep1, _Period>& __d, const _Rep2& __s) |
| 811 | { |
| 812 | typedef typename common_type<_Rep1, _Rep2>::type _Cr; |
Howard Hinnant | 473f838 | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 813 | typedef duration<_Cr, _Period> _Cd; |
| 814 | return _Cd(_Cd(__d).count() % static_cast<_Cr>(__s)); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 815 | } |
| 816 | |
| 817 | template <class _Rep1, class _Period1, class _Rep2, class _Period2> |
| 818 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 473f838 | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 819 | _LIBCPP_CONSTEXPR |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 820 | typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type |
| 821 | operator%(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs) |
| 822 | { |
Howard Hinnant | 473f838 | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 823 | typedef typename common_type<_Rep1, _Rep2>::type _Cr; |
| 824 | typedef typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type _Cd; |
| 825 | return _Cd(static_cast<_Cr>(_Cd(__lhs).count()) % static_cast<_Cr>(_Cd(__rhs).count())); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 826 | } |
| 827 | |
| 828 | ////////////////////////////////////////////////////////// |
| 829 | ///////////////////// time_point ///////////////////////// |
| 830 | ////////////////////////////////////////////////////////// |
| 831 | |
| 832 | template <class _Clock, class _Duration = typename _Clock::duration> |
Eric Fiselier | c3589a8 | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 833 | class _LIBCPP_TEMPLATE_VIS time_point |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 834 | { |
| 835 | static_assert(__is_duration<_Duration>::value, |
| 836 | "Second template parameter of time_point must be a std::chrono::duration"); |
| 837 | public: |
| 838 | typedef _Clock clock; |
| 839 | typedef _Duration duration; |
| 840 | typedef typename duration::rep rep; |
| 841 | typedef typename duration::period period; |
| 842 | private: |
| 843 | duration __d_; |
| 844 | |
| 845 | public: |
Marshall Clow | 832b304 | 2013-07-31 19:32:19 +0000 | [diff] [blame] | 846 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 time_point() : __d_(duration::zero()) {} |
| 847 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 explicit time_point(const duration& __d) : __d_(__d) {} |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 848 | |
| 849 | // conversions |
| 850 | template <class _Duration2> |
Marshall Clow | 832b304 | 2013-07-31 19:32:19 +0000 | [diff] [blame] | 851 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 852 | time_point(const time_point<clock, _Duration2>& t, |
| 853 | typename enable_if |
| 854 | < |
| 855 | is_convertible<_Duration2, duration>::value |
| 856 | >::type* = 0) |
| 857 | : __d_(t.time_since_epoch()) {} |
| 858 | |
| 859 | // observer |
| 860 | |
Marshall Clow | 832b304 | 2013-07-31 19:32:19 +0000 | [diff] [blame] | 861 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 duration time_since_epoch() const {return __d_;} |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 862 | |
| 863 | // arithmetic |
| 864 | |
Howard Hinnant | 171771a | 2013-07-08 21:06:38 +0000 | [diff] [blame] | 865 | _LIBCPP_INLINE_VISIBILITY time_point& operator+=(const duration& __d) {__d_ += __d; return *this;} |
| 866 | _LIBCPP_INLINE_VISIBILITY time_point& operator-=(const duration& __d) {__d_ -= __d; return *this;} |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 867 | |
| 868 | // special values |
| 869 | |
Howard Hinnant | 473f838 | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 870 | _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR time_point min() {return time_point(duration::min());} |
| 871 | _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR time_point max() {return time_point(duration::max());} |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 872 | }; |
| 873 | |
| 874 | } // chrono |
| 875 | |
| 876 | template <class _Clock, class _Duration1, class _Duration2> |
Eric Fiselier | c3589a8 | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 877 | struct _LIBCPP_TEMPLATE_VIS common_type<chrono::time_point<_Clock, _Duration1>, |
Howard Hinnant | 0f678bd | 2013-08-12 18:38:34 +0000 | [diff] [blame] | 878 | chrono::time_point<_Clock, _Duration2> > |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 879 | { |
| 880 | typedef chrono::time_point<_Clock, typename common_type<_Duration1, _Duration2>::type> type; |
| 881 | }; |
| 882 | |
| 883 | namespace chrono { |
| 884 | |
| 885 | template <class _ToDuration, class _Clock, class _Duration> |
Marshall Clow | 832b304 | 2013-07-31 19:32:19 +0000 | [diff] [blame] | 886 | inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 887 | time_point<_Clock, _ToDuration> |
| 888 | time_point_cast(const time_point<_Clock, _Duration>& __t) |
| 889 | { |
Howard Hinnant | 0949eed | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 890 | return time_point<_Clock, _ToDuration>(_VSTD::chrono::duration_cast<_ToDuration>(__t.time_since_epoch())); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 891 | } |
| 892 | |
Marshall Clow | 223df2e | 2015-11-05 19:33:59 +0000 | [diff] [blame] | 893 | #if _LIBCPP_STD_VER > 14 |
| 894 | template <class _ToDuration, class _Clock, class _Duration> |
| 895 | inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR |
| 896 | typename enable_if |
| 897 | < |
| 898 | __is_duration<_ToDuration>::value, |
| 899 | time_point<_Clock, _ToDuration> |
| 900 | >::type |
| 901 | floor(const time_point<_Clock, _Duration>& __t) |
| 902 | { |
| 903 | return time_point<_Clock, _ToDuration>{floor<_ToDuration>(__t.time_since_epoch())}; |
| 904 | } |
| 905 | |
| 906 | template <class _ToDuration, class _Clock, class _Duration> |
| 907 | inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR |
| 908 | typename enable_if |
| 909 | < |
| 910 | __is_duration<_ToDuration>::value, |
| 911 | time_point<_Clock, _ToDuration> |
| 912 | >::type |
| 913 | ceil(const time_point<_Clock, _Duration>& __t) |
| 914 | { |
| 915 | return time_point<_Clock, _ToDuration>{ceil<_ToDuration>(__t.time_since_epoch())}; |
| 916 | } |
| 917 | |
| 918 | template <class _ToDuration, class _Clock, class _Duration> |
| 919 | inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR |
| 920 | typename enable_if |
| 921 | < |
| 922 | __is_duration<_ToDuration>::value, |
| 923 | time_point<_Clock, _ToDuration> |
| 924 | >::type |
| 925 | round(const time_point<_Clock, _Duration>& __t) |
| 926 | { |
| 927 | return time_point<_Clock, _ToDuration>{round<_ToDuration>(__t.time_since_epoch())}; |
| 928 | } |
| 929 | |
| 930 | template <class _Rep, class _Period> |
| 931 | inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR |
| 932 | typename enable_if |
| 933 | < |
| 934 | numeric_limits<_Rep>::is_signed, |
| 935 | duration<_Rep, _Period> |
| 936 | >::type |
| 937 | abs(duration<_Rep, _Period> __d) |
| 938 | { |
| 939 | return __d >= __d.zero() ? __d : -__d; |
| 940 | } |
| 941 | #endif |
| 942 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 943 | // time_point == |
| 944 | |
| 945 | template <class _Clock, class _Duration1, class _Duration2> |
Marshall Clow | 832b304 | 2013-07-31 19:32:19 +0000 | [diff] [blame] | 946 | inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 947 | bool |
| 948 | operator==(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs) |
| 949 | { |
| 950 | return __lhs.time_since_epoch() == __rhs.time_since_epoch(); |
| 951 | } |
| 952 | |
| 953 | // time_point != |
| 954 | |
| 955 | template <class _Clock, class _Duration1, class _Duration2> |
Marshall Clow | 832b304 | 2013-07-31 19:32:19 +0000 | [diff] [blame] | 956 | inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 957 | bool |
| 958 | operator!=(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs) |
| 959 | { |
| 960 | return !(__lhs == __rhs); |
| 961 | } |
| 962 | |
| 963 | // time_point < |
| 964 | |
| 965 | template <class _Clock, class _Duration1, class _Duration2> |
Marshall Clow | 832b304 | 2013-07-31 19:32:19 +0000 | [diff] [blame] | 966 | inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 967 | bool |
| 968 | operator<(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs) |
| 969 | { |
| 970 | return __lhs.time_since_epoch() < __rhs.time_since_epoch(); |
| 971 | } |
| 972 | |
| 973 | // time_point > |
| 974 | |
| 975 | template <class _Clock, class _Duration1, class _Duration2> |
Marshall Clow | 832b304 | 2013-07-31 19:32:19 +0000 | [diff] [blame] | 976 | inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 977 | bool |
| 978 | operator>(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs) |
| 979 | { |
| 980 | return __rhs < __lhs; |
| 981 | } |
| 982 | |
| 983 | // time_point <= |
| 984 | |
| 985 | template <class _Clock, class _Duration1, class _Duration2> |
Marshall Clow | 832b304 | 2013-07-31 19:32:19 +0000 | [diff] [blame] | 986 | inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 987 | bool |
| 988 | operator<=(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs) |
| 989 | { |
| 990 | return !(__rhs < __lhs); |
| 991 | } |
| 992 | |
| 993 | // time_point >= |
| 994 | |
| 995 | template <class _Clock, class _Duration1, class _Duration2> |
Marshall Clow | 832b304 | 2013-07-31 19:32:19 +0000 | [diff] [blame] | 996 | inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 997 | bool |
| 998 | operator>=(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs) |
| 999 | { |
| 1000 | return !(__lhs < __rhs); |
| 1001 | } |
| 1002 | |
| 1003 | // time_point operator+(time_point x, duration y); |
| 1004 | |
| 1005 | template <class _Clock, class _Duration1, class _Rep2, class _Period2> |
Marshall Clow | 832b304 | 2013-07-31 19:32:19 +0000 | [diff] [blame] | 1006 | inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1007 | time_point<_Clock, typename common_type<_Duration1, duration<_Rep2, _Period2> >::type> |
| 1008 | operator+(const time_point<_Clock, _Duration1>& __lhs, const duration<_Rep2, _Period2>& __rhs) |
| 1009 | { |
| 1010 | typedef time_point<_Clock, typename common_type<_Duration1, duration<_Rep2, _Period2> >::type> _Tr; |
Marshall Clow | 832b304 | 2013-07-31 19:32:19 +0000 | [diff] [blame] | 1011 | return _Tr (__lhs.time_since_epoch() + __rhs); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1012 | } |
| 1013 | |
| 1014 | // time_point operator+(duration x, time_point y); |
| 1015 | |
| 1016 | template <class _Rep1, class _Period1, class _Clock, class _Duration2> |
Marshall Clow | 832b304 | 2013-07-31 19:32:19 +0000 | [diff] [blame] | 1017 | inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1018 | time_point<_Clock, typename common_type<duration<_Rep1, _Period1>, _Duration2>::type> |
| 1019 | operator+(const duration<_Rep1, _Period1>& __lhs, const time_point<_Clock, _Duration2>& __rhs) |
| 1020 | { |
| 1021 | return __rhs + __lhs; |
| 1022 | } |
| 1023 | |
| 1024 | // time_point operator-(time_point x, duration y); |
| 1025 | |
| 1026 | template <class _Clock, class _Duration1, class _Rep2, class _Period2> |
Marshall Clow | 832b304 | 2013-07-31 19:32:19 +0000 | [diff] [blame] | 1027 | inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1028 | time_point<_Clock, typename common_type<_Duration1, duration<_Rep2, _Period2> >::type> |
| 1029 | operator-(const time_point<_Clock, _Duration1>& __lhs, const duration<_Rep2, _Period2>& __rhs) |
| 1030 | { |
Marshall Clow | db7fa11 | 2016-11-14 18:22:19 +0000 | [diff] [blame] | 1031 | typedef time_point<_Clock, typename common_type<_Duration1, duration<_Rep2, _Period2> >::type> _Ret; |
| 1032 | return _Ret(__lhs.time_since_epoch() -__rhs); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1033 | } |
| 1034 | |
| 1035 | // duration operator-(time_point x, time_point y); |
| 1036 | |
| 1037 | template <class _Clock, class _Duration1, class _Duration2> |
Marshall Clow | 832b304 | 2013-07-31 19:32:19 +0000 | [diff] [blame] | 1038 | inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1039 | typename common_type<_Duration1, _Duration2>::type |
| 1040 | operator-(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs) |
| 1041 | { |
| 1042 | return __lhs.time_since_epoch() - __rhs.time_since_epoch(); |
| 1043 | } |
| 1044 | |
| 1045 | ////////////////////////////////////////////////////////// |
| 1046 | /////////////////////// clocks /////////////////////////// |
| 1047 | ////////////////////////////////////////////////////////// |
| 1048 | |
Howard Hinnant | 83eade6 | 2013-03-06 23:30:19 +0000 | [diff] [blame] | 1049 | class _LIBCPP_TYPE_VIS system_clock |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1050 | { |
| 1051 | public: |
| 1052 | typedef microseconds duration; |
| 1053 | typedef duration::rep rep; |
| 1054 | typedef duration::period period; |
| 1055 | typedef chrono::time_point<system_clock> time_point; |
Marshall Clow | 832b304 | 2013-07-31 19:32:19 +0000 | [diff] [blame] | 1056 | static _LIBCPP_CONSTEXPR_AFTER_CXX11 const bool is_steady = false; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1057 | |
Howard Hinnant | 756a176 | 2011-05-28 18:34:36 +0000 | [diff] [blame] | 1058 | static time_point now() _NOEXCEPT; |
| 1059 | static time_t to_time_t (const time_point& __t) _NOEXCEPT; |
| 1060 | static time_point from_time_t(time_t __t) _NOEXCEPT; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1061 | }; |
| 1062 | |
Jonathan Roelofs | c578065 | 2014-09-02 21:14:38 +0000 | [diff] [blame] | 1063 | #ifndef _LIBCPP_HAS_NO_MONOTONIC_CLOCK |
Howard Hinnant | 83eade6 | 2013-03-06 23:30:19 +0000 | [diff] [blame] | 1064 | class _LIBCPP_TYPE_VIS steady_clock |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1065 | { |
| 1066 | public: |
| 1067 | typedef nanoseconds duration; |
| 1068 | typedef duration::rep rep; |
| 1069 | typedef duration::period period; |
Howard Hinnant | f8f8521 | 2010-11-20 19:16:30 +0000 | [diff] [blame] | 1070 | typedef chrono::time_point<steady_clock, duration> time_point; |
Marshall Clow | 832b304 | 2013-07-31 19:32:19 +0000 | [diff] [blame] | 1071 | static _LIBCPP_CONSTEXPR_AFTER_CXX11 const bool is_steady = true; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1072 | |
Howard Hinnant | 756a176 | 2011-05-28 18:34:36 +0000 | [diff] [blame] | 1073 | static time_point now() _NOEXCEPT; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1074 | }; |
| 1075 | |
Howard Hinnant | f8f8521 | 2010-11-20 19:16:30 +0000 | [diff] [blame] | 1076 | typedef steady_clock high_resolution_clock; |
Jonathan Roelofs | c578065 | 2014-09-02 21:14:38 +0000 | [diff] [blame] | 1077 | #else |
| 1078 | typedef system_clock high_resolution_clock; |
| 1079 | #endif |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1080 | |
| 1081 | } // chrono |
| 1082 | |
Marshall Clow | 8d9dd7a | 2013-10-05 21:18:32 +0000 | [diff] [blame] | 1083 | #if _LIBCPP_STD_VER > 11 |
| 1084 | // Suffixes for duration literals [time.duration.literals] |
| 1085 | inline namespace literals |
Marshall Clow | f182038 | 2013-07-24 21:18:14 +0000 | [diff] [blame] | 1086 | { |
| 1087 | inline namespace chrono_literals |
| 1088 | { |
| 1089 | |
Marshall Clow | 3b53fab | 2017-08-09 15:42:50 +0000 | [diff] [blame] | 1090 | constexpr chrono::hours operator""h(unsigned long long __h) |
Howard Hinnant | ab61b2c | 2013-08-07 19:39:48 +0000 | [diff] [blame] | 1091 | { |
| 1092 | return chrono::hours(static_cast<chrono::hours::rep>(__h)); |
| 1093 | } |
Marshall Clow | f182038 | 2013-07-24 21:18:14 +0000 | [diff] [blame] | 1094 | |
Marshall Clow | 3b53fab | 2017-08-09 15:42:50 +0000 | [diff] [blame] | 1095 | constexpr chrono::duration<long double, ratio<3600,1>> operator""h(long double __h) |
Howard Hinnant | ab61b2c | 2013-08-07 19:39:48 +0000 | [diff] [blame] | 1096 | { |
| 1097 | return chrono::duration<long double, ratio<3600,1>>(__h); |
| 1098 | } |
Marshall Clow | f182038 | 2013-07-24 21:18:14 +0000 | [diff] [blame] | 1099 | |
| 1100 | |
Marshall Clow | 3b53fab | 2017-08-09 15:42:50 +0000 | [diff] [blame] | 1101 | constexpr chrono::minutes operator""min(unsigned long long __m) |
Howard Hinnant | ab61b2c | 2013-08-07 19:39:48 +0000 | [diff] [blame] | 1102 | { |
| 1103 | return chrono::minutes(static_cast<chrono::minutes::rep>(__m)); |
| 1104 | } |
Marshall Clow | f182038 | 2013-07-24 21:18:14 +0000 | [diff] [blame] | 1105 | |
Marshall Clow | 3b53fab | 2017-08-09 15:42:50 +0000 | [diff] [blame] | 1106 | constexpr chrono::duration<long double, ratio<60,1>> operator""min(long double __m) |
Howard Hinnant | ab61b2c | 2013-08-07 19:39:48 +0000 | [diff] [blame] | 1107 | { |
| 1108 | return chrono::duration<long double, ratio<60,1>> (__m); |
| 1109 | } |
Marshall Clow | f182038 | 2013-07-24 21:18:14 +0000 | [diff] [blame] | 1110 | |
| 1111 | |
Marshall Clow | 3b53fab | 2017-08-09 15:42:50 +0000 | [diff] [blame] | 1112 | constexpr chrono::seconds operator""s(unsigned long long __s) |
Howard Hinnant | ab61b2c | 2013-08-07 19:39:48 +0000 | [diff] [blame] | 1113 | { |
| 1114 | return chrono::seconds(static_cast<chrono::seconds::rep>(__s)); |
| 1115 | } |
Marshall Clow | f182038 | 2013-07-24 21:18:14 +0000 | [diff] [blame] | 1116 | |
Marshall Clow | 3b53fab | 2017-08-09 15:42:50 +0000 | [diff] [blame] | 1117 | constexpr chrono::duration<long double> operator""s(long double __s) |
Howard Hinnant | ab61b2c | 2013-08-07 19:39:48 +0000 | [diff] [blame] | 1118 | { |
| 1119 | return chrono::duration<long double> (__s); |
| 1120 | } |
Marshall Clow | f182038 | 2013-07-24 21:18:14 +0000 | [diff] [blame] | 1121 | |
| 1122 | |
Marshall Clow | 3b53fab | 2017-08-09 15:42:50 +0000 | [diff] [blame] | 1123 | constexpr chrono::milliseconds operator""ms(unsigned long long __ms) |
Howard Hinnant | ab61b2c | 2013-08-07 19:39:48 +0000 | [diff] [blame] | 1124 | { |
| 1125 | return chrono::milliseconds(static_cast<chrono::milliseconds::rep>(__ms)); |
| 1126 | } |
Marshall Clow | f182038 | 2013-07-24 21:18:14 +0000 | [diff] [blame] | 1127 | |
Marshall Clow | 3b53fab | 2017-08-09 15:42:50 +0000 | [diff] [blame] | 1128 | constexpr chrono::duration<long double, milli> operator""ms(long double __ms) |
Howard Hinnant | ab61b2c | 2013-08-07 19:39:48 +0000 | [diff] [blame] | 1129 | { |
| 1130 | return chrono::duration<long double, milli>(__ms); |
| 1131 | } |
Marshall Clow | f182038 | 2013-07-24 21:18:14 +0000 | [diff] [blame] | 1132 | |
| 1133 | |
Marshall Clow | 3b53fab | 2017-08-09 15:42:50 +0000 | [diff] [blame] | 1134 | constexpr chrono::microseconds operator""us(unsigned long long __us) |
Howard Hinnant | ab61b2c | 2013-08-07 19:39:48 +0000 | [diff] [blame] | 1135 | { |
| 1136 | return chrono::microseconds(static_cast<chrono::microseconds::rep>(__us)); |
| 1137 | } |
Marshall Clow | f182038 | 2013-07-24 21:18:14 +0000 | [diff] [blame] | 1138 | |
Marshall Clow | 3b53fab | 2017-08-09 15:42:50 +0000 | [diff] [blame] | 1139 | constexpr chrono::duration<long double, micro> operator""us(long double __us) |
Howard Hinnant | ab61b2c | 2013-08-07 19:39:48 +0000 | [diff] [blame] | 1140 | { |
| 1141 | return chrono::duration<long double, micro> (__us); |
| 1142 | } |
| 1143 | |
Marshall Clow | f182038 | 2013-07-24 21:18:14 +0000 | [diff] [blame] | 1144 | |
Marshall Clow | 3b53fab | 2017-08-09 15:42:50 +0000 | [diff] [blame] | 1145 | constexpr chrono::nanoseconds operator""ns(unsigned long long __ns) |
Howard Hinnant | ab61b2c | 2013-08-07 19:39:48 +0000 | [diff] [blame] | 1146 | { |
| 1147 | return chrono::nanoseconds(static_cast<chrono::nanoseconds::rep>(__ns)); |
| 1148 | } |
Marshall Clow | f182038 | 2013-07-24 21:18:14 +0000 | [diff] [blame] | 1149 | |
Marshall Clow | 3b53fab | 2017-08-09 15:42:50 +0000 | [diff] [blame] | 1150 | constexpr chrono::duration<long double, nano> operator""ns(long double __ns) |
Howard Hinnant | ab61b2c | 2013-08-07 19:39:48 +0000 | [diff] [blame] | 1151 | { |
| 1152 | return chrono::duration<long double, nano> (__ns); |
| 1153 | } |
Marshall Clow | f182038 | 2013-07-24 21:18:14 +0000 | [diff] [blame] | 1154 | |
| 1155 | }} |
Marshall Clow | 8d9dd7a | 2013-10-05 21:18:32 +0000 | [diff] [blame] | 1156 | |
| 1157 | namespace chrono { // hoist the literals into namespace std::chrono |
| 1158 | using namespace literals::chrono_literals; |
| 1159 | } |
| 1160 | |
Marshall Clow | f182038 | 2013-07-24 21:18:14 +0000 | [diff] [blame] | 1161 | #endif |
| 1162 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1163 | _LIBCPP_END_NAMESPACE_STD |
| 1164 | |
Eric Fiselier | 018a3d5 | 2017-05-31 22:07:49 +0000 | [diff] [blame] | 1165 | _LIBCPP_POP_MACROS |
| 1166 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1167 | #endif // _LIBCPP_CHRONO |