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代码进行编译计时,都是用的这个。操作系统