diff --git a/src/base/platform/time.cc b/src/base/platform/time.cc index 088b4e1395..d664bd0587 100644 --- a/src/base/platform/time.cc +++ b/src/base/platform/time.cc @@ -132,36 +132,6 @@ V8_INLINE uint64_t QPCNowRaw() { namespace v8 { namespace base { -TimeDelta TimeDelta::FromDays(int days) { - return TimeDelta(days * Time::kMicrosecondsPerDay); -} - - -TimeDelta TimeDelta::FromHours(int hours) { - return TimeDelta(hours * Time::kMicrosecondsPerHour); -} - - -TimeDelta TimeDelta::FromMinutes(int minutes) { - return TimeDelta(minutes * Time::kMicrosecondsPerMinute); -} - - -TimeDelta TimeDelta::FromSeconds(int64_t seconds) { - return TimeDelta(seconds * Time::kMicrosecondsPerSecond); -} - - -TimeDelta TimeDelta::FromMilliseconds(int64_t milliseconds) { - return TimeDelta(milliseconds * Time::kMicrosecondsPerMillisecond); -} - - -TimeDelta TimeDelta::FromNanoseconds(int64_t nanoseconds) { - return TimeDelta(nanoseconds / Time::kNanosecondsPerMicrosecond); -} - - int TimeDelta::InDays() const { if (IsMax()) { // Preserve max to prevent overflow. diff --git a/src/base/platform/time.h b/src/base/platform/time.h index 9e99166487..7f975c0bec 100644 --- a/src/base/platform/time.h +++ b/src/base/platform/time.h @@ -39,6 +39,25 @@ template class TimeBase; } +class TimeConstants { + public: + static constexpr int64_t kHoursPerDay = 24; + static constexpr int64_t kMillisecondsPerSecond = 1000; + static constexpr int64_t kMillisecondsPerDay = + kMillisecondsPerSecond * 60 * 60 * kHoursPerDay; + static constexpr int64_t kMicrosecondsPerMillisecond = 1000; + static constexpr int64_t kMicrosecondsPerSecond = + kMicrosecondsPerMillisecond * kMillisecondsPerSecond; + static constexpr int64_t kMicrosecondsPerMinute = kMicrosecondsPerSecond * 60; + static constexpr int64_t kMicrosecondsPerHour = kMicrosecondsPerMinute * 60; + static constexpr int64_t kMicrosecondsPerDay = + kMicrosecondsPerHour * kHoursPerDay; + static constexpr int64_t kMicrosecondsPerWeek = kMicrosecondsPerDay * 7; + static constexpr int64_t kNanosecondsPerMicrosecond = 1000; + static constexpr int64_t kNanosecondsPerSecond = + kNanosecondsPerMicrosecond * kMicrosecondsPerSecond; +}; + // ----------------------------------------------------------------------------- // TimeDelta // @@ -50,15 +69,27 @@ class V8_BASE_EXPORT TimeDelta final { constexpr TimeDelta() : delta_(0) {} // Converts units of time to TimeDeltas. - static TimeDelta FromDays(int days); - static TimeDelta FromHours(int hours); - static TimeDelta FromMinutes(int minutes); - static TimeDelta FromSeconds(int64_t seconds); - static TimeDelta FromMilliseconds(int64_t milliseconds); - static TimeDelta FromMicroseconds(int64_t microseconds) { + static constexpr TimeDelta FromDays(int days) { + return TimeDelta(days * TimeConstants::kMicrosecondsPerDay); + } + static constexpr TimeDelta FromHours(int hours) { + return TimeDelta(hours * TimeConstants::kMicrosecondsPerHour); + } + static constexpr TimeDelta FromMinutes(int minutes) { + return TimeDelta(minutes * TimeConstants::kMicrosecondsPerMinute); + } + static constexpr TimeDelta FromSeconds(int64_t seconds) { + return TimeDelta(seconds * TimeConstants::kMicrosecondsPerSecond); + } + static constexpr TimeDelta FromMilliseconds(int64_t milliseconds) { + return TimeDelta(milliseconds * TimeConstants::kMicrosecondsPerMillisecond); + } + static constexpr TimeDelta FromMicroseconds(int64_t microseconds) { return TimeDelta(microseconds); } - static TimeDelta FromNanoseconds(int64_t nanoseconds); + static constexpr TimeDelta FromNanoseconds(int64_t nanoseconds) { + return TimeDelta(nanoseconds / TimeConstants::kNanosecondsPerMicrosecond); + } // Returns the maximum time delta, which should be greater than any reasonable // time delta we might compare it to. Adding or subtracting the maximum time @@ -201,25 +232,9 @@ namespace time_internal { // classes. Each subclass provides for strong type-checking to ensure // semantically meaningful comparison/math of time values from the same clock // source or timeline. -template -class TimeBase { +template +class TimeBase : public TimeConstants { public: - static constexpr int64_t kHoursPerDay = 24; - static constexpr int64_t kMillisecondsPerSecond = 1000; - static constexpr int64_t kMillisecondsPerDay = - kMillisecondsPerSecond * 60 * 60 * kHoursPerDay; - static constexpr int64_t kMicrosecondsPerMillisecond = 1000; - static constexpr int64_t kMicrosecondsPerSecond = - kMicrosecondsPerMillisecond * kMillisecondsPerSecond; - static constexpr int64_t kMicrosecondsPerMinute = kMicrosecondsPerSecond * 60; - static constexpr int64_t kMicrosecondsPerHour = kMicrosecondsPerMinute * 60; - static constexpr int64_t kMicrosecondsPerDay = - kMicrosecondsPerHour * kHoursPerDay; - static constexpr int64_t kMicrosecondsPerWeek = kMicrosecondsPerDay * 7; - static constexpr int64_t kNanosecondsPerMicrosecond = 1000; - static constexpr int64_t kNanosecondsPerSecond = - kNanosecondsPerMicrosecond * kMicrosecondsPerSecond; - #if V8_OS_WIN // To avoid overflow in QPC to Microseconds calculations, since we multiply // by kMicrosecondsPerSecond, then the QPC value should not exceed