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 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
56 | 56 |
57 #include <iosfwd> | 57 #include <iosfwd> |
58 #include <limits> | 58 #include <limits> |
59 | 59 |
60 #include "base/base_export.h" | 60 #include "base/base_export.h" |
61 #include "base/compiler_specific.h" | 61 #include "base/compiler_specific.h" |
62 #include "base/logging.h" | 62 #include "base/logging.h" |
63 #include "base/numerics/safe_math.h" | 63 #include "base/numerics/safe_math.h" |
64 #include "build/build_config.h" | 64 #include "build/build_config.h" |
65 | 65 |
| 66 #if defined(OS_FUCHSIA) |
| 67 #include <magenta/types.h> |
| 68 #endif |
| 69 |
66 #if defined(OS_MACOSX) | 70 #if defined(OS_MACOSX) |
67 #include <CoreFoundation/CoreFoundation.h> | 71 #include <CoreFoundation/CoreFoundation.h> |
68 // Avoid Mac system header macro leak. | 72 // Avoid Mac system header macro leak. |
69 #undef TYPE_BOOL | 73 #undef TYPE_BOOL |
70 #endif | 74 #endif |
71 | 75 |
72 #if defined(OS_POSIX) | 76 #if defined(OS_POSIX) |
73 #include <unistd.h> | 77 #include <unistd.h> |
74 #include <sys/time.h> | 78 #include <sys/time.h> |
75 #endif | 79 #endif |
(...skipping 330 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
406 inline TimeClass operator+(TimeDelta delta, TimeClass t) { | 410 inline TimeClass operator+(TimeDelta delta, TimeClass t) { |
407 return t + delta; | 411 return t + delta; |
408 } | 412 } |
409 | 413 |
410 // Time ----------------------------------------------------------------------- | 414 // Time ----------------------------------------------------------------------- |
411 | 415 |
412 // Represents a wall clock time in UTC. Values are not guaranteed to be | 416 // Represents a wall clock time in UTC. Values are not guaranteed to be |
413 // monotonically non-decreasing and are subject to large amounts of skew. | 417 // monotonically non-decreasing and are subject to large amounts of skew. |
414 class BASE_EXPORT Time : public time_internal::TimeBase<Time> { | 418 class BASE_EXPORT Time : public time_internal::TimeBase<Time> { |
415 public: | 419 public: |
416 // The representation of Jan 1, 1970 UTC in microseconds since the | 420 // Offset of UNIX epoch (1970-01-01 00:00:00 UTC) from Windows FILETIME epoch |
417 // platform-dependent epoch. | 421 // (1601-01-01 00:00:00 UTC), in microseconds. This value is derived from the |
418 static const int64_t kTimeTToMicrosecondsOffset; | 422 // following: ((1970-1601)*365+89)*24*60*60*1000*1000, where 89 is the number |
| 423 // of leap year days between 1601 and 1970: (1970-1601)/4 excluding 1700, |
| 424 // 1800, and 1900. |
| 425 static constexpr int64_t kTimeTToMicrosecondsOffset = |
| 426 INT64_C(11644473600000000); |
419 | 427 |
420 #if !defined(OS_WIN) | 428 #if defined(OS_WIN) |
421 // On Mac & Linux, this value is the delta from the Windows epoch of 1601 to | |
422 // the Posix delta of 1970. This is used for migrating between the old | |
423 // 1970-based epochs to the new 1601-based ones. It should be removed from | |
424 // this global header and put in the platform-specific ones when we remove the | |
425 // migration code. | |
426 static const int64_t kWindowsEpochDeltaMicroseconds; | |
427 #else | |
428 // To avoid overflow in QPC to Microseconds calculations, since we multiply | 429 // To avoid overflow in QPC to Microseconds calculations, since we multiply |
429 // by kMicrosecondsPerSecond, then the QPC value should not exceed | 430 // by kMicrosecondsPerSecond, then the QPC value should not exceed |
430 // (2^63 - 1) / 1E6. If it exceeds that threshold, we divide then multiply. | 431 // (2^63 - 1) / 1E6. If it exceeds that threshold, we divide then multiply. |
431 enum : int64_t{kQPCOverflowThreshold = 0x8637BD05AF7}; | 432 static constexpr int64_t kQPCOverflowThreshold = INT64_C(0x8637BD05AF7); |
432 #endif | 433 #endif |
433 | 434 |
434 // Represents an exploded time that can be formatted nicely. This is kind of | 435 // Represents an exploded time that can be formatted nicely. This is kind of |
435 // like the Win32 SYSTEMTIME structure or the Unix "struct tm" with a few | 436 // like the Win32 SYSTEMTIME structure or the Unix "struct tm" with a few |
436 // additions and changes to prevent errors. | 437 // additions and changes to prevent errors. |
437 struct BASE_EXPORT Exploded { | 438 struct BASE_EXPORT Exploded { |
438 int year; // Four digit year "2007" | 439 int year; // Four digit year "2007" |
439 int month; // 1-based month (values 1 = January, etc.) | 440 int month; // 1-based month (values 1 = January, etc.) |
440 int day_of_week; // 0-based day of week (0 = Sunday, etc.) | 441 int day_of_week; // 0-based day of week (0 = Sunday, etc.) |
441 int day_of_month; // 1-based day of month (1-31) | 442 int day_of_month; // 1-based day of month (1-31) |
(...skipping 252 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
694 // For logging use only. | 695 // For logging use only. |
695 BASE_EXPORT std::ostream& operator<<(std::ostream& os, Time time); | 696 BASE_EXPORT std::ostream& operator<<(std::ostream& os, Time time); |
696 | 697 |
697 // TimeTicks ------------------------------------------------------------------ | 698 // TimeTicks ------------------------------------------------------------------ |
698 | 699 |
699 // Represents monotonically non-decreasing clock time. | 700 // Represents monotonically non-decreasing clock time. |
700 class BASE_EXPORT TimeTicks : public time_internal::TimeBase<TimeTicks> { | 701 class BASE_EXPORT TimeTicks : public time_internal::TimeBase<TimeTicks> { |
701 public: | 702 public: |
702 // The underlying clock used to generate new TimeTicks. | 703 // The underlying clock used to generate new TimeTicks. |
703 enum class Clock { | 704 enum class Clock { |
| 705 FUCHSIA_MX_CLOCK_MONOTONIC, |
704 LINUX_CLOCK_MONOTONIC, | 706 LINUX_CLOCK_MONOTONIC, |
705 IOS_CF_ABSOLUTE_TIME_MINUS_KERN_BOOTTIME, | 707 IOS_CF_ABSOLUTE_TIME_MINUS_KERN_BOOTTIME, |
706 MAC_MACH_ABSOLUTE_TIME, | 708 MAC_MACH_ABSOLUTE_TIME, |
707 WIN_QPC, | 709 WIN_QPC, |
708 WIN_ROLLOVER_PROTECTED_TIME_GET_TIME | 710 WIN_ROLLOVER_PROTECTED_TIME_GET_TIME |
709 }; | 711 }; |
710 | 712 |
711 TimeTicks() : TimeBase(0) { | 713 TimeTicks() : TimeBase(0) { |
712 } | 714 } |
713 | 715 |
714 // Platform-dependent tick count representing "right now." When | 716 // Platform-dependent tick count representing "right now." When |
715 // IsHighResolution() returns false, the resolution of the clock could be | 717 // IsHighResolution() returns false, the resolution of the clock could be |
716 // as coarse as ~15.6ms. Otherwise, the resolution should be no worse than one | 718 // as coarse as ~15.6ms. Otherwise, the resolution should be no worse than one |
717 // microsecond. | 719 // microsecond. |
718 static TimeTicks Now(); | 720 static TimeTicks Now(); |
719 | 721 |
720 // Returns true if the high resolution clock is working on this system and | 722 // Returns true if the high resolution clock is working on this system and |
721 // Now() will return high resolution values. Note that, on systems where the | 723 // Now() will return high resolution values. Note that, on systems where the |
722 // high resolution clock works but is deemed inefficient, the low resolution | 724 // high resolution clock works but is deemed inefficient, the low resolution |
723 // clock will be used instead. | 725 // clock will be used instead. |
724 static bool IsHighResolution() WARN_UNUSED_RESULT; | 726 static bool IsHighResolution() WARN_UNUSED_RESULT; |
725 | 727 |
726 // Returns true if TimeTicks is consistent across processes, meaning that | 728 // Returns true if TimeTicks is consistent across processes, meaning that |
727 // timestamps taken on different processes can be safely compared with one | 729 // timestamps taken on different processes can be safely compared with one |
728 // another. (Note that, even on platforms where this returns true, time values | 730 // another. (Note that, even on platforms where this returns true, time values |
729 // from different threads that are within one tick of each other must be | 731 // from different threads that are within one tick of each other must be |
730 // considered to have an ambiguous ordering.) | 732 // considered to have an ambiguous ordering.) |
731 static bool IsConsistentAcrossProcesses() WARN_UNUSED_RESULT; | 733 static bool IsConsistentAcrossProcesses() WARN_UNUSED_RESULT; |
732 | 734 |
| 735 #if defined(OS_FUCHSIA) |
| 736 // Creates a TimeTicks from a mx_time_t value. Note that the mx_time_t value |
| 737 // is interpreted in terms of the MX_CLOCK_MONOTONIC clock. |
| 738 static TimeTicks FromMXTime(mx_time_t nanos_since_boot); |
| 739 #endif |
| 740 |
733 #if defined(OS_WIN) | 741 #if defined(OS_WIN) |
734 // Translates an absolute QPC timestamp into a TimeTicks value. The returned | 742 // Translates an absolute QPC timestamp into a TimeTicks value. The returned |
735 // value has the same origin as Now(). Do NOT attempt to use this if | 743 // value has the same origin as Now(). Do NOT attempt to use this if |
736 // IsHighResolution() returns false. | 744 // IsHighResolution() returns false. |
737 static TimeTicks FromQPCValue(LONGLONG qpc_value); | 745 static TimeTicks FromQPCValue(LONGLONG qpc_value); |
738 #endif | 746 #endif |
739 | 747 |
740 #if defined(OS_MACOSX) && !defined(OS_IOS) | 748 #if defined(OS_MACOSX) && !defined(OS_IOS) |
741 static TimeTicks FromMachAbsoluteTime(uint64_t mach_absolute_time); | 749 static TimeTicks FromMachAbsoluteTime(uint64_t mach_absolute_time); |
742 #endif // defined(OS_MACOSX) && !defined(OS_IOS) | 750 #endif // defined(OS_MACOSX) && !defined(OS_IOS) |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
784 // Represents a clock, specific to a particular thread, than runs only while the | 792 // Represents a clock, specific to a particular thread, than runs only while the |
785 // thread is running. | 793 // thread is running. |
786 class BASE_EXPORT ThreadTicks : public time_internal::TimeBase<ThreadTicks> { | 794 class BASE_EXPORT ThreadTicks : public time_internal::TimeBase<ThreadTicks> { |
787 public: | 795 public: |
788 ThreadTicks() : TimeBase(0) { | 796 ThreadTicks() : TimeBase(0) { |
789 } | 797 } |
790 | 798 |
791 // Returns true if ThreadTicks::Now() is supported on this system. | 799 // Returns true if ThreadTicks::Now() is supported on this system. |
792 static bool IsSupported() WARN_UNUSED_RESULT { | 800 static bool IsSupported() WARN_UNUSED_RESULT { |
793 #if (defined(_POSIX_THREAD_CPUTIME) && (_POSIX_THREAD_CPUTIME >= 0)) || \ | 801 #if (defined(_POSIX_THREAD_CPUTIME) && (_POSIX_THREAD_CPUTIME >= 0)) || \ |
794 (defined(OS_MACOSX) && !defined(OS_IOS)) || defined(OS_ANDROID) | 802 (defined(OS_MACOSX) && !defined(OS_IOS)) || defined(OS_ANDROID) || \ |
| 803 defined(OS_FUCHSIA) |
795 return true; | 804 return true; |
796 #elif defined(OS_WIN) | 805 #elif defined(OS_WIN) |
797 return IsSupportedWin(); | 806 return IsSupportedWin(); |
798 #else | 807 #else |
799 return false; | 808 return false; |
800 #endif | 809 #endif |
801 } | 810 } |
802 | 811 |
803 // Waits until the initialization is completed. Needs to be guarded with a | 812 // Waits until the initialization is completed. Needs to be guarded with a |
804 // call to IsSupported(). | 813 // call to IsSupported(). |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
844 static void WaitUntilInitializedWin(); | 853 static void WaitUntilInitializedWin(); |
845 #endif | 854 #endif |
846 }; | 855 }; |
847 | 856 |
848 // For logging use only. | 857 // For logging use only. |
849 BASE_EXPORT std::ostream& operator<<(std::ostream& os, ThreadTicks time_ticks); | 858 BASE_EXPORT std::ostream& operator<<(std::ostream& os, ThreadTicks time_ticks); |
850 | 859 |
851 } // namespace base | 860 } // namespace base |
852 | 861 |
853 #endif // BASE_TIME_TIME_H_ | 862 #endif // BASE_TIME_TIME_H_ |
OLD | NEW |