avi@chromium.org | 1c519e9 | 2013-06-28 03:04:56 +0900 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
tnagel@chromium.org | 4c4f411 | 2014-05-14 02:39:11 +0900 | [diff] [blame] | 5 | // Time represents an absolute point in coordinated universal time (UTC), |
| 6 | // internally represented as microseconds (s/1,000,000) since the Windows epoch |
miu | 85c3556 | 2015-05-30 08:57:12 +0900 | [diff] [blame] | 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 |
| 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 |
| 11 | // this. |
avi@chromium.org | 1c519e9 | 2013-06-28 03:04:56 +0900 | [diff] [blame] | 12 | // |
| 13 | // TimeDelta represents a duration of time, internally represented in |
| 14 | // microseconds. |
| 15 | // |
charliea | 1cd1210 | 2015-11-05 22:48:56 +0900 | [diff] [blame] | 16 | // TimeTicks and ThreadTicks represent an abstract time that is most of the time |
| 17 | // incrementing, for use in measuring time durations. Internally, they are |
| 18 | // represented in microseconds. They can not be converted to a human-readable |
| 19 | // time, but are guaranteed not to decrease (unlike the Time class). Note that |
| 20 | // TimeTicks may "stand still" (e.g., if the computer is suspended), and |
| 21 | // ThreadTicks will "stand still" whenever the thread has been de-scheduled by |
| 22 | // the operating system. |
avi@chromium.org | 1c519e9 | 2013-06-28 03:04:56 +0900 | [diff] [blame] | 23 | // |
miu | 85c3556 | 2015-05-30 08:57:12 +0900 | [diff] [blame] | 24 | // All time classes are copyable, assignable, and occupy 64-bits per |
| 25 | // instance. Thus, they can be efficiently passed by-value (as opposed to |
| 26 | // by-reference). |
ricea | 749c642 | 2014-10-29 20:35:45 +0900 | [diff] [blame] | 27 | // |
| 28 | // Definitions of operator<< are provided to make these types work with |
| 29 | // DCHECK_EQ() and other log macros. For human-readable formatting, see |
| 30 | // "base/i18n/time_formatting.h". |
miu | 85c3556 | 2015-05-30 08:57:12 +0900 | [diff] [blame] | 31 | // |
| 32 | // So many choices! Which time class should you use? Examples: |
| 33 | // |
| 34 | // Time: Interpreting the wall-clock time provided by a remote |
| 35 | // system. Detecting whether cached resources have |
| 36 | // expired. Providing the user with a display of the current date |
| 37 | // and time. Determining the amount of time between events across |
| 38 | // re-boots of the machine. |
| 39 | // |
| 40 | // TimeTicks: Tracking the amount of time a task runs. Executing delayed |
| 41 | // tasks at the right time. Computing presentation timestamps. |
| 42 | // Synchronizing audio and video using TimeTicks as a common |
| 43 | // reference clock (lip-sync). Measuring network round-trip |
| 44 | // latency. |
| 45 | // |
| 46 | // ThreadTicks: Benchmarking how long the current thread has been doing actual |
| 47 | // work. |
avi@chromium.org | 1c519e9 | 2013-06-28 03:04:56 +0900 | [diff] [blame] | 48 | |
| 49 | #ifndef BASE_TIME_TIME_H_ |
| 50 | #define BASE_TIME_TIME_H_ |
| 51 | |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 52 | #include <stdint.h> |
avi@chromium.org | 1c519e9 | 2013-06-28 03:04:56 +0900 | [diff] [blame] | 53 | #include <time.h> |
| 54 | |
ricea | 749c642 | 2014-10-29 20:35:45 +0900 | [diff] [blame] | 55 | #include <iosfwd> |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 56 | #include <limits> |
ricea | 749c642 | 2014-10-29 20:35:45 +0900 | [diff] [blame] | 57 | |
avi@chromium.org | 1c519e9 | 2013-06-28 03:04:56 +0900 | [diff] [blame] | 58 | #include "base/base_export.h" |
rvargas | 47e8f4c | 2015-03-21 16:41:21 +0900 | [diff] [blame] | 59 | #include "base/numerics/safe_math.h" |
mostynb@opera.com | f29dd61 | 2013-09-04 08:29:12 +0900 | [diff] [blame] | 60 | #include "build/build_config.h" |
avi@chromium.org | 1c519e9 | 2013-06-28 03:04:56 +0900 | [diff] [blame] | 61 | |
| 62 | #if defined(OS_MACOSX) |
| 63 | #include <CoreFoundation/CoreFoundation.h> |
| 64 | // Avoid Mac system header macro leak. |
| 65 | #undef TYPE_BOOL |
| 66 | #endif |
| 67 | |
| 68 | #if defined(OS_POSIX) |
ernstm@chromium.org | 9bad9a1 | 2013-07-31 15:36:04 +0900 | [diff] [blame] | 69 | #include <unistd.h> |
avi@chromium.org | 1c519e9 | 2013-06-28 03:04:56 +0900 | [diff] [blame] | 70 | #include <sys/time.h> |
| 71 | #endif |
| 72 | |
| 73 | #if defined(OS_WIN) |
| 74 | // For FILETIME in FromFileTime, until it moves to a new converter class. |
| 75 | // See TODO(iyengar) below. |
| 76 | #include <windows.h> |
fdoray | eabadbd | 2015-10-15 12:46:40 +0900 | [diff] [blame] | 77 | |
| 78 | #include "base/gtest_prod_util.h" |
avi@chromium.org | 1c519e9 | 2013-06-28 03:04:56 +0900 | [diff] [blame] | 79 | #endif |
| 80 | |
avi@chromium.org | 1c519e9 | 2013-06-28 03:04:56 +0900 | [diff] [blame] | 81 | namespace base { |
| 82 | |
miu | 352541f | 2015-05-10 09:15:21 +0900 | [diff] [blame] | 83 | class TimeDelta; |
| 84 | |
| 85 | // The functions in the time_internal namespace are meant to be used only by the |
| 86 | // time classes and functions. Please use the math operators defined in the |
| 87 | // time classes instead. |
| 88 | namespace time_internal { |
| 89 | |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 90 | // Add or subtract |value| from a TimeDelta. The int64_t argument and return |
| 91 | // value are in terms of a microsecond timebase. |
| 92 | BASE_EXPORT int64_t SaturatedAdd(TimeDelta delta, int64_t value); |
| 93 | BASE_EXPORT int64_t SaturatedSub(TimeDelta delta, int64_t value); |
miu | 352541f | 2015-05-10 09:15:21 +0900 | [diff] [blame] | 94 | |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 95 | // Clamp |value| on overflow and underflow conditions. The int64_t argument and |
miu | 352541f | 2015-05-10 09:15:21 +0900 | [diff] [blame] | 96 | // return value are in terms of a microsecond timebase. |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 97 | BASE_EXPORT int64_t FromCheckedNumeric(const CheckedNumeric<int64_t> value); |
miu | 352541f | 2015-05-10 09:15:21 +0900 | [diff] [blame] | 98 | |
| 99 | } // namespace time_internal |
avi@chromium.org | 1c519e9 | 2013-06-28 03:04:56 +0900 | [diff] [blame] | 100 | |
| 101 | // TimeDelta ------------------------------------------------------------------ |
| 102 | |
| 103 | class BASE_EXPORT TimeDelta { |
| 104 | public: |
| 105 | TimeDelta() : delta_(0) { |
| 106 | } |
| 107 | |
| 108 | // Converts units of time to TimeDeltas. |
gavinp@chromium.org | 096c163 | 2014-03-06 05:02:05 +0900 | [diff] [blame] | 109 | static TimeDelta FromDays(int days); |
| 110 | static TimeDelta FromHours(int hours); |
| 111 | static TimeDelta FromMinutes(int minutes); |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 112 | static TimeDelta FromSeconds(int64_t secs); |
| 113 | static TimeDelta FromMilliseconds(int64_t ms); |
behara.ms@samsung.com | 8c037ed | 2014-04-22 18:00:53 +0900 | [diff] [blame] | 114 | static TimeDelta FromSecondsD(double secs); |
| 115 | static TimeDelta FromMillisecondsD(double ms); |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 116 | static TimeDelta FromMicroseconds(int64_t us); |
avi@chromium.org | 1c519e9 | 2013-06-28 03:04:56 +0900 | [diff] [blame] | 117 | #if defined(OS_WIN) |
| 118 | static TimeDelta FromQPCValue(LONGLONG qpc_value); |
| 119 | #endif |
| 120 | |
| 121 | // Converts an integer value representing TimeDelta to a class. This is used |
| 122 | // when deserializing a |TimeDelta| structure, using a value known to be |
| 123 | // compatible. It is not provided as a constructor because the integer type |
| 124 | // may be unclear from the perspective of a caller. |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 125 | static TimeDelta FromInternalValue(int64_t delta) { return TimeDelta(delta); } |
avi@chromium.org | 1c519e9 | 2013-06-28 03:04:56 +0900 | [diff] [blame] | 126 | |
gavinp@chromium.org | 096c163 | 2014-03-06 05:02:05 +0900 | [diff] [blame] | 127 | // Returns the maximum time delta, which should be greater than any reasonable |
| 128 | // time delta we might compare it to. Adding or subtracting the maximum time |
| 129 | // delta to a time or another time delta has an undefined result. |
| 130 | static TimeDelta Max(); |
| 131 | |
avi@chromium.org | 1c519e9 | 2013-06-28 03:04:56 +0900 | [diff] [blame] | 132 | // Returns the internal numeric value of the TimeDelta object. Please don't |
| 133 | // use this and do arithmetic on it, as it is more error prone than using the |
| 134 | // provided operators. |
| 135 | // For serializing, use FromInternalValue to reconstitute. |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 136 | int64_t ToInternalValue() const { return delta_; } |
avi@chromium.org | 1c519e9 | 2013-06-28 03:04:56 +0900 | [diff] [blame] | 137 | |
miu | f8ef55b | 2015-01-14 14:33:08 +0900 | [diff] [blame] | 138 | // Returns the magnitude (absolute value) of this TimeDelta. |
| 139 | TimeDelta magnitude() const { |
| 140 | // Some toolchains provide an incomplete C++11 implementation and lack an |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 141 | // int64_t overload for std::abs(). The following is a simple branchless |
miu | f8ef55b | 2015-01-14 14:33:08 +0900 | [diff] [blame] | 142 | // implementation: |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 143 | const int64_t mask = delta_ >> (sizeof(delta_) * 8 - 1); |
miu | f8ef55b | 2015-01-14 14:33:08 +0900 | [diff] [blame] | 144 | return TimeDelta((delta_ + mask) ^ mask); |
| 145 | } |
| 146 | |
miu | 352541f | 2015-05-10 09:15:21 +0900 | [diff] [blame] | 147 | // Returns true if the time delta is zero. |
| 148 | bool is_zero() const { |
| 149 | return delta_ == 0; |
| 150 | } |
| 151 | |
gavinp@chromium.org | 096c163 | 2014-03-06 05:02:05 +0900 | [diff] [blame] | 152 | // Returns true if the time delta is the maximum time delta. |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 153 | bool is_max() const { return delta_ == std::numeric_limits<int64_t>::max(); } |
gavinp@chromium.org | 096c163 | 2014-03-06 05:02:05 +0900 | [diff] [blame] | 154 | |
avi@chromium.org | 1c519e9 | 2013-06-28 03:04:56 +0900 | [diff] [blame] | 155 | #if defined(OS_POSIX) |
| 156 | struct timespec ToTimeSpec() const; |
| 157 | #endif |
| 158 | |
| 159 | // Returns the time delta in some unit. The F versions return a floating |
| 160 | // point value, the "regular" versions return a rounded-down value. |
| 161 | // |
| 162 | // InMillisecondsRoundedUp() instead returns an integer that is rounded up |
| 163 | // to the next full millisecond. |
| 164 | int InDays() const; |
| 165 | int InHours() const; |
| 166 | int InMinutes() const; |
| 167 | double InSecondsF() const; |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 168 | int64_t InSeconds() const; |
avi@chromium.org | 1c519e9 | 2013-06-28 03:04:56 +0900 | [diff] [blame] | 169 | double InMillisecondsF() const; |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 170 | int64_t InMilliseconds() const; |
| 171 | int64_t InMillisecondsRoundedUp() const; |
| 172 | int64_t InMicroseconds() const; |
avi@chromium.org | 1c519e9 | 2013-06-28 03:04:56 +0900 | [diff] [blame] | 173 | |
| 174 | TimeDelta& operator=(TimeDelta other) { |
| 175 | delta_ = other.delta_; |
| 176 | return *this; |
| 177 | } |
| 178 | |
| 179 | // Computations with other deltas. |
| 180 | TimeDelta operator+(TimeDelta other) const { |
miu | 352541f | 2015-05-10 09:15:21 +0900 | [diff] [blame] | 181 | return TimeDelta(time_internal::SaturatedAdd(*this, other.delta_)); |
avi@chromium.org | 1c519e9 | 2013-06-28 03:04:56 +0900 | [diff] [blame] | 182 | } |
| 183 | TimeDelta operator-(TimeDelta other) const { |
miu | 352541f | 2015-05-10 09:15:21 +0900 | [diff] [blame] | 184 | return TimeDelta(time_internal::SaturatedSub(*this, other.delta_)); |
avi@chromium.org | 1c519e9 | 2013-06-28 03:04:56 +0900 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | TimeDelta& operator+=(TimeDelta other) { |
miu | 352541f | 2015-05-10 09:15:21 +0900 | [diff] [blame] | 188 | return *this = (*this + other); |
avi@chromium.org | 1c519e9 | 2013-06-28 03:04:56 +0900 | [diff] [blame] | 189 | } |
| 190 | TimeDelta& operator-=(TimeDelta other) { |
miu | 352541f | 2015-05-10 09:15:21 +0900 | [diff] [blame] | 191 | return *this = (*this - other); |
avi@chromium.org | 1c519e9 | 2013-06-28 03:04:56 +0900 | [diff] [blame] | 192 | } |
| 193 | TimeDelta operator-() const { |
| 194 | return TimeDelta(-delta_); |
| 195 | } |
| 196 | |
michaeln | 74a8019 | 2015-02-24 06:22:42 +0900 | [diff] [blame] | 197 | // Computations with numeric types. |
| 198 | template<typename T> |
| 199 | TimeDelta operator*(T a) const { |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 200 | CheckedNumeric<int64_t> rv(delta_); |
rvargas | 47e8f4c | 2015-03-21 16:41:21 +0900 | [diff] [blame] | 201 | rv *= a; |
miu | 352541f | 2015-05-10 09:15:21 +0900 | [diff] [blame] | 202 | return TimeDelta(time_internal::FromCheckedNumeric(rv)); |
avi@chromium.org | 1c519e9 | 2013-06-28 03:04:56 +0900 | [diff] [blame] | 203 | } |
michaeln | 74a8019 | 2015-02-24 06:22:42 +0900 | [diff] [blame] | 204 | template<typename T> |
| 205 | TimeDelta operator/(T a) const { |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 206 | CheckedNumeric<int64_t> rv(delta_); |
rvargas | 47e8f4c | 2015-03-21 16:41:21 +0900 | [diff] [blame] | 207 | rv /= a; |
miu | 352541f | 2015-05-10 09:15:21 +0900 | [diff] [blame] | 208 | return TimeDelta(time_internal::FromCheckedNumeric(rv)); |
avi@chromium.org | 1c519e9 | 2013-06-28 03:04:56 +0900 | [diff] [blame] | 209 | } |
michaeln | 74a8019 | 2015-02-24 06:22:42 +0900 | [diff] [blame] | 210 | template<typename T> |
| 211 | TimeDelta& operator*=(T a) { |
miu | 352541f | 2015-05-10 09:15:21 +0900 | [diff] [blame] | 212 | return *this = (*this * a); |
avi@chromium.org | 1c519e9 | 2013-06-28 03:04:56 +0900 | [diff] [blame] | 213 | } |
michaeln | 74a8019 | 2015-02-24 06:22:42 +0900 | [diff] [blame] | 214 | template<typename T> |
| 215 | TimeDelta& operator/=(T a) { |
miu | 352541f | 2015-05-10 09:15:21 +0900 | [diff] [blame] | 216 | return *this = (*this / a); |
avi@chromium.org | 1c519e9 | 2013-06-28 03:04:56 +0900 | [diff] [blame] | 217 | } |
michaeln | 74a8019 | 2015-02-24 06:22:42 +0900 | [diff] [blame] | 218 | |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 219 | int64_t operator/(TimeDelta a) const { return delta_ / a.delta_; } |
miu | 352541f | 2015-05-10 09:15:21 +0900 | [diff] [blame] | 220 | TimeDelta operator%(TimeDelta a) const { |
| 221 | return TimeDelta(delta_ % a.delta_); |
| 222 | } |
avi@chromium.org | 1c519e9 | 2013-06-28 03:04:56 +0900 | [diff] [blame] | 223 | |
| 224 | // Comparison operators. |
| 225 | bool operator==(TimeDelta other) const { |
| 226 | return delta_ == other.delta_; |
| 227 | } |
| 228 | bool operator!=(TimeDelta other) const { |
| 229 | return delta_ != other.delta_; |
| 230 | } |
| 231 | bool operator<(TimeDelta other) const { |
| 232 | return delta_ < other.delta_; |
| 233 | } |
| 234 | bool operator<=(TimeDelta other) const { |
| 235 | return delta_ <= other.delta_; |
| 236 | } |
| 237 | bool operator>(TimeDelta other) const { |
| 238 | return delta_ > other.delta_; |
| 239 | } |
| 240 | bool operator>=(TimeDelta other) const { |
| 241 | return delta_ >= other.delta_; |
| 242 | } |
| 243 | |
| 244 | private: |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 245 | friend int64_t time_internal::SaturatedAdd(TimeDelta delta, int64_t value); |
| 246 | friend int64_t time_internal::SaturatedSub(TimeDelta delta, int64_t value); |
avi@chromium.org | 1c519e9 | 2013-06-28 03:04:56 +0900 | [diff] [blame] | 247 | |
| 248 | // Constructs a delta given the duration in microseconds. This is private |
| 249 | // to avoid confusion by callers with an integer constructor. Use |
| 250 | // FromSeconds, FromMilliseconds, etc. instead. |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 251 | explicit TimeDelta(int64_t delta_us) : delta_(delta_us) {} |
avi@chromium.org | 1c519e9 | 2013-06-28 03:04:56 +0900 | [diff] [blame] | 252 | |
rvargas | 7505c4f | 2015-07-28 05:09:02 +0900 | [diff] [blame] | 253 | // Private method to build a delta from a double. |
| 254 | static TimeDelta FromDouble(double value); |
| 255 | |
avi@chromium.org | 1c519e9 | 2013-06-28 03:04:56 +0900 | [diff] [blame] | 256 | // Delta in microseconds. |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 257 | int64_t delta_; |
avi@chromium.org | 1c519e9 | 2013-06-28 03:04:56 +0900 | [diff] [blame] | 258 | }; |
| 259 | |
michaeln | 74a8019 | 2015-02-24 06:22:42 +0900 | [diff] [blame] | 260 | template<typename T> |
| 261 | inline TimeDelta operator*(T a, TimeDelta td) { |
| 262 | return td * a; |
avi@chromium.org | 1c519e9 | 2013-06-28 03:04:56 +0900 | [diff] [blame] | 263 | } |
| 264 | |
ricea | 749c642 | 2014-10-29 20:35:45 +0900 | [diff] [blame] | 265 | // For logging use only. |
| 266 | BASE_EXPORT std::ostream& operator<<(std::ostream& os, TimeDelta time_delta); |
| 267 | |
miu | 352541f | 2015-05-10 09:15:21 +0900 | [diff] [blame] | 268 | // Do not reference the time_internal::TimeBase template class directly. Please |
| 269 | // use one of the time subclasses instead, and only reference the public |
| 270 | // TimeBase members via those classes. |
| 271 | namespace time_internal { |
avi@chromium.org | 1c519e9 | 2013-06-28 03:04:56 +0900 | [diff] [blame] | 272 | |
miu | 352541f | 2015-05-10 09:15:21 +0900 | [diff] [blame] | 273 | // TimeBase-------------------------------------------------------------------- |
| 274 | |
| 275 | // Provides value storage and comparison/math operations common to all time |
| 276 | // classes. Each subclass provides for strong type-checking to ensure |
| 277 | // semantically meaningful comparison/math of time values from the same clock |
| 278 | // source or timeline. |
| 279 | template<class TimeClass> |
| 280 | class TimeBase { |
avi@chromium.org | 1c519e9 | 2013-06-28 03:04:56 +0900 | [diff] [blame] | 281 | public: |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 282 | static const int64_t kHoursPerDay = 24; |
| 283 | static const int64_t kMillisecondsPerSecond = 1000; |
| 284 | static const int64_t kMillisecondsPerDay = |
| 285 | kMillisecondsPerSecond * 60 * 60 * kHoursPerDay; |
| 286 | static const int64_t kMicrosecondsPerMillisecond = 1000; |
| 287 | static const int64_t kMicrosecondsPerSecond = |
| 288 | kMicrosecondsPerMillisecond * kMillisecondsPerSecond; |
| 289 | static const int64_t kMicrosecondsPerMinute = kMicrosecondsPerSecond * 60; |
| 290 | static const int64_t kMicrosecondsPerHour = kMicrosecondsPerMinute * 60; |
| 291 | static const int64_t kMicrosecondsPerDay = |
| 292 | kMicrosecondsPerHour * kHoursPerDay; |
| 293 | static const int64_t kMicrosecondsPerWeek = kMicrosecondsPerDay * 7; |
| 294 | static const int64_t kNanosecondsPerMicrosecond = 1000; |
| 295 | static const int64_t kNanosecondsPerSecond = |
| 296 | kNanosecondsPerMicrosecond * kMicrosecondsPerSecond; |
avi@chromium.org | 1c519e9 | 2013-06-28 03:04:56 +0900 | [diff] [blame] | 297 | |
miu | 352541f | 2015-05-10 09:15:21 +0900 | [diff] [blame] | 298 | // Returns true if this object has not been initialized. |
| 299 | // |
| 300 | // Warning: Be careful when writing code that performs math on time values, |
| 301 | // since it's possible to produce a valid "zero" result that should not be |
| 302 | // interpreted as a "null" value. |
| 303 | bool is_null() const { |
| 304 | return us_ == 0; |
| 305 | } |
| 306 | |
| 307 | // Returns true if this object represents the maximum time. |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 308 | bool is_max() const { return us_ == std::numeric_limits<int64_t>::max(); } |
miu | 352541f | 2015-05-10 09:15:21 +0900 | [diff] [blame] | 309 | |
| 310 | // For serializing only. Use FromInternalValue() to reconstitute. Please don't |
| 311 | // use this and do arithmetic on it, as it is more error prone than using the |
| 312 | // provided operators. |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 313 | int64_t ToInternalValue() const { return us_; } |
miu | 352541f | 2015-05-10 09:15:21 +0900 | [diff] [blame] | 314 | |
| 315 | TimeClass& operator=(TimeClass other) { |
| 316 | us_ = other.us_; |
| 317 | return *(static_cast<TimeClass*>(this)); |
| 318 | } |
| 319 | |
| 320 | // Compute the difference between two times. |
| 321 | TimeDelta operator-(TimeClass other) const { |
| 322 | return TimeDelta::FromMicroseconds(us_ - other.us_); |
| 323 | } |
| 324 | |
| 325 | // Return a new time modified by some delta. |
| 326 | TimeClass operator+(TimeDelta delta) const { |
| 327 | return TimeClass(time_internal::SaturatedAdd(delta, us_)); |
| 328 | } |
| 329 | TimeClass operator-(TimeDelta delta) const { |
| 330 | return TimeClass(-time_internal::SaturatedSub(delta, us_)); |
| 331 | } |
| 332 | |
| 333 | // Modify by some time delta. |
| 334 | TimeClass& operator+=(TimeDelta delta) { |
| 335 | return static_cast<TimeClass&>(*this = (*this + delta)); |
| 336 | } |
| 337 | TimeClass& operator-=(TimeDelta delta) { |
| 338 | return static_cast<TimeClass&>(*this = (*this - delta)); |
| 339 | } |
| 340 | |
| 341 | // Comparison operators |
| 342 | bool operator==(TimeClass other) const { |
| 343 | return us_ == other.us_; |
| 344 | } |
| 345 | bool operator!=(TimeClass other) const { |
| 346 | return us_ != other.us_; |
| 347 | } |
| 348 | bool operator<(TimeClass other) const { |
| 349 | return us_ < other.us_; |
| 350 | } |
| 351 | bool operator<=(TimeClass other) const { |
| 352 | return us_ <= other.us_; |
| 353 | } |
| 354 | bool operator>(TimeClass other) const { |
| 355 | return us_ > other.us_; |
| 356 | } |
| 357 | bool operator>=(TimeClass other) const { |
| 358 | return us_ >= other.us_; |
| 359 | } |
| 360 | |
| 361 | // Converts an integer value representing TimeClass to a class. This is used |
| 362 | // when deserializing a |TimeClass| structure, using a value known to be |
| 363 | // compatible. It is not provided as a constructor because the integer type |
| 364 | // may be unclear from the perspective of a caller. |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 365 | static TimeClass FromInternalValue(int64_t us) { return TimeClass(us); } |
miu | 352541f | 2015-05-10 09:15:21 +0900 | [diff] [blame] | 366 | |
| 367 | protected: |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 368 | explicit TimeBase(int64_t us) : us_(us) {} |
miu | 352541f | 2015-05-10 09:15:21 +0900 | [diff] [blame] | 369 | |
| 370 | // Time value in a microsecond timebase. |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 371 | int64_t us_; |
miu | 352541f | 2015-05-10 09:15:21 +0900 | [diff] [blame] | 372 | }; |
| 373 | |
| 374 | } // namespace time_internal |
| 375 | |
| 376 | template<class TimeClass> |
| 377 | inline TimeClass operator+(TimeDelta delta, TimeClass t) { |
| 378 | return t + delta; |
| 379 | } |
| 380 | |
| 381 | // Time ----------------------------------------------------------------------- |
| 382 | |
| 383 | // Represents a wall clock time in UTC. Values are not guaranteed to be |
| 384 | // monotonically non-decreasing and are subject to large amounts of skew. |
| 385 | class BASE_EXPORT Time : public time_internal::TimeBase<Time> { |
| 386 | public: |
erikchen | 38dbe3d | 2014-11-12 11:57:56 +0900 | [diff] [blame] | 387 | // The representation of Jan 1, 1970 UTC in microseconds since the |
| 388 | // platform-dependent epoch. |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 389 | static const int64_t kTimeTToMicrosecondsOffset; |
erikchen | 38dbe3d | 2014-11-12 11:57:56 +0900 | [diff] [blame] | 390 | |
avi@chromium.org | 1c519e9 | 2013-06-28 03:04:56 +0900 | [diff] [blame] | 391 | #if !defined(OS_WIN) |
| 392 | // On Mac & Linux, this value is the delta from the Windows epoch of 1601 to |
| 393 | // the Posix delta of 1970. This is used for migrating between the old |
| 394 | // 1970-based epochs to the new 1601-based ones. It should be removed from |
| 395 | // this global header and put in the platform-specific ones when we remove the |
| 396 | // migration code. |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 397 | static const int64_t kWindowsEpochDeltaMicroseconds; |
fmeawad@chromium.org | 074db92 | 2014-08-06 15:21:18 +0900 | [diff] [blame] | 398 | #else |
| 399 | // To avoid overflow in QPC to Microseconds calculations, since we multiply |
| 400 | // by kMicrosecondsPerSecond, then the QPC value should not exceed |
| 401 | // (2^63 - 1) / 1E6. If it exceeds that threshold, we divide then multiply. |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 402 | enum : int64_t{kQPCOverflowThreshold = 0x8637BD05AF7}; |
avi@chromium.org | 1c519e9 | 2013-06-28 03:04:56 +0900 | [diff] [blame] | 403 | #endif |
| 404 | |
| 405 | // Represents an exploded time that can be formatted nicely. This is kind of |
| 406 | // like the Win32 SYSTEMTIME structure or the Unix "struct tm" with a few |
| 407 | // additions and changes to prevent errors. |
| 408 | struct BASE_EXPORT Exploded { |
| 409 | int year; // Four digit year "2007" |
| 410 | int month; // 1-based month (values 1 = January, etc.) |
| 411 | int day_of_week; // 0-based day of week (0 = Sunday, etc.) |
| 412 | int day_of_month; // 1-based day of month (1-31) |
| 413 | int hour; // Hour within the current day (0-23) |
| 414 | int minute; // Minute within the current hour (0-59) |
| 415 | int second; // Second within the current minute (0-59 plus leap |
| 416 | // seconds which may take it up to 60). |
| 417 | int millisecond; // Milliseconds within the current second (0-999) |
| 418 | |
| 419 | // A cursory test for whether the data members are within their |
| 420 | // respective ranges. A 'true' return value does not guarantee the |
| 421 | // Exploded value can be successfully converted to a Time value. |
| 422 | bool HasValidValues() const; |
| 423 | }; |
| 424 | |
| 425 | // Contains the NULL time. Use Time::Now() to get the current time. |
miu | 352541f | 2015-05-10 09:15:21 +0900 | [diff] [blame] | 426 | Time() : TimeBase(0) { |
avi@chromium.org | 1c519e9 | 2013-06-28 03:04:56 +0900 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | // Returns the time for epoch in Unix-like system (Jan 1, 1970). |
| 430 | static Time UnixEpoch(); |
| 431 | |
| 432 | // Returns the current time. Watch out, the system might adjust its clock |
| 433 | // in which case time will actually go backwards. We don't guarantee that |
| 434 | // times are increasing, or that two calls to Now() won't be the same. |
| 435 | static Time Now(); |
| 436 | |
| 437 | // Returns the maximum time, which should be greater than any reasonable time |
| 438 | // with which we might compare it. |
| 439 | static Time Max(); |
| 440 | |
| 441 | // Returns the current time. Same as Now() except that this function always |
| 442 | // uses system time so that there are no discrepancies between the returned |
| 443 | // time and system time even on virtual environments including our test bot. |
| 444 | // For timing sensitive unittests, this function should be used. |
| 445 | static Time NowFromSystemTime(); |
| 446 | |
| 447 | // Converts to/from time_t in UTC and a Time class. |
| 448 | // TODO(brettw) this should be removed once everybody starts using the |Time| |
| 449 | // class. |
| 450 | static Time FromTimeT(time_t tt); |
| 451 | time_t ToTimeT() const; |
| 452 | |
| 453 | // Converts time to/from a double which is the number of seconds since epoch |
| 454 | // (Jan 1, 1970). Webkit uses this format to represent time. |
| 455 | // Because WebKit initializes double time value to 0 to indicate "not |
| 456 | // initialized", we map it to empty Time object that also means "not |
| 457 | // initialized". |
| 458 | static Time FromDoubleT(double dt); |
| 459 | double ToDoubleT() const; |
| 460 | |
| 461 | #if defined(OS_POSIX) |
| 462 | // Converts the timespec structure to time. MacOS X 10.8.3 (and tentatively, |
| 463 | // earlier versions) will have the |ts|'s tv_nsec component zeroed out, |
| 464 | // having a 1 second resolution, which agrees with |
| 465 | // https://developer.apple.com/legacy/library/#technotes/tn/tn1150.html#HFSPlusDates. |
| 466 | static Time FromTimeSpec(const timespec& ts); |
| 467 | #endif |
| 468 | |
| 469 | // Converts to/from the Javascript convention for times, a number of |
| 470 | // milliseconds since the epoch: |
| 471 | // https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date/getTime. |
| 472 | static Time FromJsTime(double ms_since_epoch); |
| 473 | double ToJsTime() const; |
| 474 | |
apiccion@chromium.org | caa7e46 | 2013-09-24 02:52:36 +0900 | [diff] [blame] | 475 | // Converts to Java convention for times, a number of |
| 476 | // milliseconds since the epoch. |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 477 | int64_t ToJavaTime() const; |
apiccion@chromium.org | caa7e46 | 2013-09-24 02:52:36 +0900 | [diff] [blame] | 478 | |
avi@chromium.org | 1c519e9 | 2013-06-28 03:04:56 +0900 | [diff] [blame] | 479 | #if defined(OS_POSIX) |
| 480 | static Time FromTimeVal(struct timeval t); |
| 481 | struct timeval ToTimeVal() const; |
| 482 | #endif |
| 483 | |
| 484 | #if defined(OS_MACOSX) |
| 485 | static Time FromCFAbsoluteTime(CFAbsoluteTime t); |
| 486 | CFAbsoluteTime ToCFAbsoluteTime() const; |
| 487 | #endif |
| 488 | |
| 489 | #if defined(OS_WIN) |
| 490 | static Time FromFileTime(FILETIME ft); |
| 491 | FILETIME ToFileTime() const; |
| 492 | |
| 493 | // The minimum time of a low resolution timer. This is basically a windows |
| 494 | // constant of ~15.6ms. While it does vary on some older OS versions, we'll |
| 495 | // treat it as static across all windows versions. |
| 496 | static const int kMinLowResolutionThresholdMs = 16; |
| 497 | |
cpu | e9b293b | 2014-08-27 12:58:22 +0900 | [diff] [blame] | 498 | // Enable or disable Windows high resolution timer. |
avi@chromium.org | 1c519e9 | 2013-06-28 03:04:56 +0900 | [diff] [blame] | 499 | static void EnableHighResolutionTimer(bool enable); |
| 500 | |
| 501 | // Activates or deactivates the high resolution timer based on the |activate| |
| 502 | // flag. If the HighResolutionTimer is not Enabled (see |
| 503 | // EnableHighResolutionTimer), this function will return false. Otherwise |
| 504 | // returns true. Each successful activate call must be paired with a |
| 505 | // subsequent deactivate call. |
| 506 | // All callers to activate the high resolution timer must eventually call |
| 507 | // this function to deactivate the high resolution timer. |
| 508 | static bool ActivateHighResolutionTimer(bool activate); |
| 509 | |
| 510 | // Returns true if the high resolution timer is both enabled and activated. |
| 511 | // This is provided for testing only, and is not tracked in a thread-safe |
| 512 | // way. |
| 513 | static bool IsHighResolutionTimerInUse(); |
| 514 | #endif |
| 515 | |
| 516 | // Converts an exploded structure representing either the local time or UTC |
| 517 | // into a Time class. |
| 518 | static Time FromUTCExploded(const Exploded& exploded) { |
| 519 | return FromExploded(false, exploded); |
| 520 | } |
| 521 | static Time FromLocalExploded(const Exploded& exploded) { |
| 522 | return FromExploded(true, exploded); |
| 523 | } |
| 524 | |
avi@chromium.org | 1c519e9 | 2013-06-28 03:04:56 +0900 | [diff] [blame] | 525 | // Converts a string representation of time to a Time object. |
| 526 | // An example of a time string which is converted is as below:- |
| 527 | // "Tue, 15 Nov 1994 12:45:26 GMT". If the timezone is not specified |
| 528 | // in the input string, FromString assumes local time and FromUTCString |
| 529 | // assumes UTC. A timezone that cannot be parsed (e.g. "UTC" which is not |
| 530 | // specified in RFC822) is treated as if the timezone is not specified. |
| 531 | // TODO(iyengar) Move the FromString/FromTimeT/ToTimeT/FromFileTime to |
| 532 | // a new time converter class. |
| 533 | static bool FromString(const char* time_string, Time* parsed_time) { |
| 534 | return FromStringInternal(time_string, true, parsed_time); |
| 535 | } |
| 536 | static bool FromUTCString(const char* time_string, Time* parsed_time) { |
| 537 | return FromStringInternal(time_string, false, parsed_time); |
| 538 | } |
| 539 | |
avi@chromium.org | 1c519e9 | 2013-06-28 03:04:56 +0900 | [diff] [blame] | 540 | // Fills the given exploded structure with either the local time or UTC from |
| 541 | // this time structure (containing UTC). |
| 542 | void UTCExplode(Exploded* exploded) const { |
| 543 | return Explode(false, exploded); |
| 544 | } |
| 545 | void LocalExplode(Exploded* exploded) const { |
| 546 | return Explode(true, exploded); |
| 547 | } |
| 548 | |
| 549 | // Rounds this time down to the nearest day in local time. It will represent |
| 550 | // midnight on that day. |
| 551 | Time LocalMidnight() const; |
| 552 | |
gcasto | d3c5c10 | 2015-05-08 07:36:57 +0900 | [diff] [blame] | 553 | private: |
miu | 352541f | 2015-05-10 09:15:21 +0900 | [diff] [blame] | 554 | friend class time_internal::TimeBase<Time>; |
gcasto | d3c5c10 | 2015-05-08 07:36:57 +0900 | [diff] [blame] | 555 | |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 556 | explicit Time(int64_t us) : TimeBase(us) {} |
avi@chromium.org | 1c519e9 | 2013-06-28 03:04:56 +0900 | [diff] [blame] | 557 | |
| 558 | // Explodes the given time to either local time |is_local = true| or UTC |
| 559 | // |is_local = false|. |
| 560 | void Explode(bool is_local, Exploded* exploded) const; |
| 561 | |
| 562 | // Unexplodes a given time assuming the source is either local time |
| 563 | // |is_local = true| or UTC |is_local = false|. |
| 564 | static Time FromExploded(bool is_local, const Exploded& exploded); |
| 565 | |
| 566 | // Converts a string representation of time to a Time object. |
| 567 | // An example of a time string which is converted is as below:- |
| 568 | // "Tue, 15 Nov 1994 12:45:26 GMT". If the timezone is not specified |
| 569 | // in the input string, local time |is_local = true| or |
| 570 | // UTC |is_local = false| is assumed. A timezone that cannot be parsed |
| 571 | // (e.g. "UTC" which is not specified in RFC822) is treated as if the |
| 572 | // timezone is not specified. |
| 573 | static bool FromStringInternal(const char* time_string, |
| 574 | bool is_local, |
| 575 | Time* parsed_time); |
avi@chromium.org | 1c519e9 | 2013-06-28 03:04:56 +0900 | [diff] [blame] | 576 | }; |
| 577 | |
| 578 | // Inline the TimeDelta factory methods, for fast TimeDelta construction. |
| 579 | |
| 580 | // static |
gavinp@chromium.org | 096c163 | 2014-03-06 05:02:05 +0900 | [diff] [blame] | 581 | inline TimeDelta TimeDelta::FromDays(int days) { |
gavinp@chromium.org | 096c163 | 2014-03-06 05:02:05 +0900 | [diff] [blame] | 582 | if (days == std::numeric_limits<int>::max()) |
| 583 | return Max(); |
avi@chromium.org | 1c519e9 | 2013-06-28 03:04:56 +0900 | [diff] [blame] | 584 | return TimeDelta(days * Time::kMicrosecondsPerDay); |
| 585 | } |
| 586 | |
| 587 | // static |
gavinp@chromium.org | 096c163 | 2014-03-06 05:02:05 +0900 | [diff] [blame] | 588 | inline TimeDelta TimeDelta::FromHours(int hours) { |
gavinp@chromium.org | 096c163 | 2014-03-06 05:02:05 +0900 | [diff] [blame] | 589 | if (hours == std::numeric_limits<int>::max()) |
| 590 | return Max(); |
avi@chromium.org | 1c519e9 | 2013-06-28 03:04:56 +0900 | [diff] [blame] | 591 | return TimeDelta(hours * Time::kMicrosecondsPerHour); |
| 592 | } |
| 593 | |
| 594 | // static |
gavinp@chromium.org | 096c163 | 2014-03-06 05:02:05 +0900 | [diff] [blame] | 595 | inline TimeDelta TimeDelta::FromMinutes(int minutes) { |
gavinp@chromium.org | 096c163 | 2014-03-06 05:02:05 +0900 | [diff] [blame] | 596 | if (minutes == std::numeric_limits<int>::max()) |
| 597 | return Max(); |
avi@chromium.org | 1c519e9 | 2013-06-28 03:04:56 +0900 | [diff] [blame] | 598 | return TimeDelta(minutes * Time::kMicrosecondsPerMinute); |
| 599 | } |
| 600 | |
| 601 | // static |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 602 | inline TimeDelta TimeDelta::FromSeconds(int64_t secs) { |
rvargas | 7505c4f | 2015-07-28 05:09:02 +0900 | [diff] [blame] | 603 | return TimeDelta(secs) * Time::kMicrosecondsPerSecond; |
avi@chromium.org | 1c519e9 | 2013-06-28 03:04:56 +0900 | [diff] [blame] | 604 | } |
| 605 | |
| 606 | // static |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 607 | inline TimeDelta TimeDelta::FromMilliseconds(int64_t ms) { |
rvargas | 7505c4f | 2015-07-28 05:09:02 +0900 | [diff] [blame] | 608 | return TimeDelta(ms) * Time::kMicrosecondsPerMillisecond; |
avi@chromium.org | 1c519e9 | 2013-06-28 03:04:56 +0900 | [diff] [blame] | 609 | } |
| 610 | |
| 611 | // static |
behara.ms@samsung.com | 8c037ed | 2014-04-22 18:00:53 +0900 | [diff] [blame] | 612 | inline TimeDelta TimeDelta::FromSecondsD(double secs) { |
rvargas | 7505c4f | 2015-07-28 05:09:02 +0900 | [diff] [blame] | 613 | return FromDouble(secs * Time::kMicrosecondsPerSecond); |
behara.ms@samsung.com | 8c037ed | 2014-04-22 18:00:53 +0900 | [diff] [blame] | 614 | } |
| 615 | |
| 616 | // static |
| 617 | inline TimeDelta TimeDelta::FromMillisecondsD(double ms) { |
rvargas | 7505c4f | 2015-07-28 05:09:02 +0900 | [diff] [blame] | 618 | return FromDouble(ms * Time::kMicrosecondsPerMillisecond); |
behara.ms@samsung.com | 8c037ed | 2014-04-22 18:00:53 +0900 | [diff] [blame] | 619 | } |
| 620 | |
| 621 | // static |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 622 | inline TimeDelta TimeDelta::FromMicroseconds(int64_t us) { |
avi@chromium.org | 1c519e9 | 2013-06-28 03:04:56 +0900 | [diff] [blame] | 623 | return TimeDelta(us); |
| 624 | } |
| 625 | |
rvargas | 7505c4f | 2015-07-28 05:09:02 +0900 | [diff] [blame] | 626 | // static |
| 627 | inline TimeDelta TimeDelta::FromDouble(double value) { |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 628 | double max_magnitude = std::numeric_limits<int64_t>::max(); |
| 629 | TimeDelta delta = TimeDelta(static_cast<int64_t>(value)); |
rvargas | 7505c4f | 2015-07-28 05:09:02 +0900 | [diff] [blame] | 630 | if (value > max_magnitude) |
| 631 | delta = Max(); |
| 632 | else if (value < -max_magnitude) |
| 633 | delta = -Max(); |
| 634 | return delta; |
| 635 | } |
| 636 | |
ricea | 749c642 | 2014-10-29 20:35:45 +0900 | [diff] [blame] | 637 | // For logging use only. |
| 638 | BASE_EXPORT std::ostream& operator<<(std::ostream& os, Time time); |
| 639 | |
avi@chromium.org | 1c519e9 | 2013-06-28 03:04:56 +0900 | [diff] [blame] | 640 | // TimeTicks ------------------------------------------------------------------ |
| 641 | |
miu | 352541f | 2015-05-10 09:15:21 +0900 | [diff] [blame] | 642 | // Represents monotonically non-decreasing clock time. |
| 643 | class BASE_EXPORT TimeTicks : public time_internal::TimeBase<TimeTicks> { |
avi@chromium.org | 1c519e9 | 2013-06-28 03:04:56 +0900 | [diff] [blame] | 644 | public: |
charliea | 908ce4d | 2016-04-01 04:05:18 +0900 | [diff] [blame^] | 645 | // The underlying clock used to generate new TimeTicks. |
| 646 | enum class Clock { |
| 647 | LINUX_CLOCK_MONOTONIC, |
| 648 | IOS_CF_ABSOLUTE_TIME_MINUS_KERN_BOOTTIME, |
| 649 | MAC_MACH_ABSOLUTE_TIME, |
| 650 | WIN_QPC, |
| 651 | WIN_ROLLOVER_PROTECTED_TIME_GET_TIME |
| 652 | }; |
| 653 | |
miu | 352541f | 2015-05-10 09:15:21 +0900 | [diff] [blame] | 654 | TimeTicks() : TimeBase(0) { |
avi@chromium.org | 1c519e9 | 2013-06-28 03:04:56 +0900 | [diff] [blame] | 655 | } |
| 656 | |
miu | f8ef55b | 2015-01-14 14:33:08 +0900 | [diff] [blame] | 657 | // Platform-dependent tick count representing "right now." When |
| 658 | // IsHighResolution() returns false, the resolution of the clock could be |
| 659 | // as coarse as ~15.6ms. Otherwise, the resolution should be no worse than one |
| 660 | // microsecond. |
avi@chromium.org | 1c519e9 | 2013-06-28 03:04:56 +0900 | [diff] [blame] | 661 | static TimeTicks Now(); |
| 662 | |
miu | f8ef55b | 2015-01-14 14:33:08 +0900 | [diff] [blame] | 663 | // Returns true if the high resolution clock is working on this system and |
| 664 | // Now() will return high resolution values. Note that, on systems where the |
| 665 | // high resolution clock works but is deemed inefficient, the low resolution |
| 666 | // clock will be used instead. |
| 667 | static bool IsHighResolution(); |
| 668 | |
avi@chromium.org | 1c519e9 | 2013-06-28 03:04:56 +0900 | [diff] [blame] | 669 | #if defined(OS_WIN) |
miu | f8ef55b | 2015-01-14 14:33:08 +0900 | [diff] [blame] | 670 | // Translates an absolute QPC timestamp into a TimeTicks value. The returned |
| 671 | // value has the same origin as Now(). Do NOT attempt to use this if |
| 672 | // IsHighResolution() returns false. |
avi@chromium.org | 1c519e9 | 2013-06-28 03:04:56 +0900 | [diff] [blame] | 673 | static TimeTicks FromQPCValue(LONGLONG qpc_value); |
avi@chromium.org | 1c519e9 | 2013-06-28 03:04:56 +0900 | [diff] [blame] | 674 | #endif |
| 675 | |
charliea | 8290eef | 2015-10-08 06:46:23 +0900 | [diff] [blame] | 676 | // Get an estimate of the TimeTick value at the time of the UnixEpoch. Because |
| 677 | // Time and TimeTicks respond differently to user-set time and NTP |
| 678 | // adjustments, this number is only an estimate. Nevertheless, this can be |
| 679 | // useful when you need to relate the value of TimeTicks to a real time and |
| 680 | // date. Note: Upon first invocation, this function takes a snapshot of the |
| 681 | // realtime clock to establish a reference point. This function will return |
| 682 | // the same value for the duration of the application, but will be different |
| 683 | // in future application runs. |
pwestin@google.com | e521113 | 2013-11-01 05:29:38 +0900 | [diff] [blame] | 684 | static TimeTicks UnixEpoch(); |
| 685 | |
brianderson | e3dd73e | 2015-02-13 09:06:08 +0900 | [diff] [blame] | 686 | // Returns |this| snapped to the next tick, given a |tick_phase| and |
| 687 | // repeating |tick_interval| in both directions. |this| may be before, |
| 688 | // after, or equal to the |tick_phase|. |
| 689 | TimeTicks SnappedToNextTick(TimeTicks tick_phase, |
| 690 | TimeDelta tick_interval) const; |
| 691 | |
charliea | 908ce4d | 2016-04-01 04:05:18 +0900 | [diff] [blame^] | 692 | // Returns an enum indicating the underlying clock being used to generate |
| 693 | // TimeTicks timestamps. This function should only be used for debugging and |
| 694 | // logging purposes. |
| 695 | static Clock GetClock(); |
| 696 | |
gcasto | d3c5c10 | 2015-05-08 07:36:57 +0900 | [diff] [blame] | 697 | #if defined(OS_WIN) |
miu | 352541f | 2015-05-10 09:15:21 +0900 | [diff] [blame] | 698 | protected: |
avi@chromium.org | 1c519e9 | 2013-06-28 03:04:56 +0900 | [diff] [blame] | 699 | typedef DWORD (*TickFunctionType)(void); |
| 700 | static TickFunctionType SetMockTickFunction(TickFunctionType ticker); |
| 701 | #endif |
ksakamoto | f75c681 | 2015-05-07 13:35:19 +0900 | [diff] [blame] | 702 | |
miu | 352541f | 2015-05-10 09:15:21 +0900 | [diff] [blame] | 703 | private: |
| 704 | friend class time_internal::TimeBase<TimeTicks>; |
| 705 | |
| 706 | // Please use Now() to create a new object. This is for internal use |
| 707 | // and testing. |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 708 | explicit TimeTicks(int64_t us) : TimeBase(us) {} |
miu | 352541f | 2015-05-10 09:15:21 +0900 | [diff] [blame] | 709 | }; |
gcasto | d3c5c10 | 2015-05-08 07:36:57 +0900 | [diff] [blame] | 710 | |
ricea | 749c642 | 2014-10-29 20:35:45 +0900 | [diff] [blame] | 711 | // For logging use only. |
| 712 | BASE_EXPORT std::ostream& operator<<(std::ostream& os, TimeTicks time_ticks); |
| 713 | |
miu | 85c3556 | 2015-05-30 08:57:12 +0900 | [diff] [blame] | 714 | // ThreadTicks ---------------------------------------------------------------- |
| 715 | |
| 716 | // Represents a clock, specific to a particular thread, than runs only while the |
| 717 | // thread is running. |
| 718 | class BASE_EXPORT ThreadTicks : public time_internal::TimeBase<ThreadTicks> { |
| 719 | public: |
| 720 | ThreadTicks() : TimeBase(0) { |
| 721 | } |
| 722 | |
| 723 | // Returns true if ThreadTicks::Now() is supported on this system. |
| 724 | static bool IsSupported() { |
| 725 | #if (defined(_POSIX_THREAD_CPUTIME) && (_POSIX_THREAD_CPUTIME >= 0)) || \ |
| 726 | (defined(OS_MACOSX) && !defined(OS_IOS)) || defined(OS_ANDROID) |
| 727 | return true; |
fdoray | eabadbd | 2015-10-15 12:46:40 +0900 | [diff] [blame] | 728 | #elif defined(OS_WIN) |
| 729 | return IsSupportedWin(); |
miu | 85c3556 | 2015-05-30 08:57:12 +0900 | [diff] [blame] | 730 | #else |
| 731 | return false; |
| 732 | #endif |
| 733 | } |
| 734 | |
fdoray | eabadbd | 2015-10-15 12:46:40 +0900 | [diff] [blame] | 735 | // Waits until the initialization is completed. Needs to be guarded with a |
| 736 | // call to IsSupported(). |
| 737 | static void WaitUntilInitialized() { |
| 738 | #if defined(OS_WIN) |
| 739 | WaitUntilInitializedWin(); |
| 740 | #endif |
| 741 | } |
| 742 | |
miu | 85c3556 | 2015-05-30 08:57:12 +0900 | [diff] [blame] | 743 | // Returns thread-specific CPU-time on systems that support this feature. |
| 744 | // Needs to be guarded with a call to IsSupported(). Use this timer |
| 745 | // to (approximately) measure how much time the calling thread spent doing |
| 746 | // actual work vs. being de-scheduled. May return bogus results if the thread |
fdoray | eabadbd | 2015-10-15 12:46:40 +0900 | [diff] [blame] | 747 | // migrates to another CPU between two calls. Returns an empty ThreadTicks |
| 748 | // object until the initialization is completed. If a clock reading is |
| 749 | // absolutely needed, call WaitUntilInitialized() before this method. |
miu | 85c3556 | 2015-05-30 08:57:12 +0900 | [diff] [blame] | 750 | static ThreadTicks Now(); |
| 751 | |
| 752 | private: |
| 753 | friend class time_internal::TimeBase<ThreadTicks>; |
| 754 | |
| 755 | // Please use Now() to create a new object. This is for internal use |
| 756 | // and testing. |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 757 | explicit ThreadTicks(int64_t us) : TimeBase(us) {} |
fdoray | eabadbd | 2015-10-15 12:46:40 +0900 | [diff] [blame] | 758 | |
| 759 | #if defined(OS_WIN) |
| 760 | FRIEND_TEST_ALL_PREFIXES(TimeTicks, TSCTicksPerSecond); |
| 761 | |
| 762 | // Returns the frequency of the TSC in ticks per second, or 0 if it hasn't |
| 763 | // been measured yet. Needs to be guarded with a call to IsSupported(). |
| 764 | // This method is declared here rather than in the anonymous namespace to |
| 765 | // allow testing. |
| 766 | static double TSCTicksPerSecond(); |
| 767 | |
| 768 | static bool IsSupportedWin(); |
| 769 | static void WaitUntilInitializedWin(); |
| 770 | #endif |
miu | 85c3556 | 2015-05-30 08:57:12 +0900 | [diff] [blame] | 771 | }; |
| 772 | |
| 773 | // For logging use only. |
| 774 | BASE_EXPORT std::ostream& operator<<(std::ostream& os, ThreadTicks time_ticks); |
| 775 | |
avi@chromium.org | 1c519e9 | 2013-06-28 03:04:56 +0900 | [diff] [blame] | 776 | } // namespace base |
| 777 | |
| 778 | #endif // BASE_TIME_TIME_H_ |