Hide Google logo and custom launcher page in app list for non-Google search engines.
[chromium-blink-merge.git] / base / time / time.h
blob18de085e341714b85efe6e10a988835601bf998e
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.
5 // Time represents an absolute point in coordinated universal time (UTC),
6 // internally represented as microseconds (s/1,000,000) since the Windows epoch
7 // (1601-01-01 00:00:00 UTC) (See http://crbug.com/14734). System-dependent
8 // clock interface routines are defined in time_PLATFORM.cc.
9 //
10 // TimeDelta represents a duration of time, internally represented in
11 // microseconds.
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.
20 // These classes are represented as only a 64-bit value, so they can be
21 // efficiently passed by value.
23 // Definitions of operator<< are provided to make these types work with
24 // DCHECK_EQ() and other log macros. For human-readable formatting, see
25 // "base/i18n/time_formatting.h".
27 #ifndef BASE_TIME_TIME_H_
28 #define BASE_TIME_TIME_H_
30 #include <time.h>
32 #include <iosfwd>
34 #include "base/base_export.h"
35 #include "base/basictypes.h"
36 #include "build/build_config.h"
38 #if defined(OS_MACOSX)
39 #include <CoreFoundation/CoreFoundation.h>
40 // Avoid Mac system header macro leak.
41 #undef TYPE_BOOL
42 #endif
44 #if defined(OS_POSIX)
45 #include <unistd.h>
46 #include <sys/time.h>
47 #endif
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
55 #include <limits>
57 namespace base {
59 class Time;
60 class TimeTicks;
62 // TimeDelta ------------------------------------------------------------------
64 class BASE_EXPORT TimeDelta {
65 public:
66 TimeDelta() : delta_(0) {
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
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);
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();
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_;
103 // 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 overload for std::abs(). The following is a simple branchless
107 // implementation:
108 const int64 mask = delta_ >> (sizeof(delta_) * 8 - 1);
109 return TimeDelta((delta_ + mask) ^ mask);
112 // Returns true if the time delta is the maximum time delta.
113 bool is_max() const {
114 return delta_ == std::numeric_limits<int64>::max();
117 #if defined(OS_POSIX)
118 struct timespec ToTimeSpec() const;
119 #endif
121 // Returns the time delta in some unit. The F versions return a floating
122 // point value, the "regular" versions return a rounded-down value.
124 // InMillisecondsRoundedUp() instead returns an integer that is rounded up
125 // to the next full millisecond.
126 int InDays() const;
127 int InHours() const;
128 int InMinutes() const;
129 double InSecondsF() const;
130 int64 InSeconds() const;
131 double InMillisecondsF() const;
132 int64 InMilliseconds() const;
133 int64 InMillisecondsRoundedUp() const;
134 int64 InMicroseconds() const;
136 TimeDelta& operator=(TimeDelta other) {
137 delta_ = other.delta_;
138 return *this;
141 // Computations with other deltas.
142 TimeDelta operator+(TimeDelta other) const {
143 return TimeDelta(delta_ + other.delta_);
145 TimeDelta operator-(TimeDelta other) const {
146 return TimeDelta(delta_ - other.delta_);
149 TimeDelta& operator+=(TimeDelta other) {
150 delta_ += other.delta_;
151 return *this;
153 TimeDelta& operator-=(TimeDelta other) {
154 delta_ -= other.delta_;
155 return *this;
157 TimeDelta operator-() const {
158 return TimeDelta(-delta_);
161 // Computations with ints, note that we only allow multiplicative operations
162 // with ints, and additive operations with other deltas.
163 TimeDelta operator*(int64 a) const {
164 return TimeDelta(delta_ * a);
166 TimeDelta operator/(int64 a) const {
167 return TimeDelta(delta_ / a);
169 TimeDelta& operator*=(int64 a) {
170 delta_ *= a;
171 return *this;
173 TimeDelta& operator/=(int64 a) {
174 delta_ /= a;
175 return *this;
177 int64 operator/(TimeDelta a) const {
178 return delta_ / a.delta_;
181 // Defined below because it depends on the definition of the other classes.
182 Time operator+(Time t) const;
183 TimeTicks operator+(TimeTicks t) const;
185 // Comparison operators.
186 bool operator==(TimeDelta other) const {
187 return delta_ == other.delta_;
189 bool operator!=(TimeDelta other) const {
190 return delta_ != other.delta_;
192 bool operator<(TimeDelta other) const {
193 return delta_ < other.delta_;
195 bool operator<=(TimeDelta other) const {
196 return delta_ <= other.delta_;
198 bool operator>(TimeDelta other) const {
199 return delta_ > other.delta_;
201 bool operator>=(TimeDelta other) const {
202 return delta_ >= other.delta_;
205 private:
206 friend class Time;
207 friend class TimeTicks;
208 friend TimeDelta operator*(int64 a, TimeDelta td);
210 // Constructs a delta given the duration in microseconds. This is private
211 // to avoid confusion by callers with an integer constructor. Use
212 // FromSeconds, FromMilliseconds, etc. instead.
213 explicit TimeDelta(int64 delta_us) : delta_(delta_us) {
216 // Delta in microseconds.
217 int64 delta_;
220 inline TimeDelta operator*(int64 a, TimeDelta td) {
221 return TimeDelta(a * td.delta_);
224 // For logging use only.
225 BASE_EXPORT std::ostream& operator<<(std::ostream& os, TimeDelta time_delta);
227 // Time -----------------------------------------------------------------------
229 // Represents a wall clock time in UTC.
230 class BASE_EXPORT Time {
231 public:
232 static const int64 kMillisecondsPerSecond = 1000;
233 static const int64 kMicrosecondsPerMillisecond = 1000;
234 static const int64 kMicrosecondsPerSecond = kMicrosecondsPerMillisecond *
235 kMillisecondsPerSecond;
236 static const int64 kMicrosecondsPerMinute = kMicrosecondsPerSecond * 60;
237 static const int64 kMicrosecondsPerHour = kMicrosecondsPerMinute * 60;
238 static const int64 kMicrosecondsPerDay = kMicrosecondsPerHour * 24;
239 static const int64 kMicrosecondsPerWeek = kMicrosecondsPerDay * 7;
240 static const int64 kNanosecondsPerMicrosecond = 1000;
241 static const int64 kNanosecondsPerSecond = kNanosecondsPerMicrosecond *
242 kMicrosecondsPerSecond;
244 // The representation of Jan 1, 1970 UTC in microseconds since the
245 // platform-dependent epoch.
246 static const int64 kTimeTToMicrosecondsOffset;
248 #if !defined(OS_WIN)
249 // On Mac & Linux, this value is the delta from the Windows epoch of 1601 to
250 // the Posix delta of 1970. This is used for migrating between the old
251 // 1970-based epochs to the new 1601-based ones. It should be removed from
252 // this global header and put in the platform-specific ones when we remove the
253 // migration code.
254 static const int64 kWindowsEpochDeltaMicroseconds;
255 #else
256 // To avoid overflow in QPC to Microseconds calculations, since we multiply
257 // by kMicrosecondsPerSecond, then the QPC value should not exceed
258 // (2^63 - 1) / 1E6. If it exceeds that threshold, we divide then multiply.
259 static const int64 kQPCOverflowThreshold = 0x8637BD05AF7;
260 #endif
262 // Represents an exploded time that can be formatted nicely. This is kind of
263 // like the Win32 SYSTEMTIME structure or the Unix "struct tm" with a few
264 // additions and changes to prevent errors.
265 struct BASE_EXPORT Exploded {
266 int year; // Four digit year "2007"
267 int month; // 1-based month (values 1 = January, etc.)
268 int day_of_week; // 0-based day of week (0 = Sunday, etc.)
269 int day_of_month; // 1-based day of month (1-31)
270 int hour; // Hour within the current day (0-23)
271 int minute; // Minute within the current hour (0-59)
272 int second; // Second within the current minute (0-59 plus leap
273 // seconds which may take it up to 60).
274 int millisecond; // Milliseconds within the current second (0-999)
276 // A cursory test for whether the data members are within their
277 // respective ranges. A 'true' return value does not guarantee the
278 // Exploded value can be successfully converted to a Time value.
279 bool HasValidValues() const;
282 // Contains the NULL time. Use Time::Now() to get the current time.
283 Time() : us_(0) {
286 // Returns true if the time object has not been initialized.
287 bool is_null() const {
288 return us_ == 0;
291 // Returns true if the time object is the maximum time.
292 bool is_max() const {
293 return us_ == std::numeric_limits<int64>::max();
296 // Returns the time for epoch in Unix-like system (Jan 1, 1970).
297 static Time UnixEpoch();
299 // Returns the current time. Watch out, the system might adjust its clock
300 // in which case time will actually go backwards. We don't guarantee that
301 // times are increasing, or that two calls to Now() won't be the same.
302 static Time Now();
304 // Returns the maximum time, which should be greater than any reasonable time
305 // with which we might compare it.
306 static Time Max();
308 // Returns the current time. Same as Now() except that this function always
309 // uses system time so that there are no discrepancies between the returned
310 // time and system time even on virtual environments including our test bot.
311 // For timing sensitive unittests, this function should be used.
312 static Time NowFromSystemTime();
314 // Converts to/from time_t in UTC and a Time class.
315 // TODO(brettw) this should be removed once everybody starts using the |Time|
316 // class.
317 static Time FromTimeT(time_t tt);
318 time_t ToTimeT() const;
320 // Converts time to/from a double which is the number of seconds since epoch
321 // (Jan 1, 1970). Webkit uses this format to represent time.
322 // Because WebKit initializes double time value to 0 to indicate "not
323 // initialized", we map it to empty Time object that also means "not
324 // initialized".
325 static Time FromDoubleT(double dt);
326 double ToDoubleT() const;
328 #if defined(OS_POSIX)
329 // Converts the timespec structure to time. MacOS X 10.8.3 (and tentatively,
330 // earlier versions) will have the |ts|'s tv_nsec component zeroed out,
331 // having a 1 second resolution, which agrees with
332 // https://developer.apple.com/legacy/library/#technotes/tn/tn1150.html#HFSPlusDates.
333 static Time FromTimeSpec(const timespec& ts);
334 #endif
336 // Converts to/from the Javascript convention for times, a number of
337 // milliseconds since the epoch:
338 // https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date/getTime.
339 static Time FromJsTime(double ms_since_epoch);
340 double ToJsTime() const;
342 // Converts to Java convention for times, a number of
343 // milliseconds since the epoch.
344 int64 ToJavaTime() const;
346 #if defined(OS_POSIX)
347 static Time FromTimeVal(struct timeval t);
348 struct timeval ToTimeVal() const;
349 #endif
351 #if defined(OS_MACOSX)
352 static Time FromCFAbsoluteTime(CFAbsoluteTime t);
353 CFAbsoluteTime ToCFAbsoluteTime() const;
354 #endif
356 #if defined(OS_WIN)
357 static Time FromFileTime(FILETIME ft);
358 FILETIME ToFileTime() const;
360 // The minimum time of a low resolution timer. This is basically a windows
361 // constant of ~15.6ms. While it does vary on some older OS versions, we'll
362 // treat it as static across all windows versions.
363 static const int kMinLowResolutionThresholdMs = 16;
365 // Enable or disable Windows high resolution timer.
366 static void EnableHighResolutionTimer(bool enable);
368 // Activates or deactivates the high resolution timer based on the |activate|
369 // flag. If the HighResolutionTimer is not Enabled (see
370 // EnableHighResolutionTimer), this function will return false. Otherwise
371 // returns true. Each successful activate call must be paired with a
372 // subsequent deactivate call.
373 // All callers to activate the high resolution timer must eventually call
374 // this function to deactivate the high resolution timer.
375 static bool ActivateHighResolutionTimer(bool activate);
377 // Returns true if the high resolution timer is both enabled and activated.
378 // This is provided for testing only, and is not tracked in a thread-safe
379 // way.
380 static bool IsHighResolutionTimerInUse();
381 #endif
383 // Converts an exploded structure representing either the local time or UTC
384 // into a Time class.
385 static Time FromUTCExploded(const Exploded& exploded) {
386 return FromExploded(false, exploded);
388 static Time FromLocalExploded(const Exploded& exploded) {
389 return FromExploded(true, exploded);
392 // Converts an integer value representing Time to a class. This is used
393 // when deserializing a |Time| structure, using a value known to be
394 // compatible. It is not provided as a constructor because the integer type
395 // may be unclear from the perspective of a caller.
396 static Time FromInternalValue(int64 us) {
397 return Time(us);
400 // Converts a string representation of time to a Time object.
401 // An example of a time string which is converted is as below:-
402 // "Tue, 15 Nov 1994 12:45:26 GMT". If the timezone is not specified
403 // in the input string, FromString assumes local time and FromUTCString
404 // assumes UTC. A timezone that cannot be parsed (e.g. "UTC" which is not
405 // specified in RFC822) is treated as if the timezone is not specified.
406 // TODO(iyengar) Move the FromString/FromTimeT/ToTimeT/FromFileTime to
407 // a new time converter class.
408 static bool FromString(const char* time_string, Time* parsed_time) {
409 return FromStringInternal(time_string, true, parsed_time);
411 static bool FromUTCString(const char* time_string, Time* parsed_time) {
412 return FromStringInternal(time_string, false, parsed_time);
415 // For serializing, use FromInternalValue to reconstitute. Please don't use
416 // this and do arithmetic on it, as it is more error prone than using the
417 // provided operators.
418 int64 ToInternalValue() const {
419 return us_;
422 // Fills the given exploded structure with either the local time or UTC from
423 // this time structure (containing UTC).
424 void UTCExplode(Exploded* exploded) const {
425 return Explode(false, exploded);
427 void LocalExplode(Exploded* exploded) const {
428 return Explode(true, exploded);
431 // Rounds this time down to the nearest day in local time. It will represent
432 // midnight on that day.
433 Time LocalMidnight() const;
435 Time& operator=(Time other) {
436 us_ = other.us_;
437 return *this;
440 // Compute the difference between two times.
441 TimeDelta operator-(Time other) const {
442 return TimeDelta(us_ - other.us_);
445 // Modify by some time delta.
446 Time& operator+=(TimeDelta delta) {
447 us_ += delta.delta_;
448 return *this;
450 Time& operator-=(TimeDelta delta) {
451 us_ -= delta.delta_;
452 return *this;
455 // Return a new time modified by some delta.
456 Time operator+(TimeDelta delta) const {
457 return Time(us_ + delta.delta_);
459 Time operator-(TimeDelta delta) const {
460 return Time(us_ - delta.delta_);
463 // Comparison operators
464 bool operator==(Time other) const {
465 return us_ == other.us_;
467 bool operator!=(Time other) const {
468 return us_ != other.us_;
470 bool operator<(Time other) const {
471 return us_ < other.us_;
473 bool operator<=(Time other) const {
474 return us_ <= other.us_;
476 bool operator>(Time other) const {
477 return us_ > other.us_;
479 bool operator>=(Time other) const {
480 return us_ >= other.us_;
483 private:
484 friend class TimeDelta;
486 explicit Time(int64 us) : us_(us) {
489 // Explodes the given time to either local time |is_local = true| or UTC
490 // |is_local = false|.
491 void Explode(bool is_local, Exploded* exploded) const;
493 // Unexplodes a given time assuming the source is either local time
494 // |is_local = true| or UTC |is_local = false|.
495 static Time FromExploded(bool is_local, const Exploded& exploded);
497 // Converts a string representation of time to a Time object.
498 // An example of a time string which is converted is as below:-
499 // "Tue, 15 Nov 1994 12:45:26 GMT". If the timezone is not specified
500 // in the input string, local time |is_local = true| or
501 // UTC |is_local = false| is assumed. A timezone that cannot be parsed
502 // (e.g. "UTC" which is not specified in RFC822) is treated as if the
503 // timezone is not specified.
504 static bool FromStringInternal(const char* time_string,
505 bool is_local,
506 Time* parsed_time);
508 // Time in microseconds in UTC.
509 int64 us_;
512 // Inline the TimeDelta factory methods, for fast TimeDelta construction.
514 // static
515 inline TimeDelta TimeDelta::FromDays(int days) {
516 // Preserve max to prevent overflow.
517 if (days == std::numeric_limits<int>::max())
518 return Max();
519 return TimeDelta(days * Time::kMicrosecondsPerDay);
522 // static
523 inline TimeDelta TimeDelta::FromHours(int hours) {
524 // Preserve max to prevent overflow.
525 if (hours == std::numeric_limits<int>::max())
526 return Max();
527 return TimeDelta(hours * Time::kMicrosecondsPerHour);
530 // static
531 inline TimeDelta TimeDelta::FromMinutes(int minutes) {
532 // Preserve max to prevent overflow.
533 if (minutes == std::numeric_limits<int>::max())
534 return Max();
535 return TimeDelta(minutes * Time::kMicrosecondsPerMinute);
538 // static
539 inline TimeDelta TimeDelta::FromSeconds(int64 secs) {
540 // Preserve max to prevent overflow.
541 if (secs == std::numeric_limits<int64>::max())
542 return Max();
543 return TimeDelta(secs * Time::kMicrosecondsPerSecond);
546 // static
547 inline TimeDelta TimeDelta::FromMilliseconds(int64 ms) {
548 // Preserve max to prevent overflow.
549 if (ms == std::numeric_limits<int64>::max())
550 return Max();
551 return TimeDelta(ms * Time::kMicrosecondsPerMillisecond);
554 // static
555 inline TimeDelta TimeDelta::FromSecondsD(double secs) {
556 // Preserve max to prevent overflow.
557 if (secs == std::numeric_limits<double>::infinity())
558 return Max();
559 return TimeDelta(static_cast<int64>(secs * Time::kMicrosecondsPerSecond));
562 // static
563 inline TimeDelta TimeDelta::FromMillisecondsD(double ms) {
564 // Preserve max to prevent overflow.
565 if (ms == std::numeric_limits<double>::infinity())
566 return Max();
567 return TimeDelta(static_cast<int64>(ms * Time::kMicrosecondsPerMillisecond));
570 // static
571 inline TimeDelta TimeDelta::FromMicroseconds(int64 us) {
572 // Preserve max to prevent overflow.
573 if (us == std::numeric_limits<int64>::max())
574 return Max();
575 return TimeDelta(us);
578 inline Time TimeDelta::operator+(Time t) const {
579 return Time(t.us_ + delta_);
582 // For logging use only.
583 BASE_EXPORT std::ostream& operator<<(std::ostream& os, Time time);
585 // TimeTicks ------------------------------------------------------------------
587 class BASE_EXPORT TimeTicks {
588 public:
589 // We define this even without OS_CHROMEOS for seccomp sandbox testing.
590 #if defined(OS_LINUX)
591 // Force definition of the system trace clock; it is a chromeos-only api
592 // at the moment and surfacing it in the right place requires mucking
593 // with glibc et al.
594 static const clockid_t kClockSystemTrace = 11;
595 #endif
597 TimeTicks() : ticks_(0) {
600 // Platform-dependent tick count representing "right now." When
601 // IsHighResolution() returns false, the resolution of the clock could be
602 // as coarse as ~15.6ms. Otherwise, the resolution should be no worse than one
603 // microsecond.
604 static TimeTicks Now();
606 // Returns true if the high resolution clock is working on this system and
607 // Now() will return high resolution values. Note that, on systems where the
608 // high resolution clock works but is deemed inefficient, the low resolution
609 // clock will be used instead.
610 static bool IsHighResolution();
612 // Returns true if ThreadNow() is supported on this system.
613 static bool IsThreadNowSupported() {
614 #if (defined(_POSIX_THREAD_CPUTIME) && (_POSIX_THREAD_CPUTIME >= 0)) || \
615 (defined(OS_MACOSX) && !defined(OS_IOS)) || defined(OS_ANDROID)
616 return true;
617 #else
618 return false;
619 #endif
622 // Returns thread-specific CPU-time on systems that support this feature.
623 // Needs to be guarded with a call to IsThreadNowSupported(). Use this timer
624 // to (approximately) measure how much time the calling thread spent doing
625 // actual work vs. being de-scheduled. May return bogus results if the thread
626 // migrates to another CPU between two calls.
628 // WARNING: The returned value might NOT have the same origin as Now(). Do not
629 // perform math between TimeTicks values returned by Now() and ThreadNow() and
630 // expect meaningful results.
631 // TODO(miu): Since the timeline of these values is different, the values
632 // should be of a different type.
633 static TimeTicks ThreadNow();
635 // Returns the current system trace time or, if not available on this
636 // platform, a high-resolution time value; or a low-resolution time value if
637 // neither are avalable. On systems where a global trace clock is defined,
638 // timestamping TraceEvents's with this value guarantees synchronization
639 // between events collected inside chrome and events collected outside
640 // (e.g. kernel, X server).
642 // WARNING: The returned value might NOT have the same origin as Now(). Do not
643 // perform math between TimeTicks values returned by Now() and
644 // NowFromSystemTraceTime() and expect meaningful results.
645 // TODO(miu): Since the timeline of these values is different, the values
646 // should be of a different type.
647 static TimeTicks NowFromSystemTraceTime();
649 #if defined(OS_WIN)
650 // Translates an absolute QPC timestamp into a TimeTicks value. The returned
651 // value has the same origin as Now(). Do NOT attempt to use this if
652 // IsHighResolution() returns false.
653 static TimeTicks FromQPCValue(LONGLONG qpc_value);
654 #endif
656 // Returns true if this object has not been initialized.
657 bool is_null() const {
658 return ticks_ == 0;
661 // Converts an integer value representing TimeTicks to a class. This is used
662 // when deserializing a |TimeTicks| structure, using a value known to be
663 // compatible. It is not provided as a constructor because the integer type
664 // may be unclear from the perspective of a caller.
665 static TimeTicks FromInternalValue(int64 ticks) {
666 return TimeTicks(ticks);
669 // Get the TimeTick value at the time of the UnixEpoch. This is useful when
670 // you need to relate the value of TimeTicks to a real time and date.
671 // Note: Upon first invocation, this function takes a snapshot of the realtime
672 // clock to establish a reference point. This function will return the same
673 // value for the duration of the application, but will be different in future
674 // application runs.
675 static TimeTicks UnixEpoch();
677 // Returns the internal numeric value of the TimeTicks object.
678 // For serializing, use FromInternalValue to reconstitute.
679 int64 ToInternalValue() const {
680 return ticks_;
683 // Returns |this| snapped to the next tick, given a |tick_phase| and
684 // repeating |tick_interval| in both directions. |this| may be before,
685 // after, or equal to the |tick_phase|.
686 TimeTicks SnappedToNextTick(TimeTicks tick_phase,
687 TimeDelta tick_interval) const;
689 TimeTicks& operator=(TimeTicks other) {
690 ticks_ = other.ticks_;
691 return *this;
694 // Compute the difference between two times.
695 TimeDelta operator-(TimeTicks other) const {
696 return TimeDelta(ticks_ - other.ticks_);
699 // Modify by some time delta.
700 TimeTicks& operator+=(TimeDelta delta) {
701 ticks_ += delta.delta_;
702 return *this;
704 TimeTicks& operator-=(TimeDelta delta) {
705 ticks_ -= delta.delta_;
706 return *this;
709 // Return a new TimeTicks modified by some delta.
710 TimeTicks operator+(TimeDelta delta) const {
711 return TimeTicks(ticks_ + delta.delta_);
713 TimeTicks operator-(TimeDelta delta) const {
714 return TimeTicks(ticks_ - delta.delta_);
717 // Comparison operators
718 bool operator==(TimeTicks other) const {
719 return ticks_ == other.ticks_;
721 bool operator!=(TimeTicks other) const {
722 return ticks_ != other.ticks_;
724 bool operator<(TimeTicks other) const {
725 return ticks_ < other.ticks_;
727 bool operator<=(TimeTicks other) const {
728 return ticks_ <= other.ticks_;
730 bool operator>(TimeTicks other) const {
731 return ticks_ > other.ticks_;
733 bool operator>=(TimeTicks other) const {
734 return ticks_ >= other.ticks_;
737 protected:
738 friend class TimeDelta;
740 // Please use Now() to create a new object. This is for internal use
741 // and testing. Ticks is in microseconds.
742 explicit TimeTicks(int64 ticks) : ticks_(ticks) {
745 // Tick count in microseconds.
746 int64 ticks_;
748 #if defined(OS_WIN)
749 typedef DWORD (*TickFunctionType)(void);
750 static TickFunctionType SetMockTickFunction(TickFunctionType ticker);
751 #endif
754 inline TimeTicks TimeDelta::operator+(TimeTicks t) const {
755 return TimeTicks(t.ticks_ + delta_);
758 // For logging use only.
759 BASE_EXPORT std::ostream& operator<<(std::ostream& os, TimeTicks time_ticks);
761 } // namespace base
763 #endif // BASE_TIME_TIME_H_