michael@0: // Copyright (c) 2012 The Chromium Authors. All rights reserved. michael@0: // Use of this source code is governed by a BSD-style license that can be michael@0: // found in the LICENSE file. michael@0: michael@0: // Time represents an absolute point in time, internally represented as michael@0: // microseconds (s/1,000,000) since the Windows epoch (1601-01-01 00:00:00 UTC) michael@0: // (See http://crbug.com/14734). System-dependent clock interface routines are michael@0: // defined in time_PLATFORM.cc. michael@0: // michael@0: // TimeDelta represents a duration of time, internally represented in michael@0: // microseconds. michael@0: // michael@0: // TimeTicks represents an abstract time that is most of the time incrementing michael@0: // for use in measuring time durations. It is internally represented in michael@0: // microseconds. It can not be converted to a human-readable time, but is michael@0: // guaranteed not to decrease (if the user changes the computer clock, michael@0: // Time::Now() may actually decrease or jump). But note that TimeTicks may michael@0: // "stand still", for example if the computer suspended. michael@0: // michael@0: // These classes are represented as only a 64-bit value, so they can be michael@0: // efficiently passed by value. michael@0: michael@0: #ifndef BASE_TIME_TIME_H_ michael@0: #define BASE_TIME_TIME_H_ michael@0: michael@0: #include michael@0: michael@0: #include "base/base_export.h" michael@0: #include "base/basictypes.h" michael@0: #include "build/build_config.h" michael@0: michael@0: #if defined(OS_MACOSX) michael@0: #include michael@0: // Avoid Mac system header macro leak. michael@0: #undef TYPE_BOOL michael@0: #endif michael@0: michael@0: #if defined(OS_POSIX) michael@0: #include michael@0: #include michael@0: #endif michael@0: michael@0: #if defined(OS_WIN) michael@0: // For FILETIME in FromFileTime, until it moves to a new converter class. michael@0: // See TODO(iyengar) below. michael@0: #include michael@0: #endif michael@0: michael@0: #include michael@0: michael@0: namespace base { michael@0: michael@0: class Time; michael@0: class TimeTicks; michael@0: michael@0: // TimeDelta ------------------------------------------------------------------ michael@0: michael@0: class BASE_EXPORT TimeDelta { michael@0: public: michael@0: TimeDelta() : delta_(0) { michael@0: } michael@0: michael@0: // Converts units of time to TimeDeltas. michael@0: static TimeDelta FromDays(int64 days); michael@0: static TimeDelta FromHours(int64 hours); michael@0: static TimeDelta FromMinutes(int64 minutes); michael@0: static TimeDelta FromSeconds(int64 secs); michael@0: static TimeDelta FromMilliseconds(int64 ms); michael@0: static TimeDelta FromMicroseconds(int64 us); michael@0: #if defined(OS_WIN) michael@0: static TimeDelta FromQPCValue(LONGLONG qpc_value); michael@0: #endif michael@0: michael@0: // Converts an integer value representing TimeDelta to a class. This is used michael@0: // when deserializing a |TimeDelta| structure, using a value known to be michael@0: // compatible. It is not provided as a constructor because the integer type michael@0: // may be unclear from the perspective of a caller. michael@0: static TimeDelta FromInternalValue(int64 delta) { michael@0: return TimeDelta(delta); michael@0: } michael@0: michael@0: // Returns the internal numeric value of the TimeDelta object. Please don't michael@0: // use this and do arithmetic on it, as it is more error prone than using the michael@0: // provided operators. michael@0: // For serializing, use FromInternalValue to reconstitute. michael@0: int64 ToInternalValue() const { michael@0: return delta_; michael@0: } michael@0: michael@0: #if defined(OS_POSIX) michael@0: struct timespec ToTimeSpec() const; michael@0: #endif michael@0: michael@0: // Returns the time delta in some unit. The F versions return a floating michael@0: // point value, the "regular" versions return a rounded-down value. michael@0: // michael@0: // InMillisecondsRoundedUp() instead returns an integer that is rounded up michael@0: // to the next full millisecond. michael@0: int InDays() const; michael@0: int InHours() const; michael@0: int InMinutes() const; michael@0: double InSecondsF() const; michael@0: int64 InSeconds() const; michael@0: double InMillisecondsF() const; michael@0: int64 InMilliseconds() const; michael@0: int64 InMillisecondsRoundedUp() const; michael@0: int64 InMicroseconds() const; michael@0: michael@0: TimeDelta& operator=(TimeDelta other) { michael@0: delta_ = other.delta_; michael@0: return *this; michael@0: } michael@0: michael@0: // Computations with other deltas. michael@0: TimeDelta operator+(TimeDelta other) const { michael@0: return TimeDelta(delta_ + other.delta_); michael@0: } michael@0: TimeDelta operator-(TimeDelta other) const { michael@0: return TimeDelta(delta_ - other.delta_); michael@0: } michael@0: michael@0: TimeDelta& operator+=(TimeDelta other) { michael@0: delta_ += other.delta_; michael@0: return *this; michael@0: } michael@0: TimeDelta& operator-=(TimeDelta other) { michael@0: delta_ -= other.delta_; michael@0: return *this; michael@0: } michael@0: TimeDelta operator-() const { michael@0: return TimeDelta(-delta_); michael@0: } michael@0: michael@0: // Computations with ints, note that we only allow multiplicative operations michael@0: // with ints, and additive operations with other deltas. michael@0: TimeDelta operator*(int64 a) const { michael@0: return TimeDelta(delta_ * a); michael@0: } michael@0: TimeDelta operator/(int64 a) const { michael@0: return TimeDelta(delta_ / a); michael@0: } michael@0: TimeDelta& operator*=(int64 a) { michael@0: delta_ *= a; michael@0: return *this; michael@0: } michael@0: TimeDelta& operator/=(int64 a) { michael@0: delta_ /= a; michael@0: return *this; michael@0: } michael@0: int64 operator/(TimeDelta a) const { michael@0: return delta_ / a.delta_; michael@0: } michael@0: michael@0: // Defined below because it depends on the definition of the other classes. michael@0: Time operator+(Time t) const; michael@0: TimeTicks operator+(TimeTicks t) const; michael@0: michael@0: // Comparison operators. michael@0: bool operator==(TimeDelta other) const { michael@0: return delta_ == other.delta_; michael@0: } michael@0: bool operator!=(TimeDelta other) const { michael@0: return delta_ != other.delta_; michael@0: } michael@0: bool operator<(TimeDelta other) const { michael@0: return delta_ < other.delta_; michael@0: } michael@0: bool operator<=(TimeDelta other) const { michael@0: return delta_ <= other.delta_; michael@0: } michael@0: bool operator>(TimeDelta other) const { michael@0: return delta_ > other.delta_; michael@0: } michael@0: bool operator>=(TimeDelta other) const { michael@0: return delta_ >= other.delta_; michael@0: } michael@0: michael@0: private: michael@0: friend class Time; michael@0: friend class TimeTicks; michael@0: friend TimeDelta operator*(int64 a, TimeDelta td); michael@0: michael@0: // Constructs a delta given the duration in microseconds. This is private michael@0: // to avoid confusion by callers with an integer constructor. Use michael@0: // FromSeconds, FromMilliseconds, etc. instead. michael@0: explicit TimeDelta(int64 delta_us) : delta_(delta_us) { michael@0: } michael@0: michael@0: // Delta in microseconds. michael@0: int64 delta_; michael@0: }; michael@0: michael@0: inline TimeDelta operator*(int64 a, TimeDelta td) { michael@0: return TimeDelta(a * td.delta_); michael@0: } michael@0: michael@0: // Time ----------------------------------------------------------------------- michael@0: michael@0: // Represents a wall clock time. michael@0: class BASE_EXPORT Time { michael@0: public: michael@0: static const int64 kMillisecondsPerSecond = 1000; michael@0: static const int64 kMicrosecondsPerMillisecond = 1000; michael@0: static const int64 kMicrosecondsPerSecond = kMicrosecondsPerMillisecond * michael@0: kMillisecondsPerSecond; michael@0: static const int64 kMicrosecondsPerMinute = kMicrosecondsPerSecond * 60; michael@0: static const int64 kMicrosecondsPerHour = kMicrosecondsPerMinute * 60; michael@0: static const int64 kMicrosecondsPerDay = kMicrosecondsPerHour * 24; michael@0: static const int64 kMicrosecondsPerWeek = kMicrosecondsPerDay * 7; michael@0: static const int64 kNanosecondsPerMicrosecond = 1000; michael@0: static const int64 kNanosecondsPerSecond = kNanosecondsPerMicrosecond * michael@0: kMicrosecondsPerSecond; michael@0: michael@0: #if !defined(OS_WIN) michael@0: // On Mac & Linux, this value is the delta from the Windows epoch of 1601 to michael@0: // the Posix delta of 1970. This is used for migrating between the old michael@0: // 1970-based epochs to the new 1601-based ones. It should be removed from michael@0: // this global header and put in the platform-specific ones when we remove the michael@0: // migration code. michael@0: static const int64 kWindowsEpochDeltaMicroseconds; michael@0: #endif michael@0: michael@0: // Represents an exploded time that can be formatted nicely. This is kind of michael@0: // like the Win32 SYSTEMTIME structure or the Unix "struct tm" with a few michael@0: // additions and changes to prevent errors. michael@0: struct BASE_EXPORT Exploded { michael@0: int year; // Four digit year "2007" michael@0: int month; // 1-based month (values 1 = January, etc.) michael@0: int day_of_week; // 0-based day of week (0 = Sunday, etc.) michael@0: int day_of_month; // 1-based day of month (1-31) michael@0: int hour; // Hour within the current day (0-23) michael@0: int minute; // Minute within the current hour (0-59) michael@0: int second; // Second within the current minute (0-59 plus leap michael@0: // seconds which may take it up to 60). michael@0: int millisecond; // Milliseconds within the current second (0-999) michael@0: michael@0: // A cursory test for whether the data members are within their michael@0: // respective ranges. A 'true' return value does not guarantee the michael@0: // Exploded value can be successfully converted to a Time value. michael@0: bool HasValidValues() const; michael@0: }; michael@0: michael@0: // Contains the NULL time. Use Time::Now() to get the current time. michael@0: Time() : us_(0) { michael@0: } michael@0: michael@0: // Returns true if the time object has not been initialized. michael@0: bool is_null() const { michael@0: return us_ == 0; michael@0: } michael@0: michael@0: // Returns true if the time object is the maximum time. michael@0: bool is_max() const { michael@0: return us_ == std::numeric_limits::max(); michael@0: } michael@0: michael@0: // Returns the time for epoch in Unix-like system (Jan 1, 1970). michael@0: static Time UnixEpoch(); michael@0: michael@0: // Returns the current time. Watch out, the system might adjust its clock michael@0: // in which case time will actually go backwards. We don't guarantee that michael@0: // times are increasing, or that two calls to Now() won't be the same. michael@0: static Time Now(); michael@0: michael@0: // Returns the maximum time, which should be greater than any reasonable time michael@0: // with which we might compare it. michael@0: static Time Max(); michael@0: michael@0: // Returns the current time. Same as Now() except that this function always michael@0: // uses system time so that there are no discrepancies between the returned michael@0: // time and system time even on virtual environments including our test bot. michael@0: // For timing sensitive unittests, this function should be used. michael@0: static Time NowFromSystemTime(); michael@0: michael@0: // Converts to/from time_t in UTC and a Time class. michael@0: // TODO(brettw) this should be removed once everybody starts using the |Time| michael@0: // class. michael@0: static Time FromTimeT(time_t tt); michael@0: time_t ToTimeT() const; michael@0: michael@0: // Converts time to/from a double which is the number of seconds since epoch michael@0: // (Jan 1, 1970). Webkit uses this format to represent time. michael@0: // Because WebKit initializes double time value to 0 to indicate "not michael@0: // initialized", we map it to empty Time object that also means "not michael@0: // initialized". michael@0: static Time FromDoubleT(double dt); michael@0: double ToDoubleT() const; michael@0: michael@0: #if defined(OS_POSIX) michael@0: // Converts the timespec structure to time. MacOS X 10.8.3 (and tentatively, michael@0: // earlier versions) will have the |ts|'s tv_nsec component zeroed out, michael@0: // having a 1 second resolution, which agrees with michael@0: // https://developer.apple.com/legacy/library/#technotes/tn/tn1150.html#HFSPlusDates. michael@0: static Time FromTimeSpec(const timespec& ts); michael@0: #endif michael@0: michael@0: // Converts to/from the Javascript convention for times, a number of michael@0: // milliseconds since the epoch: michael@0: // https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date/getTime. michael@0: static Time FromJsTime(double ms_since_epoch); michael@0: double ToJsTime() const; michael@0: michael@0: // Converts to Java convention for times, a number of michael@0: // milliseconds since the epoch. michael@0: int64 ToJavaTime() const; michael@0: michael@0: #if defined(OS_POSIX) michael@0: static Time FromTimeVal(struct timeval t); michael@0: struct timeval ToTimeVal() const; michael@0: #endif michael@0: michael@0: #if defined(OS_MACOSX) michael@0: static Time FromCFAbsoluteTime(CFAbsoluteTime t); michael@0: CFAbsoluteTime ToCFAbsoluteTime() const; michael@0: #endif michael@0: michael@0: #if defined(OS_WIN) michael@0: static Time FromFileTime(FILETIME ft); michael@0: FILETIME ToFileTime() const; michael@0: michael@0: // The minimum time of a low resolution timer. This is basically a windows michael@0: // constant of ~15.6ms. While it does vary on some older OS versions, we'll michael@0: // treat it as static across all windows versions. michael@0: static const int kMinLowResolutionThresholdMs = 16; michael@0: michael@0: // Enable or disable Windows high resolution timer. If the high resolution michael@0: // timer is not enabled, calls to ActivateHighResolutionTimer will fail. michael@0: // When disabling the high resolution timer, this function will not cause michael@0: // the high resolution timer to be deactivated, but will prevent future michael@0: // activations. michael@0: // Must be called from the main thread. michael@0: // For more details see comments in time_win.cc. michael@0: static void EnableHighResolutionTimer(bool enable); michael@0: michael@0: // Activates or deactivates the high resolution timer based on the |activate| michael@0: // flag. If the HighResolutionTimer is not Enabled (see michael@0: // EnableHighResolutionTimer), this function will return false. Otherwise michael@0: // returns true. Each successful activate call must be paired with a michael@0: // subsequent deactivate call. michael@0: // All callers to activate the high resolution timer must eventually call michael@0: // this function to deactivate the high resolution timer. michael@0: static bool ActivateHighResolutionTimer(bool activate); michael@0: michael@0: // Returns true if the high resolution timer is both enabled and activated. michael@0: // This is provided for testing only, and is not tracked in a thread-safe michael@0: // way. michael@0: static bool IsHighResolutionTimerInUse(); michael@0: #endif michael@0: michael@0: // Converts an exploded structure representing either the local time or UTC michael@0: // into a Time class. michael@0: static Time FromUTCExploded(const Exploded& exploded) { michael@0: return FromExploded(false, exploded); michael@0: } michael@0: static Time FromLocalExploded(const Exploded& exploded) { michael@0: return FromExploded(true, exploded); michael@0: } michael@0: michael@0: // Converts an integer value representing Time to a class. This is used michael@0: // when deserializing a |Time| structure, using a value known to be michael@0: // compatible. It is not provided as a constructor because the integer type michael@0: // may be unclear from the perspective of a caller. michael@0: static Time FromInternalValue(int64 us) { michael@0: return Time(us); michael@0: } michael@0: michael@0: // Converts a string representation of time to a Time object. michael@0: // An example of a time string which is converted is as below:- michael@0: // "Tue, 15 Nov 1994 12:45:26 GMT". If the timezone is not specified michael@0: // in the input string, FromString assumes local time and FromUTCString michael@0: // assumes UTC. A timezone that cannot be parsed (e.g. "UTC" which is not michael@0: // specified in RFC822) is treated as if the timezone is not specified. michael@0: // TODO(iyengar) Move the FromString/FromTimeT/ToTimeT/FromFileTime to michael@0: // a new time converter class. michael@0: static bool FromString(const char* time_string, Time* parsed_time) { michael@0: return FromStringInternal(time_string, true, parsed_time); michael@0: } michael@0: static bool FromUTCString(const char* time_string, Time* parsed_time) { michael@0: return FromStringInternal(time_string, false, parsed_time); michael@0: } michael@0: michael@0: // For serializing, use FromInternalValue to reconstitute. Please don't use michael@0: // this and do arithmetic on it, as it is more error prone than using the michael@0: // provided operators. michael@0: int64 ToInternalValue() const { michael@0: return us_; michael@0: } michael@0: michael@0: // Fills the given exploded structure with either the local time or UTC from michael@0: // this time structure (containing UTC). michael@0: void UTCExplode(Exploded* exploded) const { michael@0: return Explode(false, exploded); michael@0: } michael@0: void LocalExplode(Exploded* exploded) const { michael@0: return Explode(true, exploded); michael@0: } michael@0: michael@0: // Rounds this time down to the nearest day in local time. It will represent michael@0: // midnight on that day. michael@0: Time LocalMidnight() const; michael@0: michael@0: Time& operator=(Time other) { michael@0: us_ = other.us_; michael@0: return *this; michael@0: } michael@0: michael@0: // Compute the difference between two times. michael@0: TimeDelta operator-(Time other) const { michael@0: return TimeDelta(us_ - other.us_); michael@0: } michael@0: michael@0: // Modify by some time delta. michael@0: Time& operator+=(TimeDelta delta) { michael@0: us_ += delta.delta_; michael@0: return *this; michael@0: } michael@0: Time& operator-=(TimeDelta delta) { michael@0: us_ -= delta.delta_; michael@0: return *this; michael@0: } michael@0: michael@0: // Return a new time modified by some delta. michael@0: Time operator+(TimeDelta delta) const { michael@0: return Time(us_ + delta.delta_); michael@0: } michael@0: Time operator-(TimeDelta delta) const { michael@0: return Time(us_ - delta.delta_); michael@0: } michael@0: michael@0: // Comparison operators michael@0: bool operator==(Time other) const { michael@0: return us_ == other.us_; michael@0: } michael@0: bool operator!=(Time other) const { michael@0: return us_ != other.us_; michael@0: } michael@0: bool operator<(Time other) const { michael@0: return us_ < other.us_; michael@0: } michael@0: bool operator<=(Time other) const { michael@0: return us_ <= other.us_; michael@0: } michael@0: bool operator>(Time other) const { michael@0: return us_ > other.us_; michael@0: } michael@0: bool operator>=(Time other) const { michael@0: return us_ >= other.us_; michael@0: } michael@0: michael@0: private: michael@0: friend class TimeDelta; michael@0: michael@0: explicit Time(int64 us) : us_(us) { michael@0: } michael@0: michael@0: // Explodes the given time to either local time |is_local = true| or UTC michael@0: // |is_local = false|. michael@0: void Explode(bool is_local, Exploded* exploded) const; michael@0: michael@0: // Unexplodes a given time assuming the source is either local time michael@0: // |is_local = true| or UTC |is_local = false|. michael@0: static Time FromExploded(bool is_local, const Exploded& exploded); michael@0: michael@0: // Converts a string representation of time to a Time object. michael@0: // An example of a time string which is converted is as below:- michael@0: // "Tue, 15 Nov 1994 12:45:26 GMT". If the timezone is not specified michael@0: // in the input string, local time |is_local = true| or michael@0: // UTC |is_local = false| is assumed. A timezone that cannot be parsed michael@0: // (e.g. "UTC" which is not specified in RFC822) is treated as if the michael@0: // timezone is not specified. michael@0: static bool FromStringInternal(const char* time_string, michael@0: bool is_local, michael@0: Time* parsed_time); michael@0: michael@0: // The representation of Jan 1, 1970 UTC in microseconds since the michael@0: // platform-dependent epoch. michael@0: static const int64 kTimeTToMicrosecondsOffset; michael@0: michael@0: #if defined(OS_WIN) michael@0: // Indicates whether fast timers are usable right now. For instance, michael@0: // when using battery power, we might elect to prevent high speed timers michael@0: // which would draw more power. michael@0: static bool high_resolution_timer_enabled_; michael@0: // Count of activations on the high resolution timer. Only use in tests michael@0: // which are single threaded. michael@0: static int high_resolution_timer_activated_; michael@0: #endif michael@0: michael@0: // Time in microseconds in UTC. michael@0: int64 us_; michael@0: }; michael@0: michael@0: // Inline the TimeDelta factory methods, for fast TimeDelta construction. michael@0: michael@0: // static michael@0: inline TimeDelta TimeDelta::FromDays(int64 days) { michael@0: return TimeDelta(days * Time::kMicrosecondsPerDay); michael@0: } michael@0: michael@0: // static michael@0: inline TimeDelta TimeDelta::FromHours(int64 hours) { michael@0: return TimeDelta(hours * Time::kMicrosecondsPerHour); michael@0: } michael@0: michael@0: // static michael@0: inline TimeDelta TimeDelta::FromMinutes(int64 minutes) { michael@0: return TimeDelta(minutes * Time::kMicrosecondsPerMinute); michael@0: } michael@0: michael@0: // static michael@0: inline TimeDelta TimeDelta::FromSeconds(int64 secs) { michael@0: return TimeDelta(secs * Time::kMicrosecondsPerSecond); michael@0: } michael@0: michael@0: // static michael@0: inline TimeDelta TimeDelta::FromMilliseconds(int64 ms) { michael@0: return TimeDelta(ms * Time::kMicrosecondsPerMillisecond); michael@0: } michael@0: michael@0: // static michael@0: inline TimeDelta TimeDelta::FromMicroseconds(int64 us) { michael@0: return TimeDelta(us); michael@0: } michael@0: michael@0: inline Time TimeDelta::operator+(Time t) const { michael@0: return Time(t.us_ + delta_); michael@0: } michael@0: michael@0: // TimeTicks ------------------------------------------------------------------ michael@0: michael@0: class BASE_EXPORT TimeTicks { michael@0: public: michael@0: TimeTicks() : ticks_(0) { michael@0: } michael@0: michael@0: // Platform-dependent tick count representing "right now." michael@0: // The resolution of this clock is ~1-15ms. Resolution varies depending michael@0: // on hardware/operating system configuration. michael@0: static TimeTicks Now(); michael@0: michael@0: // Returns a platform-dependent high-resolution tick count. Implementation michael@0: // is hardware dependent and may or may not return sub-millisecond michael@0: // resolution. THIS CALL IS GENERALLY MUCH MORE EXPENSIVE THAN Now() AND michael@0: // SHOULD ONLY BE USED WHEN IT IS REALLY NEEDED. michael@0: static TimeTicks HighResNow(); michael@0: michael@0: // Returns true if ThreadNow() is supported on this system. michael@0: static bool IsThreadNowSupported() { michael@0: #if defined(_POSIX_THREAD_CPUTIME) && (_POSIX_THREAD_CPUTIME >= 0) michael@0: return true; michael@0: #else michael@0: return false; michael@0: #endif michael@0: } michael@0: michael@0: // Returns thread-specific CPU-time on systems that support this feature. michael@0: // Needs to be guarded with a call to IsThreadNowSupported(). Use this timer michael@0: // to (approximately) measure how much time the calling thread spent doing michael@0: // actual work vs. being de-scheduled. May return bogus results if the thread michael@0: // migrates to another CPU between two calls. michael@0: static TimeTicks ThreadNow(); michael@0: michael@0: // Returns the current system trace time or, if none is defined, the current michael@0: // high-res time (i.e. HighResNow()). On systems where a global trace clock michael@0: // is defined, timestamping TraceEvents's with this value guarantees michael@0: // synchronization between events collected inside chrome and events michael@0: // collected outside (e.g. kernel, X server). michael@0: static TimeTicks NowFromSystemTraceTime(); michael@0: michael@0: #if defined(OS_WIN) michael@0: // Get the absolute value of QPC time drift. For testing. michael@0: static int64 GetQPCDriftMicroseconds(); michael@0: michael@0: static TimeTicks FromQPCValue(LONGLONG qpc_value); michael@0: michael@0: // Returns true if the high resolution clock is working on this system. michael@0: // This is only for testing. michael@0: static bool IsHighResClockWorking(); michael@0: michael@0: // Enable high resolution time for TimeTicks::Now(). This function will michael@0: // test for the availability of a working implementation of michael@0: // QueryPerformanceCounter(). If one is not available, this function does michael@0: // nothing and the resolution of Now() remains 1ms. Otherwise, all future michael@0: // calls to TimeTicks::Now() will have the higher resolution provided by QPC. michael@0: // Returns true if high resolution time was successfully enabled. michael@0: static bool SetNowIsHighResNowIfSupported(); michael@0: michael@0: // Returns a time value that is NOT rollover protected. michael@0: static TimeTicks UnprotectedNow(); michael@0: #endif michael@0: michael@0: // Returns true if this object has not been initialized. michael@0: bool is_null() const { michael@0: return ticks_ == 0; michael@0: } michael@0: michael@0: // Converts an integer value representing TimeTicks to a class. This is used michael@0: // when deserializing a |TimeTicks| structure, using a value known to be michael@0: // compatible. It is not provided as a constructor because the integer type michael@0: // may be unclear from the perspective of a caller. michael@0: static TimeTicks FromInternalValue(int64 ticks) { michael@0: return TimeTicks(ticks); michael@0: } michael@0: michael@0: // Returns the internal numeric value of the TimeTicks object. michael@0: // For serializing, use FromInternalValue to reconstitute. michael@0: int64 ToInternalValue() const { michael@0: return ticks_; michael@0: } michael@0: michael@0: TimeTicks& operator=(TimeTicks other) { michael@0: ticks_ = other.ticks_; michael@0: return *this; michael@0: } michael@0: michael@0: // Compute the difference between two times. michael@0: TimeDelta operator-(TimeTicks other) const { michael@0: return TimeDelta(ticks_ - other.ticks_); michael@0: } michael@0: michael@0: // Modify by some time delta. michael@0: TimeTicks& operator+=(TimeDelta delta) { michael@0: ticks_ += delta.delta_; michael@0: return *this; michael@0: } michael@0: TimeTicks& operator-=(TimeDelta delta) { michael@0: ticks_ -= delta.delta_; michael@0: return *this; michael@0: } michael@0: michael@0: // Return a new TimeTicks modified by some delta. michael@0: TimeTicks operator+(TimeDelta delta) const { michael@0: return TimeTicks(ticks_ + delta.delta_); michael@0: } michael@0: TimeTicks operator-(TimeDelta delta) const { michael@0: return TimeTicks(ticks_ - delta.delta_); michael@0: } michael@0: michael@0: // Comparison operators michael@0: bool operator==(TimeTicks other) const { michael@0: return ticks_ == other.ticks_; michael@0: } michael@0: bool operator!=(TimeTicks other) const { michael@0: return ticks_ != other.ticks_; michael@0: } michael@0: bool operator<(TimeTicks other) const { michael@0: return ticks_ < other.ticks_; michael@0: } michael@0: bool operator<=(TimeTicks other) const { michael@0: return ticks_ <= other.ticks_; michael@0: } michael@0: bool operator>(TimeTicks other) const { michael@0: return ticks_ > other.ticks_; michael@0: } michael@0: bool operator>=(TimeTicks other) const { michael@0: return ticks_ >= other.ticks_; michael@0: } michael@0: michael@0: protected: michael@0: friend class TimeDelta; michael@0: michael@0: // Please use Now() to create a new object. This is for internal use michael@0: // and testing. Ticks is in microseconds. michael@0: explicit TimeTicks(int64 ticks) : ticks_(ticks) { michael@0: } michael@0: michael@0: // Tick count in microseconds. michael@0: int64 ticks_; michael@0: michael@0: #if defined(OS_WIN) michael@0: typedef DWORD (*TickFunctionType)(void); michael@0: static TickFunctionType SetMockTickFunction(TickFunctionType ticker); michael@0: #endif michael@0: }; michael@0: michael@0: inline TimeTicks TimeDelta::operator+(TimeTicks t) const { michael@0: return TimeTicks(t.ticks_ + delta_); michael@0: } michael@0: michael@0: } // namespace base michael@0: michael@0: #endif // BASE_TIME_TIME_H_