1 // Copyright 2013 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef V8_BASE_PLATFORM_TIME_H_
6 #define V8_BASE_PLATFORM_TIME_H_
12 #include "src/base/macros.h"
14 // Forward declarations.
28 // -----------------------------------------------------------------------------
31 // This class represents a duration of time, internally represented in
34 class TimeDelta final {
36 TimeDelta() : delta_(0) {}
38 // Converts units of time to TimeDeltas.
39 static TimeDelta FromDays(int days);
40 static TimeDelta FromHours(int hours);
41 static TimeDelta FromMinutes(int minutes);
42 static TimeDelta FromSeconds(int64_t seconds);
43 static TimeDelta FromMilliseconds(int64_t milliseconds);
44 static TimeDelta FromMicroseconds(int64_t microseconds) {
45 return TimeDelta(microseconds);
47 static TimeDelta FromNanoseconds(int64_t nanoseconds);
49 // Returns the time delta in some unit. The F versions return a floating
50 // point value, the "regular" versions return a rounded-down value.
52 // InMillisecondsRoundedUp() instead returns an integer that is rounded up
53 // to the next full millisecond.
56 int InMinutes() const;
57 double InSecondsF() const;
58 int64_t InSeconds() const;
59 double InMillisecondsF() const;
60 int64_t InMilliseconds() const;
61 int64_t InMillisecondsRoundedUp() const;
62 int64_t InMicroseconds() const { return delta_; }
63 int64_t InNanoseconds() const;
65 // Converts to/from Mach time specs.
66 static TimeDelta FromMachTimespec(struct mach_timespec ts);
67 struct mach_timespec ToMachTimespec() const;
69 // Converts to/from POSIX time specs.
70 static TimeDelta FromTimespec(struct timespec ts);
71 struct timespec ToTimespec() const;
73 TimeDelta& operator=(const TimeDelta& other) {
74 delta_ = other.delta_;
78 // Computations with other deltas.
79 TimeDelta operator+(const TimeDelta& other) const {
80 return TimeDelta(delta_ + other.delta_);
82 TimeDelta operator-(const TimeDelta& other) const {
83 return TimeDelta(delta_ - other.delta_);
86 TimeDelta& operator+=(const TimeDelta& other) {
87 delta_ += other.delta_;
90 TimeDelta& operator-=(const TimeDelta& other) {
91 delta_ -= other.delta_;
94 TimeDelta operator-() const {
95 return TimeDelta(-delta_);
98 double TimesOf(const TimeDelta& other) const {
99 return static_cast<double>(delta_) / static_cast<double>(other.delta_);
101 double PercentOf(const TimeDelta& other) const {
102 return TimesOf(other) * 100.0;
105 // Computations with ints, note that we only allow multiplicative operations
106 // with ints, and additive operations with other deltas.
107 TimeDelta operator*(int64_t a) const {
108 return TimeDelta(delta_ * a);
110 TimeDelta operator/(int64_t a) const {
111 return TimeDelta(delta_ / a);
113 TimeDelta& operator*=(int64_t a) {
117 TimeDelta& operator/=(int64_t a) {
121 int64_t operator/(const TimeDelta& other) const {
122 return delta_ / other.delta_;
125 // Comparison operators.
126 bool operator==(const TimeDelta& other) const {
127 return delta_ == other.delta_;
129 bool operator!=(const TimeDelta& other) const {
130 return delta_ != other.delta_;
132 bool operator<(const TimeDelta& other) const {
133 return delta_ < other.delta_;
135 bool operator<=(const TimeDelta& other) const {
136 return delta_ <= other.delta_;
138 bool operator>(const TimeDelta& other) const {
139 return delta_ > other.delta_;
141 bool operator>=(const TimeDelta& other) const {
142 return delta_ >= other.delta_;
146 // Constructs a delta given the duration in microseconds. This is private
147 // to avoid confusion by callers with an integer constructor. Use
148 // FromSeconds, FromMilliseconds, etc. instead.
149 explicit TimeDelta(int64_t delta) : delta_(delta) {}
151 // Delta in microseconds.
156 // -----------------------------------------------------------------------------
159 // This class represents an absolute point in time, internally represented as
160 // microseconds (s/1,000,000) since 00:00:00 UTC, January 1, 1970.
164 static const int64_t kMillisecondsPerSecond = 1000;
165 static const int64_t kMicrosecondsPerMillisecond = 1000;
166 static const int64_t kMicrosecondsPerSecond = kMicrosecondsPerMillisecond *
167 kMillisecondsPerSecond;
168 static const int64_t kMicrosecondsPerMinute = kMicrosecondsPerSecond * 60;
169 static const int64_t kMicrosecondsPerHour = kMicrosecondsPerMinute * 60;
170 static const int64_t kMicrosecondsPerDay = kMicrosecondsPerHour * 24;
171 static const int64_t kMicrosecondsPerWeek = kMicrosecondsPerDay * 7;
172 static const int64_t kNanosecondsPerMicrosecond = 1000;
173 static const int64_t kNanosecondsPerSecond = kNanosecondsPerMicrosecond *
174 kMicrosecondsPerSecond;
176 // Contains the NULL time. Use Time::Now() to get the current time.
179 // Returns true if the time object has not been initialized.
180 bool IsNull() const { return us_ == 0; }
182 // Returns true if the time object is the maximum time.
183 bool IsMax() const { return us_ == std::numeric_limits<int64_t>::max(); }
185 // Returns the current time. Watch out, the system might adjust its clock
186 // in which case time will actually go backwards. We don't guarantee that
187 // times are increasing, or that two calls to Now() won't be the same.
190 // Returns the current time. Same as Now() except that this function always
191 // uses system time so that there are no discrepancies between the returned
192 // time and system time even on virtual environments including our test bot.
193 // For timing sensitive unittests, this function should be used.
194 static Time NowFromSystemTime();
196 // Returns the time for epoch in Unix-like system (Jan 1, 1970).
197 static Time UnixEpoch() { return Time(0); }
199 // Returns the maximum time, which should be greater than any reasonable time
200 // with which we might compare it.
201 static Time Max() { return Time(std::numeric_limits<int64_t>::max()); }
203 // Converts to/from internal values. The meaning of the "internal value" is
204 // completely up to the implementation, so it should be treated as opaque.
205 static Time FromInternalValue(int64_t value) {
208 int64_t ToInternalValue() const {
212 // Converts to/from POSIX time specs.
213 static Time FromTimespec(struct timespec ts);
214 struct timespec ToTimespec() const;
216 // Converts to/from POSIX time values.
217 static Time FromTimeval(struct timeval tv);
218 struct timeval ToTimeval() const;
220 // Converts to/from Windows file times.
221 static Time FromFiletime(struct _FILETIME ft);
222 struct _FILETIME ToFiletime() const;
224 // Converts to/from the Javascript convention for times, a number of
225 // milliseconds since the epoch:
226 static Time FromJsTime(double ms_since_epoch);
227 double ToJsTime() const;
229 Time& operator=(const Time& other) {
234 // Compute the difference between two times.
235 TimeDelta operator-(const Time& other) const {
236 return TimeDelta::FromMicroseconds(us_ - other.us_);
239 // Modify by some time delta.
240 Time& operator+=(const TimeDelta& delta) {
241 us_ += delta.InMicroseconds();
244 Time& operator-=(const TimeDelta& delta) {
245 us_ -= delta.InMicroseconds();
249 // Return a new time modified by some delta.
250 Time operator+(const TimeDelta& delta) const {
251 return Time(us_ + delta.InMicroseconds());
253 Time operator-(const TimeDelta& delta) const {
254 return Time(us_ - delta.InMicroseconds());
257 // Comparison operators
258 bool operator==(const Time& other) const {
259 return us_ == other.us_;
261 bool operator!=(const Time& other) const {
262 return us_ != other.us_;
264 bool operator<(const Time& other) const {
265 return us_ < other.us_;
267 bool operator<=(const Time& other) const {
268 return us_ <= other.us_;
270 bool operator>(const Time& other) const {
271 return us_ > other.us_;
273 bool operator>=(const Time& other) const {
274 return us_ >= other.us_;
278 explicit Time(int64_t us) : us_(us) {}
280 // Time in microseconds in UTC.
284 std::ostream& operator<<(std::ostream&, const Time&);
286 inline Time operator+(const TimeDelta& delta, const Time& time) {
291 // -----------------------------------------------------------------------------
294 // This class represents an abstract time that is most of the time incrementing
295 // for use in measuring time durations. It is internally represented in
296 // microseconds. It can not be converted to a human-readable time, but is
297 // guaranteed not to decrease (if the user changes the computer clock,
298 // Time::Now() may actually decrease or jump). But note that TimeTicks may
299 // "stand still", for example if the computer suspended.
301 class TimeTicks final {
303 TimeTicks() : ticks_(0) {}
305 // Platform-dependent tick count representing "right now."
306 // The resolution of this clock is ~1-15ms. Resolution varies depending
307 // on hardware/operating system configuration.
308 // This method never returns a null TimeTicks.
309 static TimeTicks Now();
311 // Returns a platform-dependent high-resolution tick count. Implementation
312 // is hardware dependent and may or may not return sub-millisecond
313 // resolution. THIS CALL IS GENERALLY MUCH MORE EXPENSIVE THAN Now() AND
314 // SHOULD ONLY BE USED WHEN IT IS REALLY NEEDED.
315 // This method never returns a null TimeTicks.
316 static TimeTicks HighResolutionNow();
318 // Returns true if the high-resolution clock is working on this system.
319 static bool IsHighResolutionClockWorking();
321 // Returns Linux kernel timestamp for generating profiler events. This method
322 // returns null TimeTicks if the kernel cannot provide the timestamps (e.g.,
323 // on non-Linux OS or if the kernel module for timestamps is not loaded).
325 static TimeTicks KernelTimestampNow();
326 static bool KernelTimestampAvailable();
328 // Returns true if this object has not been initialized.
329 bool IsNull() const { return ticks_ == 0; }
331 // Converts to/from internal values. The meaning of the "internal value" is
332 // completely up to the implementation, so it should be treated as opaque.
333 static TimeTicks FromInternalValue(int64_t value) {
334 return TimeTicks(value);
336 int64_t ToInternalValue() const {
340 TimeTicks& operator=(const TimeTicks other) {
341 ticks_ = other.ticks_;
345 // Compute the difference between two times.
346 TimeDelta operator-(const TimeTicks other) const {
347 return TimeDelta::FromMicroseconds(ticks_ - other.ticks_);
350 // Modify by some time delta.
351 TimeTicks& operator+=(const TimeDelta& delta) {
352 ticks_ += delta.InMicroseconds();
355 TimeTicks& operator-=(const TimeDelta& delta) {
356 ticks_ -= delta.InMicroseconds();
360 // Return a new TimeTicks modified by some delta.
361 TimeTicks operator+(const TimeDelta& delta) const {
362 return TimeTicks(ticks_ + delta.InMicroseconds());
364 TimeTicks operator-(const TimeDelta& delta) const {
365 return TimeTicks(ticks_ - delta.InMicroseconds());
368 // Comparison operators
369 bool operator==(const TimeTicks& other) const {
370 return ticks_ == other.ticks_;
372 bool operator!=(const TimeTicks& other) const {
373 return ticks_ != other.ticks_;
375 bool operator<(const TimeTicks& other) const {
376 return ticks_ < other.ticks_;
378 bool operator<=(const TimeTicks& other) const {
379 return ticks_ <= other.ticks_;
381 bool operator>(const TimeTicks& other) const {
382 return ticks_ > other.ticks_;
384 bool operator>=(const TimeTicks& other) const {
385 return ticks_ >= other.ticks_;
389 // Please use Now() to create a new object. This is for internal use
390 // and testing. Ticks is in microseconds.
391 explicit TimeTicks(int64_t ticks) : ticks_(ticks) {}
393 // Tick count in microseconds.
397 inline TimeTicks operator+(const TimeDelta& delta, const TimeTicks& ticks) {
398 return ticks + delta;
401 } } // namespace v8::base
403 #endif // V8_BASE_PLATFORM_TIME_H_