ipc/chromium/src/base/time.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

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) 2006-2008 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 a platform-dependent epoch. Each
michael@0 7 // platform's epoch, along with other system-dependent clock interface
michael@0 8 // routines, is 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 always incrementing for use
michael@0 14 // in measuring time durations. It is internally represented in microseconds.
michael@0 15 // It can not be converted to a human-readable time, but is guaranteed not to
michael@0 16 // decrease (if the user changes the computer clock, Time::Now() may actually
michael@0 17 // decrease or jump).
michael@0 18 //
michael@0 19 // These classes are represented as only a 64-bit value, so they can be
michael@0 20 // efficiently passed by value.
michael@0 21
michael@0 22 #ifndef BASE_TIME_H_
michael@0 23 #define BASE_TIME_H_
michael@0 24
michael@0 25 #include <time.h>
michael@0 26
michael@0 27 #include "base/basictypes.h"
michael@0 28
michael@0 29 #if defined(OS_WIN)
michael@0 30 // For FILETIME in FromFileTime, until it moves to a new converter class.
michael@0 31 // See TODO(iyengar) below.
michael@0 32 #include <windows.h>
michael@0 33 #endif
michael@0 34
michael@0 35 namespace base {
michael@0 36
michael@0 37 class Time;
michael@0 38 class TimeTicks;
michael@0 39
michael@0 40 // This unit test does a lot of manual time manipulation.
michael@0 41 class PageLoadTrackerUnitTest;
michael@0 42
michael@0 43 // TimeDelta ------------------------------------------------------------------
michael@0 44
michael@0 45 class TimeDelta {
michael@0 46 public:
michael@0 47 TimeDelta() : delta_(0) {
michael@0 48 }
michael@0 49
michael@0 50 // Converts units of time to TimeDeltas.
michael@0 51 static TimeDelta FromDays(int64_t days);
michael@0 52 static TimeDelta FromHours(int64_t hours);
michael@0 53 static TimeDelta FromMinutes(int64_t minutes);
michael@0 54 static TimeDelta FromSeconds(int64_t secs);
michael@0 55 static TimeDelta FromMilliseconds(int64_t ms);
michael@0 56 static TimeDelta FromMicroseconds(int64_t us);
michael@0 57
michael@0 58 // Returns the internal numeric value of the TimeDelta object. Please don't
michael@0 59 // use this and do arithmetic on it, as it is more error prone than using the
michael@0 60 // provided operators.
michael@0 61 int64_t ToInternalValue() const {
michael@0 62 return delta_;
michael@0 63 }
michael@0 64
michael@0 65 // Returns the time delta in some unit. The F versions return a floating
michael@0 66 // point value, the "regular" versions return a rounded-down value.
michael@0 67 int InDays() const;
michael@0 68 int InHours() const;
michael@0 69 int InMinutes() const;
michael@0 70 double InSecondsF() const;
michael@0 71 int64_t InSeconds() const;
michael@0 72 double InMillisecondsF() const;
michael@0 73 int64_t InMilliseconds() const;
michael@0 74 int64_t InMicroseconds() const;
michael@0 75
michael@0 76 TimeDelta& operator=(TimeDelta other) {
michael@0 77 delta_ = other.delta_;
michael@0 78 return *this;
michael@0 79 }
michael@0 80
michael@0 81 // Computations with other deltas.
michael@0 82 TimeDelta operator+(TimeDelta other) const {
michael@0 83 return TimeDelta(delta_ + other.delta_);
michael@0 84 }
michael@0 85 TimeDelta operator-(TimeDelta other) const {
michael@0 86 return TimeDelta(delta_ - other.delta_);
michael@0 87 }
michael@0 88
michael@0 89 TimeDelta& operator+=(TimeDelta other) {
michael@0 90 delta_ += other.delta_;
michael@0 91 return *this;
michael@0 92 }
michael@0 93 TimeDelta& operator-=(TimeDelta other) {
michael@0 94 delta_ -= other.delta_;
michael@0 95 return *this;
michael@0 96 }
michael@0 97 TimeDelta operator-() const {
michael@0 98 return TimeDelta(-delta_);
michael@0 99 }
michael@0 100
michael@0 101 // Computations with ints, note that we only allow multiplicative operations
michael@0 102 // with ints, and additive operations with other deltas.
michael@0 103 TimeDelta operator*(int64_t a) const {
michael@0 104 return TimeDelta(delta_ * a);
michael@0 105 }
michael@0 106 TimeDelta operator/(int64_t a) const {
michael@0 107 return TimeDelta(delta_ / a);
michael@0 108 }
michael@0 109 TimeDelta& operator*=(int64_t a) {
michael@0 110 delta_ *= a;
michael@0 111 return *this;
michael@0 112 }
michael@0 113 TimeDelta& operator/=(int64_t a) {
michael@0 114 delta_ /= a;
michael@0 115 return *this;
michael@0 116 }
michael@0 117 int64_t operator/(TimeDelta a) const {
michael@0 118 return delta_ / a.delta_;
michael@0 119 }
michael@0 120
michael@0 121 // Defined below because it depends on the definition of the other classes.
michael@0 122 Time operator+(Time t) const;
michael@0 123 TimeTicks operator+(TimeTicks t) const;
michael@0 124
michael@0 125 // Comparison operators.
michael@0 126 bool operator==(TimeDelta other) const {
michael@0 127 return delta_ == other.delta_;
michael@0 128 }
michael@0 129 bool operator!=(TimeDelta other) const {
michael@0 130 return delta_ != other.delta_;
michael@0 131 }
michael@0 132 bool operator<(TimeDelta other) const {
michael@0 133 return delta_ < other.delta_;
michael@0 134 }
michael@0 135 bool operator<=(TimeDelta other) const {
michael@0 136 return delta_ <= other.delta_;
michael@0 137 }
michael@0 138 bool operator>(TimeDelta other) const {
michael@0 139 return delta_ > other.delta_;
michael@0 140 }
michael@0 141 bool operator>=(TimeDelta other) const {
michael@0 142 return delta_ >= other.delta_;
michael@0 143 }
michael@0 144
michael@0 145 private:
michael@0 146 friend class Time;
michael@0 147 friend class TimeTicks;
michael@0 148 friend TimeDelta operator*(int64_t a, TimeDelta td);
michael@0 149
michael@0 150 // Constructs a delta given the duration in microseconds. This is private
michael@0 151 // to avoid confusion by callers with an integer constructor. Use
michael@0 152 // FromSeconds, FromMilliseconds, etc. instead.
michael@0 153 explicit TimeDelta(int64_t delta_us) : delta_(delta_us) {
michael@0 154 }
michael@0 155
michael@0 156 // Delta in microseconds.
michael@0 157 int64_t delta_;
michael@0 158 };
michael@0 159
michael@0 160 inline TimeDelta operator*(int64_t a, TimeDelta td) {
michael@0 161 return TimeDelta(a * td.delta_);
michael@0 162 }
michael@0 163
michael@0 164 // Time -----------------------------------------------------------------------
michael@0 165
michael@0 166 // Represents a wall clock time.
michael@0 167 class Time {
michael@0 168 public:
michael@0 169 static const int64_t kMillisecondsPerSecond = 1000;
michael@0 170 static const int64_t kMicrosecondsPerMillisecond = 1000;
michael@0 171 static const int64_t kMicrosecondsPerSecond = kMicrosecondsPerMillisecond *
michael@0 172 kMillisecondsPerSecond;
michael@0 173 static const int64_t kMicrosecondsPerMinute = kMicrosecondsPerSecond * 60;
michael@0 174 static const int64_t kMicrosecondsPerHour = kMicrosecondsPerMinute * 60;
michael@0 175 static const int64_t kMicrosecondsPerDay = kMicrosecondsPerHour * 24;
michael@0 176 static const int64_t kMicrosecondsPerWeek = kMicrosecondsPerDay * 7;
michael@0 177 static const int64_t kNanosecondsPerMicrosecond = 1000;
michael@0 178 static const int64_t kNanosecondsPerSecond = kNanosecondsPerMicrosecond *
michael@0 179 kMicrosecondsPerSecond;
michael@0 180
michael@0 181 // Represents an exploded time that can be formatted nicely. This is kind of
michael@0 182 // like the Win32 SYSTEMTIME structure or the Unix "struct tm" with a few
michael@0 183 // additions and changes to prevent errors.
michael@0 184 struct Exploded {
michael@0 185 int year; // Four digit year "2007"
michael@0 186 signed char month; // 1-based month (values 1 = January, etc.)
michael@0 187 signed char day_of_week; // 0-based day of week (0 = Sunday, etc.)
michael@0 188 signed char day_of_month; // 1-based day of month (1-31)
michael@0 189 signed char hour; // Hour within the current day (0-23)
michael@0 190 signed char minute; // Minute within the current hour (0-59)
michael@0 191 signed char second; // Second within the current minute (0-59 plus
michael@0 192 // leap seconds which may take it up to 60).
michael@0 193 int millisecond; // Milliseconds within the current second (0-999)
michael@0 194 };
michael@0 195
michael@0 196 // Contains the NULL time. Use Time::Now() to get the current time.
michael@0 197 explicit Time() : us_(0) {
michael@0 198 }
michael@0 199
michael@0 200 // Returns true if the time object has not been initialized.
michael@0 201 bool is_null() const {
michael@0 202 return us_ == 0;
michael@0 203 }
michael@0 204
michael@0 205 // Returns the current time. Watch out, the system might adjust its clock
michael@0 206 // in which case time will actually go backwards. We don't guarantee that
michael@0 207 // times are increasing, or that two calls to Now() won't be the same.
michael@0 208 static Time Now();
michael@0 209
michael@0 210 // Returns the current time. Same as Now() except that this function always
michael@0 211 // uses system time so that there are no discrepancies between the returned
michael@0 212 // time and system time even on virtual environments including our test bot.
michael@0 213 // For timing sensitive unittests, this function should be used.
michael@0 214 static Time NowFromSystemTime();
michael@0 215
michael@0 216 // Converts to/from time_t in UTC and a Time class.
michael@0 217 // TODO(brettw) this should be removed once everybody starts using the |Time|
michael@0 218 // class.
michael@0 219 static Time FromTimeT(time_t tt);
michael@0 220 time_t ToTimeT() const;
michael@0 221
michael@0 222 // Converts time to/from a double which is the number of seconds since epoch
michael@0 223 // (Jan 1, 1970). Webkit uses this format to represent time.
michael@0 224 static Time FromDoubleT(double dt);
michael@0 225 double ToDoubleT() const;
michael@0 226
michael@0 227
michael@0 228 #if defined(OS_WIN)
michael@0 229 static Time FromFileTime(FILETIME ft);
michael@0 230 FILETIME ToFileTime() const;
michael@0 231 #endif
michael@0 232
michael@0 233 // Converts an exploded structure representing either the local time or UTC
michael@0 234 // into a Time class.
michael@0 235 static Time FromUTCExploded(const Exploded& exploded) {
michael@0 236 return FromExploded(false, exploded);
michael@0 237 }
michael@0 238 static Time FromLocalExploded(const Exploded& exploded) {
michael@0 239 return FromExploded(true, exploded);
michael@0 240 }
michael@0 241
michael@0 242 // Converts an integer value representing Time to a class. This is used
michael@0 243 // when deserializing a |Time| structure, using a value known to be
michael@0 244 // compatible. It is not provided as a constructor because the integer type
michael@0 245 // may be unclear from the perspective of a caller.
michael@0 246 static Time FromInternalValue(int64_t us) {
michael@0 247 return Time(us);
michael@0 248 }
michael@0 249
michael@0 250 // Converts a string representation of time to a Time object.
michael@0 251 // An example of a time string which is converted is as below:-
michael@0 252 // "Tue, 15 Nov 1994 12:45:26 GMT". If the timezone is not specified
michael@0 253 // in the input string, we assume local time.
michael@0 254 // TODO(iyengar) Move the FromString/FromTimeT/ToTimeT/FromFileTime to
michael@0 255 // a new time converter class.
michael@0 256 static bool FromString(const wchar_t* time_string, Time* parsed_time);
michael@0 257
michael@0 258 // For serializing, use FromInternalValue to reconstitute. Please don't use
michael@0 259 // this and do arithmetic on it, as it is more error prone than using the
michael@0 260 // provided operators.
michael@0 261 int64_t ToInternalValue() const {
michael@0 262 return us_;
michael@0 263 }
michael@0 264
michael@0 265 // Fills the given exploded structure with either the local time or UTC from
michael@0 266 // this time structure (containing UTC).
michael@0 267 void UTCExplode(Exploded* exploded) const {
michael@0 268 return Explode(false, exploded);
michael@0 269 }
michael@0 270 void LocalExplode(Exploded* exploded) const {
michael@0 271 return Explode(true, exploded);
michael@0 272 }
michael@0 273
michael@0 274 // Rounds this time down to the nearest day in local time. It will represent
michael@0 275 // midnight on that day.
michael@0 276 Time LocalMidnight() const;
michael@0 277
michael@0 278 Time& operator=(Time other) {
michael@0 279 us_ = other.us_;
michael@0 280 return *this;
michael@0 281 }
michael@0 282
michael@0 283 // Compute the difference between two times.
michael@0 284 TimeDelta operator-(Time other) const {
michael@0 285 return TimeDelta(us_ - other.us_);
michael@0 286 }
michael@0 287
michael@0 288 // Modify by some time delta.
michael@0 289 Time& operator+=(TimeDelta delta) {
michael@0 290 us_ += delta.delta_;
michael@0 291 return *this;
michael@0 292 }
michael@0 293 Time& operator-=(TimeDelta delta) {
michael@0 294 us_ -= delta.delta_;
michael@0 295 return *this;
michael@0 296 }
michael@0 297
michael@0 298 // Return a new time modified by some delta.
michael@0 299 Time operator+(TimeDelta delta) const {
michael@0 300 return us_ + delta.delta_;
michael@0 301 }
michael@0 302 Time operator-(TimeDelta delta) const {
michael@0 303 return us_ - delta.delta_;
michael@0 304 }
michael@0 305
michael@0 306 // Comparison operators
michael@0 307 bool operator==(Time other) const {
michael@0 308 return us_ == other.us_;
michael@0 309 }
michael@0 310 bool operator!=(Time other) const {
michael@0 311 return us_ != other.us_;
michael@0 312 }
michael@0 313 bool operator<(Time other) const {
michael@0 314 return us_ < other.us_;
michael@0 315 }
michael@0 316 bool operator<=(Time other) const {
michael@0 317 return us_ <= other.us_;
michael@0 318 }
michael@0 319 bool operator>(Time other) const {
michael@0 320 return us_ > other.us_;
michael@0 321 }
michael@0 322 bool operator>=(Time other) const {
michael@0 323 return us_ >= other.us_;
michael@0 324 }
michael@0 325
michael@0 326 private:
michael@0 327 friend class TimeDelta;
michael@0 328
michael@0 329 // Explodes the given time to either local time |is_local = true| or UTC
michael@0 330 // |is_local = false|.
michael@0 331 void Explode(bool is_local, Exploded* exploded) const;
michael@0 332
michael@0 333 // Unexplodes a given time assuming the source is either local time
michael@0 334 // |is_local = true| or UTC |is_local = false|.
michael@0 335 static Time FromExploded(bool is_local, const Exploded& exploded);
michael@0 336
michael@0 337 Time(int64_t us) : us_(us) {
michael@0 338 }
michael@0 339
michael@0 340 // The representation of Jan 1, 1970 UTC in microseconds since the
michael@0 341 // platform-dependent epoch.
michael@0 342 static const int64_t kTimeTToMicrosecondsOffset;
michael@0 343
michael@0 344 // Time in microseconds in UTC.
michael@0 345 int64_t us_;
michael@0 346 };
michael@0 347
michael@0 348 inline Time TimeDelta::operator+(Time t) const {
michael@0 349 return Time(t.us_ + delta_);
michael@0 350 }
michael@0 351
michael@0 352 // Inline the TimeDelta factory methods, for fast TimeDelta construction.
michael@0 353
michael@0 354 // static
michael@0 355 inline TimeDelta TimeDelta::FromDays(int64_t days) {
michael@0 356 return TimeDelta(days * Time::kMicrosecondsPerDay);
michael@0 357 }
michael@0 358
michael@0 359 // static
michael@0 360 inline TimeDelta TimeDelta::FromHours(int64_t hours) {
michael@0 361 return TimeDelta(hours * Time::kMicrosecondsPerHour);
michael@0 362 }
michael@0 363
michael@0 364 // static
michael@0 365 inline TimeDelta TimeDelta::FromMinutes(int64_t minutes) {
michael@0 366 return TimeDelta(minutes * Time::kMicrosecondsPerMinute);
michael@0 367 }
michael@0 368
michael@0 369 // static
michael@0 370 inline TimeDelta TimeDelta::FromSeconds(int64_t secs) {
michael@0 371 return TimeDelta(secs * Time::kMicrosecondsPerSecond);
michael@0 372 }
michael@0 373
michael@0 374 // static
michael@0 375 inline TimeDelta TimeDelta::FromMilliseconds(int64_t ms) {
michael@0 376 return TimeDelta(ms * Time::kMicrosecondsPerMillisecond);
michael@0 377 }
michael@0 378
michael@0 379 // static
michael@0 380 inline TimeDelta TimeDelta::FromMicroseconds(int64_t us) {
michael@0 381 return TimeDelta(us);
michael@0 382 }
michael@0 383
michael@0 384 // TimeTicks ------------------------------------------------------------------
michael@0 385
michael@0 386 class TimeTicks {
michael@0 387 public:
michael@0 388 TimeTicks() : ticks_(0) {
michael@0 389 }
michael@0 390
michael@0 391 // Platform-dependent tick count representing "right now."
michael@0 392 // The resolution of this clock is ~1-15ms. Resolution varies depending
michael@0 393 // on hardware/operating system configuration.
michael@0 394 static TimeTicks Now();
michael@0 395
michael@0 396 // Returns a platform-dependent high-resolution tick count. Implementation
michael@0 397 // is hardware dependent and may or may not return sub-millisecond
michael@0 398 // resolution. THIS CALL IS GENERALLY MUCH MORE EXPENSIVE THAN Now() AND
michael@0 399 // SHOULD ONLY BE USED WHEN IT IS REALLY NEEDED.
michael@0 400 static TimeTicks HighResNow();
michael@0 401
michael@0 402 // Returns true if this object has not been initialized.
michael@0 403 bool is_null() const {
michael@0 404 return ticks_ == 0;
michael@0 405 }
michael@0 406
michael@0 407 // Returns the internal numeric value of the TimeTicks object.
michael@0 408 int64_t ToInternalValue() const {
michael@0 409 return ticks_;
michael@0 410 }
michael@0 411
michael@0 412 TimeTicks& operator=(TimeTicks other) {
michael@0 413 ticks_ = other.ticks_;
michael@0 414 return *this;
michael@0 415 }
michael@0 416
michael@0 417 // Compute the difference between two times.
michael@0 418 TimeDelta operator-(TimeTicks other) const {
michael@0 419 return TimeDelta(ticks_ - other.ticks_);
michael@0 420 }
michael@0 421
michael@0 422 // Modify by some time delta.
michael@0 423 TimeTicks& operator+=(TimeDelta delta) {
michael@0 424 ticks_ += delta.delta_;
michael@0 425 return *this;
michael@0 426 }
michael@0 427 TimeTicks& operator-=(TimeDelta delta) {
michael@0 428 ticks_ -= delta.delta_;
michael@0 429 return *this;
michael@0 430 }
michael@0 431
michael@0 432 // Return a new TimeTicks modified by some delta.
michael@0 433 TimeTicks operator+(TimeDelta delta) const {
michael@0 434 return TimeTicks(ticks_ + delta.delta_);
michael@0 435 }
michael@0 436 TimeTicks operator-(TimeDelta delta) const {
michael@0 437 return TimeTicks(ticks_ - delta.delta_);
michael@0 438 }
michael@0 439
michael@0 440 // Comparison operators
michael@0 441 bool operator==(TimeTicks other) const {
michael@0 442 return ticks_ == other.ticks_;
michael@0 443 }
michael@0 444 bool operator!=(TimeTicks other) const {
michael@0 445 return ticks_ != other.ticks_;
michael@0 446 }
michael@0 447 bool operator<(TimeTicks other) const {
michael@0 448 return ticks_ < other.ticks_;
michael@0 449 }
michael@0 450 bool operator<=(TimeTicks other) const {
michael@0 451 return ticks_ <= other.ticks_;
michael@0 452 }
michael@0 453 bool operator>(TimeTicks other) const {
michael@0 454 return ticks_ > other.ticks_;
michael@0 455 }
michael@0 456 bool operator>=(TimeTicks other) const {
michael@0 457 return ticks_ >= other.ticks_;
michael@0 458 }
michael@0 459
michael@0 460 protected:
michael@0 461 friend class TimeDelta;
michael@0 462 friend class PageLoadTrackerUnitTest;
michael@0 463
michael@0 464 // Please use Now() to create a new object. This is for internal use
michael@0 465 // and testing. Ticks is in microseconds.
michael@0 466 explicit TimeTicks(int64_t ticks) : ticks_(ticks) {
michael@0 467 }
michael@0 468
michael@0 469 // Tick count in microseconds.
michael@0 470 int64_t ticks_;
michael@0 471
michael@0 472 #if defined(OS_WIN)
michael@0 473 typedef DWORD (*TickFunctionType)(void);
michael@0 474 static TickFunctionType SetMockTickFunction(TickFunctionType ticker);
michael@0 475 #endif
michael@0 476 };
michael@0 477
michael@0 478 inline TimeTicks TimeDelta::operator+(TimeTicks t) const {
michael@0 479 return TimeTicks(t.ticks_ + delta_);
michael@0 480 }
michael@0 481
michael@0 482 } // namespace base
michael@0 483
michael@0 484 #endif // BASE_TIME_H_

mercurial