ipc/chromium/src/base/time.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/ipc/chromium/src/base/time.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,484 @@
     1.4 +// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
     1.5 +// Use of this source code is governed by a BSD-style license that can be
     1.6 +// found in the LICENSE file.
     1.7 +
     1.8 +// Time represents an absolute point in time, internally represented as
     1.9 +// microseconds (s/1,000,000) since a platform-dependent epoch.  Each
    1.10 +// platform's epoch, along with other system-dependent clock interface
    1.11 +// routines, is defined in time_PLATFORM.cc.
    1.12 +//
    1.13 +// TimeDelta represents a duration of time, internally represented in
    1.14 +// microseconds.
    1.15 +//
    1.16 +// TimeTicks represents an abstract time that is always incrementing for use
    1.17 +// in measuring time durations. It is internally represented in microseconds.
    1.18 +// It can not be converted to a human-readable time, but is guaranteed not to
    1.19 +// decrease (if the user changes the computer clock, Time::Now() may actually
    1.20 +// decrease or jump).
    1.21 +//
    1.22 +// These classes are represented as only a 64-bit value, so they can be
    1.23 +// efficiently passed by value.
    1.24 +
    1.25 +#ifndef BASE_TIME_H_
    1.26 +#define BASE_TIME_H_
    1.27 +
    1.28 +#include <time.h>
    1.29 +
    1.30 +#include "base/basictypes.h"
    1.31 +
    1.32 +#if defined(OS_WIN)
    1.33 +// For FILETIME in FromFileTime, until it moves to a new converter class.
    1.34 +// See TODO(iyengar) below.
    1.35 +#include <windows.h>
    1.36 +#endif
    1.37 +
    1.38 +namespace base {
    1.39 +
    1.40 +class Time;
    1.41 +class TimeTicks;
    1.42 +
    1.43 +// This unit test does a lot of manual time manipulation.
    1.44 +class PageLoadTrackerUnitTest;
    1.45 +
    1.46 +// TimeDelta ------------------------------------------------------------------
    1.47 +
    1.48 +class TimeDelta {
    1.49 + public:
    1.50 +  TimeDelta() : delta_(0) {
    1.51 +  }
    1.52 +
    1.53 +  // Converts units of time to TimeDeltas.
    1.54 +  static TimeDelta FromDays(int64_t days);
    1.55 +  static TimeDelta FromHours(int64_t hours);
    1.56 +  static TimeDelta FromMinutes(int64_t minutes);
    1.57 +  static TimeDelta FromSeconds(int64_t secs);
    1.58 +  static TimeDelta FromMilliseconds(int64_t ms);
    1.59 +  static TimeDelta FromMicroseconds(int64_t us);
    1.60 +
    1.61 +  // Returns the internal numeric value of the TimeDelta object. Please don't
    1.62 +  // use this and do arithmetic on it, as it is more error prone than using the
    1.63 +  // provided operators.
    1.64 +  int64_t ToInternalValue() const {
    1.65 +    return delta_;
    1.66 +  }
    1.67 +
    1.68 +  // Returns the time delta in some unit. The F versions return a floating
    1.69 +  // point value, the "regular" versions return a rounded-down value.
    1.70 +  int InDays() const;
    1.71 +  int InHours() const;
    1.72 +  int InMinutes() const;
    1.73 +  double InSecondsF() const;
    1.74 +  int64_t InSeconds() const;
    1.75 +  double InMillisecondsF() const;
    1.76 +  int64_t InMilliseconds() const;
    1.77 +  int64_t InMicroseconds() const;
    1.78 +
    1.79 +  TimeDelta& operator=(TimeDelta other) {
    1.80 +    delta_ = other.delta_;
    1.81 +    return *this;
    1.82 +  }
    1.83 +
    1.84 +  // Computations with other deltas.
    1.85 +  TimeDelta operator+(TimeDelta other) const {
    1.86 +    return TimeDelta(delta_ + other.delta_);
    1.87 +  }
    1.88 +  TimeDelta operator-(TimeDelta other) const {
    1.89 +    return TimeDelta(delta_ - other.delta_);
    1.90 +  }
    1.91 +
    1.92 +  TimeDelta& operator+=(TimeDelta other) {
    1.93 +    delta_ += other.delta_;
    1.94 +    return *this;
    1.95 +  }
    1.96 +  TimeDelta& operator-=(TimeDelta other) {
    1.97 +    delta_ -= other.delta_;
    1.98 +    return *this;
    1.99 +  }
   1.100 +  TimeDelta operator-() const {
   1.101 +    return TimeDelta(-delta_);
   1.102 +  }
   1.103 +
   1.104 +  // Computations with ints, note that we only allow multiplicative operations
   1.105 +  // with ints, and additive operations with other deltas.
   1.106 +  TimeDelta operator*(int64_t a) const {
   1.107 +    return TimeDelta(delta_ * a);
   1.108 +  }
   1.109 +  TimeDelta operator/(int64_t a) const {
   1.110 +    return TimeDelta(delta_ / a);
   1.111 +  }
   1.112 +  TimeDelta& operator*=(int64_t a) {
   1.113 +    delta_ *= a;
   1.114 +    return *this;
   1.115 +  }
   1.116 +  TimeDelta& operator/=(int64_t a) {
   1.117 +    delta_ /= a;
   1.118 +    return *this;
   1.119 +  }
   1.120 +  int64_t operator/(TimeDelta a) const {
   1.121 +    return delta_ / a.delta_;
   1.122 +  }
   1.123 +
   1.124 +  // Defined below because it depends on the definition of the other classes.
   1.125 +  Time operator+(Time t) const;
   1.126 +  TimeTicks operator+(TimeTicks t) const;
   1.127 +
   1.128 +  // Comparison operators.
   1.129 +  bool operator==(TimeDelta other) const {
   1.130 +    return delta_ == other.delta_;
   1.131 +  }
   1.132 +  bool operator!=(TimeDelta other) const {
   1.133 +    return delta_ != other.delta_;
   1.134 +  }
   1.135 +  bool operator<(TimeDelta other) const {
   1.136 +    return delta_ < other.delta_;
   1.137 +  }
   1.138 +  bool operator<=(TimeDelta other) const {
   1.139 +    return delta_ <= other.delta_;
   1.140 +  }
   1.141 +  bool operator>(TimeDelta other) const {
   1.142 +    return delta_ > other.delta_;
   1.143 +  }
   1.144 +  bool operator>=(TimeDelta other) const {
   1.145 +    return delta_ >= other.delta_;
   1.146 +  }
   1.147 +
   1.148 + private:
   1.149 +  friend class Time;
   1.150 +  friend class TimeTicks;
   1.151 +  friend TimeDelta operator*(int64_t a, TimeDelta td);
   1.152 +
   1.153 +  // Constructs a delta given the duration in microseconds. This is private
   1.154 +  // to avoid confusion by callers with an integer constructor. Use
   1.155 +  // FromSeconds, FromMilliseconds, etc. instead.
   1.156 +  explicit TimeDelta(int64_t delta_us) : delta_(delta_us) {
   1.157 +  }
   1.158 +
   1.159 +  // Delta in microseconds.
   1.160 +  int64_t delta_;
   1.161 +};
   1.162 +
   1.163 +inline TimeDelta operator*(int64_t a, TimeDelta td) {
   1.164 +  return TimeDelta(a * td.delta_);
   1.165 +}
   1.166 +
   1.167 +// Time -----------------------------------------------------------------------
   1.168 +
   1.169 +// Represents a wall clock time.
   1.170 +class Time {
   1.171 + public:
   1.172 +  static const int64_t kMillisecondsPerSecond = 1000;
   1.173 +  static const int64_t kMicrosecondsPerMillisecond = 1000;
   1.174 +  static const int64_t kMicrosecondsPerSecond = kMicrosecondsPerMillisecond *
   1.175 +                                              kMillisecondsPerSecond;
   1.176 +  static const int64_t kMicrosecondsPerMinute = kMicrosecondsPerSecond * 60;
   1.177 +  static const int64_t kMicrosecondsPerHour = kMicrosecondsPerMinute * 60;
   1.178 +  static const int64_t kMicrosecondsPerDay = kMicrosecondsPerHour * 24;
   1.179 +  static const int64_t kMicrosecondsPerWeek = kMicrosecondsPerDay * 7;
   1.180 +  static const int64_t kNanosecondsPerMicrosecond = 1000;
   1.181 +  static const int64_t kNanosecondsPerSecond = kNanosecondsPerMicrosecond *
   1.182 +                                             kMicrosecondsPerSecond;
   1.183 +
   1.184 +  // Represents an exploded time that can be formatted nicely. This is kind of
   1.185 +  // like the Win32 SYSTEMTIME structure or the Unix "struct tm" with a few
   1.186 +  // additions and changes to prevent errors.
   1.187 +  struct Exploded {
   1.188 +    int year;                 // Four digit year "2007"
   1.189 +    signed char month;        // 1-based month (values 1 = January, etc.)
   1.190 +    signed char day_of_week;  // 0-based day of week (0 = Sunday, etc.)
   1.191 +    signed char day_of_month; // 1-based day of month (1-31)
   1.192 +    signed char hour;         // Hour within the current day (0-23)
   1.193 +    signed char minute;       // Minute within the current hour (0-59)
   1.194 +    signed char second;       // Second within the current minute (0-59 plus
   1.195 +                              // leap seconds which may take it up to 60).
   1.196 +    int millisecond;          // Milliseconds within the current second (0-999)
   1.197 +  };
   1.198 +
   1.199 +  // Contains the NULL time. Use Time::Now() to get the current time.
   1.200 +  explicit Time() : us_(0) {
   1.201 +  }
   1.202 +
   1.203 +  // Returns true if the time object has not been initialized.
   1.204 +  bool is_null() const {
   1.205 +    return us_ == 0;
   1.206 +  }
   1.207 +
   1.208 +  // Returns the current time. Watch out, the system might adjust its clock
   1.209 +  // in which case time will actually go backwards. We don't guarantee that
   1.210 +  // times are increasing, or that two calls to Now() won't be the same.
   1.211 +  static Time Now();
   1.212 +
   1.213 +  // Returns the current time. Same as Now() except that this function always
   1.214 +  // uses system time so that there are no discrepancies between the returned
   1.215 +  // time and system time even on virtual environments including our test bot.
   1.216 +  // For timing sensitive unittests, this function should be used.
   1.217 +  static Time NowFromSystemTime();
   1.218 +
   1.219 +  // Converts to/from time_t in UTC and a Time class.
   1.220 +  // TODO(brettw) this should be removed once everybody starts using the |Time|
   1.221 +  // class.
   1.222 +  static Time FromTimeT(time_t tt);
   1.223 +  time_t ToTimeT() const;
   1.224 +
   1.225 +  // Converts time to/from a double which is the number of seconds since epoch
   1.226 +  // (Jan 1, 1970).  Webkit uses this format to represent time.
   1.227 +  static Time FromDoubleT(double dt);
   1.228 +  double ToDoubleT() const;
   1.229 +
   1.230 +
   1.231 +#if defined(OS_WIN)
   1.232 +  static Time FromFileTime(FILETIME ft);
   1.233 +  FILETIME ToFileTime() const;
   1.234 +#endif
   1.235 +
   1.236 +  // Converts an exploded structure representing either the local time or UTC
   1.237 +  // into a Time class.
   1.238 +  static Time FromUTCExploded(const Exploded& exploded) {
   1.239 +    return FromExploded(false, exploded);
   1.240 +  }
   1.241 +  static Time FromLocalExploded(const Exploded& exploded) {
   1.242 +    return FromExploded(true, exploded);
   1.243 +  }
   1.244 +
   1.245 +  // Converts an integer value representing Time to a class. This is used
   1.246 +  // when deserializing a |Time| structure, using a value known to be
   1.247 +  // compatible. It is not provided as a constructor because the integer type
   1.248 +  // may be unclear from the perspective of a caller.
   1.249 +  static Time FromInternalValue(int64_t us) {
   1.250 +    return Time(us);
   1.251 +  }
   1.252 +
   1.253 +  // Converts a string representation of time to a Time object.
   1.254 +  // An example of a time string which is converted is as below:-
   1.255 +  // "Tue, 15 Nov 1994 12:45:26 GMT". If the timezone is not specified
   1.256 +  // in the input string, we assume local time.
   1.257 +  // TODO(iyengar) Move the FromString/FromTimeT/ToTimeT/FromFileTime to
   1.258 +  // a new time converter class.
   1.259 +  static bool FromString(const wchar_t* time_string, Time* parsed_time);
   1.260 +
   1.261 +  // For serializing, use FromInternalValue to reconstitute. Please don't use
   1.262 +  // this and do arithmetic on it, as it is more error prone than using the
   1.263 +  // provided operators.
   1.264 +  int64_t ToInternalValue() const {
   1.265 +    return us_;
   1.266 +  }
   1.267 +
   1.268 +  // Fills the given exploded structure with either the local time or UTC from
   1.269 +  // this time structure (containing UTC).
   1.270 +  void UTCExplode(Exploded* exploded) const {
   1.271 +    return Explode(false, exploded);
   1.272 +  }
   1.273 +  void LocalExplode(Exploded* exploded) const {
   1.274 +    return Explode(true, exploded);
   1.275 +  }
   1.276 +
   1.277 +  // Rounds this time down to the nearest day in local time. It will represent
   1.278 +  // midnight on that day.
   1.279 +  Time LocalMidnight() const;
   1.280 +
   1.281 +  Time& operator=(Time other) {
   1.282 +    us_ = other.us_;
   1.283 +    return *this;
   1.284 +  }
   1.285 +
   1.286 +  // Compute the difference between two times.
   1.287 +  TimeDelta operator-(Time other) const {
   1.288 +    return TimeDelta(us_ - other.us_);
   1.289 +  }
   1.290 +
   1.291 +  // Modify by some time delta.
   1.292 +  Time& operator+=(TimeDelta delta) {
   1.293 +    us_ += delta.delta_;
   1.294 +    return *this;
   1.295 +  }
   1.296 +  Time& operator-=(TimeDelta delta) {
   1.297 +    us_ -= delta.delta_;
   1.298 +    return *this;
   1.299 +  }
   1.300 +
   1.301 +  // Return a new time modified by some delta.
   1.302 +  Time operator+(TimeDelta delta) const {
   1.303 +    return us_ + delta.delta_;
   1.304 +  }
   1.305 +  Time operator-(TimeDelta delta) const {
   1.306 +    return us_ - delta.delta_;
   1.307 +  }
   1.308 +
   1.309 +  // Comparison operators
   1.310 +  bool operator==(Time other) const {
   1.311 +    return us_ == other.us_;
   1.312 +  }
   1.313 +  bool operator!=(Time other) const {
   1.314 +    return us_ != other.us_;
   1.315 +  }
   1.316 +  bool operator<(Time other) const {
   1.317 +    return us_ < other.us_;
   1.318 +  }
   1.319 +  bool operator<=(Time other) const {
   1.320 +    return us_ <= other.us_;
   1.321 +  }
   1.322 +  bool operator>(Time other) const {
   1.323 +    return us_ > other.us_;
   1.324 +  }
   1.325 +  bool operator>=(Time other) const {
   1.326 +    return us_ >= other.us_;
   1.327 +  }
   1.328 +
   1.329 + private:
   1.330 +  friend class TimeDelta;
   1.331 +
   1.332 +  // Explodes the given time to either local time |is_local = true| or UTC
   1.333 +  // |is_local = false|.
   1.334 +  void Explode(bool is_local, Exploded* exploded) const;
   1.335 +
   1.336 +  // Unexplodes a given time assuming the source is either local time
   1.337 +  // |is_local = true| or UTC |is_local = false|.
   1.338 +  static Time FromExploded(bool is_local, const Exploded& exploded);
   1.339 +
   1.340 +  Time(int64_t us) : us_(us) {
   1.341 +  }
   1.342 +
   1.343 +  // The representation of Jan 1, 1970 UTC in microseconds since the
   1.344 +  // platform-dependent epoch.
   1.345 +  static const int64_t kTimeTToMicrosecondsOffset;
   1.346 +
   1.347 +  // Time in microseconds in UTC.
   1.348 +  int64_t us_;
   1.349 +};
   1.350 +
   1.351 +inline Time TimeDelta::operator+(Time t) const {
   1.352 +  return Time(t.us_ + delta_);
   1.353 +}
   1.354 +
   1.355 +// Inline the TimeDelta factory methods, for fast TimeDelta construction.
   1.356 +
   1.357 +// static
   1.358 +inline TimeDelta TimeDelta::FromDays(int64_t days) {
   1.359 +  return TimeDelta(days * Time::kMicrosecondsPerDay);
   1.360 +}
   1.361 +
   1.362 +// static
   1.363 +inline TimeDelta TimeDelta::FromHours(int64_t hours) {
   1.364 +  return TimeDelta(hours * Time::kMicrosecondsPerHour);
   1.365 +}
   1.366 +
   1.367 +// static
   1.368 +inline TimeDelta TimeDelta::FromMinutes(int64_t minutes) {
   1.369 +  return TimeDelta(minutes * Time::kMicrosecondsPerMinute);
   1.370 +}
   1.371 +
   1.372 +// static
   1.373 +inline TimeDelta TimeDelta::FromSeconds(int64_t secs) {
   1.374 +  return TimeDelta(secs * Time::kMicrosecondsPerSecond);
   1.375 +}
   1.376 +
   1.377 +// static
   1.378 +inline TimeDelta TimeDelta::FromMilliseconds(int64_t ms) {
   1.379 +  return TimeDelta(ms * Time::kMicrosecondsPerMillisecond);
   1.380 +}
   1.381 +
   1.382 +// static
   1.383 +inline TimeDelta TimeDelta::FromMicroseconds(int64_t us) {
   1.384 +  return TimeDelta(us);
   1.385 +}
   1.386 +
   1.387 +// TimeTicks ------------------------------------------------------------------
   1.388 +
   1.389 +class TimeTicks {
   1.390 + public:
   1.391 +  TimeTicks() : ticks_(0) {
   1.392 +  }
   1.393 +
   1.394 +  // Platform-dependent tick count representing "right now."
   1.395 +  // The resolution of this clock is ~1-15ms.  Resolution varies depending
   1.396 +  // on hardware/operating system configuration.
   1.397 +  static TimeTicks Now();
   1.398 +
   1.399 +  // Returns a platform-dependent high-resolution tick count. Implementation
   1.400 +  // is hardware dependent and may or may not return sub-millisecond
   1.401 +  // resolution.  THIS CALL IS GENERALLY MUCH MORE EXPENSIVE THAN Now() AND
   1.402 +  // SHOULD ONLY BE USED WHEN IT IS REALLY NEEDED.
   1.403 +  static TimeTicks HighResNow();
   1.404 +
   1.405 +  // Returns true if this object has not been initialized.
   1.406 +  bool is_null() const {
   1.407 +    return ticks_ == 0;
   1.408 +  }
   1.409 +
   1.410 +  // Returns the internal numeric value of the TimeTicks object.
   1.411 +  int64_t ToInternalValue() const {
   1.412 +    return ticks_;
   1.413 +  }
   1.414 +
   1.415 +  TimeTicks& operator=(TimeTicks other) {
   1.416 +    ticks_ = other.ticks_;
   1.417 +    return *this;
   1.418 +  }
   1.419 +
   1.420 +  // Compute the difference between two times.
   1.421 +  TimeDelta operator-(TimeTicks other) const {
   1.422 +    return TimeDelta(ticks_ - other.ticks_);
   1.423 +  }
   1.424 +
   1.425 +  // Modify by some time delta.
   1.426 +  TimeTicks& operator+=(TimeDelta delta) {
   1.427 +    ticks_ += delta.delta_;
   1.428 +    return *this;
   1.429 +  }
   1.430 +  TimeTicks& operator-=(TimeDelta delta) {
   1.431 +    ticks_ -= delta.delta_;
   1.432 +    return *this;
   1.433 +  }
   1.434 +
   1.435 +  // Return a new TimeTicks modified by some delta.
   1.436 +  TimeTicks operator+(TimeDelta delta) const {
   1.437 +    return TimeTicks(ticks_ + delta.delta_);
   1.438 +  }
   1.439 +  TimeTicks operator-(TimeDelta delta) const {
   1.440 +    return TimeTicks(ticks_ - delta.delta_);
   1.441 +  }
   1.442 +
   1.443 +  // Comparison operators
   1.444 +  bool operator==(TimeTicks other) const {
   1.445 +    return ticks_ == other.ticks_;
   1.446 +  }
   1.447 +  bool operator!=(TimeTicks other) const {
   1.448 +    return ticks_ != other.ticks_;
   1.449 +  }
   1.450 +  bool operator<(TimeTicks other) const {
   1.451 +    return ticks_ < other.ticks_;
   1.452 +  }
   1.453 +  bool operator<=(TimeTicks other) const {
   1.454 +    return ticks_ <= other.ticks_;
   1.455 +  }
   1.456 +  bool operator>(TimeTicks other) const {
   1.457 +    return ticks_ > other.ticks_;
   1.458 +  }
   1.459 +  bool operator>=(TimeTicks other) const {
   1.460 +    return ticks_ >= other.ticks_;
   1.461 +  }
   1.462 +
   1.463 + protected:
   1.464 +  friend class TimeDelta;
   1.465 +  friend class PageLoadTrackerUnitTest;
   1.466 +
   1.467 +  // Please use Now() to create a new object. This is for internal use
   1.468 +  // and testing. Ticks is in microseconds.
   1.469 +  explicit TimeTicks(int64_t ticks) : ticks_(ticks) {
   1.470 +  }
   1.471 +
   1.472 +  // Tick count in microseconds.
   1.473 +  int64_t ticks_;
   1.474 +
   1.475 +#if defined(OS_WIN)
   1.476 +  typedef DWORD (*TickFunctionType)(void);
   1.477 +  static TickFunctionType SetMockTickFunction(TickFunctionType ticker);
   1.478 +#endif
   1.479 +};
   1.480 +
   1.481 +inline TimeTicks TimeDelta::operator+(TimeTicks t) const {
   1.482 +  return TimeTicks(t.ticks_ + delta_);
   1.483 +}
   1.484 +
   1.485 +}  // namespace base
   1.486 +
   1.487 +#endif  // BASE_TIME_H_

mercurial