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 <time.h> 8 9 #include "base/compiler_specific.h" 10 #include "base/logging.h" 11 #include "base/strings/stringprintf.h" 12 #include "base/threading/platform_thread.h" 13 #include "build/build_config.h" 14 #include "testing/gtest/include/gtest/gtest.h" 15 16 using base::Time; 17 using base::TimeDelta; 18 using base::TimeTicks; 19 20 // Specialized test fixture allowing time strings without timezones to be 21 // tested by comparing them to a known time in the local zone. 22 // See also pr_time_unittests.cc 23 class TimeTest : public testing::Test { 24 protected: 25 virtual void SetUp() OVERRIDE { 26 // Use mktime to get a time_t, and turn it into a PRTime by converting 27 // seconds to microseconds. Use 15th Oct 2007 12:45:00 local. This 28 // must be a time guaranteed to be outside of a DST fallback hour in 29 // any timezone. 30 struct tm local_comparison_tm = { 31 0, // second 32 45, // minute 33 12, // hour 34 15, // day of month 35 10 - 1, // month 36 2007 - 1900, // year 37 0, // day of week (ignored, output only) 38 0, // day of year (ignored, output only) 39 -1 // DST in effect, -1 tells mktime to figure it out 40 }; 41 42 time_t converted_time = mktime(&local_comparison_tm); 43 ASSERT_GT(converted_time, 0); 44 comparison_time_local_ = Time::FromTimeT(converted_time); 45 46 // time_t representation of 15th Oct 2007 12:45:00 PDT 47 comparison_time_pdt_ = Time::FromTimeT(1192477500); 48 } 49 50 Time comparison_time_local_; 51 Time comparison_time_pdt_; 52 }; 53 54 // Test conversions to/from time_t and exploding/unexploding. 55 TEST_F(TimeTest, TimeT) { 56 // C library time and exploded time. 57 time_t now_t_1 = time(NULL); 58 struct tm tms; 59 #if defined(OS_WIN) 60 localtime_s(&tms, &now_t_1); 61 #elif defined(OS_POSIX) 62 localtime_r(&now_t_1, &tms); 63 #endif 64 65 // Convert to ours. 66 Time our_time_1 = Time::FromTimeT(now_t_1); 67 Time::Exploded exploded; 68 our_time_1.LocalExplode(&exploded); 69 70 // This will test both our exploding and our time_t -> Time conversion. 71 EXPECT_EQ(tms.tm_year + 1900, exploded.year); 72 EXPECT_EQ(tms.tm_mon + 1, exploded.month); 73 EXPECT_EQ(tms.tm_mday, exploded.day_of_month); 74 EXPECT_EQ(tms.tm_hour, exploded.hour); 75 EXPECT_EQ(tms.tm_min, exploded.minute); 76 EXPECT_EQ(tms.tm_sec, exploded.second); 77 78 // Convert exploded back to the time struct. 79 Time our_time_2 = Time::FromLocalExploded(exploded); 80 EXPECT_TRUE(our_time_1 == our_time_2); 81 82 time_t now_t_2 = our_time_2.ToTimeT(); 83 EXPECT_EQ(now_t_1, now_t_2); 84 85 EXPECT_EQ(10, Time().FromTimeT(10).ToTimeT()); 86 EXPECT_EQ(10.0, Time().FromTimeT(10).ToDoubleT()); 87 88 // Conversions of 0 should stay 0. 89 EXPECT_EQ(0, Time().ToTimeT()); 90 EXPECT_EQ(0, Time::FromTimeT(0).ToInternalValue()); 91 } 92 93 // Test conversions to/from javascript time. 94 TEST_F(TimeTest, JsTime) { 95 Time epoch = Time::FromJsTime(0.0); 96 EXPECT_EQ(epoch, Time::UnixEpoch()); 97 Time t = Time::FromJsTime(700000.3); 98 EXPECT_EQ(700.0003, t.ToDoubleT()); 99 t = Time::FromDoubleT(800.73); 100 EXPECT_EQ(800730.0, t.ToJsTime()); 101 } 102 103 #if defined(OS_POSIX) 104 TEST_F(TimeTest, FromTimeVal) { 105 Time now = Time::Now(); 106 Time also_now = Time::FromTimeVal(now.ToTimeVal()); 107 EXPECT_EQ(now, also_now); 108 } 109 #endif // OS_POSIX 110 111 TEST_F(TimeTest, FromExplodedWithMilliseconds) { 112 // Some platform implementations of FromExploded are liable to drop 113 // milliseconds if we aren't careful. 114 Time now = Time::NowFromSystemTime(); 115 Time::Exploded exploded1 = {0}; 116 now.UTCExplode(&exploded1); 117 exploded1.millisecond = 500; 118 Time time = Time::FromUTCExploded(exploded1); 119 Time::Exploded exploded2 = {0}; 120 time.UTCExplode(&exploded2); 121 EXPECT_EQ(exploded1.millisecond, exploded2.millisecond); 122 } 123 124 TEST_F(TimeTest, ZeroIsSymmetric) { 125 Time zero_time(Time::FromTimeT(0)); 126 EXPECT_EQ(0, zero_time.ToTimeT()); 127 128 EXPECT_EQ(0.0, zero_time.ToDoubleT()); 129 } 130 131 TEST_F(TimeTest, LocalExplode) { 132 Time a = Time::Now(); 133 Time::Exploded exploded; 134 a.LocalExplode(&exploded); 135 136 Time b = Time::FromLocalExploded(exploded); 137 138 // The exploded structure doesn't have microseconds, and on Mac & Linux, the 139 // internal OS conversion uses seconds, which will cause truncation. So we 140 // can only make sure that the delta is within one second. 141 EXPECT_TRUE((a - b) < TimeDelta::FromSeconds(1)); 142 } 143 144 TEST_F(TimeTest, UTCExplode) { 145 Time a = Time::Now(); 146 Time::Exploded exploded; 147 a.UTCExplode(&exploded); 148 149 Time b = Time::FromUTCExploded(exploded); 150 EXPECT_TRUE((a - b) < TimeDelta::FromSeconds(1)); 151 } 152 153 TEST_F(TimeTest, LocalMidnight) { 154 Time::Exploded exploded; 155 Time::Now().LocalMidnight().LocalExplode(&exploded); 156 EXPECT_EQ(0, exploded.hour); 157 EXPECT_EQ(0, exploded.minute); 158 EXPECT_EQ(0, exploded.second); 159 EXPECT_EQ(0, exploded.millisecond); 160 } 161 162 TEST_F(TimeTest, ParseTimeTest1) { 163 time_t current_time = 0; 164 time(¤t_time); 165 166 const int BUFFER_SIZE = 64; 167 struct tm local_time = {0}; 168 char time_buf[BUFFER_SIZE] = {0}; 169 #if defined(OS_WIN) 170 localtime_s(&local_time, ¤t_time); 171 asctime_s(time_buf, arraysize(time_buf), &local_time); 172 #elif defined(OS_POSIX) 173 localtime_r(¤t_time, &local_time); 174 asctime_r(&local_time, time_buf); 175 #endif 176 177 Time parsed_time; 178 EXPECT_TRUE(Time::FromString(time_buf, &parsed_time)); 179 EXPECT_EQ(current_time, parsed_time.ToTimeT()); 180 } 181 182 TEST_F(TimeTest, DayOfWeekSunday) { 183 Time time; 184 EXPECT_TRUE(Time::FromString("Sun, 06 May 2012 12:00:00 GMT", &time)); 185 Time::Exploded exploded; 186 time.UTCExplode(&exploded); 187 EXPECT_EQ(0, exploded.day_of_week); 188 } 189 190 TEST_F(TimeTest, DayOfWeekWednesday) { 191 Time time; 192 EXPECT_TRUE(Time::FromString("Wed, 09 May 2012 12:00:00 GMT", &time)); 193 Time::Exploded exploded; 194 time.UTCExplode(&exploded); 195 EXPECT_EQ(3, exploded.day_of_week); 196 } 197 198 TEST_F(TimeTest, DayOfWeekSaturday) { 199 Time time; 200 EXPECT_TRUE(Time::FromString("Sat, 12 May 2012 12:00:00 GMT", &time)); 201 Time::Exploded exploded; 202 time.UTCExplode(&exploded); 203 EXPECT_EQ(6, exploded.day_of_week); 204 } 205 206 TEST_F(TimeTest, ParseTimeTest2) { 207 Time parsed_time; 208 EXPECT_TRUE(Time::FromString("Mon, 15 Oct 2007 19:45:00 GMT", &parsed_time)); 209 EXPECT_EQ(comparison_time_pdt_, parsed_time); 210 } 211 212 TEST_F(TimeTest, ParseTimeTest3) { 213 Time parsed_time; 214 EXPECT_TRUE(Time::FromString("15 Oct 07 12:45:00", &parsed_time)); 215 EXPECT_EQ(comparison_time_local_, parsed_time); 216 } 217 218 TEST_F(TimeTest, ParseTimeTest4) { 219 Time parsed_time; 220 EXPECT_TRUE(Time::FromString("15 Oct 07 19:45 GMT", &parsed_time)); 221 EXPECT_EQ(comparison_time_pdt_, parsed_time); 222 } 223 224 TEST_F(TimeTest, ParseTimeTest5) { 225 Time parsed_time; 226 EXPECT_TRUE(Time::FromString("Mon Oct 15 12:45 PDT 2007", &parsed_time)); 227 EXPECT_EQ(comparison_time_pdt_, parsed_time); 228 } 229 230 TEST_F(TimeTest, ParseTimeTest6) { 231 Time parsed_time; 232 EXPECT_TRUE(Time::FromString("Monday, Oct 15, 2007 12:45 PM", &parsed_time)); 233 EXPECT_EQ(comparison_time_local_, parsed_time); 234 } 235 236 TEST_F(TimeTest, ParseTimeTest7) { 237 Time parsed_time; 238 EXPECT_TRUE(Time::FromString("10/15/07 12:45:00 PM", &parsed_time)); 239 EXPECT_EQ(comparison_time_local_, parsed_time); 240 } 241 242 TEST_F(TimeTest, ParseTimeTest8) { 243 Time parsed_time; 244 EXPECT_TRUE(Time::FromString("15-OCT-2007 12:45pm", &parsed_time)); 245 EXPECT_EQ(comparison_time_local_, parsed_time); 246 } 247 248 TEST_F(TimeTest, ParseTimeTest9) { 249 Time parsed_time; 250 EXPECT_TRUE(Time::FromString("16 Oct 2007 4:45-JST (Tuesday)", &parsed_time)); 251 EXPECT_EQ(comparison_time_pdt_, parsed_time); 252 } 253 254 TEST_F(TimeTest, ParseTimeTest10) { 255 Time parsed_time; 256 EXPECT_TRUE(Time::FromString("15/10/07 12:45", &parsed_time)); 257 EXPECT_EQ(parsed_time, comparison_time_local_); 258 } 259 260 // Test some of edge cases around epoch, etc. 261 TEST_F(TimeTest, ParseTimeTestEpoch0) { 262 Time parsed_time; 263 264 // time_t == epoch == 0 265 EXPECT_TRUE(Time::FromString("Thu Jan 01 01:00:00 +0100 1970", 266 &parsed_time)); 267 EXPECT_EQ(0, parsed_time.ToTimeT()); 268 EXPECT_TRUE(Time::FromString("Thu Jan 01 00:00:00 GMT 1970", 269 &parsed_time)); 270 EXPECT_EQ(0, parsed_time.ToTimeT()); 271 } 272 273 TEST_F(TimeTest, ParseTimeTestEpoch1) { 274 Time parsed_time; 275 276 // time_t == 1 second after epoch == 1 277 EXPECT_TRUE(Time::FromString("Thu Jan 01 01:00:01 +0100 1970", 278 &parsed_time)); 279 EXPECT_EQ(1, parsed_time.ToTimeT()); 280 EXPECT_TRUE(Time::FromString("Thu Jan 01 00:00:01 GMT 1970", 281 &parsed_time)); 282 EXPECT_EQ(1, parsed_time.ToTimeT()); 283 } 284 285 TEST_F(TimeTest, ParseTimeTestEpoch2) { 286 Time parsed_time; 287 288 // time_t == 2 seconds after epoch == 2 289 EXPECT_TRUE(Time::FromString("Thu Jan 01 01:00:02 +0100 1970", 290 &parsed_time)); 291 EXPECT_EQ(2, parsed_time.ToTimeT()); 292 EXPECT_TRUE(Time::FromString("Thu Jan 01 00:00:02 GMT 1970", 293 &parsed_time)); 294 EXPECT_EQ(2, parsed_time.ToTimeT()); 295 } 296 297 TEST_F(TimeTest, ParseTimeTestEpochNeg1) { 298 Time parsed_time; 299 300 // time_t == 1 second before epoch == -1 301 EXPECT_TRUE(Time::FromString("Thu Jan 01 00:59:59 +0100 1970", 302 &parsed_time)); 303 EXPECT_EQ(-1, parsed_time.ToTimeT()); 304 EXPECT_TRUE(Time::FromString("Wed Dec 31 23:59:59 GMT 1969", 305 &parsed_time)); 306 EXPECT_EQ(-1, parsed_time.ToTimeT()); 307 } 308 309 // If time_t is 32 bits, a date after year 2038 will overflow time_t and 310 // cause timegm() to return -1. The parsed time should not be 1 second 311 // before epoch. 312 TEST_F(TimeTest, ParseTimeTestEpochNotNeg1) { 313 Time parsed_time; 314 315 EXPECT_TRUE(Time::FromString("Wed Dec 31 23:59:59 GMT 2100", 316 &parsed_time)); 317 EXPECT_NE(-1, parsed_time.ToTimeT()); 318 } 319 320 TEST_F(TimeTest, ParseTimeTestEpochNeg2) { 321 Time parsed_time; 322 323 // time_t == 2 seconds before epoch == -2 324 EXPECT_TRUE(Time::FromString("Thu Jan 01 00:59:58 +0100 1970", 325 &parsed_time)); 326 EXPECT_EQ(-2, parsed_time.ToTimeT()); 327 EXPECT_TRUE(Time::FromString("Wed Dec 31 23:59:58 GMT 1969", 328 &parsed_time)); 329 EXPECT_EQ(-2, parsed_time.ToTimeT()); 330 } 331 332 TEST_F(TimeTest, ParseTimeTestEpoch1960) { 333 Time parsed_time; 334 335 // time_t before Epoch, in 1960 336 EXPECT_TRUE(Time::FromString("Wed Jun 29 19:40:01 +0100 1960", 337 &parsed_time)); 338 EXPECT_EQ(-299999999, parsed_time.ToTimeT()); 339 EXPECT_TRUE(Time::FromString("Wed Jun 29 18:40:01 GMT 1960", 340 &parsed_time)); 341 EXPECT_EQ(-299999999, parsed_time.ToTimeT()); 342 EXPECT_TRUE(Time::FromString("Wed Jun 29 17:40:01 GMT 1960", 343 &parsed_time)); 344 EXPECT_EQ(-300003599, parsed_time.ToTimeT()); 345 } 346 347 TEST_F(TimeTest, ParseTimeTestEmpty) { 348 Time parsed_time; 349 EXPECT_FALSE(Time::FromString("", &parsed_time)); 350 } 351 352 TEST_F(TimeTest, ParseTimeTestInvalidString) { 353 Time parsed_time; 354 EXPECT_FALSE(Time::FromString("Monday morning 2000", &parsed_time)); 355 } 356 357 TEST_F(TimeTest, ExplodeBeforeUnixEpoch) { 358 static const int kUnixEpochYear = 1970; // In case this changes (ha!). 359 Time t; 360 Time::Exploded exploded; 361 362 t = Time::UnixEpoch() - TimeDelta::FromMicroseconds(1); 363 t.UTCExplode(&exploded); 364 EXPECT_TRUE(exploded.HasValidValues()); 365 // Should be 1969-12-31 23:59:59 999 milliseconds (and 999 microseconds). 366 EXPECT_EQ(kUnixEpochYear - 1, exploded.year); 367 EXPECT_EQ(12, exploded.month); 368 EXPECT_EQ(31, exploded.day_of_month); 369 EXPECT_EQ(23, exploded.hour); 370 EXPECT_EQ(59, exploded.minute); 371 EXPECT_EQ(59, exploded.second); 372 EXPECT_EQ(999, exploded.millisecond); 373 374 t = Time::UnixEpoch() - TimeDelta::FromMicroseconds(1000); 375 t.UTCExplode(&exploded); 376 EXPECT_TRUE(exploded.HasValidValues()); 377 // Should be 1969-12-31 23:59:59 999 milliseconds. 378 EXPECT_EQ(kUnixEpochYear - 1, exploded.year); 379 EXPECT_EQ(12, exploded.month); 380 EXPECT_EQ(31, exploded.day_of_month); 381 EXPECT_EQ(23, exploded.hour); 382 EXPECT_EQ(59, exploded.minute); 383 EXPECT_EQ(59, exploded.second); 384 EXPECT_EQ(999, exploded.millisecond); 385 386 t = Time::UnixEpoch() - TimeDelta::FromMicroseconds(1001); 387 t.UTCExplode(&exploded); 388 EXPECT_TRUE(exploded.HasValidValues()); 389 // Should be 1969-12-31 23:59:59 998 milliseconds (and 999 microseconds). 390 EXPECT_EQ(kUnixEpochYear - 1, exploded.year); 391 EXPECT_EQ(12, exploded.month); 392 EXPECT_EQ(31, exploded.day_of_month); 393 EXPECT_EQ(23, exploded.hour); 394 EXPECT_EQ(59, exploded.minute); 395 EXPECT_EQ(59, exploded.second); 396 EXPECT_EQ(998, exploded.millisecond); 397 398 t = Time::UnixEpoch() - TimeDelta::FromMilliseconds(1000); 399 t.UTCExplode(&exploded); 400 EXPECT_TRUE(exploded.HasValidValues()); 401 // Should be 1969-12-31 23:59:59. 402 EXPECT_EQ(kUnixEpochYear - 1, exploded.year); 403 EXPECT_EQ(12, exploded.month); 404 EXPECT_EQ(31, exploded.day_of_month); 405 EXPECT_EQ(23, exploded.hour); 406 EXPECT_EQ(59, exploded.minute); 407 EXPECT_EQ(59, exploded.second); 408 EXPECT_EQ(0, exploded.millisecond); 409 410 t = Time::UnixEpoch() - TimeDelta::FromMilliseconds(1001); 411 t.UTCExplode(&exploded); 412 EXPECT_TRUE(exploded.HasValidValues()); 413 // Should be 1969-12-31 23:59:58 999 milliseconds. 414 EXPECT_EQ(kUnixEpochYear - 1, exploded.year); 415 EXPECT_EQ(12, exploded.month); 416 EXPECT_EQ(31, exploded.day_of_month); 417 EXPECT_EQ(23, exploded.hour); 418 EXPECT_EQ(59, exploded.minute); 419 EXPECT_EQ(58, exploded.second); 420 EXPECT_EQ(999, exploded.millisecond); 421 422 // Make sure we still handle at/after Unix epoch correctly. 423 t = Time::UnixEpoch(); 424 t.UTCExplode(&exploded); 425 EXPECT_TRUE(exploded.HasValidValues()); 426 // Should be 1970-12-31 00:00:00 0 milliseconds. 427 EXPECT_EQ(kUnixEpochYear, exploded.year); 428 EXPECT_EQ(1, exploded.month); 429 EXPECT_EQ(1, exploded.day_of_month); 430 EXPECT_EQ(0, exploded.hour); 431 EXPECT_EQ(0, exploded.minute); 432 EXPECT_EQ(0, exploded.second); 433 EXPECT_EQ(0, exploded.millisecond); 434 435 t = Time::UnixEpoch() + TimeDelta::FromMicroseconds(1); 436 t.UTCExplode(&exploded); 437 EXPECT_TRUE(exploded.HasValidValues()); 438 // Should be 1970-01-01 00:00:00 0 milliseconds (and 1 microsecond). 439 EXPECT_EQ(kUnixEpochYear, exploded.year); 440 EXPECT_EQ(1, exploded.month); 441 EXPECT_EQ(1, exploded.day_of_month); 442 EXPECT_EQ(0, exploded.hour); 443 EXPECT_EQ(0, exploded.minute); 444 EXPECT_EQ(0, exploded.second); 445 EXPECT_EQ(0, exploded.millisecond); 446 447 t = Time::UnixEpoch() + TimeDelta::FromMicroseconds(1000); 448 t.UTCExplode(&exploded); 449 EXPECT_TRUE(exploded.HasValidValues()); 450 // Should be 1970-01-01 00:00:00 1 millisecond. 451 EXPECT_EQ(kUnixEpochYear, exploded.year); 452 EXPECT_EQ(1, exploded.month); 453 EXPECT_EQ(1, exploded.day_of_month); 454 EXPECT_EQ(0, exploded.hour); 455 EXPECT_EQ(0, exploded.minute); 456 EXPECT_EQ(0, exploded.second); 457 EXPECT_EQ(1, exploded.millisecond); 458 459 t = Time::UnixEpoch() + TimeDelta::FromMilliseconds(1000); 460 t.UTCExplode(&exploded); 461 EXPECT_TRUE(exploded.HasValidValues()); 462 // Should be 1970-01-01 00:00:01. 463 EXPECT_EQ(kUnixEpochYear, exploded.year); 464 EXPECT_EQ(1, exploded.month); 465 EXPECT_EQ(1, exploded.day_of_month); 466 EXPECT_EQ(0, exploded.hour); 467 EXPECT_EQ(0, exploded.minute); 468 EXPECT_EQ(1, exploded.second); 469 EXPECT_EQ(0, exploded.millisecond); 470 471 t = Time::UnixEpoch() + TimeDelta::FromMilliseconds(1001); 472 t.UTCExplode(&exploded); 473 EXPECT_TRUE(exploded.HasValidValues()); 474 // Should be 1970-01-01 00:00:01 1 millisecond. 475 EXPECT_EQ(kUnixEpochYear, exploded.year); 476 EXPECT_EQ(1, exploded.month); 477 EXPECT_EQ(1, exploded.day_of_month); 478 EXPECT_EQ(0, exploded.hour); 479 EXPECT_EQ(0, exploded.minute); 480 EXPECT_EQ(1, exploded.second); 481 EXPECT_EQ(1, exploded.millisecond); 482 } 483 484 TEST_F(TimeTest, Max) { 485 Time max = Time::Max(); 486 EXPECT_TRUE(max.is_max()); 487 EXPECT_EQ(max, Time::Max()); 488 EXPECT_GT(max, Time::Now()); 489 EXPECT_GT(max, Time()); 490 } 491 492 TEST_F(TimeTest, MaxConversions) { 493 Time t = Time::Max(); 494 EXPECT_EQ(std::numeric_limits<int64>::max(), t.ToInternalValue()); 495 496 t = Time::FromDoubleT(std::numeric_limits<double>::max()); 497 EXPECT_TRUE(t.is_max()); 498 EXPECT_EQ(std::numeric_limits<double>::max(), t.ToDoubleT()); 499 500 t = Time::FromJsTime(std::numeric_limits<double>::max()); 501 EXPECT_TRUE(t.is_max()); 502 EXPECT_EQ(std::numeric_limits<double>::max(), t.ToJsTime()); 503 504 t = Time::FromTimeT(std::numeric_limits<time_t>::max()); 505 EXPECT_TRUE(t.is_max()); 506 EXPECT_EQ(std::numeric_limits<time_t>::max(), t.ToTimeT()); 507 508 #if defined(OS_POSIX) 509 struct timeval tval; 510 tval.tv_sec = std::numeric_limits<time_t>::max(); 511 tval.tv_usec = static_cast<suseconds_t>(Time::kMicrosecondsPerSecond) - 1; 512 t = Time::FromTimeVal(tval); 513 EXPECT_TRUE(t.is_max()); 514 tval = t.ToTimeVal(); 515 EXPECT_EQ(std::numeric_limits<time_t>::max(), tval.tv_sec); 516 EXPECT_EQ(static_cast<suseconds_t>(Time::kMicrosecondsPerSecond) - 1, 517 tval.tv_usec); 518 #endif 519 520 #if defined(OS_MACOSX) 521 t = Time::FromCFAbsoluteTime(std::numeric_limits<CFAbsoluteTime>::max()); 522 EXPECT_TRUE(t.is_max()); 523 EXPECT_EQ(std::numeric_limits<CFAbsoluteTime>::max(), t.ToCFAbsoluteTime()); 524 #endif 525 526 #if defined(OS_WIN) 527 FILETIME ftime; 528 ftime.dwHighDateTime = std::numeric_limits<DWORD>::max(); 529 ftime.dwLowDateTime = std::numeric_limits<DWORD>::max(); 530 t = Time::FromFileTime(ftime); 531 EXPECT_TRUE(t.is_max()); 532 ftime = t.ToFileTime(); 533 EXPECT_EQ(std::numeric_limits<DWORD>::max(), ftime.dwHighDateTime); 534 EXPECT_EQ(std::numeric_limits<DWORD>::max(), ftime.dwLowDateTime); 535 #endif 536 } 537 538 #if defined(OS_MACOSX) 539 TEST_F(TimeTest, TimeTOverflow) { 540 Time t = Time::FromInternalValue(std::numeric_limits<int64>::max() - 1); 541 EXPECT_FALSE(t.is_max()); 542 EXPECT_EQ(std::numeric_limits<time_t>::max(), t.ToTimeT()); 543 } 544 #endif 545 546 #if defined(OS_ANDROID) 547 TEST_F(TimeTest, FromLocalExplodedCrashOnAndroid) { 548 // This crashed inside Time:: FromLocalExploded() on Android 4.1.2. 549 // See http://crbug.com/287821 550 Time::Exploded midnight = {2013, // year 551 10, // month 552 0, // day_of_week 553 13, // day_of_month 554 0, // hour 555 0, // minute 556 0, // second 557 }; 558 // The string passed to putenv() must be a char* and the documentation states 559 // that it 'becomes part of the environment', so use a static buffer. 560 static char buffer[] = "TZ=America/Santiago"; 561 putenv(buffer); 562 tzset(); 563 Time t = Time::FromLocalExploded(midnight); 564 EXPECT_EQ(1381633200, t.ToTimeT()); 565 } 566 #endif // OS_ANDROID 567 568 TEST(TimeTicks, Deltas) { 569 for (int index = 0; index < 50; index++) { 570 TimeTicks ticks_start = TimeTicks::Now(); 571 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(10)); 572 TimeTicks ticks_stop = TimeTicks::Now(); 573 TimeDelta delta = ticks_stop - ticks_start; 574 // Note: Although we asked for a 10ms sleep, if the 575 // time clock has a finer granularity than the Sleep() 576 // clock, it is quite possible to wakeup early. Here 577 // is how that works: 578 // Time(ms timer) Time(us timer) 579 // 5 5010 580 // 6 6010 581 // 7 7010 582 // 8 8010 583 // 9 9000 584 // Elapsed 4ms 3990us 585 // 586 // Unfortunately, our InMilliseconds() function truncates 587 // rather than rounds. We should consider fixing this 588 // so that our averages come out better. 589 EXPECT_GE(delta.InMilliseconds(), 9); 590 EXPECT_GE(delta.InMicroseconds(), 9000); 591 EXPECT_EQ(delta.InSeconds(), 0); 592 } 593 } 594 595 static void HighResClockTest(TimeTicks (*GetTicks)()) { 596 #if defined(OS_WIN) 597 // HighResNow doesn't work on some systems. Since the product still works 598 // even if it doesn't work, it makes this entire test questionable. 599 if (!TimeTicks::IsHighResClockWorking()) 600 return; 601 #endif 602 603 // Why do we loop here? 604 // We're trying to measure that intervals increment in a VERY small amount 605 // of time -- less than 15ms. Unfortunately, if we happen to have a 606 // context switch in the middle of our test, the context switch could easily 607 // exceed our limit. So, we iterate on this several times. As long as we're 608 // able to detect the fine-granularity timers at least once, then the test 609 // has succeeded. 610 611 const int kTargetGranularityUs = 15000; // 15ms 612 613 bool success = false; 614 int retries = 100; // Arbitrary. 615 TimeDelta delta; 616 while (!success && retries--) { 617 TimeTicks ticks_start = GetTicks(); 618 // Loop until we can detect that the clock has changed. Non-HighRes timers 619 // will increment in chunks, e.g. 15ms. By spinning until we see a clock 620 // change, we detect the minimum time between measurements. 621 do { 622 delta = GetTicks() - ticks_start; 623 } while (delta.InMilliseconds() == 0); 624 625 if (delta.InMicroseconds() <= kTargetGranularityUs) 626 success = true; 627 } 628 629 // In high resolution mode, we expect to see the clock increment 630 // in intervals less than 15ms. 631 EXPECT_TRUE(success); 632 } 633 634 TEST(TimeTicks, HighResNow) { 635 HighResClockTest(&TimeTicks::HighResNow); 636 } 637 638 TEST(TimeTicks, ThreadNow) { 639 if (TimeTicks::IsThreadNowSupported()) { 640 TimeTicks begin = TimeTicks::Now(); 641 TimeTicks begin_thread = TimeTicks::ThreadNow(); 642 // Make sure that ThreadNow value is non-zero. 643 EXPECT_GT(begin_thread, TimeTicks()); 644 // Sleep for 10 milliseconds to get the thread de-scheduled. 645 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(10)); 646 TimeTicks end_thread = TimeTicks::ThreadNow(); 647 TimeTicks end = TimeTicks::Now(); 648 TimeDelta delta = end - begin; 649 TimeDelta delta_thread = end_thread - begin_thread; 650 // Make sure that some thread time have elapsed. 651 EXPECT_GT(delta_thread.InMicroseconds(), 0); 652 // But the thread time is at least 9ms less than clock time. 653 TimeDelta difference = delta - delta_thread; 654 EXPECT_GE(difference.InMicroseconds(), 9000); 655 } 656 } 657 658 TEST(TimeTicks, NowFromSystemTraceTime) { 659 // Re-use HighResNow test for now since clock properties are identical. 660 HighResClockTest(&TimeTicks::NowFromSystemTraceTime); 661 } 662 663 TEST(TimeDelta, FromAndIn) { 664 EXPECT_TRUE(TimeDelta::FromDays(2) == TimeDelta::FromHours(48)); 665 EXPECT_TRUE(TimeDelta::FromHours(3) == TimeDelta::FromMinutes(180)); 666 EXPECT_TRUE(TimeDelta::FromMinutes(2) == TimeDelta::FromSeconds(120)); 667 EXPECT_TRUE(TimeDelta::FromSeconds(2) == TimeDelta::FromMilliseconds(2000)); 668 EXPECT_TRUE(TimeDelta::FromMilliseconds(2) == 669 TimeDelta::FromMicroseconds(2000)); 670 EXPECT_EQ(13, TimeDelta::FromDays(13).InDays()); 671 EXPECT_EQ(13, TimeDelta::FromHours(13).InHours()); 672 EXPECT_EQ(13, TimeDelta::FromMinutes(13).InMinutes()); 673 EXPECT_EQ(13, TimeDelta::FromSeconds(13).InSeconds()); 674 EXPECT_EQ(13.0, TimeDelta::FromSeconds(13).InSecondsF()); 675 EXPECT_EQ(13, TimeDelta::FromMilliseconds(13).InMilliseconds()); 676 EXPECT_EQ(13.0, TimeDelta::FromMilliseconds(13).InMillisecondsF()); 677 EXPECT_EQ(13, TimeDelta::FromMicroseconds(13).InMicroseconds()); 678 } 679 680 #if defined(OS_POSIX) 681 TEST(TimeDelta, TimeSpecConversion) { 682 struct timespec result = TimeDelta::FromSeconds(0).ToTimeSpec(); 683 EXPECT_EQ(result.tv_sec, 0); 684 EXPECT_EQ(result.tv_nsec, 0); 685 686 result = TimeDelta::FromSeconds(1).ToTimeSpec(); 687 EXPECT_EQ(result.tv_sec, 1); 688 EXPECT_EQ(result.tv_nsec, 0); 689 690 result = TimeDelta::FromMicroseconds(1).ToTimeSpec(); 691 EXPECT_EQ(result.tv_sec, 0); 692 EXPECT_EQ(result.tv_nsec, 1000); 693 694 result = TimeDelta::FromMicroseconds( 695 Time::kMicrosecondsPerSecond + 1).ToTimeSpec(); 696 EXPECT_EQ(result.tv_sec, 1); 697 EXPECT_EQ(result.tv_nsec, 1000); 698 } 699 #endif // OS_POSIX 700 701 // Our internal time format is serialized in things like databases, so it's 702 // important that it's consistent across all our platforms. We use the 1601 703 // Windows epoch as the internal format across all platforms. 704 TEST(TimeDelta, WindowsEpoch) { 705 Time::Exploded exploded; 706 exploded.year = 1970; 707 exploded.month = 1; 708 exploded.day_of_week = 0; // Should be unusued. 709 exploded.day_of_month = 1; 710 exploded.hour = 0; 711 exploded.minute = 0; 712 exploded.second = 0; 713 exploded.millisecond = 0; 714 Time t = Time::FromUTCExploded(exploded); 715 // Unix 1970 epoch. 716 EXPECT_EQ(GG_INT64_C(11644473600000000), t.ToInternalValue()); 717 718 // We can't test 1601 epoch, since the system time functions on Linux 719 // only compute years starting from 1900. 720 } 721