security/sandbox/chromium/base/time/time.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/security/sandbox/chromium/base/time/time.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,682 @@
     1.4 +// Copyright (c) 2012 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 the Windows epoch (1601-01-01 00:00:00 UTC)
    1.10 +// (See http://crbug.com/14734).  System-dependent clock interface routines are
    1.11 +// 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 most of the time incrementing
    1.17 +// for use in measuring time durations. It is internally represented in
    1.18 +// microseconds.  It can not be converted to a human-readable time, but is
    1.19 +// guaranteed not to decrease (if the user changes the computer clock,
    1.20 +// Time::Now() may actually decrease or jump).  But note that TimeTicks may
    1.21 +// "stand still", for example if the computer suspended.
    1.22 +//
    1.23 +// These classes are represented as only a 64-bit value, so they can be
    1.24 +// efficiently passed by value.
    1.25 +
    1.26 +#ifndef BASE_TIME_TIME_H_
    1.27 +#define BASE_TIME_TIME_H_
    1.28 +
    1.29 +#include <time.h>
    1.30 +
    1.31 +#include "base/base_export.h"
    1.32 +#include "base/basictypes.h"
    1.33 +#include "build/build_config.h"
    1.34 +
    1.35 +#if defined(OS_MACOSX)
    1.36 +#include <CoreFoundation/CoreFoundation.h>
    1.37 +// Avoid Mac system header macro leak.
    1.38 +#undef TYPE_BOOL
    1.39 +#endif
    1.40 +
    1.41 +#if defined(OS_POSIX)
    1.42 +#include <unistd.h>
    1.43 +#include <sys/time.h>
    1.44 +#endif
    1.45 +
    1.46 +#if defined(OS_WIN)
    1.47 +// For FILETIME in FromFileTime, until it moves to a new converter class.
    1.48 +// See TODO(iyengar) below.
    1.49 +#include <windows.h>
    1.50 +#endif
    1.51 +
    1.52 +#include <limits>
    1.53 +
    1.54 +namespace base {
    1.55 +
    1.56 +class Time;
    1.57 +class TimeTicks;
    1.58 +
    1.59 +// TimeDelta ------------------------------------------------------------------
    1.60 +
    1.61 +class BASE_EXPORT TimeDelta {
    1.62 + public:
    1.63 +  TimeDelta() : delta_(0) {
    1.64 +  }
    1.65 +
    1.66 +  // Converts units of time to TimeDeltas.
    1.67 +  static TimeDelta FromDays(int64 days);
    1.68 +  static TimeDelta FromHours(int64 hours);
    1.69 +  static TimeDelta FromMinutes(int64 minutes);
    1.70 +  static TimeDelta FromSeconds(int64 secs);
    1.71 +  static TimeDelta FromMilliseconds(int64 ms);
    1.72 +  static TimeDelta FromMicroseconds(int64 us);
    1.73 +#if defined(OS_WIN)
    1.74 +  static TimeDelta FromQPCValue(LONGLONG qpc_value);
    1.75 +#endif
    1.76 +
    1.77 +  // Converts an integer value representing TimeDelta to a class. This is used
    1.78 +  // when deserializing a |TimeDelta| structure, using a value known to be
    1.79 +  // compatible. It is not provided as a constructor because the integer type
    1.80 +  // may be unclear from the perspective of a caller.
    1.81 +  static TimeDelta FromInternalValue(int64 delta) {
    1.82 +    return TimeDelta(delta);
    1.83 +  }
    1.84 +
    1.85 +  // Returns the internal numeric value of the TimeDelta object. Please don't
    1.86 +  // use this and do arithmetic on it, as it is more error prone than using the
    1.87 +  // provided operators.
    1.88 +  // For serializing, use FromInternalValue to reconstitute.
    1.89 +  int64 ToInternalValue() const {
    1.90 +    return delta_;
    1.91 +  }
    1.92 +
    1.93 +#if defined(OS_POSIX)
    1.94 +  struct timespec ToTimeSpec() const;
    1.95 +#endif
    1.96 +
    1.97 +  // Returns the time delta in some unit. The F versions return a floating
    1.98 +  // point value, the "regular" versions return a rounded-down value.
    1.99 +  //
   1.100 +  // InMillisecondsRoundedUp() instead returns an integer that is rounded up
   1.101 +  // to the next full millisecond.
   1.102 +  int InDays() const;
   1.103 +  int InHours() const;
   1.104 +  int InMinutes() const;
   1.105 +  double InSecondsF() const;
   1.106 +  int64 InSeconds() const;
   1.107 +  double InMillisecondsF() const;
   1.108 +  int64 InMilliseconds() const;
   1.109 +  int64 InMillisecondsRoundedUp() const;
   1.110 +  int64 InMicroseconds() const;
   1.111 +
   1.112 +  TimeDelta& operator=(TimeDelta other) {
   1.113 +    delta_ = other.delta_;
   1.114 +    return *this;
   1.115 +  }
   1.116 +
   1.117 +  // Computations with other deltas.
   1.118 +  TimeDelta operator+(TimeDelta other) const {
   1.119 +    return TimeDelta(delta_ + other.delta_);
   1.120 +  }
   1.121 +  TimeDelta operator-(TimeDelta other) const {
   1.122 +    return TimeDelta(delta_ - other.delta_);
   1.123 +  }
   1.124 +
   1.125 +  TimeDelta& operator+=(TimeDelta other) {
   1.126 +    delta_ += other.delta_;
   1.127 +    return *this;
   1.128 +  }
   1.129 +  TimeDelta& operator-=(TimeDelta other) {
   1.130 +    delta_ -= other.delta_;
   1.131 +    return *this;
   1.132 +  }
   1.133 +  TimeDelta operator-() const {
   1.134 +    return TimeDelta(-delta_);
   1.135 +  }
   1.136 +
   1.137 +  // Computations with ints, note that we only allow multiplicative operations
   1.138 +  // with ints, and additive operations with other deltas.
   1.139 +  TimeDelta operator*(int64 a) const {
   1.140 +    return TimeDelta(delta_ * a);
   1.141 +  }
   1.142 +  TimeDelta operator/(int64 a) const {
   1.143 +    return TimeDelta(delta_ / a);
   1.144 +  }
   1.145 +  TimeDelta& operator*=(int64 a) {
   1.146 +    delta_ *= a;
   1.147 +    return *this;
   1.148 +  }
   1.149 +  TimeDelta& operator/=(int64 a) {
   1.150 +    delta_ /= a;
   1.151 +    return *this;
   1.152 +  }
   1.153 +  int64 operator/(TimeDelta a) const {
   1.154 +    return delta_ / a.delta_;
   1.155 +  }
   1.156 +
   1.157 +  // Defined below because it depends on the definition of the other classes.
   1.158 +  Time operator+(Time t) const;
   1.159 +  TimeTicks operator+(TimeTicks t) const;
   1.160 +
   1.161 +  // Comparison operators.
   1.162 +  bool operator==(TimeDelta other) const {
   1.163 +    return delta_ == other.delta_;
   1.164 +  }
   1.165 +  bool operator!=(TimeDelta other) const {
   1.166 +    return delta_ != other.delta_;
   1.167 +  }
   1.168 +  bool operator<(TimeDelta other) const {
   1.169 +    return delta_ < other.delta_;
   1.170 +  }
   1.171 +  bool operator<=(TimeDelta other) const {
   1.172 +    return delta_ <= other.delta_;
   1.173 +  }
   1.174 +  bool operator>(TimeDelta other) const {
   1.175 +    return delta_ > other.delta_;
   1.176 +  }
   1.177 +  bool operator>=(TimeDelta other) const {
   1.178 +    return delta_ >= other.delta_;
   1.179 +  }
   1.180 +
   1.181 + private:
   1.182 +  friend class Time;
   1.183 +  friend class TimeTicks;
   1.184 +  friend TimeDelta operator*(int64 a, TimeDelta td);
   1.185 +
   1.186 +  // Constructs a delta given the duration in microseconds. This is private
   1.187 +  // to avoid confusion by callers with an integer constructor. Use
   1.188 +  // FromSeconds, FromMilliseconds, etc. instead.
   1.189 +  explicit TimeDelta(int64 delta_us) : delta_(delta_us) {
   1.190 +  }
   1.191 +
   1.192 +  // Delta in microseconds.
   1.193 +  int64 delta_;
   1.194 +};
   1.195 +
   1.196 +inline TimeDelta operator*(int64 a, TimeDelta td) {
   1.197 +  return TimeDelta(a * td.delta_);
   1.198 +}
   1.199 +
   1.200 +// Time -----------------------------------------------------------------------
   1.201 +
   1.202 +// Represents a wall clock time.
   1.203 +class BASE_EXPORT Time {
   1.204 + public:
   1.205 +  static const int64 kMillisecondsPerSecond = 1000;
   1.206 +  static const int64 kMicrosecondsPerMillisecond = 1000;
   1.207 +  static const int64 kMicrosecondsPerSecond = kMicrosecondsPerMillisecond *
   1.208 +                                              kMillisecondsPerSecond;
   1.209 +  static const int64 kMicrosecondsPerMinute = kMicrosecondsPerSecond * 60;
   1.210 +  static const int64 kMicrosecondsPerHour = kMicrosecondsPerMinute * 60;
   1.211 +  static const int64 kMicrosecondsPerDay = kMicrosecondsPerHour * 24;
   1.212 +  static const int64 kMicrosecondsPerWeek = kMicrosecondsPerDay * 7;
   1.213 +  static const int64 kNanosecondsPerMicrosecond = 1000;
   1.214 +  static const int64 kNanosecondsPerSecond = kNanosecondsPerMicrosecond *
   1.215 +                                             kMicrosecondsPerSecond;
   1.216 +
   1.217 +#if !defined(OS_WIN)
   1.218 +  // On Mac & Linux, this value is the delta from the Windows epoch of 1601 to
   1.219 +  // the Posix delta of 1970. This is used for migrating between the old
   1.220 +  // 1970-based epochs to the new 1601-based ones. It should be removed from
   1.221 +  // this global header and put in the platform-specific ones when we remove the
   1.222 +  // migration code.
   1.223 +  static const int64 kWindowsEpochDeltaMicroseconds;
   1.224 +#endif
   1.225 +
   1.226 +  // Represents an exploded time that can be formatted nicely. This is kind of
   1.227 +  // like the Win32 SYSTEMTIME structure or the Unix "struct tm" with a few
   1.228 +  // additions and changes to prevent errors.
   1.229 +  struct BASE_EXPORT Exploded {
   1.230 +    int year;          // Four digit year "2007"
   1.231 +    int month;         // 1-based month (values 1 = January, etc.)
   1.232 +    int day_of_week;   // 0-based day of week (0 = Sunday, etc.)
   1.233 +    int day_of_month;  // 1-based day of month (1-31)
   1.234 +    int hour;          // Hour within the current day (0-23)
   1.235 +    int minute;        // Minute within the current hour (0-59)
   1.236 +    int second;        // Second within the current minute (0-59 plus leap
   1.237 +                       //   seconds which may take it up to 60).
   1.238 +    int millisecond;   // Milliseconds within the current second (0-999)
   1.239 +
   1.240 +    // A cursory test for whether the data members are within their
   1.241 +    // respective ranges. A 'true' return value does not guarantee the
   1.242 +    // Exploded value can be successfully converted to a Time value.
   1.243 +    bool HasValidValues() const;
   1.244 +  };
   1.245 +
   1.246 +  // Contains the NULL time. Use Time::Now() to get the current time.
   1.247 +  Time() : us_(0) {
   1.248 +  }
   1.249 +
   1.250 +  // Returns true if the time object has not been initialized.
   1.251 +  bool is_null() const {
   1.252 +    return us_ == 0;
   1.253 +  }
   1.254 +
   1.255 +  // Returns true if the time object is the maximum time.
   1.256 +  bool is_max() const {
   1.257 +    return us_ == std::numeric_limits<int64>::max();
   1.258 +  }
   1.259 +
   1.260 +  // Returns the time for epoch in Unix-like system (Jan 1, 1970).
   1.261 +  static Time UnixEpoch();
   1.262 +
   1.263 +  // Returns the current time. Watch out, the system might adjust its clock
   1.264 +  // in which case time will actually go backwards. We don't guarantee that
   1.265 +  // times are increasing, or that two calls to Now() won't be the same.
   1.266 +  static Time Now();
   1.267 +
   1.268 +  // Returns the maximum time, which should be greater than any reasonable time
   1.269 +  // with which we might compare it.
   1.270 +  static Time Max();
   1.271 +
   1.272 +  // Returns the current time. Same as Now() except that this function always
   1.273 +  // uses system time so that there are no discrepancies between the returned
   1.274 +  // time and system time even on virtual environments including our test bot.
   1.275 +  // For timing sensitive unittests, this function should be used.
   1.276 +  static Time NowFromSystemTime();
   1.277 +
   1.278 +  // Converts to/from time_t in UTC and a Time class.
   1.279 +  // TODO(brettw) this should be removed once everybody starts using the |Time|
   1.280 +  // class.
   1.281 +  static Time FromTimeT(time_t tt);
   1.282 +  time_t ToTimeT() const;
   1.283 +
   1.284 +  // Converts time to/from a double which is the number of seconds since epoch
   1.285 +  // (Jan 1, 1970).  Webkit uses this format to represent time.
   1.286 +  // Because WebKit initializes double time value to 0 to indicate "not
   1.287 +  // initialized", we map it to empty Time object that also means "not
   1.288 +  // initialized".
   1.289 +  static Time FromDoubleT(double dt);
   1.290 +  double ToDoubleT() const;
   1.291 +
   1.292 +#if defined(OS_POSIX)
   1.293 +  // Converts the timespec structure to time. MacOS X 10.8.3 (and tentatively,
   1.294 +  // earlier versions) will have the |ts|'s tv_nsec component zeroed out,
   1.295 +  // having a 1 second resolution, which agrees with
   1.296 +  // https://developer.apple.com/legacy/library/#technotes/tn/tn1150.html#HFSPlusDates.
   1.297 +  static Time FromTimeSpec(const timespec& ts);
   1.298 +#endif
   1.299 +
   1.300 +  // Converts to/from the Javascript convention for times, a number of
   1.301 +  // milliseconds since the epoch:
   1.302 +  // https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date/getTime.
   1.303 +  static Time FromJsTime(double ms_since_epoch);
   1.304 +  double ToJsTime() const;
   1.305 +
   1.306 +  // Converts to Java convention for times, a number of
   1.307 +  // milliseconds since the epoch.
   1.308 +  int64 ToJavaTime() const;
   1.309 +
   1.310 +#if defined(OS_POSIX)
   1.311 +  static Time FromTimeVal(struct timeval t);
   1.312 +  struct timeval ToTimeVal() const;
   1.313 +#endif
   1.314 +
   1.315 +#if defined(OS_MACOSX)
   1.316 +  static Time FromCFAbsoluteTime(CFAbsoluteTime t);
   1.317 +  CFAbsoluteTime ToCFAbsoluteTime() const;
   1.318 +#endif
   1.319 +
   1.320 +#if defined(OS_WIN)
   1.321 +  static Time FromFileTime(FILETIME ft);
   1.322 +  FILETIME ToFileTime() const;
   1.323 +
   1.324 +  // The minimum time of a low resolution timer.  This is basically a windows
   1.325 +  // constant of ~15.6ms.  While it does vary on some older OS versions, we'll
   1.326 +  // treat it as static across all windows versions.
   1.327 +  static const int kMinLowResolutionThresholdMs = 16;
   1.328 +
   1.329 +  // Enable or disable Windows high resolution timer. If the high resolution
   1.330 +  // timer is not enabled, calls to ActivateHighResolutionTimer will fail.
   1.331 +  // When disabling the high resolution timer, this function will not cause
   1.332 +  // the high resolution timer to be deactivated, but will prevent future
   1.333 +  // activations.
   1.334 +  // Must be called from the main thread.
   1.335 +  // For more details see comments in time_win.cc.
   1.336 +  static void EnableHighResolutionTimer(bool enable);
   1.337 +
   1.338 +  // Activates or deactivates the high resolution timer based on the |activate|
   1.339 +  // flag.  If the HighResolutionTimer is not Enabled (see
   1.340 +  // EnableHighResolutionTimer), this function will return false.  Otherwise
   1.341 +  // returns true.  Each successful activate call must be paired with a
   1.342 +  // subsequent deactivate call.
   1.343 +  // All callers to activate the high resolution timer must eventually call
   1.344 +  // this function to deactivate the high resolution timer.
   1.345 +  static bool ActivateHighResolutionTimer(bool activate);
   1.346 +
   1.347 +  // Returns true if the high resolution timer is both enabled and activated.
   1.348 +  // This is provided for testing only, and is not tracked in a thread-safe
   1.349 +  // way.
   1.350 +  static bool IsHighResolutionTimerInUse();
   1.351 +#endif
   1.352 +
   1.353 +  // Converts an exploded structure representing either the local time or UTC
   1.354 +  // into a Time class.
   1.355 +  static Time FromUTCExploded(const Exploded& exploded) {
   1.356 +    return FromExploded(false, exploded);
   1.357 +  }
   1.358 +  static Time FromLocalExploded(const Exploded& exploded) {
   1.359 +    return FromExploded(true, exploded);
   1.360 +  }
   1.361 +
   1.362 +  // Converts an integer value representing Time to a class. This is used
   1.363 +  // when deserializing a |Time| structure, using a value known to be
   1.364 +  // compatible. It is not provided as a constructor because the integer type
   1.365 +  // may be unclear from the perspective of a caller.
   1.366 +  static Time FromInternalValue(int64 us) {
   1.367 +    return Time(us);
   1.368 +  }
   1.369 +
   1.370 +  // Converts a string representation of time to a Time object.
   1.371 +  // An example of a time string which is converted is as below:-
   1.372 +  // "Tue, 15 Nov 1994 12:45:26 GMT". If the timezone is not specified
   1.373 +  // in the input string, FromString assumes local time and FromUTCString
   1.374 +  // assumes UTC. A timezone that cannot be parsed (e.g. "UTC" which is not
   1.375 +  // specified in RFC822) is treated as if the timezone is not specified.
   1.376 +  // TODO(iyengar) Move the FromString/FromTimeT/ToTimeT/FromFileTime to
   1.377 +  // a new time converter class.
   1.378 +  static bool FromString(const char* time_string, Time* parsed_time) {
   1.379 +    return FromStringInternal(time_string, true, parsed_time);
   1.380 +  }
   1.381 +  static bool FromUTCString(const char* time_string, Time* parsed_time) {
   1.382 +    return FromStringInternal(time_string, false, parsed_time);
   1.383 +  }
   1.384 +
   1.385 +  // For serializing, use FromInternalValue to reconstitute. Please don't use
   1.386 +  // this and do arithmetic on it, as it is more error prone than using the
   1.387 +  // provided operators.
   1.388 +  int64 ToInternalValue() const {
   1.389 +    return us_;
   1.390 +  }
   1.391 +
   1.392 +  // Fills the given exploded structure with either the local time or UTC from
   1.393 +  // this time structure (containing UTC).
   1.394 +  void UTCExplode(Exploded* exploded) const {
   1.395 +    return Explode(false, exploded);
   1.396 +  }
   1.397 +  void LocalExplode(Exploded* exploded) const {
   1.398 +    return Explode(true, exploded);
   1.399 +  }
   1.400 +
   1.401 +  // Rounds this time down to the nearest day in local time. It will represent
   1.402 +  // midnight on that day.
   1.403 +  Time LocalMidnight() const;
   1.404 +
   1.405 +  Time& operator=(Time other) {
   1.406 +    us_ = other.us_;
   1.407 +    return *this;
   1.408 +  }
   1.409 +
   1.410 +  // Compute the difference between two times.
   1.411 +  TimeDelta operator-(Time other) const {
   1.412 +    return TimeDelta(us_ - other.us_);
   1.413 +  }
   1.414 +
   1.415 +  // Modify by some time delta.
   1.416 +  Time& operator+=(TimeDelta delta) {
   1.417 +    us_ += delta.delta_;
   1.418 +    return *this;
   1.419 +  }
   1.420 +  Time& operator-=(TimeDelta delta) {
   1.421 +    us_ -= delta.delta_;
   1.422 +    return *this;
   1.423 +  }
   1.424 +
   1.425 +  // Return a new time modified by some delta.
   1.426 +  Time operator+(TimeDelta delta) const {
   1.427 +    return Time(us_ + delta.delta_);
   1.428 +  }
   1.429 +  Time operator-(TimeDelta delta) const {
   1.430 +    return Time(us_ - delta.delta_);
   1.431 +  }
   1.432 +
   1.433 +  // Comparison operators
   1.434 +  bool operator==(Time other) const {
   1.435 +    return us_ == other.us_;
   1.436 +  }
   1.437 +  bool operator!=(Time other) const {
   1.438 +    return us_ != other.us_;
   1.439 +  }
   1.440 +  bool operator<(Time other) const {
   1.441 +    return us_ < other.us_;
   1.442 +  }
   1.443 +  bool operator<=(Time other) const {
   1.444 +    return us_ <= other.us_;
   1.445 +  }
   1.446 +  bool operator>(Time other) const {
   1.447 +    return us_ > other.us_;
   1.448 +  }
   1.449 +  bool operator>=(Time other) const {
   1.450 +    return us_ >= other.us_;
   1.451 +  }
   1.452 +
   1.453 + private:
   1.454 +  friend class TimeDelta;
   1.455 +
   1.456 +  explicit Time(int64 us) : us_(us) {
   1.457 +  }
   1.458 +
   1.459 +  // Explodes the given time to either local time |is_local = true| or UTC
   1.460 +  // |is_local = false|.
   1.461 +  void Explode(bool is_local, Exploded* exploded) const;
   1.462 +
   1.463 +  // Unexplodes a given time assuming the source is either local time
   1.464 +  // |is_local = true| or UTC |is_local = false|.
   1.465 +  static Time FromExploded(bool is_local, const Exploded& exploded);
   1.466 +
   1.467 +  // Converts a string representation of time to a Time object.
   1.468 +  // An example of a time string which is converted is as below:-
   1.469 +  // "Tue, 15 Nov 1994 12:45:26 GMT". If the timezone is not specified
   1.470 +  // in the input string, local time |is_local = true| or
   1.471 +  // UTC |is_local = false| is assumed. A timezone that cannot be parsed
   1.472 +  // (e.g. "UTC" which is not specified in RFC822) is treated as if the
   1.473 +  // timezone is not specified.
   1.474 +  static bool FromStringInternal(const char* time_string,
   1.475 +                                 bool is_local,
   1.476 +                                 Time* parsed_time);
   1.477 +
   1.478 +  // The representation of Jan 1, 1970 UTC in microseconds since the
   1.479 +  // platform-dependent epoch.
   1.480 +  static const int64 kTimeTToMicrosecondsOffset;
   1.481 +
   1.482 +#if defined(OS_WIN)
   1.483 +  // Indicates whether fast timers are usable right now.  For instance,
   1.484 +  // when using battery power, we might elect to prevent high speed timers
   1.485 +  // which would draw more power.
   1.486 +  static bool high_resolution_timer_enabled_;
   1.487 +  // Count of activations on the high resolution timer.  Only use in tests
   1.488 +  // which are single threaded.
   1.489 +  static int high_resolution_timer_activated_;
   1.490 +#endif
   1.491 +
   1.492 +  // Time in microseconds in UTC.
   1.493 +  int64 us_;
   1.494 +};
   1.495 +
   1.496 +// Inline the TimeDelta factory methods, for fast TimeDelta construction.
   1.497 +
   1.498 +// static
   1.499 +inline TimeDelta TimeDelta::FromDays(int64 days) {
   1.500 +  return TimeDelta(days * Time::kMicrosecondsPerDay);
   1.501 +}
   1.502 +
   1.503 +// static
   1.504 +inline TimeDelta TimeDelta::FromHours(int64 hours) {
   1.505 +  return TimeDelta(hours * Time::kMicrosecondsPerHour);
   1.506 +}
   1.507 +
   1.508 +// static
   1.509 +inline TimeDelta TimeDelta::FromMinutes(int64 minutes) {
   1.510 +  return TimeDelta(minutes * Time::kMicrosecondsPerMinute);
   1.511 +}
   1.512 +
   1.513 +// static
   1.514 +inline TimeDelta TimeDelta::FromSeconds(int64 secs) {
   1.515 +  return TimeDelta(secs * Time::kMicrosecondsPerSecond);
   1.516 +}
   1.517 +
   1.518 +// static
   1.519 +inline TimeDelta TimeDelta::FromMilliseconds(int64 ms) {
   1.520 +  return TimeDelta(ms * Time::kMicrosecondsPerMillisecond);
   1.521 +}
   1.522 +
   1.523 +// static
   1.524 +inline TimeDelta TimeDelta::FromMicroseconds(int64 us) {
   1.525 +  return TimeDelta(us);
   1.526 +}
   1.527 +
   1.528 +inline Time TimeDelta::operator+(Time t) const {
   1.529 +  return Time(t.us_ + delta_);
   1.530 +}
   1.531 +
   1.532 +// TimeTicks ------------------------------------------------------------------
   1.533 +
   1.534 +class BASE_EXPORT TimeTicks {
   1.535 + public:
   1.536 +  TimeTicks() : ticks_(0) {
   1.537 +  }
   1.538 +
   1.539 +  // Platform-dependent tick count representing "right now."
   1.540 +  // The resolution of this clock is ~1-15ms.  Resolution varies depending
   1.541 +  // on hardware/operating system configuration.
   1.542 +  static TimeTicks Now();
   1.543 +
   1.544 +  // Returns a platform-dependent high-resolution tick count. Implementation
   1.545 +  // is hardware dependent and may or may not return sub-millisecond
   1.546 +  // resolution.  THIS CALL IS GENERALLY MUCH MORE EXPENSIVE THAN Now() AND
   1.547 +  // SHOULD ONLY BE USED WHEN IT IS REALLY NEEDED.
   1.548 +  static TimeTicks HighResNow();
   1.549 +
   1.550 +  // Returns true if ThreadNow() is supported on this system.
   1.551 +  static bool IsThreadNowSupported() {
   1.552 +#if defined(_POSIX_THREAD_CPUTIME) && (_POSIX_THREAD_CPUTIME >= 0)
   1.553 +    return true;
   1.554 +#else
   1.555 +    return false;
   1.556 +#endif
   1.557 +  }
   1.558 +
   1.559 +  // Returns thread-specific CPU-time on systems that support this feature.
   1.560 +  // Needs to be guarded with a call to IsThreadNowSupported(). Use this timer
   1.561 +  // to (approximately) measure how much time the calling thread spent doing
   1.562 +  // actual work vs. being de-scheduled. May return bogus results if the thread
   1.563 +  // migrates to another CPU between two calls.
   1.564 +  static TimeTicks ThreadNow();
   1.565 +
   1.566 +  // Returns the current system trace time or, if none is defined, the current
   1.567 +  // high-res time (i.e. HighResNow()). On systems where a global trace clock
   1.568 +  // is defined, timestamping TraceEvents's with this value guarantees
   1.569 +  // synchronization between events collected inside chrome and events
   1.570 +  // collected outside (e.g. kernel, X server).
   1.571 +  static TimeTicks NowFromSystemTraceTime();
   1.572 +
   1.573 +#if defined(OS_WIN)
   1.574 +  // Get the absolute value of QPC time drift. For testing.
   1.575 +  static int64 GetQPCDriftMicroseconds();
   1.576 +
   1.577 +  static TimeTicks FromQPCValue(LONGLONG qpc_value);
   1.578 +
   1.579 +  // Returns true if the high resolution clock is working on this system.
   1.580 +  // This is only for testing.
   1.581 +  static bool IsHighResClockWorking();
   1.582 +
   1.583 +  // Enable high resolution time for TimeTicks::Now(). This function will
   1.584 +  // test for the availability of a working implementation of
   1.585 +  // QueryPerformanceCounter(). If one is not available, this function does
   1.586 +  // nothing and the resolution of Now() remains 1ms. Otherwise, all future
   1.587 +  // calls to TimeTicks::Now() will have the higher resolution provided by QPC.
   1.588 +  // Returns true if high resolution time was successfully enabled.
   1.589 +  static bool SetNowIsHighResNowIfSupported();
   1.590 +
   1.591 +  // Returns a time value that is NOT rollover protected.
   1.592 +  static TimeTicks UnprotectedNow();
   1.593 +#endif
   1.594 +
   1.595 +  // Returns true if this object has not been initialized.
   1.596 +  bool is_null() const {
   1.597 +    return ticks_ == 0;
   1.598 +  }
   1.599 +
   1.600 +  // Converts an integer value representing TimeTicks to a class. This is used
   1.601 +  // when deserializing a |TimeTicks| structure, using a value known to be
   1.602 +  // compatible. It is not provided as a constructor because the integer type
   1.603 +  // may be unclear from the perspective of a caller.
   1.604 +  static TimeTicks FromInternalValue(int64 ticks) {
   1.605 +    return TimeTicks(ticks);
   1.606 +  }
   1.607 +
   1.608 +  // Returns the internal numeric value of the TimeTicks object.
   1.609 +  // For serializing, use FromInternalValue to reconstitute.
   1.610 +  int64 ToInternalValue() const {
   1.611 +    return ticks_;
   1.612 +  }
   1.613 +
   1.614 +  TimeTicks& operator=(TimeTicks other) {
   1.615 +    ticks_ = other.ticks_;
   1.616 +    return *this;
   1.617 +  }
   1.618 +
   1.619 +  // Compute the difference between two times.
   1.620 +  TimeDelta operator-(TimeTicks other) const {
   1.621 +    return TimeDelta(ticks_ - other.ticks_);
   1.622 +  }
   1.623 +
   1.624 +  // Modify by some time delta.
   1.625 +  TimeTicks& operator+=(TimeDelta delta) {
   1.626 +    ticks_ += delta.delta_;
   1.627 +    return *this;
   1.628 +  }
   1.629 +  TimeTicks& operator-=(TimeDelta delta) {
   1.630 +    ticks_ -= delta.delta_;
   1.631 +    return *this;
   1.632 +  }
   1.633 +
   1.634 +  // Return a new TimeTicks modified by some delta.
   1.635 +  TimeTicks operator+(TimeDelta delta) const {
   1.636 +    return TimeTicks(ticks_ + delta.delta_);
   1.637 +  }
   1.638 +  TimeTicks operator-(TimeDelta delta) const {
   1.639 +    return TimeTicks(ticks_ - delta.delta_);
   1.640 +  }
   1.641 +
   1.642 +  // Comparison operators
   1.643 +  bool operator==(TimeTicks other) const {
   1.644 +    return ticks_ == other.ticks_;
   1.645 +  }
   1.646 +  bool operator!=(TimeTicks other) const {
   1.647 +    return ticks_ != other.ticks_;
   1.648 +  }
   1.649 +  bool operator<(TimeTicks other) const {
   1.650 +    return ticks_ < other.ticks_;
   1.651 +  }
   1.652 +  bool operator<=(TimeTicks other) const {
   1.653 +    return ticks_ <= other.ticks_;
   1.654 +  }
   1.655 +  bool operator>(TimeTicks other) const {
   1.656 +    return ticks_ > other.ticks_;
   1.657 +  }
   1.658 +  bool operator>=(TimeTicks other) const {
   1.659 +    return ticks_ >= other.ticks_;
   1.660 +  }
   1.661 +
   1.662 + protected:
   1.663 +  friend class TimeDelta;
   1.664 +
   1.665 +  // Please use Now() to create a new object. This is for internal use
   1.666 +  // and testing. Ticks is in microseconds.
   1.667 +  explicit TimeTicks(int64 ticks) : ticks_(ticks) {
   1.668 +  }
   1.669 +
   1.670 +  // Tick count in microseconds.
   1.671 +  int64 ticks_;
   1.672 +
   1.673 +#if defined(OS_WIN)
   1.674 +  typedef DWORD (*TickFunctionType)(void);
   1.675 +  static TickFunctionType SetMockTickFunction(TickFunctionType ticker);
   1.676 +#endif
   1.677 +};
   1.678 +
   1.679 +inline TimeTicks TimeDelta::operator+(TimeTicks t) const {
   1.680 +  return TimeTicks(t.ticks_ + delta_);
   1.681 +}
   1.682 +
   1.683 +}  // namespace base
   1.684 +
   1.685 +#endif  // BASE_TIME_TIME_H_

mercurial