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