| OLD | NEW | 
|    1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |    1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 
|    2 // Use of this source code is governed by a BSD-style license that can be |    2 // Use of this source code is governed by a BSD-style license that can be | 
|    3 // found in the LICENSE file. |    3 // found in the LICENSE file. | 
|    4  |    4  | 
|    5 // Time represents an absolute point in coordinated universal time (UTC), |    5 // Time represents an absolute point in coordinated universal time (UTC), | 
|    6 // internally represented as microseconds (s/1,000,000) since the Windows epoch |    6 // internally represented as microseconds (s/1,000,000) since the Windows epoch | 
|    7 // (1601-01-01 00:00:00 UTC) (See http://crbug.com/14734).  System-dependent |    7 // (1601-01-01 00:00:00 UTC) (See http://crbug.com/14734).  System-dependent | 
|    8 // clock interface routines are defined in time_PLATFORM.cc. |    8 // clock interface routines are defined in time_PLATFORM.cc. | 
|    9 // |    9 // | 
|   10 // TimeDelta represents a duration of time, internally represented in |   10 // TimeDelta represents a duration of time, internally represented in | 
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|   50 #if defined(OS_WIN) |   50 #if defined(OS_WIN) | 
|   51 // For FILETIME in FromFileTime, until it moves to a new converter class. |   51 // For FILETIME in FromFileTime, until it moves to a new converter class. | 
|   52 // See TODO(iyengar) below. |   52 // See TODO(iyengar) below. | 
|   53 #include <windows.h> |   53 #include <windows.h> | 
|   54 #endif |   54 #endif | 
|   55  |   55  | 
|   56 #include <limits> |   56 #include <limits> | 
|   57  |   57  | 
|   58 namespace base { |   58 namespace base { | 
|   59  |   59  | 
|   60 class TimeDelta; |   60 class Time; | 
|   61  |   61 class TimeTicks; | 
|   62 // The functions in the time_internal namespace are meant to be used only by the |  | 
|   63 // time classes and functions.  Please use the math operators defined in the |  | 
|   64 // time classes instead. |  | 
|   65 namespace time_internal { |  | 
|   66  |  | 
|   67 // Add or subtract |value| from a TimeDelta. The int64 argument and return value |  | 
|   68 // are in terms of a microsecond timebase. |  | 
|   69 BASE_EXPORT int64 SaturatedAdd(TimeDelta delta, int64 value); |  | 
|   70 BASE_EXPORT int64 SaturatedSub(TimeDelta delta, int64 value); |  | 
|   71  |  | 
|   72 // Clamp |value| on overflow and underflow conditions. The int64 argument and |  | 
|   73 // return value are in terms of a microsecond timebase. |  | 
|   74 BASE_EXPORT int64 FromCheckedNumeric(const CheckedNumeric<int64> value); |  | 
|   75  |  | 
|   76 }  // namespace time_internal |  | 
|   77  |   62  | 
|   78 // TimeDelta ------------------------------------------------------------------ |   63 // TimeDelta ------------------------------------------------------------------ | 
|   79  |   64  | 
|   80 class BASE_EXPORT TimeDelta { |   65 class BASE_EXPORT TimeDelta { | 
|   81  public: |   66  public: | 
|   82   TimeDelta() : delta_(0) { |   67   TimeDelta() : delta_(0) { | 
|   83   } |   68   } | 
|   84  |   69  | 
|   85   // Converts units of time to TimeDeltas. |   70   // Converts units of time to TimeDeltas. | 
|   86   static TimeDelta FromDays(int days); |   71   static TimeDelta FromDays(int days); | 
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  118  |  103  | 
|  119   // Returns the magnitude (absolute value) of this TimeDelta. |  104   // Returns the magnitude (absolute value) of this TimeDelta. | 
|  120   TimeDelta magnitude() const { |  105   TimeDelta magnitude() const { | 
|  121     // Some toolchains provide an incomplete C++11 implementation and lack an |  106     // Some toolchains provide an incomplete C++11 implementation and lack an | 
|  122     // int64 overload for std::abs().  The following is a simple branchless |  107     // int64 overload for std::abs().  The following is a simple branchless | 
|  123     // implementation: |  108     // implementation: | 
|  124     const int64 mask = delta_ >> (sizeof(delta_) * 8 - 1); |  109     const int64 mask = delta_ >> (sizeof(delta_) * 8 - 1); | 
|  125     return TimeDelta((delta_ + mask) ^ mask); |  110     return TimeDelta((delta_ + mask) ^ mask); | 
|  126   } |  111   } | 
|  127  |  112  | 
|  128   // Returns true if the time delta is zero. |  | 
|  129   bool is_zero() const { |  | 
|  130     return delta_ == 0; |  | 
|  131   } |  | 
|  132  |  | 
|  133   // Returns true if the time delta is the maximum time delta. |  113   // Returns true if the time delta is the maximum time delta. | 
|  134   bool is_max() const { |  114   bool is_max() const { | 
|  135     return delta_ == std::numeric_limits<int64>::max(); |  115     return delta_ == std::numeric_limits<int64>::max(); | 
|  136   } |  116   } | 
|  137  |  117  | 
|  138 #if defined(OS_POSIX) |  118 #if defined(OS_POSIX) | 
|  139   struct timespec ToTimeSpec() const; |  119   struct timespec ToTimeSpec() const; | 
|  140 #endif |  120 #endif | 
|  141  |  121  | 
|  142   // Returns the time delta in some unit. The F versions return a floating |  122   // Returns the time delta in some unit. The F versions return a floating | 
| (...skipping 11 matching lines...) Expand all  Loading... | 
|  154   int64 InMillisecondsRoundedUp() const; |  134   int64 InMillisecondsRoundedUp() const; | 
|  155   int64 InMicroseconds() const; |  135   int64 InMicroseconds() const; | 
|  156  |  136  | 
|  157   TimeDelta& operator=(TimeDelta other) { |  137   TimeDelta& operator=(TimeDelta other) { | 
|  158     delta_ = other.delta_; |  138     delta_ = other.delta_; | 
|  159     return *this; |  139     return *this; | 
|  160   } |  140   } | 
|  161  |  141  | 
|  162   // Computations with other deltas. |  142   // Computations with other deltas. | 
|  163   TimeDelta operator+(TimeDelta other) const { |  143   TimeDelta operator+(TimeDelta other) const { | 
|  164     return TimeDelta(time_internal::SaturatedAdd(*this, other.delta_)); |  144     return TimeDelta(SaturatedAdd(other.delta_)); | 
|  165   } |  145   } | 
|  166   TimeDelta operator-(TimeDelta other) const { |  146   TimeDelta operator-(TimeDelta other) const { | 
|  167     return TimeDelta(time_internal::SaturatedSub(*this, other.delta_)); |  147     return TimeDelta(SaturatedSub(other.delta_)); | 
|  168   } |  148   } | 
|  169  |  149  | 
|  170   TimeDelta& operator+=(TimeDelta other) { |  150   TimeDelta& operator+=(TimeDelta other) { | 
|  171     return *this = (*this + other); |  151     delta_ = SaturatedAdd(other.delta_); | 
 |  152     return *this; | 
|  172   } |  153   } | 
|  173   TimeDelta& operator-=(TimeDelta other) { |  154   TimeDelta& operator-=(TimeDelta other) { | 
|  174     return *this = (*this - other); |  155     delta_ = SaturatedSub(other.delta_); | 
 |  156     return *this; | 
|  175   } |  157   } | 
|  176   TimeDelta operator-() const { |  158   TimeDelta operator-() const { | 
|  177     return TimeDelta(-delta_); |  159     return TimeDelta(-delta_); | 
|  178   } |  160   } | 
|  179  |  161  | 
|  180   // Computations with numeric types. |  162   // Computations with numeric types. | 
|  181   template<typename T> |  163   template<typename T> | 
|  182   TimeDelta operator*(T a) const { |  164   TimeDelta operator*(T a) const { | 
|  183     CheckedNumeric<int64> rv(delta_); |  165     CheckedNumeric<int64> rv(delta_); | 
|  184     rv *= a; |  166     rv *= a; | 
|  185     return TimeDelta(time_internal::FromCheckedNumeric(rv)); |  167     return TimeDelta(FromCheckedNumeric(rv)); | 
|  186   } |  168   } | 
|  187   template<typename T> |  169   template<typename T> | 
|  188   TimeDelta operator/(T a) const { |  170   TimeDelta operator/(T a) const { | 
|  189     CheckedNumeric<int64> rv(delta_); |  171     CheckedNumeric<int64> rv(delta_); | 
|  190     rv /= a; |  172     rv /= a; | 
|  191     return TimeDelta(time_internal::FromCheckedNumeric(rv)); |  173     return TimeDelta(FromCheckedNumeric(rv)); | 
|  192   } |  174   } | 
|  193   template<typename T> |  175   template<typename T> | 
|  194   TimeDelta& operator*=(T a) { |  176   TimeDelta& operator*=(T a) { | 
|  195     return *this = (*this * a); |  177     CheckedNumeric<int64> rv(delta_); | 
 |  178     rv *= a; | 
 |  179     delta_ = FromCheckedNumeric(rv); | 
 |  180     return *this; | 
|  196   } |  181   } | 
|  197   template<typename T> |  182   template<typename T> | 
|  198   TimeDelta& operator/=(T a) { |  183   TimeDelta& operator/=(T a) { | 
|  199     return *this = (*this / a); |  184     CheckedNumeric<int64> rv(delta_); | 
 |  185     rv /= a; | 
 |  186     delta_ = FromCheckedNumeric(rv); | 
 |  187     return *this; | 
|  200   } |  188   } | 
|  201  |  189  | 
|  202   int64 operator/(TimeDelta a) const { |  190   int64 operator/(TimeDelta a) const { | 
|  203     return delta_ / a.delta_; |  191     return delta_ / a.delta_; | 
|  204   } |  192   } | 
|  205   TimeDelta operator%(TimeDelta a) const { |  193  | 
|  206     return TimeDelta(delta_ % a.delta_); |  194   // Defined below because it depends on the definition of the other classes. | 
|  207   } |  195   Time operator+(Time t) const; | 
 |  196   TimeTicks operator+(TimeTicks t) const; | 
|  208  |  197  | 
|  209   // Comparison operators. |  198   // Comparison operators. | 
|  210   bool operator==(TimeDelta other) const { |  199   bool operator==(TimeDelta other) const { | 
|  211     return delta_ == other.delta_; |  200     return delta_ == other.delta_; | 
|  212   } |  201   } | 
|  213   bool operator!=(TimeDelta other) const { |  202   bool operator!=(TimeDelta other) const { | 
|  214     return delta_ != other.delta_; |  203     return delta_ != other.delta_; | 
|  215   } |  204   } | 
|  216   bool operator<(TimeDelta other) const { |  205   bool operator<(TimeDelta other) const { | 
|  217     return delta_ < other.delta_; |  206     return delta_ < other.delta_; | 
|  218   } |  207   } | 
|  219   bool operator<=(TimeDelta other) const { |  208   bool operator<=(TimeDelta other) const { | 
|  220     return delta_ <= other.delta_; |  209     return delta_ <= other.delta_; | 
|  221   } |  210   } | 
|  222   bool operator>(TimeDelta other) const { |  211   bool operator>(TimeDelta other) const { | 
|  223     return delta_ > other.delta_; |  212     return delta_ > other.delta_; | 
|  224   } |  213   } | 
|  225   bool operator>=(TimeDelta other) const { |  214   bool operator>=(TimeDelta other) const { | 
|  226     return delta_ >= other.delta_; |  215     return delta_ >= other.delta_; | 
|  227   } |  216   } | 
|  228  |  217  | 
|  229  private: |  218  private: | 
|  230   friend int64 time_internal::SaturatedAdd(TimeDelta delta, int64 value); |  219   friend class Time; | 
|  231   friend int64 time_internal::SaturatedSub(TimeDelta delta, int64 value); |  220   friend class TimeTicks; | 
|  232  |  221  | 
|  233   // Constructs a delta given the duration in microseconds. This is private |  222   // Constructs a delta given the duration in microseconds. This is private | 
|  234   // to avoid confusion by callers with an integer constructor. Use |  223   // to avoid confusion by callers with an integer constructor. Use | 
|  235   // FromSeconds, FromMilliseconds, etc. instead. |  224   // FromSeconds, FromMilliseconds, etc. instead. | 
|  236   explicit TimeDelta(int64 delta_us) : delta_(delta_us) { |  225   explicit TimeDelta(int64 delta_us) : delta_(delta_us) { | 
|  237   } |  226   } | 
|  238  |  227  | 
 |  228   // Add or subtract |value| from this delta. | 
 |  229   int64 SaturatedAdd(int64 value) const; | 
 |  230   int64 SaturatedSub(int64 value) const; | 
 |  231  | 
 |  232   // Clamp |value| on overflow and underflow conditions. | 
 |  233   static int64 FromCheckedNumeric(const CheckedNumeric<int64> value); | 
 |  234  | 
|  239   // Delta in microseconds. |  235   // Delta in microseconds. | 
|  240   int64 delta_; |  236   int64 delta_; | 
|  241 }; |  237 }; | 
|  242  |  238  | 
|  243 template<typename T> |  239 template<typename T> | 
|  244 inline TimeDelta operator*(T a, TimeDelta td) { |  240 inline TimeDelta operator*(T a, TimeDelta td) { | 
|  245   return td * a; |  241   return td * a; | 
|  246 } |  242 } | 
|  247  |  243  | 
|  248 // For logging use only. |  244 // For logging use only. | 
|  249 BASE_EXPORT std::ostream& operator<<(std::ostream& os, TimeDelta time_delta); |  245 BASE_EXPORT std::ostream& operator<<(std::ostream& os, TimeDelta time_delta); | 
|  250  |  246  | 
|  251 // Do not reference the time_internal::TimeBase template class directly.  Please |  247 // Time ----------------------------------------------------------------------- | 
|  252 // use one of the time subclasses instead, and only reference the public |  | 
|  253 // TimeBase members via those classes. |  | 
|  254 namespace time_internal { |  | 
|  255  |  248  | 
|  256 // TimeBase-------------------------------------------------------------------- |  249 // Represents a wall clock time in UTC. | 
|  257  |  250 class BASE_EXPORT Time { | 
|  258 // Provides value storage and comparison/math operations common to all time |  | 
|  259 // classes. Each subclass provides for strong type-checking to ensure |  | 
|  260 // semantically meaningful comparison/math of time values from the same clock |  | 
|  261 // source or timeline. |  | 
|  262 template<class TimeClass> |  | 
|  263 class TimeBase { |  | 
|  264  public: |  251  public: | 
|  265   static const int64 kHoursPerDay = 24; |  252   static const int64 kHoursPerDay = 24; | 
|  266   static const int64 kMillisecondsPerSecond = 1000; |  253   static const int64 kMillisecondsPerSecond = 1000; | 
|  267   static const int64 kMillisecondsPerDay = kMillisecondsPerSecond * 60 * 60 * |  254   static const int64 kMillisecondsPerDay = kMillisecondsPerSecond * 60 * 60 * | 
|  268                                            kHoursPerDay; |  255                                            kHoursPerDay; | 
|  269   static const int64 kMicrosecondsPerMillisecond = 1000; |  256   static const int64 kMicrosecondsPerMillisecond = 1000; | 
|  270   static const int64 kMicrosecondsPerSecond = kMicrosecondsPerMillisecond * |  257   static const int64 kMicrosecondsPerSecond = kMicrosecondsPerMillisecond * | 
|  271                                               kMillisecondsPerSecond; |  258                                               kMillisecondsPerSecond; | 
|  272   static const int64 kMicrosecondsPerMinute = kMicrosecondsPerSecond * 60; |  259   static const int64 kMicrosecondsPerMinute = kMicrosecondsPerSecond * 60; | 
|  273   static const int64 kMicrosecondsPerHour = kMicrosecondsPerMinute * 60; |  260   static const int64 kMicrosecondsPerHour = kMicrosecondsPerMinute * 60; | 
|  274   static const int64 kMicrosecondsPerDay = kMicrosecondsPerHour * kHoursPerDay; |  261   static const int64 kMicrosecondsPerDay = kMicrosecondsPerHour * kHoursPerDay; | 
|  275   static const int64 kMicrosecondsPerWeek = kMicrosecondsPerDay * 7; |  262   static const int64 kMicrosecondsPerWeek = kMicrosecondsPerDay * 7; | 
|  276   static const int64 kNanosecondsPerMicrosecond = 1000; |  263   static const int64 kNanosecondsPerMicrosecond = 1000; | 
|  277   static const int64 kNanosecondsPerSecond = kNanosecondsPerMicrosecond * |  264   static const int64 kNanosecondsPerSecond = kNanosecondsPerMicrosecond * | 
|  278                                              kMicrosecondsPerSecond; |  265                                              kMicrosecondsPerSecond; | 
|  279  |  266  | 
|  280   // Returns true if this object has not been initialized. |  | 
|  281   // |  | 
|  282   // Warning: Be careful when writing code that performs math on time values, |  | 
|  283   // since it's possible to produce a valid "zero" result that should not be |  | 
|  284   // interpreted as a "null" value. |  | 
|  285   bool is_null() const { |  | 
|  286     return us_ == 0; |  | 
|  287   } |  | 
|  288  |  | 
|  289   // Returns true if this object represents the maximum time. |  | 
|  290   bool is_max() const { |  | 
|  291     return us_ == std::numeric_limits<int64>::max(); |  | 
|  292   } |  | 
|  293  |  | 
|  294   // For serializing only. Use FromInternalValue() to reconstitute. Please don't |  | 
|  295   // use this and do arithmetic on it, as it is more error prone than using the |  | 
|  296   // provided operators. |  | 
|  297   int64 ToInternalValue() const { |  | 
|  298     return us_; |  | 
|  299   } |  | 
|  300  |  | 
|  301   TimeClass& operator=(TimeClass other) { |  | 
|  302     us_ = other.us_; |  | 
|  303     return *(static_cast<TimeClass*>(this)); |  | 
|  304   } |  | 
|  305  |  | 
|  306   // Compute the difference between two times. |  | 
|  307   TimeDelta operator-(TimeClass other) const { |  | 
|  308     return TimeDelta::FromMicroseconds(us_ - other.us_); |  | 
|  309   } |  | 
|  310  |  | 
|  311   // Return a new time modified by some delta. |  | 
|  312   TimeClass operator+(TimeDelta delta) const { |  | 
|  313     return TimeClass(time_internal::SaturatedAdd(delta, us_)); |  | 
|  314   } |  | 
|  315   TimeClass operator-(TimeDelta delta) const { |  | 
|  316     return TimeClass(-time_internal::SaturatedSub(delta, us_)); |  | 
|  317   } |  | 
|  318  |  | 
|  319   // Modify by some time delta. |  | 
|  320   TimeClass& operator+=(TimeDelta delta) { |  | 
|  321     return static_cast<TimeClass&>(*this = (*this + delta)); |  | 
|  322   } |  | 
|  323   TimeClass& operator-=(TimeDelta delta) { |  | 
|  324     return static_cast<TimeClass&>(*this = (*this - delta)); |  | 
|  325   } |  | 
|  326  |  | 
|  327   // Comparison operators |  | 
|  328   bool operator==(TimeClass other) const { |  | 
|  329     return us_ == other.us_; |  | 
|  330   } |  | 
|  331   bool operator!=(TimeClass other) const { |  | 
|  332     return us_ != other.us_; |  | 
|  333   } |  | 
|  334   bool operator<(TimeClass other) const { |  | 
|  335     return us_ < other.us_; |  | 
|  336   } |  | 
|  337   bool operator<=(TimeClass other) const { |  | 
|  338     return us_ <= other.us_; |  | 
|  339   } |  | 
|  340   bool operator>(TimeClass other) const { |  | 
|  341     return us_ > other.us_; |  | 
|  342   } |  | 
|  343   bool operator>=(TimeClass other) const { |  | 
|  344     return us_ >= other.us_; |  | 
|  345   } |  | 
|  346  |  | 
|  347   // Converts an integer value representing TimeClass to a class. This is used |  | 
|  348   // when deserializing a |TimeClass| structure, using a value known to be |  | 
|  349   // compatible. It is not provided as a constructor because the integer type |  | 
|  350   // may be unclear from the perspective of a caller. |  | 
|  351   static TimeClass FromInternalValue(int64 us) { |  | 
|  352     return TimeClass(us); |  | 
|  353   } |  | 
|  354  |  | 
|  355  protected: |  | 
|  356   explicit TimeBase(int64 us) : us_(us) { |  | 
|  357   } |  | 
|  358  |  | 
|  359   // Time value in a microsecond timebase. |  | 
|  360   int64 us_; |  | 
|  361 }; |  | 
|  362  |  | 
|  363 }  // namespace time_internal |  | 
|  364  |  | 
|  365 template<class TimeClass> |  | 
|  366 inline TimeClass operator+(TimeDelta delta, TimeClass t) { |  | 
|  367   return t + delta; |  | 
|  368 } |  | 
|  369  |  | 
|  370 // Time ----------------------------------------------------------------------- |  | 
|  371  |  | 
|  372 // Represents a wall clock time in UTC. Values are not guaranteed to be |  | 
|  373 // monotonically non-decreasing and are subject to large amounts of skew. |  | 
|  374 class BASE_EXPORT Time : public time_internal::TimeBase<Time> { |  | 
|  375  public: |  | 
|  376   // The representation of Jan 1, 1970 UTC in microseconds since the |  267   // The representation of Jan 1, 1970 UTC in microseconds since the | 
|  377   // platform-dependent epoch. |  268   // platform-dependent epoch. | 
|  378   static const int64 kTimeTToMicrosecondsOffset; |  269   static const int64 kTimeTToMicrosecondsOffset; | 
|  379  |  270  | 
|  380 #if !defined(OS_WIN) |  271 #if !defined(OS_WIN) | 
|  381   // On Mac & Linux, this value is the delta from the Windows epoch of 1601 to |  272   // On Mac & Linux, this value is the delta from the Windows epoch of 1601 to | 
|  382   // the Posix delta of 1970. This is used for migrating between the old |  273   // the Posix delta of 1970. This is used for migrating between the old | 
|  383   // 1970-based epochs to the new 1601-based ones. It should be removed from |  274   // 1970-based epochs to the new 1601-based ones. It should be removed from | 
|  384   // this global header and put in the platform-specific ones when we remove the |  275   // this global header and put in the platform-specific ones when we remove the | 
|  385   // migration code. |  276   // migration code. | 
| (...skipping 19 matching lines...) Expand all  Loading... | 
|  405                        //   seconds which may take it up to 60). |  296                        //   seconds which may take it up to 60). | 
|  406     int millisecond;   // Milliseconds within the current second (0-999) |  297     int millisecond;   // Milliseconds within the current second (0-999) | 
|  407  |  298  | 
|  408     // A cursory test for whether the data members are within their |  299     // A cursory test for whether the data members are within their | 
|  409     // respective ranges. A 'true' return value does not guarantee the |  300     // respective ranges. A 'true' return value does not guarantee the | 
|  410     // Exploded value can be successfully converted to a Time value. |  301     // Exploded value can be successfully converted to a Time value. | 
|  411     bool HasValidValues() const; |  302     bool HasValidValues() const; | 
|  412   }; |  303   }; | 
|  413  |  304  | 
|  414   // Contains the NULL time. Use Time::Now() to get the current time. |  305   // Contains the NULL time. Use Time::Now() to get the current time. | 
|  415   Time() : TimeBase(0) { |  306   Time() : us_(0) { | 
 |  307   } | 
 |  308  | 
 |  309   // Returns true if the time object has not been initialized. | 
 |  310   bool is_null() const { | 
 |  311     return us_ == 0; | 
 |  312   } | 
 |  313  | 
 |  314   // Returns true if the time object is the maximum time. | 
 |  315   bool is_max() const { | 
 |  316     return us_ == std::numeric_limits<int64>::max(); | 
|  416   } |  317   } | 
|  417  |  318  | 
|  418   // Returns the time for epoch in Unix-like system (Jan 1, 1970). |  319   // Returns the time for epoch in Unix-like system (Jan 1, 1970). | 
|  419   static Time UnixEpoch(); |  320   static Time UnixEpoch(); | 
|  420  |  321  | 
|  421   // Returns the current time. Watch out, the system might adjust its clock |  322   // Returns the current time. Watch out, the system might adjust its clock | 
|  422   // in which case time will actually go backwards. We don't guarantee that |  323   // in which case time will actually go backwards. We don't guarantee that | 
|  423   // times are increasing, or that two calls to Now() won't be the same. |  324   // times are increasing, or that two calls to Now() won't be the same. | 
|  424   static Time Now(); |  325   static Time Now(); | 
|  425  |  326  | 
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  504  |  405  | 
|  505   // Converts an exploded structure representing either the local time or UTC |  406   // Converts an exploded structure representing either the local time or UTC | 
|  506   // into a Time class. |  407   // into a Time class. | 
|  507   static Time FromUTCExploded(const Exploded& exploded) { |  408   static Time FromUTCExploded(const Exploded& exploded) { | 
|  508     return FromExploded(false, exploded); |  409     return FromExploded(false, exploded); | 
|  509   } |  410   } | 
|  510   static Time FromLocalExploded(const Exploded& exploded) { |  411   static Time FromLocalExploded(const Exploded& exploded) { | 
|  511     return FromExploded(true, exploded); |  412     return FromExploded(true, exploded); | 
|  512   } |  413   } | 
|  513  |  414  | 
 |  415   // Converts an integer value representing Time to a class. This is used | 
 |  416   // when deserializing a |Time| structure, using a value known to be | 
 |  417   // compatible. It is not provided as a constructor because the integer type | 
 |  418   // may be unclear from the perspective of a caller. | 
 |  419   static Time FromInternalValue(int64 us) { | 
 |  420     return Time(us); | 
 |  421   } | 
 |  422  | 
|  514   // Converts a string representation of time to a Time object. |  423   // Converts a string representation of time to a Time object. | 
|  515   // An example of a time string which is converted is as below:- |  424   // An example of a time string which is converted is as below:- | 
|  516   // "Tue, 15 Nov 1994 12:45:26 GMT". If the timezone is not specified |  425   // "Tue, 15 Nov 1994 12:45:26 GMT". If the timezone is not specified | 
|  517   // in the input string, FromString assumes local time and FromUTCString |  426   // in the input string, FromString assumes local time and FromUTCString | 
|  518   // assumes UTC. A timezone that cannot be parsed (e.g. "UTC" which is not |  427   // assumes UTC. A timezone that cannot be parsed (e.g. "UTC" which is not | 
|  519   // specified in RFC822) is treated as if the timezone is not specified. |  428   // specified in RFC822) is treated as if the timezone is not specified. | 
|  520   // TODO(iyengar) Move the FromString/FromTimeT/ToTimeT/FromFileTime to |  429   // TODO(iyengar) Move the FromString/FromTimeT/ToTimeT/FromFileTime to | 
|  521   // a new time converter class. |  430   // a new time converter class. | 
|  522   static bool FromString(const char* time_string, Time* parsed_time) { |  431   static bool FromString(const char* time_string, Time* parsed_time) { | 
|  523     return FromStringInternal(time_string, true, parsed_time); |  432     return FromStringInternal(time_string, true, parsed_time); | 
|  524   } |  433   } | 
|  525   static bool FromUTCString(const char* time_string, Time* parsed_time) { |  434   static bool FromUTCString(const char* time_string, Time* parsed_time) { | 
|  526     return FromStringInternal(time_string, false, parsed_time); |  435     return FromStringInternal(time_string, false, parsed_time); | 
|  527   } |  436   } | 
|  528  |  437  | 
 |  438   // For serializing, use FromInternalValue to reconstitute. Please don't use | 
 |  439   // this and do arithmetic on it, as it is more error prone than using the | 
 |  440   // provided operators. | 
 |  441   int64 ToInternalValue() const { | 
 |  442     return us_; | 
 |  443   } | 
 |  444  | 
|  529   // Fills the given exploded structure with either the local time or UTC from |  445   // Fills the given exploded structure with either the local time or UTC from | 
|  530   // this time structure (containing UTC). |  446   // this time structure (containing UTC). | 
|  531   void UTCExplode(Exploded* exploded) const { |  447   void UTCExplode(Exploded* exploded) const { | 
|  532     return Explode(false, exploded); |  448     return Explode(false, exploded); | 
|  533   } |  449   } | 
|  534   void LocalExplode(Exploded* exploded) const { |  450   void LocalExplode(Exploded* exploded) const { | 
|  535     return Explode(true, exploded); |  451     return Explode(true, exploded); | 
|  536   } |  452   } | 
|  537  |  453  | 
|  538   // Rounds this time down to the nearest day in local time. It will represent |  454   // Rounds this time down to the nearest day in local time. It will represent | 
|  539   // midnight on that day. |  455   // midnight on that day. | 
|  540   Time LocalMidnight() const; |  456   Time LocalMidnight() const; | 
|  541  |  457  | 
 |  458   Time& operator=(Time other) { | 
 |  459     us_ = other.us_; | 
 |  460     return *this; | 
 |  461   } | 
 |  462  | 
 |  463   // Compute the difference between two times. | 
 |  464   TimeDelta operator-(Time other) const { | 
 |  465     return TimeDelta(us_ - other.us_); | 
 |  466   } | 
 |  467  | 
 |  468   // Modify by some time delta. | 
 |  469   Time& operator+=(TimeDelta delta) { | 
 |  470     us_ = delta.SaturatedAdd(us_); | 
 |  471     return *this; | 
 |  472   } | 
 |  473   Time& operator-=(TimeDelta delta) { | 
 |  474     us_ = -delta.SaturatedSub(us_); | 
 |  475     return *this; | 
 |  476   } | 
 |  477  | 
 |  478   // Return a new time modified by some delta. | 
 |  479   Time operator+(TimeDelta delta) const { | 
 |  480     return Time(delta.SaturatedAdd(us_)); | 
 |  481   } | 
 |  482   Time operator-(TimeDelta delta) const { | 
 |  483     return Time(-delta.SaturatedSub(us_)); | 
 |  484   } | 
 |  485  | 
 |  486   // Comparison operators | 
 |  487   bool operator==(Time other) const { | 
 |  488     return us_ == other.us_; | 
 |  489   } | 
 |  490   bool operator!=(Time other) const { | 
 |  491     return us_ != other.us_; | 
 |  492   } | 
 |  493   bool operator<(Time other) const { | 
 |  494     return us_ < other.us_; | 
 |  495   } | 
 |  496   bool operator<=(Time other) const { | 
 |  497     return us_ <= other.us_; | 
 |  498   } | 
 |  499   bool operator>(Time other) const { | 
 |  500     return us_ > other.us_; | 
 |  501   } | 
 |  502   bool operator>=(Time other) const { | 
 |  503     return us_ >= other.us_; | 
 |  504   } | 
 |  505  | 
|  542  private: |  506  private: | 
|  543   friend class time_internal::TimeBase<Time>; |  507   friend class TimeDelta; | 
|  544  |  508  | 
|  545   explicit Time(int64 us) : TimeBase(us) { |  509   explicit Time(int64 us) : us_(us) { | 
|  546   } |  510   } | 
|  547  |  511  | 
|  548   // Explodes the given time to either local time |is_local = true| or UTC |  512   // Explodes the given time to either local time |is_local = true| or UTC | 
|  549   // |is_local = false|. |  513   // |is_local = false|. | 
|  550   void Explode(bool is_local, Exploded* exploded) const; |  514   void Explode(bool is_local, Exploded* exploded) const; | 
|  551  |  515  | 
|  552   // Unexplodes a given time assuming the source is either local time |  516   // Unexplodes a given time assuming the source is either local time | 
|  553   // |is_local = true| or UTC |is_local = false|. |  517   // |is_local = true| or UTC |is_local = false|. | 
|  554   static Time FromExploded(bool is_local, const Exploded& exploded); |  518   static Time FromExploded(bool is_local, const Exploded& exploded); | 
|  555  |  519  | 
|  556   // Converts a string representation of time to a Time object. |  520   // Converts a string representation of time to a Time object. | 
|  557   // An example of a time string which is converted is as below:- |  521   // An example of a time string which is converted is as below:- | 
|  558   // "Tue, 15 Nov 1994 12:45:26 GMT". If the timezone is not specified |  522   // "Tue, 15 Nov 1994 12:45:26 GMT". If the timezone is not specified | 
|  559   // in the input string, local time |is_local = true| or |  523   // in the input string, local time |is_local = true| or | 
|  560   // UTC |is_local = false| is assumed. A timezone that cannot be parsed |  524   // UTC |is_local = false| is assumed. A timezone that cannot be parsed | 
|  561   // (e.g. "UTC" which is not specified in RFC822) is treated as if the |  525   // (e.g. "UTC" which is not specified in RFC822) is treated as if the | 
|  562   // timezone is not specified. |  526   // timezone is not specified. | 
|  563   static bool FromStringInternal(const char* time_string, |  527   static bool FromStringInternal(const char* time_string, | 
|  564                                  bool is_local, |  528                                  bool is_local, | 
|  565                                  Time* parsed_time); |  529                                  Time* parsed_time); | 
 |  530  | 
 |  531   // Time in microseconds in UTC. | 
 |  532   int64 us_; | 
|  566 }; |  533 }; | 
|  567  |  534  | 
|  568 // Inline the TimeDelta factory methods, for fast TimeDelta construction. |  535 // Inline the TimeDelta factory methods, for fast TimeDelta construction. | 
|  569  |  536  | 
|  570 // static |  537 // static | 
|  571 inline TimeDelta TimeDelta::FromDays(int days) { |  538 inline TimeDelta TimeDelta::FromDays(int days) { | 
|  572   // Preserve max to prevent overflow. |  539   // Preserve max to prevent overflow. | 
|  573   if (days == std::numeric_limits<int>::max()) |  540   if (days == std::numeric_limits<int>::max()) | 
|  574     return Max(); |  541     return Max(); | 
|  575   return TimeDelta(days * Time::kMicrosecondsPerDay); |  542   return TimeDelta(days * Time::kMicrosecondsPerDay); | 
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  624 } |  591 } | 
|  625  |  592  | 
|  626 // static |  593 // static | 
|  627 inline TimeDelta TimeDelta::FromMicroseconds(int64 us) { |  594 inline TimeDelta TimeDelta::FromMicroseconds(int64 us) { | 
|  628   // Preserve max to prevent overflow. |  595   // Preserve max to prevent overflow. | 
|  629   if (us == std::numeric_limits<int64>::max()) |  596   if (us == std::numeric_limits<int64>::max()) | 
|  630     return Max(); |  597     return Max(); | 
|  631   return TimeDelta(us); |  598   return TimeDelta(us); | 
|  632 } |  599 } | 
|  633  |  600  | 
 |  601 inline Time TimeDelta::operator+(Time t) const { | 
 |  602   return Time(SaturatedAdd(t.us_)); | 
 |  603 } | 
 |  604  | 
|  634 // For logging use only. |  605 // For logging use only. | 
|  635 BASE_EXPORT std::ostream& operator<<(std::ostream& os, Time time); |  606 BASE_EXPORT std::ostream& operator<<(std::ostream& os, Time time); | 
|  636  |  607  | 
|  637 // TimeTicks ------------------------------------------------------------------ |  608 // TimeTicks ------------------------------------------------------------------ | 
|  638  |  609  | 
|  639 // Represents monotonically non-decreasing clock time. |  610 class BASE_EXPORT TimeTicks { | 
|  640 class BASE_EXPORT TimeTicks : public time_internal::TimeBase<TimeTicks> { |  | 
|  641  public: |  611  public: | 
|  642   // We define this even without OS_CHROMEOS for seccomp sandbox testing. |  612   // We define this even without OS_CHROMEOS for seccomp sandbox testing. | 
|  643 #if defined(OS_LINUX) |  613 #if defined(OS_LINUX) | 
|  644   // Force definition of the system trace clock; it is a chromeos-only api |  614   // Force definition of the system trace clock; it is a chromeos-only api | 
|  645   // at the moment and surfacing it in the right place requires mucking |  615   // at the moment and surfacing it in the right place requires mucking | 
|  646   // with glibc et al. |  616   // with glibc et al. | 
|  647   static const clockid_t kClockSystemTrace = 11; |  617   static const clockid_t kClockSystemTrace = 11; | 
|  648 #endif |  618 #endif | 
|  649  |  619  | 
|  650   TimeTicks() : TimeBase(0) { |  620   TimeTicks() : ticks_(0) { | 
|  651   } |  621   } | 
|  652  |  622  | 
|  653   // Platform-dependent tick count representing "right now." When |  623   // Platform-dependent tick count representing "right now." When | 
|  654   // IsHighResolution() returns false, the resolution of the clock could be |  624   // IsHighResolution() returns false, the resolution of the clock could be | 
|  655   // as coarse as ~15.6ms. Otherwise, the resolution should be no worse than one |  625   // as coarse as ~15.6ms. Otherwise, the resolution should be no worse than one | 
|  656   // microsecond. |  626   // microsecond. | 
|  657   static TimeTicks Now(); |  627   static TimeTicks Now(); | 
|  658  |  628  | 
|  659   // Returns true if the high resolution clock is working on this system and |  629   // Returns true if the high resolution clock is working on this system and | 
|  660   // Now() will return high resolution values. Note that, on systems where the |  630   // Now() will return high resolution values. Note that, on systems where the | 
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  699   // should be of a different type. |  669   // should be of a different type. | 
|  700   static TimeTicks NowFromSystemTraceTime(); |  670   static TimeTicks NowFromSystemTraceTime(); | 
|  701  |  671  | 
|  702 #if defined(OS_WIN) |  672 #if defined(OS_WIN) | 
|  703   // Translates an absolute QPC timestamp into a TimeTicks value. The returned |  673   // Translates an absolute QPC timestamp into a TimeTicks value. The returned | 
|  704   // value has the same origin as Now(). Do NOT attempt to use this if |  674   // value has the same origin as Now(). Do NOT attempt to use this if | 
|  705   // IsHighResolution() returns false. |  675   // IsHighResolution() returns false. | 
|  706   static TimeTicks FromQPCValue(LONGLONG qpc_value); |  676   static TimeTicks FromQPCValue(LONGLONG qpc_value); | 
|  707 #endif |  677 #endif | 
|  708  |  678  | 
 |  679   // Returns true if this object has not been initialized. | 
 |  680   bool is_null() const { | 
 |  681     return ticks_ == 0; | 
 |  682   } | 
 |  683  | 
 |  684   // Returns true if the time delta is the maximum delta. | 
 |  685   bool is_max() const { | 
 |  686     return ticks_ == std::numeric_limits<int64>::max(); | 
 |  687   } | 
 |  688  | 
 |  689   // Converts an integer value representing TimeTicks to a class. This is used | 
 |  690   // when deserializing a |TimeTicks| structure, using a value known to be | 
 |  691   // compatible. It is not provided as a constructor because the integer type | 
 |  692   // may be unclear from the perspective of a caller. | 
 |  693   static TimeTicks FromInternalValue(int64 ticks) { | 
 |  694     return TimeTicks(ticks); | 
 |  695   } | 
 |  696  | 
|  709   // Get the TimeTick value at the time of the UnixEpoch. This is useful when |  697   // Get the TimeTick value at the time of the UnixEpoch. This is useful when | 
|  710   // you need to relate the value of TimeTicks to a real time and date. |  698   // you need to relate the value of TimeTicks to a real time and date. | 
|  711   // Note: Upon first invocation, this function takes a snapshot of the realtime |  699   // Note: Upon first invocation, this function takes a snapshot of the realtime | 
|  712   // clock to establish a reference point.  This function will return the same |  700   // clock to establish a reference point.  This function will return the same | 
|  713   // value for the duration of the application, but will be different in future |  701   // value for the duration of the application, but will be different in future | 
|  714   // application runs. |  702   // application runs. | 
|  715   static TimeTicks UnixEpoch(); |  703   static TimeTicks UnixEpoch(); | 
|  716  |  704  | 
 |  705   // Returns the internal numeric value of the TimeTicks object. | 
 |  706   // For serializing, use FromInternalValue to reconstitute. | 
 |  707   int64 ToInternalValue() const { | 
 |  708     return ticks_; | 
 |  709   } | 
 |  710  | 
|  717   // Returns |this| snapped to the next tick, given a |tick_phase| and |  711   // Returns |this| snapped to the next tick, given a |tick_phase| and | 
|  718   // repeating |tick_interval| in both directions. |this| may be before, |  712   // repeating |tick_interval| in both directions. |this| may be before, | 
|  719   // after, or equal to the |tick_phase|. |  713   // after, or equal to the |tick_phase|. | 
|  720   TimeTicks SnappedToNextTick(TimeTicks tick_phase, |  714   TimeTicks SnappedToNextTick(TimeTicks tick_phase, | 
|  721                               TimeDelta tick_interval) const; |  715                               TimeDelta tick_interval) const; | 
|  722  |  716  | 
 |  717   TimeTicks& operator=(TimeTicks other) { | 
 |  718     ticks_ = other.ticks_; | 
 |  719     return *this; | 
 |  720   } | 
 |  721  | 
 |  722   // Compute the difference between two times. | 
 |  723   TimeDelta operator-(TimeTicks other) const { | 
 |  724     return TimeDelta(ticks_ - other.ticks_); | 
 |  725   } | 
 |  726  | 
 |  727   // Modify by some time delta. | 
 |  728   TimeTicks& operator+=(TimeDelta delta) { | 
 |  729     ticks_ = delta.SaturatedAdd(ticks_); | 
 |  730     return *this; | 
 |  731   } | 
 |  732   TimeTicks& operator-=(TimeDelta delta) { | 
 |  733     ticks_ = -delta.SaturatedSub(ticks_); | 
 |  734     return *this; | 
 |  735   } | 
 |  736  | 
 |  737   // Return a new TimeTicks modified by some delta. | 
 |  738   TimeTicks operator+(TimeDelta delta) const { | 
 |  739     return TimeTicks(delta.SaturatedAdd(ticks_)); | 
 |  740   } | 
 |  741   TimeTicks operator-(TimeDelta delta) const { | 
 |  742     return TimeTicks(-delta.SaturatedSub(ticks_)); | 
 |  743   } | 
 |  744  | 
 |  745   // Comparison operators | 
 |  746   bool operator==(TimeTicks other) const { | 
 |  747     return ticks_ == other.ticks_; | 
 |  748   } | 
 |  749   bool operator!=(TimeTicks other) const { | 
 |  750     return ticks_ != other.ticks_; | 
 |  751   } | 
 |  752   bool operator<(TimeTicks other) const { | 
 |  753     return ticks_ < other.ticks_; | 
 |  754   } | 
 |  755   bool operator<=(TimeTicks other) const { | 
 |  756     return ticks_ <= other.ticks_; | 
 |  757   } | 
 |  758   bool operator>(TimeTicks other) const { | 
 |  759     return ticks_ > other.ticks_; | 
 |  760   } | 
 |  761   bool operator>=(TimeTicks other) const { | 
 |  762     return ticks_ >= other.ticks_; | 
 |  763   } | 
 |  764  | 
 |  765  protected: | 
 |  766   friend class TimeDelta; | 
 |  767  | 
 |  768   // Please use Now() to create a new object. This is for internal use | 
 |  769   // and testing. Ticks is in microseconds. | 
 |  770   explicit TimeTicks(int64 ticks) : ticks_(ticks) { | 
 |  771   } | 
 |  772  | 
 |  773   // Tick count in microseconds. | 
 |  774   int64 ticks_; | 
 |  775  | 
|  723 #if defined(OS_WIN) |  776 #if defined(OS_WIN) | 
|  724  protected: |  | 
|  725   typedef DWORD (*TickFunctionType)(void); |  777   typedef DWORD (*TickFunctionType)(void); | 
|  726   static TickFunctionType SetMockTickFunction(TickFunctionType ticker); |  778   static TickFunctionType SetMockTickFunction(TickFunctionType ticker); | 
|  727 #endif |  779 #endif | 
 |  780 }; | 
|  728  |  781  | 
|  729  private: |  782 inline TimeTicks TimeDelta::operator+(TimeTicks t) const { | 
|  730   friend class time_internal::TimeBase<TimeTicks>; |  783   return TimeTicks(SaturatedAdd(t.ticks_)); | 
|  731  |  784 } | 
|  732   // Please use Now() to create a new object. This is for internal use |  | 
|  733   // and testing. |  | 
|  734   explicit TimeTicks(int64 us) : TimeBase(us) { |  | 
|  735   } |  | 
|  736 }; |  | 
|  737  |  785  | 
|  738 // For logging use only. |  786 // For logging use only. | 
|  739 BASE_EXPORT std::ostream& operator<<(std::ostream& os, TimeTicks time_ticks); |  787 BASE_EXPORT std::ostream& operator<<(std::ostream& os, TimeTicks time_ticks); | 
|  740  |  788  | 
|  741 }  // namespace base |  789 }  // namespace base | 
|  742  |  790  | 
|  743 #endif  // BASE_TIME_TIME_H_ |  791 #endif  // BASE_TIME_TIME_H_ | 
| OLD | NEW |