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; |
| 51 | typedef Period period; |
| 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 | |
Howard Hinnant | 473f838 | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 78 | constexpr duration operator+() const; |
| 79 | constexpr duration operator-() const; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 80 | duration& operator++(); |
| 81 | duration operator++(int); |
| 82 | duration& operator--(); |
| 83 | duration operator--(int); |
| 84 | |
| 85 | duration& operator+=(const duration& d); |
| 86 | duration& operator-=(const duration& d); |
| 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 | f182038 | 2013-07-24 21:18:14 +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 |
| 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 | 66c6f97 | 2011-11-29 16:45:27 +0000 | [diff] [blame] | 308 | #include <__undef_min_max> |
| 309 | |
Howard Hinnant | 08e1747 | 2011-10-17 20:05:10 +0000 | [diff] [blame] | 310 | #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 311 | #pragma GCC system_header |
Howard Hinnant | 08e1747 | 2011-10-17 20:05:10 +0000 | [diff] [blame] | 312 | #endif |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 313 | |
| 314 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 315 | |
| 316 | namespace chrono |
| 317 | { |
| 318 | |
Howard Hinnant | 0f678bd | 2013-08-12 18:38:34 +0000 | [diff] [blame] | 319 | template <class _Rep, class _Period = ratio<1> > class _LIBCPP_TYPE_VIS_ONLY duration; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 320 | |
Howard Hinnant | d8bc09b | 2010-05-18 17:32:30 +0000 | [diff] [blame] | 321 | template <class _Tp> |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 322 | struct __is_duration : false_type {}; |
| 323 | |
| 324 | template <class _Rep, class _Period> |
| 325 | struct __is_duration<duration<_Rep, _Period> > : true_type {}; |
| 326 | |
| 327 | template <class _Rep, class _Period> |
| 328 | struct __is_duration<const duration<_Rep, _Period> > : true_type {}; |
| 329 | |
| 330 | template <class _Rep, class _Period> |
| 331 | struct __is_duration<volatile duration<_Rep, _Period> > : true_type {}; |
| 332 | |
| 333 | template <class _Rep, class _Period> |
| 334 | struct __is_duration<const volatile duration<_Rep, _Period> > : true_type {}; |
| 335 | |
| 336 | } // chrono |
| 337 | |
| 338 | template <class _Rep1, class _Period1, class _Rep2, class _Period2> |
Howard Hinnant | 0f678bd | 2013-08-12 18:38:34 +0000 | [diff] [blame] | 339 | struct _LIBCPP_TYPE_VIS_ONLY common_type<chrono::duration<_Rep1, _Period1>, |
| 340 | chrono::duration<_Rep2, _Period2> > |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 341 | { |
| 342 | typedef chrono::duration<typename common_type<_Rep1, _Rep2>::type, |
| 343 | typename __ratio_gcd<_Period1, _Period2>::type> type; |
| 344 | }; |
| 345 | |
| 346 | namespace chrono { |
| 347 | |
| 348 | // duration_cast |
| 349 | |
| 350 | template <class _FromDuration, class _ToDuration, |
| 351 | class _Period = typename ratio_divide<typename _FromDuration::period, typename _ToDuration::period>::type, |
| 352 | bool = _Period::num == 1, |
| 353 | bool = _Period::den == 1> |
| 354 | struct __duration_cast; |
| 355 | |
| 356 | template <class _FromDuration, class _ToDuration, class _Period> |
| 357 | struct __duration_cast<_FromDuration, _ToDuration, _Period, true, true> |
| 358 | { |
Howard Hinnant | 473f838 | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 359 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 360 | _ToDuration operator()(const _FromDuration& __fd) const |
| 361 | { |
| 362 | return _ToDuration(static_cast<typename _ToDuration::rep>(__fd.count())); |
| 363 | } |
| 364 | }; |
| 365 | |
| 366 | template <class _FromDuration, class _ToDuration, class _Period> |
| 367 | struct __duration_cast<_FromDuration, _ToDuration, _Period, true, false> |
| 368 | { |
Howard Hinnant | 473f838 | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 369 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 370 | _ToDuration operator()(const _FromDuration& __fd) const |
| 371 | { |
| 372 | typedef typename common_type<typename _ToDuration::rep, typename _FromDuration::rep, intmax_t>::type _Ct; |
| 373 | return _ToDuration(static_cast<typename _ToDuration::rep>( |
| 374 | static_cast<_Ct>(__fd.count()) / static_cast<_Ct>(_Period::den))); |
| 375 | } |
| 376 | }; |
| 377 | |
| 378 | template <class _FromDuration, class _ToDuration, class _Period> |
| 379 | struct __duration_cast<_FromDuration, _ToDuration, _Period, false, true> |
| 380 | { |
Howard Hinnant | 473f838 | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 381 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 382 | _ToDuration operator()(const _FromDuration& __fd) const |
| 383 | { |
| 384 | typedef typename common_type<typename _ToDuration::rep, typename _FromDuration::rep, intmax_t>::type _Ct; |
| 385 | return _ToDuration(static_cast<typename _ToDuration::rep>( |
| 386 | static_cast<_Ct>(__fd.count()) * static_cast<_Ct>(_Period::num))); |
| 387 | } |
| 388 | }; |
| 389 | |
| 390 | template <class _FromDuration, class _ToDuration, class _Period> |
| 391 | struct __duration_cast<_FromDuration, _ToDuration, _Period, false, false> |
| 392 | { |
Howard Hinnant | 473f838 | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 393 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 394 | _ToDuration operator()(const _FromDuration& __fd) const |
| 395 | { |
| 396 | typedef typename common_type<typename _ToDuration::rep, typename _FromDuration::rep, intmax_t>::type _Ct; |
| 397 | return _ToDuration(static_cast<typename _ToDuration::rep>( |
| 398 | static_cast<_Ct>(__fd.count()) * static_cast<_Ct>(_Period::num) |
| 399 | / static_cast<_Ct>(_Period::den))); |
| 400 | } |
| 401 | }; |
| 402 | |
| 403 | template <class _ToDuration, class _Rep, class _Period> |
| 404 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 473f838 | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 405 | _LIBCPP_CONSTEXPR |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 406 | typename enable_if |
| 407 | < |
| 408 | __is_duration<_ToDuration>::value, |
| 409 | _ToDuration |
| 410 | >::type |
| 411 | duration_cast(const duration<_Rep, _Period>& __fd) |
| 412 | { |
| 413 | return __duration_cast<duration<_Rep, _Period>, _ToDuration>()(__fd); |
| 414 | } |
| 415 | |
Howard Hinnant | 422a53f | 2010-09-21 21:28:23 +0000 | [diff] [blame] | 416 | template <class _Rep> |
Howard Hinnant | 0f678bd | 2013-08-12 18:38:34 +0000 | [diff] [blame] | 417 | struct _LIBCPP_TYPE_VIS_ONLY treat_as_floating_point : is_floating_point<_Rep> {}; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 418 | |
Marshall Clow | a3866e4 | 2015-11-30 05:39:30 +0000 | [diff] [blame] | 419 | #if _LIBCPP_STD_VER > 14 && !defined(_LIBCPP_HAS_NO_VARIABLE_TEMPLATES) |
| 420 | template <class _Rep> _LIBCPP_CONSTEXPR bool treat_as_floating_point_v |
| 421 | = treat_as_floating_point<_Rep>::value; |
| 422 | #endif |
| 423 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 424 | template <class _Rep> |
Howard Hinnant | 0f678bd | 2013-08-12 18:38:34 +0000 | [diff] [blame] | 425 | struct _LIBCPP_TYPE_VIS_ONLY duration_values |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 426 | { |
| 427 | public: |
Howard Hinnant | 473f838 | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 428 | _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR _Rep zero() {return _Rep(0);} |
| 429 | _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR _Rep max() {return numeric_limits<_Rep>::max();} |
| 430 | _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] | 431 | }; |
| 432 | |
Marshall Clow | 223df2e | 2015-11-05 19:33:59 +0000 | [diff] [blame] | 433 | #if _LIBCPP_STD_VER > 14 |
| 434 | template <class _ToDuration, class _Rep, class _Period> |
| 435 | inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR |
| 436 | typename enable_if |
| 437 | < |
| 438 | __is_duration<_ToDuration>::value, |
| 439 | _ToDuration |
| 440 | >::type |
| 441 | floor(const duration<_Rep, _Period>& __d) |
| 442 | { |
| 443 | _ToDuration __t = duration_cast<_ToDuration>(__d); |
| 444 | if (__t > __d) |
| 445 | __t = __t - _ToDuration{1}; |
| 446 | return __t; |
| 447 | } |
| 448 | |
| 449 | template <class _ToDuration, class _Rep, class _Period> |
| 450 | inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR |
| 451 | typename enable_if |
| 452 | < |
| 453 | __is_duration<_ToDuration>::value, |
| 454 | _ToDuration |
| 455 | >::type |
| 456 | ceil(const duration<_Rep, _Period>& __d) |
| 457 | { |
| 458 | _ToDuration __t = duration_cast<_ToDuration>(__d); |
| 459 | if (__t < __d) |
| 460 | __t = __t + _ToDuration{1}; |
| 461 | return __t; |
| 462 | } |
| 463 | |
| 464 | template <class _ToDuration, class _Rep, class _Period> |
| 465 | inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR |
| 466 | typename enable_if |
| 467 | < |
| 468 | __is_duration<_ToDuration>::value, |
| 469 | _ToDuration |
| 470 | >::type |
| 471 | round(const duration<_Rep, _Period>& __d) |
| 472 | { |
| 473 | _ToDuration __lower = floor<_ToDuration>(__d); |
| 474 | _ToDuration __upper = __lower + _ToDuration{1}; |
| 475 | auto __lowerDiff = __d - __lower; |
| 476 | auto __upperDiff = __upper - __d; |
| 477 | if (__lowerDiff < __upperDiff) |
| 478 | return __lower; |
| 479 | if (__lowerDiff > __upperDiff) |
| 480 | return __upper; |
| 481 | return __lower.count() & 1 ? __upper : __lower; |
| 482 | } |
| 483 | #endif |
| 484 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 485 | // duration |
| 486 | |
| 487 | template <class _Rep, class _Period> |
Howard Hinnant | 0f678bd | 2013-08-12 18:38:34 +0000 | [diff] [blame] | 488 | class _LIBCPP_TYPE_VIS_ONLY duration |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 489 | { |
| 490 | static_assert(!__is_duration<_Rep>::value, "A duration representation can not be a duration"); |
| 491 | static_assert(__is_ratio<_Period>::value, "Second template parameter of duration must be a std::ratio"); |
| 492 | static_assert(_Period::num > 0, "duration period must be positive"); |
Howard Hinnant | 713f4db | 2013-08-31 16:51:56 +0000 | [diff] [blame] | 493 | |
| 494 | template <class _R1, class _R2> |
| 495 | struct __no_overflow |
| 496 | { |
| 497 | private: |
| 498 | static const intmax_t __gcd_n1_n2 = __static_gcd<_R1::num, _R2::num>::value; |
| 499 | static const intmax_t __gcd_d1_d2 = __static_gcd<_R1::den, _R2::den>::value; |
| 500 | static const intmax_t __n1 = _R1::num / __gcd_n1_n2; |
| 501 | static const intmax_t __d1 = _R1::den / __gcd_d1_d2; |
| 502 | static const intmax_t __n2 = _R2::num / __gcd_n1_n2; |
| 503 | static const intmax_t __d2 = _R2::den / __gcd_d1_d2; |
| 504 | static const intmax_t max = -((intmax_t(1) << (sizeof(intmax_t) * CHAR_BIT - 1)) + 1); |
| 505 | |
| 506 | template <intmax_t _Xp, intmax_t _Yp, bool __overflow> |
| 507 | struct __mul // __overflow == false |
| 508 | { |
| 509 | static const intmax_t value = _Xp * _Yp; |
| 510 | }; |
| 511 | |
| 512 | template <intmax_t _Xp, intmax_t _Yp> |
| 513 | struct __mul<_Xp, _Yp, true> |
| 514 | { |
| 515 | static const intmax_t value = 1; |
| 516 | }; |
| 517 | |
| 518 | public: |
| 519 | static const bool value = (__n1 <= max / __d2) && (__n2 <= max / __d1); |
| 520 | typedef ratio<__mul<__n1, __d2, !value>::value, |
| 521 | __mul<__n2, __d1, !value>::value> type; |
| 522 | }; |
| 523 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 524 | public: |
| 525 | typedef _Rep rep; |
| 526 | typedef _Period period; |
| 527 | private: |
| 528 | rep __rep_; |
| 529 | public: |
| 530 | |
Marshall Clow | 832b304 | 2013-07-31 19:32:19 +0000 | [diff] [blame] | 531 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR |
Eric Fiselier | 8c57032 | 2016-11-18 06:42:17 +0000 | [diff] [blame] | 532 | #ifndef _LIBCPP_CXX03_LANG |
Marshall Clow | 832b304 | 2013-07-31 19:32:19 +0000 | [diff] [blame] | 533 | duration() = default; |
Marshall Clow | 0777473 | 2013-07-31 19:39:37 +0000 | [diff] [blame] | 534 | #else |
| 535 | duration() {} |
Marshall Clow | 832b304 | 2013-07-31 19:32:19 +0000 | [diff] [blame] | 536 | #endif |
| 537 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 538 | template <class _Rep2> |
Howard Hinnant | 473f838 | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 539 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 540 | explicit duration(const _Rep2& __r, |
| 541 | typename enable_if |
| 542 | < |
| 543 | is_convertible<_Rep2, rep>::value && |
| 544 | (treat_as_floating_point<rep>::value || |
| 545 | !treat_as_floating_point<_Rep2>::value) |
| 546 | >::type* = 0) |
| 547 | : __rep_(__r) {} |
| 548 | |
| 549 | // conversions |
| 550 | template <class _Rep2, class _Period2> |
Howard Hinnant | 473f838 | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 551 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 552 | duration(const duration<_Rep2, _Period2>& __d, |
| 553 | typename enable_if |
| 554 | < |
Howard Hinnant | 713f4db | 2013-08-31 16:51:56 +0000 | [diff] [blame] | 555 | __no_overflow<_Period2, period>::value && ( |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 556 | treat_as_floating_point<rep>::value || |
Howard Hinnant | 713f4db | 2013-08-31 16:51:56 +0000 | [diff] [blame] | 557 | (__no_overflow<_Period2, period>::type::den == 1 && |
| 558 | !treat_as_floating_point<_Rep2>::value)) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 559 | >::type* = 0) |
Howard Hinnant | 0949eed | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 560 | : __rep_(_VSTD::chrono::duration_cast<duration>(__d).count()) {} |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 561 | |
| 562 | // observer |
| 563 | |
Howard Hinnant | 473f838 | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 564 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR rep count() const {return __rep_;} |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 565 | |
| 566 | // arithmetic |
| 567 | |
Howard Hinnant | 473f838 | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 568 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR duration operator+() const {return *this;} |
| 569 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR duration operator-() const {return duration(-__rep_);} |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 570 | _LIBCPP_INLINE_VISIBILITY duration& operator++() {++__rep_; return *this;} |
| 571 | _LIBCPP_INLINE_VISIBILITY duration operator++(int) {return duration(__rep_++);} |
| 572 | _LIBCPP_INLINE_VISIBILITY duration& operator--() {--__rep_; return *this;} |
| 573 | _LIBCPP_INLINE_VISIBILITY duration operator--(int) {return duration(__rep_--);} |
| 574 | |
| 575 | _LIBCPP_INLINE_VISIBILITY duration& operator+=(const duration& __d) {__rep_ += __d.count(); return *this;} |
| 576 | _LIBCPP_INLINE_VISIBILITY duration& operator-=(const duration& __d) {__rep_ -= __d.count(); return *this;} |
| 577 | |
| 578 | _LIBCPP_INLINE_VISIBILITY duration& operator*=(const rep& rhs) {__rep_ *= rhs; return *this;} |
| 579 | _LIBCPP_INLINE_VISIBILITY duration& operator/=(const rep& rhs) {__rep_ /= rhs; return *this;} |
| 580 | _LIBCPP_INLINE_VISIBILITY duration& operator%=(const rep& rhs) {__rep_ %= rhs; return *this;} |
| 581 | _LIBCPP_INLINE_VISIBILITY duration& operator%=(const duration& rhs) {__rep_ %= rhs.count(); return *this;} |
| 582 | |
| 583 | // special values |
| 584 | |
Howard Hinnant | 473f838 | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 585 | _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR duration zero() {return duration(duration_values<rep>::zero());} |
| 586 | _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR duration min() {return duration(duration_values<rep>::min());} |
| 587 | _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] | 588 | }; |
| 589 | |
| 590 | typedef duration<long long, nano> nanoseconds; |
| 591 | typedef duration<long long, micro> microseconds; |
| 592 | typedef duration<long long, milli> milliseconds; |
| 593 | typedef duration<long long > seconds; |
| 594 | typedef duration< long, ratio< 60> > minutes; |
| 595 | typedef duration< long, ratio<3600> > hours; |
| 596 | |
| 597 | // Duration == |
| 598 | |
| 599 | template <class _LhsDuration, class _RhsDuration> |
| 600 | struct __duration_eq |
| 601 | { |
Howard Hinnant | 473f838 | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 602 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR |
Howard Hinnant | b9d9fb4 | 2013-06-28 18:09:35 +0000 | [diff] [blame] | 603 | bool operator()(const _LhsDuration& __lhs, const _RhsDuration& __rhs) const |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 604 | { |
| 605 | typedef typename common_type<_LhsDuration, _RhsDuration>::type _Ct; |
| 606 | return _Ct(__lhs).count() == _Ct(__rhs).count(); |
| 607 | } |
| 608 | }; |
| 609 | |
| 610 | template <class _LhsDuration> |
| 611 | struct __duration_eq<_LhsDuration, _LhsDuration> |
| 612 | { |
Howard Hinnant | 473f838 | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 613 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR |
Howard Hinnant | b9d9fb4 | 2013-06-28 18:09:35 +0000 | [diff] [blame] | 614 | bool operator()(const _LhsDuration& __lhs, const _LhsDuration& __rhs) const |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 615 | {return __lhs.count() == __rhs.count();} |
| 616 | }; |
| 617 | |
| 618 | template <class _Rep1, class _Period1, class _Rep2, class _Period2> |
| 619 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 473f838 | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 620 | _LIBCPP_CONSTEXPR |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 621 | bool |
| 622 | operator==(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs) |
| 623 | { |
| 624 | return __duration_eq<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >()(__lhs, __rhs); |
| 625 | } |
| 626 | |
| 627 | // Duration != |
| 628 | |
| 629 | template <class _Rep1, class _Period1, class _Rep2, class _Period2> |
| 630 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 473f838 | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 631 | _LIBCPP_CONSTEXPR |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 632 | bool |
| 633 | operator!=(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs) |
| 634 | { |
| 635 | return !(__lhs == __rhs); |
| 636 | } |
| 637 | |
| 638 | // Duration < |
| 639 | |
| 640 | template <class _LhsDuration, class _RhsDuration> |
| 641 | struct __duration_lt |
| 642 | { |
Howard Hinnant | 473f838 | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 643 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR |
Howard Hinnant | b9d9fb4 | 2013-06-28 18:09:35 +0000 | [diff] [blame] | 644 | bool operator()(const _LhsDuration& __lhs, const _RhsDuration& __rhs) const |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 645 | { |
| 646 | typedef typename common_type<_LhsDuration, _RhsDuration>::type _Ct; |
| 647 | return _Ct(__lhs).count() < _Ct(__rhs).count(); |
| 648 | } |
| 649 | }; |
| 650 | |
| 651 | template <class _LhsDuration> |
| 652 | struct __duration_lt<_LhsDuration, _LhsDuration> |
| 653 | { |
Howard Hinnant | 473f838 | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 654 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR |
Howard Hinnant | b9d9fb4 | 2013-06-28 18:09:35 +0000 | [diff] [blame] | 655 | bool operator()(const _LhsDuration& __lhs, const _LhsDuration& __rhs) const |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 656 | {return __lhs.count() < __rhs.count();} |
| 657 | }; |
| 658 | |
| 659 | template <class _Rep1, class _Period1, class _Rep2, class _Period2> |
| 660 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 473f838 | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 661 | _LIBCPP_CONSTEXPR |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 662 | bool |
| 663 | operator< (const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs) |
| 664 | { |
| 665 | return __duration_lt<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >()(__lhs, __rhs); |
| 666 | } |
| 667 | |
| 668 | // Duration > |
| 669 | |
| 670 | template <class _Rep1, class _Period1, class _Rep2, class _Period2> |
| 671 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 473f838 | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 672 | _LIBCPP_CONSTEXPR |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 673 | bool |
| 674 | operator> (const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs) |
| 675 | { |
| 676 | return __rhs < __lhs; |
| 677 | } |
| 678 | |
| 679 | // Duration <= |
| 680 | |
| 681 | template <class _Rep1, class _Period1, class _Rep2, class _Period2> |
| 682 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 473f838 | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 683 | _LIBCPP_CONSTEXPR |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 684 | bool |
| 685 | operator<=(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs) |
| 686 | { |
| 687 | return !(__rhs < __lhs); |
| 688 | } |
| 689 | |
| 690 | // Duration >= |
| 691 | |
| 692 | template <class _Rep1, class _Period1, class _Rep2, class _Period2> |
| 693 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 473f838 | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 694 | _LIBCPP_CONSTEXPR |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 695 | bool |
| 696 | operator>=(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs) |
| 697 | { |
| 698 | return !(__lhs < __rhs); |
| 699 | } |
| 700 | |
| 701 | // Duration + |
| 702 | |
| 703 | template <class _Rep1, class _Period1, class _Rep2, class _Period2> |
| 704 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 473f838 | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 705 | _LIBCPP_CONSTEXPR |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 706 | typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type |
| 707 | operator+(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs) |
| 708 | { |
Howard Hinnant | 473f838 | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 709 | typedef typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type _Cd; |
| 710 | return _Cd(_Cd(__lhs).count() + _Cd(__rhs).count()); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 711 | } |
| 712 | |
| 713 | // Duration - |
| 714 | |
| 715 | template <class _Rep1, class _Period1, class _Rep2, class _Period2> |
| 716 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 473f838 | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 717 | _LIBCPP_CONSTEXPR |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 718 | typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type |
| 719 | operator-(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs) |
| 720 | { |
Howard Hinnant | 473f838 | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 721 | typedef typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type _Cd; |
| 722 | return _Cd(_Cd(__lhs).count() - _Cd(__rhs).count()); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 723 | } |
| 724 | |
| 725 | // Duration * |
| 726 | |
| 727 | template <class _Rep1, class _Period, class _Rep2> |
| 728 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 473f838 | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 729 | _LIBCPP_CONSTEXPR |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 730 | typename enable_if |
| 731 | < |
| 732 | is_convertible<_Rep2, typename common_type<_Rep1, _Rep2>::type>::value, |
| 733 | duration<typename common_type<_Rep1, _Rep2>::type, _Period> |
| 734 | >::type |
| 735 | operator*(const duration<_Rep1, _Period>& __d, const _Rep2& __s) |
| 736 | { |
| 737 | typedef typename common_type<_Rep1, _Rep2>::type _Cr; |
Howard Hinnant | 473f838 | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 738 | typedef duration<_Cr, _Period> _Cd; |
| 739 | return _Cd(_Cd(__d).count() * static_cast<_Cr>(__s)); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 740 | } |
| 741 | |
| 742 | template <class _Rep1, class _Period, class _Rep2> |
| 743 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 473f838 | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 744 | _LIBCPP_CONSTEXPR |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 745 | typename enable_if |
| 746 | < |
| 747 | is_convertible<_Rep1, typename common_type<_Rep1, _Rep2>::type>::value, |
| 748 | duration<typename common_type<_Rep1, _Rep2>::type, _Period> |
| 749 | >::type |
| 750 | operator*(const _Rep1& __s, const duration<_Rep2, _Period>& __d) |
| 751 | { |
| 752 | return __d * __s; |
| 753 | } |
| 754 | |
| 755 | // Duration / |
| 756 | |
| 757 | template <class _Duration, class _Rep, bool = __is_duration<_Rep>::value> |
| 758 | struct __duration_divide_result |
| 759 | { |
| 760 | }; |
| 761 | |
| 762 | template <class _Duration, class _Rep2, |
| 763 | bool = is_convertible<_Rep2, |
| 764 | typename common_type<typename _Duration::rep, _Rep2>::type>::value> |
| 765 | struct __duration_divide_imp |
| 766 | { |
| 767 | }; |
| 768 | |
| 769 | template <class _Rep1, class _Period, class _Rep2> |
| 770 | struct __duration_divide_imp<duration<_Rep1, _Period>, _Rep2, true> |
| 771 | { |
| 772 | typedef duration<typename common_type<_Rep1, _Rep2>::type, _Period> type; |
| 773 | }; |
| 774 | |
| 775 | template <class _Rep1, class _Period, class _Rep2> |
| 776 | struct __duration_divide_result<duration<_Rep1, _Period>, _Rep2, false> |
| 777 | : __duration_divide_imp<duration<_Rep1, _Period>, _Rep2> |
| 778 | { |
| 779 | }; |
| 780 | |
| 781 | template <class _Rep1, class _Period, class _Rep2> |
| 782 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 473f838 | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 783 | _LIBCPP_CONSTEXPR |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 784 | typename __duration_divide_result<duration<_Rep1, _Period>, _Rep2>::type |
| 785 | operator/(const duration<_Rep1, _Period>& __d, const _Rep2& __s) |
| 786 | { |
| 787 | typedef typename common_type<_Rep1, _Rep2>::type _Cr; |
Howard Hinnant | 473f838 | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 788 | typedef duration<_Cr, _Period> _Cd; |
| 789 | return _Cd(_Cd(__d).count() / static_cast<_Cr>(__s)); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 790 | } |
| 791 | |
| 792 | template <class _Rep1, class _Period1, class _Rep2, class _Period2> |
| 793 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 473f838 | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 794 | _LIBCPP_CONSTEXPR |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 795 | typename common_type<_Rep1, _Rep2>::type |
| 796 | operator/(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs) |
| 797 | { |
| 798 | typedef typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type _Ct; |
| 799 | return _Ct(__lhs).count() / _Ct(__rhs).count(); |
| 800 | } |
| 801 | |
| 802 | // Duration % |
| 803 | |
| 804 | template <class _Rep1, class _Period, class _Rep2> |
| 805 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 473f838 | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 806 | _LIBCPP_CONSTEXPR |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 807 | typename __duration_divide_result<duration<_Rep1, _Period>, _Rep2>::type |
| 808 | operator%(const duration<_Rep1, _Period>& __d, const _Rep2& __s) |
| 809 | { |
| 810 | typedef typename common_type<_Rep1, _Rep2>::type _Cr; |
Howard Hinnant | 473f838 | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 811 | typedef duration<_Cr, _Period> _Cd; |
| 812 | return _Cd(_Cd(__d).count() % static_cast<_Cr>(__s)); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 813 | } |
| 814 | |
| 815 | template <class _Rep1, class _Period1, class _Rep2, class _Period2> |
| 816 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 473f838 | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 817 | _LIBCPP_CONSTEXPR |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 818 | typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type |
| 819 | operator%(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs) |
| 820 | { |
Howard Hinnant | 473f838 | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 821 | typedef typename common_type<_Rep1, _Rep2>::type _Cr; |
| 822 | typedef typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type _Cd; |
| 823 | 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] | 824 | } |
| 825 | |
| 826 | ////////////////////////////////////////////////////////// |
| 827 | ///////////////////// time_point ///////////////////////// |
| 828 | ////////////////////////////////////////////////////////// |
| 829 | |
| 830 | template <class _Clock, class _Duration = typename _Clock::duration> |
Howard Hinnant | 0f678bd | 2013-08-12 18:38:34 +0000 | [diff] [blame] | 831 | class _LIBCPP_TYPE_VIS_ONLY time_point |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 832 | { |
| 833 | static_assert(__is_duration<_Duration>::value, |
| 834 | "Second template parameter of time_point must be a std::chrono::duration"); |
| 835 | public: |
| 836 | typedef _Clock clock; |
| 837 | typedef _Duration duration; |
| 838 | typedef typename duration::rep rep; |
| 839 | typedef typename duration::period period; |
| 840 | private: |
| 841 | duration __d_; |
| 842 | |
| 843 | public: |
Marshall Clow | 832b304 | 2013-07-31 19:32:19 +0000 | [diff] [blame] | 844 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 time_point() : __d_(duration::zero()) {} |
| 845 | _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] | 846 | |
| 847 | // conversions |
| 848 | template <class _Duration2> |
Marshall Clow | 832b304 | 2013-07-31 19:32:19 +0000 | [diff] [blame] | 849 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 850 | time_point(const time_point<clock, _Duration2>& t, |
| 851 | typename enable_if |
| 852 | < |
| 853 | is_convertible<_Duration2, duration>::value |
| 854 | >::type* = 0) |
| 855 | : __d_(t.time_since_epoch()) {} |
| 856 | |
| 857 | // observer |
| 858 | |
Marshall Clow | 832b304 | 2013-07-31 19:32:19 +0000 | [diff] [blame] | 859 | _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] | 860 | |
| 861 | // arithmetic |
| 862 | |
Howard Hinnant | 171771a | 2013-07-08 21:06:38 +0000 | [diff] [blame] | 863 | _LIBCPP_INLINE_VISIBILITY time_point& operator+=(const duration& __d) {__d_ += __d; return *this;} |
| 864 | _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] | 865 | |
| 866 | // special values |
| 867 | |
Howard Hinnant | 473f838 | 2012-07-13 19:17:27 +0000 | [diff] [blame] | 868 | _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR time_point min() {return time_point(duration::min());} |
| 869 | _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] | 870 | }; |
| 871 | |
| 872 | } // chrono |
| 873 | |
| 874 | template <class _Clock, class _Duration1, class _Duration2> |
Howard Hinnant | 0f678bd | 2013-08-12 18:38:34 +0000 | [diff] [blame] | 875 | struct _LIBCPP_TYPE_VIS_ONLY common_type<chrono::time_point<_Clock, _Duration1>, |
| 876 | chrono::time_point<_Clock, _Duration2> > |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 877 | { |
| 878 | typedef chrono::time_point<_Clock, typename common_type<_Duration1, _Duration2>::type> type; |
| 879 | }; |
| 880 | |
| 881 | namespace chrono { |
| 882 | |
| 883 | template <class _ToDuration, class _Clock, class _Duration> |
Marshall Clow | 832b304 | 2013-07-31 19:32:19 +0000 | [diff] [blame] | 884 | inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 885 | time_point<_Clock, _ToDuration> |
| 886 | time_point_cast(const time_point<_Clock, _Duration>& __t) |
| 887 | { |
Howard Hinnant | 0949eed | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 888 | 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] | 889 | } |
| 890 | |
Marshall Clow | 223df2e | 2015-11-05 19:33:59 +0000 | [diff] [blame] | 891 | #if _LIBCPP_STD_VER > 14 |
| 892 | template <class _ToDuration, class _Clock, class _Duration> |
| 893 | inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR |
| 894 | typename enable_if |
| 895 | < |
| 896 | __is_duration<_ToDuration>::value, |
| 897 | time_point<_Clock, _ToDuration> |
| 898 | >::type |
| 899 | floor(const time_point<_Clock, _Duration>& __t) |
| 900 | { |
| 901 | return time_point<_Clock, _ToDuration>{floor<_ToDuration>(__t.time_since_epoch())}; |
| 902 | } |
| 903 | |
| 904 | template <class _ToDuration, class _Clock, class _Duration> |
| 905 | inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR |
| 906 | typename enable_if |
| 907 | < |
| 908 | __is_duration<_ToDuration>::value, |
| 909 | time_point<_Clock, _ToDuration> |
| 910 | >::type |
| 911 | ceil(const time_point<_Clock, _Duration>& __t) |
| 912 | { |
| 913 | return time_point<_Clock, _ToDuration>{ceil<_ToDuration>(__t.time_since_epoch())}; |
| 914 | } |
| 915 | |
| 916 | template <class _ToDuration, class _Clock, class _Duration> |
| 917 | inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR |
| 918 | typename enable_if |
| 919 | < |
| 920 | __is_duration<_ToDuration>::value, |
| 921 | time_point<_Clock, _ToDuration> |
| 922 | >::type |
| 923 | round(const time_point<_Clock, _Duration>& __t) |
| 924 | { |
| 925 | return time_point<_Clock, _ToDuration>{round<_ToDuration>(__t.time_since_epoch())}; |
| 926 | } |
| 927 | |
| 928 | template <class _Rep, class _Period> |
| 929 | inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR |
| 930 | typename enable_if |
| 931 | < |
| 932 | numeric_limits<_Rep>::is_signed, |
| 933 | duration<_Rep, _Period> |
| 934 | >::type |
| 935 | abs(duration<_Rep, _Period> __d) |
| 936 | { |
| 937 | return __d >= __d.zero() ? __d : -__d; |
| 938 | } |
| 939 | #endif |
| 940 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 941 | // time_point == |
| 942 | |
| 943 | template <class _Clock, class _Duration1, class _Duration2> |
Marshall Clow | 832b304 | 2013-07-31 19:32:19 +0000 | [diff] [blame] | 944 | inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 945 | bool |
| 946 | operator==(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs) |
| 947 | { |
| 948 | return __lhs.time_since_epoch() == __rhs.time_since_epoch(); |
| 949 | } |
| 950 | |
| 951 | // time_point != |
| 952 | |
| 953 | template <class _Clock, class _Duration1, class _Duration2> |
Marshall Clow | 832b304 | 2013-07-31 19:32:19 +0000 | [diff] [blame] | 954 | inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 955 | bool |
| 956 | operator!=(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs) |
| 957 | { |
| 958 | return !(__lhs == __rhs); |
| 959 | } |
| 960 | |
| 961 | // time_point < |
| 962 | |
| 963 | template <class _Clock, class _Duration1, class _Duration2> |
Marshall Clow | 832b304 | 2013-07-31 19:32:19 +0000 | [diff] [blame] | 964 | inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 965 | bool |
| 966 | operator<(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs) |
| 967 | { |
| 968 | return __lhs.time_since_epoch() < __rhs.time_since_epoch(); |
| 969 | } |
| 970 | |
| 971 | // time_point > |
| 972 | |
| 973 | template <class _Clock, class _Duration1, class _Duration2> |
Marshall Clow | 832b304 | 2013-07-31 19:32:19 +0000 | [diff] [blame] | 974 | inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 975 | bool |
| 976 | operator>(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs) |
| 977 | { |
| 978 | return __rhs < __lhs; |
| 979 | } |
| 980 | |
| 981 | // time_point <= |
| 982 | |
| 983 | template <class _Clock, class _Duration1, class _Duration2> |
Marshall Clow | 832b304 | 2013-07-31 19:32:19 +0000 | [diff] [blame] | 984 | inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 985 | bool |
| 986 | operator<=(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs) |
| 987 | { |
| 988 | return !(__rhs < __lhs); |
| 989 | } |
| 990 | |
| 991 | // time_point >= |
| 992 | |
| 993 | template <class _Clock, class _Duration1, class _Duration2> |
Marshall Clow | 832b304 | 2013-07-31 19:32:19 +0000 | [diff] [blame] | 994 | inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 995 | bool |
| 996 | operator>=(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs) |
| 997 | { |
| 998 | return !(__lhs < __rhs); |
| 999 | } |
| 1000 | |
| 1001 | // time_point operator+(time_point x, duration y); |
| 1002 | |
| 1003 | template <class _Clock, class _Duration1, class _Rep2, class _Period2> |
Marshall Clow | 832b304 | 2013-07-31 19:32:19 +0000 | [diff] [blame] | 1004 | inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1005 | time_point<_Clock, typename common_type<_Duration1, duration<_Rep2, _Period2> >::type> |
| 1006 | operator+(const time_point<_Clock, _Duration1>& __lhs, const duration<_Rep2, _Period2>& __rhs) |
| 1007 | { |
| 1008 | 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] | 1009 | return _Tr (__lhs.time_since_epoch() + __rhs); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1010 | } |
| 1011 | |
| 1012 | // time_point operator+(duration x, time_point y); |
| 1013 | |
| 1014 | template <class _Rep1, class _Period1, class _Clock, class _Duration2> |
Marshall Clow | 832b304 | 2013-07-31 19:32:19 +0000 | [diff] [blame] | 1015 | inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1016 | time_point<_Clock, typename common_type<duration<_Rep1, _Period1>, _Duration2>::type> |
| 1017 | operator+(const duration<_Rep1, _Period1>& __lhs, const time_point<_Clock, _Duration2>& __rhs) |
| 1018 | { |
| 1019 | return __rhs + __lhs; |
| 1020 | } |
| 1021 | |
| 1022 | // time_point operator-(time_point x, duration y); |
| 1023 | |
| 1024 | template <class _Clock, class _Duration1, class _Rep2, class _Period2> |
Marshall Clow | 832b304 | 2013-07-31 19:32:19 +0000 | [diff] [blame] | 1025 | inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1026 | time_point<_Clock, typename common_type<_Duration1, duration<_Rep2, _Period2> >::type> |
| 1027 | operator-(const time_point<_Clock, _Duration1>& __lhs, const duration<_Rep2, _Period2>& __rhs) |
| 1028 | { |
Marshall Clow | db7fa11 | 2016-11-14 18:22:19 +0000 | [diff] [blame] | 1029 | typedef time_point<_Clock, typename common_type<_Duration1, duration<_Rep2, _Period2> >::type> _Ret; |
| 1030 | return _Ret(__lhs.time_since_epoch() -__rhs); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1031 | } |
| 1032 | |
| 1033 | // duration operator-(time_point x, time_point y); |
| 1034 | |
| 1035 | template <class _Clock, class _Duration1, class _Duration2> |
Marshall Clow | 832b304 | 2013-07-31 19:32:19 +0000 | [diff] [blame] | 1036 | inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1037 | typename common_type<_Duration1, _Duration2>::type |
| 1038 | operator-(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs) |
| 1039 | { |
| 1040 | return __lhs.time_since_epoch() - __rhs.time_since_epoch(); |
| 1041 | } |
| 1042 | |
| 1043 | ////////////////////////////////////////////////////////// |
| 1044 | /////////////////////// clocks /////////////////////////// |
| 1045 | ////////////////////////////////////////////////////////// |
| 1046 | |
Howard Hinnant | 83eade6 | 2013-03-06 23:30:19 +0000 | [diff] [blame] | 1047 | class _LIBCPP_TYPE_VIS system_clock |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1048 | { |
| 1049 | public: |
| 1050 | typedef microseconds duration; |
| 1051 | typedef duration::rep rep; |
| 1052 | typedef duration::period period; |
| 1053 | typedef chrono::time_point<system_clock> time_point; |
Marshall Clow | 832b304 | 2013-07-31 19:32:19 +0000 | [diff] [blame] | 1054 | static _LIBCPP_CONSTEXPR_AFTER_CXX11 const bool is_steady = false; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1055 | |
Howard Hinnant | 756a176 | 2011-05-28 18:34:36 +0000 | [diff] [blame] | 1056 | static time_point now() _NOEXCEPT; |
| 1057 | static time_t to_time_t (const time_point& __t) _NOEXCEPT; |
| 1058 | static time_point from_time_t(time_t __t) _NOEXCEPT; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1059 | }; |
| 1060 | |
Jonathan Roelofs | c578065 | 2014-09-02 21:14:38 +0000 | [diff] [blame] | 1061 | #ifndef _LIBCPP_HAS_NO_MONOTONIC_CLOCK |
Howard Hinnant | 83eade6 | 2013-03-06 23:30:19 +0000 | [diff] [blame] | 1062 | class _LIBCPP_TYPE_VIS steady_clock |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1063 | { |
| 1064 | public: |
| 1065 | typedef nanoseconds duration; |
| 1066 | typedef duration::rep rep; |
| 1067 | typedef duration::period period; |
Howard Hinnant | f8f8521 | 2010-11-20 19:16:30 +0000 | [diff] [blame] | 1068 | typedef chrono::time_point<steady_clock, duration> time_point; |
Marshall Clow | 832b304 | 2013-07-31 19:32:19 +0000 | [diff] [blame] | 1069 | static _LIBCPP_CONSTEXPR_AFTER_CXX11 const bool is_steady = true; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1070 | |
Howard Hinnant | 756a176 | 2011-05-28 18:34:36 +0000 | [diff] [blame] | 1071 | static time_point now() _NOEXCEPT; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1072 | }; |
| 1073 | |
Howard Hinnant | f8f8521 | 2010-11-20 19:16:30 +0000 | [diff] [blame] | 1074 | typedef steady_clock high_resolution_clock; |
Jonathan Roelofs | c578065 | 2014-09-02 21:14:38 +0000 | [diff] [blame] | 1075 | #else |
| 1076 | typedef system_clock high_resolution_clock; |
| 1077 | #endif |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1078 | |
| 1079 | } // chrono |
| 1080 | |
Marshall Clow | 8d9dd7a | 2013-10-05 21:18:32 +0000 | [diff] [blame] | 1081 | #if _LIBCPP_STD_VER > 11 |
| 1082 | // Suffixes for duration literals [time.duration.literals] |
| 1083 | inline namespace literals |
Marshall Clow | f182038 | 2013-07-24 21:18:14 +0000 | [diff] [blame] | 1084 | { |
| 1085 | inline namespace chrono_literals |
| 1086 | { |
| 1087 | |
Howard Hinnant | ab61b2c | 2013-08-07 19:39:48 +0000 | [diff] [blame] | 1088 | constexpr chrono::hours operator"" h(unsigned long long __h) |
| 1089 | { |
| 1090 | return chrono::hours(static_cast<chrono::hours::rep>(__h)); |
| 1091 | } |
Marshall Clow | f182038 | 2013-07-24 21:18:14 +0000 | [diff] [blame] | 1092 | |
Howard Hinnant | ab61b2c | 2013-08-07 19:39:48 +0000 | [diff] [blame] | 1093 | constexpr chrono::duration<long double, ratio<3600,1>> operator"" h(long double __h) |
| 1094 | { |
| 1095 | return chrono::duration<long double, ratio<3600,1>>(__h); |
| 1096 | } |
Marshall Clow | f182038 | 2013-07-24 21:18:14 +0000 | [diff] [blame] | 1097 | |
| 1098 | |
Howard Hinnant | ab61b2c | 2013-08-07 19:39:48 +0000 | [diff] [blame] | 1099 | constexpr chrono::minutes operator"" min(unsigned long long __m) |
| 1100 | { |
| 1101 | return chrono::minutes(static_cast<chrono::minutes::rep>(__m)); |
| 1102 | } |
Marshall Clow | f182038 | 2013-07-24 21:18:14 +0000 | [diff] [blame] | 1103 | |
Howard Hinnant | ab61b2c | 2013-08-07 19:39:48 +0000 | [diff] [blame] | 1104 | constexpr chrono::duration<long double, ratio<60,1>> operator"" min(long double __m) |
| 1105 | { |
| 1106 | return chrono::duration<long double, ratio<60,1>> (__m); |
| 1107 | } |
Marshall Clow | f182038 | 2013-07-24 21:18:14 +0000 | [diff] [blame] | 1108 | |
| 1109 | |
Howard Hinnant | ab61b2c | 2013-08-07 19:39:48 +0000 | [diff] [blame] | 1110 | constexpr chrono::seconds operator"" s(unsigned long long __s) |
| 1111 | { |
| 1112 | return chrono::seconds(static_cast<chrono::seconds::rep>(__s)); |
| 1113 | } |
Marshall Clow | f182038 | 2013-07-24 21:18:14 +0000 | [diff] [blame] | 1114 | |
Howard Hinnant | ab61b2c | 2013-08-07 19:39:48 +0000 | [diff] [blame] | 1115 | constexpr chrono::duration<long double> operator"" s(long double __s) |
| 1116 | { |
| 1117 | return chrono::duration<long double> (__s); |
| 1118 | } |
Marshall Clow | f182038 | 2013-07-24 21:18:14 +0000 | [diff] [blame] | 1119 | |
| 1120 | |
Howard Hinnant | ab61b2c | 2013-08-07 19:39:48 +0000 | [diff] [blame] | 1121 | constexpr chrono::milliseconds operator"" ms(unsigned long long __ms) |
| 1122 | { |
| 1123 | return chrono::milliseconds(static_cast<chrono::milliseconds::rep>(__ms)); |
| 1124 | } |
Marshall Clow | f182038 | 2013-07-24 21:18:14 +0000 | [diff] [blame] | 1125 | |
Howard Hinnant | ab61b2c | 2013-08-07 19:39:48 +0000 | [diff] [blame] | 1126 | constexpr chrono::duration<long double, milli> operator"" ms(long double __ms) |
| 1127 | { |
| 1128 | return chrono::duration<long double, milli>(__ms); |
| 1129 | } |
Marshall Clow | f182038 | 2013-07-24 21:18:14 +0000 | [diff] [blame] | 1130 | |
| 1131 | |
Howard Hinnant | ab61b2c | 2013-08-07 19:39:48 +0000 | [diff] [blame] | 1132 | constexpr chrono::microseconds operator"" us(unsigned long long __us) |
| 1133 | { |
| 1134 | return chrono::microseconds(static_cast<chrono::microseconds::rep>(__us)); |
| 1135 | } |
Marshall Clow | f182038 | 2013-07-24 21:18:14 +0000 | [diff] [blame] | 1136 | |
Howard Hinnant | ab61b2c | 2013-08-07 19:39:48 +0000 | [diff] [blame] | 1137 | constexpr chrono::duration<long double, micro> operator"" us(long double __us) |
| 1138 | { |
| 1139 | return chrono::duration<long double, micro> (__us); |
| 1140 | } |
| 1141 | |
Marshall Clow | f182038 | 2013-07-24 21:18:14 +0000 | [diff] [blame] | 1142 | |
Howard Hinnant | ab61b2c | 2013-08-07 19:39:48 +0000 | [diff] [blame] | 1143 | constexpr chrono::nanoseconds operator"" ns(unsigned long long __ns) |
| 1144 | { |
| 1145 | return chrono::nanoseconds(static_cast<chrono::nanoseconds::rep>(__ns)); |
| 1146 | } |
Marshall Clow | f182038 | 2013-07-24 21:18:14 +0000 | [diff] [blame] | 1147 | |
Howard Hinnant | ab61b2c | 2013-08-07 19:39:48 +0000 | [diff] [blame] | 1148 | constexpr chrono::duration<long double, nano> operator"" ns(long double __ns) |
| 1149 | { |
| 1150 | return chrono::duration<long double, nano> (__ns); |
| 1151 | } |
Marshall Clow | f182038 | 2013-07-24 21:18:14 +0000 | [diff] [blame] | 1152 | |
| 1153 | }} |
Marshall Clow | 8d9dd7a | 2013-10-05 21:18:32 +0000 | [diff] [blame] | 1154 | |
| 1155 | namespace chrono { // hoist the literals into namespace std::chrono |
| 1156 | using namespace literals::chrono_literals; |
| 1157 | } |
| 1158 | |
Marshall Clow | f182038 | 2013-07-24 21:18:14 +0000 | [diff] [blame] | 1159 | #endif |
| 1160 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1161 | _LIBCPP_END_NAMESPACE_STD |
| 1162 | |
| 1163 | #endif // _LIBCPP_CHRONO |