5 #ifndef V8_BASE_PLATFORM_TIME_H_ 6 #define V8_BASE_PLATFORM_TIME_H_ 14 #include "src/base/base-export.h" 15 #include "src/base/bits.h" 16 #include "src/base/macros.h" 17 #include "src/base/safe_math.h" 19 #include "src/base/win32-headers.h" 37 namespace time_internal {
38 template<
class TimeClass>
44 static constexpr
int64_t kHoursPerDay = 24;
45 static constexpr
int64_t kMillisecondsPerSecond = 1000;
46 static constexpr
int64_t kMillisecondsPerDay =
47 kMillisecondsPerSecond * 60 * 60 * kHoursPerDay;
48 static constexpr
int64_t kMicrosecondsPerMillisecond = 1000;
49 static constexpr
int64_t kMicrosecondsPerSecond =
50 kMicrosecondsPerMillisecond * kMillisecondsPerSecond;
51 static constexpr
int64_t kMicrosecondsPerMinute = kMicrosecondsPerSecond * 60;
52 static constexpr
int64_t kMicrosecondsPerHour = kMicrosecondsPerMinute * 60;
53 static constexpr
int64_t kMicrosecondsPerDay =
54 kMicrosecondsPerHour * kHoursPerDay;
55 static constexpr
int64_t kMicrosecondsPerWeek = kMicrosecondsPerDay * 7;
56 static constexpr
int64_t kNanosecondsPerMicrosecond = 1000;
57 static constexpr
int64_t kNanosecondsPerSecond =
58 kNanosecondsPerMicrosecond * kMicrosecondsPerSecond;
72 static constexpr
TimeDelta FromDays(
int days) {
73 return TimeDelta(days * TimeConstants::kMicrosecondsPerDay);
75 static constexpr
TimeDelta FromHours(
int hours) {
76 return TimeDelta(hours * TimeConstants::kMicrosecondsPerHour);
78 static constexpr
TimeDelta FromMinutes(
int minutes) {
79 return TimeDelta(minutes * TimeConstants::kMicrosecondsPerMinute);
82 return TimeDelta(seconds * TimeConstants::kMicrosecondsPerSecond);
85 return TimeDelta(milliseconds * TimeConstants::kMicrosecondsPerMillisecond);
91 return TimeDelta(nanoseconds / TimeConstants::kNanosecondsPerMicrosecond);
105 constexpr
bool IsZero()
const {
return delta_ == 0; }
108 constexpr
bool IsMax()
const {
109 return delta_ == std::numeric_limits<int64_t>::max();
111 constexpr
bool IsMin()
const {
112 return delta_ == std::numeric_limits<int64_t>::min();
122 int InMinutes()
const;
123 double InSecondsF()
const;
125 double InMillisecondsF()
const;
126 int64_t InMilliseconds()
const;
127 int64_t InMillisecondsRoundedUp()
const;
128 int64_t InMicroseconds()
const;
132 static TimeDelta FromMachTimespec(
struct mach_timespec ts);
133 struct mach_timespec ToMachTimespec() const;
136 static TimeDelta FromTimespec(
struct timespec ts);
137 struct timespec ToTimespec() const;
150 delta_ += other.delta_;
154 delta_ -= other.delta_;
159 double TimesOf(
const TimeDelta& other)
const {
160 return static_cast<double>(delta_) / static_cast<double>(other.delta_);
162 double PercentOf(
const TimeDelta& other)
const {
163 return TimesOf(other) * 100.0;
183 return delta_ / other.delta_;
187 constexpr
bool operator==(
const TimeDelta& other)
const {
188 return delta_ == other.delta_;
190 constexpr
bool operator!=(
const TimeDelta& other)
const {
191 return delta_ != other.delta_;
193 constexpr
bool operator<(
const TimeDelta& other)
const {
194 return delta_ < other.delta_;
196 constexpr
bool operator<=(
const TimeDelta& other)
const {
197 return delta_ <= other.delta_;
199 constexpr
bool operator>(
const TimeDelta& other)
const {
200 return delta_ > other.delta_;
202 constexpr
bool operator>=(
const TimeDelta& other)
const {
203 return delta_ >= other.delta_;
219 return TimeDelta(std::numeric_limits<int64_t>::max());
223 constexpr TimeDelta TimeDelta::Min() {
224 return TimeDelta(std::numeric_limits<int64_t>::min());
227 namespace time_internal {
235 template <
class TimeClass>
236 class TimeBase :
public TimeConstants {
242 static constexpr
int64_t kQPCOverflowThreshold = INT64_C(0x8637BD05AF7);
250 constexpr
bool IsNull()
const {
return us_ == 0; }
254 static TimeClass Max() {
255 return TimeClass(std::numeric_limits<int64_t>::max());
257 static TimeClass Min() {
258 return TimeClass(std::numeric_limits<int64_t>::min());
262 constexpr
bool IsMax()
const {
263 return us_ == std::numeric_limits<int64_t>::max();
265 constexpr
bool IsMin()
const {
266 return us_ == std::numeric_limits<int64_t>::min();
272 int64_t ToInternalValue()
const {
return us_; }
274 TimeClass& operator=(TimeClass other) {
276 return *(
static_cast<TimeClass*
>(
this));
280 TimeDelta operator-(TimeClass other)
const {
281 return TimeDelta::FromMicroseconds(us_ - other.us_);
285 TimeClass operator+(TimeDelta delta)
const {
286 return TimeClass(bits::SignedSaturatedAdd64(delta.delta_, us_));
288 TimeClass operator-(TimeDelta delta)
const {
289 return TimeClass(-bits::SignedSaturatedSub64(delta.delta_, us_));
293 TimeClass& operator+=(TimeDelta delta) {
294 return static_cast<TimeClass&
>(*
this = (*
this + delta));
296 TimeClass& operator-=(TimeDelta delta) {
297 return static_cast<TimeClass&
>(*
this = (*
this - delta));
301 bool operator==(TimeClass other)
const {
302 return us_ == other.us_;
304 bool operator!=(TimeClass other)
const {
305 return us_ != other.us_;
307 bool operator<(TimeClass other)
const {
308 return us_ < other.us_;
310 bool operator<=(TimeClass other)
const {
311 return us_ <= other.us_;
313 bool operator>(TimeClass other)
const {
314 return us_ > other.us_;
316 bool operator>=(TimeClass other)
const {
317 return us_ >= other.us_;
324 static TimeClass FromInternalValue(
int64_t us) {
return TimeClass(us); }
327 explicit constexpr TimeBase(
int64_t us) : us_(us) {}
345 constexpr
Time() : TimeBase(0) {}
356 static Time NowFromSystemTime();
359 static Time UnixEpoch() {
return Time(0); }
362 static Time FromTimespec(
struct timespec ts);
363 struct timespec ToTimespec() const;
366 static Time FromTimeval(
struct timeval tv);
367 struct timeval ToTimeval() const;
370 static Time FromFiletime(
struct _FILETIME ft);
371 struct _FILETIME ToFiletime() const;
375 static Time FromJsTime(
double ms_since_epoch);
376 double ToJsTime()
const;
380 explicit constexpr
Time(
int64_t us) : TimeBase(us) {}
383 V8_BASE_EXPORT std::ostream& operator<<(std::ostream&,
const Time&);
418 static bool IsHighResolution();
429 return ticks + delta;
443 static bool IsSupported();
447 static void WaitUntilInitialized() {
449 WaitUntilInitializedWin();
466 static ThreadTicks GetForThread(
const HANDLE& thread_handle);
470 template <
class TimeClass>
480 static double TSCTicksPerSecond();
481 static bool IsSupportedWin();
482 static void WaitUntilInitializedWin();
489 #endif // V8_BASE_PLATFORM_TIME_H_