| 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 <cmath> |
| 7 #include <limits> | 8 #include <limits> |
| 8 #include <ostream> | 9 #include <ostream> |
| 9 | 10 |
| 10 #include "base/float_util.h" | 11 #include "base/float_util.h" |
| 11 #include "base/lazy_instance.h" | 12 #include "base/lazy_instance.h" |
| 12 #include "base/logging.h" | 13 #include "base/logging.h" |
| 13 #include "base/third_party/nspr/prtime.h" | 14 #include "base/third_party/nspr/prtime.h" |
| 14 #include "base/third_party/nspr/prtypes.h" | 15 #include "base/third_party/nspr/prtypes.h" |
| 15 | 16 |
| 16 namespace base { | 17 namespace base { |
| (...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 206 }; | 207 }; |
| 207 | 208 |
| 208 static LazyInstance<UnixEpochSingleton>::Leaky | 209 static LazyInstance<UnixEpochSingleton>::Leaky |
| 209 leaky_unix_epoch_singleton_instance = LAZY_INSTANCE_INITIALIZER; | 210 leaky_unix_epoch_singleton_instance = LAZY_INSTANCE_INITIALIZER; |
| 210 | 211 |
| 211 // Static | 212 // Static |
| 212 TimeTicks TimeTicks::UnixEpoch() { | 213 TimeTicks TimeTicks::UnixEpoch() { |
| 213 return leaky_unix_epoch_singleton_instance.Get().unix_epoch(); | 214 return leaky_unix_epoch_singleton_instance.Get().unix_epoch(); |
| 214 } | 215 } |
| 215 | 216 |
| 217 TimeTicks TimeTicks::FromWebKit(double dt) { |
| 218 // Outside of 2**53 the conversion to/from double isn't presevative. |
| 219 |
| 220 if (dt == 0 || IsNaN(dt)) |
| 221 return TimeTicks(); // Preserve 0 so we can tell it doesn't exist. |
| 222 |
| 223 double dus = dt * static_cast<double>(Time::kMicrosecondsPerSecond); |
| 224 |
| 225 // Check double isn't too big for an int64 |
| 226 if (dus >= static_cast<double>(std::numeric_limits<int64>::max())) |
| 227 return TimeTicks(std::numeric_limits<int64>::max()); |
| 228 if (dus <= static_cast<double>(std::numeric_limits<int64>::min())) |
| 229 return TimeTicks(std::numeric_limits<int64>::min()); |
| 230 |
| 231 int64 epoch_us = static_cast<int64>(dus); |
| 232 |
| 233 // Make sure the Epoch adjustment won't cause over/under flow. |
| 234 if (UnixEpoch().ticks_ > 0 && |
| 235 epoch_us >= std::numeric_limits<int64>::max() - UnixEpoch().ticks_) |
| 236 return TimeTicks(std::numeric_limits<int64>::max()); |
| 237 if (UnixEpoch().ticks_ < 0 && |
| 238 epoch_us <= std::numeric_limits<int64>::min() - UnixEpoch().ticks_) |
| 239 return TimeTicks(std::numeric_limits<int64>::min()); |
| 240 |
| 241 return TimeTicks(epoch_us + UnixEpoch().ticks_); |
| 242 } |
| 243 |
| 244 double TimeTicks::ToWebKit() const { |
| 245 if (is_null()) |
| 246 return 0; // Preserve 0 so we can tell it doesn't exist. |
| 247 |
| 248 if (ticks_ == std::numeric_limits<int64>::max()) |
| 249 return std::numeric_limits<double>::infinity(); |
| 250 if (ticks_ == std::numeric_limits<int64>::min()) |
| 251 return -std::numeric_limits<double>::infinity(); |
| 252 |
| 253 return (static_cast<double>(ticks_ - UnixEpoch().ticks_) / |
| 254 static_cast<double>(Time::kMicrosecondsPerSecond)); |
| 255 } |
| 256 |
| 216 // Time::Exploded ------------------------------------------------------------- | 257 // Time::Exploded ------------------------------------------------------------- |
| 217 | 258 |
| 218 inline bool is_in_range(int value, int lo, int hi) { | 259 inline bool is_in_range(int value, int lo, int hi) { |
| 219 return lo <= value && value <= hi; | 260 return lo <= value && value <= hi; |
| 220 } | 261 } |
| 221 | 262 |
| 222 bool Time::Exploded::HasValidValues() const { | 263 bool Time::Exploded::HasValidValues() const { |
| 223 return is_in_range(month, 1, 12) && | 264 return is_in_range(month, 1, 12) && |
| 224 is_in_range(day_of_week, 0, 6) && | 265 is_in_range(day_of_week, 0, 6) && |
| 225 is_in_range(day_of_month, 1, 31) && | 266 is_in_range(day_of_month, 1, 31) && |
| 226 is_in_range(hour, 0, 23) && | 267 is_in_range(hour, 0, 23) && |
| 227 is_in_range(minute, 0, 59) && | 268 is_in_range(minute, 0, 59) && |
| 228 is_in_range(second, 0, 60) && | 269 is_in_range(second, 0, 60) && |
| 229 is_in_range(millisecond, 0, 999); | 270 is_in_range(millisecond, 0, 999); |
| 230 } | 271 } |
| 231 | 272 |
| 232 } // namespace base | 273 } // namespace base |
| OLD | NEW |