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