Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
michael@0 | 2 | // Use of this source code is governed by a BSD-style license that can be |
michael@0 | 3 | // found in the LICENSE file. |
michael@0 | 4 | |
michael@0 | 5 | // Time represents an absolute point in time, internally represented as |
michael@0 | 6 | // microseconds (s/1,000,000) since the Windows epoch (1601-01-01 00:00:00 UTC) |
michael@0 | 7 | // (See http://crbug.com/14734). System-dependent clock interface routines are |
michael@0 | 8 | // defined in time_PLATFORM.cc. |
michael@0 | 9 | // |
michael@0 | 10 | // TimeDelta represents a duration of time, internally represented in |
michael@0 | 11 | // microseconds. |
michael@0 | 12 | // |
michael@0 | 13 | // TimeTicks represents an abstract time that is most of the time incrementing |
michael@0 | 14 | // for use in measuring time durations. It is internally represented in |
michael@0 | 15 | // microseconds. It can not be converted to a human-readable time, but is |
michael@0 | 16 | // guaranteed not to decrease (if the user changes the computer clock, |
michael@0 | 17 | // Time::Now() may actually decrease or jump). But note that TimeTicks may |
michael@0 | 18 | // "stand still", for example if the computer suspended. |
michael@0 | 19 | // |
michael@0 | 20 | // These classes are represented as only a 64-bit value, so they can be |
michael@0 | 21 | // efficiently passed by value. |
michael@0 | 22 | |
michael@0 | 23 | #ifndef BASE_TIME_TIME_H_ |
michael@0 | 24 | #define BASE_TIME_TIME_H_ |
michael@0 | 25 | |
michael@0 | 26 | #include <time.h> |
michael@0 | 27 | |
michael@0 | 28 | #include "base/base_export.h" |
michael@0 | 29 | #include "base/basictypes.h" |
michael@0 | 30 | #include "build/build_config.h" |
michael@0 | 31 | |
michael@0 | 32 | #if defined(OS_MACOSX) |
michael@0 | 33 | #include <CoreFoundation/CoreFoundation.h> |
michael@0 | 34 | // Avoid Mac system header macro leak. |
michael@0 | 35 | #undef TYPE_BOOL |
michael@0 | 36 | #endif |
michael@0 | 37 | |
michael@0 | 38 | #if defined(OS_POSIX) |
michael@0 | 39 | #include <unistd.h> |
michael@0 | 40 | #include <sys/time.h> |
michael@0 | 41 | #endif |
michael@0 | 42 | |
michael@0 | 43 | #if defined(OS_WIN) |
michael@0 | 44 | // For FILETIME in FromFileTime, until it moves to a new converter class. |
michael@0 | 45 | // See TODO(iyengar) below. |
michael@0 | 46 | #include <windows.h> |
michael@0 | 47 | #endif |
michael@0 | 48 | |
michael@0 | 49 | #include <limits> |
michael@0 | 50 | |
michael@0 | 51 | namespace base { |
michael@0 | 52 | |
michael@0 | 53 | class Time; |
michael@0 | 54 | class TimeTicks; |
michael@0 | 55 | |
michael@0 | 56 | // TimeDelta ------------------------------------------------------------------ |
michael@0 | 57 | |
michael@0 | 58 | class BASE_EXPORT TimeDelta { |
michael@0 | 59 | public: |
michael@0 | 60 | TimeDelta() : delta_(0) { |
michael@0 | 61 | } |
michael@0 | 62 | |
michael@0 | 63 | // Converts units of time to TimeDeltas. |
michael@0 | 64 | static TimeDelta FromDays(int64 days); |
michael@0 | 65 | static TimeDelta FromHours(int64 hours); |
michael@0 | 66 | static TimeDelta FromMinutes(int64 minutes); |
michael@0 | 67 | static TimeDelta FromSeconds(int64 secs); |
michael@0 | 68 | static TimeDelta FromMilliseconds(int64 ms); |
michael@0 | 69 | static TimeDelta FromMicroseconds(int64 us); |
michael@0 | 70 | #if defined(OS_WIN) |
michael@0 | 71 | static TimeDelta FromQPCValue(LONGLONG qpc_value); |
michael@0 | 72 | #endif |
michael@0 | 73 | |
michael@0 | 74 | // Converts an integer value representing TimeDelta to a class. This is used |
michael@0 | 75 | // when deserializing a |TimeDelta| structure, using a value known to be |
michael@0 | 76 | // compatible. It is not provided as a constructor because the integer type |
michael@0 | 77 | // may be unclear from the perspective of a caller. |
michael@0 | 78 | static TimeDelta FromInternalValue(int64 delta) { |
michael@0 | 79 | return TimeDelta(delta); |
michael@0 | 80 | } |
michael@0 | 81 | |
michael@0 | 82 | // Returns the internal numeric value of the TimeDelta object. Please don't |
michael@0 | 83 | // use this and do arithmetic on it, as it is more error prone than using the |
michael@0 | 84 | // provided operators. |
michael@0 | 85 | // For serializing, use FromInternalValue to reconstitute. |
michael@0 | 86 | int64 ToInternalValue() const { |
michael@0 | 87 | return delta_; |
michael@0 | 88 | } |
michael@0 | 89 | |
michael@0 | 90 | #if defined(OS_POSIX) |
michael@0 | 91 | struct timespec ToTimeSpec() const; |
michael@0 | 92 | #endif |
michael@0 | 93 | |
michael@0 | 94 | // Returns the time delta in some unit. The F versions return a floating |
michael@0 | 95 | // point value, the "regular" versions return a rounded-down value. |
michael@0 | 96 | // |
michael@0 | 97 | // InMillisecondsRoundedUp() instead returns an integer that is rounded up |
michael@0 | 98 | // to the next full millisecond. |
michael@0 | 99 | int InDays() const; |
michael@0 | 100 | int InHours() const; |
michael@0 | 101 | int InMinutes() const; |
michael@0 | 102 | double InSecondsF() const; |
michael@0 | 103 | int64 InSeconds() const; |
michael@0 | 104 | double InMillisecondsF() const; |
michael@0 | 105 | int64 InMilliseconds() const; |
michael@0 | 106 | int64 InMillisecondsRoundedUp() const; |
michael@0 | 107 | int64 InMicroseconds() const; |
michael@0 | 108 | |
michael@0 | 109 | TimeDelta& operator=(TimeDelta other) { |
michael@0 | 110 | delta_ = other.delta_; |
michael@0 | 111 | return *this; |
michael@0 | 112 | } |
michael@0 | 113 | |
michael@0 | 114 | // Computations with other deltas. |
michael@0 | 115 | TimeDelta operator+(TimeDelta other) const { |
michael@0 | 116 | return TimeDelta(delta_ + other.delta_); |
michael@0 | 117 | } |
michael@0 | 118 | TimeDelta operator-(TimeDelta other) const { |
michael@0 | 119 | return TimeDelta(delta_ - other.delta_); |
michael@0 | 120 | } |
michael@0 | 121 | |
michael@0 | 122 | TimeDelta& operator+=(TimeDelta other) { |
michael@0 | 123 | delta_ += other.delta_; |
michael@0 | 124 | return *this; |
michael@0 | 125 | } |
michael@0 | 126 | TimeDelta& operator-=(TimeDelta other) { |
michael@0 | 127 | delta_ -= other.delta_; |
michael@0 | 128 | return *this; |
michael@0 | 129 | } |
michael@0 | 130 | TimeDelta operator-() const { |
michael@0 | 131 | return TimeDelta(-delta_); |
michael@0 | 132 | } |
michael@0 | 133 | |
michael@0 | 134 | // Computations with ints, note that we only allow multiplicative operations |
michael@0 | 135 | // with ints, and additive operations with other deltas. |
michael@0 | 136 | TimeDelta operator*(int64 a) const { |
michael@0 | 137 | return TimeDelta(delta_ * a); |
michael@0 | 138 | } |
michael@0 | 139 | TimeDelta operator/(int64 a) const { |
michael@0 | 140 | return TimeDelta(delta_ / a); |
michael@0 | 141 | } |
michael@0 | 142 | TimeDelta& operator*=(int64 a) { |
michael@0 | 143 | delta_ *= a; |
michael@0 | 144 | return *this; |
michael@0 | 145 | } |
michael@0 | 146 | TimeDelta& operator/=(int64 a) { |
michael@0 | 147 | delta_ /= a; |
michael@0 | 148 | return *this; |
michael@0 | 149 | } |
michael@0 | 150 | int64 operator/(TimeDelta a) const { |
michael@0 | 151 | return delta_ / a.delta_; |
michael@0 | 152 | } |
michael@0 | 153 | |
michael@0 | 154 | // Defined below because it depends on the definition of the other classes. |
michael@0 | 155 | Time operator+(Time t) const; |
michael@0 | 156 | TimeTicks operator+(TimeTicks t) const; |
michael@0 | 157 | |
michael@0 | 158 | // Comparison operators. |
michael@0 | 159 | bool operator==(TimeDelta other) const { |
michael@0 | 160 | return delta_ == other.delta_; |
michael@0 | 161 | } |
michael@0 | 162 | bool operator!=(TimeDelta other) const { |
michael@0 | 163 | return delta_ != other.delta_; |
michael@0 | 164 | } |
michael@0 | 165 | bool operator<(TimeDelta other) const { |
michael@0 | 166 | return delta_ < other.delta_; |
michael@0 | 167 | } |
michael@0 | 168 | bool operator<=(TimeDelta other) const { |
michael@0 | 169 | return delta_ <= other.delta_; |
michael@0 | 170 | } |
michael@0 | 171 | bool operator>(TimeDelta other) const { |
michael@0 | 172 | return delta_ > other.delta_; |
michael@0 | 173 | } |
michael@0 | 174 | bool operator>=(TimeDelta other) const { |
michael@0 | 175 | return delta_ >= other.delta_; |
michael@0 | 176 | } |
michael@0 | 177 | |
michael@0 | 178 | private: |
michael@0 | 179 | friend class Time; |
michael@0 | 180 | friend class TimeTicks; |
michael@0 | 181 | friend TimeDelta operator*(int64 a, TimeDelta td); |
michael@0 | 182 | |
michael@0 | 183 | // Constructs a delta given the duration in microseconds. This is private |
michael@0 | 184 | // to avoid confusion by callers with an integer constructor. Use |
michael@0 | 185 | // FromSeconds, FromMilliseconds, etc. instead. |
michael@0 | 186 | explicit TimeDelta(int64 delta_us) : delta_(delta_us) { |
michael@0 | 187 | } |
michael@0 | 188 | |
michael@0 | 189 | // Delta in microseconds. |
michael@0 | 190 | int64 delta_; |
michael@0 | 191 | }; |
michael@0 | 192 | |
michael@0 | 193 | inline TimeDelta operator*(int64 a, TimeDelta td) { |
michael@0 | 194 | return TimeDelta(a * td.delta_); |
michael@0 | 195 | } |
michael@0 | 196 | |
michael@0 | 197 | // Time ----------------------------------------------------------------------- |
michael@0 | 198 | |
michael@0 | 199 | // Represents a wall clock time. |
michael@0 | 200 | class BASE_EXPORT Time { |
michael@0 | 201 | public: |
michael@0 | 202 | static const int64 kMillisecondsPerSecond = 1000; |
michael@0 | 203 | static const int64 kMicrosecondsPerMillisecond = 1000; |
michael@0 | 204 | static const int64 kMicrosecondsPerSecond = kMicrosecondsPerMillisecond * |
michael@0 | 205 | kMillisecondsPerSecond; |
michael@0 | 206 | static const int64 kMicrosecondsPerMinute = kMicrosecondsPerSecond * 60; |
michael@0 | 207 | static const int64 kMicrosecondsPerHour = kMicrosecondsPerMinute * 60; |
michael@0 | 208 | static const int64 kMicrosecondsPerDay = kMicrosecondsPerHour * 24; |
michael@0 | 209 | static const int64 kMicrosecondsPerWeek = kMicrosecondsPerDay * 7; |
michael@0 | 210 | static const int64 kNanosecondsPerMicrosecond = 1000; |
michael@0 | 211 | static const int64 kNanosecondsPerSecond = kNanosecondsPerMicrosecond * |
michael@0 | 212 | kMicrosecondsPerSecond; |
michael@0 | 213 | |
michael@0 | 214 | #if !defined(OS_WIN) |
michael@0 | 215 | // On Mac & Linux, this value is the delta from the Windows epoch of 1601 to |
michael@0 | 216 | // the Posix delta of 1970. This is used for migrating between the old |
michael@0 | 217 | // 1970-based epochs to the new 1601-based ones. It should be removed from |
michael@0 | 218 | // this global header and put in the platform-specific ones when we remove the |
michael@0 | 219 | // migration code. |
michael@0 | 220 | static const int64 kWindowsEpochDeltaMicroseconds; |
michael@0 | 221 | #endif |
michael@0 | 222 | |
michael@0 | 223 | // Represents an exploded time that can be formatted nicely. This is kind of |
michael@0 | 224 | // like the Win32 SYSTEMTIME structure or the Unix "struct tm" with a few |
michael@0 | 225 | // additions and changes to prevent errors. |
michael@0 | 226 | struct BASE_EXPORT Exploded { |
michael@0 | 227 | int year; // Four digit year "2007" |
michael@0 | 228 | int month; // 1-based month (values 1 = January, etc.) |
michael@0 | 229 | int day_of_week; // 0-based day of week (0 = Sunday, etc.) |
michael@0 | 230 | int day_of_month; // 1-based day of month (1-31) |
michael@0 | 231 | int hour; // Hour within the current day (0-23) |
michael@0 | 232 | int minute; // Minute within the current hour (0-59) |
michael@0 | 233 | int second; // Second within the current minute (0-59 plus leap |
michael@0 | 234 | // seconds which may take it up to 60). |
michael@0 | 235 | int millisecond; // Milliseconds within the current second (0-999) |
michael@0 | 236 | |
michael@0 | 237 | // A cursory test for whether the data members are within their |
michael@0 | 238 | // respective ranges. A 'true' return value does not guarantee the |
michael@0 | 239 | // Exploded value can be successfully converted to a Time value. |
michael@0 | 240 | bool HasValidValues() const; |
michael@0 | 241 | }; |
michael@0 | 242 | |
michael@0 | 243 | // Contains the NULL time. Use Time::Now() to get the current time. |
michael@0 | 244 | Time() : us_(0) { |
michael@0 | 245 | } |
michael@0 | 246 | |
michael@0 | 247 | // Returns true if the time object has not been initialized. |
michael@0 | 248 | bool is_null() const { |
michael@0 | 249 | return us_ == 0; |
michael@0 | 250 | } |
michael@0 | 251 | |
michael@0 | 252 | // Returns true if the time object is the maximum time. |
michael@0 | 253 | bool is_max() const { |
michael@0 | 254 | return us_ == std::numeric_limits<int64>::max(); |
michael@0 | 255 | } |
michael@0 | 256 | |
michael@0 | 257 | // Returns the time for epoch in Unix-like system (Jan 1, 1970). |
michael@0 | 258 | static Time UnixEpoch(); |
michael@0 | 259 | |
michael@0 | 260 | // Returns the current time. Watch out, the system might adjust its clock |
michael@0 | 261 | // in which case time will actually go backwards. We don't guarantee that |
michael@0 | 262 | // times are increasing, or that two calls to Now() won't be the same. |
michael@0 | 263 | static Time Now(); |
michael@0 | 264 | |
michael@0 | 265 | // Returns the maximum time, which should be greater than any reasonable time |
michael@0 | 266 | // with which we might compare it. |
michael@0 | 267 | static Time Max(); |
michael@0 | 268 | |
michael@0 | 269 | // Returns the current time. Same as Now() except that this function always |
michael@0 | 270 | // uses system time so that there are no discrepancies between the returned |
michael@0 | 271 | // time and system time even on virtual environments including our test bot. |
michael@0 | 272 | // For timing sensitive unittests, this function should be used. |
michael@0 | 273 | static Time NowFromSystemTime(); |
michael@0 | 274 | |
michael@0 | 275 | // Converts to/from time_t in UTC and a Time class. |
michael@0 | 276 | // TODO(brettw) this should be removed once everybody starts using the |Time| |
michael@0 | 277 | // class. |
michael@0 | 278 | static Time FromTimeT(time_t tt); |
michael@0 | 279 | time_t ToTimeT() const; |
michael@0 | 280 | |
michael@0 | 281 | // Converts time to/from a double which is the number of seconds since epoch |
michael@0 | 282 | // (Jan 1, 1970). Webkit uses this format to represent time. |
michael@0 | 283 | // Because WebKit initializes double time value to 0 to indicate "not |
michael@0 | 284 | // initialized", we map it to empty Time object that also means "not |
michael@0 | 285 | // initialized". |
michael@0 | 286 | static Time FromDoubleT(double dt); |
michael@0 | 287 | double ToDoubleT() const; |
michael@0 | 288 | |
michael@0 | 289 | #if defined(OS_POSIX) |
michael@0 | 290 | // Converts the timespec structure to time. MacOS X 10.8.3 (and tentatively, |
michael@0 | 291 | // earlier versions) will have the |ts|'s tv_nsec component zeroed out, |
michael@0 | 292 | // having a 1 second resolution, which agrees with |
michael@0 | 293 | // https://developer.apple.com/legacy/library/#technotes/tn/tn1150.html#HFSPlusDates. |
michael@0 | 294 | static Time FromTimeSpec(const timespec& ts); |
michael@0 | 295 | #endif |
michael@0 | 296 | |
michael@0 | 297 | // Converts to/from the Javascript convention for times, a number of |
michael@0 | 298 | // milliseconds since the epoch: |
michael@0 | 299 | // https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date/getTime. |
michael@0 | 300 | static Time FromJsTime(double ms_since_epoch); |
michael@0 | 301 | double ToJsTime() const; |
michael@0 | 302 | |
michael@0 | 303 | // Converts to Java convention for times, a number of |
michael@0 | 304 | // milliseconds since the epoch. |
michael@0 | 305 | int64 ToJavaTime() const; |
michael@0 | 306 | |
michael@0 | 307 | #if defined(OS_POSIX) |
michael@0 | 308 | static Time FromTimeVal(struct timeval t); |
michael@0 | 309 | struct timeval ToTimeVal() const; |
michael@0 | 310 | #endif |
michael@0 | 311 | |
michael@0 | 312 | #if defined(OS_MACOSX) |
michael@0 | 313 | static Time FromCFAbsoluteTime(CFAbsoluteTime t); |
michael@0 | 314 | CFAbsoluteTime ToCFAbsoluteTime() const; |
michael@0 | 315 | #endif |
michael@0 | 316 | |
michael@0 | 317 | #if defined(OS_WIN) |
michael@0 | 318 | static Time FromFileTime(FILETIME ft); |
michael@0 | 319 | FILETIME ToFileTime() const; |
michael@0 | 320 | |
michael@0 | 321 | // The minimum time of a low resolution timer. This is basically a windows |
michael@0 | 322 | // constant of ~15.6ms. While it does vary on some older OS versions, we'll |
michael@0 | 323 | // treat it as static across all windows versions. |
michael@0 | 324 | static const int kMinLowResolutionThresholdMs = 16; |
michael@0 | 325 | |
michael@0 | 326 | // Enable or disable Windows high resolution timer. If the high resolution |
michael@0 | 327 | // timer is not enabled, calls to ActivateHighResolutionTimer will fail. |
michael@0 | 328 | // When disabling the high resolution timer, this function will not cause |
michael@0 | 329 | // the high resolution timer to be deactivated, but will prevent future |
michael@0 | 330 | // activations. |
michael@0 | 331 | // Must be called from the main thread. |
michael@0 | 332 | // For more details see comments in time_win.cc. |
michael@0 | 333 | static void EnableHighResolutionTimer(bool enable); |
michael@0 | 334 | |
michael@0 | 335 | // Activates or deactivates the high resolution timer based on the |activate| |
michael@0 | 336 | // flag. If the HighResolutionTimer is not Enabled (see |
michael@0 | 337 | // EnableHighResolutionTimer), this function will return false. Otherwise |
michael@0 | 338 | // returns true. Each successful activate call must be paired with a |
michael@0 | 339 | // subsequent deactivate call. |
michael@0 | 340 | // All callers to activate the high resolution timer must eventually call |
michael@0 | 341 | // this function to deactivate the high resolution timer. |
michael@0 | 342 | static bool ActivateHighResolutionTimer(bool activate); |
michael@0 | 343 | |
michael@0 | 344 | // Returns true if the high resolution timer is both enabled and activated. |
michael@0 | 345 | // This is provided for testing only, and is not tracked in a thread-safe |
michael@0 | 346 | // way. |
michael@0 | 347 | static bool IsHighResolutionTimerInUse(); |
michael@0 | 348 | #endif |
michael@0 | 349 | |
michael@0 | 350 | // Converts an exploded structure representing either the local time or UTC |
michael@0 | 351 | // into a Time class. |
michael@0 | 352 | static Time FromUTCExploded(const Exploded& exploded) { |
michael@0 | 353 | return FromExploded(false, exploded); |
michael@0 | 354 | } |
michael@0 | 355 | static Time FromLocalExploded(const Exploded& exploded) { |
michael@0 | 356 | return FromExploded(true, exploded); |
michael@0 | 357 | } |
michael@0 | 358 | |
michael@0 | 359 | // Converts an integer value representing Time to a class. This is used |
michael@0 | 360 | // when deserializing a |Time| structure, using a value known to be |
michael@0 | 361 | // compatible. It is not provided as a constructor because the integer type |
michael@0 | 362 | // may be unclear from the perspective of a caller. |
michael@0 | 363 | static Time FromInternalValue(int64 us) { |
michael@0 | 364 | return Time(us); |
michael@0 | 365 | } |
michael@0 | 366 | |
michael@0 | 367 | // Converts a string representation of time to a Time object. |
michael@0 | 368 | // An example of a time string which is converted is as below:- |
michael@0 | 369 | // "Tue, 15 Nov 1994 12:45:26 GMT". If the timezone is not specified |
michael@0 | 370 | // in the input string, FromString assumes local time and FromUTCString |
michael@0 | 371 | // assumes UTC. A timezone that cannot be parsed (e.g. "UTC" which is not |
michael@0 | 372 | // specified in RFC822) is treated as if the timezone is not specified. |
michael@0 | 373 | // TODO(iyengar) Move the FromString/FromTimeT/ToTimeT/FromFileTime to |
michael@0 | 374 | // a new time converter class. |
michael@0 | 375 | static bool FromString(const char* time_string, Time* parsed_time) { |
michael@0 | 376 | return FromStringInternal(time_string, true, parsed_time); |
michael@0 | 377 | } |
michael@0 | 378 | static bool FromUTCString(const char* time_string, Time* parsed_time) { |
michael@0 | 379 | return FromStringInternal(time_string, false, parsed_time); |
michael@0 | 380 | } |
michael@0 | 381 | |
michael@0 | 382 | // For serializing, use FromInternalValue to reconstitute. Please don't use |
michael@0 | 383 | // this and do arithmetic on it, as it is more error prone than using the |
michael@0 | 384 | // provided operators. |
michael@0 | 385 | int64 ToInternalValue() const { |
michael@0 | 386 | return us_; |
michael@0 | 387 | } |
michael@0 | 388 | |
michael@0 | 389 | // Fills the given exploded structure with either the local time or UTC from |
michael@0 | 390 | // this time structure (containing UTC). |
michael@0 | 391 | void UTCExplode(Exploded* exploded) const { |
michael@0 | 392 | return Explode(false, exploded); |
michael@0 | 393 | } |
michael@0 | 394 | void LocalExplode(Exploded* exploded) const { |
michael@0 | 395 | return Explode(true, exploded); |
michael@0 | 396 | } |
michael@0 | 397 | |
michael@0 | 398 | // Rounds this time down to the nearest day in local time. It will represent |
michael@0 | 399 | // midnight on that day. |
michael@0 | 400 | Time LocalMidnight() const; |
michael@0 | 401 | |
michael@0 | 402 | Time& operator=(Time other) { |
michael@0 | 403 | us_ = other.us_; |
michael@0 | 404 | return *this; |
michael@0 | 405 | } |
michael@0 | 406 | |
michael@0 | 407 | // Compute the difference between two times. |
michael@0 | 408 | TimeDelta operator-(Time other) const { |
michael@0 | 409 | return TimeDelta(us_ - other.us_); |
michael@0 | 410 | } |
michael@0 | 411 | |
michael@0 | 412 | // Modify by some time delta. |
michael@0 | 413 | Time& operator+=(TimeDelta delta) { |
michael@0 | 414 | us_ += delta.delta_; |
michael@0 | 415 | return *this; |
michael@0 | 416 | } |
michael@0 | 417 | Time& operator-=(TimeDelta delta) { |
michael@0 | 418 | us_ -= delta.delta_; |
michael@0 | 419 | return *this; |
michael@0 | 420 | } |
michael@0 | 421 | |
michael@0 | 422 | // Return a new time modified by some delta. |
michael@0 | 423 | Time operator+(TimeDelta delta) const { |
michael@0 | 424 | return Time(us_ + delta.delta_); |
michael@0 | 425 | } |
michael@0 | 426 | Time operator-(TimeDelta delta) const { |
michael@0 | 427 | return Time(us_ - delta.delta_); |
michael@0 | 428 | } |
michael@0 | 429 | |
michael@0 | 430 | // Comparison operators |
michael@0 | 431 | bool operator==(Time other) const { |
michael@0 | 432 | return us_ == other.us_; |
michael@0 | 433 | } |
michael@0 | 434 | bool operator!=(Time other) const { |
michael@0 | 435 | return us_ != other.us_; |
michael@0 | 436 | } |
michael@0 | 437 | bool operator<(Time other) const { |
michael@0 | 438 | return us_ < other.us_; |
michael@0 | 439 | } |
michael@0 | 440 | bool operator<=(Time other) const { |
michael@0 | 441 | return us_ <= other.us_; |
michael@0 | 442 | } |
michael@0 | 443 | bool operator>(Time other) const { |
michael@0 | 444 | return us_ > other.us_; |
michael@0 | 445 | } |
michael@0 | 446 | bool operator>=(Time other) const { |
michael@0 | 447 | return us_ >= other.us_; |
michael@0 | 448 | } |
michael@0 | 449 | |
michael@0 | 450 | private: |
michael@0 | 451 | friend class TimeDelta; |
michael@0 | 452 | |
michael@0 | 453 | explicit Time(int64 us) : us_(us) { |
michael@0 | 454 | } |
michael@0 | 455 | |
michael@0 | 456 | // Explodes the given time to either local time |is_local = true| or UTC |
michael@0 | 457 | // |is_local = false|. |
michael@0 | 458 | void Explode(bool is_local, Exploded* exploded) const; |
michael@0 | 459 | |
michael@0 | 460 | // Unexplodes a given time assuming the source is either local time |
michael@0 | 461 | // |is_local = true| or UTC |is_local = false|. |
michael@0 | 462 | static Time FromExploded(bool is_local, const Exploded& exploded); |
michael@0 | 463 | |
michael@0 | 464 | // Converts a string representation of time to a Time object. |
michael@0 | 465 | // An example of a time string which is converted is as below:- |
michael@0 | 466 | // "Tue, 15 Nov 1994 12:45:26 GMT". If the timezone is not specified |
michael@0 | 467 | // in the input string, local time |is_local = true| or |
michael@0 | 468 | // UTC |is_local = false| is assumed. A timezone that cannot be parsed |
michael@0 | 469 | // (e.g. "UTC" which is not specified in RFC822) is treated as if the |
michael@0 | 470 | // timezone is not specified. |
michael@0 | 471 | static bool FromStringInternal(const char* time_string, |
michael@0 | 472 | bool is_local, |
michael@0 | 473 | Time* parsed_time); |
michael@0 | 474 | |
michael@0 | 475 | // The representation of Jan 1, 1970 UTC in microseconds since the |
michael@0 | 476 | // platform-dependent epoch. |
michael@0 | 477 | static const int64 kTimeTToMicrosecondsOffset; |
michael@0 | 478 | |
michael@0 | 479 | #if defined(OS_WIN) |
michael@0 | 480 | // Indicates whether fast timers are usable right now. For instance, |
michael@0 | 481 | // when using battery power, we might elect to prevent high speed timers |
michael@0 | 482 | // which would draw more power. |
michael@0 | 483 | static bool high_resolution_timer_enabled_; |
michael@0 | 484 | // Count of activations on the high resolution timer. Only use in tests |
michael@0 | 485 | // which are single threaded. |
michael@0 | 486 | static int high_resolution_timer_activated_; |
michael@0 | 487 | #endif |
michael@0 | 488 | |
michael@0 | 489 | // Time in microseconds in UTC. |
michael@0 | 490 | int64 us_; |
michael@0 | 491 | }; |
michael@0 | 492 | |
michael@0 | 493 | // Inline the TimeDelta factory methods, for fast TimeDelta construction. |
michael@0 | 494 | |
michael@0 | 495 | // static |
michael@0 | 496 | inline TimeDelta TimeDelta::FromDays(int64 days) { |
michael@0 | 497 | return TimeDelta(days * Time::kMicrosecondsPerDay); |
michael@0 | 498 | } |
michael@0 | 499 | |
michael@0 | 500 | // static |
michael@0 | 501 | inline TimeDelta TimeDelta::FromHours(int64 hours) { |
michael@0 | 502 | return TimeDelta(hours * Time::kMicrosecondsPerHour); |
michael@0 | 503 | } |
michael@0 | 504 | |
michael@0 | 505 | // static |
michael@0 | 506 | inline TimeDelta TimeDelta::FromMinutes(int64 minutes) { |
michael@0 | 507 | return TimeDelta(minutes * Time::kMicrosecondsPerMinute); |
michael@0 | 508 | } |
michael@0 | 509 | |
michael@0 | 510 | // static |
michael@0 | 511 | inline TimeDelta TimeDelta::FromSeconds(int64 secs) { |
michael@0 | 512 | return TimeDelta(secs * Time::kMicrosecondsPerSecond); |
michael@0 | 513 | } |
michael@0 | 514 | |
michael@0 | 515 | // static |
michael@0 | 516 | inline TimeDelta TimeDelta::FromMilliseconds(int64 ms) { |
michael@0 | 517 | return TimeDelta(ms * Time::kMicrosecondsPerMillisecond); |
michael@0 | 518 | } |
michael@0 | 519 | |
michael@0 | 520 | // static |
michael@0 | 521 | inline TimeDelta TimeDelta::FromMicroseconds(int64 us) { |
michael@0 | 522 | return TimeDelta(us); |
michael@0 | 523 | } |
michael@0 | 524 | |
michael@0 | 525 | inline Time TimeDelta::operator+(Time t) const { |
michael@0 | 526 | return Time(t.us_ + delta_); |
michael@0 | 527 | } |
michael@0 | 528 | |
michael@0 | 529 | // TimeTicks ------------------------------------------------------------------ |
michael@0 | 530 | |
michael@0 | 531 | class BASE_EXPORT TimeTicks { |
michael@0 | 532 | public: |
michael@0 | 533 | TimeTicks() : ticks_(0) { |
michael@0 | 534 | } |
michael@0 | 535 | |
michael@0 | 536 | // Platform-dependent tick count representing "right now." |
michael@0 | 537 | // The resolution of this clock is ~1-15ms. Resolution varies depending |
michael@0 | 538 | // on hardware/operating system configuration. |
michael@0 | 539 | static TimeTicks Now(); |
michael@0 | 540 | |
michael@0 | 541 | // Returns a platform-dependent high-resolution tick count. Implementation |
michael@0 | 542 | // is hardware dependent and may or may not return sub-millisecond |
michael@0 | 543 | // resolution. THIS CALL IS GENERALLY MUCH MORE EXPENSIVE THAN Now() AND |
michael@0 | 544 | // SHOULD ONLY BE USED WHEN IT IS REALLY NEEDED. |
michael@0 | 545 | static TimeTicks HighResNow(); |
michael@0 | 546 | |
michael@0 | 547 | // Returns true if ThreadNow() is supported on this system. |
michael@0 | 548 | static bool IsThreadNowSupported() { |
michael@0 | 549 | #if defined(_POSIX_THREAD_CPUTIME) && (_POSIX_THREAD_CPUTIME >= 0) |
michael@0 | 550 | return true; |
michael@0 | 551 | #else |
michael@0 | 552 | return false; |
michael@0 | 553 | #endif |
michael@0 | 554 | } |
michael@0 | 555 | |
michael@0 | 556 | // Returns thread-specific CPU-time on systems that support this feature. |
michael@0 | 557 | // Needs to be guarded with a call to IsThreadNowSupported(). Use this timer |
michael@0 | 558 | // to (approximately) measure how much time the calling thread spent doing |
michael@0 | 559 | // actual work vs. being de-scheduled. May return bogus results if the thread |
michael@0 | 560 | // migrates to another CPU between two calls. |
michael@0 | 561 | static TimeTicks ThreadNow(); |
michael@0 | 562 | |
michael@0 | 563 | // Returns the current system trace time or, if none is defined, the current |
michael@0 | 564 | // high-res time (i.e. HighResNow()). On systems where a global trace clock |
michael@0 | 565 | // is defined, timestamping TraceEvents's with this value guarantees |
michael@0 | 566 | // synchronization between events collected inside chrome and events |
michael@0 | 567 | // collected outside (e.g. kernel, X server). |
michael@0 | 568 | static TimeTicks NowFromSystemTraceTime(); |
michael@0 | 569 | |
michael@0 | 570 | #if defined(OS_WIN) |
michael@0 | 571 | // Get the absolute value of QPC time drift. For testing. |
michael@0 | 572 | static int64 GetQPCDriftMicroseconds(); |
michael@0 | 573 | |
michael@0 | 574 | static TimeTicks FromQPCValue(LONGLONG qpc_value); |
michael@0 | 575 | |
michael@0 | 576 | // Returns true if the high resolution clock is working on this system. |
michael@0 | 577 | // This is only for testing. |
michael@0 | 578 | static bool IsHighResClockWorking(); |
michael@0 | 579 | |
michael@0 | 580 | // Enable high resolution time for TimeTicks::Now(). This function will |
michael@0 | 581 | // test for the availability of a working implementation of |
michael@0 | 582 | // QueryPerformanceCounter(). If one is not available, this function does |
michael@0 | 583 | // nothing and the resolution of Now() remains 1ms. Otherwise, all future |
michael@0 | 584 | // calls to TimeTicks::Now() will have the higher resolution provided by QPC. |
michael@0 | 585 | // Returns true if high resolution time was successfully enabled. |
michael@0 | 586 | static bool SetNowIsHighResNowIfSupported(); |
michael@0 | 587 | |
michael@0 | 588 | // Returns a time value that is NOT rollover protected. |
michael@0 | 589 | static TimeTicks UnprotectedNow(); |
michael@0 | 590 | #endif |
michael@0 | 591 | |
michael@0 | 592 | // Returns true if this object has not been initialized. |
michael@0 | 593 | bool is_null() const { |
michael@0 | 594 | return ticks_ == 0; |
michael@0 | 595 | } |
michael@0 | 596 | |
michael@0 | 597 | // Converts an integer value representing TimeTicks to a class. This is used |
michael@0 | 598 | // when deserializing a |TimeTicks| structure, using a value known to be |
michael@0 | 599 | // compatible. It is not provided as a constructor because the integer type |
michael@0 | 600 | // may be unclear from the perspective of a caller. |
michael@0 | 601 | static TimeTicks FromInternalValue(int64 ticks) { |
michael@0 | 602 | return TimeTicks(ticks); |
michael@0 | 603 | } |
michael@0 | 604 | |
michael@0 | 605 | // Returns the internal numeric value of the TimeTicks object. |
michael@0 | 606 | // For serializing, use FromInternalValue to reconstitute. |
michael@0 | 607 | int64 ToInternalValue() const { |
michael@0 | 608 | return ticks_; |
michael@0 | 609 | } |
michael@0 | 610 | |
michael@0 | 611 | TimeTicks& operator=(TimeTicks other) { |
michael@0 | 612 | ticks_ = other.ticks_; |
michael@0 | 613 | return *this; |
michael@0 | 614 | } |
michael@0 | 615 | |
michael@0 | 616 | // Compute the difference between two times. |
michael@0 | 617 | TimeDelta operator-(TimeTicks other) const { |
michael@0 | 618 | return TimeDelta(ticks_ - other.ticks_); |
michael@0 | 619 | } |
michael@0 | 620 | |
michael@0 | 621 | // Modify by some time delta. |
michael@0 | 622 | TimeTicks& operator+=(TimeDelta delta) { |
michael@0 | 623 | ticks_ += delta.delta_; |
michael@0 | 624 | return *this; |
michael@0 | 625 | } |
michael@0 | 626 | TimeTicks& operator-=(TimeDelta delta) { |
michael@0 | 627 | ticks_ -= delta.delta_; |
michael@0 | 628 | return *this; |
michael@0 | 629 | } |
michael@0 | 630 | |
michael@0 | 631 | // Return a new TimeTicks modified by some delta. |
michael@0 | 632 | TimeTicks operator+(TimeDelta delta) const { |
michael@0 | 633 | return TimeTicks(ticks_ + delta.delta_); |
michael@0 | 634 | } |
michael@0 | 635 | TimeTicks operator-(TimeDelta delta) const { |
michael@0 | 636 | return TimeTicks(ticks_ - delta.delta_); |
michael@0 | 637 | } |
michael@0 | 638 | |
michael@0 | 639 | // Comparison operators |
michael@0 | 640 | bool operator==(TimeTicks other) const { |
michael@0 | 641 | return ticks_ == other.ticks_; |
michael@0 | 642 | } |
michael@0 | 643 | bool operator!=(TimeTicks other) const { |
michael@0 | 644 | return ticks_ != other.ticks_; |
michael@0 | 645 | } |
michael@0 | 646 | bool operator<(TimeTicks other) const { |
michael@0 | 647 | return ticks_ < other.ticks_; |
michael@0 | 648 | } |
michael@0 | 649 | bool operator<=(TimeTicks other) const { |
michael@0 | 650 | return ticks_ <= other.ticks_; |
michael@0 | 651 | } |
michael@0 | 652 | bool operator>(TimeTicks other) const { |
michael@0 | 653 | return ticks_ > other.ticks_; |
michael@0 | 654 | } |
michael@0 | 655 | bool operator>=(TimeTicks other) const { |
michael@0 | 656 | return ticks_ >= other.ticks_; |
michael@0 | 657 | } |
michael@0 | 658 | |
michael@0 | 659 | protected: |
michael@0 | 660 | friend class TimeDelta; |
michael@0 | 661 | |
michael@0 | 662 | // Please use Now() to create a new object. This is for internal use |
michael@0 | 663 | // and testing. Ticks is in microseconds. |
michael@0 | 664 | explicit TimeTicks(int64 ticks) : ticks_(ticks) { |
michael@0 | 665 | } |
michael@0 | 666 | |
michael@0 | 667 | // Tick count in microseconds. |
michael@0 | 668 | int64 ticks_; |
michael@0 | 669 | |
michael@0 | 670 | #if defined(OS_WIN) |
michael@0 | 671 | typedef DWORD (*TickFunctionType)(void); |
michael@0 | 672 | static TickFunctionType SetMockTickFunction(TickFunctionType ticker); |
michael@0 | 673 | #endif |
michael@0 | 674 | }; |
michael@0 | 675 | |
michael@0 | 676 | inline TimeTicks TimeDelta::operator+(TimeTicks t) const { |
michael@0 | 677 | return TimeTicks(t.ticks_ + delta_); |
michael@0 | 678 | } |
michael@0 | 679 | |
michael@0 | 680 | } // namespace base |
michael@0 | 681 | |
michael@0 | 682 | #endif // BASE_TIME_TIME_H_ |