time.h revision 5f1c94371a64b3196d4be9466099bb892df9b88e
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 51namespace base { 52 53class Time; 54class TimeTicks; 55 56// TimeDelta ------------------------------------------------------------------ 57 58class 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 205inline 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. 212class 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#else 234 // To avoid overflow in QPC to Microseconds calculations, since we multiply 235 // by kMicrosecondsPerSecond, then the QPC value should not exceed 236 // (2^63 - 1) / 1E6. If it exceeds that threshold, we divide then multiply. 237 static const int64 kQPCOverflowThreshold = 0x8637BD05AF7; 238#endif 239 240 // Represents an exploded time that can be formatted nicely. This is kind of 241 // like the Win32 SYSTEMTIME structure or the Unix "struct tm" with a few 242 // additions and changes to prevent errors. 243 struct BASE_EXPORT Exploded { 244 int year; // Four digit year "2007" 245 int month; // 1-based month (values 1 = January, etc.) 246 int day_of_week; // 0-based day of week (0 = Sunday, etc.) 247 int day_of_month; // 1-based day of month (1-31) 248 int hour; // Hour within the current day (0-23) 249 int minute; // Minute within the current hour (0-59) 250 int second; // Second within the current minute (0-59 plus leap 251 // seconds which may take it up to 60). 252 int millisecond; // Milliseconds within the current second (0-999) 253 254 // A cursory test for whether the data members are within their 255 // respective ranges. A 'true' return value does not guarantee the 256 // Exploded value can be successfully converted to a Time value. 257 bool HasValidValues() const; 258 }; 259 260 // Contains the NULL time. Use Time::Now() to get the current time. 261 Time() : us_(0) { 262 } 263 264 // Returns true if the time object has not been initialized. 265 bool is_null() const { 266 return us_ == 0; 267 } 268 269 // Returns true if the time object is the maximum time. 270 bool is_max() const { 271 return us_ == std::numeric_limits<int64>::max(); 272 } 273 274 // Returns the time for epoch in Unix-like system (Jan 1, 1970). 275 static Time UnixEpoch(); 276 277 // Returns the current time. Watch out, the system might adjust its clock 278 // in which case time will actually go backwards. We don't guarantee that 279 // times are increasing, or that two calls to Now() won't be the same. 280 static Time Now(); 281 282 // Returns the maximum time, which should be greater than any reasonable time 283 // with which we might compare it. 284 static Time Max(); 285 286 // Returns the current time. Same as Now() except that this function always 287 // uses system time so that there are no discrepancies between the returned 288 // time and system time even on virtual environments including our test bot. 289 // For timing sensitive unittests, this function should be used. 290 static Time NowFromSystemTime(); 291 292 // Converts to/from time_t in UTC and a Time class. 293 // TODO(brettw) this should be removed once everybody starts using the |Time| 294 // class. 295 static Time FromTimeT(time_t tt); 296 time_t ToTimeT() const; 297 298 // Converts time to/from a double which is the number of seconds since epoch 299 // (Jan 1, 1970). Webkit uses this format to represent time. 300 // Because WebKit initializes double time value to 0 to indicate "not 301 // initialized", we map it to empty Time object that also means "not 302 // initialized". 303 static Time FromDoubleT(double dt); 304 double ToDoubleT() const; 305 306#if defined(OS_POSIX) 307 // Converts the timespec structure to time. MacOS X 10.8.3 (and tentatively, 308 // earlier versions) will have the |ts|'s tv_nsec component zeroed out, 309 // having a 1 second resolution, which agrees with 310 // https://developer.apple.com/legacy/library/#technotes/tn/tn1150.html#HFSPlusDates. 311 static Time FromTimeSpec(const timespec& ts); 312#endif 313 314 // Converts to/from the Javascript convention for times, a number of 315 // milliseconds since the epoch: 316 // https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date/getTime. 317 static Time FromJsTime(double ms_since_epoch); 318 double ToJsTime() const; 319 320 // Converts to Java convention for times, a number of 321 // milliseconds since the epoch. 322 int64 ToJavaTime() const; 323 324#if defined(OS_POSIX) 325 static Time FromTimeVal(struct timeval t); 326 struct timeval ToTimeVal() const; 327#endif 328 329#if defined(OS_MACOSX) 330 static Time FromCFAbsoluteTime(CFAbsoluteTime t); 331 CFAbsoluteTime ToCFAbsoluteTime() const; 332#endif 333 334#if defined(OS_WIN) 335 static Time FromFileTime(FILETIME ft); 336 FILETIME ToFileTime() const; 337 338 // The minimum time of a low resolution timer. This is basically a windows 339 // constant of ~15.6ms. While it does vary on some older OS versions, we'll 340 // treat it as static across all windows versions. 341 static const int kMinLowResolutionThresholdMs = 16; 342 343 // Enable or disable Windows high resolution timer. If the high resolution 344 // timer is not enabled, calls to ActivateHighResolutionTimer will fail. 345 // When disabling the high resolution timer, this function will not cause 346 // the high resolution timer to be deactivated, but will prevent future 347 // activations. 348 // Must be called from the main thread. 349 // For more details see comments in time_win.cc. 350 static void EnableHighResolutionTimer(bool enable); 351 352 // Activates or deactivates the high resolution timer based on the |activate| 353 // flag. If the HighResolutionTimer is not Enabled (see 354 // EnableHighResolutionTimer), this function will return false. Otherwise 355 // returns true. Each successful activate call must be paired with a 356 // subsequent deactivate call. 357 // All callers to activate the high resolution timer must eventually call 358 // this function to deactivate the high resolution timer. 359 static bool ActivateHighResolutionTimer(bool activate); 360 361 // Returns true if the high resolution timer is both enabled and activated. 362 // This is provided for testing only, and is not tracked in a thread-safe 363 // way. 364 static bool IsHighResolutionTimerInUse(); 365#endif 366 367 // Converts an exploded structure representing either the local time or UTC 368 // into a Time class. 369 static Time FromUTCExploded(const Exploded& exploded) { 370 return FromExploded(false, exploded); 371 } 372 static Time FromLocalExploded(const Exploded& exploded) { 373 return FromExploded(true, exploded); 374 } 375 376 // Converts an integer value representing Time to a class. This is used 377 // when deserializing a |Time| structure, using a value known to be 378 // compatible. It is not provided as a constructor because the integer type 379 // may be unclear from the perspective of a caller. 380 static Time FromInternalValue(int64 us) { 381 return Time(us); 382 } 383 384 // Converts a string representation of time to a Time object. 385 // An example of a time string which is converted is as below:- 386 // "Tue, 15 Nov 1994 12:45:26 GMT". If the timezone is not specified 387 // in the input string, FromString assumes local time and FromUTCString 388 // assumes UTC. A timezone that cannot be parsed (e.g. "UTC" which is not 389 // specified in RFC822) is treated as if the timezone is not specified. 390 // TODO(iyengar) Move the FromString/FromTimeT/ToTimeT/FromFileTime to 391 // a new time converter class. 392 static bool FromString(const char* time_string, Time* parsed_time) { 393 return FromStringInternal(time_string, true, parsed_time); 394 } 395 static bool FromUTCString(const char* time_string, Time* parsed_time) { 396 return FromStringInternal(time_string, false, parsed_time); 397 } 398 399 // For serializing, use FromInternalValue to reconstitute. Please don't use 400 // this and do arithmetic on it, as it is more error prone than using the 401 // provided operators. 402 int64 ToInternalValue() const { 403 return us_; 404 } 405 406 // Fills the given exploded structure with either the local time or UTC from 407 // this time structure (containing UTC). 408 void UTCExplode(Exploded* exploded) const { 409 return Explode(false, exploded); 410 } 411 void LocalExplode(Exploded* exploded) const { 412 return Explode(true, exploded); 413 } 414 415 // Rounds this time down to the nearest day in local time. It will represent 416 // midnight on that day. 417 Time LocalMidnight() const; 418 419 Time& operator=(Time other) { 420 us_ = other.us_; 421 return *this; 422 } 423 424 // Compute the difference between two times. 425 TimeDelta operator-(Time other) const { 426 return TimeDelta(us_ - other.us_); 427 } 428 429 // Modify by some time delta. 430 Time& operator+=(TimeDelta delta) { 431 us_ += delta.delta_; 432 return *this; 433 } 434 Time& operator-=(TimeDelta delta) { 435 us_ -= delta.delta_; 436 return *this; 437 } 438 439 // Return a new time modified by some delta. 440 Time operator+(TimeDelta delta) const { 441 return Time(us_ + delta.delta_); 442 } 443 Time operator-(TimeDelta delta) const { 444 return Time(us_ - delta.delta_); 445 } 446 447 // Comparison operators 448 bool operator==(Time other) const { 449 return us_ == other.us_; 450 } 451 bool operator!=(Time other) const { 452 return us_ != other.us_; 453 } 454 bool operator<(Time other) const { 455 return us_ < other.us_; 456 } 457 bool operator<=(Time other) const { 458 return us_ <= other.us_; 459 } 460 bool operator>(Time other) const { 461 return us_ > other.us_; 462 } 463 bool operator>=(Time other) const { 464 return us_ >= other.us_; 465 } 466 467 private: 468 friend class TimeDelta; 469 470 explicit Time(int64 us) : us_(us) { 471 } 472 473 // Explodes the given time to either local time |is_local = true| or UTC 474 // |is_local = false|. 475 void Explode(bool is_local, Exploded* exploded) const; 476 477 // Unexplodes a given time assuming the source is either local time 478 // |is_local = true| or UTC |is_local = false|. 479 static Time FromExploded(bool is_local, const Exploded& exploded); 480 481 // Converts a string representation of time to a Time object. 482 // An example of a time string which is converted is as below:- 483 // "Tue, 15 Nov 1994 12:45:26 GMT". If the timezone is not specified 484 // in the input string, local time |is_local = true| or 485 // UTC |is_local = false| is assumed. A timezone that cannot be parsed 486 // (e.g. "UTC" which is not specified in RFC822) is treated as if the 487 // timezone is not specified. 488 static bool FromStringInternal(const char* time_string, 489 bool is_local, 490 Time* parsed_time); 491 492 // The representation of Jan 1, 1970 UTC in microseconds since the 493 // platform-dependent epoch. 494 static const int64 kTimeTToMicrosecondsOffset; 495 496#if defined(OS_WIN) 497 // Indicates whether fast timers are usable right now. For instance, 498 // when using battery power, we might elect to prevent high speed timers 499 // which would draw more power. 500 static bool high_resolution_timer_enabled_; 501 // Count of activations on the high resolution timer. Only use in tests 502 // which are single threaded. 503 static int high_resolution_timer_activated_; 504#endif 505 506 // Time in microseconds in UTC. 507 int64 us_; 508}; 509 510// Inline the TimeDelta factory methods, for fast TimeDelta construction. 511 512// static 513inline TimeDelta TimeDelta::FromDays(int days) { 514 // Preserve max to prevent overflow. 515 if (days == std::numeric_limits<int>::max()) 516 return Max(); 517 return TimeDelta(days * Time::kMicrosecondsPerDay); 518} 519 520// static 521inline TimeDelta TimeDelta::FromHours(int hours) { 522 // Preserve max to prevent overflow. 523 if (hours == std::numeric_limits<int>::max()) 524 return Max(); 525 return TimeDelta(hours * Time::kMicrosecondsPerHour); 526} 527 528// static 529inline TimeDelta TimeDelta::FromMinutes(int minutes) { 530 // Preserve max to prevent overflow. 531 if (minutes == std::numeric_limits<int>::max()) 532 return Max(); 533 return TimeDelta(minutes * Time::kMicrosecondsPerMinute); 534} 535 536// static 537inline TimeDelta TimeDelta::FromSeconds(int64 secs) { 538 // Preserve max to prevent overflow. 539 if (secs == std::numeric_limits<int64>::max()) 540 return Max(); 541 return TimeDelta(secs * Time::kMicrosecondsPerSecond); 542} 543 544// static 545inline TimeDelta TimeDelta::FromMilliseconds(int64 ms) { 546 // Preserve max to prevent overflow. 547 if (ms == std::numeric_limits<int64>::max()) 548 return Max(); 549 return TimeDelta(ms * Time::kMicrosecondsPerMillisecond); 550} 551 552// static 553inline TimeDelta TimeDelta::FromSecondsD(double secs) { 554 // Preserve max to prevent overflow. 555 if (secs == std::numeric_limits<double>::infinity()) 556 return Max(); 557 return TimeDelta(secs * Time::kMicrosecondsPerSecond); 558} 559 560// static 561inline TimeDelta TimeDelta::FromMillisecondsD(double ms) { 562 // Preserve max to prevent overflow. 563 if (ms == std::numeric_limits<double>::infinity()) 564 return Max(); 565 return TimeDelta(ms * Time::kMicrosecondsPerMillisecond); 566} 567 568// static 569inline TimeDelta TimeDelta::FromMicroseconds(int64 us) { 570 // Preserve max to prevent overflow. 571 if (us == std::numeric_limits<int64>::max()) 572 return Max(); 573 return TimeDelta(us); 574} 575 576inline Time TimeDelta::operator+(Time t) const { 577 return Time(t.us_ + delta_); 578} 579 580// TimeTicks ------------------------------------------------------------------ 581 582class BASE_EXPORT TimeTicks { 583 public: 584 // We define this even without OS_CHROMEOS for seccomp sandbox testing. 585#if defined(OS_LINUX) 586 // Force definition of the system trace clock; it is a chromeos-only api 587 // at the moment and surfacing it in the right place requires mucking 588 // with glibc et al. 589 static const clockid_t kClockSystemTrace = 11; 590#endif 591 592 TimeTicks() : ticks_(0) { 593 } 594 595 // Platform-dependent tick count representing "right now." 596 // The resolution of this clock is ~1-15ms. Resolution varies depending 597 // on hardware/operating system configuration. 598 static TimeTicks Now(); 599 600 // Returns a platform-dependent high-resolution tick count. Implementation 601 // is hardware dependent and may or may not return sub-millisecond 602 // resolution. THIS CALL IS GENERALLY MUCH MORE EXPENSIVE THAN Now() AND 603 // SHOULD ONLY BE USED WHEN IT IS REALLY NEEDED. 604 static TimeTicks HighResNow(); 605 606 static bool IsHighResNowFastAndReliable(); 607 608 // Returns true if ThreadNow() is supported on this system. 609 static bool IsThreadNowSupported() { 610#if (defined(_POSIX_THREAD_CPUTIME) && (_POSIX_THREAD_CPUTIME >= 0)) || \ 611 (defined(OS_MACOSX) && !defined(OS_IOS)) || defined(OS_ANDROID) 612 return true; 613#else 614 return false; 615#endif 616 } 617 618 // Returns thread-specific CPU-time on systems that support this feature. 619 // Needs to be guarded with a call to IsThreadNowSupported(). Use this timer 620 // to (approximately) measure how much time the calling thread spent doing 621 // actual work vs. being de-scheduled. May return bogus results if the thread 622 // migrates to another CPU between two calls. 623 static TimeTicks ThreadNow(); 624 625 // Returns the current system trace time or, if none is defined, the current 626 // high-res time (i.e. HighResNow()). On systems where a global trace clock 627 // is defined, timestamping TraceEvents's with this value guarantees 628 // synchronization between events collected inside chrome and events 629 // collected outside (e.g. kernel, X server). 630 static TimeTicks NowFromSystemTraceTime(); 631 632#if defined(OS_WIN) 633 // Get the absolute value of QPC time drift. For testing. 634 static int64 GetQPCDriftMicroseconds(); 635 636 static TimeTicks FromQPCValue(LONGLONG qpc_value); 637 638 // Returns true if the high resolution clock is working on this system. 639 // This is only for testing. 640 static bool IsHighResClockWorking(); 641 642 // Enable high resolution time for TimeTicks::Now(). This function will 643 // test for the availability of a working implementation of 644 // QueryPerformanceCounter(). If one is not available, this function does 645 // nothing and the resolution of Now() remains 1ms. Otherwise, all future 646 // calls to TimeTicks::Now() will have the higher resolution provided by QPC. 647 // Returns true if high resolution time was successfully enabled. 648 static bool SetNowIsHighResNowIfSupported(); 649 650 // Returns a time value that is NOT rollover protected. 651 static TimeTicks UnprotectedNow(); 652#endif 653 654 // Returns true if this object has not been initialized. 655 bool is_null() const { 656 return ticks_ == 0; 657 } 658 659 // Converts an integer value representing TimeTicks to a class. This is used 660 // when deserializing a |TimeTicks| structure, using a value known to be 661 // compatible. It is not provided as a constructor because the integer type 662 // may be unclear from the perspective of a caller. 663 static TimeTicks FromInternalValue(int64 ticks) { 664 return TimeTicks(ticks); 665 } 666 667 // Get the TimeTick value at the time of the UnixEpoch. This is useful when 668 // you need to relate the value of TimeTicks to a real time and date. 669 // Note: Upon first invocation, this function takes a snapshot of the realtime 670 // clock to establish a reference point. This function will return the same 671 // value for the duration of the application, but will be different in future 672 // application runs. 673 static TimeTicks UnixEpoch(); 674 675 // Returns the internal numeric value of the TimeTicks object. 676 // For serializing, use FromInternalValue to reconstitute. 677 int64 ToInternalValue() const { 678 return ticks_; 679 } 680 681 TimeTicks& operator=(TimeTicks other) { 682 ticks_ = other.ticks_; 683 return *this; 684 } 685 686 // Compute the difference between two times. 687 TimeDelta operator-(TimeTicks other) const { 688 return TimeDelta(ticks_ - other.ticks_); 689 } 690 691 // Modify by some time delta. 692 TimeTicks& operator+=(TimeDelta delta) { 693 ticks_ += delta.delta_; 694 return *this; 695 } 696 TimeTicks& operator-=(TimeDelta delta) { 697 ticks_ -= delta.delta_; 698 return *this; 699 } 700 701 // Return a new TimeTicks modified by some delta. 702 TimeTicks operator+(TimeDelta delta) const { 703 return TimeTicks(ticks_ + delta.delta_); 704 } 705 TimeTicks operator-(TimeDelta delta) const { 706 return TimeTicks(ticks_ - delta.delta_); 707 } 708 709 // Comparison operators 710 bool operator==(TimeTicks other) const { 711 return ticks_ == other.ticks_; 712 } 713 bool operator!=(TimeTicks other) const { 714 return ticks_ != other.ticks_; 715 } 716 bool operator<(TimeTicks other) const { 717 return ticks_ < other.ticks_; 718 } 719 bool operator<=(TimeTicks other) const { 720 return ticks_ <= other.ticks_; 721 } 722 bool operator>(TimeTicks other) const { 723 return ticks_ > other.ticks_; 724 } 725 bool operator>=(TimeTicks other) const { 726 return ticks_ >= other.ticks_; 727 } 728 729 protected: 730 friend class TimeDelta; 731 732 // Please use Now() to create a new object. This is for internal use 733 // and testing. Ticks is in microseconds. 734 explicit TimeTicks(int64 ticks) : ticks_(ticks) { 735 } 736 737 // Tick count in microseconds. 738 int64 ticks_; 739 740#if defined(OS_WIN) 741 typedef DWORD (*TickFunctionType)(void); 742 static TickFunctionType SetMockTickFunction(TickFunctionType ticker); 743#endif 744}; 745 746inline TimeTicks TimeDelta::operator+(TimeTicks t) const { 747 return TimeTicks(t.ticks_ + delta_); 748} 749 750} // namespace base 751 752#endif // BASE_TIME_TIME_H_ 753