| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // Time represents an absolute point in coordinated universal time (UTC), | 5 // Time represents an absolute point in coordinated universal time (UTC), |
| 6 // internally represented as microseconds (s/1,000,000) since the Windows epoch | 6 // internally represented as microseconds (s/1,000,000) since the Windows epoch |
| 7 // (1601-01-01 00:00:00 UTC). System-dependent clock interface routines are | 7 // (1601-01-01 00:00:00 UTC). System-dependent clock interface routines are |
| 8 // defined in time_PLATFORM.cc. Note that values for Time may skew and jump | 8 // defined in time_PLATFORM.cc. Note that values for Time may skew and jump |
| 9 // around as the operating system makes adjustments to synchronize (e.g., with | 9 // around as the operating system makes adjustments to synchronize (e.g., with |
| 10 // NTP servers). Thus, client code that uses the Time class must account for | 10 // NTP servers). Thus, client code that uses the Time class must account for |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 42 // Synchronizing audio and video using TimeTicks as a common | 42 // Synchronizing audio and video using TimeTicks as a common |
| 43 // reference clock (lip-sync). Measuring network round-trip | 43 // reference clock (lip-sync). Measuring network round-trip |
| 44 // latency. | 44 // latency. |
| 45 // | 45 // |
| 46 // ThreadTicks: Benchmarking how long the current thread has been doing actual | 46 // ThreadTicks: Benchmarking how long the current thread has been doing actual |
| 47 // work. | 47 // work. |
| 48 | 48 |
| 49 #ifndef BASE_TIME_TIME_H_ | 49 #ifndef BASE_TIME_TIME_H_ |
| 50 #define BASE_TIME_TIME_H_ | 50 #define BASE_TIME_TIME_H_ |
| 51 | 51 |
| 52 #include <stdint.h> |
| 52 #include <time.h> | 53 #include <time.h> |
| 53 | 54 |
| 54 #include <iosfwd> | 55 #include <iosfwd> |
| 56 #include <limits> |
| 55 | 57 |
| 56 #include "base/base_export.h" | 58 #include "base/base_export.h" |
| 57 #include "base/basictypes.h" | |
| 58 #include "base/numerics/safe_math.h" | 59 #include "base/numerics/safe_math.h" |
| 59 #include "build/build_config.h" | 60 #include "build/build_config.h" |
| 60 | 61 |
| 61 #if defined(OS_MACOSX) | 62 #if defined(OS_MACOSX) |
| 62 #include <CoreFoundation/CoreFoundation.h> | 63 #include <CoreFoundation/CoreFoundation.h> |
| 63 // Avoid Mac system header macro leak. | 64 // Avoid Mac system header macro leak. |
| 64 #undef TYPE_BOOL | 65 #undef TYPE_BOOL |
| 65 #endif | 66 #endif |
| 66 | 67 |
| 67 #if defined(OS_POSIX) | 68 #if defined(OS_POSIX) |
| 68 #include <unistd.h> | 69 #include <unistd.h> |
| 69 #include <sys/time.h> | 70 #include <sys/time.h> |
| 70 #endif | 71 #endif |
| 71 | 72 |
| 72 #if defined(OS_WIN) | 73 #if defined(OS_WIN) |
| 73 // For FILETIME in FromFileTime, until it moves to a new converter class. | 74 // For FILETIME in FromFileTime, until it moves to a new converter class. |
| 74 // See TODO(iyengar) below. | 75 // See TODO(iyengar) below. |
| 75 #include <windows.h> | 76 #include <windows.h> |
| 76 | 77 |
| 77 #include "base/gtest_prod_util.h" | 78 #include "base/gtest_prod_util.h" |
| 78 #endif | 79 #endif |
| 79 | 80 |
| 80 #include <limits> | |
| 81 | |
| 82 namespace base { | 81 namespace base { |
| 83 | 82 |
| 84 class TimeDelta; | 83 class TimeDelta; |
| 85 | 84 |
| 86 // The functions in the time_internal namespace are meant to be used only by the | 85 // The functions in the time_internal namespace are meant to be used only by the |
| 87 // time classes and functions. Please use the math operators defined in the | 86 // time classes and functions. Please use the math operators defined in the |
| 88 // time classes instead. | 87 // time classes instead. |
| 89 namespace time_internal { | 88 namespace time_internal { |
| 90 | 89 |
| 91 // Add or subtract |value| from a TimeDelta. The int64 argument and return value | 90 // Add or subtract |value| from a TimeDelta. The int64_t argument and return |
| 92 // are in terms of a microsecond timebase. | 91 // value are in terms of a microsecond timebase. |
| 93 BASE_EXPORT int64 SaturatedAdd(TimeDelta delta, int64 value); | 92 BASE_EXPORT int64_t SaturatedAdd(TimeDelta delta, int64_t value); |
| 94 BASE_EXPORT int64 SaturatedSub(TimeDelta delta, int64 value); | 93 BASE_EXPORT int64_t SaturatedSub(TimeDelta delta, int64_t value); |
| 95 | 94 |
| 96 // Clamp |value| on overflow and underflow conditions. The int64 argument and | 95 // Clamp |value| on overflow and underflow conditions. The int64_t argument and |
| 97 // return value are in terms of a microsecond timebase. | 96 // return value are in terms of a microsecond timebase. |
| 98 BASE_EXPORT int64 FromCheckedNumeric(const CheckedNumeric<int64> value); | 97 BASE_EXPORT int64_t FromCheckedNumeric(const CheckedNumeric<int64_t> value); |
| 99 | 98 |
| 100 } // namespace time_internal | 99 } // namespace time_internal |
| 101 | 100 |
| 102 // TimeDelta ------------------------------------------------------------------ | 101 // TimeDelta ------------------------------------------------------------------ |
| 103 | 102 |
| 104 class BASE_EXPORT TimeDelta { | 103 class BASE_EXPORT TimeDelta { |
| 105 public: | 104 public: |
| 106 TimeDelta() : delta_(0) { | 105 TimeDelta() : delta_(0) { |
| 107 } | 106 } |
| 108 | 107 |
| 109 // Converts units of time to TimeDeltas. | 108 // Converts units of time to TimeDeltas. |
| 110 static TimeDelta FromDays(int days); | 109 static TimeDelta FromDays(int days); |
| 111 static TimeDelta FromHours(int hours); | 110 static TimeDelta FromHours(int hours); |
| 112 static TimeDelta FromMinutes(int minutes); | 111 static TimeDelta FromMinutes(int minutes); |
| 113 static TimeDelta FromSeconds(int64 secs); | 112 static TimeDelta FromSeconds(int64_t secs); |
| 114 static TimeDelta FromMilliseconds(int64 ms); | 113 static TimeDelta FromMilliseconds(int64_t ms); |
| 115 static TimeDelta FromSecondsD(double secs); | 114 static TimeDelta FromSecondsD(double secs); |
| 116 static TimeDelta FromMillisecondsD(double ms); | 115 static TimeDelta FromMillisecondsD(double ms); |
| 117 static TimeDelta FromMicroseconds(int64 us); | 116 static TimeDelta FromMicroseconds(int64_t us); |
| 118 #if defined(OS_WIN) | 117 #if defined(OS_WIN) |
| 119 static TimeDelta FromQPCValue(LONGLONG qpc_value); | 118 static TimeDelta FromQPCValue(LONGLONG qpc_value); |
| 120 #endif | 119 #endif |
| 121 | 120 |
| 122 // Converts an integer value representing TimeDelta to a class. This is used | 121 // Converts an integer value representing TimeDelta to a class. This is used |
| 123 // when deserializing a |TimeDelta| structure, using a value known to be | 122 // when deserializing a |TimeDelta| structure, using a value known to be |
| 124 // compatible. It is not provided as a constructor because the integer type | 123 // compatible. It is not provided as a constructor because the integer type |
| 125 // may be unclear from the perspective of a caller. | 124 // may be unclear from the perspective of a caller. |
| 126 static TimeDelta FromInternalValue(int64 delta) { | 125 static TimeDelta FromInternalValue(int64_t delta) { return TimeDelta(delta); } |
| 127 return TimeDelta(delta); | |
| 128 } | |
| 129 | 126 |
| 130 // Returns the maximum time delta, which should be greater than any reasonable | 127 // Returns the maximum time delta, which should be greater than any reasonable |
| 131 // time delta we might compare it to. Adding or subtracting the maximum time | 128 // time delta we might compare it to. Adding or subtracting the maximum time |
| 132 // delta to a time or another time delta has an undefined result. | 129 // delta to a time or another time delta has an undefined result. |
| 133 static TimeDelta Max(); | 130 static TimeDelta Max(); |
| 134 | 131 |
| 135 // Returns the internal numeric value of the TimeDelta object. Please don't | 132 // Returns the internal numeric value of the TimeDelta object. Please don't |
| 136 // use this and do arithmetic on it, as it is more error prone than using the | 133 // use this and do arithmetic on it, as it is more error prone than using the |
| 137 // provided operators. | 134 // provided operators. |
| 138 // For serializing, use FromInternalValue to reconstitute. | 135 // For serializing, use FromInternalValue to reconstitute. |
| 139 int64 ToInternalValue() const { | 136 int64_t ToInternalValue() const { return delta_; } |
| 140 return delta_; | |
| 141 } | |
| 142 | 137 |
| 143 // Returns the magnitude (absolute value) of this TimeDelta. | 138 // Returns the magnitude (absolute value) of this TimeDelta. |
| 144 TimeDelta magnitude() const { | 139 TimeDelta magnitude() const { |
| 145 // Some toolchains provide an incomplete C++11 implementation and lack an | 140 // Some toolchains provide an incomplete C++11 implementation and lack an |
| 146 // int64 overload for std::abs(). The following is a simple branchless | 141 // int64_t overload for std::abs(). The following is a simple branchless |
| 147 // implementation: | 142 // implementation: |
| 148 const int64 mask = delta_ >> (sizeof(delta_) * 8 - 1); | 143 const int64_t mask = delta_ >> (sizeof(delta_) * 8 - 1); |
| 149 return TimeDelta((delta_ + mask) ^ mask); | 144 return TimeDelta((delta_ + mask) ^ mask); |
| 150 } | 145 } |
| 151 | 146 |
| 152 // Returns true if the time delta is zero. | 147 // Returns true if the time delta is zero. |
| 153 bool is_zero() const { | 148 bool is_zero() const { |
| 154 return delta_ == 0; | 149 return delta_ == 0; |
| 155 } | 150 } |
| 156 | 151 |
| 157 // Returns true if the time delta is the maximum time delta. | 152 // Returns true if the time delta is the maximum time delta. |
| 158 bool is_max() const { | 153 bool is_max() const { return delta_ == std::numeric_limits<int64_t>::max(); } |
| 159 return delta_ == std::numeric_limits<int64>::max(); | |
| 160 } | |
| 161 | 154 |
| 162 #if defined(OS_POSIX) | 155 #if defined(OS_POSIX) |
| 163 struct timespec ToTimeSpec() const; | 156 struct timespec ToTimeSpec() const; |
| 164 #endif | 157 #endif |
| 165 | 158 |
| 166 // Returns the time delta in some unit. The F versions return a floating | 159 // Returns the time delta in some unit. The F versions return a floating |
| 167 // point value, the "regular" versions return a rounded-down value. | 160 // point value, the "regular" versions return a rounded-down value. |
| 168 // | 161 // |
| 169 // InMillisecondsRoundedUp() instead returns an integer that is rounded up | 162 // InMillisecondsRoundedUp() instead returns an integer that is rounded up |
| 170 // to the next full millisecond. | 163 // to the next full millisecond. |
| 171 int InDays() const; | 164 int InDays() const; |
| 172 int InHours() const; | 165 int InHours() const; |
| 173 int InMinutes() const; | 166 int InMinutes() const; |
| 174 double InSecondsF() const; | 167 double InSecondsF() const; |
| 175 int64 InSeconds() const; | 168 int64_t InSeconds() const; |
| 176 double InMillisecondsF() const; | 169 double InMillisecondsF() const; |
| 177 int64 InMilliseconds() const; | 170 int64_t InMilliseconds() const; |
| 178 int64 InMillisecondsRoundedUp() const; | 171 int64_t InMillisecondsRoundedUp() const; |
| 179 int64 InMicroseconds() const; | 172 int64_t InMicroseconds() const; |
| 180 | 173 |
| 181 TimeDelta& operator=(TimeDelta other) { | 174 TimeDelta& operator=(TimeDelta other) { |
| 182 delta_ = other.delta_; | 175 delta_ = other.delta_; |
| 183 return *this; | 176 return *this; |
| 184 } | 177 } |
| 185 | 178 |
| 186 // Computations with other deltas. | 179 // Computations with other deltas. |
| 187 TimeDelta operator+(TimeDelta other) const { | 180 TimeDelta operator+(TimeDelta other) const { |
| 188 return TimeDelta(time_internal::SaturatedAdd(*this, other.delta_)); | 181 return TimeDelta(time_internal::SaturatedAdd(*this, other.delta_)); |
| 189 } | 182 } |
| 190 TimeDelta operator-(TimeDelta other) const { | 183 TimeDelta operator-(TimeDelta other) const { |
| 191 return TimeDelta(time_internal::SaturatedSub(*this, other.delta_)); | 184 return TimeDelta(time_internal::SaturatedSub(*this, other.delta_)); |
| 192 } | 185 } |
| 193 | 186 |
| 194 TimeDelta& operator+=(TimeDelta other) { | 187 TimeDelta& operator+=(TimeDelta other) { |
| 195 return *this = (*this + other); | 188 return *this = (*this + other); |
| 196 } | 189 } |
| 197 TimeDelta& operator-=(TimeDelta other) { | 190 TimeDelta& operator-=(TimeDelta other) { |
| 198 return *this = (*this - other); | 191 return *this = (*this - other); |
| 199 } | 192 } |
| 200 TimeDelta operator-() const { | 193 TimeDelta operator-() const { |
| 201 return TimeDelta(-delta_); | 194 return TimeDelta(-delta_); |
| 202 } | 195 } |
| 203 | 196 |
| 204 // Computations with numeric types. | 197 // Computations with numeric types. |
| 205 template<typename T> | 198 template<typename T> |
| 206 TimeDelta operator*(T a) const { | 199 TimeDelta operator*(T a) const { |
| 207 CheckedNumeric<int64> rv(delta_); | 200 CheckedNumeric<int64_t> rv(delta_); |
| 208 rv *= a; | 201 rv *= a; |
| 209 return TimeDelta(time_internal::FromCheckedNumeric(rv)); | 202 return TimeDelta(time_internal::FromCheckedNumeric(rv)); |
| 210 } | 203 } |
| 211 template<typename T> | 204 template<typename T> |
| 212 TimeDelta operator/(T a) const { | 205 TimeDelta operator/(T a) const { |
| 213 CheckedNumeric<int64> rv(delta_); | 206 CheckedNumeric<int64_t> rv(delta_); |
| 214 rv /= a; | 207 rv /= a; |
| 215 return TimeDelta(time_internal::FromCheckedNumeric(rv)); | 208 return TimeDelta(time_internal::FromCheckedNumeric(rv)); |
| 216 } | 209 } |
| 217 template<typename T> | 210 template<typename T> |
| 218 TimeDelta& operator*=(T a) { | 211 TimeDelta& operator*=(T a) { |
| 219 return *this = (*this * a); | 212 return *this = (*this * a); |
| 220 } | 213 } |
| 221 template<typename T> | 214 template<typename T> |
| 222 TimeDelta& operator/=(T a) { | 215 TimeDelta& operator/=(T a) { |
| 223 return *this = (*this / a); | 216 return *this = (*this / a); |
| 224 } | 217 } |
| 225 | 218 |
| 226 int64 operator/(TimeDelta a) const { | 219 int64_t operator/(TimeDelta a) const { return delta_ / a.delta_; } |
| 227 return delta_ / a.delta_; | |
| 228 } | |
| 229 TimeDelta operator%(TimeDelta a) const { | 220 TimeDelta operator%(TimeDelta a) const { |
| 230 return TimeDelta(delta_ % a.delta_); | 221 return TimeDelta(delta_ % a.delta_); |
| 231 } | 222 } |
| 232 | 223 |
| 233 // Comparison operators. | 224 // Comparison operators. |
| 234 bool operator==(TimeDelta other) const { | 225 bool operator==(TimeDelta other) const { |
| 235 return delta_ == other.delta_; | 226 return delta_ == other.delta_; |
| 236 } | 227 } |
| 237 bool operator!=(TimeDelta other) const { | 228 bool operator!=(TimeDelta other) const { |
| 238 return delta_ != other.delta_; | 229 return delta_ != other.delta_; |
| 239 } | 230 } |
| 240 bool operator<(TimeDelta other) const { | 231 bool operator<(TimeDelta other) const { |
| 241 return delta_ < other.delta_; | 232 return delta_ < other.delta_; |
| 242 } | 233 } |
| 243 bool operator<=(TimeDelta other) const { | 234 bool operator<=(TimeDelta other) const { |
| 244 return delta_ <= other.delta_; | 235 return delta_ <= other.delta_; |
| 245 } | 236 } |
| 246 bool operator>(TimeDelta other) const { | 237 bool operator>(TimeDelta other) const { |
| 247 return delta_ > other.delta_; | 238 return delta_ > other.delta_; |
| 248 } | 239 } |
| 249 bool operator>=(TimeDelta other) const { | 240 bool operator>=(TimeDelta other) const { |
| 250 return delta_ >= other.delta_; | 241 return delta_ >= other.delta_; |
| 251 } | 242 } |
| 252 | 243 |
| 253 private: | 244 private: |
| 254 friend int64 time_internal::SaturatedAdd(TimeDelta delta, int64 value); | 245 friend int64_t time_internal::SaturatedAdd(TimeDelta delta, int64_t value); |
| 255 friend int64 time_internal::SaturatedSub(TimeDelta delta, int64 value); | 246 friend int64_t time_internal::SaturatedSub(TimeDelta delta, int64_t value); |
| 256 | 247 |
| 257 // Constructs a delta given the duration in microseconds. This is private | 248 // Constructs a delta given the duration in microseconds. This is private |
| 258 // to avoid confusion by callers with an integer constructor. Use | 249 // to avoid confusion by callers with an integer constructor. Use |
| 259 // FromSeconds, FromMilliseconds, etc. instead. | 250 // FromSeconds, FromMilliseconds, etc. instead. |
| 260 explicit TimeDelta(int64 delta_us) : delta_(delta_us) { | 251 explicit TimeDelta(int64_t delta_us) : delta_(delta_us) {} |
| 261 } | |
| 262 | 252 |
| 263 // Private method to build a delta from a double. | 253 // Private method to build a delta from a double. |
| 264 static TimeDelta FromDouble(double value); | 254 static TimeDelta FromDouble(double value); |
| 265 | 255 |
| 266 // Delta in microseconds. | 256 // Delta in microseconds. |
| 267 int64 delta_; | 257 int64_t delta_; |
| 268 }; | 258 }; |
| 269 | 259 |
| 270 template<typename T> | 260 template<typename T> |
| 271 inline TimeDelta operator*(T a, TimeDelta td) { | 261 inline TimeDelta operator*(T a, TimeDelta td) { |
| 272 return td * a; | 262 return td * a; |
| 273 } | 263 } |
| 274 | 264 |
| 275 // For logging use only. | 265 // For logging use only. |
| 276 BASE_EXPORT std::ostream& operator<<(std::ostream& os, TimeDelta time_delta); | 266 BASE_EXPORT std::ostream& operator<<(std::ostream& os, TimeDelta time_delta); |
| 277 | 267 |
| 278 // Do not reference the time_internal::TimeBase template class directly. Please | 268 // Do not reference the time_internal::TimeBase template class directly. Please |
| 279 // use one of the time subclasses instead, and only reference the public | 269 // use one of the time subclasses instead, and only reference the public |
| 280 // TimeBase members via those classes. | 270 // TimeBase members via those classes. |
| 281 namespace time_internal { | 271 namespace time_internal { |
| 282 | 272 |
| 283 // TimeBase-------------------------------------------------------------------- | 273 // TimeBase-------------------------------------------------------------------- |
| 284 | 274 |
| 285 // Provides value storage and comparison/math operations common to all time | 275 // Provides value storage and comparison/math operations common to all time |
| 286 // classes. Each subclass provides for strong type-checking to ensure | 276 // classes. Each subclass provides for strong type-checking to ensure |
| 287 // semantically meaningful comparison/math of time values from the same clock | 277 // semantically meaningful comparison/math of time values from the same clock |
| 288 // source or timeline. | 278 // source or timeline. |
| 289 template<class TimeClass> | 279 template<class TimeClass> |
| 290 class TimeBase { | 280 class TimeBase { |
| 291 public: | 281 public: |
| 292 static const int64 kHoursPerDay = 24; | 282 static const int64_t kHoursPerDay = 24; |
| 293 static const int64 kMillisecondsPerSecond = 1000; | 283 static const int64_t kMillisecondsPerSecond = 1000; |
| 294 static const int64 kMillisecondsPerDay = kMillisecondsPerSecond * 60 * 60 * | 284 static const int64_t kMillisecondsPerDay = |
| 295 kHoursPerDay; | 285 kMillisecondsPerSecond * 60 * 60 * kHoursPerDay; |
| 296 static const int64 kMicrosecondsPerMillisecond = 1000; | 286 static const int64_t kMicrosecondsPerMillisecond = 1000; |
| 297 static const int64 kMicrosecondsPerSecond = kMicrosecondsPerMillisecond * | 287 static const int64_t kMicrosecondsPerSecond = |
| 298 kMillisecondsPerSecond; | 288 kMicrosecondsPerMillisecond * kMillisecondsPerSecond; |
| 299 static const int64 kMicrosecondsPerMinute = kMicrosecondsPerSecond * 60; | 289 static const int64_t kMicrosecondsPerMinute = kMicrosecondsPerSecond * 60; |
| 300 static const int64 kMicrosecondsPerHour = kMicrosecondsPerMinute * 60; | 290 static const int64_t kMicrosecondsPerHour = kMicrosecondsPerMinute * 60; |
| 301 static const int64 kMicrosecondsPerDay = kMicrosecondsPerHour * kHoursPerDay; | 291 static const int64_t kMicrosecondsPerDay = |
| 302 static const int64 kMicrosecondsPerWeek = kMicrosecondsPerDay * 7; | 292 kMicrosecondsPerHour * kHoursPerDay; |
| 303 static const int64 kNanosecondsPerMicrosecond = 1000; | 293 static const int64_t kMicrosecondsPerWeek = kMicrosecondsPerDay * 7; |
| 304 static const int64 kNanosecondsPerSecond = kNanosecondsPerMicrosecond * | 294 static const int64_t kNanosecondsPerMicrosecond = 1000; |
| 305 kMicrosecondsPerSecond; | 295 static const int64_t kNanosecondsPerSecond = |
| 296 kNanosecondsPerMicrosecond * kMicrosecondsPerSecond; |
| 306 | 297 |
| 307 // Returns true if this object has not been initialized. | 298 // Returns true if this object has not been initialized. |
| 308 // | 299 // |
| 309 // Warning: Be careful when writing code that performs math on time values, | 300 // Warning: Be careful when writing code that performs math on time values, |
| 310 // since it's possible to produce a valid "zero" result that should not be | 301 // since it's possible to produce a valid "zero" result that should not be |
| 311 // interpreted as a "null" value. | 302 // interpreted as a "null" value. |
| 312 bool is_null() const { | 303 bool is_null() const { |
| 313 return us_ == 0; | 304 return us_ == 0; |
| 314 } | 305 } |
| 315 | 306 |
| 316 // Returns true if this object represents the maximum time. | 307 // Returns true if this object represents the maximum time. |
| 317 bool is_max() const { | 308 bool is_max() const { return us_ == std::numeric_limits<int64_t>::max(); } |
| 318 return us_ == std::numeric_limits<int64>::max(); | |
| 319 } | |
| 320 | 309 |
| 321 // For serializing only. Use FromInternalValue() to reconstitute. Please don't | 310 // For serializing only. Use FromInternalValue() to reconstitute. Please don't |
| 322 // use this and do arithmetic on it, as it is more error prone than using the | 311 // use this and do arithmetic on it, as it is more error prone than using the |
| 323 // provided operators. | 312 // provided operators. |
| 324 int64 ToInternalValue() const { | 313 int64_t ToInternalValue() const { return us_; } |
| 325 return us_; | |
| 326 } | |
| 327 | 314 |
| 328 TimeClass& operator=(TimeClass other) { | 315 TimeClass& operator=(TimeClass other) { |
| 329 us_ = other.us_; | 316 us_ = other.us_; |
| 330 return *(static_cast<TimeClass*>(this)); | 317 return *(static_cast<TimeClass*>(this)); |
| 331 } | 318 } |
| 332 | 319 |
| 333 // Compute the difference between two times. | 320 // Compute the difference between two times. |
| 334 TimeDelta operator-(TimeClass other) const { | 321 TimeDelta operator-(TimeClass other) const { |
| 335 return TimeDelta::FromMicroseconds(us_ - other.us_); | 322 return TimeDelta::FromMicroseconds(us_ - other.us_); |
| 336 } | 323 } |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 368 return us_ > other.us_; | 355 return us_ > other.us_; |
| 369 } | 356 } |
| 370 bool operator>=(TimeClass other) const { | 357 bool operator>=(TimeClass other) const { |
| 371 return us_ >= other.us_; | 358 return us_ >= other.us_; |
| 372 } | 359 } |
| 373 | 360 |
| 374 // Converts an integer value representing TimeClass to a class. This is used | 361 // Converts an integer value representing TimeClass to a class. This is used |
| 375 // when deserializing a |TimeClass| structure, using a value known to be | 362 // when deserializing a |TimeClass| structure, using a value known to be |
| 376 // compatible. It is not provided as a constructor because the integer type | 363 // compatible. It is not provided as a constructor because the integer type |
| 377 // may be unclear from the perspective of a caller. | 364 // may be unclear from the perspective of a caller. |
| 378 static TimeClass FromInternalValue(int64 us) { | 365 static TimeClass FromInternalValue(int64_t us) { return TimeClass(us); } |
| 379 return TimeClass(us); | |
| 380 } | |
| 381 | 366 |
| 382 protected: | 367 protected: |
| 383 explicit TimeBase(int64 us) : us_(us) { | 368 explicit TimeBase(int64_t us) : us_(us) {} |
| 384 } | |
| 385 | 369 |
| 386 // Time value in a microsecond timebase. | 370 // Time value in a microsecond timebase. |
| 387 int64 us_; | 371 int64_t us_; |
| 388 }; | 372 }; |
| 389 | 373 |
| 390 } // namespace time_internal | 374 } // namespace time_internal |
| 391 | 375 |
| 392 template<class TimeClass> | 376 template<class TimeClass> |
| 393 inline TimeClass operator+(TimeDelta delta, TimeClass t) { | 377 inline TimeClass operator+(TimeDelta delta, TimeClass t) { |
| 394 return t + delta; | 378 return t + delta; |
| 395 } | 379 } |
| 396 | 380 |
| 397 // Time ----------------------------------------------------------------------- | 381 // Time ----------------------------------------------------------------------- |
| 398 | 382 |
| 399 // Represents a wall clock time in UTC. Values are not guaranteed to be | 383 // Represents a wall clock time in UTC. Values are not guaranteed to be |
| 400 // monotonically non-decreasing and are subject to large amounts of skew. | 384 // monotonically non-decreasing and are subject to large amounts of skew. |
| 401 class BASE_EXPORT Time : public time_internal::TimeBase<Time> { | 385 class BASE_EXPORT Time : public time_internal::TimeBase<Time> { |
| 402 public: | 386 public: |
| 403 // The representation of Jan 1, 1970 UTC in microseconds since the | 387 // The representation of Jan 1, 1970 UTC in microseconds since the |
| 404 // platform-dependent epoch. | 388 // platform-dependent epoch. |
| 405 static const int64 kTimeTToMicrosecondsOffset; | 389 static const int64_t kTimeTToMicrosecondsOffset; |
| 406 | 390 |
| 407 #if !defined(OS_WIN) | 391 #if !defined(OS_WIN) |
| 408 // On Mac & Linux, this value is the delta from the Windows epoch of 1601 to | 392 // On Mac & Linux, this value is the delta from the Windows epoch of 1601 to |
| 409 // the Posix delta of 1970. This is used for migrating between the old | 393 // the Posix delta of 1970. This is used for migrating between the old |
| 410 // 1970-based epochs to the new 1601-based ones. It should be removed from | 394 // 1970-based epochs to the new 1601-based ones. It should be removed from |
| 411 // this global header and put in the platform-specific ones when we remove the | 395 // this global header and put in the platform-specific ones when we remove the |
| 412 // migration code. | 396 // migration code. |
| 413 static const int64 kWindowsEpochDeltaMicroseconds; | 397 static const int64_t kWindowsEpochDeltaMicroseconds; |
| 414 #else | 398 #else |
| 415 // To avoid overflow in QPC to Microseconds calculations, since we multiply | 399 // To avoid overflow in QPC to Microseconds calculations, since we multiply |
| 416 // by kMicrosecondsPerSecond, then the QPC value should not exceed | 400 // by kMicrosecondsPerSecond, then the QPC value should not exceed |
| 417 // (2^63 - 1) / 1E6. If it exceeds that threshold, we divide then multiply. | 401 // (2^63 - 1) / 1E6. If it exceeds that threshold, we divide then multiply. |
| 418 enum : int64 { kQPCOverflowThreshold = 0x8637BD05AF7 }; | 402 enum : int64_t{kQPCOverflowThreshold = 0x8637BD05AF7}; |
| 419 #endif | 403 #endif |
| 420 | 404 |
| 421 // Represents an exploded time that can be formatted nicely. This is kind of | 405 // Represents an exploded time that can be formatted nicely. This is kind of |
| 422 // like the Win32 SYSTEMTIME structure or the Unix "struct tm" with a few | 406 // like the Win32 SYSTEMTIME structure or the Unix "struct tm" with a few |
| 423 // additions and changes to prevent errors. | 407 // additions and changes to prevent errors. |
| 424 struct BASE_EXPORT Exploded { | 408 struct BASE_EXPORT Exploded { |
| 425 int year; // Four digit year "2007" | 409 int year; // Four digit year "2007" |
| 426 int month; // 1-based month (values 1 = January, etc.) | 410 int month; // 1-based month (values 1 = January, etc.) |
| 427 int day_of_week; // 0-based day of week (0 = Sunday, etc.) | 411 int day_of_week; // 0-based day of week (0 = Sunday, etc.) |
| 428 int day_of_month; // 1-based day of month (1-31) | 412 int day_of_month; // 1-based day of month (1-31) |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 483 #endif | 467 #endif |
| 484 | 468 |
| 485 // Converts to/from the Javascript convention for times, a number of | 469 // Converts to/from the Javascript convention for times, a number of |
| 486 // milliseconds since the epoch: | 470 // milliseconds since the epoch: |
| 487 // https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date/g
etTime. | 471 // https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date/g
etTime. |
| 488 static Time FromJsTime(double ms_since_epoch); | 472 static Time FromJsTime(double ms_since_epoch); |
| 489 double ToJsTime() const; | 473 double ToJsTime() const; |
| 490 | 474 |
| 491 // Converts to Java convention for times, a number of | 475 // Converts to Java convention for times, a number of |
| 492 // milliseconds since the epoch. | 476 // milliseconds since the epoch. |
| 493 int64 ToJavaTime() const; | 477 int64_t ToJavaTime() const; |
| 494 | 478 |
| 495 #if defined(OS_POSIX) | 479 #if defined(OS_POSIX) |
| 496 static Time FromTimeVal(struct timeval t); | 480 static Time FromTimeVal(struct timeval t); |
| 497 struct timeval ToTimeVal() const; | 481 struct timeval ToTimeVal() const; |
| 498 #endif | 482 #endif |
| 499 | 483 |
| 500 #if defined(OS_MACOSX) | 484 #if defined(OS_MACOSX) |
| 501 static Time FromCFAbsoluteTime(CFAbsoluteTime t); | 485 static Time FromCFAbsoluteTime(CFAbsoluteTime t); |
| 502 CFAbsoluteTime ToCFAbsoluteTime() const; | 486 CFAbsoluteTime ToCFAbsoluteTime() const; |
| 503 #endif | 487 #endif |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 562 return Explode(true, exploded); | 546 return Explode(true, exploded); |
| 563 } | 547 } |
| 564 | 548 |
| 565 // Rounds this time down to the nearest day in local time. It will represent | 549 // Rounds this time down to the nearest day in local time. It will represent |
| 566 // midnight on that day. | 550 // midnight on that day. |
| 567 Time LocalMidnight() const; | 551 Time LocalMidnight() const; |
| 568 | 552 |
| 569 private: | 553 private: |
| 570 friend class time_internal::TimeBase<Time>; | 554 friend class time_internal::TimeBase<Time>; |
| 571 | 555 |
| 572 explicit Time(int64 us) : TimeBase(us) { | 556 explicit Time(int64_t us) : TimeBase(us) {} |
| 573 } | |
| 574 | 557 |
| 575 // Explodes the given time to either local time |is_local = true| or UTC | 558 // Explodes the given time to either local time |is_local = true| or UTC |
| 576 // |is_local = false|. | 559 // |is_local = false|. |
| 577 void Explode(bool is_local, Exploded* exploded) const; | 560 void Explode(bool is_local, Exploded* exploded) const; |
| 578 | 561 |
| 579 // Unexplodes a given time assuming the source is either local time | 562 // Unexplodes a given time assuming the source is either local time |
| 580 // |is_local = true| or UTC |is_local = false|. | 563 // |is_local = true| or UTC |is_local = false|. |
| 581 static Time FromExploded(bool is_local, const Exploded& exploded); | 564 static Time FromExploded(bool is_local, const Exploded& exploded); |
| 582 | 565 |
| 583 // Converts a string representation of time to a Time object. | 566 // Converts a string representation of time to a Time object. |
| (...skipping 25 matching lines...) Expand all Loading... |
| 609 } | 592 } |
| 610 | 593 |
| 611 // static | 594 // static |
| 612 inline TimeDelta TimeDelta::FromMinutes(int minutes) { | 595 inline TimeDelta TimeDelta::FromMinutes(int minutes) { |
| 613 if (minutes == std::numeric_limits<int>::max()) | 596 if (minutes == std::numeric_limits<int>::max()) |
| 614 return Max(); | 597 return Max(); |
| 615 return TimeDelta(minutes * Time::kMicrosecondsPerMinute); | 598 return TimeDelta(minutes * Time::kMicrosecondsPerMinute); |
| 616 } | 599 } |
| 617 | 600 |
| 618 // static | 601 // static |
| 619 inline TimeDelta TimeDelta::FromSeconds(int64 secs) { | 602 inline TimeDelta TimeDelta::FromSeconds(int64_t secs) { |
| 620 return TimeDelta(secs) * Time::kMicrosecondsPerSecond; | 603 return TimeDelta(secs) * Time::kMicrosecondsPerSecond; |
| 621 } | 604 } |
| 622 | 605 |
| 623 // static | 606 // static |
| 624 inline TimeDelta TimeDelta::FromMilliseconds(int64 ms) { | 607 inline TimeDelta TimeDelta::FromMilliseconds(int64_t ms) { |
| 625 return TimeDelta(ms) * Time::kMicrosecondsPerMillisecond; | 608 return TimeDelta(ms) * Time::kMicrosecondsPerMillisecond; |
| 626 } | 609 } |
| 627 | 610 |
| 628 // static | 611 // static |
| 629 inline TimeDelta TimeDelta::FromSecondsD(double secs) { | 612 inline TimeDelta TimeDelta::FromSecondsD(double secs) { |
| 630 return FromDouble(secs * Time::kMicrosecondsPerSecond); | 613 return FromDouble(secs * Time::kMicrosecondsPerSecond); |
| 631 } | 614 } |
| 632 | 615 |
| 633 // static | 616 // static |
| 634 inline TimeDelta TimeDelta::FromMillisecondsD(double ms) { | 617 inline TimeDelta TimeDelta::FromMillisecondsD(double ms) { |
| 635 return FromDouble(ms * Time::kMicrosecondsPerMillisecond); | 618 return FromDouble(ms * Time::kMicrosecondsPerMillisecond); |
| 636 } | 619 } |
| 637 | 620 |
| 638 // static | 621 // static |
| 639 inline TimeDelta TimeDelta::FromMicroseconds(int64 us) { | 622 inline TimeDelta TimeDelta::FromMicroseconds(int64_t us) { |
| 640 return TimeDelta(us); | 623 return TimeDelta(us); |
| 641 } | 624 } |
| 642 | 625 |
| 643 // static | 626 // static |
| 644 inline TimeDelta TimeDelta::FromDouble(double value) { | 627 inline TimeDelta TimeDelta::FromDouble(double value) { |
| 645 double max_magnitude = std::numeric_limits<int64>::max(); | 628 double max_magnitude = std::numeric_limits<int64_t>::max(); |
| 646 TimeDelta delta = TimeDelta(static_cast<int64>(value)); | 629 TimeDelta delta = TimeDelta(static_cast<int64_t>(value)); |
| 647 if (value > max_magnitude) | 630 if (value > max_magnitude) |
| 648 delta = Max(); | 631 delta = Max(); |
| 649 else if (value < -max_magnitude) | 632 else if (value < -max_magnitude) |
| 650 delta = -Max(); | 633 delta = -Max(); |
| 651 return delta; | 634 return delta; |
| 652 } | 635 } |
| 653 | 636 |
| 654 // For logging use only. | 637 // For logging use only. |
| 655 BASE_EXPORT std::ostream& operator<<(std::ostream& os, Time time); | 638 BASE_EXPORT std::ostream& operator<<(std::ostream& os, Time time); |
| 656 | 639 |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 701 protected: | 684 protected: |
| 702 typedef DWORD (*TickFunctionType)(void); | 685 typedef DWORD (*TickFunctionType)(void); |
| 703 static TickFunctionType SetMockTickFunction(TickFunctionType ticker); | 686 static TickFunctionType SetMockTickFunction(TickFunctionType ticker); |
| 704 #endif | 687 #endif |
| 705 | 688 |
| 706 private: | 689 private: |
| 707 friend class time_internal::TimeBase<TimeTicks>; | 690 friend class time_internal::TimeBase<TimeTicks>; |
| 708 | 691 |
| 709 // Please use Now() to create a new object. This is for internal use | 692 // Please use Now() to create a new object. This is for internal use |
| 710 // and testing. | 693 // and testing. |
| 711 explicit TimeTicks(int64 us) : TimeBase(us) { | 694 explicit TimeTicks(int64_t us) : TimeBase(us) {} |
| 712 } | |
| 713 }; | 695 }; |
| 714 | 696 |
| 715 // For logging use only. | 697 // For logging use only. |
| 716 BASE_EXPORT std::ostream& operator<<(std::ostream& os, TimeTicks time_ticks); | 698 BASE_EXPORT std::ostream& operator<<(std::ostream& os, TimeTicks time_ticks); |
| 717 | 699 |
| 718 // ThreadTicks ---------------------------------------------------------------- | 700 // ThreadTicks ---------------------------------------------------------------- |
| 719 | 701 |
| 720 // Represents a clock, specific to a particular thread, than runs only while the | 702 // Represents a clock, specific to a particular thread, than runs only while the |
| 721 // thread is running. | 703 // thread is running. |
| 722 class BASE_EXPORT ThreadTicks : public time_internal::TimeBase<ThreadTicks> { | 704 class BASE_EXPORT ThreadTicks : public time_internal::TimeBase<ThreadTicks> { |
| (...skipping 28 matching lines...) Expand all Loading... |
| 751 // migrates to another CPU between two calls. Returns an empty ThreadTicks | 733 // migrates to another CPU between two calls. Returns an empty ThreadTicks |
| 752 // object until the initialization is completed. If a clock reading is | 734 // object until the initialization is completed. If a clock reading is |
| 753 // absolutely needed, call WaitUntilInitialized() before this method. | 735 // absolutely needed, call WaitUntilInitialized() before this method. |
| 754 static ThreadTicks Now(); | 736 static ThreadTicks Now(); |
| 755 | 737 |
| 756 private: | 738 private: |
| 757 friend class time_internal::TimeBase<ThreadTicks>; | 739 friend class time_internal::TimeBase<ThreadTicks>; |
| 758 | 740 |
| 759 // Please use Now() to create a new object. This is for internal use | 741 // Please use Now() to create a new object. This is for internal use |
| 760 // and testing. | 742 // and testing. |
| 761 explicit ThreadTicks(int64 us) : TimeBase(us) { | 743 explicit ThreadTicks(int64_t us) : TimeBase(us) {} |
| 762 } | |
| 763 | 744 |
| 764 #if defined(OS_WIN) | 745 #if defined(OS_WIN) |
| 765 FRIEND_TEST_ALL_PREFIXES(TimeTicks, TSCTicksPerSecond); | 746 FRIEND_TEST_ALL_PREFIXES(TimeTicks, TSCTicksPerSecond); |
| 766 | 747 |
| 767 // Returns the frequency of the TSC in ticks per second, or 0 if it hasn't | 748 // Returns the frequency of the TSC in ticks per second, or 0 if it hasn't |
| 768 // been measured yet. Needs to be guarded with a call to IsSupported(). | 749 // been measured yet. Needs to be guarded with a call to IsSupported(). |
| 769 // This method is declared here rather than in the anonymous namespace to | 750 // This method is declared here rather than in the anonymous namespace to |
| 770 // allow testing. | 751 // allow testing. |
| 771 static double TSCTicksPerSecond(); | 752 static double TSCTicksPerSecond(); |
| 772 | 753 |
| 773 static bool IsSupportedWin(); | 754 static bool IsSupportedWin(); |
| 774 static void WaitUntilInitializedWin(); | 755 static void WaitUntilInitializedWin(); |
| 775 #endif | 756 #endif |
| 776 }; | 757 }; |
| 777 | 758 |
| 778 // For logging use only. | 759 // For logging use only. |
| 779 BASE_EXPORT std::ostream& operator<<(std::ostream& os, ThreadTicks time_ticks); | 760 BASE_EXPORT std::ostream& operator<<(std::ostream& os, ThreadTicks time_ticks); |
| 780 | 761 |
| 781 } // namespace base | 762 } // namespace base |
| 782 | 763 |
| 783 #endif // BASE_TIME_TIME_H_ | 764 #endif // BASE_TIME_TIME_H_ |
| OLD | NEW |