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

Unified 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 side-by-side diff with in-line comments
Download patch
Index: base/time/time.cc
diff --git a/base/time/time.cc b/base/time/time.cc
index bf6c998ebf8cd2dae00b08dfa08e16f011f7503f..9834188597d479038b7bca055d2584feea091913 100644
--- a/base/time/time.cc
+++ b/base/time/time.cc
@@ -97,20 +97,21 @@ int64 TimeDelta::InMicroseconds() const {
return delta_;
}
-int64 TimeDelta::SaturatedAdd(int64 value) const {
- CheckedNumeric<int64> rv(delta_);
+namespace time_internal {
+
+int64 SaturatedAdd(TimeDelta delta, int64 value) {
+ CheckedNumeric<int64> rv(delta.delta_);
rv += value;
return FromCheckedNumeric(rv);
}
-int64 TimeDelta::SaturatedSub(int64 value) const {
- CheckedNumeric<int64> rv(delta_);
+int64 SaturatedSub(TimeDelta delta, int64 value) {
+ CheckedNumeric<int64> rv(delta.delta_);
rv -= value;
return FromCheckedNumeric(rv);
}
-// static
-int64 TimeDelta::FromCheckedNumeric(const CheckedNumeric<int64> value) {
+int64 FromCheckedNumeric(const CheckedNumeric<int64> value) {
if (value.IsValid())
return value.ValueUnsafe();
@@ -124,6 +125,8 @@ int64 TimeDelta::FromCheckedNumeric(const CheckedNumeric<int64> value) {
return value.ValueOrDefault(limit);
}
+} // namespace time_internal
+
std::ostream& operator<<(std::ostream& os, TimeDelta time_delta) {
return os << time_delta.InSecondsF() << "s";
}
@@ -305,15 +308,12 @@ TimeTicks TimeTicks::SnappedToNextTick(TimeTicks tick_phase,
TimeDelta tick_interval) const {
// |interval_offset| is the offset from |this| to the next multiple of
// |tick_interval| after |tick_phase|, possibly negative if in the past.
- TimeDelta interval_offset = TimeDelta::FromInternalValue(
- (tick_phase - *this).ToInternalValue() % tick_interval.ToInternalValue());
+ TimeDelta interval_offset = (tick_phase - *this) % tick_interval;
// If |this| is exactly on the interval (i.e. offset==0), don't adjust.
// Otherwise, if |tick_phase| was in the past, adjust forward to the next
// tick after |this|.
- if (interval_offset.ToInternalValue() != 0 && tick_phase < *this) {
+ if (!interval_offset.is_zero() && tick_phase < *this)
interval_offset += tick_interval;
- }
-
return *this + interval_offset;
}
« 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