| 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 <ios> | 7 #include <ios> |
| 8 #include <limits> | 8 #include <limits> |
| 9 #include <ostream> | 9 #include <ostream> |
| 10 #include <sstream> | 10 #include <sstream> |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 90 } | 90 } |
| 91 | 91 |
| 92 int64 TimeDelta::InMicroseconds() const { | 92 int64 TimeDelta::InMicroseconds() const { |
| 93 if (is_max()) { | 93 if (is_max()) { |
| 94 // Preserve max to prevent overflow. | 94 // Preserve max to prevent overflow. |
| 95 return std::numeric_limits<int64>::max(); | 95 return std::numeric_limits<int64>::max(); |
| 96 } | 96 } |
| 97 return delta_; | 97 return delta_; |
| 98 } | 98 } |
| 99 | 99 |
| 100 int64 TimeDelta::SaturatedAdd(int64 value) const { |
| 101 CheckedNumeric<int64> rv(delta_); |
| 102 rv += value; |
| 103 return FromCheckedNumeric(rv); |
| 104 } |
| 105 |
| 106 int64 TimeDelta::SaturatedSub(int64 value) const { |
| 107 CheckedNumeric<int64> rv(delta_); |
| 108 rv -= value; |
| 109 return FromCheckedNumeric(rv); |
| 110 } |
| 111 |
| 112 // static |
| 113 int64 TimeDelta::FromCheckedNumeric(const CheckedNumeric<int64> value) { |
| 114 if (value.IsValid()) |
| 115 return value.ValueUnsafe(); |
| 116 |
| 117 // We could return max/min but we don't really expose what the maximum delta |
| 118 // is. Instead, return max/(-max), which is something that clients can reason |
| 119 // about. |
| 120 // TODO(rvargas) crbug.com/332611: don't use internal values. |
| 121 int64 limit = std::numeric_limits<int64>::max(); |
| 122 if (value.validity() == internal::RANGE_UNDERFLOW) |
| 123 limit = -limit; |
| 124 return value.ValueOrDefault(limit); |
| 125 } |
| 126 |
| 100 std::ostream& operator<<(std::ostream& os, TimeDelta time_delta) { | 127 std::ostream& operator<<(std::ostream& os, TimeDelta time_delta) { |
| 101 return os << time_delta.InSecondsF() << "s"; | 128 return os << time_delta.InSecondsF() << "s"; |
| 102 } | 129 } |
| 103 | 130 |
| 104 // Time ----------------------------------------------------------------------- | 131 // Time ----------------------------------------------------------------------- |
| 105 | 132 |
| 106 // static | 133 // static |
| 107 Time Time::Max() { | 134 Time Time::Max() { |
| 108 return Time(std::numeric_limits<int64>::max()); | 135 return Time(std::numeric_limits<int64>::max()); |
| 109 } | 136 } |
| (...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 310 return is_in_range(month, 1, 12) && | 337 return is_in_range(month, 1, 12) && |
| 311 is_in_range(day_of_week, 0, 6) && | 338 is_in_range(day_of_week, 0, 6) && |
| 312 is_in_range(day_of_month, 1, 31) && | 339 is_in_range(day_of_month, 1, 31) && |
| 313 is_in_range(hour, 0, 23) && | 340 is_in_range(hour, 0, 23) && |
| 314 is_in_range(minute, 0, 59) && | 341 is_in_range(minute, 0, 59) && |
| 315 is_in_range(second, 0, 60) && | 342 is_in_range(second, 0, 60) && |
| 316 is_in_range(millisecond, 0, 999); | 343 is_in_range(millisecond, 0, 999); |
| 317 } | 344 } |
| 318 | 345 |
| 319 } // namespace base | 346 } // namespace base |
| OLD | NEW |