michael@0: // Copyright (c) 2006-2008 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 a platform-dependent epoch. Each michael@0: // platform's epoch, along with other system-dependent clock interface michael@0: // routines, is 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 always incrementing for use michael@0: // in measuring time durations. It is internally represented in microseconds. michael@0: // It can not be converted to a human-readable time, but is guaranteed not to michael@0: // decrease (if the user changes the computer clock, Time::Now() may actually michael@0: // decrease or jump). 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_H_ michael@0: #define BASE_TIME_H_ michael@0: michael@0: #include michael@0: michael@0: #include "base/basictypes.h" 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: namespace base { michael@0: michael@0: class Time; michael@0: class TimeTicks; michael@0: michael@0: // This unit test does a lot of manual time manipulation. michael@0: class PageLoadTrackerUnitTest; michael@0: michael@0: // TimeDelta ------------------------------------------------------------------ michael@0: michael@0: class 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_t days); michael@0: static TimeDelta FromHours(int64_t hours); michael@0: static TimeDelta FromMinutes(int64_t minutes); michael@0: static TimeDelta FromSeconds(int64_t secs); michael@0: static TimeDelta FromMilliseconds(int64_t ms); michael@0: static TimeDelta FromMicroseconds(int64_t us); 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: int64_t ToInternalValue() const { michael@0: return delta_; michael@0: } 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: int InDays() const; michael@0: int InHours() const; michael@0: int InMinutes() const; michael@0: double InSecondsF() const; michael@0: int64_t InSeconds() const; michael@0: double InMillisecondsF() const; michael@0: int64_t InMilliseconds() const; michael@0: int64_t 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_t a) const { michael@0: return TimeDelta(delta_ * a); michael@0: } michael@0: TimeDelta operator/(int64_t a) const { michael@0: return TimeDelta(delta_ / a); michael@0: } michael@0: TimeDelta& operator*=(int64_t a) { michael@0: delta_ *= a; michael@0: return *this; michael@0: } michael@0: TimeDelta& operator/=(int64_t a) { michael@0: delta_ /= a; michael@0: return *this; michael@0: } michael@0: int64_t 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_t 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_t delta_us) : delta_(delta_us) { michael@0: } michael@0: michael@0: // Delta in microseconds. michael@0: int64_t delta_; michael@0: }; michael@0: michael@0: inline TimeDelta operator*(int64_t 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 Time { michael@0: public: michael@0: static const int64_t kMillisecondsPerSecond = 1000; michael@0: static const int64_t kMicrosecondsPerMillisecond = 1000; michael@0: static const int64_t kMicrosecondsPerSecond = kMicrosecondsPerMillisecond * michael@0: kMillisecondsPerSecond; michael@0: static const int64_t kMicrosecondsPerMinute = kMicrosecondsPerSecond * 60; michael@0: static const int64_t kMicrosecondsPerHour = kMicrosecondsPerMinute * 60; michael@0: static const int64_t kMicrosecondsPerDay = kMicrosecondsPerHour * 24; michael@0: static const int64_t kMicrosecondsPerWeek = kMicrosecondsPerDay * 7; michael@0: static const int64_t kNanosecondsPerMicrosecond = 1000; michael@0: static const int64_t kNanosecondsPerSecond = kNanosecondsPerMicrosecond * michael@0: kMicrosecondsPerSecond; 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 Exploded { michael@0: int year; // Four digit year "2007" michael@0: signed char month; // 1-based month (values 1 = January, etc.) michael@0: signed char day_of_week; // 0-based day of week (0 = Sunday, etc.) michael@0: signed char day_of_month; // 1-based day of month (1-31) michael@0: signed char hour; // Hour within the current day (0-23) michael@0: signed char minute; // Minute within the current hour (0-59) michael@0: signed char second; // Second within the current minute (0-59 plus michael@0: // leap seconds which may take it up to 60). michael@0: int millisecond; // Milliseconds within the current second (0-999) michael@0: }; michael@0: michael@0: // Contains the NULL time. Use Time::Now() to get the current time. michael@0: explicit 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 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 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: static Time FromDoubleT(double dt); michael@0: double ToDoubleT() const; michael@0: michael@0: michael@0: #if defined(OS_WIN) michael@0: static Time FromFileTime(FILETIME ft); michael@0: FILETIME ToFileTime() const; 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_t 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, we assume local time. michael@0: // TODO(iyengar) Move the FromString/FromTimeT/ToTimeT/FromFileTime to michael@0: // a new time converter class. michael@0: static bool FromString(const wchar_t* time_string, Time* parsed_time); 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_t 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 us_ + delta.delta_; michael@0: } michael@0: Time operator-(TimeDelta delta) const { michael@0: return 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: // 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: Time(int64_t us) : us_(us) { michael@0: } 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_t kTimeTToMicrosecondsOffset; michael@0: michael@0: // Time in microseconds in UTC. michael@0: int64_t 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: // Inline the TimeDelta factory methods, for fast TimeDelta construction. michael@0: michael@0: // static michael@0: inline TimeDelta TimeDelta::FromDays(int64_t days) { michael@0: return TimeDelta(days * Time::kMicrosecondsPerDay); michael@0: } michael@0: michael@0: // static michael@0: inline TimeDelta TimeDelta::FromHours(int64_t hours) { michael@0: return TimeDelta(hours * Time::kMicrosecondsPerHour); michael@0: } michael@0: michael@0: // static michael@0: inline TimeDelta TimeDelta::FromMinutes(int64_t minutes) { michael@0: return TimeDelta(minutes * Time::kMicrosecondsPerMinute); michael@0: } michael@0: michael@0: // static michael@0: inline TimeDelta TimeDelta::FromSeconds(int64_t secs) { michael@0: return TimeDelta(secs * Time::kMicrosecondsPerSecond); michael@0: } michael@0: michael@0: // static michael@0: inline TimeDelta TimeDelta::FromMilliseconds(int64_t ms) { michael@0: return TimeDelta(ms * Time::kMicrosecondsPerMillisecond); michael@0: } michael@0: michael@0: // static michael@0: inline TimeDelta TimeDelta::FromMicroseconds(int64_t us) { michael@0: return TimeDelta(us); michael@0: } michael@0: michael@0: // TimeTicks ------------------------------------------------------------------ michael@0: michael@0: class 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 this object has not been initialized. michael@0: bool is_null() const { michael@0: return ticks_ == 0; michael@0: } michael@0: michael@0: // Returns the internal numeric value of the TimeTicks object. michael@0: int64_t 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: friend class PageLoadTrackerUnitTest; 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_t ticks) : ticks_(ticks) { michael@0: } michael@0: michael@0: // Tick count in microseconds. michael@0: int64_t 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_H_