Index: chromecast/public/time_delta.h |
diff --git a/chromecast/public/time_delta.h b/chromecast/public/time_delta.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..56f5a579db5bacd7b0a149c9331894d9c7cb25d6 |
--- /dev/null |
+++ b/chromecast/public/time_delta.h |
@@ -0,0 +1,98 @@ |
+// Copyright (c) 2015 The Chromium Authors. All rights reserved. |
gunsch
2015/07/27 17:14:49
no (c)
halliwell
2015/07/28 02:19:36
file removed.
|
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#ifndef CHROMECAST_PUBLIC_TIME_DELTA_H_ |
+#define CHROMECAST_PUBLIC_TIME_DELTA_H_ |
+ |
+#include <stdint.h> |
+ |
+#include <limits> |
+ |
+namespace chromecast { |
+ |
+class TimeDelta { |
gunsch
2015/07/27 17:14:50
Could we just use |uint64_t microseconds| everywhe
byungchul
2015/07/27 18:22:23
+1
halliwell
2015/07/28 02:19:36
Done.
|
+ public: |
+ TimeDelta() : delta_(0) {} |
+ |
+ // Indicates an invalid or missing timestamp |
+ static TimeDelta Invalid() { |
+ return TimeDelta(std::numeric_limits<int64_t>::min()); |
+ } |
+ // Maximum possible TimeDelta value |
+ static TimeDelta Max() { |
+ return TimeDelta(std::numeric_limits<int64_t>::max()); |
+ } |
+ |
+ // Convert units of time to DeltaDelta |
+ static TimeDelta FromMilliseconds(int64_t ms) { |
+ if (ms == std::numeric_limits<int64_t>::max()) |
+ return Max(); |
+ return TimeDelta(ms * 1000); |
+ } |
+ static TimeDelta FromMicroseconds(int64_t us) { return TimeDelta(us); } |
+ |
+ // Convert to units of time |
+ int64_t InMicroseconds() const { return delta_; } |
+ int64_t InMilliseconds() const { |
+ if (delta_ == std::numeric_limits<int64_t>::max()) |
+ return std::numeric_limits<int64_t>::max(); |
+ return delta_ / 1000; |
+ } |
+ double InSecondsF() const { |
+ if (delta_ == std::numeric_limits<int64_t>::max()) |
+ return std::numeric_limits<double>::infinity(); |
+ const int64_t kMicrosecondsPerSecond = 1000000; |
+ return static_cast<double>(delta_) / kMicrosecondsPerSecond; |
+ } |
+ |
+ // Arithmetic operations. These prevent overflow and clamp to (+max, -max) |
+ // when overflow would have occurred. |
+ TimeDelta operator+(TimeDelta other) const { |
gunsch
2015/07/27 17:14:49
Do we need/use the addition/subtraction operators?
halliwell
2015/07/28 02:19:36
we did use them (and no others, that's why I didn'
|
+ const int64_t max_int = std::numeric_limits<int64_t>::max(); |
+ const int64_t min_int = std::numeric_limits<int64_t>::min(); |
+ if (((other.delta_ > 0) && (delta_ > (max_int - other.delta_)))) |
+ return TimeDelta(max_int); |
+ else if ((other.delta_ < 0) && (delta_ < (min_int - other.delta_))) |
+ return TimeDelta(-max_int); |
+ else |
+ return TimeDelta(delta_ + other.delta_); |
+ } |
+ |
+ TimeDelta operator-(TimeDelta other) const { |
+ const int64_t max_int = std::numeric_limits<int64_t>::max(); |
+ const int64_t min_int = std::numeric_limits<int64_t>::min(); |
+ |
+ if ((other.delta_ > 0) && (delta_ < (min_int + other.delta_))) |
+ return TimeDelta(-max_int); |
+ else if ((other.delta_ < 0) && (delta_ > (max_int + other.delta_))) |
+ return TimeDelta(max_int); |
+ else |
+ return TimeDelta(delta_ - other.delta_); |
+ } |
+ |
+ // Comparisons |
+ bool operator==(const TimeDelta& other) const { |
+ return delta_ == other.delta_; |
+ } |
+ bool operator!=(const TimeDelta& other) const { |
+ return delta_ != other.delta_; |
+ } |
+ bool operator<=(const TimeDelta& other) const { |
+ return delta_ <= other.delta_; |
+ } |
+ bool operator<(const TimeDelta& other) const { return delta_ < other.delta_; } |
+ bool operator>=(const TimeDelta& other) const { |
+ return delta_ >= other.delta_; |
+ } |
+ bool operator>(const TimeDelta& other) const { return delta_ > other.delta_; } |
+ |
+ private: |
+ explicit TimeDelta(int64_t delta) : delta_(delta) {} |
+ |
+ int64_t delta_; |
+}; |
+ |
+} // namespace chromecast |
+ |
+#endif // CHROMECAST_PUBLIC_TIME_DELTA_H_ |