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