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

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

Issue 1250453004: Revert of Base: Make TimeDelta constructors deal with overflow. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 5 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 126 matching lines...) Expand 10 before | Expand all | Expand 10 after
137 Time Time::Max() { 137 Time Time::Max() {
138 return Time(std::numeric_limits<int64>::max()); 138 return Time(std::numeric_limits<int64>::max());
139 } 139 }
140 140
141 // static 141 // static
142 Time Time::FromTimeT(time_t tt) { 142 Time Time::FromTimeT(time_t tt) {
143 if (tt == 0) 143 if (tt == 0)
144 return Time(); // Preserve 0 so we can tell it doesn't exist. 144 return Time(); // Preserve 0 so we can tell it doesn't exist.
145 if (tt == std::numeric_limits<time_t>::max()) 145 if (tt == std::numeric_limits<time_t>::max())
146 return Max(); 146 return Max();
147 return Time(kTimeTToMicrosecondsOffset) + TimeDelta::FromSeconds(tt); 147 return Time((tt * kMicrosecondsPerSecond) + kTimeTToMicrosecondsOffset);
148 } 148 }
149 149
150 time_t Time::ToTimeT() const { 150 time_t Time::ToTimeT() const {
151 if (is_null()) 151 if (is_null())
152 return 0; // Preserve 0 so we can tell it doesn't exist. 152 return 0; // Preserve 0 so we can tell it doesn't exist.
153 if (is_max()) { 153 if (is_max()) {
154 // Preserve max without offset to prevent overflow. 154 // Preserve max without offset to prevent overflow.
155 return std::numeric_limits<time_t>::max(); 155 return std::numeric_limits<time_t>::max();
156 } 156 }
157 if (std::numeric_limits<int64>::max() - kTimeTToMicrosecondsOffset <= us_) { 157 if (std::numeric_limits<int64>::max() - kTimeTToMicrosecondsOffset <= us_) {
158 DLOG(WARNING) << "Overflow when converting base::Time with internal " << 158 DLOG(WARNING) << "Overflow when converting base::Time with internal " <<
159 "value " << us_ << " to time_t."; 159 "value " << us_ << " to time_t.";
160 return std::numeric_limits<time_t>::max(); 160 return std::numeric_limits<time_t>::max();
161 } 161 }
162 return (us_ - kTimeTToMicrosecondsOffset) / kMicrosecondsPerSecond; 162 return (us_ - kTimeTToMicrosecondsOffset) / kMicrosecondsPerSecond;
163 } 163 }
164 164
165 // static 165 // static
166 Time Time::FromDoubleT(double dt) { 166 Time Time::FromDoubleT(double dt) {
167 if (dt == 0 || std::isnan(dt)) 167 if (dt == 0 || std::isnan(dt))
168 return Time(); // Preserve 0 so we can tell it doesn't exist. 168 return Time(); // Preserve 0 so we can tell it doesn't exist.
169 return Time(kTimeTToMicrosecondsOffset) + TimeDelta::FromSecondsD(dt); 169 if (dt == std::numeric_limits<double>::infinity())
170 return Max();
171 return Time(static_cast<int64>((dt *
172 static_cast<double>(kMicrosecondsPerSecond)) +
173 kTimeTToMicrosecondsOffset));
170 } 174 }
171 175
172 double Time::ToDoubleT() const { 176 double Time::ToDoubleT() const {
173 if (is_null()) 177 if (is_null())
174 return 0; // Preserve 0 so we can tell it doesn't exist. 178 return 0; // Preserve 0 so we can tell it doesn't exist.
175 if (is_max()) { 179 if (is_max()) {
176 // Preserve max without offset to prevent overflow. 180 // Preserve max without offset to prevent overflow.
177 return std::numeric_limits<double>::infinity(); 181 return std::numeric_limits<double>::infinity();
178 } 182 }
179 return (static_cast<double>(us_ - kTimeTToMicrosecondsOffset) / 183 return (static_cast<double>(us_ - kTimeTToMicrosecondsOffset) /
180 static_cast<double>(kMicrosecondsPerSecond)); 184 static_cast<double>(kMicrosecondsPerSecond));
181 } 185 }
182 186
183 #if defined(OS_POSIX) 187 #if defined(OS_POSIX)
184 // static 188 // static
185 Time Time::FromTimeSpec(const timespec& ts) { 189 Time Time::FromTimeSpec(const timespec& ts) {
186 return FromDoubleT(ts.tv_sec + 190 return FromDoubleT(ts.tv_sec +
187 static_cast<double>(ts.tv_nsec) / 191 static_cast<double>(ts.tv_nsec) /
188 base::Time::kNanosecondsPerSecond); 192 base::Time::kNanosecondsPerSecond);
189 } 193 }
190 #endif 194 #endif
191 195
192 // static 196 // static
193 Time Time::FromJsTime(double ms_since_epoch) { 197 Time Time::FromJsTime(double ms_since_epoch) {
194 // The epoch is a valid time, so this constructor doesn't interpret 198 // The epoch is a valid time, so this constructor doesn't interpret
195 // 0 as the null time. 199 // 0 as the null time.
196 return Time(kTimeTToMicrosecondsOffset) + 200 if (ms_since_epoch == std::numeric_limits<double>::infinity())
197 TimeDelta::FromMillisecondsD(ms_since_epoch); 201 return Max();
202 return Time(static_cast<int64>(ms_since_epoch * kMicrosecondsPerMillisecond) +
203 kTimeTToMicrosecondsOffset);
198 } 204 }
199 205
200 double Time::ToJsTime() const { 206 double Time::ToJsTime() const {
201 if (is_null()) { 207 if (is_null()) {
202 // Preserve 0 so the invalid result doesn't depend on the platform. 208 // Preserve 0 so the invalid result doesn't depend on the platform.
203 return 0; 209 return 0;
204 } 210 }
205 if (is_max()) { 211 if (is_max()) {
206 // Preserve max without offset to prevent overflow. 212 // Preserve max without offset to prevent overflow.
207 return std::numeric_limits<double>::infinity(); 213 return std::numeric_limits<double>::infinity();
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after
341 return is_in_range(month, 1, 12) && 347 return is_in_range(month, 1, 12) &&
342 is_in_range(day_of_week, 0, 6) && 348 is_in_range(day_of_week, 0, 6) &&
343 is_in_range(day_of_month, 1, 31) && 349 is_in_range(day_of_month, 1, 31) &&
344 is_in_range(hour, 0, 23) && 350 is_in_range(hour, 0, 23) &&
345 is_in_range(minute, 0, 59) && 351 is_in_range(minute, 0, 59) &&
346 is_in_range(second, 0, 60) && 352 is_in_range(second, 0, 60) &&
347 is_in_range(millisecond, 0, 999); 353 is_in_range(millisecond, 0, 999);
348 } 354 }
349 355
350 } // namespace base 356 } // 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