2 * Copyright 2008, Google Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following disclaimer
13 * in the documentation and/or other materials provided with the
15 * * Neither the name of Google Inc. nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 // Time represents an absolute point in time, internally represented as
34 // microseconds (s/1,000,000) since a platform-dependent epoch. Each
35 // platform's epoch, along with other system-dependent clock interface
36 // routines, is defined in time_PLATFORM.cc.
38 // TimeDelta represents a duration of time, internally represented in
41 // TimeTicks represents an abstract time that is always incrementing for use
42 // in measuring time durations. It is internally represented in microseconds.
43 // It can not be converted to a human-readable time, but is guaranteed not to
44 // decrease (if the user changes the computer clock, Time::Now() may actually
47 // These classes are represented as only a 64-bit value, so they can be
48 // efficiently passed by value.
50 #ifndef NATIVE_CLIENT_SERVICE_RUNTIME_TIME_H__
51 #define NATIVE_CLIENT_SERVICE_RUNTIME_TIME_H__
56 // This unit test does a lot of manual time manipulation.
57 class PageLoadTrackerUnitTest
;
59 // TimeDelta ------------------------------------------------------------------
63 TimeDelta() : delta_(0) {
66 // Converts units of time to TimeDeltas.
67 static TimeDelta
FromDays(int64 days
);
68 static TimeDelta
FromHours(int64 hours
);
69 static TimeDelta
FromMinutes(int64 minutes
);
70 static TimeDelta
FromSeconds(int64 secs
);
71 static TimeDelta
FromMilliseconds(int64 ms
);
72 static TimeDelta
FromMicroseconds(int64 us
);
74 // Returns the internal numeric value of the TimeDelta object. Please don't
75 // use this and do arithmetic on it, as it is more error prone than using the
76 // provided operators.
77 int64
ToInternalValue() const {
81 // Returns the time delta in some unit. The F versions return a floating
82 // point value, the "regular" versions return a rounded-down value.
84 double InSecondsF() const;
85 int64
InSeconds() const;
86 double InMillisecondsF() const;
87 int64
InMilliseconds() const;
88 int64
InMicroseconds() const;
90 TimeDelta
& operator=(TimeDelta other
) {
91 delta_
= other
.delta_
;
95 // Computations with other deltas.
96 TimeDelta
operator+(TimeDelta other
) const {
97 return TimeDelta(delta_
+ other
.delta_
);
99 TimeDelta
operator-(TimeDelta other
) const {
100 return TimeDelta(delta_
- other
.delta_
);
103 TimeDelta
& operator+=(TimeDelta other
) {
104 delta_
+= other
.delta_
;
107 TimeDelta
& operator-=(TimeDelta other
) {
108 delta_
-= other
.delta_
;
111 TimeDelta
operator-() const {
112 return TimeDelta(-delta_
);
115 // Computations with ints, note that we only allow multiplicative operations
116 // with ints, and additive operations with other deltas.
117 TimeDelta
operator*(int64 a
) const {
118 return TimeDelta(delta_
* a
);
120 TimeDelta
operator/(int64 a
) const {
121 return TimeDelta(delta_
/ a
);
123 TimeDelta
& operator*=(int64 a
) {
127 TimeDelta
& operator/=(int64 a
) {
131 int64
operator/(TimeDelta a
) const {
132 return delta_
/ a
.delta_
;
135 // Defined below because it depends on the definition of the other classes.
136 Time
operator+(Time t
) const;
137 TimeTicks
operator+(TimeTicks t
) const;
139 // Comparison operators.
140 bool operator==(TimeDelta other
) const {
141 return delta_
== other
.delta_
;
143 bool operator!=(TimeDelta other
) const {
144 return delta_
!= other
.delta_
;
146 bool operator<(TimeDelta other
) const {
147 return delta_
< other
.delta_
;
149 bool operator<=(TimeDelta other
) const {
150 return delta_
<= other
.delta_
;
152 bool operator>(TimeDelta other
) const {
153 return delta_
> other
.delta_
;
155 bool operator>=(TimeDelta other
) const {
156 return delta_
>= other
.delta_
;
161 friend class TimeTicks
;
162 friend TimeDelta
operator*(int64 a
, TimeDelta td
);
164 // Constructs a delta given the duration in microseconds. This is private
165 // to avoid confusion by callers with an integer constructor. Use
166 // FromSeconds, FromMilliseconds, etc. instead.
167 explicit TimeDelta(int64 delta_us
) : delta_(delta_us
) {
170 // Delta in microseconds.
174 inline TimeDelta
operator*(int64 a
, TimeDelta td
) {
175 return TimeDelta(a
* td
.delta_
);
178 // Time -----------------------------------------------------------------------
180 // Represents a wall clock time.
183 static const int64 kMillisecondsPerSecond
= 1000;
184 static const int64 kMicrosecondsPerMillisecond
= 1000;
185 static const int64 kNanosecondsPerMicrosecond
= 1000;
186 static const int64 kMicrosecondsPerSecond
= kMicrosecondsPerMillisecond
*
187 kMillisecondsPerSecond
;
188 static const int64 kMicrosecondsPerMinute
= kMicrosecondsPerSecond
* 60;
189 static const int64 kMicrosecondsPerHour
= kMicrosecondsPerMinute
* 60;
190 static const int64 kMicrosecondsPerDay
= kMicrosecondsPerHour
* 24;
191 static const int64 kMicrosecondsPerWeek
= kMicrosecondsPerDay
* 7;
193 // Represents an exploded time that can be formatted nicely. This is kind of
194 // like the Win32 SYSTEMTIME structure or the Unix "struct tm" with a few
195 // additions and changes to prevent errors.
197 int year
; // Four digit year "2007"
198 int month
; // 1-based month (values 1 = January, etc.)
199 int day_of_week
; // 0-based day of week (0 = Sunday, etc.)
200 int day_of_month
; // 1-based day of month (1-31)
201 int hour
; // Hour within the current day (0-23)
202 int minute
; // Minute within the current hour (0-59)
203 int second
; // Second within the current minute (0-59 plus leap
204 // seconds which may take it up to 60).
205 int millisecond
; // Milliseconds within the current second (0-999)
208 // Contains the NULL time. Use Time::Now() to get the current time.
209 explicit Time() : us_(0) {
212 // Returns true if the time object has not been initialized.
213 bool is_null() const {
217 // Returns the current time. Watch out, the system might adjust its clock
218 // in which case time will actually go backwards. We don't guarantee that
219 // times are increasing, or that two calls to Now() won't be the same.
222 // Converts to/from time_t in UTC and a Time class.
223 // TODO this should be removed once everybody starts using the |Time|
225 static Time
FromTimeT(time_t tt
);
226 time_t ToTimeT() const;
228 // Converts time to a double which is the number of seconds since epoch
229 // (Jan 1, 1970). Webkit uses this format to represent time.
230 double ToDoubleT() const;
233 static Time
FromFileTime(FILETIME ft
);
234 FILETIME
ToFileTime() const;
237 // Converts an exploded structure representing either the local time or UTC
238 // into a Time class.
239 static Time
FromUTCExploded(const Exploded
& exploded
) {
240 return FromExploded(false, exploded
);
242 static Time
FromLocalExploded(const Exploded
& exploded
) {
243 return FromExploded(true, exploded
);
246 // Converts an integer value representing Time to a class. This is used
247 // when deserializing a |Time| structure, using a value known to be
248 // compatible. It is not provided as a constructor because the integer type
249 // may be unclear from the perspective of a caller.
250 static Time
FromInternalValue(int64 us
) {
254 // Converts a string representation of time to a Time object.
255 // An example of a time string which is converted is as below:-
256 // "Tue, 15 Nov 1994 12:45:26 GMT". If the timezone is not specified
257 // in the input string, we assume local time.
258 // TODO Move the FromString/FromTimeT/ToTimeT/FromFileTime to
259 // a new time converter class.
260 // NOTE - removed to avoid the need in string suppport
261 // static bool FromString(const wchar_t* time_string, Time* parsed_time);
263 // For serializing, use FromInternalValue to reconstitute. Please don't use
264 // this and do arithmetic on it, as it is more error prone than using the
265 // provided operators.
266 int64
ToInternalValue() const {
270 // Fills the given exploded structure with either the local time or UTC from
271 // this time structure (containing UTC).
272 void UTCExplode(Exploded
* exploded
) const {
273 return Explode(false, exploded
);
275 void LocalExplode(Exploded
* exploded
) const {
276 return Explode(true, exploded
);
279 // Rounds this time down to the nearest day in local time. It will represent
280 // midnight on that day.
281 Time
LocalMidnight() const;
283 Time
& operator=(Time other
) {
288 // Compute the difference between two times.
289 TimeDelta
operator-(Time other
) const {
290 return TimeDelta(us_
- other
.us_
);
293 // Modify by some time delta.
294 Time
& operator+=(TimeDelta delta
) {
298 Time
& operator-=(TimeDelta delta
) {
303 // Return a new time modified by some delta.
304 Time
operator+(TimeDelta delta
) const {
305 return us_
+ delta
.delta_
;
307 Time
operator-(TimeDelta delta
) const {
308 return us_
- delta
.delta_
;
311 // Comparison operators
312 bool operator==(Time other
) const {
313 return us_
== other
.us_
;
315 bool operator!=(Time other
) const {
316 return us_
!= other
.us_
;
318 bool operator<(Time other
) const {
319 return us_
< other
.us_
;
321 bool operator<=(Time other
) const {
322 return us_
<= other
.us_
;
324 bool operator>(Time other
) const {
325 return us_
> other
.us_
;
327 bool operator>=(Time other
) const {
328 return us_
>= other
.us_
;
332 friend class TimeDelta
;
334 // Platform-dependent wall clock interface
335 static int64
CurrentWallclockMicroseconds();
337 // Initialize or resynchronize the clock.
338 static void InitializeClock();
340 // Explodes the given time to either local time |is_local = true| or UTC
341 // |is_local = false|.
342 void Explode(bool is_local
, Exploded
* exploded
) const;
344 // Unexplodes a given time assuming the source is either local time
345 // |is_local = true| or UTC |is_local = false|.
346 static Time
FromExploded(bool is_local
, const Exploded
& exploded
);
348 Time(int64 us
) : us_(us
) {
351 // The representation of Jan 1, 1970 UTC in microseconds since the
352 // platform-dependent epoch.
353 static const int64 kTimeTToMicrosecondsOffset
;
355 // Time in microseconds in UTC.
358 // The initial time sampled via this API.
359 static int64 initial_time_
;
361 // The initial clock counter sampled via this API.
362 static TimeTicks initial_ticks_
;
365 inline Time
TimeDelta::operator+(Time t
) const {
366 return Time(t
.us_
+ delta_
);
369 // TimeTicks ------------------------------------------------------------------
373 TimeTicks() : ticks_(0) {
376 // TODO: made this constructor public, but might undo this change
377 // when we cleanup the time-handling code.
378 explicit TimeTicks(int64 ticks
) : ticks_(ticks
) {
381 // Platform-dependent tick count representing "right now."
382 // The resolution of this clock is ~1-5ms. Resolution varies depending
383 // on hardware/operating system configuration.
384 static TimeTicks
Now();
386 // Returns a platform-dependent high-resolution tick count. IT IS BROKEN ON
387 // SOME HARDWARE and is designed to be used for profiling and perf testing
388 // only (see the impl for more information).
389 static TimeTicks
UnreliableHighResNow();
392 // Returns true if this object has not been initialized.
393 bool is_null() const {
397 TimeTicks
& operator=(TimeTicks other
) {
398 ticks_
= other
.ticks_
;
402 // Compute the difference between two times.
403 TimeDelta
operator-(TimeTicks other
) const {
404 return TimeDelta(ticks_
- other
.ticks_
);
407 // Modify by some time delta.
408 TimeTicks
& operator+=(TimeDelta delta
) {
409 ticks_
+= delta
.delta_
;
412 TimeTicks
& operator-=(TimeDelta delta
) {
413 ticks_
-= delta
.delta_
;
417 // Return a new TimeTicks modified by some delta.
418 TimeTicks
operator+(TimeDelta delta
) const {
419 return TimeTicks(ticks_
+ delta
.delta_
);
421 TimeTicks
operator-(TimeDelta delta
) const {
422 return TimeTicks(ticks_
- delta
.delta_
);
425 // Comparison operators
426 bool operator==(TimeTicks other
) const {
427 return ticks_
== other
.ticks_
;
429 bool operator!=(TimeTicks other
) const {
430 return ticks_
!= other
.ticks_
;
432 bool operator<(TimeTicks other
) const {
433 return ticks_
< other
.ticks_
;
435 bool operator<=(TimeTicks other
) const {
436 return ticks_
<= other
.ticks_
;
438 bool operator>(TimeTicks other
) const {
439 return ticks_
> other
.ticks_
;
441 bool operator>=(TimeTicks other
) const {
442 return ticks_
>= other
.ticks_
;
444 #if NACL_LINUX || NACL_OSX
445 void InitTimespec(struct timespec
*ts
) const;
448 friend class TimeDelta
;
449 friend class PageLoadTrackerUnitTest
;
453 // Tick count in microseconds.
457 // The function to use for counting ticks.
458 typedef int (__stdcall
*TickFunction
)(void);
459 static TickFunction tick_function_
;
463 inline TimeTicks
TimeDelta::operator+(TimeTicks t
) const {
464 return TimeTicks(t
.ticks_
+ delta_
);
467 #endif // NATIVE_CLIENT_SERVICE_RUNTIME_TIME_H__