Chromium Code Reviews| 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 <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 Loading... | |
| 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((tt * kMicrosecondsPerSecond) + kTimeTToMicrosecondsOffset); | 147 return Time(kTimeTToMicrosecondsOffset) + TimeDelta::FromSeconds(tt); |
|
Nico
2015/07/17 17:00:23
I like the changes in this file :-)
| |
| 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 if (dt == std::numeric_limits<double>::infinity()) | 169 return Time(kTimeTToMicrosecondsOffset) + TimeDelta::FromSecondsD(dt); |
| 170 return Max(); | |
| 171 return Time(static_cast<int64>((dt * | |
| 172 static_cast<double>(kMicrosecondsPerSecond)) + | |
| 173 kTimeTToMicrosecondsOffset)); | |
| 174 } | 170 } |
| 175 | 171 |
| 176 double Time::ToDoubleT() const { | 172 double Time::ToDoubleT() const { |
| 177 if (is_null()) | 173 if (is_null()) |
| 178 return 0; // Preserve 0 so we can tell it doesn't exist. | 174 return 0; // Preserve 0 so we can tell it doesn't exist. |
| 179 if (is_max()) { | 175 if (is_max()) { |
| 180 // Preserve max without offset to prevent overflow. | 176 // Preserve max without offset to prevent overflow. |
| 181 return std::numeric_limits<double>::infinity(); | 177 return std::numeric_limits<double>::infinity(); |
| 182 } | 178 } |
| 183 return (static_cast<double>(us_ - kTimeTToMicrosecondsOffset) / | 179 return (static_cast<double>(us_ - kTimeTToMicrosecondsOffset) / |
| 184 static_cast<double>(kMicrosecondsPerSecond)); | 180 static_cast<double>(kMicrosecondsPerSecond)); |
| 185 } | 181 } |
| 186 | 182 |
| 187 #if defined(OS_POSIX) | 183 #if defined(OS_POSIX) |
| 188 // static | 184 // static |
| 189 Time Time::FromTimeSpec(const timespec& ts) { | 185 Time Time::FromTimeSpec(const timespec& ts) { |
| 190 return FromDoubleT(ts.tv_sec + | 186 return FromDoubleT(ts.tv_sec + |
| 191 static_cast<double>(ts.tv_nsec) / | 187 static_cast<double>(ts.tv_nsec) / |
| 192 base::Time::kNanosecondsPerSecond); | 188 base::Time::kNanosecondsPerSecond); |
| 193 } | 189 } |
| 194 #endif | 190 #endif |
| 195 | 191 |
| 196 // static | 192 // static |
| 197 Time Time::FromJsTime(double ms_since_epoch) { | 193 Time Time::FromJsTime(double ms_since_epoch) { |
| 198 // The epoch is a valid time, so this constructor doesn't interpret | 194 // The epoch is a valid time, so this constructor doesn't interpret |
| 199 // 0 as the null time. | 195 // 0 as the null time. |
| 200 if (ms_since_epoch == std::numeric_limits<double>::infinity()) | 196 return Time(kTimeTToMicrosecondsOffset) + |
| 201 return Max(); | 197 TimeDelta::FromMillisecondsD(ms_since_epoch); |
| 202 return Time(static_cast<int64>(ms_since_epoch * kMicrosecondsPerMillisecond) + | |
| 203 kTimeTToMicrosecondsOffset); | |
| 204 } | 198 } |
| 205 | 199 |
| 206 double Time::ToJsTime() const { | 200 double Time::ToJsTime() const { |
| 207 if (is_null()) { | 201 if (is_null()) { |
| 208 // Preserve 0 so the invalid result doesn't depend on the platform. | 202 // Preserve 0 so the invalid result doesn't depend on the platform. |
| 209 return 0; | 203 return 0; |
| 210 } | 204 } |
| 211 if (is_max()) { | 205 if (is_max()) { |
| 212 // Preserve max without offset to prevent overflow. | 206 // Preserve max without offset to prevent overflow. |
| 213 return std::numeric_limits<double>::infinity(); | 207 return std::numeric_limits<double>::infinity(); |
| (...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 347 return is_in_range(month, 1, 12) && | 341 return is_in_range(month, 1, 12) && |
| 348 is_in_range(day_of_week, 0, 6) && | 342 is_in_range(day_of_week, 0, 6) && |
| 349 is_in_range(day_of_month, 1, 31) && | 343 is_in_range(day_of_month, 1, 31) && |
| 350 is_in_range(hour, 0, 23) && | 344 is_in_range(hour, 0, 23) && |
| 351 is_in_range(minute, 0, 59) && | 345 is_in_range(minute, 0, 59) && |
| 352 is_in_range(second, 0, 60) && | 346 is_in_range(second, 0, 60) && |
| 353 is_in_range(millisecond, 0, 999); | 347 is_in_range(millisecond, 0, 999); |
| 354 } | 348 } |
| 355 | 349 |
| 356 } // namespace base | 350 } // namespace base |
| OLD | NEW |