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 "base/compiler_specific.h" 13 #include "base/logging.h" 14 #include "base/macros.h" 15 #include "base/strings/stringprintf.h" 16 #include "base/threading/platform_thread.h" 17 #include "build/build_config.h" 18 #include "testing/gtest/include/gtest/gtest.h" 19 20 namespace base { 21 22 namespace { 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 27 class 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. 59 TEST_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. 98 TEST_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) 108 TEST_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 115 TEST_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 128 TEST_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 135 TEST_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 148 TEST_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 157 TEST_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 166 TEST_F(TimeTest, ParseTimeTest1) { 167 time_t current_time = 0; 168 time(¤t_time); 169 170 const int BUFFER_SIZE = 64; 171 struct tm local_time = {0}; 172 char time_buf[BUFFER_SIZE] = {0}; 173 #if defined(OS_WIN) 174 localtime_s(&local_time, ¤t_time); 175 asctime_s(time_buf, arraysize(time_buf), &local_time); 176 #elif defined(OS_POSIX) 177 localtime_r(¤t_time, &local_time); 178 asctime_r(&local_time, time_buf); 179 #endif 180 181 Time parsed_time; 182 EXPECT_TRUE(Time::FromString(time_buf, &parsed_time)); 183 EXPECT_EQ(current_time, parsed_time.ToTimeT()); 184 } 185 186 TEST_F(TimeTest, DayOfWeekSunday) { 187 Time time; 188 EXPECT_TRUE(Time::FromString("Sun, 06 May 2012 12:00:00 GMT", &time)); 189 Time::Exploded exploded; 190 time.UTCExplode(&exploded); 191 EXPECT_EQ(0, exploded.day_of_week); 192 } 193 194 TEST_F(TimeTest, DayOfWeekWednesday) { 195 Time time; 196 EXPECT_TRUE(Time::FromString("Wed, 09 May 2012 12:00:00 GMT", &time)); 197 Time::Exploded exploded; 198 time.UTCExplode(&exploded); 199 EXPECT_EQ(3, exploded.day_of_week); 200 } 201 202 TEST_F(TimeTest, DayOfWeekSaturday) { 203 Time time; 204 EXPECT_TRUE(Time::FromString("Sat, 12 May 2012 12:00:00 GMT", &time)); 205 Time::Exploded exploded; 206 time.UTCExplode(&exploded); 207 EXPECT_EQ(6, exploded.day_of_week); 208 } 209 210 TEST_F(TimeTest, ParseTimeTest2) { 211 Time parsed_time; 212 EXPECT_TRUE(Time::FromString("Mon, 15 Oct 2007 19:45:00 GMT", &parsed_time)); 213 EXPECT_EQ(comparison_time_pdt_, parsed_time); 214 } 215 216 TEST_F(TimeTest, ParseTimeTest3) { 217 Time parsed_time; 218 EXPECT_TRUE(Time::FromString("15 Oct 07 12:45:00", &parsed_time)); 219 EXPECT_EQ(comparison_time_local_, parsed_time); 220 } 221 222 TEST_F(TimeTest, ParseTimeTest4) { 223 Time parsed_time; 224 EXPECT_TRUE(Time::FromString("15 Oct 07 19:45 GMT", &parsed_time)); 225 EXPECT_EQ(comparison_time_pdt_, parsed_time); 226 } 227 228 TEST_F(TimeTest, ParseTimeTest5) { 229 Time parsed_time; 230 EXPECT_TRUE(Time::FromString("Mon Oct 15 12:45 PDT 2007", &parsed_time)); 231 EXPECT_EQ(comparison_time_pdt_, parsed_time); 232 } 233 234 TEST_F(TimeTest, ParseTimeTest6) { 235 Time parsed_time; 236 EXPECT_TRUE(Time::FromString("Monday, Oct 15, 2007 12:45 PM", &parsed_time)); 237 EXPECT_EQ(comparison_time_local_, parsed_time); 238 } 239 240 TEST_F(TimeTest, ParseTimeTest7) { 241 Time parsed_time; 242 EXPECT_TRUE(Time::FromString("10/15/07 12:45:00 PM", &parsed_time)); 243 EXPECT_EQ(comparison_time_local_, parsed_time); 244 } 245 246 TEST_F(TimeTest, ParseTimeTest8) { 247 Time parsed_time; 248 EXPECT_TRUE(Time::FromString("15-OCT-2007 12:45pm", &parsed_time)); 249 EXPECT_EQ(comparison_time_local_, parsed_time); 250 } 251 252 TEST_F(TimeTest, ParseTimeTest9) { 253 Time parsed_time; 254 EXPECT_TRUE(Time::FromString("16 Oct 2007 4:45-JST (Tuesday)", &parsed_time)); 255 EXPECT_EQ(comparison_time_pdt_, parsed_time); 256 } 257 258 TEST_F(TimeTest, ParseTimeTest10) { 259 Time parsed_time; 260 EXPECT_TRUE(Time::FromString("15/10/07 12:45", &parsed_time)); 261 EXPECT_EQ(parsed_time, comparison_time_local_); 262 } 263 264 // Test some of edge cases around epoch, etc. 265 TEST_F(TimeTest, ParseTimeTestEpoch0) { 266 Time parsed_time; 267 268 // time_t == epoch == 0 269 EXPECT_TRUE(Time::FromString("Thu Jan 01 01:00:00 +0100 1970", 270 &parsed_time)); 271 EXPECT_EQ(0, parsed_time.ToTimeT()); 272 EXPECT_TRUE(Time::FromString("Thu Jan 01 00:00:00 GMT 1970", 273 &parsed_time)); 274 EXPECT_EQ(0, parsed_time.ToTimeT()); 275 } 276 277 TEST_F(TimeTest, ParseTimeTestEpoch1) { 278 Time parsed_time; 279 280 // time_t == 1 second after epoch == 1 281 EXPECT_TRUE(Time::FromString("Thu Jan 01 01:00:01 +0100 1970", 282 &parsed_time)); 283 EXPECT_EQ(1, parsed_time.ToTimeT()); 284 EXPECT_TRUE(Time::FromString("Thu Jan 01 00:00:01 GMT 1970", 285 &parsed_time)); 286 EXPECT_EQ(1, parsed_time.ToTimeT()); 287 } 288 289 TEST_F(TimeTest, ParseTimeTestEpoch2) { 290 Time parsed_time; 291 292 // time_t == 2 seconds after epoch == 2 293 EXPECT_TRUE(Time::FromString("Thu Jan 01 01:00:02 +0100 1970", 294 &parsed_time)); 295 EXPECT_EQ(2, parsed_time.ToTimeT()); 296 EXPECT_TRUE(Time::FromString("Thu Jan 01 00:00:02 GMT 1970", 297 &parsed_time)); 298 EXPECT_EQ(2, parsed_time.ToTimeT()); 299 } 300 301 TEST_F(TimeTest, ParseTimeTestEpochNeg1) { 302 Time parsed_time; 303 304 // time_t == 1 second before epoch == -1 305 EXPECT_TRUE(Time::FromString("Thu Jan 01 00:59:59 +0100 1970", 306 &parsed_time)); 307 EXPECT_EQ(-1, parsed_time.ToTimeT()); 308 EXPECT_TRUE(Time::FromString("Wed Dec 31 23:59:59 GMT 1969", 309 &parsed_time)); 310 EXPECT_EQ(-1, parsed_time.ToTimeT()); 311 } 312 313 // If time_t is 32 bits, a date after year 2038 will overflow time_t and 314 // cause timegm() to return -1. The parsed time should not be 1 second 315 // before epoch. 316 TEST_F(TimeTest, ParseTimeTestEpochNotNeg1) { 317 Time parsed_time; 318 319 EXPECT_TRUE(Time::FromString("Wed Dec 31 23:59:59 GMT 2100", 320 &parsed_time)); 321 EXPECT_NE(-1, parsed_time.ToTimeT()); 322 } 323 324 TEST_F(TimeTest, ParseTimeTestEpochNeg2) { 325 Time parsed_time; 326 327 // time_t == 2 seconds before epoch == -2 328 EXPECT_TRUE(Time::FromString("Thu Jan 01 00:59:58 +0100 1970", 329 &parsed_time)); 330 EXPECT_EQ(-2, parsed_time.ToTimeT()); 331 EXPECT_TRUE(Time::FromString("Wed Dec 31 23:59:58 GMT 1969", 332 &parsed_time)); 333 EXPECT_EQ(-2, parsed_time.ToTimeT()); 334 } 335 336 TEST_F(TimeTest, ParseTimeTestEpoch1960) { 337 Time parsed_time; 338 339 // time_t before Epoch, in 1960 340 EXPECT_TRUE(Time::FromString("Wed Jun 29 19:40:01 +0100 1960", 341 &parsed_time)); 342 EXPECT_EQ(-299999999, parsed_time.ToTimeT()); 343 EXPECT_TRUE(Time::FromString("Wed Jun 29 18:40:01 GMT 1960", 344 &parsed_time)); 345 EXPECT_EQ(-299999999, parsed_time.ToTimeT()); 346 EXPECT_TRUE(Time::FromString("Wed Jun 29 17:40:01 GMT 1960", 347 &parsed_time)); 348 EXPECT_EQ(-300003599, parsed_time.ToTimeT()); 349 } 350 351 TEST_F(TimeTest, ParseTimeTestEmpty) { 352 Time parsed_time; 353 EXPECT_FALSE(Time::FromString("", &parsed_time)); 354 } 355 356 TEST_F(TimeTest, ParseTimeTestInvalidString) { 357 Time parsed_time; 358 EXPECT_FALSE(Time::FromString("Monday morning 2000", &parsed_time)); 359 } 360 361 TEST_F(TimeTest, ExplodeBeforeUnixEpoch) { 362 static const int kUnixEpochYear = 1970; // In case this changes (ha!). 363 Time t; 364 Time::Exploded exploded; 365 366 t = Time::UnixEpoch() - TimeDelta::FromMicroseconds(1); 367 t.UTCExplode(&exploded); 368 EXPECT_TRUE(exploded.HasValidValues()); 369 // Should be 1969-12-31 23:59:59 999 milliseconds (and 999 microseconds). 370 EXPECT_EQ(kUnixEpochYear - 1, exploded.year); 371 EXPECT_EQ(12, exploded.month); 372 EXPECT_EQ(31, exploded.day_of_month); 373 EXPECT_EQ(23, exploded.hour); 374 EXPECT_EQ(59, exploded.minute); 375 EXPECT_EQ(59, exploded.second); 376 EXPECT_EQ(999, exploded.millisecond); 377 378 t = Time::UnixEpoch() - TimeDelta::FromMicroseconds(1000); 379 t.UTCExplode(&exploded); 380 EXPECT_TRUE(exploded.HasValidValues()); 381 // Should be 1969-12-31 23:59:59 999 milliseconds. 382 EXPECT_EQ(kUnixEpochYear - 1, exploded.year); 383 EXPECT_EQ(12, exploded.month); 384 EXPECT_EQ(31, exploded.day_of_month); 385 EXPECT_EQ(23, exploded.hour); 386 EXPECT_EQ(59, exploded.minute); 387 EXPECT_EQ(59, exploded.second); 388 EXPECT_EQ(999, exploded.millisecond); 389 390 t = Time::UnixEpoch() - TimeDelta::FromMicroseconds(1001); 391 t.UTCExplode(&exploded); 392 EXPECT_TRUE(exploded.HasValidValues()); 393 // Should be 1969-12-31 23:59:59 998 milliseconds (and 999 microseconds). 394 EXPECT_EQ(kUnixEpochYear - 1, exploded.year); 395 EXPECT_EQ(12, exploded.month); 396 EXPECT_EQ(31, exploded.day_of_month); 397 EXPECT_EQ(23, exploded.hour); 398 EXPECT_EQ(59, exploded.minute); 399 EXPECT_EQ(59, exploded.second); 400 EXPECT_EQ(998, exploded.millisecond); 401 402 t = Time::UnixEpoch() - TimeDelta::FromMilliseconds(1000); 403 t.UTCExplode(&exploded); 404 EXPECT_TRUE(exploded.HasValidValues()); 405 // Should be 1969-12-31 23:59:59. 406 EXPECT_EQ(kUnixEpochYear - 1, exploded.year); 407 EXPECT_EQ(12, exploded.month); 408 EXPECT_EQ(31, exploded.day_of_month); 409 EXPECT_EQ(23, exploded.hour); 410 EXPECT_EQ(59, exploded.minute); 411 EXPECT_EQ(59, exploded.second); 412 EXPECT_EQ(0, exploded.millisecond); 413 414 t = Time::UnixEpoch() - TimeDelta::FromMilliseconds(1001); 415 t.UTCExplode(&exploded); 416 EXPECT_TRUE(exploded.HasValidValues()); 417 // Should be 1969-12-31 23:59:58 999 milliseconds. 418 EXPECT_EQ(kUnixEpochYear - 1, exploded.year); 419 EXPECT_EQ(12, exploded.month); 420 EXPECT_EQ(31, exploded.day_of_month); 421 EXPECT_EQ(23, exploded.hour); 422 EXPECT_EQ(59, exploded.minute); 423 EXPECT_EQ(58, exploded.second); 424 EXPECT_EQ(999, exploded.millisecond); 425 426 // Make sure we still handle at/after Unix epoch correctly. 427 t = Time::UnixEpoch(); 428 t.UTCExplode(&exploded); 429 EXPECT_TRUE(exploded.HasValidValues()); 430 // Should be 1970-12-31 00:00:00 0 milliseconds. 431 EXPECT_EQ(kUnixEpochYear, exploded.year); 432 EXPECT_EQ(1, exploded.month); 433 EXPECT_EQ(1, exploded.day_of_month); 434 EXPECT_EQ(0, exploded.hour); 435 EXPECT_EQ(0, exploded.minute); 436 EXPECT_EQ(0, exploded.second); 437 EXPECT_EQ(0, exploded.millisecond); 438 439 t = Time::UnixEpoch() + TimeDelta::FromMicroseconds(1); 440 t.UTCExplode(&exploded); 441 EXPECT_TRUE(exploded.HasValidValues()); 442 // Should be 1970-01-01 00:00:00 0 milliseconds (and 1 microsecond). 443 EXPECT_EQ(kUnixEpochYear, exploded.year); 444 EXPECT_EQ(1, exploded.month); 445 EXPECT_EQ(1, exploded.day_of_month); 446 EXPECT_EQ(0, exploded.hour); 447 EXPECT_EQ(0, exploded.minute); 448 EXPECT_EQ(0, exploded.second); 449 EXPECT_EQ(0, exploded.millisecond); 450 451 t = Time::UnixEpoch() + TimeDelta::FromMicroseconds(1000); 452 t.UTCExplode(&exploded); 453 EXPECT_TRUE(exploded.HasValidValues()); 454 // Should be 1970-01-01 00:00:00 1 millisecond. 455 EXPECT_EQ(kUnixEpochYear, exploded.year); 456 EXPECT_EQ(1, exploded.month); 457 EXPECT_EQ(1, exploded.day_of_month); 458 EXPECT_EQ(0, exploded.hour); 459 EXPECT_EQ(0, exploded.minute); 460 EXPECT_EQ(0, exploded.second); 461 EXPECT_EQ(1, exploded.millisecond); 462 463 t = Time::UnixEpoch() + TimeDelta::FromMilliseconds(1000); 464 t.UTCExplode(&exploded); 465 EXPECT_TRUE(exploded.HasValidValues()); 466 // Should be 1970-01-01 00:00:01. 467 EXPECT_EQ(kUnixEpochYear, exploded.year); 468 EXPECT_EQ(1, exploded.month); 469 EXPECT_EQ(1, exploded.day_of_month); 470 EXPECT_EQ(0, exploded.hour); 471 EXPECT_EQ(0, exploded.minute); 472 EXPECT_EQ(1, exploded.second); 473 EXPECT_EQ(0, exploded.millisecond); 474 475 t = Time::UnixEpoch() + TimeDelta::FromMilliseconds(1001); 476 t.UTCExplode(&exploded); 477 EXPECT_TRUE(exploded.HasValidValues()); 478 // Should be 1970-01-01 00:00:01 1 millisecond. 479 EXPECT_EQ(kUnixEpochYear, exploded.year); 480 EXPECT_EQ(1, exploded.month); 481 EXPECT_EQ(1, exploded.day_of_month); 482 EXPECT_EQ(0, exploded.hour); 483 EXPECT_EQ(0, exploded.minute); 484 EXPECT_EQ(1, exploded.second); 485 EXPECT_EQ(1, exploded.millisecond); 486 } 487 488 TEST_F(TimeTest, Max) { 489 Time max = Time::Max(); 490 EXPECT_TRUE(max.is_max()); 491 EXPECT_EQ(max, Time::Max()); 492 EXPECT_GT(max, Time::Now()); 493 EXPECT_GT(max, Time()); 494 } 495 496 TEST_F(TimeTest, MaxConversions) { 497 Time t = Time::Max(); 498 EXPECT_EQ(std::numeric_limits<int64_t>::max(), t.ToInternalValue()); 499 500 t = Time::FromDoubleT(std::numeric_limits<double>::infinity()); 501 EXPECT_TRUE(t.is_max()); 502 EXPECT_EQ(std::numeric_limits<double>::infinity(), t.ToDoubleT()); 503 504 t = Time::FromJsTime(std::numeric_limits<double>::infinity()); 505 EXPECT_TRUE(t.is_max()); 506 EXPECT_EQ(std::numeric_limits<double>::infinity(), t.ToJsTime()); 507 508 t = Time::FromTimeT(std::numeric_limits<time_t>::max()); 509 EXPECT_TRUE(t.is_max()); 510 EXPECT_EQ(std::numeric_limits<time_t>::max(), t.ToTimeT()); 511 512 #if defined(OS_POSIX) 513 struct timeval tval; 514 tval.tv_sec = std::numeric_limits<time_t>::max(); 515 tval.tv_usec = static_cast<suseconds_t>(Time::kMicrosecondsPerSecond) - 1; 516 t = Time::FromTimeVal(tval); 517 EXPECT_TRUE(t.is_max()); 518 tval = t.ToTimeVal(); 519 EXPECT_EQ(std::numeric_limits<time_t>::max(), tval.tv_sec); 520 EXPECT_EQ(static_cast<suseconds_t>(Time::kMicrosecondsPerSecond) - 1, 521 tval.tv_usec); 522 #endif 523 524 #if defined(OS_MACOSX) 525 t = Time::FromCFAbsoluteTime(std::numeric_limits<CFAbsoluteTime>::infinity()); 526 EXPECT_TRUE(t.is_max()); 527 EXPECT_EQ(std::numeric_limits<CFAbsoluteTime>::infinity(), 528 t.ToCFAbsoluteTime()); 529 #endif 530 531 #if defined(OS_WIN) 532 FILETIME ftime; 533 ftime.dwHighDateTime = std::numeric_limits<DWORD>::max(); 534 ftime.dwLowDateTime = std::numeric_limits<DWORD>::max(); 535 t = Time::FromFileTime(ftime); 536 EXPECT_TRUE(t.is_max()); 537 ftime = t.ToFileTime(); 538 EXPECT_EQ(std::numeric_limits<DWORD>::max(), ftime.dwHighDateTime); 539 EXPECT_EQ(std::numeric_limits<DWORD>::max(), ftime.dwLowDateTime); 540 #endif 541 } 542 543 #if defined(OS_MACOSX) 544 TEST_F(TimeTest, TimeTOverflow) { 545 Time t = Time::FromInternalValue(std::numeric_limits<int64_t>::max() - 1); 546 EXPECT_FALSE(t.is_max()); 547 EXPECT_EQ(std::numeric_limits<time_t>::max(), t.ToTimeT()); 548 } 549 #endif 550 551 #if defined(OS_ANDROID) 552 TEST_F(TimeTest, FromLocalExplodedCrashOnAndroid) { 553 // This crashed inside Time:: FromLocalExploded() on Android 4.1.2. 554 // See http://crbug.com/287821 555 Time::Exploded midnight = {2013, // year 556 10, // month 557 0, // day_of_week 558 13, // day_of_month 559 0, // hour 560 0, // minute 561 0, // second 562 }; 563 // The string passed to putenv() must be a char* and the documentation states 564 // that it 'becomes part of the environment', so use a static buffer. 565 static char buffer[] = "TZ=America/Santiago"; 566 putenv(buffer); 567 tzset(); 568 Time t = Time::FromLocalExploded(midnight); 569 EXPECT_EQ(1381633200, t.ToTimeT()); 570 } 571 #endif // OS_ANDROID 572 573 TEST(TimeTicks, Deltas) { 574 for (int index = 0; index < 50; index++) { 575 TimeTicks ticks_start = TimeTicks::Now(); 576 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(10)); 577 TimeTicks ticks_stop = TimeTicks::Now(); 578 TimeDelta delta = ticks_stop - ticks_start; 579 // Note: Although we asked for a 10ms sleep, if the 580 // time clock has a finer granularity than the Sleep() 581 // clock, it is quite possible to wakeup early. Here 582 // is how that works: 583 // Time(ms timer) Time(us timer) 584 // 5 5010 585 // 6 6010 586 // 7 7010 587 // 8 8010 588 // 9 9000 589 // Elapsed 4ms 3990us 590 // 591 // Unfortunately, our InMilliseconds() function truncates 592 // rather than rounds. We should consider fixing this 593 // so that our averages come out better. 594 EXPECT_GE(delta.InMilliseconds(), 9); 595 EXPECT_GE(delta.InMicroseconds(), 9000); 596 EXPECT_EQ(delta.InSeconds(), 0); 597 } 598 } 599 600 static void HighResClockTest(TimeTicks (*GetTicks)()) { 601 // IsHighResolution() is false on some systems. Since the product still works 602 // even if it's false, it makes this entire test questionable. 603 if (!TimeTicks::IsHighResolution()) 604 return; 605 606 // Why do we loop here? 607 // We're trying to measure that intervals increment in a VERY small amount 608 // of time -- less than 15ms. Unfortunately, if we happen to have a 609 // context switch in the middle of our test, the context switch could easily 610 // exceed our limit. So, we iterate on this several times. As long as we're 611 // able to detect the fine-granularity timers at least once, then the test 612 // has succeeded. 613 614 const int kTargetGranularityUs = 15000; // 15ms 615 616 bool success = false; 617 int retries = 100; // Arbitrary. 618 TimeDelta delta; 619 while (!success && retries--) { 620 TimeTicks ticks_start = GetTicks(); 621 // Loop until we can detect that the clock has changed. Non-HighRes timers 622 // will increment in chunks, e.g. 15ms. By spinning until we see a clock 623 // change, we detect the minimum time between measurements. 624 do { 625 delta = GetTicks() - ticks_start; 626 } while (delta.InMilliseconds() == 0); 627 628 if (delta.InMicroseconds() <= kTargetGranularityUs) 629 success = true; 630 } 631 632 // In high resolution mode, we expect to see the clock increment 633 // in intervals less than 15ms. 634 EXPECT_TRUE(success); 635 } 636 637 TEST(TimeTicks, HighRes) { 638 HighResClockTest(&TimeTicks::Now); 639 } 640 641 // Fails frequently on Android http://crbug.com/352633 with: 642 // Expected: (delta_thread.InMicroseconds()) > (0), actual: 0 vs 0 643 #if defined(OS_ANDROID) 644 #define MAYBE_ThreadNow DISABLED_ThreadNow 645 #else 646 #define MAYBE_ThreadNow ThreadNow 647 #endif 648 TEST(ThreadTicks, MAYBE_ThreadNow) { 649 if (ThreadTicks::IsSupported()) { 650 ThreadTicks::WaitUntilInitialized(); 651 TimeTicks begin = TimeTicks::Now(); 652 ThreadTicks begin_thread = ThreadTicks::Now(); 653 // Make sure that ThreadNow value is non-zero. 654 EXPECT_GT(begin_thread, ThreadTicks()); 655 // Sleep for 10 milliseconds to get the thread de-scheduled. 656 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(10)); 657 ThreadTicks end_thread = ThreadTicks::Now(); 658 TimeTicks end = TimeTicks::Now(); 659 TimeDelta delta = end - begin; 660 TimeDelta delta_thread = end_thread - begin_thread; 661 // Make sure that some thread time have elapsed. 662 EXPECT_GT(delta_thread.InMicroseconds(), 0); 663 // But the thread time is at least 9ms less than clock time. 664 TimeDelta difference = delta - delta_thread; 665 EXPECT_GE(difference.InMicroseconds(), 9000); 666 } 667 } 668 669 TEST(TimeTicks, SnappedToNextTickBasic) { 670 base::TimeTicks phase = base::TimeTicks::FromInternalValue(4000); 671 base::TimeDelta interval = base::TimeDelta::FromMicroseconds(1000); 672 base::TimeTicks timestamp; 673 674 // Timestamp in previous interval. 675 timestamp = base::TimeTicks::FromInternalValue(3500); 676 EXPECT_EQ(4000, 677 timestamp.SnappedToNextTick(phase, interval).ToInternalValue()); 678 679 // Timestamp in next interval. 680 timestamp = base::TimeTicks::FromInternalValue(4500); 681 EXPECT_EQ(5000, 682 timestamp.SnappedToNextTick(phase, interval).ToInternalValue()); 683 684 // Timestamp multiple intervals before. 685 timestamp = base::TimeTicks::FromInternalValue(2500); 686 EXPECT_EQ(3000, 687 timestamp.SnappedToNextTick(phase, interval).ToInternalValue()); 688 689 // Timestamp multiple intervals after. 690 timestamp = base::TimeTicks::FromInternalValue(6500); 691 EXPECT_EQ(7000, 692 timestamp.SnappedToNextTick(phase, interval).ToInternalValue()); 693 694 // Timestamp on previous interval. 695 timestamp = base::TimeTicks::FromInternalValue(3000); 696 EXPECT_EQ(3000, 697 timestamp.SnappedToNextTick(phase, interval).ToInternalValue()); 698 699 // Timestamp on next interval. 700 timestamp = base::TimeTicks::FromInternalValue(5000); 701 EXPECT_EQ(5000, 702 timestamp.SnappedToNextTick(phase, interval).ToInternalValue()); 703 704 // Timestamp equal to phase. 705 timestamp = base::TimeTicks::FromInternalValue(4000); 706 EXPECT_EQ(4000, 707 timestamp.SnappedToNextTick(phase, interval).ToInternalValue()); 708 } 709 710 TEST(TimeTicks, SnappedToNextTickOverflow) { 711 // int(big_timestamp / interval) < 0, so this causes a crash if the number of 712 // intervals elapsed is attempted to be stored in an int. 713 base::TimeTicks phase = base::TimeTicks::FromInternalValue(0); 714 base::TimeDelta interval = base::TimeDelta::FromMicroseconds(4000); 715 base::TimeTicks big_timestamp = 716 base::TimeTicks::FromInternalValue(8635916564000); 717 718 EXPECT_EQ(8635916564000, 719 big_timestamp.SnappedToNextTick(phase, interval).ToInternalValue()); 720 EXPECT_EQ(8635916564000, 721 big_timestamp.SnappedToNextTick(big_timestamp, interval) 722 .ToInternalValue()); 723 } 724 725 TEST(TimeDelta, FromAndIn) { 726 EXPECT_TRUE(TimeDelta::FromDays(2) == TimeDelta::FromHours(48)); 727 EXPECT_TRUE(TimeDelta::FromHours(3) == TimeDelta::FromMinutes(180)); 728 EXPECT_TRUE(TimeDelta::FromMinutes(2) == TimeDelta::FromSeconds(120)); 729 EXPECT_TRUE(TimeDelta::FromSeconds(2) == TimeDelta::FromMilliseconds(2000)); 730 EXPECT_TRUE(TimeDelta::FromMilliseconds(2) == 731 TimeDelta::FromMicroseconds(2000)); 732 EXPECT_TRUE(TimeDelta::FromSecondsD(2.3) == 733 TimeDelta::FromMilliseconds(2300)); 734 EXPECT_TRUE(TimeDelta::FromMillisecondsD(2.5) == 735 TimeDelta::FromMicroseconds(2500)); 736 EXPECT_EQ(13, TimeDelta::FromDays(13).InDays()); 737 EXPECT_EQ(13, TimeDelta::FromHours(13).InHours()); 738 EXPECT_EQ(13, TimeDelta::FromMinutes(13).InMinutes()); 739 EXPECT_EQ(13, TimeDelta::FromSeconds(13).InSeconds()); 740 EXPECT_EQ(13.0, TimeDelta::FromSeconds(13).InSecondsF()); 741 EXPECT_EQ(13, TimeDelta::FromMilliseconds(13).InMilliseconds()); 742 EXPECT_EQ(13.0, TimeDelta::FromMilliseconds(13).InMillisecondsF()); 743 EXPECT_EQ(13, TimeDelta::FromSecondsD(13.1).InSeconds()); 744 EXPECT_EQ(13.1, TimeDelta::FromSecondsD(13.1).InSecondsF()); 745 EXPECT_EQ(13, TimeDelta::FromMillisecondsD(13.3).InMilliseconds()); 746 EXPECT_EQ(13.3, TimeDelta::FromMillisecondsD(13.3).InMillisecondsF()); 747 EXPECT_EQ(13, TimeDelta::FromMicroseconds(13).InMicroseconds()); 748 EXPECT_EQ(3.456, TimeDelta::FromMillisecondsD(3.45678).InMillisecondsF()); 749 } 750 751 #if defined(OS_POSIX) 752 TEST(TimeDelta, TimeSpecConversion) { 753 struct timespec result = TimeDelta::FromSeconds(0).ToTimeSpec(); 754 EXPECT_EQ(result.tv_sec, 0); 755 EXPECT_EQ(result.tv_nsec, 0); 756 757 result = TimeDelta::FromSeconds(1).ToTimeSpec(); 758 EXPECT_EQ(result.tv_sec, 1); 759 EXPECT_EQ(result.tv_nsec, 0); 760 761 result = TimeDelta::FromMicroseconds(1).ToTimeSpec(); 762 EXPECT_EQ(result.tv_sec, 0); 763 EXPECT_EQ(result.tv_nsec, 1000); 764 765 result = TimeDelta::FromMicroseconds( 766 Time::kMicrosecondsPerSecond + 1).ToTimeSpec(); 767 EXPECT_EQ(result.tv_sec, 1); 768 EXPECT_EQ(result.tv_nsec, 1000); 769 } 770 #endif // OS_POSIX 771 772 // Our internal time format is serialized in things like databases, so it's 773 // important that it's consistent across all our platforms. We use the 1601 774 // Windows epoch as the internal format across all platforms. 775 TEST(TimeDelta, WindowsEpoch) { 776 Time::Exploded exploded; 777 exploded.year = 1970; 778 exploded.month = 1; 779 exploded.day_of_week = 0; // Should be unusued. 780 exploded.day_of_month = 1; 781 exploded.hour = 0; 782 exploded.minute = 0; 783 exploded.second = 0; 784 exploded.millisecond = 0; 785 Time t = Time::FromUTCExploded(exploded); 786 // Unix 1970 epoch. 787 EXPECT_EQ(INT64_C(11644473600000000), t.ToInternalValue()); 788 789 // We can't test 1601 epoch, since the system time functions on Linux 790 // only compute years starting from 1900. 791 } 792 793 // We could define this separately for Time, TimeTicks and TimeDelta but the 794 // definitions would be identical anyway. 795 template <class Any> 796 std::string AnyToString(Any any) { 797 std::ostringstream oss; 798 oss << any; 799 return oss.str(); 800 } 801 802 TEST(TimeDelta, Magnitude) { 803 const int64_t zero = 0; 804 EXPECT_EQ(TimeDelta::FromMicroseconds(zero), 805 TimeDelta::FromMicroseconds(zero).magnitude()); 806 807 const int64_t one = 1; 808 const int64_t negative_one = -1; 809 EXPECT_EQ(TimeDelta::FromMicroseconds(one), 810 TimeDelta::FromMicroseconds(one).magnitude()); 811 EXPECT_EQ(TimeDelta::FromMicroseconds(one), 812 TimeDelta::FromMicroseconds(negative_one).magnitude()); 813 814 const int64_t max_int64_minus_one = std::numeric_limits<int64_t>::max() - 1; 815 const int64_t min_int64_plus_two = std::numeric_limits<int64_t>::min() + 2; 816 EXPECT_EQ(TimeDelta::FromMicroseconds(max_int64_minus_one), 817 TimeDelta::FromMicroseconds(max_int64_minus_one).magnitude()); 818 EXPECT_EQ(TimeDelta::FromMicroseconds(max_int64_minus_one), 819 TimeDelta::FromMicroseconds(min_int64_plus_two).magnitude()); 820 } 821 822 TEST(TimeDelta, Max) { 823 TimeDelta max = TimeDelta::Max(); 824 EXPECT_TRUE(max.is_max()); 825 EXPECT_EQ(max, TimeDelta::Max()); 826 EXPECT_GT(max, TimeDelta::FromDays(100 * 365)); 827 EXPECT_GT(max, TimeDelta()); 828 } 829 830 bool IsMin(TimeDelta delta) { 831 return (-delta).is_max(); 832 } 833 834 TEST(TimeDelta, MaxConversions) { 835 TimeDelta t = TimeDelta::Max(); 836 EXPECT_EQ(std::numeric_limits<int64_t>::max(), t.ToInternalValue()); 837 838 EXPECT_EQ(std::numeric_limits<int>::max(), t.InDays()); 839 EXPECT_EQ(std::numeric_limits<int>::max(), t.InHours()); 840 EXPECT_EQ(std::numeric_limits<int>::max(), t.InMinutes()); 841 EXPECT_EQ(std::numeric_limits<double>::infinity(), t.InSecondsF()); 842 EXPECT_EQ(std::numeric_limits<int64_t>::max(), t.InSeconds()); 843 EXPECT_EQ(std::numeric_limits<double>::infinity(), t.InMillisecondsF()); 844 EXPECT_EQ(std::numeric_limits<int64_t>::max(), t.InMilliseconds()); 845 EXPECT_EQ(std::numeric_limits<int64_t>::max(), t.InMillisecondsRoundedUp()); 846 847 t = TimeDelta::FromDays(std::numeric_limits<int>::max()); 848 EXPECT_TRUE(t.is_max()); 849 850 t = TimeDelta::FromHours(std::numeric_limits<int>::max()); 851 EXPECT_TRUE(t.is_max()); 852 853 t = TimeDelta::FromMinutes(std::numeric_limits<int>::max()); 854 EXPECT_TRUE(t.is_max()); 855 856 int64_t max_int = std::numeric_limits<int64_t>::max(); 857 858 t = TimeDelta::FromSeconds(max_int / Time::kMicrosecondsPerSecond + 1); 859 EXPECT_TRUE(t.is_max()); 860 861 t = TimeDelta::FromMilliseconds(max_int / Time::kMillisecondsPerSecond + 1); 862 EXPECT_TRUE(t.is_max()); 863 864 t = TimeDelta::FromMicroseconds(max_int); 865 EXPECT_TRUE(t.is_max()); 866 867 t = TimeDelta::FromSeconds(-max_int / Time::kMicrosecondsPerSecond - 1); 868 EXPECT_TRUE(IsMin(t)); 869 870 t = TimeDelta::FromMilliseconds(-max_int / Time::kMillisecondsPerSecond - 1); 871 EXPECT_TRUE(IsMin(t)); 872 873 t = TimeDelta::FromMicroseconds(-max_int); 874 EXPECT_TRUE(IsMin(t)); 875 876 t = -TimeDelta::FromMicroseconds(std::numeric_limits<int64_t>::min()); 877 EXPECT_FALSE(IsMin(t)); 878 879 t = TimeDelta::FromSecondsD(std::numeric_limits<double>::infinity()); 880 EXPECT_TRUE(t.is_max()); 881 882 double max_d = max_int; 883 884 t = TimeDelta::FromSecondsD(max_d / Time::kMicrosecondsPerSecond + 1); 885 EXPECT_TRUE(t.is_max()); 886 887 t = TimeDelta::FromMillisecondsD(std::numeric_limits<double>::infinity()); 888 EXPECT_TRUE(t.is_max()); 889 890 t = TimeDelta::FromMillisecondsD(max_d / Time::kMillisecondsPerSecond * 2); 891 EXPECT_TRUE(t.is_max()); 892 893 t = TimeDelta::FromSecondsD(-max_d / Time::kMicrosecondsPerSecond - 1); 894 EXPECT_TRUE(IsMin(t)); 895 896 t = TimeDelta::FromMillisecondsD(-max_d / Time::kMillisecondsPerSecond * 2); 897 EXPECT_TRUE(IsMin(t)); 898 } 899 900 TEST(TimeDelta, NumericOperators) { 901 double d = 0.5; 902 EXPECT_EQ(TimeDelta::FromMilliseconds(500), 903 TimeDelta::FromMilliseconds(1000) * d); 904 EXPECT_EQ(TimeDelta::FromMilliseconds(2000), 905 TimeDelta::FromMilliseconds(1000) / d); 906 EXPECT_EQ(TimeDelta::FromMilliseconds(500), 907 TimeDelta::FromMilliseconds(1000) *= d); 908 EXPECT_EQ(TimeDelta::FromMilliseconds(2000), 909 TimeDelta::FromMilliseconds(1000) /= d); 910 EXPECT_EQ(TimeDelta::FromMilliseconds(500), 911 d * TimeDelta::FromMilliseconds(1000)); 912 913 float f = 0.5; 914 EXPECT_EQ(TimeDelta::FromMilliseconds(500), 915 TimeDelta::FromMilliseconds(1000) * f); 916 EXPECT_EQ(TimeDelta::FromMilliseconds(2000), 917 TimeDelta::FromMilliseconds(1000) / f); 918 EXPECT_EQ(TimeDelta::FromMilliseconds(500), 919 TimeDelta::FromMilliseconds(1000) *= f); 920 EXPECT_EQ(TimeDelta::FromMilliseconds(2000), 921 TimeDelta::FromMilliseconds(1000) /= f); 922 EXPECT_EQ(TimeDelta::FromMilliseconds(500), 923 f * TimeDelta::FromMilliseconds(1000)); 924 925 int i = 2; 926 EXPECT_EQ(TimeDelta::FromMilliseconds(2000), 927 TimeDelta::FromMilliseconds(1000) * i); 928 EXPECT_EQ(TimeDelta::FromMilliseconds(500), 929 TimeDelta::FromMilliseconds(1000) / i); 930 EXPECT_EQ(TimeDelta::FromMilliseconds(2000), 931 TimeDelta::FromMilliseconds(1000) *= i); 932 EXPECT_EQ(TimeDelta::FromMilliseconds(500), 933 TimeDelta::FromMilliseconds(1000) /= i); 934 EXPECT_EQ(TimeDelta::FromMilliseconds(2000), 935 i * TimeDelta::FromMilliseconds(1000)); 936 937 int64_t i64 = 2; 938 EXPECT_EQ(TimeDelta::FromMilliseconds(2000), 939 TimeDelta::FromMilliseconds(1000) * i64); 940 EXPECT_EQ(TimeDelta::FromMilliseconds(500), 941 TimeDelta::FromMilliseconds(1000) / i64); 942 EXPECT_EQ(TimeDelta::FromMilliseconds(2000), 943 TimeDelta::FromMilliseconds(1000) *= i64); 944 EXPECT_EQ(TimeDelta::FromMilliseconds(500), 945 TimeDelta::FromMilliseconds(1000) /= i64); 946 EXPECT_EQ(TimeDelta::FromMilliseconds(2000), 947 i64 * TimeDelta::FromMilliseconds(1000)); 948 949 EXPECT_EQ(TimeDelta::FromMilliseconds(500), 950 TimeDelta::FromMilliseconds(1000) * 0.5); 951 EXPECT_EQ(TimeDelta::FromMilliseconds(2000), 952 TimeDelta::FromMilliseconds(1000) / 0.5); 953 EXPECT_EQ(TimeDelta::FromMilliseconds(500), 954 TimeDelta::FromMilliseconds(1000) *= 0.5); 955 EXPECT_EQ(TimeDelta::FromMilliseconds(2000), 956 TimeDelta::FromMilliseconds(1000) /= 0.5); 957 EXPECT_EQ(TimeDelta::FromMilliseconds(500), 958 0.5 * TimeDelta::FromMilliseconds(1000)); 959 960 EXPECT_EQ(TimeDelta::FromMilliseconds(2000), 961 TimeDelta::FromMilliseconds(1000) * 2); 962 EXPECT_EQ(TimeDelta::FromMilliseconds(500), 963 TimeDelta::FromMilliseconds(1000) / 2); 964 EXPECT_EQ(TimeDelta::FromMilliseconds(2000), 965 TimeDelta::FromMilliseconds(1000) *= 2); 966 EXPECT_EQ(TimeDelta::FromMilliseconds(500), 967 TimeDelta::FromMilliseconds(1000) /= 2); 968 EXPECT_EQ(TimeDelta::FromMilliseconds(2000), 969 2 * TimeDelta::FromMilliseconds(1000)); 970 } 971 972 TEST(TimeDelta, Overflows) { 973 // Some sanity checks. 974 EXPECT_TRUE(TimeDelta::Max().is_max()); 975 EXPECT_TRUE(IsMin(-TimeDelta::Max())); 976 EXPECT_GT(TimeDelta(), -TimeDelta::Max()); 977 978 TimeDelta large_delta = TimeDelta::Max() - TimeDelta::FromMilliseconds(1); 979 TimeDelta large_negative = -large_delta; 980 EXPECT_GT(TimeDelta(), large_negative); 981 EXPECT_FALSE(large_delta.is_max()); 982 EXPECT_FALSE(IsMin(-large_negative)); 983 TimeDelta one_second = TimeDelta::FromSeconds(1); 984 985 // Test +, -, * and / operators. 986 EXPECT_TRUE((large_delta + one_second).is_max()); 987 EXPECT_TRUE(IsMin(large_negative + (-one_second))); 988 EXPECT_TRUE(IsMin(large_negative - one_second)); 989 EXPECT_TRUE((large_delta - (-one_second)).is_max()); 990 EXPECT_TRUE((large_delta * 2).is_max()); 991 EXPECT_TRUE(IsMin(large_delta * -2)); 992 EXPECT_TRUE((large_delta / 0.5).is_max()); 993 EXPECT_TRUE(IsMin(large_delta / -0.5)); 994 995 // Test +=, -=, *= and /= operators. 996 TimeDelta delta = large_delta; 997 delta += one_second; 998 EXPECT_TRUE(delta.is_max()); 999 delta = large_negative; 1000 delta += -one_second; 1001 EXPECT_TRUE(IsMin(delta)); 1002 1003 delta = large_negative; 1004 delta -= one_second; 1005 EXPECT_TRUE(IsMin(delta)); 1006 delta = large_delta; 1007 delta -= -one_second; 1008 EXPECT_TRUE(delta.is_max()); 1009 1010 delta = large_delta; 1011 delta *= 2; 1012 EXPECT_TRUE(delta.is_max()); 1013 delta = large_negative; 1014 delta *= 1.5; 1015 EXPECT_TRUE(IsMin(delta)); 1016 1017 delta = large_delta; 1018 delta /= 0.5; 1019 EXPECT_TRUE(delta.is_max()); 1020 delta = large_negative; 1021 delta /= 0.5; 1022 EXPECT_TRUE(IsMin(delta)); 1023 1024 // Test operations with Time and TimeTicks. 1025 EXPECT_TRUE((large_delta + Time::Now()).is_max()); 1026 EXPECT_TRUE((large_delta + TimeTicks::Now()).is_max()); 1027 EXPECT_TRUE((Time::Now() + large_delta).is_max()); 1028 EXPECT_TRUE((TimeTicks::Now() + large_delta).is_max()); 1029 1030 Time time_now = Time::Now(); 1031 EXPECT_EQ(one_second, (time_now + one_second) - time_now); 1032 EXPECT_EQ(-one_second, (time_now - one_second) - time_now); 1033 1034 TimeTicks ticks_now = TimeTicks::Now(); 1035 EXPECT_EQ(-one_second, (ticks_now - one_second) - ticks_now); 1036 EXPECT_EQ(one_second, (ticks_now + one_second) - ticks_now); 1037 } 1038 1039 TEST(TimeDeltaLogging, DCheckEqCompiles) { 1040 DCHECK_EQ(TimeDelta(), TimeDelta()); 1041 } 1042 1043 TEST(TimeDeltaLogging, EmptyIsZero) { 1044 TimeDelta zero; 1045 EXPECT_EQ("0s", AnyToString(zero)); 1046 } 1047 1048 TEST(TimeDeltaLogging, FiveHundredMs) { 1049 TimeDelta five_hundred_ms = TimeDelta::FromMilliseconds(500); 1050 EXPECT_EQ("0.5s", AnyToString(five_hundred_ms)); 1051 } 1052 1053 TEST(TimeDeltaLogging, MinusTenSeconds) { 1054 TimeDelta minus_ten_seconds = TimeDelta::FromSeconds(-10); 1055 EXPECT_EQ("-10s", AnyToString(minus_ten_seconds)); 1056 } 1057 1058 TEST(TimeDeltaLogging, DoesNotMessUpFormattingFlags) { 1059 std::ostringstream oss; 1060 std::ios_base::fmtflags flags_before = oss.flags(); 1061 oss << TimeDelta(); 1062 EXPECT_EQ(flags_before, oss.flags()); 1063 } 1064 1065 TEST(TimeDeltaLogging, DoesNotMakeStreamBad) { 1066 std::ostringstream oss; 1067 oss << TimeDelta(); 1068 EXPECT_TRUE(oss.good()); 1069 } 1070 1071 TEST(TimeLogging, DCheckEqCompiles) { 1072 DCHECK_EQ(Time(), Time()); 1073 } 1074 1075 TEST(TimeLogging, ChromeBirthdate) { 1076 Time birthdate; 1077 ASSERT_TRUE(Time::FromString("Tue, 02 Sep 2008 09:42:18 GMT", &birthdate)); 1078 EXPECT_EQ("2008-09-02 09:42:18.000 UTC", AnyToString(birthdate)); 1079 } 1080 1081 TEST(TimeLogging, DoesNotMessUpFormattingFlags) { 1082 std::ostringstream oss; 1083 std::ios_base::fmtflags flags_before = oss.flags(); 1084 oss << Time(); 1085 EXPECT_EQ(flags_before, oss.flags()); 1086 } 1087 1088 TEST(TimeLogging, DoesNotMakeStreamBad) { 1089 std::ostringstream oss; 1090 oss << Time(); 1091 EXPECT_TRUE(oss.good()); 1092 } 1093 1094 TEST(TimeTicksLogging, DCheckEqCompiles) { 1095 DCHECK_EQ(TimeTicks(), TimeTicks()); 1096 } 1097 1098 TEST(TimeTicksLogging, ZeroTime) { 1099 TimeTicks zero; 1100 EXPECT_EQ("0 bogo-microseconds", AnyToString(zero)); 1101 } 1102 1103 TEST(TimeTicksLogging, FortyYearsLater) { 1104 TimeTicks forty_years_later = 1105 TimeTicks() + TimeDelta::FromDays(365.25 * 40); 1106 EXPECT_EQ("1262304000000000 bogo-microseconds", 1107 AnyToString(forty_years_later)); 1108 } 1109 1110 TEST(TimeTicksLogging, DoesNotMessUpFormattingFlags) { 1111 std::ostringstream oss; 1112 std::ios_base::fmtflags flags_before = oss.flags(); 1113 oss << TimeTicks(); 1114 EXPECT_EQ(flags_before, oss.flags()); 1115 } 1116 1117 TEST(TimeTicksLogging, DoesNotMakeStreamBad) { 1118 std::ostringstream oss; 1119 oss << TimeTicks(); 1120 EXPECT_TRUE(oss.good()); 1121 } 1122 1123 } // namespace 1124 1125 } // namespace base 1126