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 3// found in the LICENSE file. 4 5#include "base/time/time.h" 6 7#include <stdint.h> 8#include <time.h> 9#include <limits> 10#include <string> 11 12#include <gtest/gtest.h> 13 14#include "base/compiler_specific.h" 15#include "base/logging.h" 16#include "base/macros.h" 17#include "base/strings/stringprintf.h" 18#include "build/build_config.h" 19 20namespace base { 21 22namespace { 23 24// Specialized test fixture allowing time strings without timezones to be 25// tested by comparing them to a known time in the local zone. 26// See also pr_time_unittests.cc 27class TimeTest : public testing::Test { 28 protected: 29 void SetUp() override { 30 // Use mktime to get a time_t, and turn it into a PRTime by converting 31 // seconds to microseconds. Use 15th Oct 2007 12:45:00 local. This 32 // must be a time guaranteed to be outside of a DST fallback hour in 33 // any timezone. 34 struct tm local_comparison_tm = { 35 0, // second 36 45, // minute 37 12, // hour 38 15, // day of month 39 10 - 1, // month 40 2007 - 1900, // year 41 0, // day of week (ignored, output only) 42 0, // day of year (ignored, output only) 43 -1 // DST in effect, -1 tells mktime to figure it out 44 }; 45 46 time_t converted_time = mktime(&local_comparison_tm); 47 ASSERT_GT(converted_time, 0); 48 comparison_time_local_ = Time::FromTimeT(converted_time); 49 50 // time_t representation of 15th Oct 2007 12:45:00 PDT 51 comparison_time_pdt_ = Time::FromTimeT(1192477500); 52 } 53 54 Time comparison_time_local_; 55 Time comparison_time_pdt_; 56}; 57 58// Test conversions to/from time_t and exploding/unexploding. 59TEST_F(TimeTest, TimeT) { 60 // C library time and exploded time. 61 time_t now_t_1 = time(NULL); 62 struct tm tms; 63#if defined(OS_WIN) 64 localtime_s(&tms, &now_t_1); 65#elif defined(OS_POSIX) 66 localtime_r(&now_t_1, &tms); 67#endif 68 69 // Convert to ours. 70 Time our_time_1 = Time::FromTimeT(now_t_1); 71 Time::Exploded exploded; 72 our_time_1.LocalExplode(&exploded); 73 74 // This will test both our exploding and our time_t -> Time conversion. 75 EXPECT_EQ(tms.tm_year + 1900, exploded.year); 76 EXPECT_EQ(tms.tm_mon + 1, exploded.month); 77 EXPECT_EQ(tms.tm_mday, exploded.day_of_month); 78 EXPECT_EQ(tms.tm_hour, exploded.hour); 79 EXPECT_EQ(tms.tm_min, exploded.minute); 80 EXPECT_EQ(tms.tm_sec, exploded.second); 81 82 // Convert exploded back to the time struct. 83 Time our_time_2 = Time::FromLocalExploded(exploded); 84 EXPECT_TRUE(our_time_1 == our_time_2); 85 86 time_t now_t_2 = our_time_2.ToTimeT(); 87 EXPECT_EQ(now_t_1, now_t_2); 88 89 EXPECT_EQ(10, Time().FromTimeT(10).ToTimeT()); 90 EXPECT_EQ(10.0, Time().FromTimeT(10).ToDoubleT()); 91 92 // Conversions of 0 should stay 0. 93 EXPECT_EQ(0, Time().ToTimeT()); 94 EXPECT_EQ(0, Time::FromTimeT(0).ToInternalValue()); 95} 96 97// Test conversions to/from javascript time. 98TEST_F(TimeTest, JsTime) { 99 Time epoch = Time::FromJsTime(0.0); 100 EXPECT_EQ(epoch, Time::UnixEpoch()); 101 Time t = Time::FromJsTime(700000.3); 102 EXPECT_EQ(700.0003, t.ToDoubleT()); 103 t = Time::FromDoubleT(800.73); 104 EXPECT_EQ(800730.0, t.ToJsTime()); 105} 106 107#if defined(OS_POSIX) 108TEST_F(TimeTest, FromTimeVal) { 109 Time now = Time::Now(); 110 Time also_now = Time::FromTimeVal(now.ToTimeVal()); 111 EXPECT_EQ(now, also_now); 112} 113#endif // OS_POSIX 114 115TEST_F(TimeTest, FromExplodedWithMilliseconds) { 116 // Some platform implementations of FromExploded are liable to drop 117 // milliseconds if we aren't careful. 118 Time now = Time::NowFromSystemTime(); 119 Time::Exploded exploded1 = {0}; 120 now.UTCExplode(&exploded1); 121 exploded1.millisecond = 500; 122 Time time = Time::FromUTCExploded(exploded1); 123 Time::Exploded exploded2 = {0}; 124 time.UTCExplode(&exploded2); 125 EXPECT_EQ(exploded1.millisecond, exploded2.millisecond); 126} 127 128TEST_F(TimeTest, ZeroIsSymmetric) { 129 Time zero_time(Time::FromTimeT(0)); 130 EXPECT_EQ(0, zero_time.ToTimeT()); 131 132 EXPECT_EQ(0.0, zero_time.ToDoubleT()); 133} 134 135TEST_F(TimeTest, LocalExplode) { 136 Time a = Time::Now(); 137 Time::Exploded exploded; 138 a.LocalExplode(&exploded); 139 140 Time b = Time::FromLocalExploded(exploded); 141 142 // The exploded structure doesn't have microseconds, and on Mac & Linux, the 143 // internal OS conversion uses seconds, which will cause truncation. So we 144 // can only make sure that the delta is within one second. 145 EXPECT_TRUE((a - b) < TimeDelta::FromSeconds(1)); 146} 147 148TEST_F(TimeTest, UTCExplode) { 149 Time a = Time::Now(); 150 Time::Exploded exploded; 151 a.UTCExplode(&exploded); 152 153 Time b = Time::FromUTCExploded(exploded); 154 EXPECT_TRUE((a - b) < TimeDelta::FromSeconds(1)); 155} 156 157TEST_F(TimeTest, LocalMidnight) { 158 Time::Exploded exploded; 159 Time::Now().LocalMidnight().LocalExplode(&exploded); 160 EXPECT_EQ(0, exploded.hour); 161 EXPECT_EQ(0, exploded.minute); 162 EXPECT_EQ(0, exploded.second); 163 EXPECT_EQ(0, exploded.millisecond); 164} 165 166TEST_F(TimeTest, ExplodeBeforeUnixEpoch) { 167 static const int kUnixEpochYear = 1970; // In case this changes (ha!). 168 Time t; 169 Time::Exploded exploded; 170 171 t = Time::UnixEpoch() - TimeDelta::FromMicroseconds(1); 172 t.UTCExplode(&exploded); 173 EXPECT_TRUE(exploded.HasValidValues()); 174 // Should be 1969-12-31 23:59:59 999 milliseconds (and 999 microseconds). 175 EXPECT_EQ(kUnixEpochYear - 1, exploded.year); 176 EXPECT_EQ(12, exploded.month); 177 EXPECT_EQ(31, exploded.day_of_month); 178 EXPECT_EQ(23, exploded.hour); 179 EXPECT_EQ(59, exploded.minute); 180 EXPECT_EQ(59, exploded.second); 181 EXPECT_EQ(999, exploded.millisecond); 182 183 t = Time::UnixEpoch() - TimeDelta::FromMicroseconds(1000); 184 t.UTCExplode(&exploded); 185 EXPECT_TRUE(exploded.HasValidValues()); 186 // Should be 1969-12-31 23:59:59 999 milliseconds. 187 EXPECT_EQ(kUnixEpochYear - 1, exploded.year); 188 EXPECT_EQ(12, exploded.month); 189 EXPECT_EQ(31, exploded.day_of_month); 190 EXPECT_EQ(23, exploded.hour); 191 EXPECT_EQ(59, exploded.minute); 192 EXPECT_EQ(59, exploded.second); 193 EXPECT_EQ(999, exploded.millisecond); 194 195 t = Time::UnixEpoch() - TimeDelta::FromMicroseconds(1001); 196 t.UTCExplode(&exploded); 197 EXPECT_TRUE(exploded.HasValidValues()); 198 // Should be 1969-12-31 23:59:59 998 milliseconds (and 999 microseconds). 199 EXPECT_EQ(kUnixEpochYear - 1, exploded.year); 200 EXPECT_EQ(12, exploded.month); 201 EXPECT_EQ(31, exploded.day_of_month); 202 EXPECT_EQ(23, exploded.hour); 203 EXPECT_EQ(59, exploded.minute); 204 EXPECT_EQ(59, exploded.second); 205 EXPECT_EQ(998, exploded.millisecond); 206 207 t = Time::UnixEpoch() - TimeDelta::FromMilliseconds(1000); 208 t.UTCExplode(&exploded); 209 EXPECT_TRUE(exploded.HasValidValues()); 210 // Should be 1969-12-31 23:59:59. 211 EXPECT_EQ(kUnixEpochYear - 1, exploded.year); 212 EXPECT_EQ(12, exploded.month); 213 EXPECT_EQ(31, exploded.day_of_month); 214 EXPECT_EQ(23, exploded.hour); 215 EXPECT_EQ(59, exploded.minute); 216 EXPECT_EQ(59, exploded.second); 217 EXPECT_EQ(0, exploded.millisecond); 218 219 t = Time::UnixEpoch() - TimeDelta::FromMilliseconds(1001); 220 t.UTCExplode(&exploded); 221 EXPECT_TRUE(exploded.HasValidValues()); 222 // Should be 1969-12-31 23:59:58 999 milliseconds. 223 EXPECT_EQ(kUnixEpochYear - 1, exploded.year); 224 EXPECT_EQ(12, exploded.month); 225 EXPECT_EQ(31, exploded.day_of_month); 226 EXPECT_EQ(23, exploded.hour); 227 EXPECT_EQ(59, exploded.minute); 228 EXPECT_EQ(58, exploded.second); 229 EXPECT_EQ(999, exploded.millisecond); 230 231 // Make sure we still handle at/after Unix epoch correctly. 232 t = Time::UnixEpoch(); 233 t.UTCExplode(&exploded); 234 EXPECT_TRUE(exploded.HasValidValues()); 235 // Should be 1970-12-31 00:00:00 0 milliseconds. 236 EXPECT_EQ(kUnixEpochYear, exploded.year); 237 EXPECT_EQ(1, exploded.month); 238 EXPECT_EQ(1, exploded.day_of_month); 239 EXPECT_EQ(0, exploded.hour); 240 EXPECT_EQ(0, exploded.minute); 241 EXPECT_EQ(0, exploded.second); 242 EXPECT_EQ(0, exploded.millisecond); 243 244 t = Time::UnixEpoch() + TimeDelta::FromMicroseconds(1); 245 t.UTCExplode(&exploded); 246 EXPECT_TRUE(exploded.HasValidValues()); 247 // Should be 1970-01-01 00:00:00 0 milliseconds (and 1 microsecond). 248 EXPECT_EQ(kUnixEpochYear, exploded.year); 249 EXPECT_EQ(1, exploded.month); 250 EXPECT_EQ(1, exploded.day_of_month); 251 EXPECT_EQ(0, exploded.hour); 252 EXPECT_EQ(0, exploded.minute); 253 EXPECT_EQ(0, exploded.second); 254 EXPECT_EQ(0, exploded.millisecond); 255 256 t = Time::UnixEpoch() + TimeDelta::FromMicroseconds(1000); 257 t.UTCExplode(&exploded); 258 EXPECT_TRUE(exploded.HasValidValues()); 259 // Should be 1970-01-01 00:00:00 1 millisecond. 260 EXPECT_EQ(kUnixEpochYear, exploded.year); 261 EXPECT_EQ(1, exploded.month); 262 EXPECT_EQ(1, exploded.day_of_month); 263 EXPECT_EQ(0, exploded.hour); 264 EXPECT_EQ(0, exploded.minute); 265 EXPECT_EQ(0, exploded.second); 266 EXPECT_EQ(1, exploded.millisecond); 267 268 t = Time::UnixEpoch() + TimeDelta::FromMilliseconds(1000); 269 t.UTCExplode(&exploded); 270 EXPECT_TRUE(exploded.HasValidValues()); 271 // Should be 1970-01-01 00:00:01. 272 EXPECT_EQ(kUnixEpochYear, exploded.year); 273 EXPECT_EQ(1, exploded.month); 274 EXPECT_EQ(1, exploded.day_of_month); 275 EXPECT_EQ(0, exploded.hour); 276 EXPECT_EQ(0, exploded.minute); 277 EXPECT_EQ(1, exploded.second); 278 EXPECT_EQ(0, exploded.millisecond); 279 280 t = Time::UnixEpoch() + TimeDelta::FromMilliseconds(1001); 281 t.UTCExplode(&exploded); 282 EXPECT_TRUE(exploded.HasValidValues()); 283 // Should be 1970-01-01 00:00:01 1 millisecond. 284 EXPECT_EQ(kUnixEpochYear, exploded.year); 285 EXPECT_EQ(1, exploded.month); 286 EXPECT_EQ(1, exploded.day_of_month); 287 EXPECT_EQ(0, exploded.hour); 288 EXPECT_EQ(0, exploded.minute); 289 EXPECT_EQ(1, exploded.second); 290 EXPECT_EQ(1, exploded.millisecond); 291} 292 293TEST_F(TimeTest, Max) { 294 Time max = Time::Max(); 295 EXPECT_TRUE(max.is_max()); 296 EXPECT_EQ(max, Time::Max()); 297 EXPECT_GT(max, Time::Now()); 298 EXPECT_GT(max, Time()); 299} 300 301TEST_F(TimeTest, MaxConversions) { 302 Time t = Time::Max(); 303 EXPECT_EQ(std::numeric_limits<int64_t>::max(), t.ToInternalValue()); 304 305 t = Time::FromDoubleT(std::numeric_limits<double>::infinity()); 306 EXPECT_TRUE(t.is_max()); 307 EXPECT_EQ(std::numeric_limits<double>::infinity(), t.ToDoubleT()); 308 309 t = Time::FromJsTime(std::numeric_limits<double>::infinity()); 310 EXPECT_TRUE(t.is_max()); 311 EXPECT_EQ(std::numeric_limits<double>::infinity(), t.ToJsTime()); 312 313 t = Time::FromTimeT(std::numeric_limits<time_t>::max()); 314 EXPECT_TRUE(t.is_max()); 315 EXPECT_EQ(std::numeric_limits<time_t>::max(), t.ToTimeT()); 316 317#if defined(OS_POSIX) 318 struct timeval tval; 319 tval.tv_sec = std::numeric_limits<time_t>::max(); 320 tval.tv_usec = static_cast<suseconds_t>(Time::kMicrosecondsPerSecond) - 1; 321 t = Time::FromTimeVal(tval); 322 EXPECT_TRUE(t.is_max()); 323 tval = t.ToTimeVal(); 324 EXPECT_EQ(std::numeric_limits<time_t>::max(), tval.tv_sec); 325 EXPECT_EQ(static_cast<suseconds_t>(Time::kMicrosecondsPerSecond) - 1, 326 tval.tv_usec); 327#endif 328 329#if defined(OS_MACOSX) 330 t = Time::FromCFAbsoluteTime(std::numeric_limits<CFAbsoluteTime>::infinity()); 331 EXPECT_TRUE(t.is_max()); 332 EXPECT_EQ(std::numeric_limits<CFAbsoluteTime>::infinity(), 333 t.ToCFAbsoluteTime()); 334#endif 335 336#if defined(OS_WIN) 337 FILETIME ftime; 338 ftime.dwHighDateTime = std::numeric_limits<DWORD>::max(); 339 ftime.dwLowDateTime = std::numeric_limits<DWORD>::max(); 340 t = Time::FromFileTime(ftime); 341 EXPECT_TRUE(t.is_max()); 342 ftime = t.ToFileTime(); 343 EXPECT_EQ(std::numeric_limits<DWORD>::max(), ftime.dwHighDateTime); 344 EXPECT_EQ(std::numeric_limits<DWORD>::max(), ftime.dwLowDateTime); 345#endif 346} 347 348#if defined(OS_MACOSX) 349TEST_F(TimeTest, TimeTOverflow) { 350 Time t = Time::FromInternalValue(std::numeric_limits<int64_t>::max() - 1); 351 EXPECT_FALSE(t.is_max()); 352 EXPECT_EQ(std::numeric_limits<time_t>::max(), t.ToTimeT()); 353} 354#endif 355 356#if defined(OS_ANDROID) 357TEST_F(TimeTest, FromLocalExplodedCrashOnAndroid) { 358 // This crashed inside Time:: FromLocalExploded() on Android 4.1.2. 359 // See http://crbug.com/287821 360 Time::Exploded midnight = {2013, // year 361 10, // month 362 0, // day_of_week 363 13, // day_of_month 364 0, // hour 365 0, // minute 366 0, // second 367 }; 368 // The string passed to putenv() must be a char* and the documentation states 369 // that it 'becomes part of the environment', so use a static buffer. 370 static char buffer[] = "TZ=America/Santiago"; 371 putenv(buffer); 372 tzset(); 373 Time t = Time::FromLocalExploded(midnight); 374 EXPECT_EQ(1381633200, t.ToTimeT()); 375} 376#endif // OS_ANDROID 377 378static void HighResClockTest(TimeTicks (*GetTicks)()) { 379 // IsHighResolution() is false on some systems. Since the product still works 380 // even if it's false, it makes this entire test questionable. 381 if (!TimeTicks::IsHighResolution()) 382 return; 383 384 // Why do we loop here? 385 // We're trying to measure that intervals increment in a VERY small amount 386 // of time -- less than 15ms. Unfortunately, if we happen to have a 387 // context switch in the middle of our test, the context switch could easily 388 // exceed our limit. So, we iterate on this several times. As long as we're 389 // able to detect the fine-granularity timers at least once, then the test 390 // has succeeded. 391 392 const int kTargetGranularityUs = 15000; // 15ms 393 394 bool success = false; 395 int retries = 100; // Arbitrary. 396 TimeDelta delta; 397 while (!success && retries--) { 398 TimeTicks ticks_start = GetTicks(); 399 // Loop until we can detect that the clock has changed. Non-HighRes timers 400 // will increment in chunks, e.g. 15ms. By spinning until we see a clock 401 // change, we detect the minimum time between measurements. 402 do { 403 delta = GetTicks() - ticks_start; 404 } while (delta.InMilliseconds() == 0); 405 406 if (delta.InMicroseconds() <= kTargetGranularityUs) 407 success = true; 408 } 409 410 // In high resolution mode, we expect to see the clock increment 411 // in intervals less than 15ms. 412 EXPECT_TRUE(success); 413} 414 415TEST(TimeTicks, HighRes) { 416 HighResClockTest(&TimeTicks::Now); 417} 418 419TEST(TimeTicks, SnappedToNextTickBasic) { 420 base::TimeTicks phase = base::TimeTicks::FromInternalValue(4000); 421 base::TimeDelta interval = base::TimeDelta::FromMicroseconds(1000); 422 base::TimeTicks timestamp; 423 424 // Timestamp in previous interval. 425 timestamp = base::TimeTicks::FromInternalValue(3500); 426 EXPECT_EQ(4000, 427 timestamp.SnappedToNextTick(phase, interval).ToInternalValue()); 428 429 // Timestamp in next interval. 430 timestamp = base::TimeTicks::FromInternalValue(4500); 431 EXPECT_EQ(5000, 432 timestamp.SnappedToNextTick(phase, interval).ToInternalValue()); 433 434 // Timestamp multiple intervals before. 435 timestamp = base::TimeTicks::FromInternalValue(2500); 436 EXPECT_EQ(3000, 437 timestamp.SnappedToNextTick(phase, interval).ToInternalValue()); 438 439 // Timestamp multiple intervals after. 440 timestamp = base::TimeTicks::FromInternalValue(6500); 441 EXPECT_EQ(7000, 442 timestamp.SnappedToNextTick(phase, interval).ToInternalValue()); 443 444 // Timestamp on previous interval. 445 timestamp = base::TimeTicks::FromInternalValue(3000); 446 EXPECT_EQ(3000, 447 timestamp.SnappedToNextTick(phase, interval).ToInternalValue()); 448 449 // Timestamp on next interval. 450 timestamp = base::TimeTicks::FromInternalValue(5000); 451 EXPECT_EQ(5000, 452 timestamp.SnappedToNextTick(phase, interval).ToInternalValue()); 453 454 // Timestamp equal to phase. 455 timestamp = base::TimeTicks::FromInternalValue(4000); 456 EXPECT_EQ(4000, 457 timestamp.SnappedToNextTick(phase, interval).ToInternalValue()); 458} 459 460TEST(TimeTicks, SnappedToNextTickOverflow) { 461 // int(big_timestamp / interval) < 0, so this causes a crash if the number of 462 // intervals elapsed is attempted to be stored in an int. 463 base::TimeTicks phase = base::TimeTicks::FromInternalValue(0); 464 base::TimeDelta interval = base::TimeDelta::FromMicroseconds(4000); 465 base::TimeTicks big_timestamp = 466 base::TimeTicks::FromInternalValue(8635916564000); 467 468 EXPECT_EQ(8635916564000, 469 big_timestamp.SnappedToNextTick(phase, interval).ToInternalValue()); 470 EXPECT_EQ(8635916564000, 471 big_timestamp.SnappedToNextTick(big_timestamp, interval) 472 .ToInternalValue()); 473} 474 475TEST(TimeDelta, FromAndIn) { 476 EXPECT_TRUE(TimeDelta::FromDays(2) == TimeDelta::FromHours(48)); 477 EXPECT_TRUE(TimeDelta::FromHours(3) == TimeDelta::FromMinutes(180)); 478 EXPECT_TRUE(TimeDelta::FromMinutes(2) == TimeDelta::FromSeconds(120)); 479 EXPECT_TRUE(TimeDelta::FromSeconds(2) == TimeDelta::FromMilliseconds(2000)); 480 EXPECT_TRUE(TimeDelta::FromMilliseconds(2) == 481 TimeDelta::FromMicroseconds(2000)); 482 EXPECT_TRUE(TimeDelta::FromSecondsD(2.3) == 483 TimeDelta::FromMilliseconds(2300)); 484 EXPECT_TRUE(TimeDelta::FromMillisecondsD(2.5) == 485 TimeDelta::FromMicroseconds(2500)); 486 EXPECT_EQ(13, TimeDelta::FromDays(13).InDays()); 487 EXPECT_EQ(13, TimeDelta::FromHours(13).InHours()); 488 EXPECT_EQ(13, TimeDelta::FromMinutes(13).InMinutes()); 489 EXPECT_EQ(13, TimeDelta::FromSeconds(13).InSeconds()); 490 EXPECT_EQ(13.0, TimeDelta::FromSeconds(13).InSecondsF()); 491 EXPECT_EQ(13, TimeDelta::FromMilliseconds(13).InMilliseconds()); 492 EXPECT_EQ(13.0, TimeDelta::FromMilliseconds(13).InMillisecondsF()); 493 EXPECT_EQ(13, TimeDelta::FromSecondsD(13.1).InSeconds()); 494 EXPECT_EQ(13.1, TimeDelta::FromSecondsD(13.1).InSecondsF()); 495 EXPECT_EQ(13, TimeDelta::FromMillisecondsD(13.3).InMilliseconds()); 496 EXPECT_EQ(13.3, TimeDelta::FromMillisecondsD(13.3).InMillisecondsF()); 497 EXPECT_EQ(13, TimeDelta::FromMicroseconds(13).InMicroseconds()); 498 EXPECT_EQ(3.456, TimeDelta::FromMillisecondsD(3.45678).InMillisecondsF()); 499} 500 501#if defined(OS_POSIX) 502TEST(TimeDelta, TimeSpecConversion) { 503 struct timespec result = TimeDelta::FromSeconds(0).ToTimeSpec(); 504 EXPECT_EQ(result.tv_sec, 0); 505 EXPECT_EQ(result.tv_nsec, 0); 506 507 result = TimeDelta::FromSeconds(1).ToTimeSpec(); 508 EXPECT_EQ(result.tv_sec, 1); 509 EXPECT_EQ(result.tv_nsec, 0); 510 511 result = TimeDelta::FromMicroseconds(1).ToTimeSpec(); 512 EXPECT_EQ(result.tv_sec, 0); 513 EXPECT_EQ(result.tv_nsec, 1000); 514 515 result = TimeDelta::FromMicroseconds( 516 Time::kMicrosecondsPerSecond + 1).ToTimeSpec(); 517 EXPECT_EQ(result.tv_sec, 1); 518 EXPECT_EQ(result.tv_nsec, 1000); 519} 520#endif // OS_POSIX 521 522// Our internal time format is serialized in things like databases, so it's 523// important that it's consistent across all our platforms. We use the 1601 524// Windows epoch as the internal format across all platforms. 525TEST(TimeDelta, WindowsEpoch) { 526 Time::Exploded exploded; 527 exploded.year = 1970; 528 exploded.month = 1; 529 exploded.day_of_week = 0; // Should be unusued. 530 exploded.day_of_month = 1; 531 exploded.hour = 0; 532 exploded.minute = 0; 533 exploded.second = 0; 534 exploded.millisecond = 0; 535 Time t = Time::FromUTCExploded(exploded); 536 // Unix 1970 epoch. 537 EXPECT_EQ(11644473600000000ll, t.ToInternalValue()); 538 539 // We can't test 1601 epoch, since the system time functions on Linux 540 // only compute years starting from 1900. 541} 542 543// We could define this separately for Time, TimeTicks and TimeDelta but the 544// definitions would be identical anyway. 545template <class Any> 546std::string AnyToString(Any any) { 547 std::ostringstream oss; 548 oss << any; 549 return oss.str(); 550} 551 552TEST(TimeDelta, Magnitude) { 553 const int64_t zero = 0; 554 EXPECT_EQ(TimeDelta::FromMicroseconds(zero), 555 TimeDelta::FromMicroseconds(zero).magnitude()); 556 557 const int64_t one = 1; 558 const int64_t negative_one = -1; 559 EXPECT_EQ(TimeDelta::FromMicroseconds(one), 560 TimeDelta::FromMicroseconds(one).magnitude()); 561 EXPECT_EQ(TimeDelta::FromMicroseconds(one), 562 TimeDelta::FromMicroseconds(negative_one).magnitude()); 563 564 const int64_t max_int64_minus_one = std::numeric_limits<int64_t>::max() - 1; 565 const int64_t min_int64_plus_two = std::numeric_limits<int64_t>::min() + 2; 566 EXPECT_EQ(TimeDelta::FromMicroseconds(max_int64_minus_one), 567 TimeDelta::FromMicroseconds(max_int64_minus_one).magnitude()); 568 EXPECT_EQ(TimeDelta::FromMicroseconds(max_int64_minus_one), 569 TimeDelta::FromMicroseconds(min_int64_plus_two).magnitude()); 570} 571 572TEST(TimeDelta, Max) { 573 TimeDelta max = TimeDelta::Max(); 574 EXPECT_TRUE(max.is_max()); 575 EXPECT_EQ(max, TimeDelta::Max()); 576 EXPECT_GT(max, TimeDelta::FromDays(100 * 365)); 577 EXPECT_GT(max, TimeDelta()); 578} 579 580bool IsMin(TimeDelta delta) { 581 return (-delta).is_max(); 582} 583 584TEST(TimeDelta, MaxConversions) { 585 TimeDelta t = TimeDelta::Max(); 586 EXPECT_EQ(std::numeric_limits<int64_t>::max(), t.ToInternalValue()); 587 588 EXPECT_EQ(std::numeric_limits<int>::max(), t.InDays()); 589 EXPECT_EQ(std::numeric_limits<int>::max(), t.InHours()); 590 EXPECT_EQ(std::numeric_limits<int>::max(), t.InMinutes()); 591 EXPECT_EQ(std::numeric_limits<double>::infinity(), t.InSecondsF()); 592 EXPECT_EQ(std::numeric_limits<int64_t>::max(), t.InSeconds()); 593 EXPECT_EQ(std::numeric_limits<double>::infinity(), t.InMillisecondsF()); 594 EXPECT_EQ(std::numeric_limits<int64_t>::max(), t.InMilliseconds()); 595 EXPECT_EQ(std::numeric_limits<int64_t>::max(), t.InMillisecondsRoundedUp()); 596 597 t = TimeDelta::FromDays(std::numeric_limits<int>::max()); 598 EXPECT_TRUE(t.is_max()); 599 600 t = TimeDelta::FromHours(std::numeric_limits<int>::max()); 601 EXPECT_TRUE(t.is_max()); 602 603 t = TimeDelta::FromMinutes(std::numeric_limits<int>::max()); 604 EXPECT_TRUE(t.is_max()); 605 606 int64_t max_int = std::numeric_limits<int64_t>::max(); 607 608 t = TimeDelta::FromSeconds(max_int / Time::kMicrosecondsPerSecond + 1); 609 EXPECT_TRUE(t.is_max()); 610 611 t = TimeDelta::FromMilliseconds(max_int / Time::kMillisecondsPerSecond + 1); 612 EXPECT_TRUE(t.is_max()); 613 614 t = TimeDelta::FromMicroseconds(max_int); 615 EXPECT_TRUE(t.is_max()); 616 617 t = TimeDelta::FromSeconds(-max_int / Time::kMicrosecondsPerSecond - 1); 618 EXPECT_TRUE(IsMin(t)); 619 620 t = TimeDelta::FromMilliseconds(-max_int / Time::kMillisecondsPerSecond - 1); 621 EXPECT_TRUE(IsMin(t)); 622 623 t = TimeDelta::FromMicroseconds(-max_int); 624 EXPECT_TRUE(IsMin(t)); 625 626 t = -TimeDelta::FromMicroseconds(std::numeric_limits<int64_t>::min()); 627 EXPECT_FALSE(IsMin(t)); 628 629 t = TimeDelta::FromSecondsD(std::numeric_limits<double>::infinity()); 630 EXPECT_TRUE(t.is_max()); 631 632 double max_d = max_int; 633 634 t = TimeDelta::FromSecondsD(max_d / Time::kMicrosecondsPerSecond + 1); 635 EXPECT_TRUE(t.is_max()); 636 637 t = TimeDelta::FromMillisecondsD(std::numeric_limits<double>::infinity()); 638 EXPECT_TRUE(t.is_max()); 639 640 t = TimeDelta::FromMillisecondsD(max_d / Time::kMillisecondsPerSecond * 2); 641 EXPECT_TRUE(t.is_max()); 642 643 t = TimeDelta::FromSecondsD(-max_d / Time::kMicrosecondsPerSecond - 1); 644 EXPECT_TRUE(IsMin(t)); 645 646 t = TimeDelta::FromMillisecondsD(-max_d / Time::kMillisecondsPerSecond * 2); 647 EXPECT_TRUE(IsMin(t)); 648} 649 650TEST(TimeDelta, NumericOperators) { 651 double d = 0.5; 652 EXPECT_EQ(TimeDelta::FromMilliseconds(500), 653 TimeDelta::FromMilliseconds(1000) * d); 654 EXPECT_EQ(TimeDelta::FromMilliseconds(2000), 655 TimeDelta::FromMilliseconds(1000) / d); 656 EXPECT_EQ(TimeDelta::FromMilliseconds(500), 657 TimeDelta::FromMilliseconds(1000) *= d); 658 EXPECT_EQ(TimeDelta::FromMilliseconds(2000), 659 TimeDelta::FromMilliseconds(1000) /= d); 660 EXPECT_EQ(TimeDelta::FromMilliseconds(500), 661 d * TimeDelta::FromMilliseconds(1000)); 662 663 float f = 0.5; 664 EXPECT_EQ(TimeDelta::FromMilliseconds(500), 665 TimeDelta::FromMilliseconds(1000) * f); 666 EXPECT_EQ(TimeDelta::FromMilliseconds(2000), 667 TimeDelta::FromMilliseconds(1000) / f); 668 EXPECT_EQ(TimeDelta::FromMilliseconds(500), 669 TimeDelta::FromMilliseconds(1000) *= f); 670 EXPECT_EQ(TimeDelta::FromMilliseconds(2000), 671 TimeDelta::FromMilliseconds(1000) /= f); 672 EXPECT_EQ(TimeDelta::FromMilliseconds(500), 673 f * TimeDelta::FromMilliseconds(1000)); 674 675 int i = 2; 676 EXPECT_EQ(TimeDelta::FromMilliseconds(2000), 677 TimeDelta::FromMilliseconds(1000) * i); 678 EXPECT_EQ(TimeDelta::FromMilliseconds(500), 679 TimeDelta::FromMilliseconds(1000) / i); 680 EXPECT_EQ(TimeDelta::FromMilliseconds(2000), 681 TimeDelta::FromMilliseconds(1000) *= i); 682 EXPECT_EQ(TimeDelta::FromMilliseconds(500), 683 TimeDelta::FromMilliseconds(1000) /= i); 684 EXPECT_EQ(TimeDelta::FromMilliseconds(2000), 685 i * TimeDelta::FromMilliseconds(1000)); 686 687 int64_t i64 = 2; 688 EXPECT_EQ(TimeDelta::FromMilliseconds(2000), 689 TimeDelta::FromMilliseconds(1000) * i64); 690 EXPECT_EQ(TimeDelta::FromMilliseconds(500), 691 TimeDelta::FromMilliseconds(1000) / i64); 692 EXPECT_EQ(TimeDelta::FromMilliseconds(2000), 693 TimeDelta::FromMilliseconds(1000) *= i64); 694 EXPECT_EQ(TimeDelta::FromMilliseconds(500), 695 TimeDelta::FromMilliseconds(1000) /= i64); 696 EXPECT_EQ(TimeDelta::FromMilliseconds(2000), 697 i64 * TimeDelta::FromMilliseconds(1000)); 698 699 EXPECT_EQ(TimeDelta::FromMilliseconds(500), 700 TimeDelta::FromMilliseconds(1000) * 0.5); 701 EXPECT_EQ(TimeDelta::FromMilliseconds(2000), 702 TimeDelta::FromMilliseconds(1000) / 0.5); 703 EXPECT_EQ(TimeDelta::FromMilliseconds(500), 704 TimeDelta::FromMilliseconds(1000) *= 0.5); 705 EXPECT_EQ(TimeDelta::FromMilliseconds(2000), 706 TimeDelta::FromMilliseconds(1000) /= 0.5); 707 EXPECT_EQ(TimeDelta::FromMilliseconds(500), 708 0.5 * TimeDelta::FromMilliseconds(1000)); 709 710 EXPECT_EQ(TimeDelta::FromMilliseconds(2000), 711 TimeDelta::FromMilliseconds(1000) * 2); 712 EXPECT_EQ(TimeDelta::FromMilliseconds(500), 713 TimeDelta::FromMilliseconds(1000) / 2); 714 EXPECT_EQ(TimeDelta::FromMilliseconds(2000), 715 TimeDelta::FromMilliseconds(1000) *= 2); 716 EXPECT_EQ(TimeDelta::FromMilliseconds(500), 717 TimeDelta::FromMilliseconds(1000) /= 2); 718 EXPECT_EQ(TimeDelta::FromMilliseconds(2000), 719 2 * TimeDelta::FromMilliseconds(1000)); 720} 721 722TEST(TimeDelta, Overflows) { 723 // Some sanity checks. 724 EXPECT_TRUE(TimeDelta::Max().is_max()); 725 EXPECT_TRUE(IsMin(-TimeDelta::Max())); 726 EXPECT_GT(TimeDelta(), -TimeDelta::Max()); 727 728 TimeDelta large_delta = TimeDelta::Max() - TimeDelta::FromMilliseconds(1); 729 TimeDelta large_negative = -large_delta; 730 EXPECT_GT(TimeDelta(), large_negative); 731 EXPECT_FALSE(large_delta.is_max()); 732 EXPECT_FALSE(IsMin(-large_negative)); 733 TimeDelta one_second = TimeDelta::FromSeconds(1); 734 735 // Test +, -, * and / operators. 736 EXPECT_TRUE((large_delta + one_second).is_max()); 737 EXPECT_TRUE(IsMin(large_negative + (-one_second))); 738 EXPECT_TRUE(IsMin(large_negative - one_second)); 739 EXPECT_TRUE((large_delta - (-one_second)).is_max()); 740 EXPECT_TRUE((large_delta * 2).is_max()); 741 EXPECT_TRUE(IsMin(large_delta * -2)); 742 EXPECT_TRUE((large_delta / 0.5).is_max()); 743 EXPECT_TRUE(IsMin(large_delta / -0.5)); 744 745 // Test +=, -=, *= and /= operators. 746 TimeDelta delta = large_delta; 747 delta += one_second; 748 EXPECT_TRUE(delta.is_max()); 749 delta = large_negative; 750 delta += -one_second; 751 EXPECT_TRUE(IsMin(delta)); 752 753 delta = large_negative; 754 delta -= one_second; 755 EXPECT_TRUE(IsMin(delta)); 756 delta = large_delta; 757 delta -= -one_second; 758 EXPECT_TRUE(delta.is_max()); 759 760 delta = large_delta; 761 delta *= 2; 762 EXPECT_TRUE(delta.is_max()); 763 delta = large_negative; 764 delta *= 1.5; 765 EXPECT_TRUE(IsMin(delta)); 766 767 delta = large_delta; 768 delta /= 0.5; 769 EXPECT_TRUE(delta.is_max()); 770 delta = large_negative; 771 delta /= 0.5; 772 EXPECT_TRUE(IsMin(delta)); 773 774 // Test operations with Time and TimeTicks. 775 EXPECT_TRUE((large_delta + Time::Now()).is_max()); 776 EXPECT_TRUE((large_delta + TimeTicks::Now()).is_max()); 777 EXPECT_TRUE((Time::Now() + large_delta).is_max()); 778 EXPECT_TRUE((TimeTicks::Now() + large_delta).is_max()); 779 780 Time time_now = Time::Now(); 781 EXPECT_EQ(one_second, (time_now + one_second) - time_now); 782 EXPECT_EQ(-one_second, (time_now - one_second) - time_now); 783 784 TimeTicks ticks_now = TimeTicks::Now(); 785 EXPECT_EQ(-one_second, (ticks_now - one_second) - ticks_now); 786 EXPECT_EQ(one_second, (ticks_now + one_second) - ticks_now); 787} 788 789TEST(TimeDeltaLogging, DCheckEqCompiles) { 790 DCHECK_EQ(TimeDelta(), TimeDelta()); 791} 792 793TEST(TimeDeltaLogging, EmptyIsZero) { 794 TimeDelta zero; 795 EXPECT_EQ("0s", AnyToString(zero)); 796} 797 798TEST(TimeDeltaLogging, FiveHundredMs) { 799 TimeDelta five_hundred_ms = TimeDelta::FromMilliseconds(500); 800 EXPECT_EQ("0.5s", AnyToString(five_hundred_ms)); 801} 802 803TEST(TimeDeltaLogging, MinusTenSeconds) { 804 TimeDelta minus_ten_seconds = TimeDelta::FromSeconds(-10); 805 EXPECT_EQ("-10s", AnyToString(minus_ten_seconds)); 806} 807 808TEST(TimeDeltaLogging, DoesNotMessUpFormattingFlags) { 809 std::ostringstream oss; 810 std::ios_base::fmtflags flags_before = oss.flags(); 811 oss << TimeDelta(); 812 EXPECT_EQ(flags_before, oss.flags()); 813} 814 815TEST(TimeDeltaLogging, DoesNotMakeStreamBad) { 816 std::ostringstream oss; 817 oss << TimeDelta(); 818 EXPECT_TRUE(oss.good()); 819} 820 821TEST(TimeLogging, DCheckEqCompiles) { 822 DCHECK_EQ(Time(), Time()); 823} 824 825TEST(TimeLogging, DoesNotMessUpFormattingFlags) { 826 std::ostringstream oss; 827 std::ios_base::fmtflags flags_before = oss.flags(); 828 oss << Time(); 829 EXPECT_EQ(flags_before, oss.flags()); 830} 831 832TEST(TimeLogging, DoesNotMakeStreamBad) { 833 std::ostringstream oss; 834 oss << Time(); 835 EXPECT_TRUE(oss.good()); 836} 837 838TEST(TimeTicksLogging, DCheckEqCompiles) { 839 DCHECK_EQ(TimeTicks(), TimeTicks()); 840} 841 842TEST(TimeTicksLogging, ZeroTime) { 843 TimeTicks zero; 844 EXPECT_EQ("0 bogo-microseconds", AnyToString(zero)); 845} 846 847TEST(TimeTicksLogging, FortyYearsLater) { 848 TimeTicks forty_years_later = 849 TimeTicks() + TimeDelta::FromDays(365.25 * 40); 850 EXPECT_EQ("1262304000000000 bogo-microseconds", 851 AnyToString(forty_years_later)); 852} 853 854TEST(TimeTicksLogging, DoesNotMessUpFormattingFlags) { 855 std::ostringstream oss; 856 std::ios_base::fmtflags flags_before = oss.flags(); 857 oss << TimeTicks(); 858 EXPECT_EQ(flags_before, oss.flags()); 859} 860 861TEST(TimeTicksLogging, DoesNotMakeStreamBad) { 862 std::ostringstream oss; 863 oss << TimeTicks(); 864 EXPECT_TRUE(oss.good()); 865} 866 867} // namespace 868 869} // namespace base 870