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 #include "base/time/time.h" | 5 #include "base/time/time.h" |
6 | 6 |
7 #include <cmath> | 7 #include <cmath> |
8 #include <ios> | 8 #include <ios> |
9 #include <limits> | 9 #include <limits> |
10 #include <ostream> | 10 #include <ostream> |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
97 return std::numeric_limits<int64_t>::max(); | 97 return std::numeric_limits<int64_t>::max(); |
98 } | 98 } |
99 return delta_; | 99 return delta_; |
100 } | 100 } |
101 | 101 |
102 namespace time_internal { | 102 namespace time_internal { |
103 | 103 |
104 int64_t SaturatedAdd(TimeDelta delta, int64_t value) { | 104 int64_t SaturatedAdd(TimeDelta delta, int64_t value) { |
105 CheckedNumeric<int64_t> rv(delta.delta_); | 105 CheckedNumeric<int64_t> rv(delta.delta_); |
106 rv += value; | 106 rv += value; |
107 return FromCheckedNumeric(rv); | 107 if (rv.IsValid()) |
| 108 return rv.ValueOrDie(); |
| 109 // Positive RHS overflows. Negative RHS underflows. |
| 110 if (value < 0) |
| 111 return -std::numeric_limits<int64_t>::max(); |
| 112 return std::numeric_limits<int64_t>::max(); |
108 } | 113 } |
109 | 114 |
110 int64_t SaturatedSub(TimeDelta delta, int64_t value) { | 115 int64_t SaturatedSub(TimeDelta delta, int64_t value) { |
111 CheckedNumeric<int64_t> rv(delta.delta_); | 116 CheckedNumeric<int64_t> rv(delta.delta_); |
112 rv -= value; | 117 rv -= value; |
113 return FromCheckedNumeric(rv); | 118 if (rv.IsValid()) |
114 } | 119 return rv.ValueOrDie(); |
115 | 120 // Negative RHS overflows. Positive RHS underflows. |
116 int64_t FromCheckedNumeric(const CheckedNumeric<int64_t> value) { | 121 if (value < 0) |
117 if (value.IsValid()) | 122 return std::numeric_limits<int64_t>::max(); |
118 return value.ValueUnsafe(); | 123 return -std::numeric_limits<int64_t>::max(); |
119 | |
120 // We could return max/min but we don't really expose what the maximum delta | |
121 // is. Instead, return max/(-max), which is something that clients can reason | |
122 // about. | |
123 // TODO(rvargas) crbug.com/332611: don't use internal values. | |
124 int64_t limit = std::numeric_limits<int64_t>::max(); | |
125 if (value.validity() == internal::RANGE_UNDERFLOW) | |
126 limit = -limit; | |
127 return value.ValueOrDefault(limit); | |
128 } | 124 } |
129 | 125 |
130 } // namespace time_internal | 126 } // namespace time_internal |
131 | 127 |
132 std::ostream& operator<<(std::ostream& os, TimeDelta time_delta) { | 128 std::ostream& operator<<(std::ostream& os, TimeDelta time_delta) { |
133 return os << time_delta.InSecondsF() << "s"; | 129 return os << time_delta.InSecondsF() << "s"; |
134 } | 130 } |
135 | 131 |
136 // Time ----------------------------------------------------------------------- | 132 // Time ----------------------------------------------------------------------- |
137 | 133 |
(...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
341 return is_in_range(month, 1, 12) && | 337 return is_in_range(month, 1, 12) && |
342 is_in_range(day_of_week, 0, 6) && | 338 is_in_range(day_of_week, 0, 6) && |
343 is_in_range(day_of_month, 1, 31) && | 339 is_in_range(day_of_month, 1, 31) && |
344 is_in_range(hour, 0, 23) && | 340 is_in_range(hour, 0, 23) && |
345 is_in_range(minute, 0, 59) && | 341 is_in_range(minute, 0, 59) && |
346 is_in_range(second, 0, 60) && | 342 is_in_range(second, 0, 60) && |
347 is_in_range(millisecond, 0, 999); | 343 is_in_range(millisecond, 0, 999); |
348 } | 344 } |
349 | 345 |
350 } // namespace base | 346 } // namespace base |
OLD | NEW |