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