15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// Time represents an absolute point in coordinated universal time (UTC),
6cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// internally represented as microseconds (s/1,000,000) since the Windows epoch
7cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// (1601-01-01 00:00:00 UTC) (See http://crbug.com/14734).  System-dependent
8cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// clock interface routines are defined in time_PLATFORM.cc.
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// TimeDelta represents a duration of time, internally represented in
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// microseconds.
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// TimeTicks represents an abstract time that is most of the time incrementing
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// for use in measuring time durations. It is internally represented in
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// microseconds.  It can not be converted to a human-readable time, but is
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// guaranteed not to decrease (if the user changes the computer clock,
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Time::Now() may actually decrease or jump).  But note that TimeTicks may
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// "stand still", for example if the computer suspended.
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// These classes are represented as only a 64-bit value, so they can be
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// efficiently passed by value.
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
23eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#ifndef BASE_TIME_TIME_H_
24eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#define BASE_TIME_TIME_H_
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <time.h>
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/base_export.h"
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/basictypes.h"
3058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#include "build/build_config.h"
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(OS_MACOSX)
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <CoreFoundation/CoreFoundation.h>
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Avoid Mac system header macro leak.
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#undef TYPE_BOOL
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(OS_POSIX)
39a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)#include <unistd.h>
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <sys/time.h>
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(OS_WIN)
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// For FILETIME in FromFileTime, until it moves to a new converter class.
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// See TODO(iyengar) below.
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <windows.h>
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <limits>
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace base {
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class Time;
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class TimeTicks;
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// TimeDelta ------------------------------------------------------------------
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class BASE_EXPORT TimeDelta {
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  TimeDelta() : delta_(0) {
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Converts units of time to TimeDeltas.
64a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  static TimeDelta FromDays(int days);
65a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  static TimeDelta FromHours(int hours);
66a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  static TimeDelta FromMinutes(int minutes);
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static TimeDelta FromSeconds(int64 secs);
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static TimeDelta FromMilliseconds(int64 ms);
690529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  static TimeDelta FromSecondsD(double secs);
700529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  static TimeDelta FromMillisecondsD(double ms);
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static TimeDelta FromMicroseconds(int64 us);
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(OS_WIN)
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static TimeDelta FromQPCValue(LONGLONG qpc_value);
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Converts an integer value representing TimeDelta to a class. This is used
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // when deserializing a |TimeDelta| structure, using a value known to be
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // compatible. It is not provided as a constructor because the integer type
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // may be unclear from the perspective of a caller.
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static TimeDelta FromInternalValue(int64 delta) {
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return TimeDelta(delta);
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
84a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // Returns the maximum time delta, which should be greater than any reasonable
85a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // time delta we might compare it to. Adding or subtracting the maximum time
86a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // delta to a time or another time delta has an undefined result.
87a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  static TimeDelta Max();
88a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns the internal numeric value of the TimeDelta object. Please don't
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // use this and do arithmetic on it, as it is more error prone than using the
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // provided operators.
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // For serializing, use FromInternalValue to reconstitute.
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int64 ToInternalValue() const {
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return delta_;
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
97a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // Returns true if the time delta is the maximum time delta.
98a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  bool is_max() const {
99a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    return delta_ == std::numeric_limits<int64>::max();
100a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  }
101a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(OS_POSIX)
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  struct timespec ToTimeSpec() const;
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns the time delta in some unit. The F versions return a floating
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // point value, the "regular" versions return a rounded-down value.
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // InMillisecondsRoundedUp() instead returns an integer that is rounded up
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // to the next full millisecond.
1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int InDays() const;
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int InHours() const;
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int InMinutes() const;
1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  double InSecondsF() const;
1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int64 InSeconds() const;
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  double InMillisecondsF() const;
1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int64 InMilliseconds() const;
1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int64 InMillisecondsRoundedUp() const;
1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int64 InMicroseconds() const;
1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  TimeDelta& operator=(TimeDelta other) {
1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    delta_ = other.delta_;
1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return *this;
1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Computations with other deltas.
1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  TimeDelta operator+(TimeDelta other) const {
1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return TimeDelta(delta_ + other.delta_);
1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  TimeDelta operator-(TimeDelta other) const {
1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return TimeDelta(delta_ - other.delta_);
1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  TimeDelta& operator+=(TimeDelta other) {
1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    delta_ += other.delta_;
1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return *this;
1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  TimeDelta& operator-=(TimeDelta other) {
1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    delta_ -= other.delta_;
1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return *this;
1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  TimeDelta operator-() const {
1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return TimeDelta(-delta_);
1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Computations with ints, note that we only allow multiplicative operations
1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // with ints, and additive operations with other deltas.
1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  TimeDelta operator*(int64 a) const {
1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return TimeDelta(delta_ * a);
1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  TimeDelta operator/(int64 a) const {
1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return TimeDelta(delta_ / a);
1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  TimeDelta& operator*=(int64 a) {
1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    delta_ *= a;
1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return *this;
1575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  TimeDelta& operator/=(int64 a) {
1595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    delta_ /= a;
1605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return *this;
1615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int64 operator/(TimeDelta a) const {
1635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return delta_ / a.delta_;
1645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Defined below because it depends on the definition of the other classes.
1675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Time operator+(Time t) const;
1685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  TimeTicks operator+(TimeTicks t) const;
1695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Comparison operators.
1715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool operator==(TimeDelta other) const {
1725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return delta_ == other.delta_;
1735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool operator!=(TimeDelta other) const {
1755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return delta_ != other.delta_;
1765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool operator<(TimeDelta other) const {
1785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return delta_ < other.delta_;
1795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool operator<=(TimeDelta other) const {
1815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return delta_ <= other.delta_;
1825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool operator>(TimeDelta other) const {
1845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return delta_ > other.delta_;
1855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool operator>=(TimeDelta other) const {
1875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return delta_ >= other.delta_;
1885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
1915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  friend class Time;
1925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  friend class TimeTicks;
1935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  friend TimeDelta operator*(int64 a, TimeDelta td);
1945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Constructs a delta given the duration in microseconds. This is private
1965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // to avoid confusion by callers with an integer constructor. Use
1975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // FromSeconds, FromMilliseconds, etc. instead.
1985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  explicit TimeDelta(int64 delta_us) : delta_(delta_us) {
1995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
2005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Delta in microseconds.
2025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int64 delta_;
2035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
2045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)inline TimeDelta operator*(int64 a, TimeDelta td) {
2065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return TimeDelta(a * td.delta_);
2075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Time -----------------------------------------------------------------------
2105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
211cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// Represents a wall clock time in UTC.
2125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class BASE_EXPORT Time {
2135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
2145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static const int64 kMillisecondsPerSecond = 1000;
2155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static const int64 kMicrosecondsPerMillisecond = 1000;
2165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static const int64 kMicrosecondsPerSecond = kMicrosecondsPerMillisecond *
2175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                              kMillisecondsPerSecond;
2185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static const int64 kMicrosecondsPerMinute = kMicrosecondsPerSecond * 60;
2195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static const int64 kMicrosecondsPerHour = kMicrosecondsPerMinute * 60;
2205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static const int64 kMicrosecondsPerDay = kMicrosecondsPerHour * 24;
2215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static const int64 kMicrosecondsPerWeek = kMicrosecondsPerDay * 7;
2225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static const int64 kNanosecondsPerMicrosecond = 1000;
2235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static const int64 kNanosecondsPerSecond = kNanosecondsPerMicrosecond *
2245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                             kMicrosecondsPerSecond;
2255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if !defined(OS_WIN)
2275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // On Mac & Linux, this value is the delta from the Windows epoch of 1601 to
2285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // the Posix delta of 1970. This is used for migrating between the old
2295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // 1970-based epochs to the new 1601-based ones. It should be removed from
2305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // this global header and put in the platform-specific ones when we remove the
2315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // migration code.
2325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static const int64 kWindowsEpochDeltaMicroseconds;
2335f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#else
2345f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  // To avoid overflow in QPC to Microseconds calculations, since we multiply
2355f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  // by kMicrosecondsPerSecond, then the QPC value should not exceed
2365f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  // (2^63 - 1) / 1E6. If it exceeds that threshold, we divide then multiply.
2375f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  static const int64 kQPCOverflowThreshold = 0x8637BD05AF7;
2385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
2395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Represents an exploded time that can be formatted nicely. This is kind of
2415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // like the Win32 SYSTEMTIME structure or the Unix "struct tm" with a few
2425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // additions and changes to prevent errors.
2435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  struct BASE_EXPORT Exploded {
2445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    int year;          // Four digit year "2007"
2455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    int month;         // 1-based month (values 1 = January, etc.)
2465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    int day_of_week;   // 0-based day of week (0 = Sunday, etc.)
2475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    int day_of_month;  // 1-based day of month (1-31)
2485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    int hour;          // Hour within the current day (0-23)
2495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    int minute;        // Minute within the current hour (0-59)
2505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    int second;        // Second within the current minute (0-59 plus leap
2515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                       //   seconds which may take it up to 60).
2525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    int millisecond;   // Milliseconds within the current second (0-999)
2535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // A cursory test for whether the data members are within their
2555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // respective ranges. A 'true' return value does not guarantee the
2565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Exploded value can be successfully converted to a Time value.
2575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    bool HasValidValues() const;
2585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  };
2595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Contains the NULL time. Use Time::Now() to get the current time.
2612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  Time() : us_(0) {
2625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
2635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns true if the time object has not been initialized.
2655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool is_null() const {
2665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return us_ == 0;
2675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
2685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns true if the time object is the maximum time.
2705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool is_max() const {
2715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return us_ == std::numeric_limits<int64>::max();
2725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
2735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns the time for epoch in Unix-like system (Jan 1, 1970).
2755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static Time UnixEpoch();
2765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns the current time. Watch out, the system might adjust its clock
2785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // in which case time will actually go backwards. We don't guarantee that
2795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // times are increasing, or that two calls to Now() won't be the same.
2805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static Time Now();
2815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns the maximum time, which should be greater than any reasonable time
2835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // with which we might compare it.
2845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static Time Max();
2855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns the current time. Same as Now() except that this function always
2875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // uses system time so that there are no discrepancies between the returned
2885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // time and system time even on virtual environments including our test bot.
2895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // For timing sensitive unittests, this function should be used.
2905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static Time NowFromSystemTime();
2915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Converts to/from time_t in UTC and a Time class.
2935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // TODO(brettw) this should be removed once everybody starts using the |Time|
2945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // class.
2955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static Time FromTimeT(time_t tt);
2965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  time_t ToTimeT() const;
2975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Converts time to/from a double which is the number of seconds since epoch
2995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // (Jan 1, 1970).  Webkit uses this format to represent time.
3005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Because WebKit initializes double time value to 0 to indicate "not
3015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // initialized", we map it to empty Time object that also means "not
3025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // initialized".
3035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static Time FromDoubleT(double dt);
3045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  double ToDoubleT() const;
3055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
306868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#if defined(OS_POSIX)
307868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Converts the timespec structure to time. MacOS X 10.8.3 (and tentatively,
308868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // earlier versions) will have the |ts|'s tv_nsec component zeroed out,
309868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // having a 1 second resolution, which agrees with
310868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // https://developer.apple.com/legacy/library/#technotes/tn/tn1150.html#HFSPlusDates.
311868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  static Time FromTimeSpec(const timespec& ts);
312868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#endif
313868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
3145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Converts to/from the Javascript convention for times, a number of
3155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // milliseconds since the epoch:
3165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date/getTime.
3175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static Time FromJsTime(double ms_since_epoch);
3185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  double ToJsTime() const;
3195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
32068043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  // Converts to Java convention for times, a number of
32168043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  // milliseconds since the epoch.
32268043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  int64 ToJavaTime() const;
32368043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)
3245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(OS_POSIX)
3255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static Time FromTimeVal(struct timeval t);
3265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  struct timeval ToTimeVal() const;
3275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
3285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(OS_MACOSX)
3305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static Time FromCFAbsoluteTime(CFAbsoluteTime t);
3315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  CFAbsoluteTime ToCFAbsoluteTime() const;
3325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
3335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(OS_WIN)
3355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static Time FromFileTime(FILETIME ft);
3365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FILETIME ToFileTime() const;
3375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The minimum time of a low resolution timer.  This is basically a windows
3395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // constant of ~15.6ms.  While it does vary on some older OS versions, we'll
3405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // treat it as static across all windows versions.
3415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static const int kMinLowResolutionThresholdMs = 16;
3425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3431320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // Enable or disable Windows high resolution timer.
3445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static void EnableHighResolutionTimer(bool enable);
3455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Activates or deactivates the high resolution timer based on the |activate|
3475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // flag.  If the HighResolutionTimer is not Enabled (see
3485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // EnableHighResolutionTimer), this function will return false.  Otherwise
3495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // returns true.  Each successful activate call must be paired with a
3505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // subsequent deactivate call.
3515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // All callers to activate the high resolution timer must eventually call
3525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // this function to deactivate the high resolution timer.
3535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static bool ActivateHighResolutionTimer(bool activate);
3545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns true if the high resolution timer is both enabled and activated.
3565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // This is provided for testing only, and is not tracked in a thread-safe
3575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // way.
3585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static bool IsHighResolutionTimerInUse();
3595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
3605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Converts an exploded structure representing either the local time or UTC
3625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // into a Time class.
3635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static Time FromUTCExploded(const Exploded& exploded) {
3645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return FromExploded(false, exploded);
3655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
3665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static Time FromLocalExploded(const Exploded& exploded) {
3675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return FromExploded(true, exploded);
3685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
3695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Converts an integer value representing Time to a class. This is used
3715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // when deserializing a |Time| structure, using a value known to be
3725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // compatible. It is not provided as a constructor because the integer type
3735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // may be unclear from the perspective of a caller.
3745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static Time FromInternalValue(int64 us) {
3755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return Time(us);
3765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
3775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Converts a string representation of time to a Time object.
3795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // An example of a time string which is converted is as below:-
3805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // "Tue, 15 Nov 1994 12:45:26 GMT". If the timezone is not specified
3815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // in the input string, FromString assumes local time and FromUTCString
3825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // assumes UTC. A timezone that cannot be parsed (e.g. "UTC" which is not
3835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // specified in RFC822) is treated as if the timezone is not specified.
3845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // TODO(iyengar) Move the FromString/FromTimeT/ToTimeT/FromFileTime to
3855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // a new time converter class.
3865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static bool FromString(const char* time_string, Time* parsed_time) {
3875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return FromStringInternal(time_string, true, parsed_time);
3885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
3895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static bool FromUTCString(const char* time_string, Time* parsed_time) {
3905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return FromStringInternal(time_string, false, parsed_time);
3915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
3925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // For serializing, use FromInternalValue to reconstitute. Please don't use
3945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // this and do arithmetic on it, as it is more error prone than using the
3955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // provided operators.
3965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int64 ToInternalValue() const {
3975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return us_;
3985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
3995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Fills the given exploded structure with either the local time or UTC from
4015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // this time structure (containing UTC).
4025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void UTCExplode(Exploded* exploded) const {
4035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return Explode(false, exploded);
4045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
4055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void LocalExplode(Exploded* exploded) const {
4065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return Explode(true, exploded);
4075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
4085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Rounds this time down to the nearest day in local time. It will represent
4105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // midnight on that day.
4115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Time LocalMidnight() const;
4125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Time& operator=(Time other) {
4145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    us_ = other.us_;
4155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return *this;
4165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
4175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Compute the difference between two times.
4195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  TimeDelta operator-(Time other) const {
4205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return TimeDelta(us_ - other.us_);
4215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
4225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Modify by some time delta.
4245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Time& operator+=(TimeDelta delta) {
4255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    us_ += delta.delta_;
4265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return *this;
4275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
4285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Time& operator-=(TimeDelta delta) {
4295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    us_ -= delta.delta_;
4305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return *this;
4315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
4325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Return a new time modified by some delta.
4345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Time operator+(TimeDelta delta) const {
4355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return Time(us_ + delta.delta_);
4365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
4375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Time operator-(TimeDelta delta) const {
4385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return Time(us_ - delta.delta_);
4395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
4405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Comparison operators
4425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool operator==(Time other) const {
4435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return us_ == other.us_;
4445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
4455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool operator!=(Time other) const {
4465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return us_ != other.us_;
4475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
4485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool operator<(Time other) const {
4495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return us_ < other.us_;
4505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
4515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool operator<=(Time other) const {
4525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return us_ <= other.us_;
4535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
4545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool operator>(Time other) const {
4555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return us_ > other.us_;
4565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
4575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool operator>=(Time other) const {
4585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return us_ >= other.us_;
4595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
4605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
4625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  friend class TimeDelta;
4635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  explicit Time(int64 us) : us_(us) {
4655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
4665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Explodes the given time to either local time |is_local = true| or UTC
4685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |is_local = false|.
4695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void Explode(bool is_local, Exploded* exploded) const;
4705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Unexplodes a given time assuming the source is either local time
4725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |is_local = true| or UTC |is_local = false|.
4735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static Time FromExploded(bool is_local, const Exploded& exploded);
4745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Converts a string representation of time to a Time object.
4765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // An example of a time string which is converted is as below:-
4775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // "Tue, 15 Nov 1994 12:45:26 GMT". If the timezone is not specified
4785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // in the input string, local time |is_local = true| or
4795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // UTC |is_local = false| is assumed. A timezone that cannot be parsed
4805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // (e.g. "UTC" which is not specified in RFC822) is treated as if the
4815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // timezone is not specified.
4825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static bool FromStringInternal(const char* time_string,
4835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                 bool is_local,
4845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                 Time* parsed_time);
4855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The representation of Jan 1, 1970 UTC in microseconds since the
4875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // platform-dependent epoch.
4885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static const int64 kTimeTToMicrosecondsOffset;
4895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Time in microseconds in UTC.
4915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int64 us_;
4925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
4935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Inline the TimeDelta factory methods, for fast TimeDelta construction.
4955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// static
497a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)inline TimeDelta TimeDelta::FromDays(int days) {
498a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // Preserve max to prevent overflow.
499a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (days == std::numeric_limits<int>::max())
500a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    return Max();
5015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return TimeDelta(days * Time::kMicrosecondsPerDay);
5025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
5035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// static
505a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)inline TimeDelta TimeDelta::FromHours(int hours) {
506a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // Preserve max to prevent overflow.
507a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (hours == std::numeric_limits<int>::max())
508a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    return Max();
5095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return TimeDelta(hours * Time::kMicrosecondsPerHour);
5105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
5115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// static
513a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)inline TimeDelta TimeDelta::FromMinutes(int minutes) {
514a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // Preserve max to prevent overflow.
515a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (minutes == std::numeric_limits<int>::max())
516a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    return Max();
5175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return TimeDelta(minutes * Time::kMicrosecondsPerMinute);
5185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
5195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// static
5215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)inline TimeDelta TimeDelta::FromSeconds(int64 secs) {
522a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // Preserve max to prevent overflow.
523a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (secs == std::numeric_limits<int64>::max())
524a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    return Max();
5255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return TimeDelta(secs * Time::kMicrosecondsPerSecond);
5265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
5275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// static
5295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)inline TimeDelta TimeDelta::FromMilliseconds(int64 ms) {
530a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // Preserve max to prevent overflow.
531a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (ms == std::numeric_limits<int64>::max())
532a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    return Max();
5335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return TimeDelta(ms * Time::kMicrosecondsPerMillisecond);
5345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
5355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// static
5370529e5d033099cbfc42635f6f6183833b09dff6eBen Murdochinline TimeDelta TimeDelta::FromSecondsD(double secs) {
5380529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  // Preserve max to prevent overflow.
5390529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  if (secs == std::numeric_limits<double>::infinity())
5400529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    return Max();
5410529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  return TimeDelta(secs * Time::kMicrosecondsPerSecond);
5420529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch}
5430529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
5440529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch// static
5450529e5d033099cbfc42635f6f6183833b09dff6eBen Murdochinline TimeDelta TimeDelta::FromMillisecondsD(double ms) {
5460529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  // Preserve max to prevent overflow.
5470529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  if (ms == std::numeric_limits<double>::infinity())
5480529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    return Max();
5490529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  return TimeDelta(ms * Time::kMicrosecondsPerMillisecond);
5500529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch}
5510529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
5520529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch// static
5535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)inline TimeDelta TimeDelta::FromMicroseconds(int64 us) {
554a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // Preserve max to prevent overflow.
555a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (us == std::numeric_limits<int64>::max())
556a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    return Max();
5575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return TimeDelta(us);
5585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
5595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)inline Time TimeDelta::operator+(Time t) const {
5615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return Time(t.us_ + delta_);
5625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
5635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// TimeTicks ------------------------------------------------------------------
5655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class BASE_EXPORT TimeTicks {
5675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
56846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  // We define this even without OS_CHROMEOS for seccomp sandbox testing.
56946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#if defined(OS_LINUX)
57046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  // Force definition of the system trace clock; it is a chromeos-only api
57146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  // at the moment and surfacing it in the right place requires mucking
57246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  // with glibc et al.
57346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  static const clockid_t kClockSystemTrace = 11;
57446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#endif
57546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
5765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  TimeTicks() : ticks_(0) {
5775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
5785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Platform-dependent tick count representing "right now."
5805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The resolution of this clock is ~1-15ms.  Resolution varies depending
5815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // on hardware/operating system configuration.
5825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static TimeTicks Now();
5835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns a platform-dependent high-resolution tick count. Implementation
5855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // is hardware dependent and may or may not return sub-millisecond
5865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // resolution.  THIS CALL IS GENERALLY MUCH MORE EXPENSIVE THAN Now() AND
5875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // SHOULD ONLY BE USED WHEN IT IS REALLY NEEDED.
5885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static TimeTicks HighResNow();
5895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5904e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  static bool IsHighResNowFastAndReliable();
5914e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
592a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  // Returns true if ThreadNow() is supported on this system.
593a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  static bool IsThreadNowSupported() {
594f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#if (defined(_POSIX_THREAD_CPUTIME) && (_POSIX_THREAD_CPUTIME >= 0)) || \
595f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    (defined(OS_MACOSX) && !defined(OS_IOS)) || defined(OS_ANDROID)
596a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    return true;
597a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)#else
598a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    return false;
599a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)#endif
600a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  }
601a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
602a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  // Returns thread-specific CPU-time on systems that support this feature.
603a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  // Needs to be guarded with a call to IsThreadNowSupported(). Use this timer
604a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  // to (approximately) measure how much time the calling thread spent doing
605a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  // actual work vs. being de-scheduled. May return bogus results if the thread
606a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  // migrates to another CPU between two calls.
607a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  static TimeTicks ThreadNow();
608a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
6095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns the current system trace time or, if none is defined, the current
6105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // high-res time (i.e. HighResNow()). On systems where a global trace clock
6115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // is defined, timestamping TraceEvents's with this value guarantees
6125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // synchronization between events collected inside chrome and events
6135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // collected outside (e.g. kernel, X server).
6145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static TimeTicks NowFromSystemTraceTime();
6155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(OS_WIN)
6175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Get the absolute value of QPC time drift. For testing.
6185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static int64 GetQPCDriftMicroseconds();
6195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static TimeTicks FromQPCValue(LONGLONG qpc_value);
6215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns true if the high resolution clock is working on this system.
6235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // This is only for testing.
6245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static bool IsHighResClockWorking();
62558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
62658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // Returns a time value that is NOT rollover protected.
62758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  static TimeTicks UnprotectedNow();
6285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
6295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns true if this object has not been initialized.
6315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool is_null() const {
6325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return ticks_ == 0;
6335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
6345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Converts an integer value representing TimeTicks to a class. This is used
6365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // when deserializing a |TimeTicks| structure, using a value known to be
6375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // compatible. It is not provided as a constructor because the integer type
6385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // may be unclear from the perspective of a caller.
6395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static TimeTicks FromInternalValue(int64 ticks) {
6405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return TimeTicks(ticks);
6415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
6425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6430f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // Get the TimeTick value at the time of the UnixEpoch. This is useful when
6440f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // you need to relate the value of TimeTicks to a real time and date.
6450f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // Note: Upon first invocation, this function takes a snapshot of the realtime
6460f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // clock to establish a reference point.  This function will return the same
6470f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // value for the duration of the application, but will be different in future
6480f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // application runs.
6490f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  static TimeTicks UnixEpoch();
6500f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
6515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns the internal numeric value of the TimeTicks object.
6525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // For serializing, use FromInternalValue to reconstitute.
6535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int64 ToInternalValue() const {
6545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return ticks_;
6555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
6565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  TimeTicks& operator=(TimeTicks other) {
6585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ticks_ = other.ticks_;
6595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return *this;
6605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
6615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Compute the difference between two times.
6635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  TimeDelta operator-(TimeTicks other) const {
6645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return TimeDelta(ticks_ - other.ticks_);
6655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
6665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Modify by some time delta.
6685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  TimeTicks& operator+=(TimeDelta delta) {
6695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ticks_ += delta.delta_;
6705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return *this;
6715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
6725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  TimeTicks& operator-=(TimeDelta delta) {
6735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ticks_ -= delta.delta_;
6745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return *this;
6755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
6765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Return a new TimeTicks modified by some delta.
6785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  TimeTicks operator+(TimeDelta delta) const {
6795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return TimeTicks(ticks_ + delta.delta_);
6805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
6815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  TimeTicks operator-(TimeDelta delta) const {
6825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return TimeTicks(ticks_ - delta.delta_);
6835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
6845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Comparison operators
6865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool operator==(TimeTicks other) const {
6875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return ticks_ == other.ticks_;
6885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
6895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool operator!=(TimeTicks other) const {
6905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return ticks_ != other.ticks_;
6915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
6925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool operator<(TimeTicks other) const {
6935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return ticks_ < other.ticks_;
6945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
6955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool operator<=(TimeTicks other) const {
6965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return ticks_ <= other.ticks_;
6975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
6985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool operator>(TimeTicks other) const {
6995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return ticks_ > other.ticks_;
7005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
7015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool operator>=(TimeTicks other) const {
7025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return ticks_ >= other.ticks_;
7035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
7045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) protected:
7065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  friend class TimeDelta;
7075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Please use Now() to create a new object. This is for internal use
7095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // and testing. Ticks is in microseconds.
7105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  explicit TimeTicks(int64 ticks) : ticks_(ticks) {
7115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
7125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Tick count in microseconds.
7145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int64 ticks_;
7155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(OS_WIN)
7175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  typedef DWORD (*TickFunctionType)(void);
7185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static TickFunctionType SetMockTickFunction(TickFunctionType ticker);
7195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
7205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
7215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)inline TimeTicks TimeDelta::operator+(TimeTicks t) const {
7235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return TimeTicks(t.ticks_ + delta_);
7245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
7255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace base
7275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
728eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#endif  // BASE_TIME_TIME_H_
729