class TimeConstants {
public:
static constexpr int64_t kHoursPerDay = 24;
static constexpr int64_t kMillisecondsPerSecond = 1000;
static constexpr int64_t kMillisecondsPerDay =
kMillisecondsPerSecond * 60 * 60 * kHoursPerDay;
// ...
};複製代碼
TimeDeltahtml
class V8_BASE_EXPORT TimeDelta final {
public:
constexpr TimeDelta() : delta_(0) {}
// Converts units of time to TimeDeltas.
static constexpr TimeDelta FromDays(int days) {
return TimeDelta(days * TimeConstants::kMicrosecondsPerDay);
}
// ...
}複製代碼
TimeBasewindows
template <class TimeClass>
class TimeBase : public TimeConstants {
public:
// ...
int64_t ToInternalValue() const { return us_; }
// ...
static TimeClass FromInternalValue(int64_t us) { return TimeClass(us); }
protected:
explicit constexpr TimeBase(int64_t us) : us_(us) {}
// Time value in a microsecond timebase.
int64_t us_;
};複製代碼
Timeoop
// -----------------------------------------------------------------------------
// Time
//
// This class represents an absolute point in time, internally represented as
// microseconds (s/1,000,000) since 00:00:00 UTC, January 1, 1970.
class V8_BASE_EXPORT Time final : public time_internal::TimeBase<Time> {
// ...
};複製代碼
TimeTicksspa
// -----------------------------------------------------------------------------
// TimeTicks
//
// This class represents an abstract time that is most of the time incrementing
// for use in measuring time durations. It is internally represented in
// microseconds. It can not be converted to a human-readable time, but is
// guaranteed not to decrease (if the user changes the computer clock,
// Time::Now() may actually decrease or jump). But note that TimeTicks may
// "stand still", for example if the computer suspended.
class V8_BASE_EXPORT TimeTicks final : public time_internal::TimeBase<TimeTicks> {
// ...
};複製代碼
這類事件戳比起上的Time優點在於能夠保證數值一直在增長,而且不會受外界因素影響(機器掛了另算)。因此不管是libuv設置輪詢開始時間或處理定時器任務,仍是V8在對JS代碼進行編譯計時,都是用的這個。操作系統