Index: base/time/time.cc |
diff --git a/base/time/time.cc b/base/time/time.cc |
index 9f3c53d6695b1e8b4960dc4911c761dff3b7aa56..dfe4ef04ff7718083ba8917167efc406359d9157 100644 |
--- a/base/time/time.cc |
+++ b/base/time/time.cc |
@@ -4,6 +4,7 @@ |
#include "base/time/time.h" |
+#include <cmath> |
#include <limits> |
#include <ostream> |
@@ -213,6 +214,46 @@ TimeTicks TimeTicks::UnixEpoch() { |
return leaky_unix_epoch_singleton_instance.Get().unix_epoch(); |
} |
+TimeTicks TimeTicks::FromWebKit(double dt) { |
+ // Outside of 2**53 the conversion to/from double isn't presevative. |
+ |
+ if (dt == 0 || IsNaN(dt)) |
+ return TimeTicks(); // Preserve 0 so we can tell it doesn't exist. |
+ |
+ double dus = dt * static_cast<double>(Time::kMicrosecondsPerSecond); |
+ |
+ // Check double isn't too big for an int64 |
+ if (dus >= static_cast<double>(std::numeric_limits<int64>::max())) |
+ return TimeTicks(std::numeric_limits<int64>::max()); |
+ if (dus <= static_cast<double>(std::numeric_limits<int64>::min())) |
+ return TimeTicks(std::numeric_limits<int64>::min()); |
+ |
+ int64 epoch_us = static_cast<int64>(dus); |
+ |
+ // Make sure the Epoch adjustment won't cause over/under flow. |
+ if (UnixEpoch().ticks_ > 0 && |
+ epoch_us >= std::numeric_limits<int64>::max() - UnixEpoch().ticks_) |
+ return TimeTicks(std::numeric_limits<int64>::max()); |
+ if (UnixEpoch().ticks_ < 0 && |
+ epoch_us <= std::numeric_limits<int64>::min() - UnixEpoch().ticks_) |
+ return TimeTicks(std::numeric_limits<int64>::min()); |
+ |
+ return TimeTicks(epoch_us + UnixEpoch().ticks_); |
+} |
+ |
+double TimeTicks::ToWebKit() const { |
+ if (is_null()) |
+ return 0; // Preserve 0 so we can tell it doesn't exist. |
+ |
+ if (ticks_ == std::numeric_limits<int64>::max()) |
+ return std::numeric_limits<double>::infinity(); |
+ if (ticks_ == std::numeric_limits<int64>::min()) |
+ return -std::numeric_limits<double>::infinity(); |
+ |
+ return (static_cast<double>(ticks_ - UnixEpoch().ticks_) / |
+ static_cast<double>(Time::kMicrosecondsPerSecond)); |
+} |
+ |
// Time::Exploded ------------------------------------------------------------- |
inline bool is_in_range(int value, int lo, int hi) { |