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

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

Issue 1122443004: Fixit: Factor out common base::Time* math operator overloads. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Added/Fixed comments. Added is_zero(). 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
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 int64 TimeDelta::SaturatedAdd(int64 value) const { 100 namespace time_internal {
101 CheckedNumeric<int64> rv(delta_); 101
102 int64 SaturatedAdd(TimeDelta delta, int64 value) {
103 CheckedNumeric<int64> rv(delta.delta_);
102 rv += value; 104 rv += value;
103 return FromCheckedNumeric(rv); 105 return FromCheckedNumeric(rv);
104 } 106 }
105 107
106 int64 TimeDelta::SaturatedSub(int64 value) const { 108 int64 SaturatedSub(TimeDelta delta, int64 value) {
107 CheckedNumeric<int64> rv(delta_); 109 CheckedNumeric<int64> rv(delta.delta_);
108 rv -= value; 110 rv -= value;
109 return FromCheckedNumeric(rv); 111 return FromCheckedNumeric(rv);
110 } 112 }
111 113
112 // static 114 int64 FromCheckedNumeric(const CheckedNumeric<int64> value) {
113 int64 TimeDelta::FromCheckedNumeric(const CheckedNumeric<int64> value) {
114 if (value.IsValid()) 115 if (value.IsValid())
115 return value.ValueUnsafe(); 116 return value.ValueUnsafe();
116 117
117 // We could return max/min but we don't really expose what the maximum delta 118 // 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 // is. Instead, return max/(-max), which is something that clients can reason
119 // about. 120 // about.
120 // TODO(rvargas) crbug.com/332611: don't use internal values. 121 // TODO(rvargas) crbug.com/332611: don't use internal values.
121 int64 limit = std::numeric_limits<int64>::max(); 122 int64 limit = std::numeric_limits<int64>::max();
122 if (value.validity() == internal::RANGE_UNDERFLOW) 123 if (value.validity() == internal::RANGE_UNDERFLOW)
123 limit = -limit; 124 limit = -limit;
124 return value.ValueOrDefault(limit); 125 return value.ValueOrDefault(limit);
125 } 126 }
126 127
128 } // namespace time_internal
129
127 std::ostream& operator<<(std::ostream& os, TimeDelta time_delta) { 130 std::ostream& operator<<(std::ostream& os, TimeDelta time_delta) {
128 return os << time_delta.InSecondsF() << "s"; 131 return os << time_delta.InSecondsF() << "s";
129 } 132 }
130 133
131 // Time ----------------------------------------------------------------------- 134 // Time -----------------------------------------------------------------------
132 135
133 // static 136 // static
134 Time Time::Max() { 137 Time Time::Max() {
135 return Time(std::numeric_limits<int64>::max()); 138 return Time(std::numeric_limits<int64>::max());
136 } 139 }
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after
298 301
299 // Static 302 // Static
300 TimeTicks TimeTicks::UnixEpoch() { 303 TimeTicks TimeTicks::UnixEpoch() {
301 return leaky_unix_epoch_singleton_instance.Get().unix_epoch(); 304 return leaky_unix_epoch_singleton_instance.Get().unix_epoch();
302 } 305 }
303 306
304 TimeTicks TimeTicks::SnappedToNextTick(TimeTicks tick_phase, 307 TimeTicks TimeTicks::SnappedToNextTick(TimeTicks tick_phase,
305 TimeDelta tick_interval) const { 308 TimeDelta tick_interval) const {
306 // |interval_offset| is the offset from |this| to the next multiple of 309 // |interval_offset| is the offset from |this| to the next multiple of
307 // |tick_interval| after |tick_phase|, possibly negative if in the past. 310 // |tick_interval| after |tick_phase|, possibly negative if in the past.
308 TimeDelta interval_offset = TimeDelta::FromInternalValue( 311 TimeDelta interval_offset = (tick_phase - *this) % tick_interval;
309 (tick_phase - *this).ToInternalValue() % tick_interval.ToInternalValue());
310 // If |this| is exactly on the interval (i.e. offset==0), don't adjust. 312 // If |this| is exactly on the interval (i.e. offset==0), don't adjust.
311 // Otherwise, if |tick_phase| was in the past, adjust forward to the next 313 // Otherwise, if |tick_phase| was in the past, adjust forward to the next
312 // tick after |this|. 314 // tick after |this|.
313 if (interval_offset.ToInternalValue() != 0 && tick_phase < *this) { 315 if (!interval_offset.is_zero() && tick_phase < *this)
314 interval_offset += tick_interval; 316 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
« base/time/time.h ('K') | « 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