OLD | NEW |
---|---|
(Empty) | |
1 // 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.
| |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef CHROMECAST_PUBLIC_TIME_DELTA_H_ | |
6 #define CHROMECAST_PUBLIC_TIME_DELTA_H_ | |
7 | |
8 #include <stdint.h> | |
9 | |
10 #include <limits> | |
11 | |
12 namespace chromecast { | |
13 | |
14 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.
| |
15 public: | |
16 TimeDelta() : delta_(0) {} | |
17 | |
18 // Indicates an invalid or missing timestamp | |
19 static TimeDelta Invalid() { | |
20 return TimeDelta(std::numeric_limits<int64_t>::min()); | |
21 } | |
22 // Maximum possible TimeDelta value | |
23 static TimeDelta Max() { | |
24 return TimeDelta(std::numeric_limits<int64_t>::max()); | |
25 } | |
26 | |
27 // Convert units of time to DeltaDelta | |
28 static TimeDelta FromMilliseconds(int64_t ms) { | |
29 if (ms == std::numeric_limits<int64_t>::max()) | |
30 return Max(); | |
31 return TimeDelta(ms * 1000); | |
32 } | |
33 static TimeDelta FromMicroseconds(int64_t us) { return TimeDelta(us); } | |
34 | |
35 // Convert to units of time | |
36 int64_t InMicroseconds() const { return delta_; } | |
37 int64_t InMilliseconds() const { | |
38 if (delta_ == std::numeric_limits<int64_t>::max()) | |
39 return std::numeric_limits<int64_t>::max(); | |
40 return delta_ / 1000; | |
41 } | |
42 double InSecondsF() const { | |
43 if (delta_ == std::numeric_limits<int64_t>::max()) | |
44 return std::numeric_limits<double>::infinity(); | |
45 const int64_t kMicrosecondsPerSecond = 1000000; | |
46 return static_cast<double>(delta_) / kMicrosecondsPerSecond; | |
47 } | |
48 | |
49 // Arithmetic operations. These prevent overflow and clamp to (+max, -max) | |
50 // when overflow would have occurred. | |
51 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'
| |
52 const int64_t max_int = std::numeric_limits<int64_t>::max(); | |
53 const int64_t min_int = std::numeric_limits<int64_t>::min(); | |
54 if (((other.delta_ > 0) && (delta_ > (max_int - other.delta_)))) | |
55 return TimeDelta(max_int); | |
56 else if ((other.delta_ < 0) && (delta_ < (min_int - other.delta_))) | |
57 return TimeDelta(-max_int); | |
58 else | |
59 return TimeDelta(delta_ + other.delta_); | |
60 } | |
61 | |
62 TimeDelta operator-(TimeDelta other) const { | |
63 const int64_t max_int = std::numeric_limits<int64_t>::max(); | |
64 const int64_t min_int = std::numeric_limits<int64_t>::min(); | |
65 | |
66 if ((other.delta_ > 0) && (delta_ < (min_int + other.delta_))) | |
67 return TimeDelta(-max_int); | |
68 else if ((other.delta_ < 0) && (delta_ > (max_int + other.delta_))) | |
69 return TimeDelta(max_int); | |
70 else | |
71 return TimeDelta(delta_ - other.delta_); | |
72 } | |
73 | |
74 // Comparisons | |
75 bool operator==(const TimeDelta& other) const { | |
76 return delta_ == other.delta_; | |
77 } | |
78 bool operator!=(const TimeDelta& other) const { | |
79 return delta_ != other.delta_; | |
80 } | |
81 bool operator<=(const TimeDelta& other) const { | |
82 return delta_ <= other.delta_; | |
83 } | |
84 bool operator<(const TimeDelta& other) const { return delta_ < other.delta_; } | |
85 bool operator>=(const TimeDelta& other) const { | |
86 return delta_ >= other.delta_; | |
87 } | |
88 bool operator>(const TimeDelta& other) const { return delta_ > other.delta_; } | |
89 | |
90 private: | |
91 explicit TimeDelta(int64_t delta) : delta_(delta) {} | |
92 | |
93 int64_t delta_; | |
94 }; | |
95 | |
96 } // namespace chromecast | |
97 | |
98 #endif // CHROMECAST_PUBLIC_TIME_DELTA_H_ | |
OLD | NEW |