| 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.h" | 5 #include "base/time.h" |
| 6 | 6 |
| 7 #include <math.h> | 7 #include <math.h> |
| 8 #if defined(OS_WIN) | 8 #if defined(OS_WIN) |
| 9 #include <float.h> | 9 #include <float.h> |
| 10 #endif | 10 #endif |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 67 | 67 |
| 68 // static | 68 // static |
| 69 Time Time::Max() { | 69 Time Time::Max() { |
| 70 return Time(std::numeric_limits<int64>::max()); | 70 return Time(std::numeric_limits<int64>::max()); |
| 71 } | 71 } |
| 72 | 72 |
| 73 // static | 73 // static |
| 74 Time Time::FromTimeT(time_t tt) { | 74 Time Time::FromTimeT(time_t tt) { |
| 75 if (tt == 0) | 75 if (tt == 0) |
| 76 return Time(); // Preserve 0 so we can tell it doesn't exist. | 76 return Time(); // Preserve 0 so we can tell it doesn't exist. |
| 77 if (tt == std::numeric_limits<time_t>::max()) |
| 78 return Max(); |
| 77 return Time((tt * kMicrosecondsPerSecond) + kTimeTToMicrosecondsOffset); | 79 return Time((tt * kMicrosecondsPerSecond) + kTimeTToMicrosecondsOffset); |
| 78 } | 80 } |
| 79 | 81 |
| 80 time_t Time::ToTimeT() const { | 82 time_t Time::ToTimeT() const { |
| 81 if (us_ == 0) | 83 if (is_null()) |
| 82 return 0; // Preserve 0 so we can tell it doesn't exist. | 84 return 0; // Preserve 0 so we can tell it doesn't exist. |
| 85 if (is_max()) { |
| 86 // Preserve max without offset to prevent overflow. |
| 87 return std::numeric_limits<time_t>::max(); |
| 88 } |
| 89 if (std::numeric_limits<int64>::max() - kTimeTToMicrosecondsOffset <= us_) { |
| 90 DLOG(WARNING) << "Overflow when converting base::Time with internal " << |
| 91 "value " << us_ << " to time_t."; |
| 92 return std::numeric_limits<time_t>::max(); |
| 93 } |
| 83 return (us_ - kTimeTToMicrosecondsOffset) / kMicrosecondsPerSecond; | 94 return (us_ - kTimeTToMicrosecondsOffset) / kMicrosecondsPerSecond; |
| 84 } | 95 } |
| 85 | 96 |
| 86 // static | 97 // static |
| 87 Time Time::FromDoubleT(double dt) { | 98 Time Time::FromDoubleT(double dt) { |
| 88 if (dt == 0 || isnan(dt)) | 99 if (dt == 0 || isnan(dt)) |
| 89 return Time(); // Preserve 0 so we can tell it doesn't exist. | 100 return Time(); // Preserve 0 so we can tell it doesn't exist. |
| 101 if (dt == std::numeric_limits<double>::max()) |
| 102 return Max(); |
| 90 return Time(static_cast<int64>((dt * | 103 return Time(static_cast<int64>((dt * |
| 91 static_cast<double>(kMicrosecondsPerSecond)) + | 104 static_cast<double>(kMicrosecondsPerSecond)) + |
| 92 kTimeTToMicrosecondsOffset)); | 105 kTimeTToMicrosecondsOffset)); |
| 93 } | 106 } |
| 94 | 107 |
| 95 double Time::ToDoubleT() const { | 108 double Time::ToDoubleT() const { |
| 96 if (us_ == 0) | 109 if (is_null()) |
| 97 return 0; // Preserve 0 so we can tell it doesn't exist. | 110 return 0; // Preserve 0 so we can tell it doesn't exist. |
| 111 if (is_max()) { |
| 112 // Preserve max without offset to prevent overflow. |
| 113 return std::numeric_limits<double>::max(); |
| 114 } |
| 98 return (static_cast<double>(us_ - kTimeTToMicrosecondsOffset) / | 115 return (static_cast<double>(us_ - kTimeTToMicrosecondsOffset) / |
| 99 static_cast<double>(kMicrosecondsPerSecond)); | 116 static_cast<double>(kMicrosecondsPerSecond)); |
| 100 } | 117 } |
| 101 | 118 |
| 102 // static | 119 // static |
| 103 Time Time::FromJsTime(double ms_since_epoch) { | 120 Time Time::FromJsTime(double ms_since_epoch) { |
| 104 // The epoch is a valid time, so this constructor doesn't interpret | 121 // The epoch is a valid time, so this constructor doesn't interpret |
| 105 // 0 as the null time. | 122 // 0 as the null time. |
| 123 if (ms_since_epoch == std::numeric_limits<double>::max()) |
| 124 return Max(); |
| 106 return Time(static_cast<int64>(ms_since_epoch * kMicrosecondsPerMillisecond) + | 125 return Time(static_cast<int64>(ms_since_epoch * kMicrosecondsPerMillisecond) + |
| 107 kTimeTToMicrosecondsOffset); | 126 kTimeTToMicrosecondsOffset); |
| 108 } | 127 } |
| 109 | 128 |
| 110 double Time::ToJsTime() const { | 129 double Time::ToJsTime() const { |
| 111 if (us_ == 0) { | 130 if (is_null()) { |
| 112 // Preserve 0 so the invalid result doesn't depend on the platform. | 131 // Preserve 0 so the invalid result doesn't depend on the platform. |
| 113 return 0; | 132 return 0; |
| 114 } | 133 } |
| 134 if (is_max()) { |
| 135 // Preserve max without offset to prevent overflow. |
| 136 return std::numeric_limits<double>::max(); |
| 137 } |
| 115 return (static_cast<double>(us_ - kTimeTToMicrosecondsOffset) / | 138 return (static_cast<double>(us_ - kTimeTToMicrosecondsOffset) / |
| 116 kMicrosecondsPerMillisecond); | 139 kMicrosecondsPerMillisecond); |
| 117 } | 140 } |
| 118 | 141 |
| 119 // static | 142 // static |
| 120 Time Time::UnixEpoch() { | 143 Time Time::UnixEpoch() { |
| 121 Time time; | 144 Time time; |
| 122 time.us_ = kTimeTToMicrosecondsOffset; | 145 time.us_ = kTimeTToMicrosecondsOffset; |
| 123 return time; | 146 return time; |
| 124 } | 147 } |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 161 return is_in_range(month, 1, 12) && | 184 return is_in_range(month, 1, 12) && |
| 162 is_in_range(day_of_week, 0, 6) && | 185 is_in_range(day_of_week, 0, 6) && |
| 163 is_in_range(day_of_month, 1, 31) && | 186 is_in_range(day_of_month, 1, 31) && |
| 164 is_in_range(hour, 0, 23) && | 187 is_in_range(hour, 0, 23) && |
| 165 is_in_range(minute, 0, 59) && | 188 is_in_range(minute, 0, 59) && |
| 166 is_in_range(second, 0, 60) && | 189 is_in_range(second, 0, 60) && |
| 167 is_in_range(millisecond, 0, 999); | 190 is_in_range(millisecond, 0, 999); |
| 168 } | 191 } |
| 169 | 192 |
| 170 } // namespace base | 193 } // namespace base |
| OLD | NEW |