1dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org// Copyright 2013 the V8 project authors. All rights reserved.
23484964a86451e86dcf04be9bd8c0d76ee04f081rossberg@chromium.org// Use of this source code is governed by a BSD-style license that can be
33484964a86451e86dcf04be9bd8c0d76ee04f081rossberg@chromium.org// found in the LICENSE file.
4dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org
55de0074a922429f5e0ec2cf140c2d2989bf88140yangguo@chromium.org#ifndef V8_BASE_PLATFORM_TIME_H_
65de0074a922429f5e0ec2cf140c2d2989bf88140yangguo@chromium.org#define V8_BASE_PLATFORM_TIME_H_
7dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org
8e31286d471eb2e656a1809383fa16b76053dd673machenbach@chromium.org#include <time.h>
9dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org#include <limits>
10dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org
11248dd43badb99ffce44eae2d767cda3cefaad521machenbach@chromium.org#include "src/base/macros.h"
12dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org
13dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org// Forward declarations.
14dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.orgextern "C" {
15dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.orgstruct _FILETIME;
16e900018c7a2a695fde788911564da37535c7e736mstarzinger@chromium.orgstruct mach_timespec;
17e900018c7a2a695fde788911564da37535c7e736mstarzinger@chromium.orgstruct timespec;
18dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.orgstruct timeval;
19dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org}
20dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org
21dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.orgnamespace v8 {
225de0074a922429f5e0ec2cf140c2d2989bf88140yangguo@chromium.orgnamespace base {
23dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org
24dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.orgclass Time;
25dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.orgclass TimeTicks;
26dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org
27dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org// -----------------------------------------------------------------------------
28dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org// TimeDelta
29dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org//
30dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org// This class represents a duration of time, internally represented in
31dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org// microseonds.
32dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org
33ada3a6017e603965f87fa34f6e2fa60379e8d697machenbach@chromium.orgclass TimeDelta FINAL {
34dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org public:
35dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  TimeDelta() : delta_(0) {}
36dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org
37dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  // Converts units of time to TimeDeltas.
38dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  static TimeDelta FromDays(int days);
39dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  static TimeDelta FromHours(int hours);
40dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  static TimeDelta FromMinutes(int minutes);
41dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  static TimeDelta FromSeconds(int64_t seconds);
42dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  static TimeDelta FromMilliseconds(int64_t milliseconds);
43dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  static TimeDelta FromMicroseconds(int64_t microseconds) {
44dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org    return TimeDelta(microseconds);
45dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  }
46dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  static TimeDelta FromNanoseconds(int64_t nanoseconds);
47dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org
48dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  // Returns the time delta in some unit. The F versions return a floating
49dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  // point value, the "regular" versions return a rounded-down value.
50dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  //
51dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  // InMillisecondsRoundedUp() instead returns an integer that is rounded up
52dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  // to the next full millisecond.
53dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  int InDays() const;
54dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  int InHours() const;
55dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  int InMinutes() const;
56dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  double InSecondsF() const;
57dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  int64_t InSeconds() const;
58dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  double InMillisecondsF() const;
59dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  int64_t InMilliseconds() const;
60dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  int64_t InMillisecondsRoundedUp() const;
61dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  int64_t InMicroseconds() const { return delta_; }
62dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  int64_t InNanoseconds() const;
63dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org
64e900018c7a2a695fde788911564da37535c7e736mstarzinger@chromium.org  // Converts to/from Mach time specs.
65e900018c7a2a695fde788911564da37535c7e736mstarzinger@chromium.org  static TimeDelta FromMachTimespec(struct mach_timespec ts);
66e900018c7a2a695fde788911564da37535c7e736mstarzinger@chromium.org  struct mach_timespec ToMachTimespec() const;
67e900018c7a2a695fde788911564da37535c7e736mstarzinger@chromium.org
68e900018c7a2a695fde788911564da37535c7e736mstarzinger@chromium.org  // Converts to/from POSIX time specs.
69e900018c7a2a695fde788911564da37535c7e736mstarzinger@chromium.org  static TimeDelta FromTimespec(struct timespec ts);
70e900018c7a2a695fde788911564da37535c7e736mstarzinger@chromium.org  struct timespec ToTimespec() const;
71e900018c7a2a695fde788911564da37535c7e736mstarzinger@chromium.org
72dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  TimeDelta& operator=(const TimeDelta& other) {
73dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org    delta_ = other.delta_;
74dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org    return *this;
75dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  }
76dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org
77dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  // Computations with other deltas.
78dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  TimeDelta operator+(const TimeDelta& other) const {
79dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org    return TimeDelta(delta_ + other.delta_);
80dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  }
81dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  TimeDelta operator-(const TimeDelta& other) const {
82dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org    return TimeDelta(delta_ - other.delta_);
83dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  }
84dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org
85dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  TimeDelta& operator+=(const TimeDelta& other) {
86dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org    delta_ += other.delta_;
87dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org    return *this;
88dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  }
89dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  TimeDelta& operator-=(const TimeDelta& other) {
90dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org    delta_ -= other.delta_;
91dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org    return *this;
92dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  }
93dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  TimeDelta operator-() const {
94dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org    return TimeDelta(-delta_);
95dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  }
96dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org
97dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  double TimesOf(const TimeDelta& other) const {
98dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org    return static_cast<double>(delta_) / static_cast<double>(other.delta_);
99dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  }
100dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  double PercentOf(const TimeDelta& other) const {
101dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org    return TimesOf(other) * 100.0;
102dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  }
103dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org
104dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  // Computations with ints, note that we only allow multiplicative operations
105dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  // with ints, and additive operations with other deltas.
106dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  TimeDelta operator*(int64_t a) const {
107dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org    return TimeDelta(delta_ * a);
108dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  }
109dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  TimeDelta operator/(int64_t a) const {
110dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org    return TimeDelta(delta_ / a);
111dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  }
112dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  TimeDelta& operator*=(int64_t a) {
113dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org    delta_ *= a;
114dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org    return *this;
115dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  }
116dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  TimeDelta& operator/=(int64_t a) {
117dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org    delta_ /= a;
118dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org    return *this;
119dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  }
120dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  int64_t operator/(const TimeDelta& other) const {
121dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org    return delta_ / other.delta_;
122dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  }
123dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org
124dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  // Comparison operators.
125dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  bool operator==(const TimeDelta& other) const {
126dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org    return delta_ == other.delta_;
127dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  }
128dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  bool operator!=(const TimeDelta& other) const {
129dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org    return delta_ != other.delta_;
130dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  }
131dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  bool operator<(const TimeDelta& other) const {
132dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org    return delta_ < other.delta_;
133dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  }
134dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  bool operator<=(const TimeDelta& other) const {
135dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org    return delta_ <= other.delta_;
136dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  }
137dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  bool operator>(const TimeDelta& other) const {
138dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org    return delta_ > other.delta_;
139dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  }
140dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  bool operator>=(const TimeDelta& other) const {
141dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org    return delta_ >= other.delta_;
142dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  }
143dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org
144dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org private:
145dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  // Constructs a delta given the duration in microseconds. This is private
146dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  // to avoid confusion by callers with an integer constructor. Use
147dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  // FromSeconds, FromMilliseconds, etc. instead.
148dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  explicit TimeDelta(int64_t delta) : delta_(delta) {}
149dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org
150dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  // Delta in microseconds.
151dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  int64_t delta_;
152dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org};
153dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org
154dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org
155dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org// -----------------------------------------------------------------------------
156dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org// Time
157dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org//
158dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org// This class represents an absolute point in time, internally represented as
159dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org// microseconds (s/1,000,000) since 00:00:00 UTC, January 1, 1970.
160dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org
161ada3a6017e603965f87fa34f6e2fa60379e8d697machenbach@chromium.orgclass Time FINAL {
162dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org public:
163dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  static const int64_t kMillisecondsPerSecond = 1000;
164dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  static const int64_t kMicrosecondsPerMillisecond = 1000;
165dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  static const int64_t kMicrosecondsPerSecond = kMicrosecondsPerMillisecond *
166dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org                                                kMillisecondsPerSecond;
167dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  static const int64_t kMicrosecondsPerMinute = kMicrosecondsPerSecond * 60;
168dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  static const int64_t kMicrosecondsPerHour = kMicrosecondsPerMinute * 60;
169dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  static const int64_t kMicrosecondsPerDay = kMicrosecondsPerHour * 24;
170dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  static const int64_t kMicrosecondsPerWeek = kMicrosecondsPerDay * 7;
171dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  static const int64_t kNanosecondsPerMicrosecond = 1000;
172dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  static const int64_t kNanosecondsPerSecond = kNanosecondsPerMicrosecond *
173dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org                                               kMicrosecondsPerSecond;
174dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org
175dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  // Contains the NULL time. Use Time::Now() to get the current time.
176dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  Time() : us_(0) {}
177dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org
178dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  // Returns true if the time object has not been initialized.
179dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  bool IsNull() const { return us_ == 0; }
180dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org
181dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  // Returns true if the time object is the maximum time.
182dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  bool IsMax() const { return us_ == std::numeric_limits<int64_t>::max(); }
183dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org
184dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  // Returns the current time. Watch out, the system might adjust its clock
185dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  // in which case time will actually go backwards. We don't guarantee that
186dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  // times are increasing, or that two calls to Now() won't be the same.
187dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  static Time Now();
188dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org
189dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  // Returns the current time. Same as Now() except that this function always
190dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  // uses system time so that there are no discrepancies between the returned
191dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  // time and system time even on virtual environments including our test bot.
192dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  // For timing sensitive unittests, this function should be used.
193dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  static Time NowFromSystemTime();
194dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org
195dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  // Returns the time for epoch in Unix-like system (Jan 1, 1970).
196dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  static Time UnixEpoch() { return Time(0); }
197dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org
198dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  // Returns the maximum time, which should be greater than any reasonable time
199dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  // with which we might compare it.
200dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  static Time Max() { return Time(std::numeric_limits<int64_t>::max()); }
201dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org
202c5d4971574b7a205fa0e788d8121dc79485e5e67hpayer@chromium.org  // Converts to/from internal values. The meaning of the "internal value" is
203c5d4971574b7a205fa0e788d8121dc79485e5e67hpayer@chromium.org  // completely up to the implementation, so it should be treated as opaque.
204c5d4971574b7a205fa0e788d8121dc79485e5e67hpayer@chromium.org  static Time FromInternalValue(int64_t value) {
205c5d4971574b7a205fa0e788d8121dc79485e5e67hpayer@chromium.org    return Time(value);
206c5d4971574b7a205fa0e788d8121dc79485e5e67hpayer@chromium.org  }
207c5d4971574b7a205fa0e788d8121dc79485e5e67hpayer@chromium.org  int64_t ToInternalValue() const {
208c5d4971574b7a205fa0e788d8121dc79485e5e67hpayer@chromium.org    return us_;
209c5d4971574b7a205fa0e788d8121dc79485e5e67hpayer@chromium.org  }
210c5d4971574b7a205fa0e788d8121dc79485e5e67hpayer@chromium.org
211e900018c7a2a695fde788911564da37535c7e736mstarzinger@chromium.org  // Converts to/from POSIX time specs.
212e900018c7a2a695fde788911564da37535c7e736mstarzinger@chromium.org  static Time FromTimespec(struct timespec ts);
213e900018c7a2a695fde788911564da37535c7e736mstarzinger@chromium.org  struct timespec ToTimespec() const;
214e900018c7a2a695fde788911564da37535c7e736mstarzinger@chromium.org
215dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  // Converts to/from POSIX time values.
216dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  static Time FromTimeval(struct timeval tv);
217dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  struct timeval ToTimeval() const;
218dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org
219dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  // Converts to/from Windows file times.
220dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  static Time FromFiletime(struct _FILETIME ft);
221dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  struct _FILETIME ToFiletime() const;
222dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org
223dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  // Converts to/from the Javascript convention for times, a number of
224dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  // milliseconds since the epoch:
225dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  static Time FromJsTime(double ms_since_epoch);
226dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  double ToJsTime() const;
227dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org
228dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  Time& operator=(const Time& other) {
229dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org    us_ = other.us_;
230dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org    return *this;
231dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  }
232dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org
233dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  // Compute the difference between two times.
234dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  TimeDelta operator-(const Time& other) const {
235dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org    return TimeDelta::FromMicroseconds(us_ - other.us_);
236dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  }
237dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org
238dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  // Modify by some time delta.
239dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  Time& operator+=(const TimeDelta& delta) {
240dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org    us_ += delta.InMicroseconds();
241dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org    return *this;
242dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  }
243dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  Time& operator-=(const TimeDelta& delta) {
244dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org    us_ -= delta.InMicroseconds();
245dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org    return *this;
246dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  }
247dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org
248dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  // Return a new time modified by some delta.
249dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  Time operator+(const TimeDelta& delta) const {
250dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org    return Time(us_ + delta.InMicroseconds());
251dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  }
252dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  Time operator-(const TimeDelta& delta) const {
253dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org    return Time(us_ - delta.InMicroseconds());
254dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  }
255dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org
256dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  // Comparison operators
257dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  bool operator==(const Time& other) const {
258dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org    return us_ == other.us_;
259dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  }
260dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  bool operator!=(const Time& other) const {
261dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org    return us_ != other.us_;
262dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  }
263dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  bool operator<(const Time& other) const {
264dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org    return us_ < other.us_;
265dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  }
266dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  bool operator<=(const Time& other) const {
267dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org    return us_ <= other.us_;
268dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  }
269dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  bool operator>(const Time& other) const {
270dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org    return us_ > other.us_;
271dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  }
272dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  bool operator>=(const Time& other) const {
273dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org    return us_ >= other.us_;
274dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  }
275dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org
276dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org private:
277dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  explicit Time(int64_t us) : us_(us) {}
278dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org
279dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  // Time in microseconds in UTC.
280dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  int64_t us_;
281dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org};
282dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org
283dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.orginline Time operator+(const TimeDelta& delta, const Time& time) {
284dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  return time + delta;
285dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org}
286dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org
287dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org
288dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org// -----------------------------------------------------------------------------
289dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org// TimeTicks
290dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org//
291dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org// This class represents an abstract time that is most of the time incrementing
292dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org// for use in measuring time durations. It is internally represented in
293dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org// microseconds.  It can not be converted to a human-readable time, but is
294dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org// guaranteed not to decrease (if the user changes the computer clock,
295dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org// Time::Now() may actually decrease or jump).  But note that TimeTicks may
296dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org// "stand still", for example if the computer suspended.
297dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org
298ada3a6017e603965f87fa34f6e2fa60379e8d697machenbach@chromium.orgclass TimeTicks FINAL {
299dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org public:
300dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  TimeTicks() : ticks_(0) {}
301dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org
302dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  // Platform-dependent tick count representing "right now."
303dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  // The resolution of this clock is ~1-15ms.  Resolution varies depending
304dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  // on hardware/operating system configuration.
305dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  // This method never returns a null TimeTicks.
306dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  static TimeTicks Now();
307dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org
308dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  // Returns a platform-dependent high-resolution tick count. Implementation
309dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  // is hardware dependent and may or may not return sub-millisecond
310dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  // resolution.  THIS CALL IS GENERALLY MUCH MORE EXPENSIVE THAN Now() AND
311dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  // SHOULD ONLY BE USED WHEN IT IS REALLY NEEDED.
312dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  // This method never returns a null TimeTicks.
313d8a3a149cb9dac7437e264a2fe50f680418c3a45jkummerow@chromium.org  static TimeTicks HighResolutionNow();
314d8a3a149cb9dac7437e264a2fe50f680418c3a45jkummerow@chromium.org
315d8a3a149cb9dac7437e264a2fe50f680418c3a45jkummerow@chromium.org  // Returns true if the high-resolution clock is working on this system.
316d8a3a149cb9dac7437e264a2fe50f680418c3a45jkummerow@chromium.org  static bool IsHighResolutionClockWorking();
317dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org
318d0bddc653152f270a27fe32d5d7b0f5c0fa3b00cmachenbach@chromium.org  // Returns Linux kernel timestamp for generating profiler events. This method
319d0bddc653152f270a27fe32d5d7b0f5c0fa3b00cmachenbach@chromium.org  // returns null TimeTicks if the kernel cannot provide the timestamps (e.g.,
320d0bddc653152f270a27fe32d5d7b0f5c0fa3b00cmachenbach@chromium.org  // on non-Linux OS or if the kernel module for timestamps is not loaded).
321d0bddc653152f270a27fe32d5d7b0f5c0fa3b00cmachenbach@chromium.org
322d0bddc653152f270a27fe32d5d7b0f5c0fa3b00cmachenbach@chromium.org  static TimeTicks KernelTimestampNow();
323d0bddc653152f270a27fe32d5d7b0f5c0fa3b00cmachenbach@chromium.org  static bool KernelTimestampAvailable();
324d0bddc653152f270a27fe32d5d7b0f5c0fa3b00cmachenbach@chromium.org
325dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  // Returns true if this object has not been initialized.
326dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  bool IsNull() const { return ticks_ == 0; }
327dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org
328c5d4971574b7a205fa0e788d8121dc79485e5e67hpayer@chromium.org  // Converts to/from internal values. The meaning of the "internal value" is
329c5d4971574b7a205fa0e788d8121dc79485e5e67hpayer@chromium.org  // completely up to the implementation, so it should be treated as opaque.
330c5d4971574b7a205fa0e788d8121dc79485e5e67hpayer@chromium.org  static TimeTicks FromInternalValue(int64_t value) {
331c5d4971574b7a205fa0e788d8121dc79485e5e67hpayer@chromium.org    return TimeTicks(value);
332c5d4971574b7a205fa0e788d8121dc79485e5e67hpayer@chromium.org  }
333c5d4971574b7a205fa0e788d8121dc79485e5e67hpayer@chromium.org  int64_t ToInternalValue() const {
334c5d4971574b7a205fa0e788d8121dc79485e5e67hpayer@chromium.org    return ticks_;
335c5d4971574b7a205fa0e788d8121dc79485e5e67hpayer@chromium.org  }
336c5d4971574b7a205fa0e788d8121dc79485e5e67hpayer@chromium.org
337dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  TimeTicks& operator=(const TimeTicks other) {
338dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org    ticks_ = other.ticks_;
339dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org    return *this;
340dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  }
341dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org
342dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  // Compute the difference between two times.
343dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  TimeDelta operator-(const TimeTicks other) const {
344dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org    return TimeDelta::FromMicroseconds(ticks_ - other.ticks_);
345dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  }
346dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org
347dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  // Modify by some time delta.
348dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  TimeTicks& operator+=(const TimeDelta& delta) {
349dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org    ticks_ += delta.InMicroseconds();
350dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org    return *this;
351dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  }
352dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  TimeTicks& operator-=(const TimeDelta& delta) {
353dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org    ticks_ -= delta.InMicroseconds();
354dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org    return *this;
355dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  }
356dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org
357dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  // Return a new TimeTicks modified by some delta.
358dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  TimeTicks operator+(const TimeDelta& delta) const {
359dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org    return TimeTicks(ticks_ + delta.InMicroseconds());
360dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  }
361dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  TimeTicks operator-(const TimeDelta& delta) const {
362dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org    return TimeTicks(ticks_ - delta.InMicroseconds());
363dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  }
364dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org
365dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  // Comparison operators
366dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  bool operator==(const TimeTicks& other) const {
367dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org    return ticks_ == other.ticks_;
368dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  }
369dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  bool operator!=(const TimeTicks& other) const {
370dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org    return ticks_ != other.ticks_;
371dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  }
372dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  bool operator<(const TimeTicks& other) const {
373dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org    return ticks_ < other.ticks_;
374dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  }
375dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  bool operator<=(const TimeTicks& other) const {
376dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org    return ticks_ <= other.ticks_;
377dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  }
378dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  bool operator>(const TimeTicks& other) const {
379dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org    return ticks_ > other.ticks_;
380dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  }
381dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  bool operator>=(const TimeTicks& other) const {
382dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org    return ticks_ >= other.ticks_;
383dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  }
384dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org
385dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org private:
386dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  // Please use Now() to create a new object. This is for internal use
387dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  // and testing. Ticks is in microseconds.
388dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  explicit TimeTicks(int64_t ticks) : ticks_(ticks) {}
389dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org
390dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  // Tick count in microseconds.
391dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  int64_t ticks_;
392dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org};
393dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org
394dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.orginline TimeTicks operator+(const TimeDelta& delta, const TimeTicks& ticks) {
395dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org  return ticks + delta;
396dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org}
397dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org
3985de0074a922429f5e0ec2cf140c2d2989bf88140yangguo@chromium.org} }  // namespace v8::base
399dc94e19484d1700cb0ec22365444223e49a3ac1ejkummerow@chromium.org
4005de0074a922429f5e0ec2cf140c2d2989bf88140yangguo@chromium.org#endif  // V8_BASE_PLATFORM_TIME_H_
401