Commit 5d41105b authored by Michael Lippautz's avatar Michael Lippautz Committed by Commit Bot

base: Make constructors for TimeDelta constexpr

Change-Id: I87d17d087f47eb60efbd2ddaf04d5d574c73164c
Reviewed-on: https://chromium-review.googlesource.com/c/1299245Reviewed-by: 's avatarHannes Payer <hpayer@chromium.org>
Commit-Queue: Michael Lippautz <mlippautz@chromium.org>
Cr-Commit-Position: refs/heads/master@{#57008}
parent ef503f07
...@@ -132,36 +132,6 @@ V8_INLINE uint64_t QPCNowRaw() { ...@@ -132,36 +132,6 @@ V8_INLINE uint64_t QPCNowRaw() {
namespace v8 { namespace v8 {
namespace base { 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 { int TimeDelta::InDays() const {
if (IsMax()) { if (IsMax()) {
// Preserve max to prevent overflow. // Preserve max to prevent overflow.
......
...@@ -39,6 +39,25 @@ template<class TimeClass> ...@@ -39,6 +39,25 @@ template<class TimeClass>
class TimeBase; 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 // TimeDelta
// //
...@@ -50,15 +69,27 @@ class V8_BASE_EXPORT TimeDelta final { ...@@ -50,15 +69,27 @@ class V8_BASE_EXPORT TimeDelta final {
constexpr TimeDelta() : delta_(0) {} constexpr TimeDelta() : delta_(0) {}
// Converts units of time to TimeDeltas. // Converts units of time to TimeDeltas.
static TimeDelta FromDays(int days); static constexpr TimeDelta FromDays(int days) {
static TimeDelta FromHours(int hours); return TimeDelta(days * TimeConstants::kMicrosecondsPerDay);
static TimeDelta FromMinutes(int minutes); }
static TimeDelta FromSeconds(int64_t seconds); static constexpr TimeDelta FromHours(int hours) {
static TimeDelta FromMilliseconds(int64_t milliseconds); return TimeDelta(hours * TimeConstants::kMicrosecondsPerHour);
static TimeDelta FromMicroseconds(int64_t microseconds) { }
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); 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 // 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 // time delta we might compare it to. Adding or subtracting the maximum time
...@@ -201,25 +232,9 @@ namespace time_internal { ...@@ -201,25 +232,9 @@ namespace time_internal {
// classes. Each subclass provides for strong type-checking to ensure // classes. Each subclass provides for strong type-checking to ensure
// semantically meaningful comparison/math of time values from the same clock // semantically meaningful comparison/math of time values from the same clock
// source or timeline. // source or timeline.
template<class TimeClass> template <class TimeClass>
class TimeBase { class TimeBase : public TimeConstants {
public: 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 #if V8_OS_WIN
// To avoid overflow in QPC to Microseconds calculations, since we multiply // To avoid overflow in QPC to Microseconds calculations, since we multiply
// by kMicrosecondsPerSecond, then the QPC value should not exceed // by kMicrosecondsPerSecond, then the QPC value should not exceed
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment