| 6 |
| 7 | #include <cmath> |
| 8 | #include <ios> |
| 9 | #include <limits> |
| 10 | #include <ostream> |
| 11 | #include <sstream> |
| 12 |
| 13 | #include "base/logging.h" |
| 14 | #include "base/macros.h" |
| 15 | #include "base/no_destructor.h" |
| 16 | #include "base/strings/stringprintf.h" |
| 17 | #include "base/third_party/nspr/prtime.h" |
| 18 | #include "base/time/time_override.h" |
| 19 | #include "build/build_config.h" |
| 20 |
| 21 | namespace base { |
| 22 |
| 23 | namespace internal { |
| 24 |
| 25 | TimeNowFunction g_time_now_function = &subtle::TimeNowIgnoringOverride; |
| 26 |
| 27 | TimeNowFunction g_time_now_from_system_time_function = |
| 28 | &subtle::TimeNowFromSystemTimeIgnoringOverride; |
| 29 |
| 30 | TimeTicksNowFunction g_time_ticks_now_function = |
| 31 | &subtle::TimeTicksNowIgnoringOverride; |
| 32 |
| 33 | ThreadTicksNowFunction g_thread_ticks_now_function = |
| 34 | &subtle::ThreadTicksNowIgnoringOverride; |
| 35 |
| 36 | } // namespace internal |
| 37 |
| 38 | // TimeDelta ------------------------------------------------------------------ |
| 39 |
| 40 | int TimeDelta::InDays() const { |
| 41 | if (is_max()) { |
| 42 | // Preserve max to prevent overflow. |
| 43 | return std::numeric_limits<int>::max(); |
| 44 | } |
| 45 | if (is_min()) { |
| 46 | // Preserve min to prevent underflow. |
| 47 | return std::numeric_limits<int>::min(); |
| 48 | } |
| 49 | return static_cast<int>(delta_ / Time::kMicrosecondsPerDay); |
| 50 | } |
| 51 |
| 52 | int TimeDelta::InDaysFloored() const { |
| 53 | if (is_max()) { |
| 54 | // Preserve max to prevent overflow. |
| 55 | return std::numeric_limits<int>::max(); |
| 56 | } |
| 57 | if (is_min()) { |
| 58 | // Preserve min to prevent underflow. |
| 59 | return std::numeric_limits<int>::min(); |
| 60 | } |
| 61 | int result = delta_ / Time::kMicrosecondsPerDay; |
| 62 | int64_t remainder = delta_ - (result * Time::kMicrosecondsPerDay); |
| 63 | if (remainder < 0) { |
| 64 | --result; // Use floor(), not trunc() rounding behavior. |
| 65 | } |
| 66 | return result; |
| 67 | } |
| 68 |
| 69 | int TimeDelta::InHours() const { |
| 70 | if (is_max()) { |
| 71 | // Preserve max to prevent overflow. |
| 72 | return std::numeric_limits<int>::max(); |
| 73 | } |
| 74 | if (is_min()) { |
| 75 | // Preserve min to prevent underflow. |
| 76 | return std::numeric_limits<int>::min(); |
| 77 | } |
| 78 | return static_cast<int>(delta_ / Time::kMicrosecondsPerHour); |
| 79 | } |
| 80 |
| 81 | int TimeDelta::InMinutes() const { |
| 82 | if (is_max()) { |
| 83 | // Preserve max to prevent overflow. |
| 84 | return std::numeric_limits<int>::max(); |
| 85 | } |
| 86 | if (is_min()) { |
| 87 | // Preserve min to prevent underflow. |
| 88 | return std::numeric_limits<int>::min(); |
| 89 | } |
| 90 | return static_cast<int>(delta_ / Time::kMicrosecondsPerMinute); |
| 91 | } |
| 92 |
| 93 | double TimeDelta::InSecondsF() const { |
| 94 | if (is_max()) { |
| 95 | // Preserve max to prevent overflow. |
| 96 | return std::numeric_limits<double>::infinity(); |
| 97 | } |
| 98 | if (is_min()) { |
| 99 | // Preserve min to prevent underflow. |
| 100 | return -std::numeric_limits<double>::infinity(); |
| 101 | } |
| 102 | return static_cast<double>(delta_) / Time::kMicrosecondsPerSecond; |
| 103 | } |
| 104 |
| 105 | int64_t TimeDelta::InSeconds() const { |
| 106 | if (is_max()) { |
| 107 | // Preserve max to prevent overflow. |
| 108 | return std::numeric_limits<int64_t>::max(); |
| 109 | } |
| 110 | if (is_min()) { |
| 111 | // Preserve min to prevent underflow. |
| 112 | return std::numeric_limits<int64_t>::min(); |
| 113 | } |
| 114 | return delta_ / Time::kMicrosecondsPerSecond; |
| 115 | } |
| 116 |
| 117 | double TimeDelta::InMillisecondsF() const { |
| 118 | if (is_max()) { |
| 119 | // Preserve max to prevent overflow. |
| 120 | return std::numeric_limits<double>::infinity(); |
| 121 | } |
| 122 | if (is_min()) { |
| 123 | // Preserve min to prevent underflow. |
| 124 | return -std::numeric_limits<double>::infinity(); |
| 125 | } |
| 126 | return static_cast<double>(delta_) / Time::kMicrosecondsPerMillisecond; |
| 127 | } |
| 128 |
| 129 | int64_t TimeDelta::InMilliseconds() const { |
| 130 | if (is_max()) { |
| 131 | // Preserve max to prevent overflow. |
| 132 | return std::numeric_limits<int64_t>::max(); |
| 133 | } |
| 134 | if (is_min()) { |
| 135 | // Preserve min to prevent underflow. |
| 136 | return std::numeric_limits<int64_t>::min(); |
| 137 | } |
| 138 | return delta_ / Time::kMicrosecondsPerMillisecond; |
| 139 | } |
| 140 |
| 141 | int64_t TimeDelta::InMillisecondsRoundedUp() const { |
| 142 | if (is_max()) { |
| 143 | // Preserve max to prevent overflow. |
| 144 | return std::numeric_limits<int64_t>::max(); |
| 145 | } |
| 146 | if (is_min()) { |
| 147 | // Preserve min to prevent underflow. |
| 148 | return std::numeric_limits<int64_t>::min(); |
| 149 | } |
| 150 | int64_t result = delta_ / Time::kMicrosecondsPerMillisecond; |
| 151 | int64_t remainder = delta_ - (result * Time::kMicrosecondsPerMillisecond); |
| 152 | if (remainder > 0) { |
| 153 | ++result; // Use ceil(), not trunc() rounding behavior. |
| 154 | } |
| 155 | return result; |
| 156 | } |
| 157 |
| 158 | double TimeDelta::InMicrosecondsF() const { |
| 159 | if (is_max()) { |
| 160 | // Preserve max to prevent overflow. |
| 161 | return std::numeric_limits<double>::infinity(); |
| 162 | } |
| 163 | if (is_min()) { |
| 164 | // Preserve min to prevent underflow. |
| 165 | return -std::numeric_limits<double>::infinity(); |
| 166 | } |
| 167 | return static_cast<double>(delta_); |
| 168 | } |
| 169 |
| 170 | int64_t TimeDelta::InNanoseconds() const { |
| 171 | if (is_max()) { |
| 172 | // Preserve max to prevent overflow. |
| 173 | return std::numeric_limits<int64_t>::max(); |
| 174 | } |
| 175 | if (is_min()) { |
| 176 | // Preserve min to prevent underflow. |
| 177 | return std::numeric_limits<int64_t>::min(); |
| 178 | } |
| 179 | return delta_ * Time::kNanosecondsPerMicrosecond; |
| 180 | } |
| 181 |
| 182 | std::ostream& operator<<(std::ostream& os, TimeDelta time_delta) { |
| 183 | return os << time_delta.InSecondsF() << " s"; |
| 184 | } |
| 185 |
| 186 | // Time ----------------------------------------------------------------------- |
| 187 |
| 188 | // static |
| 189 | Time Time::Now() { |
| 190 | return internal::g_time_now_function(); |
| 191 | } |
| 192 |
| 193 | // static |
| 194 | Time Time::NowFromSystemTime() { |
| 195 | // Just use g_time_now_function because it returns the system time. |
| 196 | return internal::g_time_now_from_system_time_function(); |
| 197 | } |
| 198 |
| 199 | // static |
| 200 | Time Time::FromDeltaSinceWindowsEpoch(TimeDelta delta) { |
| 201 | return Time(delta.InMicroseconds()); |
| 202 | } |
| 203 |
| 204 | TimeDelta Time::ToDeltaSinceWindowsEpoch() const { |
| 205 | return TimeDelta::FromMicroseconds(us_); |
| 206 | } |
| 207 |
| 208 | // static |
| 209 | Time Time::FromTimeT(time_t tt) { |
| 210 | if (tt == 0) |
| 211 | return Time(); // Preserve 0 so we can tell it doesn't exist. |
| 212 | if (tt == std::numeric_limits<time_t>::max()) |
| 213 | return Max(); |
| 214 | return Time(kTimeTToMicrosecondsOffset) + TimeDelta::FromSeconds(tt); |
| 215 | } |
| 216 |
| 217 | time_t Time::ToTimeT() const { |
| 218 | if (is_null()) |
| 219 | return 0; // Preserve 0 so we can tell it doesn't exist. |
| 220 | if (is_max()) { |
| 221 | // Preserve max without offset to prevent overflow. |
| 222 | return std::numeric_limits<time_t>::max(); |
| 223 | } |
| 224 | if (is_min()) { |
| 225 | // Preserve min without offset to prevent underflow. |
| 226 | return std::numeric_limits<time_t>::min(); |
| 227 | } |
| 228 | if (std::numeric_limits<int64_t>::max() - kTimeTToMicrosecondsOffset <= us_) { |
| 229 | DLOG(WARNING) << "Overflow when converting base::Time with internal " << |
| 230 | "value " << us_ << " to time_t."; |
| 231 | return std::numeric_limits<time_t>::max(); |
| 232 | } |
| 233 | return (us_ - kTimeTToMicrosecondsOffset) / kMicrosecondsPerSecond; |
| 234 | } |
| 235 |
| 236 | // static |
| 237 | Time Time::FromDoubleT(double dt) { |
| 238 | if (dt == 0 || std::isnan(dt)) |
| 239 | return Time(); // Preserve 0 so we can tell it doesn't exist. |
| 240 | return Time(kTimeTToMicrosecondsOffset) + TimeDelta::FromSecondsD(dt); |
| 241 | } |
| 242 |
| 243 | double Time::ToDoubleT() const { |
| 244 | if (is_null()) |
| 245 | return 0; // Preserve 0 so we can tell it doesn't exist. |
| 246 | if (is_max()) { |
| 247 | // Preserve max without offset to prevent overflow. |
| 248 | return std::numeric_limits<double>::infinity(); |
| 249 | } |
| 250 | if (is_min()) { |
| 251 | // Preserve min without offset to prevent underflow. |
| 252 | return -std::numeric_limits<double>::infinity(); |
| 253 | } |
| 254 | return (static_cast<double>(us_ - kTimeTToMicrosecondsOffset) / |
| 255 | static_cast<double>(kMicrosecondsPerSecond)); |
| 256 | } |
| 257 |
| 258 | #if defined(OS_POSIX) |
| 259 | // static |
| 260 | Time Time::FromTimeSpec(const timespec& ts) { |
| 261 | return FromDoubleT(ts.tv_sec + |
| 262 | static_cast<double>(ts.tv_nsec) / |
| 263 | base::Time::kNanosecondsPerSecond); |
| 264 | } |
| 265 | #endif |
| 266 |
| 267 | // static |
| 268 | Time Time::FromJsTime(double ms_since_epoch) { |
| 269 | // The epoch is a valid time, so this constructor doesn't interpret |
| 270 | // 0 as the null time. |
| 271 | return Time(kTimeTToMicrosecondsOffset) + |
| 272 | TimeDelta::FromMillisecondsD(ms_since_epoch); |
| 273 | } |
| 274 |
| 275 | double Time::ToJsTime() const { |
| 276 | if (is_null()) { |
| 277 | // Preserve 0 so the invalid result doesn't depend on the platform. |
| 278 | return 0; |
| 279 | } |
| 280 | return ToJsTimeIgnoringNull(); |
| 281 | } |
| 282 |
| 283 | double Time::ToJsTimeIgnoringNull() const { |
| 284 | if (is_max()) { |
| 285 | // Preserve max without offset to prevent overflow. |
| 286 | return std::numeric_limits<double>::infinity(); |
| 287 | } |
| 288 | if (is_min()) { |
| 289 | // Preserve min without offset to prevent underflow. |
| 290 | return -std::numeric_limits<double>::infinity(); |
| 291 | } |
| 292 | return (static_cast<double>(us_ - kTimeTToMicrosecondsOffset) / |
| 293 | kMicrosecondsPerMillisecond); |
| 294 | } |
| 295 |
| 296 | Time Time::FromJavaTime(int64_t ms_since_epoch) { |
| 297 | return base::Time::UnixEpoch() + |
| 298 | base::TimeDelta::FromMilliseconds(ms_since_epoch); |
| 299 | } |
| 300 |
| 301 | int64_t Time::ToJavaTime() const { |
| 302 | if (is_null()) { |
| 303 | // Preserve 0 so the invalid result doesn't depend on the platform. |
| 304 | return 0; |
| 305 | } |
| 306 | if (is_max()) { |
| 307 | // Preserve max without offset to prevent overflow. |
| 308 | return std::numeric_limits<int64_t>::max(); |
| 309 | } |
| 310 | if (is_min()) { |
| 311 | // Preserve min without offset to prevent underflow. |
| 312 | return std::numeric_limits<int64_t>::min(); |
| 313 | } |
| 314 | return ((us_ - kTimeTToMicrosecondsOffset) / |
| 315 | kMicrosecondsPerMillisecond); |
| 316 | } |
| 317 |
| 318 | // static |
| 319 | Time Time::UnixEpoch() { |
| 320 | Time time; |
| 321 | time.us_ = kTimeTToMicrosecondsOffset; |
| 322 | return time; |
| 323 | } |
| 324 |
| 325 | Time Time::Midnight(bool is_local) const { |
| 326 | Exploded exploded; |
| 327 | Explode(is_local, &exploded); |
| 328 | exploded.hour = 0; |
| 329 | exploded.minute = 0; |
| 330 | exploded.second = 0; |
| 331 | exploded.millisecond = 0; |
| 332 | Time out_time; |
| 333 | if (FromExploded(is_local, exploded, &out_time)) { |
| 334 | return out_time; |
| 335 | } else if (is_local) { |
| 336 | // Hitting this branch means 00:00:00am of the current day |
| 337 | // does not exist (due to Daylight Saving Time in some countries |
| 338 | // where clocks are shifted at midnight). In this case, midnight |
| 339 | // should be defined as 01:00:00am. |
| 340 | exploded.hour = 1; |
| 341 | if (FromExploded(is_local, exploded, &out_time)) |
| 342 | return out_time; |
| 343 | } |
| 344 | // This function must not fail. |
| 345 | NOTREACHED(); |
| 346 | return Time(); |
| 347 | } |
| 348 |
| 349 | // static |
| 350 | bool Time::FromStringInternal(const char* time_string, |
| 351 | bool is_local, |
| 352 | Time* parsed_time) { |
| 353 | DCHECK((time_string != nullptr) && (parsed_time != nullptr)); |
| 354 |
| 355 | if (time_string[0] == '\0') |
| 356 | return false; |
| 357 |
| 358 | PRTime result_time = 0; |
| 359 | PRStatus result = PR_ParseTimeString(time_string, |
| 360 | is_local ? PR_FALSE : PR_TRUE, |
| 361 | &result_time); |
| 362 | if (PR_SUCCESS != result) |
| 363 | return false; |
| 364 |
| 365 | result_time += kTimeTToMicrosecondsOffset; |
| 366 | *parsed_time = Time(result_time); |
| 367 | return true; |
| 368 | } |
| 369 |
| 370 | // static |
| 371 | bool Time::ExplodedMostlyEquals(const Exploded& lhs, const Exploded& rhs) { |
| 372 | return lhs.year == rhs.year && lhs.month == rhs.month && |
| 373 | lhs.day_of_month == rhs.day_of_month && lhs.hour == rhs.hour && |
| 374 | lhs.minute == rhs.minute && lhs.second == rhs.second && |
| 375 | lhs.millisecond == rhs.millisecond; |
| 376 | } |
| 377 |
| 378 | // static |
| 379 | bool Time::FromMillisecondsSinceUnixEpoch(int64_t unix_milliseconds, |
| 380 | Time* time) { |
| 381 | // Adjust the provided time from milliseconds since the Unix epoch (1970) to |
| 382 | // microseconds since the Windows epoch (1601), avoiding overflows. |
| 383 | base::CheckedNumeric<int64_t> checked_microseconds_win_epoch = |
| 384 | unix_milliseconds; |
| 385 | checked_microseconds_win_epoch *= kMicrosecondsPerMillisecond; |
| 386 | checked_microseconds_win_epoch += kTimeTToMicrosecondsOffset; |
| 387 | if (!checked_microseconds_win_epoch.IsValid()) { |
| 388 | *time = base::Time(0); |
| 389 | return false; |
| 390 | } |
| 391 |
| 392 | *time = Time(checked_microseconds_win_epoch.ValueOrDie()); |
| 393 | return true; |
| 394 | } |
| 395 |
| 396 | int64_t Time::ToRoundedDownMillisecondsSinceUnixEpoch() const { |
| 397 | // Adjust from Windows epoch (1601) to Unix epoch (1970). |
| 398 | int64_t microseconds = us_ - kTimeTToMicrosecondsOffset; |
| 399 |
| 400 | // Round the microseconds towards -infinity. |
| 401 | if (microseconds >= 0) { |
| 402 | // In this case, rounding towards -infinity means rounding towards 0. |
| 403 | return microseconds / kMicrosecondsPerMillisecond; |
| 404 | } else { |
| 405 | return (microseconds + 1) / kMicrosecondsPerMillisecond - 1; |
| 406 | } |
| 407 | } |
| 408 |
| 409 | std::ostream& operator<<(std::ostream& os, Time time) { |
| 410 | Time::Exploded exploded; |
| 411 | time.UTCExplode(&exploded); |
| 412 | // Use StringPrintf because iostreams formatting is painful. |
| 413 | return os << StringPrintf("%04d-%02d-%02d %02d:%02d:%02d.%03d UTC", |
| 414 | exploded.year, |
| 415 | exploded.month, |
| 416 | exploded.day_of_month, |
| 417 | exploded.hour, |
| 418 | exploded.minute, |
| 419 | exploded.second, |
| 420 | exploded.millisecond); |
| 421 | } |
| 422 |
| 423 | // TimeTicks ------------------------------------------------------------------ |
| 424 |
| 425 | // static |
| 426 | TimeTicks TimeTicks::Now() { |
| 427 | return internal::g_time_ticks_now_function(); |
| 428 | } |
| 429 |
| 430 | // static |
| 431 | TimeTicks TimeTicks::UnixEpoch() { |
| 432 | static const base::NoDestructor<base::TimeTicks> epoch([]() { |
| 433 | return subtle::TimeTicksNowIgnoringOverride() - |
| 434 | (subtle::TimeNowIgnoringOverride() - Time::UnixEpoch()); |
| 435 | }()); |
| 436 | return *epoch; |
| 437 | } |
| 438 |
| 439 | TimeTicks TimeTicks::SnappedToNextTick(TimeTicks tick_phase, |
| 440 | TimeDelta tick_interval) const { |
| 441 | // |interval_offset| is the offset from |this| to the next multiple of |
| 442 | // |tick_interval| after |tick_phase|, possibly negative if in the past. |
| 443 | TimeDelta interval_offset = (tick_phase - *this) % tick_interval; |
| 444 | // If |this| is exactly on the interval (i.e. offset==0), don't adjust. |
| 445 | // Otherwise, if |tick_phase| was in the past, adjust forward to the next |
| 446 | // tick after |this|. |
| 447 | if (!interval_offset.is_zero() && tick_phase < *this) |
| 448 | interval_offset += tick_interval; |
| 449 | return *this + interval_offset; |
| 450 | } |
| 451 |
| 452 | std::ostream& operator<<(std::ostream& os, TimeTicks time_ticks) { |
| 453 | // This function formats a TimeTicks object as "bogo-microseconds". |
| 454 | // The origin and granularity of the count are platform-specific, and may very |
| 455 | // from run to run. Although bogo-microseconds usually roughly correspond to |
| 456 | // real microseconds, the only real guarantee is that the number never goes |
| 457 | // down during a single run. |
| 458 | const TimeDelta as_time_delta = time_ticks - TimeTicks(); |
| 459 | return os << as_time_delta.InMicroseconds() << " bogo-microseconds"; |
| 460 | } |
| 461 |
| 462 | // ThreadTicks ---------------------------------------------------------------- |
| 463 |
| 464 | // static |
| 465 | ThreadTicks ThreadTicks::Now() { |
| 466 | return internal::g_thread_ticks_now_function(); |
| 467 | } |
| 468 |
| 469 | std::ostream& operator<<(std::ostream& os, ThreadTicks thread_ticks) { |
| 470 | const TimeDelta as_time_delta = thread_ticks - ThreadTicks(); |
| 471 | return os << as_time_delta.InMicroseconds() << " bogo-thread-microseconds"; |
| 472 | } |
| 473 |
| 474 | // Time::Exploded ------------------------------------------------------------- |
| 475 |
| 476 | inline bool is_in_range(int value, int lo, int hi) { |
| 477 | return lo <= value && value <= hi; |
| 478 | } |
| 479 |
| 480 | bool Time::Exploded::HasValidValues() const { |
| 481 | return is_in_range(month, 1, 12) && |
| 482 | is_in_range(day_of_week, 0, 6) && |
| 483 | is_in_range(day_of_month, 1, 31) && |
| 484 | is_in_range(hour, 0, 23) && |
| 485 | is_in_range(minute, 0, 59) && |
| 486 | is_in_range(second, 0, 60) && |
| 487 | is_in_range(millisecond, 0, 999); |
| 488 | } |
| 489 |
| 490 | } // namespace base |
| 491 |