| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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 #ifndef WTF_Time_h | 5 #include "platform/wtf/Time.h" |
| 6 #define WTF_Time_h | |
| 7 | 6 |
| 8 #include "base/time/time.h" | 7 // The contents of this header was moved to platform/wtf as part of |
| 9 #include "wtf/CurrentTime.h" | 8 // WTF migration project. See the following post for details: |
| 10 | 9 // https://groups.google.com/a/chromium.org/d/msg/blink-dev/tLdAZCTlcAA/bYXVT8gY
CAAJ |
| 11 namespace WTF { | |
| 12 // Provides thin wrappers around the following basic time types from | |
| 13 // base/time package: | |
| 14 // | |
| 15 // - WTF::TimeDelta is an alias for base::TimeDelta and represents a duration | |
| 16 // of time. | |
| 17 // - WTF::TimeTicks wraps base::TimeTicks and represents a monotonic time | |
| 18 // value. | |
| 19 // - WTF::Time wraps base::Time and represents a wall time value. | |
| 20 // | |
| 21 // For usage guideline please see the documentation in base/time/time.h | |
| 22 | |
| 23 using TimeDelta = base::TimeDelta; | |
| 24 | |
| 25 namespace internal { | |
| 26 | |
| 27 template <class WrappedTimeType> | |
| 28 class TimeWrapper { | |
| 29 public: | |
| 30 TimeWrapper() {} | |
| 31 | |
| 32 static TimeWrapper Now() { | |
| 33 if (WTF::getTimeFunctionForTesting()) { | |
| 34 double seconds = (WTF::getTimeFunctionForTesting())(); | |
| 35 return TimeWrapper() + TimeDelta::FromSecondsD(seconds); | |
| 36 } | |
| 37 return TimeWrapper(WrappedTimeType::Now()); | |
| 38 } | |
| 39 | |
| 40 int64_t ToInternalValueForTesting() const { | |
| 41 return m_value.ToInternalValue(); | |
| 42 } | |
| 43 | |
| 44 // Only use this conversion when interfacing with legacy code that represents | |
| 45 // time in double. Converting to double can lead to losing information for | |
| 46 // large time values. | |
| 47 double InSeconds() const { | |
| 48 return (m_value - WrappedTimeType()).InSecondsF(); | |
| 49 } | |
| 50 | |
| 51 static TimeWrapper FromSeconds(double seconds) { | |
| 52 return WrappedTimeType() + TimeDelta::FromSecondsD(seconds); | |
| 53 } | |
| 54 | |
| 55 TimeWrapper& operator=(TimeWrapper other) { | |
| 56 m_value = other.m_value; | |
| 57 return *this; | |
| 58 } | |
| 59 | |
| 60 TimeDelta operator-(TimeWrapper other) const { | |
| 61 return m_value - other.m_value; | |
| 62 } | |
| 63 | |
| 64 TimeWrapper operator+(TimeDelta delta) const { | |
| 65 return TimeWrapper(m_value + delta); | |
| 66 } | |
| 67 TimeWrapper operator-(TimeDelta delta) const { | |
| 68 return TimeWrapper(m_value - delta); | |
| 69 } | |
| 70 | |
| 71 TimeWrapper& operator+=(TimeDelta delta) { | |
| 72 m_value += delta; | |
| 73 return *this; | |
| 74 } | |
| 75 TimeWrapper& operator-=(TimeDelta delta) { | |
| 76 m_value -= delta; | |
| 77 return *this; | |
| 78 } | |
| 79 | |
| 80 bool operator==(TimeWrapper other) const { return m_value == other.m_value; } | |
| 81 bool operator!=(TimeWrapper other) const { return m_value != other.m_value; } | |
| 82 bool operator<(TimeWrapper other) const { return m_value < other.m_value; } | |
| 83 bool operator<=(TimeWrapper other) const { return m_value <= other.m_value; } | |
| 84 bool operator>(TimeWrapper other) const { return m_value > other.m_value; } | |
| 85 bool operator>=(TimeWrapper other) const { return m_value >= other.m_value; } | |
| 86 | |
| 87 private: | |
| 88 WrappedTimeType m_value; | |
| 89 TimeWrapper(WrappedTimeType value) : m_value(value) {} | |
| 90 }; | |
| 91 | |
| 92 } // namespace internal | |
| 93 | |
| 94 using Time = internal::TimeWrapper<base::Time>; | |
| 95 using TimeTicks = internal::TimeWrapper<base::TimeTicks>; | |
| 96 | |
| 97 } // namespace WTF | |
| 98 | |
| 99 using WTF::Time; | |
| 100 using WTF::TimeDelta; | |
| 101 using WTF::TimeTicks; | |
| 102 | |
| 103 #endif // Time_h | |
| OLD | NEW |