~alinuxninja/nginx-edge/trunk

« back to all changes in this revision

Viewing changes to debian/modules/ngx_pagespeed/psol/include/third_party/chromium/src/base/time/time.h

  • Committer: Vivian
  • Date: 2015-12-04 18:20:11 UTC
  • Revision ID: git-v1:a36f2bc32e884f7473b3a47040e5411306144d7d
* Do not extract psol.tar.gz

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// Copyright (c) 2012 The Chromium 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.
4
 
 
5
 
// Time represents an absolute point in time, internally represented as
6
 
// microseconds (s/1,000,000) since the Windows epoch (1601-01-01 00:00:00 UTC)
7
 
// (See http://crbug.com/14734).  System-dependent clock interface routines are
8
 
// defined in time_PLATFORM.cc.
9
 
//
10
 
// TimeDelta represents a duration of time, internally represented in
11
 
// microseconds.
12
 
//
13
 
// TimeTicks represents an abstract time that is most of the time incrementing
14
 
// for use in measuring time durations. It is internally represented in
15
 
// microseconds.  It can not be converted to a human-readable time, but is
16
 
// guaranteed not to decrease (if the user changes the computer clock,
17
 
// Time::Now() may actually decrease or jump).  But note that TimeTicks may
18
 
// "stand still", for example if the computer suspended.
19
 
//
20
 
// These classes are represented as only a 64-bit value, so they can be
21
 
// efficiently passed by value.
22
 
 
23
 
#ifndef BASE_TIME_TIME_H_
24
 
#define BASE_TIME_TIME_H_
25
 
 
26
 
#include <time.h>
27
 
 
28
 
#include "base/base_export.h"
29
 
#include "base/basictypes.h"
30
 
#include "build/build_config.h"
31
 
 
32
 
#if defined(OS_MACOSX)
33
 
#include <CoreFoundation/CoreFoundation.h>
34
 
// Avoid Mac system header macro leak.
35
 
#undef TYPE_BOOL
36
 
#endif
37
 
 
38
 
#if defined(OS_POSIX)
39
 
#include <unistd.h>
40
 
#include <sys/time.h>
41
 
#endif
42
 
 
43
 
#if defined(OS_WIN)
44
 
// For FILETIME in FromFileTime, until it moves to a new converter class.
45
 
// See TODO(iyengar) below.
46
 
#include <windows.h>
47
 
#endif
48
 
 
49
 
#include <limits>
50
 
 
51
 
namespace base {
52
 
 
53
 
class Time;
54
 
class TimeTicks;
55
 
 
56
 
// TimeDelta ------------------------------------------------------------------
57
 
 
58
 
class BASE_EXPORT TimeDelta {
59
 
 public:
60
 
  TimeDelta() : delta_(0) {
61
 
  }
62
 
 
63
 
  // Converts units of time to TimeDeltas.
64
 
  static TimeDelta FromDays(int days);
65
 
  static TimeDelta FromHours(int hours);
66
 
  static TimeDelta FromMinutes(int minutes);
67
 
  static TimeDelta FromSeconds(int64 secs);
68
 
  static TimeDelta FromMilliseconds(int64 ms);
69
 
  static TimeDelta FromMicroseconds(int64 us);
70
 
#if defined(OS_WIN)
71
 
  static TimeDelta FromQPCValue(LONGLONG qpc_value);
72
 
#endif
73
 
 
74
 
  // Converts an integer value representing TimeDelta to a class. This is used
75
 
  // when deserializing a |TimeDelta| structure, using a value known to be
76
 
  // compatible. It is not provided as a constructor because the integer type
77
 
  // may be unclear from the perspective of a caller.
78
 
  static TimeDelta FromInternalValue(int64 delta) {
79
 
    return TimeDelta(delta);
80
 
  }
81
 
 
82
 
  // Returns the maximum time delta, which should be greater than any reasonable
83
 
  // time delta we might compare it to. Adding or subtracting the maximum time
84
 
  // delta to a time or another time delta has an undefined result.
85
 
  static TimeDelta Max();
86
 
 
87
 
  // Returns the internal numeric value of the TimeDelta object. Please don't
88
 
  // use this and do arithmetic on it, as it is more error prone than using the
89
 
  // provided operators.
90
 
  // For serializing, use FromInternalValue to reconstitute.
91
 
  int64 ToInternalValue() const {
92
 
    return delta_;
93
 
  }
94
 
 
95
 
  // Returns true if the time delta is the maximum time delta.
96
 
  bool is_max() const {
97
 
    return delta_ == std::numeric_limits<int64>::max();
98
 
  }
99
 
 
100
 
#if defined(OS_POSIX)
101
 
  struct timespec ToTimeSpec() const;
102
 
#endif
103
 
 
104
 
  // Returns the time delta in some unit. The F versions return a floating
105
 
  // point value, the "regular" versions return a rounded-down value.
106
 
  //
107
 
  // InMillisecondsRoundedUp() instead returns an integer that is rounded up
108
 
  // to the next full millisecond.
109
 
  int InDays() const;
110
 
  int InHours() const;
111
 
  int InMinutes() const;
112
 
  double InSecondsF() const;
113
 
  int64 InSeconds() const;
114
 
  double InMillisecondsF() const;
115
 
  int64 InMilliseconds() const;
116
 
  int64 InMillisecondsRoundedUp() const;
117
 
  int64 InMicroseconds() const;
118
 
 
119
 
  TimeDelta& operator=(TimeDelta other) {
120
 
    delta_ = other.delta_;
121
 
    return *this;
122
 
  }
123
 
 
124
 
  // Computations with other deltas.
125
 
  TimeDelta operator+(TimeDelta other) const {
126
 
    return TimeDelta(delta_ + other.delta_);
127
 
  }
128
 
  TimeDelta operator-(TimeDelta other) const {
129
 
    return TimeDelta(delta_ - other.delta_);
130
 
  }
131
 
 
132
 
  TimeDelta& operator+=(TimeDelta other) {
133
 
    delta_ += other.delta_;
134
 
    return *this;
135
 
  }
136
 
  TimeDelta& operator-=(TimeDelta other) {
137
 
    delta_ -= other.delta_;
138
 
    return *this;
139
 
  }
140
 
  TimeDelta operator-() const {
141
 
    return TimeDelta(-delta_);
142
 
  }
143
 
 
144
 
  // Computations with ints, note that we only allow multiplicative operations
145
 
  // with ints, and additive operations with other deltas.
146
 
  TimeDelta operator*(int64 a) const {
147
 
    return TimeDelta(delta_ * a);
148
 
  }
149
 
  TimeDelta operator/(int64 a) const {
150
 
    return TimeDelta(delta_ / a);
151
 
  }
152
 
  TimeDelta& operator*=(int64 a) {
153
 
    delta_ *= a;
154
 
    return *this;
155
 
  }
156
 
  TimeDelta& operator/=(int64 a) {
157
 
    delta_ /= a;
158
 
    return *this;
159
 
  }
160
 
  int64 operator/(TimeDelta a) const {
161
 
    return delta_ / a.delta_;
162
 
  }
163
 
 
164
 
  // Defined below because it depends on the definition of the other classes.
165
 
  Time operator+(Time t) const;
166
 
  TimeTicks operator+(TimeTicks t) const;
167
 
 
168
 
  // Comparison operators.
169
 
  bool operator==(TimeDelta other) const {
170
 
    return delta_ == other.delta_;
171
 
  }
172
 
  bool operator!=(TimeDelta other) const {
173
 
    return delta_ != other.delta_;
174
 
  }
175
 
  bool operator<(TimeDelta other) const {
176
 
    return delta_ < other.delta_;
177
 
  }
178
 
  bool operator<=(TimeDelta other) const {
179
 
    return delta_ <= other.delta_;
180
 
  }
181
 
  bool operator>(TimeDelta other) const {
182
 
    return delta_ > other.delta_;
183
 
  }
184
 
  bool operator>=(TimeDelta other) const {
185
 
    return delta_ >= other.delta_;
186
 
  }
187
 
 
188
 
 private:
189
 
  friend class Time;
190
 
  friend class TimeTicks;
191
 
  friend TimeDelta operator*(int64 a, TimeDelta td);
192
 
 
193
 
  // Constructs a delta given the duration in microseconds. This is private
194
 
  // to avoid confusion by callers with an integer constructor. Use
195
 
  // FromSeconds, FromMilliseconds, etc. instead.
196
 
  explicit TimeDelta(int64 delta_us) : delta_(delta_us) {
197
 
  }
198
 
 
199
 
  // Delta in microseconds.
200
 
  int64 delta_;
201
 
};
202
 
 
203
 
inline TimeDelta operator*(int64 a, TimeDelta td) {
204
 
  return TimeDelta(a * td.delta_);
205
 
}
206
 
 
207
 
// Time -----------------------------------------------------------------------
208
 
 
209
 
// Represents a wall clock time.
210
 
class BASE_EXPORT Time {
211
 
 public:
212
 
  static const int64 kMillisecondsPerSecond = 1000;
213
 
  static const int64 kMicrosecondsPerMillisecond = 1000;
214
 
  static const int64 kMicrosecondsPerSecond = kMicrosecondsPerMillisecond *
215
 
                                              kMillisecondsPerSecond;
216
 
  static const int64 kMicrosecondsPerMinute = kMicrosecondsPerSecond * 60;
217
 
  static const int64 kMicrosecondsPerHour = kMicrosecondsPerMinute * 60;
218
 
  static const int64 kMicrosecondsPerDay = kMicrosecondsPerHour * 24;
219
 
  static const int64 kMicrosecondsPerWeek = kMicrosecondsPerDay * 7;
220
 
  static const int64 kNanosecondsPerMicrosecond = 1000;
221
 
  static const int64 kNanosecondsPerSecond = kNanosecondsPerMicrosecond *
222
 
                                             kMicrosecondsPerSecond;
223
 
 
224
 
#if !defined(OS_WIN)
225
 
  // On Mac & Linux, this value is the delta from the Windows epoch of 1601 to
226
 
  // the Posix delta of 1970. This is used for migrating between the old
227
 
  // 1970-based epochs to the new 1601-based ones. It should be removed from
228
 
  // this global header and put in the platform-specific ones when we remove the
229
 
  // migration code.
230
 
  static const int64 kWindowsEpochDeltaMicroseconds;
231
 
#endif
232
 
 
233
 
  // Represents an exploded time that can be formatted nicely. This is kind of
234
 
  // like the Win32 SYSTEMTIME structure or the Unix "struct tm" with a few
235
 
  // additions and changes to prevent errors.
236
 
  struct BASE_EXPORT Exploded {
237
 
    int year;          // Four digit year "2007"
238
 
    int month;         // 1-based month (values 1 = January, etc.)
239
 
    int day_of_week;   // 0-based day of week (0 = Sunday, etc.)
240
 
    int day_of_month;  // 1-based day of month (1-31)
241
 
    int hour;          // Hour within the current day (0-23)
242
 
    int minute;        // Minute within the current hour (0-59)
243
 
    int second;        // Second within the current minute (0-59 plus leap
244
 
                       //   seconds which may take it up to 60).
245
 
    int millisecond;   // Milliseconds within the current second (0-999)
246
 
 
247
 
    // A cursory test for whether the data members are within their
248
 
    // respective ranges. A 'true' return value does not guarantee the
249
 
    // Exploded value can be successfully converted to a Time value.
250
 
    bool HasValidValues() const;
251
 
  };
252
 
 
253
 
  // Contains the NULL time. Use Time::Now() to get the current time.
254
 
  Time() : us_(0) {
255
 
  }
256
 
 
257
 
  // Returns true if the time object has not been initialized.
258
 
  bool is_null() const {
259
 
    return us_ == 0;
260
 
  }
261
 
 
262
 
  // Returns true if the time object is the maximum time.
263
 
  bool is_max() const {
264
 
    return us_ == std::numeric_limits<int64>::max();
265
 
  }
266
 
 
267
 
  // Returns the time for epoch in Unix-like system (Jan 1, 1970).
268
 
  static Time UnixEpoch();
269
 
 
270
 
  // Returns the current time. Watch out, the system might adjust its clock
271
 
  // in which case time will actually go backwards. We don't guarantee that
272
 
  // times are increasing, or that two calls to Now() won't be the same.
273
 
  static Time Now();
274
 
 
275
 
  // Returns the maximum time, which should be greater than any reasonable time
276
 
  // with which we might compare it.
277
 
  static Time Max();
278
 
 
279
 
  // Returns the current time. Same as Now() except that this function always
280
 
  // uses system time so that there are no discrepancies between the returned
281
 
  // time and system time even on virtual environments including our test bot.
282
 
  // For timing sensitive unittests, this function should be used.
283
 
  static Time NowFromSystemTime();
284
 
 
285
 
  // Converts to/from time_t in UTC and a Time class.
286
 
  // TODO(brettw) this should be removed once everybody starts using the |Time|
287
 
  // class.
288
 
  static Time FromTimeT(time_t tt);
289
 
  time_t ToTimeT() const;
290
 
 
291
 
  // Converts time to/from a double which is the number of seconds since epoch
292
 
  // (Jan 1, 1970).  Webkit uses this format to represent time.
293
 
  // Because WebKit initializes double time value to 0 to indicate "not
294
 
  // initialized", we map it to empty Time object that also means "not
295
 
  // initialized".
296
 
  static Time FromDoubleT(double dt);
297
 
  double ToDoubleT() const;
298
 
 
299
 
#if defined(OS_POSIX)
300
 
  // Converts the timespec structure to time. MacOS X 10.8.3 (and tentatively,
301
 
  // earlier versions) will have the |ts|'s tv_nsec component zeroed out,
302
 
  // having a 1 second resolution, which agrees with
303
 
  // https://developer.apple.com/legacy/library/#technotes/tn/tn1150.html#HFSPlusDates.
304
 
  static Time FromTimeSpec(const timespec& ts);
305
 
#endif
306
 
 
307
 
  // Converts to/from the Javascript convention for times, a number of
308
 
  // milliseconds since the epoch:
309
 
  // https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date/getTime.
310
 
  static Time FromJsTime(double ms_since_epoch);
311
 
  double ToJsTime() const;
312
 
 
313
 
  // Converts to Java convention for times, a number of
314
 
  // milliseconds since the epoch.
315
 
  int64 ToJavaTime() const;
316
 
 
317
 
#if defined(OS_POSIX)
318
 
  static Time FromTimeVal(struct timeval t);
319
 
  struct timeval ToTimeVal() const;
320
 
#endif
321
 
 
322
 
#if defined(OS_MACOSX)
323
 
  static Time FromCFAbsoluteTime(CFAbsoluteTime t);
324
 
  CFAbsoluteTime ToCFAbsoluteTime() const;
325
 
#endif
326
 
 
327
 
#if defined(OS_WIN)
328
 
  static Time FromFileTime(FILETIME ft);
329
 
  FILETIME ToFileTime() const;
330
 
 
331
 
  // The minimum time of a low resolution timer.  This is basically a windows
332
 
  // constant of ~15.6ms.  While it does vary on some older OS versions, we'll
333
 
  // treat it as static across all windows versions.
334
 
  static const int kMinLowResolutionThresholdMs = 16;
335
 
 
336
 
  // Enable or disable Windows high resolution timer. If the high resolution
337
 
  // timer is not enabled, calls to ActivateHighResolutionTimer will fail.
338
 
  // When disabling the high resolution timer, this function will not cause
339
 
  // the high resolution timer to be deactivated, but will prevent future
340
 
  // activations.
341
 
  // Must be called from the main thread.
342
 
  // For more details see comments in time_win.cc.
343
 
  static void EnableHighResolutionTimer(bool enable);
344
 
 
345
 
  // Activates or deactivates the high resolution timer based on the |activate|
346
 
  // flag.  If the HighResolutionTimer is not Enabled (see
347
 
  // EnableHighResolutionTimer), this function will return false.  Otherwise
348
 
  // returns true.  Each successful activate call must be paired with a
349
 
  // subsequent deactivate call.
350
 
  // All callers to activate the high resolution timer must eventually call
351
 
  // this function to deactivate the high resolution timer.
352
 
  static bool ActivateHighResolutionTimer(bool activate);
353
 
 
354
 
  // Returns true if the high resolution timer is both enabled and activated.
355
 
  // This is provided for testing only, and is not tracked in a thread-safe
356
 
  // way.
357
 
  static bool IsHighResolutionTimerInUse();
358
 
#endif
359
 
 
360
 
  // Converts an exploded structure representing either the local time or UTC
361
 
  // into a Time class.
362
 
  static Time FromUTCExploded(const Exploded& exploded) {
363
 
    return FromExploded(false, exploded);
364
 
  }
365
 
  static Time FromLocalExploded(const Exploded& exploded) {
366
 
    return FromExploded(true, exploded);
367
 
  }
368
 
 
369
 
  // Converts an integer value representing Time to a class. This is used
370
 
  // when deserializing a |Time| structure, using a value known to be
371
 
  // compatible. It is not provided as a constructor because the integer type
372
 
  // may be unclear from the perspective of a caller.
373
 
  static Time FromInternalValue(int64 us) {
374
 
    return Time(us);
375
 
  }
376
 
 
377
 
  // Converts a string representation of time to a Time object.
378
 
  // An example of a time string which is converted is as below:-
379
 
  // "Tue, 15 Nov 1994 12:45:26 GMT". If the timezone is not specified
380
 
  // in the input string, FromString assumes local time and FromUTCString
381
 
  // assumes UTC. A timezone that cannot be parsed (e.g. "UTC" which is not
382
 
  // specified in RFC822) is treated as if the timezone is not specified.
383
 
  // TODO(iyengar) Move the FromString/FromTimeT/ToTimeT/FromFileTime to
384
 
  // a new time converter class.
385
 
  static bool FromString(const char* time_string, Time* parsed_time) {
386
 
    return FromStringInternal(time_string, true, parsed_time);
387
 
  }
388
 
  static bool FromUTCString(const char* time_string, Time* parsed_time) {
389
 
    return FromStringInternal(time_string, false, parsed_time);
390
 
  }
391
 
 
392
 
  // For serializing, use FromInternalValue to reconstitute. Please don't use
393
 
  // this and do arithmetic on it, as it is more error prone than using the
394
 
  // provided operators.
395
 
  int64 ToInternalValue() const {
396
 
    return us_;
397
 
  }
398
 
 
399
 
  // Fills the given exploded structure with either the local time or UTC from
400
 
  // this time structure (containing UTC).
401
 
  void UTCExplode(Exploded* exploded) const {
402
 
    return Explode(false, exploded);
403
 
  }
404
 
  void LocalExplode(Exploded* exploded) const {
405
 
    return Explode(true, exploded);
406
 
  }
407
 
 
408
 
  // Rounds this time down to the nearest day in local time. It will represent
409
 
  // midnight on that day.
410
 
  Time LocalMidnight() const;
411
 
 
412
 
  Time& operator=(Time other) {
413
 
    us_ = other.us_;
414
 
    return *this;
415
 
  }
416
 
 
417
 
  // Compute the difference between two times.
418
 
  TimeDelta operator-(Time other) const {
419
 
    return TimeDelta(us_ - other.us_);
420
 
  }
421
 
 
422
 
  // Modify by some time delta.
423
 
  Time& operator+=(TimeDelta delta) {
424
 
    us_ += delta.delta_;
425
 
    return *this;
426
 
  }
427
 
  Time& operator-=(TimeDelta delta) {
428
 
    us_ -= delta.delta_;
429
 
    return *this;
430
 
  }
431
 
 
432
 
  // Return a new time modified by some delta.
433
 
  Time operator+(TimeDelta delta) const {
434
 
    return Time(us_ + delta.delta_);
435
 
  }
436
 
  Time operator-(TimeDelta delta) const {
437
 
    return Time(us_ - delta.delta_);
438
 
  }
439
 
 
440
 
  // Comparison operators
441
 
  bool operator==(Time other) const {
442
 
    return us_ == other.us_;
443
 
  }
444
 
  bool operator!=(Time other) const {
445
 
    return us_ != other.us_;
446
 
  }
447
 
  bool operator<(Time other) const {
448
 
    return us_ < other.us_;
449
 
  }
450
 
  bool operator<=(Time other) const {
451
 
    return us_ <= other.us_;
452
 
  }
453
 
  bool operator>(Time other) const {
454
 
    return us_ > other.us_;
455
 
  }
456
 
  bool operator>=(Time other) const {
457
 
    return us_ >= other.us_;
458
 
  }
459
 
 
460
 
 private:
461
 
  friend class TimeDelta;
462
 
 
463
 
  explicit Time(int64 us) : us_(us) {
464
 
  }
465
 
 
466
 
  // Explodes the given time to either local time |is_local = true| or UTC
467
 
  // |is_local = false|.
468
 
  void Explode(bool is_local, Exploded* exploded) const;
469
 
 
470
 
  // Unexplodes a given time assuming the source is either local time
471
 
  // |is_local = true| or UTC |is_local = false|.
472
 
  static Time FromExploded(bool is_local, const Exploded& exploded);
473
 
 
474
 
  // Converts a string representation of time to a Time object.
475
 
  // An example of a time string which is converted is as below:-
476
 
  // "Tue, 15 Nov 1994 12:45:26 GMT". If the timezone is not specified
477
 
  // in the input string, local time |is_local = true| or
478
 
  // UTC |is_local = false| is assumed. A timezone that cannot be parsed
479
 
  // (e.g. "UTC" which is not specified in RFC822) is treated as if the
480
 
  // timezone is not specified.
481
 
  static bool FromStringInternal(const char* time_string,
482
 
                                 bool is_local,
483
 
                                 Time* parsed_time);
484
 
 
485
 
  // The representation of Jan 1, 1970 UTC in microseconds since the
486
 
  // platform-dependent epoch.
487
 
  static const int64 kTimeTToMicrosecondsOffset;
488
 
 
489
 
#if defined(OS_WIN)
490
 
  // Indicates whether fast timers are usable right now.  For instance,
491
 
  // when using battery power, we might elect to prevent high speed timers
492
 
  // which would draw more power.
493
 
  static bool high_resolution_timer_enabled_;
494
 
  // Count of activations on the high resolution timer.  Only use in tests
495
 
  // which are single threaded.
496
 
  static int high_resolution_timer_activated_;
497
 
#endif
498
 
 
499
 
  // Time in microseconds in UTC.
500
 
  int64 us_;
501
 
};
502
 
 
503
 
// Inline the TimeDelta factory methods, for fast TimeDelta construction.
504
 
 
505
 
// static
506
 
inline TimeDelta TimeDelta::FromDays(int days) {
507
 
  // Preserve max to prevent overflow.
508
 
  if (days == std::numeric_limits<int>::max())
509
 
    return Max();
510
 
  return TimeDelta(days * Time::kMicrosecondsPerDay);
511
 
}
512
 
 
513
 
// static
514
 
inline TimeDelta TimeDelta::FromHours(int hours) {
515
 
  // Preserve max to prevent overflow.
516
 
  if (hours == std::numeric_limits<int>::max())
517
 
    return Max();
518
 
  return TimeDelta(hours * Time::kMicrosecondsPerHour);
519
 
}
520
 
 
521
 
// static
522
 
inline TimeDelta TimeDelta::FromMinutes(int minutes) {
523
 
  // Preserve max to prevent overflow.
524
 
  if (minutes == std::numeric_limits<int>::max())
525
 
    return Max();
526
 
  return TimeDelta(minutes * Time::kMicrosecondsPerMinute);
527
 
}
528
 
 
529
 
// static
530
 
inline TimeDelta TimeDelta::FromSeconds(int64 secs) {
531
 
  // Preserve max to prevent overflow.
532
 
  if (secs == std::numeric_limits<int64>::max())
533
 
    return Max();
534
 
  return TimeDelta(secs * Time::kMicrosecondsPerSecond);
535
 
}
536
 
 
537
 
// static
538
 
inline TimeDelta TimeDelta::FromMilliseconds(int64 ms) {
539
 
  // Preserve max to prevent overflow.
540
 
  if (ms == std::numeric_limits<int64>::max())
541
 
    return Max();
542
 
  return TimeDelta(ms * Time::kMicrosecondsPerMillisecond);
543
 
}
544
 
 
545
 
// static
546
 
inline TimeDelta TimeDelta::FromMicroseconds(int64 us) {
547
 
  // Preserve max to prevent overflow.
548
 
  if (us == std::numeric_limits<int64>::max())
549
 
    return Max();
550
 
  return TimeDelta(us);
551
 
}
552
 
 
553
 
inline Time TimeDelta::operator+(Time t) const {
554
 
  return Time(t.us_ + delta_);
555
 
}
556
 
 
557
 
// TimeTicks ------------------------------------------------------------------
558
 
 
559
 
class BASE_EXPORT TimeTicks {
560
 
 public:
561
 
  TimeTicks() : ticks_(0) {
562
 
  }
563
 
 
564
 
  // Platform-dependent tick count representing "right now."
565
 
  // The resolution of this clock is ~1-15ms.  Resolution varies depending
566
 
  // on hardware/operating system configuration.
567
 
  static TimeTicks Now();
568
 
 
569
 
  // Returns a platform-dependent high-resolution tick count. Implementation
570
 
  // is hardware dependent and may or may not return sub-millisecond
571
 
  // resolution.  THIS CALL IS GENERALLY MUCH MORE EXPENSIVE THAN Now() AND
572
 
  // SHOULD ONLY BE USED WHEN IT IS REALLY NEEDED.
573
 
  static TimeTicks HighResNow();
574
 
 
575
 
  static bool IsHighResNowFastAndReliable();
576
 
 
577
 
  // Returns true if ThreadNow() is supported on this system.
578
 
  static bool IsThreadNowSupported() {
579
 
#if (defined(_POSIX_THREAD_CPUTIME) && (_POSIX_THREAD_CPUTIME >= 0)) || \
580
 
    (defined(OS_MACOSX) && !defined(OS_IOS)) || defined(OS_ANDROID)
581
 
    return true;
582
 
#else
583
 
    return false;
584
 
#endif
585
 
  }
586
 
 
587
 
  // Returns thread-specific CPU-time on systems that support this feature.
588
 
  // Needs to be guarded with a call to IsThreadNowSupported(). Use this timer
589
 
  // to (approximately) measure how much time the calling thread spent doing
590
 
  // actual work vs. being de-scheduled. May return bogus results if the thread
591
 
  // migrates to another CPU between two calls.
592
 
  static TimeTicks ThreadNow();
593
 
 
594
 
  // Returns the current system trace time or, if none is defined, the current
595
 
  // high-res time (i.e. HighResNow()). On systems where a global trace clock
596
 
  // is defined, timestamping TraceEvents's with this value guarantees
597
 
  // synchronization between events collected inside chrome and events
598
 
  // collected outside (e.g. kernel, X server).
599
 
  static TimeTicks NowFromSystemTraceTime();
600
 
 
601
 
#if defined(OS_WIN)
602
 
  // Get the absolute value of QPC time drift. For testing.
603
 
  static int64 GetQPCDriftMicroseconds();
604
 
 
605
 
  static TimeTicks FromQPCValue(LONGLONG qpc_value);
606
 
 
607
 
  // Returns true if the high resolution clock is working on this system.
608
 
  // This is only for testing.
609
 
  static bool IsHighResClockWorking();
610
 
 
611
 
  // Enable high resolution time for TimeTicks::Now(). This function will
612
 
  // test for the availability of a working implementation of
613
 
  // QueryPerformanceCounter(). If one is not available, this function does
614
 
  // nothing and the resolution of Now() remains 1ms. Otherwise, all future
615
 
  // calls to TimeTicks::Now() will have the higher resolution provided by QPC.
616
 
  // Returns true if high resolution time was successfully enabled.
617
 
  static bool SetNowIsHighResNowIfSupported();
618
 
 
619
 
  // Returns a time value that is NOT rollover protected.
620
 
  static TimeTicks UnprotectedNow();
621
 
#endif
622
 
 
623
 
  // Returns true if this object has not been initialized.
624
 
  bool is_null() const {
625
 
    return ticks_ == 0;
626
 
  }
627
 
 
628
 
  // Converts an integer value representing TimeTicks to a class. This is used
629
 
  // when deserializing a |TimeTicks| structure, using a value known to be
630
 
  // compatible. It is not provided as a constructor because the integer type
631
 
  // may be unclear from the perspective of a caller.
632
 
  static TimeTicks FromInternalValue(int64 ticks) {
633
 
    return TimeTicks(ticks);
634
 
  }
635
 
 
636
 
  // Get the TimeTick value at the time of the UnixEpoch. This is useful when
637
 
  // you need to relate the value of TimeTicks to a real time and date.
638
 
  // Note: Upon first invocation, this function takes a snapshot of the realtime
639
 
  // clock to establish a reference point.  This function will return the same
640
 
  // value for the duration of the application, but will be different in future
641
 
  // application runs.
642
 
  static TimeTicks UnixEpoch();
643
 
 
644
 
  // Returns the internal numeric value of the TimeTicks object.
645
 
  // For serializing, use FromInternalValue to reconstitute.
646
 
  int64 ToInternalValue() const {
647
 
    return ticks_;
648
 
  }
649
 
 
650
 
  TimeTicks& operator=(TimeTicks other) {
651
 
    ticks_ = other.ticks_;
652
 
    return *this;
653
 
  }
654
 
 
655
 
  // Compute the difference between two times.
656
 
  TimeDelta operator-(TimeTicks other) const {
657
 
    return TimeDelta(ticks_ - other.ticks_);
658
 
  }
659
 
 
660
 
  // Modify by some time delta.
661
 
  TimeTicks& operator+=(TimeDelta delta) {
662
 
    ticks_ += delta.delta_;
663
 
    return *this;
664
 
  }
665
 
  TimeTicks& operator-=(TimeDelta delta) {
666
 
    ticks_ -= delta.delta_;
667
 
    return *this;
668
 
  }
669
 
 
670
 
  // Return a new TimeTicks modified by some delta.
671
 
  TimeTicks operator+(TimeDelta delta) const {
672
 
    return TimeTicks(ticks_ + delta.delta_);
673
 
  }
674
 
  TimeTicks operator-(TimeDelta delta) const {
675
 
    return TimeTicks(ticks_ - delta.delta_);
676
 
  }
677
 
 
678
 
  // Comparison operators
679
 
  bool operator==(TimeTicks other) const {
680
 
    return ticks_ == other.ticks_;
681
 
  }
682
 
  bool operator!=(TimeTicks other) const {
683
 
    return ticks_ != other.ticks_;
684
 
  }
685
 
  bool operator<(TimeTicks other) const {
686
 
    return ticks_ < other.ticks_;
687
 
  }
688
 
  bool operator<=(TimeTicks other) const {
689
 
    return ticks_ <= other.ticks_;
690
 
  }
691
 
  bool operator>(TimeTicks other) const {
692
 
    return ticks_ > other.ticks_;
693
 
  }
694
 
  bool operator>=(TimeTicks other) const {
695
 
    return ticks_ >= other.ticks_;
696
 
  }
697
 
 
698
 
 protected:
699
 
  friend class TimeDelta;
700
 
 
701
 
  // Please use Now() to create a new object. This is for internal use
702
 
  // and testing. Ticks is in microseconds.
703
 
  explicit TimeTicks(int64 ticks) : ticks_(ticks) {
704
 
  }
705
 
 
706
 
  // Tick count in microseconds.
707
 
  int64 ticks_;
708
 
 
709
 
#if defined(OS_WIN)
710
 
  typedef DWORD (*TickFunctionType)(void);
711
 
  static TickFunctionType SetMockTickFunction(TickFunctionType ticker);
712
 
#endif
713
 
};
714
 
 
715
 
inline TimeTicks TimeDelta::operator+(TimeTicks t) const {
716
 
  return TimeTicks(t.ticks_ + delta_);
717
 
}
718
 
 
719
 
}  // namespace base
720
 
 
721
 
#endif  // BASE_TIME_TIME_H_