Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(19)

Side by Side Diff: base/time/time.cc

Issue 1130953002: Revert of Fixit: Factor out common base::Time* math operator overloads. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/time/time.h ('k') | base/time/time_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 namespace time_internal { 100 int64 TimeDelta::SaturatedAdd(int64 value) const {
101 101 CheckedNumeric<int64> rv(delta_);
102 int64 SaturatedAdd(TimeDelta delta, int64 value) {
103 CheckedNumeric<int64> rv(delta.delta_);
104 rv += value; 102 rv += value;
105 return FromCheckedNumeric(rv); 103 return FromCheckedNumeric(rv);
106 } 104 }
107 105
108 int64 SaturatedSub(TimeDelta delta, int64 value) { 106 int64 TimeDelta::SaturatedSub(int64 value) const {
109 CheckedNumeric<int64> rv(delta.delta_); 107 CheckedNumeric<int64> rv(delta_);
110 rv -= value; 108 rv -= value;
111 return FromCheckedNumeric(rv); 109 return FromCheckedNumeric(rv);
112 } 110 }
113 111
114 int64 FromCheckedNumeric(const CheckedNumeric<int64> value) { 112 // static
113 int64 TimeDelta::FromCheckedNumeric(const CheckedNumeric<int64> value) {
115 if (value.IsValid()) 114 if (value.IsValid())
116 return value.ValueUnsafe(); 115 return value.ValueUnsafe();
117 116
118 // We could return max/min but we don't really expose what the maximum delta 117 // We could return max/min but we don't really expose what the maximum delta
119 // is. Instead, return max/(-max), which is something that clients can reason 118 // is. Instead, return max/(-max), which is something that clients can reason
120 // about. 119 // about.
121 // TODO(rvargas) crbug.com/332611: don't use internal values. 120 // TODO(rvargas) crbug.com/332611: don't use internal values.
122 int64 limit = std::numeric_limits<int64>::max(); 121 int64 limit = std::numeric_limits<int64>::max();
123 if (value.validity() == internal::RANGE_UNDERFLOW) 122 if (value.validity() == internal::RANGE_UNDERFLOW)
124 limit = -limit; 123 limit = -limit;
125 return value.ValueOrDefault(limit); 124 return value.ValueOrDefault(limit);
126 } 125 }
127 126
128 } // namespace time_internal
129
130 std::ostream& operator<<(std::ostream& os, TimeDelta time_delta) { 127 std::ostream& operator<<(std::ostream& os, TimeDelta time_delta) {
131 return os << time_delta.InSecondsF() << "s"; 128 return os << time_delta.InSecondsF() << "s";
132 } 129 }
133 130
134 // Time ----------------------------------------------------------------------- 131 // Time -----------------------------------------------------------------------
135 132
136 // static 133 // static
137 Time Time::Max() { 134 Time Time::Max() {
138 return Time(std::numeric_limits<int64>::max()); 135 return Time(std::numeric_limits<int64>::max());
139 } 136 }
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after
301 298
302 // Static 299 // Static
303 TimeTicks TimeTicks::UnixEpoch() { 300 TimeTicks TimeTicks::UnixEpoch() {
304 return leaky_unix_epoch_singleton_instance.Get().unix_epoch(); 301 return leaky_unix_epoch_singleton_instance.Get().unix_epoch();
305 } 302 }
306 303
307 TimeTicks TimeTicks::SnappedToNextTick(TimeTicks tick_phase, 304 TimeTicks TimeTicks::SnappedToNextTick(TimeTicks tick_phase,
308 TimeDelta tick_interval) const { 305 TimeDelta tick_interval) const {
309 // |interval_offset| is the offset from |this| to the next multiple of 306 // |interval_offset| is the offset from |this| to the next multiple of
310 // |tick_interval| after |tick_phase|, possibly negative if in the past. 307 // |tick_interval| after |tick_phase|, possibly negative if in the past.
311 TimeDelta interval_offset = (tick_phase - *this) % tick_interval; 308 TimeDelta interval_offset = TimeDelta::FromInternalValue(
309 (tick_phase - *this).ToInternalValue() % tick_interval.ToInternalValue());
312 // If |this| is exactly on the interval (i.e. offset==0), don't adjust. 310 // If |this| is exactly on the interval (i.e. offset==0), don't adjust.
313 // Otherwise, if |tick_phase| was in the past, adjust forward to the next 311 // Otherwise, if |tick_phase| was in the past, adjust forward to the next
314 // tick after |this|. 312 // tick after |this|.
315 if (!interval_offset.is_zero() && tick_phase < *this) 313 if (interval_offset.ToInternalValue() != 0 && tick_phase < *this) {
316 interval_offset += tick_interval; 314 interval_offset += tick_interval;
315 }
316
317 return *this + interval_offset; 317 return *this + interval_offset;
318 } 318 }
319 319
320 std::ostream& operator<<(std::ostream& os, TimeTicks time_ticks) { 320 std::ostream& operator<<(std::ostream& os, TimeTicks time_ticks) {
321 // This function formats a TimeTicks object as "bogo-microseconds". 321 // This function formats a TimeTicks object as "bogo-microseconds".
322 // The origin and granularity of the count are platform-specific, and may very 322 // The origin and granularity of the count are platform-specific, and may very
323 // from run to run. Although bogo-microseconds usually roughly correspond to 323 // from run to run. Although bogo-microseconds usually roughly correspond to
324 // real microseconds, the only real guarantee is that the number never goes 324 // real microseconds, the only real guarantee is that the number never goes
325 // down during a single run. 325 // down during a single run.
326 const TimeDelta as_time_delta = time_ticks - TimeTicks(); 326 const TimeDelta as_time_delta = time_ticks - TimeTicks();
(...skipping 10 matching lines...) Expand all
337 return is_in_range(month, 1, 12) && 337 return is_in_range(month, 1, 12) &&
338 is_in_range(day_of_week, 0, 6) && 338 is_in_range(day_of_week, 0, 6) &&
339 is_in_range(day_of_month, 1, 31) && 339 is_in_range(day_of_month, 1, 31) &&
340 is_in_range(hour, 0, 23) && 340 is_in_range(hour, 0, 23) &&
341 is_in_range(minute, 0, 59) && 341 is_in_range(minute, 0, 59) &&
342 is_in_range(second, 0, 60) && 342 is_in_range(second, 0, 60) &&
343 is_in_range(millisecond, 0, 999); 343 is_in_range(millisecond, 0, 999);
344 } 344 }
345 345
346 } // namespace base 346 } // namespace base
OLDNEW
« no previous file with comments | « base/time/time.h ('k') | base/time/time_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698