| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // Time represents an absolute point in coordinated universal time (UTC), | 5 // Time represents an absolute point in coordinated universal time (UTC), |
| 6 // internally represented as microseconds (s/1,000,000) since the Windows epoch | 6 // internally represented as microseconds (s/1,000,000) since the Windows epoch |
| 7 // (1601-01-01 00:00:00 UTC). System-dependent clock interface routines are | 7 // (1601-01-01 00:00:00 UTC). System-dependent clock interface routines are |
| 8 // defined in time_PLATFORM.cc. Note that values for Time may skew and jump | 8 // defined in time_PLATFORM.cc. Note that values for Time may skew and jump |
| 9 // around as the operating system makes adjustments to synchronize (e.g., with | 9 // around as the operating system makes adjustments to synchronize (e.g., with |
| 10 // NTP servers). Thus, client code that uses the Time class must account for | 10 // NTP servers). Thus, client code that uses the Time class must account for |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 68 #include <CoreFoundation/CoreFoundation.h> | 68 #include <CoreFoundation/CoreFoundation.h> |
| 69 // Avoid Mac system header macro leak. | 69 // Avoid Mac system header macro leak. |
| 70 #undef TYPE_BOOL | 70 #undef TYPE_BOOL |
| 71 #endif | 71 #endif |
| 72 | 72 |
| 73 #if defined(OS_POSIX) | 73 #if defined(OS_POSIX) |
| 74 #include <unistd.h> | 74 #include <unistd.h> |
| 75 #include <sys/time.h> | 75 #include <sys/time.h> |
| 76 #endif | 76 #endif |
| 77 | 77 |
| 78 #if defined(OS_WIN) | |
| 79 // For FILETIME in FromFileTime, until it moves to a new converter class. | |
| 80 // See TODO(iyengar) below. | |
| 81 #include <windows.h> | |
| 82 #endif | |
| 83 | |
| 84 #include <limits> | 78 #include <limits> |
| 85 | 79 |
| 86 namespace base { | 80 namespace base { |
| 87 | 81 |
| 88 class TimeDelta; | 82 class TimeDelta; |
| 89 | 83 |
| 90 // The functions in the time_internal namespace are meant to be used only by the | 84 // The functions in the time_internal namespace are meant to be used only by the |
| 91 // time classes and functions. Please use the math operators defined in the | 85 // time classes and functions. Please use the math operators defined in the |
| 92 // time classes instead. | 86 // time classes instead. |
| 93 namespace time_internal { | 87 namespace time_internal { |
| (...skipping 18 matching lines...) Expand all Loading... |
| 112 | 106 |
| 113 // Converts units of time to TimeDeltas. | 107 // Converts units of time to TimeDeltas. |
| 114 static TimeDelta FromDays(int days); | 108 static TimeDelta FromDays(int days); |
| 115 static TimeDelta FromHours(int hours); | 109 static TimeDelta FromHours(int hours); |
| 116 static TimeDelta FromMinutes(int minutes); | 110 static TimeDelta FromMinutes(int minutes); |
| 117 static TimeDelta FromSeconds(int64 secs); | 111 static TimeDelta FromSeconds(int64 secs); |
| 118 static TimeDelta FromMilliseconds(int64 ms); | 112 static TimeDelta FromMilliseconds(int64 ms); |
| 119 static TimeDelta FromSecondsD(double secs); | 113 static TimeDelta FromSecondsD(double secs); |
| 120 static TimeDelta FromMillisecondsD(double ms); | 114 static TimeDelta FromMillisecondsD(double ms); |
| 121 static TimeDelta FromMicroseconds(int64 us); | 115 static TimeDelta FromMicroseconds(int64 us); |
| 122 #if defined(OS_WIN) | |
| 123 static TimeDelta FromQPCValue(LONGLONG qpc_value); | |
| 124 #endif | |
| 125 | 116 |
| 126 // Converts an integer value representing TimeDelta to a class. This is used | 117 // Converts an integer value representing TimeDelta to a class. This is used |
| 127 // when deserializing a |TimeDelta| structure, using a value known to be | 118 // when deserializing a |TimeDelta| structure, using a value known to be |
| 128 // compatible. It is not provided as a constructor because the integer type | 119 // compatible. It is not provided as a constructor because the integer type |
| 129 // may be unclear from the perspective of a caller. | 120 // may be unclear from the perspective of a caller. |
| 130 static TimeDelta FromInternalValue(int64 delta) { | 121 static TimeDelta FromInternalValue(int64 delta) { |
| 131 return TimeDelta(delta); | 122 return TimeDelta(delta); |
| 132 } | 123 } |
| 133 | 124 |
| 134 // Returns the maximum time delta, which should be greater than any reasonable | 125 // Returns the maximum time delta, which should be greater than any reasonable |
| (...skipping 263 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 398 // Time ----------------------------------------------------------------------- | 389 // Time ----------------------------------------------------------------------- |
| 399 | 390 |
| 400 // Represents a wall clock time in UTC. Values are not guaranteed to be | 391 // Represents a wall clock time in UTC. Values are not guaranteed to be |
| 401 // monotonically non-decreasing and are subject to large amounts of skew. | 392 // monotonically non-decreasing and are subject to large amounts of skew. |
| 402 class BASE_EXPORT Time : public time_internal::TimeBase<Time> { | 393 class BASE_EXPORT Time : public time_internal::TimeBase<Time> { |
| 403 public: | 394 public: |
| 404 // The representation of Jan 1, 1970 UTC in microseconds since the | 395 // The representation of Jan 1, 1970 UTC in microseconds since the |
| 405 // platform-dependent epoch. | 396 // platform-dependent epoch. |
| 406 static const int64 kTimeTToMicrosecondsOffset; | 397 static const int64 kTimeTToMicrosecondsOffset; |
| 407 | 398 |
| 408 #if !defined(OS_WIN) | |
| 409 // On Mac & Linux, this value is the delta from the Windows epoch of 1601 to | 399 // On Mac & Linux, this value is the delta from the Windows epoch of 1601 to |
| 410 // the Posix delta of 1970. This is used for migrating between the old | 400 // the Posix delta of 1970. This is used for migrating between the old |
| 411 // 1970-based epochs to the new 1601-based ones. It should be removed from | 401 // 1970-based epochs to the new 1601-based ones. It should be removed from |
| 412 // this global header and put in the platform-specific ones when we remove the | 402 // this global header and put in the platform-specific ones when we remove the |
| 413 // migration code. | 403 // migration code. |
| 414 static const int64 kWindowsEpochDeltaMicroseconds; | 404 static const int64 kWindowsEpochDeltaMicroseconds; |
| 415 #else | |
| 416 // To avoid overflow in QPC to Microseconds calculations, since we multiply | |
| 417 // by kMicrosecondsPerSecond, then the QPC value should not exceed | |
| 418 // (2^63 - 1) / 1E6. If it exceeds that threshold, we divide then multiply. | |
| 419 static const int64 kQPCOverflowThreshold = 0x8637BD05AF7; | |
| 420 #endif | |
| 421 | 405 |
| 422 // Represents an exploded time that can be formatted nicely. This is kind of | 406 // Represents an exploded time that can be formatted nicely. This is kind of |
| 423 // like the Win32 SYSTEMTIME structure or the Unix "struct tm" with a few | 407 // like the Win32 SYSTEMTIME structure or the Unix "struct tm" with a few |
| 424 // additions and changes to prevent errors. | 408 // additions and changes to prevent errors. |
| 425 struct BASE_EXPORT Exploded { | 409 struct BASE_EXPORT Exploded { |
| 426 int year; // Four digit year "2007" | 410 int year; // Four digit year "2007" |
| 427 int month; // 1-based month (values 1 = January, etc.) | 411 int month; // 1-based month (values 1 = January, etc.) |
| 428 int day_of_week; // 0-based day of week (0 = Sunday, etc.) | 412 int day_of_week; // 0-based day of week (0 = Sunday, etc.) |
| 429 int day_of_month; // 1-based day of month (1-31) | 413 int day_of_month; // 1-based day of month (1-31) |
| 430 int hour; // Hour within the current day (0-23) | 414 int hour; // Hour within the current day (0-23) |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 496 #if defined(OS_POSIX) | 480 #if defined(OS_POSIX) |
| 497 static Time FromTimeVal(struct timeval t); | 481 static Time FromTimeVal(struct timeval t); |
| 498 struct timeval ToTimeVal() const; | 482 struct timeval ToTimeVal() const; |
| 499 #endif | 483 #endif |
| 500 | 484 |
| 501 #if defined(OS_MACOSX) | 485 #if defined(OS_MACOSX) |
| 502 static Time FromCFAbsoluteTime(CFAbsoluteTime t); | 486 static Time FromCFAbsoluteTime(CFAbsoluteTime t); |
| 503 CFAbsoluteTime ToCFAbsoluteTime() const; | 487 CFAbsoluteTime ToCFAbsoluteTime() const; |
| 504 #endif | 488 #endif |
| 505 | 489 |
| 506 #if defined(OS_WIN) | |
| 507 static Time FromFileTime(FILETIME ft); | |
| 508 FILETIME ToFileTime() const; | |
| 509 | |
| 510 // The minimum time of a low resolution timer. This is basically a windows | |
| 511 // constant of ~15.6ms. While it does vary on some older OS versions, we'll | |
| 512 // treat it as static across all windows versions. | |
| 513 static const int kMinLowResolutionThresholdMs = 16; | |
| 514 | |
| 515 // Enable or disable Windows high resolution timer. | |
| 516 static void EnableHighResolutionTimer(bool enable); | |
| 517 | |
| 518 // Activates or deactivates the high resolution timer based on the |activate| | |
| 519 // flag. If the HighResolutionTimer is not Enabled (see | |
| 520 // EnableHighResolutionTimer), this function will return false. Otherwise | |
| 521 // returns true. Each successful activate call must be paired with a | |
| 522 // subsequent deactivate call. | |
| 523 // All callers to activate the high resolution timer must eventually call | |
| 524 // this function to deactivate the high resolution timer. | |
| 525 static bool ActivateHighResolutionTimer(bool activate); | |
| 526 | |
| 527 // Returns true if the high resolution timer is both enabled and activated. | |
| 528 // This is provided for testing only, and is not tracked in a thread-safe | |
| 529 // way. | |
| 530 static bool IsHighResolutionTimerInUse(); | |
| 531 #endif | |
| 532 | |
| 533 // Converts an exploded structure representing either the local time or UTC | 490 // Converts an exploded structure representing either the local time or UTC |
| 534 // into a Time class. | 491 // into a Time class. |
| 535 static Time FromUTCExploded(const Exploded& exploded) { | 492 static Time FromUTCExploded(const Exploded& exploded) { |
| 536 return FromExploded(false, exploded); | 493 return FromExploded(false, exploded); |
| 537 } | 494 } |
| 538 static Time FromLocalExploded(const Exploded& exploded) { | 495 static Time FromLocalExploded(const Exploded& exploded) { |
| 539 return FromExploded(true, exploded); | 496 return FromExploded(true, exploded); |
| 540 } | 497 } |
| 541 | 498 |
| 542 // Converts a string representation of time to a Time object. | 499 // Converts a string representation of time to a Time object. |
| (...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 675 // as coarse as ~15.6ms. Otherwise, the resolution should be no worse than one | 632 // as coarse as ~15.6ms. Otherwise, the resolution should be no worse than one |
| 676 // microsecond. | 633 // microsecond. |
| 677 static TimeTicks Now(); | 634 static TimeTicks Now(); |
| 678 | 635 |
| 679 // Returns true if the high resolution clock is working on this system and | 636 // Returns true if the high resolution clock is working on this system and |
| 680 // Now() will return high resolution values. Note that, on systems where the | 637 // Now() will return high resolution values. Note that, on systems where the |
| 681 // high resolution clock works but is deemed inefficient, the low resolution | 638 // high resolution clock works but is deemed inefficient, the low resolution |
| 682 // clock will be used instead. | 639 // clock will be used instead. |
| 683 static bool IsHighResolution(); | 640 static bool IsHighResolution(); |
| 684 | 641 |
| 685 #if defined(OS_WIN) | |
| 686 // Translates an absolute QPC timestamp into a TimeTicks value. The returned | |
| 687 // value has the same origin as Now(). Do NOT attempt to use this if | |
| 688 // IsHighResolution() returns false. | |
| 689 static TimeTicks FromQPCValue(LONGLONG qpc_value); | |
| 690 #endif | |
| 691 | |
| 692 // Get the TimeTick value at the time of the UnixEpoch. This is useful when | 642 // Get the TimeTick value at the time of the UnixEpoch. This is useful when |
| 693 // you need to relate the value of TimeTicks to a real time and date. | 643 // you need to relate the value of TimeTicks to a real time and date. |
| 694 // Note: Upon first invocation, this function takes a snapshot of the realtime | 644 // Note: Upon first invocation, this function takes a snapshot of the realtime |
| 695 // clock to establish a reference point. This function will return the same | 645 // clock to establish a reference point. This function will return the same |
| 696 // value for the duration of the application, but will be different in future | 646 // value for the duration of the application, but will be different in future |
| 697 // application runs. | 647 // application runs. |
| 698 static TimeTicks UnixEpoch(); | 648 static TimeTicks UnixEpoch(); |
| 699 | 649 |
| 700 // Returns |this| snapped to the next tick, given a |tick_phase| and | 650 // Returns |this| snapped to the next tick, given a |tick_phase| and |
| 701 // repeating |tick_interval| in both directions. |this| may be before, | 651 // repeating |tick_interval| in both directions. |this| may be before, |
| 702 // after, or equal to the |tick_phase|. | 652 // after, or equal to the |tick_phase|. |
| 703 TimeTicks SnappedToNextTick(TimeTicks tick_phase, | 653 TimeTicks SnappedToNextTick(TimeTicks tick_phase, |
| 704 TimeDelta tick_interval) const; | 654 TimeDelta tick_interval) const; |
| 705 | 655 |
| 706 #if defined(OS_WIN) | |
| 707 protected: | |
| 708 typedef DWORD (*TickFunctionType)(void); | |
| 709 static TickFunctionType SetMockTickFunction(TickFunctionType ticker); | |
| 710 #endif | |
| 711 | |
| 712 private: | 656 private: |
| 713 friend class time_internal::TimeBase<TimeTicks>; | 657 friend class time_internal::TimeBase<TimeTicks>; |
| 714 | 658 |
| 715 // Please use Now() to create a new object. This is for internal use | 659 // Please use Now() to create a new object. This is for internal use |
| 716 // and testing. | 660 // and testing. |
| 717 explicit TimeTicks(int64 us) : TimeBase(us) { | 661 explicit TimeTicks(int64 us) : TimeBase(us) { |
| 718 } | 662 } |
| 719 }; | 663 }; |
| 720 | 664 |
| 721 // For logging use only. | 665 // For logging use only. |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 801 explicit TraceTicks(int64 us) : TimeBase(us) { | 745 explicit TraceTicks(int64 us) : TimeBase(us) { |
| 802 } | 746 } |
| 803 }; | 747 }; |
| 804 | 748 |
| 805 // For logging use only. | 749 // For logging use only. |
| 806 BASE_EXPORT std::ostream& operator<<(std::ostream& os, TraceTicks time_ticks); | 750 BASE_EXPORT std::ostream& operator<<(std::ostream& os, TraceTicks time_ticks); |
| 807 | 751 |
| 808 } // namespace base | 752 } // namespace base |
| 809 | 753 |
| 810 #endif // BASE_TIME_TIME_H_ | 754 #endif // BASE_TIME_TIME_H_ |
| OLD | NEW |