Made WebRTC tests report JS errors better.
[chromium-blink-merge.git] / base / time / time_unittest.cc
blobef80e43859303c43eac80aef93b475db49883909
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/time.h"
7 #include <time.h>
9 #include <string>
11 #include "base/compiler_specific.h"
12 #include "base/logging.h"
13 #include "base/strings/stringprintf.h"
14 #include "base/threading/platform_thread.h"
15 #include "build/build_config.h"
16 #include "testing/gtest/include/gtest/gtest.h"
18 namespace base {
20 namespace {
22 // Specialized test fixture allowing time strings without timezones to be
23 // tested by comparing them to a known time in the local zone.
24 // See also pr_time_unittests.cc
25 class TimeTest : public testing::Test {
26 protected:
27 virtual void SetUp() override {
28 // Use mktime to get a time_t, and turn it into a PRTime by converting
29 // seconds to microseconds. Use 15th Oct 2007 12:45:00 local. This
30 // must be a time guaranteed to be outside of a DST fallback hour in
31 // any timezone.
32 struct tm local_comparison_tm = {
33 0, // second
34 45, // minute
35 12, // hour
36 15, // day of month
37 10 - 1, // month
38 2007 - 1900, // year
39 0, // day of week (ignored, output only)
40 0, // day of year (ignored, output only)
41 -1 // DST in effect, -1 tells mktime to figure it out
44 time_t converted_time = mktime(&local_comparison_tm);
45 ASSERT_GT(converted_time, 0);
46 comparison_time_local_ = Time::FromTimeT(converted_time);
48 // time_t representation of 15th Oct 2007 12:45:00 PDT
49 comparison_time_pdt_ = Time::FromTimeT(1192477500);
52 Time comparison_time_local_;
53 Time comparison_time_pdt_;
56 // Test conversions to/from time_t and exploding/unexploding.
57 TEST_F(TimeTest, TimeT) {
58 // C library time and exploded time.
59 time_t now_t_1 = time(NULL);
60 struct tm tms;
61 #if defined(OS_WIN)
62 localtime_s(&tms, &now_t_1);
63 #elif defined(OS_POSIX)
64 localtime_r(&now_t_1, &tms);
65 #endif
67 // Convert to ours.
68 Time our_time_1 = Time::FromTimeT(now_t_1);
69 Time::Exploded exploded;
70 our_time_1.LocalExplode(&exploded);
72 // This will test both our exploding and our time_t -> Time conversion.
73 EXPECT_EQ(tms.tm_year + 1900, exploded.year);
74 EXPECT_EQ(tms.tm_mon + 1, exploded.month);
75 EXPECT_EQ(tms.tm_mday, exploded.day_of_month);
76 EXPECT_EQ(tms.tm_hour, exploded.hour);
77 EXPECT_EQ(tms.tm_min, exploded.minute);
78 EXPECT_EQ(tms.tm_sec, exploded.second);
80 // Convert exploded back to the time struct.
81 Time our_time_2 = Time::FromLocalExploded(exploded);
82 EXPECT_TRUE(our_time_1 == our_time_2);
84 time_t now_t_2 = our_time_2.ToTimeT();
85 EXPECT_EQ(now_t_1, now_t_2);
87 EXPECT_EQ(10, Time().FromTimeT(10).ToTimeT());
88 EXPECT_EQ(10.0, Time().FromTimeT(10).ToDoubleT());
90 // Conversions of 0 should stay 0.
91 EXPECT_EQ(0, Time().ToTimeT());
92 EXPECT_EQ(0, Time::FromTimeT(0).ToInternalValue());
95 // Test conversions to/from javascript time.
96 TEST_F(TimeTest, JsTime) {
97 Time epoch = Time::FromJsTime(0.0);
98 EXPECT_EQ(epoch, Time::UnixEpoch());
99 Time t = Time::FromJsTime(700000.3);
100 EXPECT_EQ(700.0003, t.ToDoubleT());
101 t = Time::FromDoubleT(800.73);
102 EXPECT_EQ(800730.0, t.ToJsTime());
105 #if defined(OS_POSIX)
106 TEST_F(TimeTest, FromTimeVal) {
107 Time now = Time::Now();
108 Time also_now = Time::FromTimeVal(now.ToTimeVal());
109 EXPECT_EQ(now, also_now);
111 #endif // OS_POSIX
113 TEST_F(TimeTest, FromExplodedWithMilliseconds) {
114 // Some platform implementations of FromExploded are liable to drop
115 // milliseconds if we aren't careful.
116 Time now = Time::NowFromSystemTime();
117 Time::Exploded exploded1 = {0};
118 now.UTCExplode(&exploded1);
119 exploded1.millisecond = 500;
120 Time time = Time::FromUTCExploded(exploded1);
121 Time::Exploded exploded2 = {0};
122 time.UTCExplode(&exploded2);
123 EXPECT_EQ(exploded1.millisecond, exploded2.millisecond);
126 TEST_F(TimeTest, ZeroIsSymmetric) {
127 Time zero_time(Time::FromTimeT(0));
128 EXPECT_EQ(0, zero_time.ToTimeT());
130 EXPECT_EQ(0.0, zero_time.ToDoubleT());
133 TEST_F(TimeTest, LocalExplode) {
134 Time a = Time::Now();
135 Time::Exploded exploded;
136 a.LocalExplode(&exploded);
138 Time b = Time::FromLocalExploded(exploded);
140 // The exploded structure doesn't have microseconds, and on Mac & Linux, the
141 // internal OS conversion uses seconds, which will cause truncation. So we
142 // can only make sure that the delta is within one second.
143 EXPECT_TRUE((a - b) < TimeDelta::FromSeconds(1));
146 TEST_F(TimeTest, UTCExplode) {
147 Time a = Time::Now();
148 Time::Exploded exploded;
149 a.UTCExplode(&exploded);
151 Time b = Time::FromUTCExploded(exploded);
152 EXPECT_TRUE((a - b) < TimeDelta::FromSeconds(1));
155 TEST_F(TimeTest, LocalMidnight) {
156 Time::Exploded exploded;
157 Time::Now().LocalMidnight().LocalExplode(&exploded);
158 EXPECT_EQ(0, exploded.hour);
159 EXPECT_EQ(0, exploded.minute);
160 EXPECT_EQ(0, exploded.second);
161 EXPECT_EQ(0, exploded.millisecond);
164 TEST_F(TimeTest, ParseTimeTest1) {
165 time_t current_time = 0;
166 time(&current_time);
168 const int BUFFER_SIZE = 64;
169 struct tm local_time = {0};
170 char time_buf[BUFFER_SIZE] = {0};
171 #if defined(OS_WIN)
172 localtime_s(&local_time, &current_time);
173 asctime_s(time_buf, arraysize(time_buf), &local_time);
174 #elif defined(OS_POSIX)
175 localtime_r(&current_time, &local_time);
176 asctime_r(&local_time, time_buf);
177 #endif
179 Time parsed_time;
180 EXPECT_TRUE(Time::FromString(time_buf, &parsed_time));
181 EXPECT_EQ(current_time, parsed_time.ToTimeT());
184 TEST_F(TimeTest, DayOfWeekSunday) {
185 Time time;
186 EXPECT_TRUE(Time::FromString("Sun, 06 May 2012 12:00:00 GMT", &time));
187 Time::Exploded exploded;
188 time.UTCExplode(&exploded);
189 EXPECT_EQ(0, exploded.day_of_week);
192 TEST_F(TimeTest, DayOfWeekWednesday) {
193 Time time;
194 EXPECT_TRUE(Time::FromString("Wed, 09 May 2012 12:00:00 GMT", &time));
195 Time::Exploded exploded;
196 time.UTCExplode(&exploded);
197 EXPECT_EQ(3, exploded.day_of_week);
200 TEST_F(TimeTest, DayOfWeekSaturday) {
201 Time time;
202 EXPECT_TRUE(Time::FromString("Sat, 12 May 2012 12:00:00 GMT", &time));
203 Time::Exploded exploded;
204 time.UTCExplode(&exploded);
205 EXPECT_EQ(6, exploded.day_of_week);
208 TEST_F(TimeTest, ParseTimeTest2) {
209 Time parsed_time;
210 EXPECT_TRUE(Time::FromString("Mon, 15 Oct 2007 19:45:00 GMT", &parsed_time));
211 EXPECT_EQ(comparison_time_pdt_, parsed_time);
214 TEST_F(TimeTest, ParseTimeTest3) {
215 Time parsed_time;
216 EXPECT_TRUE(Time::FromString("15 Oct 07 12:45:00", &parsed_time));
217 EXPECT_EQ(comparison_time_local_, parsed_time);
220 TEST_F(TimeTest, ParseTimeTest4) {
221 Time parsed_time;
222 EXPECT_TRUE(Time::FromString("15 Oct 07 19:45 GMT", &parsed_time));
223 EXPECT_EQ(comparison_time_pdt_, parsed_time);
226 TEST_F(TimeTest, ParseTimeTest5) {
227 Time parsed_time;
228 EXPECT_TRUE(Time::FromString("Mon Oct 15 12:45 PDT 2007", &parsed_time));
229 EXPECT_EQ(comparison_time_pdt_, parsed_time);
232 TEST_F(TimeTest, ParseTimeTest6) {
233 Time parsed_time;
234 EXPECT_TRUE(Time::FromString("Monday, Oct 15, 2007 12:45 PM", &parsed_time));
235 EXPECT_EQ(comparison_time_local_, parsed_time);
238 TEST_F(TimeTest, ParseTimeTest7) {
239 Time parsed_time;
240 EXPECT_TRUE(Time::FromString("10/15/07 12:45:00 PM", &parsed_time));
241 EXPECT_EQ(comparison_time_local_, parsed_time);
244 TEST_F(TimeTest, ParseTimeTest8) {
245 Time parsed_time;
246 EXPECT_TRUE(Time::FromString("15-OCT-2007 12:45pm", &parsed_time));
247 EXPECT_EQ(comparison_time_local_, parsed_time);
250 TEST_F(TimeTest, ParseTimeTest9) {
251 Time parsed_time;
252 EXPECT_TRUE(Time::FromString("16 Oct 2007 4:45-JST (Tuesday)", &parsed_time));
253 EXPECT_EQ(comparison_time_pdt_, parsed_time);
256 TEST_F(TimeTest, ParseTimeTest10) {
257 Time parsed_time;
258 EXPECT_TRUE(Time::FromString("15/10/07 12:45", &parsed_time));
259 EXPECT_EQ(parsed_time, comparison_time_local_);
262 // Test some of edge cases around epoch, etc.
263 TEST_F(TimeTest, ParseTimeTestEpoch0) {
264 Time parsed_time;
266 // time_t == epoch == 0
267 EXPECT_TRUE(Time::FromString("Thu Jan 01 01:00:00 +0100 1970",
268 &parsed_time));
269 EXPECT_EQ(0, parsed_time.ToTimeT());
270 EXPECT_TRUE(Time::FromString("Thu Jan 01 00:00:00 GMT 1970",
271 &parsed_time));
272 EXPECT_EQ(0, parsed_time.ToTimeT());
275 TEST_F(TimeTest, ParseTimeTestEpoch1) {
276 Time parsed_time;
278 // time_t == 1 second after epoch == 1
279 EXPECT_TRUE(Time::FromString("Thu Jan 01 01:00:01 +0100 1970",
280 &parsed_time));
281 EXPECT_EQ(1, parsed_time.ToTimeT());
282 EXPECT_TRUE(Time::FromString("Thu Jan 01 00:00:01 GMT 1970",
283 &parsed_time));
284 EXPECT_EQ(1, parsed_time.ToTimeT());
287 TEST_F(TimeTest, ParseTimeTestEpoch2) {
288 Time parsed_time;
290 // time_t == 2 seconds after epoch == 2
291 EXPECT_TRUE(Time::FromString("Thu Jan 01 01:00:02 +0100 1970",
292 &parsed_time));
293 EXPECT_EQ(2, parsed_time.ToTimeT());
294 EXPECT_TRUE(Time::FromString("Thu Jan 01 00:00:02 GMT 1970",
295 &parsed_time));
296 EXPECT_EQ(2, parsed_time.ToTimeT());
299 TEST_F(TimeTest, ParseTimeTestEpochNeg1) {
300 Time parsed_time;
302 // time_t == 1 second before epoch == -1
303 EXPECT_TRUE(Time::FromString("Thu Jan 01 00:59:59 +0100 1970",
304 &parsed_time));
305 EXPECT_EQ(-1, parsed_time.ToTimeT());
306 EXPECT_TRUE(Time::FromString("Wed Dec 31 23:59:59 GMT 1969",
307 &parsed_time));
308 EXPECT_EQ(-1, parsed_time.ToTimeT());
311 // If time_t is 32 bits, a date after year 2038 will overflow time_t and
312 // cause timegm() to return -1. The parsed time should not be 1 second
313 // before epoch.
314 TEST_F(TimeTest, ParseTimeTestEpochNotNeg1) {
315 Time parsed_time;
317 EXPECT_TRUE(Time::FromString("Wed Dec 31 23:59:59 GMT 2100",
318 &parsed_time));
319 EXPECT_NE(-1, parsed_time.ToTimeT());
322 TEST_F(TimeTest, ParseTimeTestEpochNeg2) {
323 Time parsed_time;
325 // time_t == 2 seconds before epoch == -2
326 EXPECT_TRUE(Time::FromString("Thu Jan 01 00:59:58 +0100 1970",
327 &parsed_time));
328 EXPECT_EQ(-2, parsed_time.ToTimeT());
329 EXPECT_TRUE(Time::FromString("Wed Dec 31 23:59:58 GMT 1969",
330 &parsed_time));
331 EXPECT_EQ(-2, parsed_time.ToTimeT());
334 TEST_F(TimeTest, ParseTimeTestEpoch1960) {
335 Time parsed_time;
337 // time_t before Epoch, in 1960
338 EXPECT_TRUE(Time::FromString("Wed Jun 29 19:40:01 +0100 1960",
339 &parsed_time));
340 EXPECT_EQ(-299999999, parsed_time.ToTimeT());
341 EXPECT_TRUE(Time::FromString("Wed Jun 29 18:40:01 GMT 1960",
342 &parsed_time));
343 EXPECT_EQ(-299999999, parsed_time.ToTimeT());
344 EXPECT_TRUE(Time::FromString("Wed Jun 29 17:40:01 GMT 1960",
345 &parsed_time));
346 EXPECT_EQ(-300003599, parsed_time.ToTimeT());
349 TEST_F(TimeTest, ParseTimeTestEmpty) {
350 Time parsed_time;
351 EXPECT_FALSE(Time::FromString("", &parsed_time));
354 TEST_F(TimeTest, ParseTimeTestInvalidString) {
355 Time parsed_time;
356 EXPECT_FALSE(Time::FromString("Monday morning 2000", &parsed_time));
359 TEST_F(TimeTest, ExplodeBeforeUnixEpoch) {
360 static const int kUnixEpochYear = 1970; // In case this changes (ha!).
361 Time t;
362 Time::Exploded exploded;
364 t = Time::UnixEpoch() - TimeDelta::FromMicroseconds(1);
365 t.UTCExplode(&exploded);
366 EXPECT_TRUE(exploded.HasValidValues());
367 // Should be 1969-12-31 23:59:59 999 milliseconds (and 999 microseconds).
368 EXPECT_EQ(kUnixEpochYear - 1, exploded.year);
369 EXPECT_EQ(12, exploded.month);
370 EXPECT_EQ(31, exploded.day_of_month);
371 EXPECT_EQ(23, exploded.hour);
372 EXPECT_EQ(59, exploded.minute);
373 EXPECT_EQ(59, exploded.second);
374 EXPECT_EQ(999, exploded.millisecond);
376 t = Time::UnixEpoch() - TimeDelta::FromMicroseconds(1000);
377 t.UTCExplode(&exploded);
378 EXPECT_TRUE(exploded.HasValidValues());
379 // Should be 1969-12-31 23:59:59 999 milliseconds.
380 EXPECT_EQ(kUnixEpochYear - 1, exploded.year);
381 EXPECT_EQ(12, exploded.month);
382 EXPECT_EQ(31, exploded.day_of_month);
383 EXPECT_EQ(23, exploded.hour);
384 EXPECT_EQ(59, exploded.minute);
385 EXPECT_EQ(59, exploded.second);
386 EXPECT_EQ(999, exploded.millisecond);
388 t = Time::UnixEpoch() - TimeDelta::FromMicroseconds(1001);
389 t.UTCExplode(&exploded);
390 EXPECT_TRUE(exploded.HasValidValues());
391 // Should be 1969-12-31 23:59:59 998 milliseconds (and 999 microseconds).
392 EXPECT_EQ(kUnixEpochYear - 1, exploded.year);
393 EXPECT_EQ(12, exploded.month);
394 EXPECT_EQ(31, exploded.day_of_month);
395 EXPECT_EQ(23, exploded.hour);
396 EXPECT_EQ(59, exploded.minute);
397 EXPECT_EQ(59, exploded.second);
398 EXPECT_EQ(998, exploded.millisecond);
400 t = Time::UnixEpoch() - TimeDelta::FromMilliseconds(1000);
401 t.UTCExplode(&exploded);
402 EXPECT_TRUE(exploded.HasValidValues());
403 // Should be 1969-12-31 23:59:59.
404 EXPECT_EQ(kUnixEpochYear - 1, exploded.year);
405 EXPECT_EQ(12, exploded.month);
406 EXPECT_EQ(31, exploded.day_of_month);
407 EXPECT_EQ(23, exploded.hour);
408 EXPECT_EQ(59, exploded.minute);
409 EXPECT_EQ(59, exploded.second);
410 EXPECT_EQ(0, exploded.millisecond);
412 t = Time::UnixEpoch() - TimeDelta::FromMilliseconds(1001);
413 t.UTCExplode(&exploded);
414 EXPECT_TRUE(exploded.HasValidValues());
415 // Should be 1969-12-31 23:59:58 999 milliseconds.
416 EXPECT_EQ(kUnixEpochYear - 1, exploded.year);
417 EXPECT_EQ(12, exploded.month);
418 EXPECT_EQ(31, exploded.day_of_month);
419 EXPECT_EQ(23, exploded.hour);
420 EXPECT_EQ(59, exploded.minute);
421 EXPECT_EQ(58, exploded.second);
422 EXPECT_EQ(999, exploded.millisecond);
424 // Make sure we still handle at/after Unix epoch correctly.
425 t = Time::UnixEpoch();
426 t.UTCExplode(&exploded);
427 EXPECT_TRUE(exploded.HasValidValues());
428 // Should be 1970-12-31 00:00:00 0 milliseconds.
429 EXPECT_EQ(kUnixEpochYear, exploded.year);
430 EXPECT_EQ(1, exploded.month);
431 EXPECT_EQ(1, exploded.day_of_month);
432 EXPECT_EQ(0, exploded.hour);
433 EXPECT_EQ(0, exploded.minute);
434 EXPECT_EQ(0, exploded.second);
435 EXPECT_EQ(0, exploded.millisecond);
437 t = Time::UnixEpoch() + TimeDelta::FromMicroseconds(1);
438 t.UTCExplode(&exploded);
439 EXPECT_TRUE(exploded.HasValidValues());
440 // Should be 1970-01-01 00:00:00 0 milliseconds (and 1 microsecond).
441 EXPECT_EQ(kUnixEpochYear, exploded.year);
442 EXPECT_EQ(1, exploded.month);
443 EXPECT_EQ(1, exploded.day_of_month);
444 EXPECT_EQ(0, exploded.hour);
445 EXPECT_EQ(0, exploded.minute);
446 EXPECT_EQ(0, exploded.second);
447 EXPECT_EQ(0, exploded.millisecond);
449 t = Time::UnixEpoch() + TimeDelta::FromMicroseconds(1000);
450 t.UTCExplode(&exploded);
451 EXPECT_TRUE(exploded.HasValidValues());
452 // Should be 1970-01-01 00:00:00 1 millisecond.
453 EXPECT_EQ(kUnixEpochYear, exploded.year);
454 EXPECT_EQ(1, exploded.month);
455 EXPECT_EQ(1, exploded.day_of_month);
456 EXPECT_EQ(0, exploded.hour);
457 EXPECT_EQ(0, exploded.minute);
458 EXPECT_EQ(0, exploded.second);
459 EXPECT_EQ(1, exploded.millisecond);
461 t = Time::UnixEpoch() + TimeDelta::FromMilliseconds(1000);
462 t.UTCExplode(&exploded);
463 EXPECT_TRUE(exploded.HasValidValues());
464 // Should be 1970-01-01 00:00:01.
465 EXPECT_EQ(kUnixEpochYear, exploded.year);
466 EXPECT_EQ(1, exploded.month);
467 EXPECT_EQ(1, exploded.day_of_month);
468 EXPECT_EQ(0, exploded.hour);
469 EXPECT_EQ(0, exploded.minute);
470 EXPECT_EQ(1, exploded.second);
471 EXPECT_EQ(0, exploded.millisecond);
473 t = Time::UnixEpoch() + TimeDelta::FromMilliseconds(1001);
474 t.UTCExplode(&exploded);
475 EXPECT_TRUE(exploded.HasValidValues());
476 // Should be 1970-01-01 00:00:01 1 millisecond.
477 EXPECT_EQ(kUnixEpochYear, exploded.year);
478 EXPECT_EQ(1, exploded.month);
479 EXPECT_EQ(1, exploded.day_of_month);
480 EXPECT_EQ(0, exploded.hour);
481 EXPECT_EQ(0, exploded.minute);
482 EXPECT_EQ(1, exploded.second);
483 EXPECT_EQ(1, exploded.millisecond);
486 TEST_F(TimeTest, TimeDeltaMax) {
487 TimeDelta max = TimeDelta::Max();
488 EXPECT_TRUE(max.is_max());
489 EXPECT_EQ(max, TimeDelta::Max());
490 EXPECT_GT(max, TimeDelta::FromDays(100 * 365));
491 EXPECT_GT(max, TimeDelta());
494 TEST_F(TimeTest, TimeDeltaMaxConversions) {
495 TimeDelta t = TimeDelta::Max();
496 EXPECT_EQ(std::numeric_limits<int64>::max(), t.ToInternalValue());
498 EXPECT_EQ(std::numeric_limits<int>::max(), t.InDays());
499 EXPECT_EQ(std::numeric_limits<int>::max(), t.InHours());
500 EXPECT_EQ(std::numeric_limits<int>::max(), t.InMinutes());
501 EXPECT_EQ(std::numeric_limits<double>::infinity(), t.InSecondsF());
502 EXPECT_EQ(std::numeric_limits<int64>::max(), t.InSeconds());
503 EXPECT_EQ(std::numeric_limits<double>::infinity(), t.InMillisecondsF());
504 EXPECT_EQ(std::numeric_limits<int64>::max(), t.InMilliseconds());
505 EXPECT_EQ(std::numeric_limits<int64>::max(), t.InMillisecondsRoundedUp());
507 t = TimeDelta::FromDays(std::numeric_limits<int>::max());
508 EXPECT_TRUE(t.is_max());
510 t = TimeDelta::FromHours(std::numeric_limits<int>::max());
511 EXPECT_TRUE(t.is_max());
513 t = TimeDelta::FromMinutes(std::numeric_limits<int>::max());
514 EXPECT_TRUE(t.is_max());
516 t = TimeDelta::FromSeconds(std::numeric_limits<int64>::max());
517 EXPECT_TRUE(t.is_max());
519 t = TimeDelta::FromMilliseconds(std::numeric_limits<int64>::max());
520 EXPECT_TRUE(t.is_max());
522 t = TimeDelta::FromSecondsD(std::numeric_limits<double>::infinity());
523 EXPECT_TRUE(t.is_max());
525 t = TimeDelta::FromMillisecondsD(std::numeric_limits<double>::infinity());
526 EXPECT_TRUE(t.is_max());
528 t = TimeDelta::FromMicroseconds(std::numeric_limits<int64>::max());
529 EXPECT_TRUE(t.is_max());
532 TEST_F(TimeTest, Max) {
533 Time max = Time::Max();
534 EXPECT_TRUE(max.is_max());
535 EXPECT_EQ(max, Time::Max());
536 EXPECT_GT(max, Time::Now());
537 EXPECT_GT(max, Time());
540 TEST_F(TimeTest, MaxConversions) {
541 Time t = Time::Max();
542 EXPECT_EQ(std::numeric_limits<int64>::max(), t.ToInternalValue());
544 t = Time::FromDoubleT(std::numeric_limits<double>::infinity());
545 EXPECT_TRUE(t.is_max());
546 EXPECT_EQ(std::numeric_limits<double>::infinity(), t.ToDoubleT());
548 t = Time::FromJsTime(std::numeric_limits<double>::infinity());
549 EXPECT_TRUE(t.is_max());
550 EXPECT_EQ(std::numeric_limits<double>::infinity(), t.ToJsTime());
552 t = Time::FromTimeT(std::numeric_limits<time_t>::max());
553 EXPECT_TRUE(t.is_max());
554 EXPECT_EQ(std::numeric_limits<time_t>::max(), t.ToTimeT());
556 #if defined(OS_POSIX)
557 struct timeval tval;
558 tval.tv_sec = std::numeric_limits<time_t>::max();
559 tval.tv_usec = static_cast<suseconds_t>(Time::kMicrosecondsPerSecond) - 1;
560 t = Time::FromTimeVal(tval);
561 EXPECT_TRUE(t.is_max());
562 tval = t.ToTimeVal();
563 EXPECT_EQ(std::numeric_limits<time_t>::max(), tval.tv_sec);
564 EXPECT_EQ(static_cast<suseconds_t>(Time::kMicrosecondsPerSecond) - 1,
565 tval.tv_usec);
566 #endif
568 #if defined(OS_MACOSX)
569 t = Time::FromCFAbsoluteTime(std::numeric_limits<CFAbsoluteTime>::infinity());
570 EXPECT_TRUE(t.is_max());
571 EXPECT_EQ(std::numeric_limits<CFAbsoluteTime>::infinity(),
572 t.ToCFAbsoluteTime());
573 #endif
575 #if defined(OS_WIN)
576 FILETIME ftime;
577 ftime.dwHighDateTime = std::numeric_limits<DWORD>::max();
578 ftime.dwLowDateTime = std::numeric_limits<DWORD>::max();
579 t = Time::FromFileTime(ftime);
580 EXPECT_TRUE(t.is_max());
581 ftime = t.ToFileTime();
582 EXPECT_EQ(std::numeric_limits<DWORD>::max(), ftime.dwHighDateTime);
583 EXPECT_EQ(std::numeric_limits<DWORD>::max(), ftime.dwLowDateTime);
584 #endif
587 #if defined(OS_MACOSX)
588 TEST_F(TimeTest, TimeTOverflow) {
589 Time t = Time::FromInternalValue(std::numeric_limits<int64>::max() - 1);
590 EXPECT_FALSE(t.is_max());
591 EXPECT_EQ(std::numeric_limits<time_t>::max(), t.ToTimeT());
593 #endif
595 #if defined(OS_ANDROID)
596 TEST_F(TimeTest, FromLocalExplodedCrashOnAndroid) {
597 // This crashed inside Time:: FromLocalExploded() on Android 4.1.2.
598 // See http://crbug.com/287821
599 Time::Exploded midnight = {2013, // year
600 10, // month
601 0, // day_of_week
602 13, // day_of_month
603 0, // hour
604 0, // minute
605 0, // second
607 // The string passed to putenv() must be a char* and the documentation states
608 // that it 'becomes part of the environment', so use a static buffer.
609 static char buffer[] = "TZ=America/Santiago";
610 putenv(buffer);
611 tzset();
612 Time t = Time::FromLocalExploded(midnight);
613 EXPECT_EQ(1381633200, t.ToTimeT());
615 #endif // OS_ANDROID
617 TEST(TimeTicks, Deltas) {
618 for (int index = 0; index < 50; index++) {
619 TimeTicks ticks_start = TimeTicks::Now();
620 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(10));
621 TimeTicks ticks_stop = TimeTicks::Now();
622 TimeDelta delta = ticks_stop - ticks_start;
623 // Note: Although we asked for a 10ms sleep, if the
624 // time clock has a finer granularity than the Sleep()
625 // clock, it is quite possible to wakeup early. Here
626 // is how that works:
627 // Time(ms timer) Time(us timer)
628 // 5 5010
629 // 6 6010
630 // 7 7010
631 // 8 8010
632 // 9 9000
633 // Elapsed 4ms 3990us
635 // Unfortunately, our InMilliseconds() function truncates
636 // rather than rounds. We should consider fixing this
637 // so that our averages come out better.
638 EXPECT_GE(delta.InMilliseconds(), 9);
639 EXPECT_GE(delta.InMicroseconds(), 9000);
640 EXPECT_EQ(delta.InSeconds(), 0);
644 static void HighResClockTest(TimeTicks (*GetTicks)()) {
645 #if defined(OS_WIN)
646 // HighResNow doesn't work on some systems. Since the product still works
647 // even if it doesn't work, it makes this entire test questionable.
648 if (!TimeTicks::IsHighResClockWorking())
649 return;
650 #endif
652 // Why do we loop here?
653 // We're trying to measure that intervals increment in a VERY small amount
654 // of time -- less than 15ms. Unfortunately, if we happen to have a
655 // context switch in the middle of our test, the context switch could easily
656 // exceed our limit. So, we iterate on this several times. As long as we're
657 // able to detect the fine-granularity timers at least once, then the test
658 // has succeeded.
660 const int kTargetGranularityUs = 15000; // 15ms
662 bool success = false;
663 int retries = 100; // Arbitrary.
664 TimeDelta delta;
665 while (!success && retries--) {
666 TimeTicks ticks_start = GetTicks();
667 // Loop until we can detect that the clock has changed. Non-HighRes timers
668 // will increment in chunks, e.g. 15ms. By spinning until we see a clock
669 // change, we detect the minimum time between measurements.
670 do {
671 delta = GetTicks() - ticks_start;
672 } while (delta.InMilliseconds() == 0);
674 if (delta.InMicroseconds() <= kTargetGranularityUs)
675 success = true;
678 // In high resolution mode, we expect to see the clock increment
679 // in intervals less than 15ms.
680 EXPECT_TRUE(success);
683 TEST(TimeTicks, HighResNow) {
684 HighResClockTest(&TimeTicks::HighResNow);
687 // Fails frequently on Android http://crbug.com/352633 with:
688 // Expected: (delta_thread.InMicroseconds()) > (0), actual: 0 vs 0
689 #if defined(OS_ANDROID)
690 #define MAYBE_ThreadNow DISABLED_ThreadNow
691 #else
692 #define MAYBE_ThreadNow ThreadNow
693 #endif
694 TEST(TimeTicks, MAYBE_ThreadNow) {
695 if (TimeTicks::IsThreadNowSupported()) {
696 TimeTicks begin = TimeTicks::Now();
697 TimeTicks begin_thread = TimeTicks::ThreadNow();
698 // Make sure that ThreadNow value is non-zero.
699 EXPECT_GT(begin_thread, TimeTicks());
700 // Sleep for 10 milliseconds to get the thread de-scheduled.
701 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(10));
702 TimeTicks end_thread = TimeTicks::ThreadNow();
703 TimeTicks end = TimeTicks::Now();
704 TimeDelta delta = end - begin;
705 TimeDelta delta_thread = end_thread - begin_thread;
706 // Make sure that some thread time have elapsed.
707 EXPECT_GT(delta_thread.InMicroseconds(), 0);
708 // But the thread time is at least 9ms less than clock time.
709 TimeDelta difference = delta - delta_thread;
710 EXPECT_GE(difference.InMicroseconds(), 9000);
714 TEST(TimeTicks, NowFromSystemTraceTime) {
715 // Re-use HighResNow test for now since clock properties are identical.
716 HighResClockTest(&TimeTicks::NowFromSystemTraceTime);
719 TEST(TimeDelta, FromAndIn) {
720 EXPECT_TRUE(TimeDelta::FromDays(2) == TimeDelta::FromHours(48));
721 EXPECT_TRUE(TimeDelta::FromHours(3) == TimeDelta::FromMinutes(180));
722 EXPECT_TRUE(TimeDelta::FromMinutes(2) == TimeDelta::FromSeconds(120));
723 EXPECT_TRUE(TimeDelta::FromSeconds(2) == TimeDelta::FromMilliseconds(2000));
724 EXPECT_TRUE(TimeDelta::FromMilliseconds(2) ==
725 TimeDelta::FromMicroseconds(2000));
726 EXPECT_TRUE(TimeDelta::FromSecondsD(2.3) ==
727 TimeDelta::FromMilliseconds(2300));
728 EXPECT_TRUE(TimeDelta::FromMillisecondsD(2.5) ==
729 TimeDelta::FromMicroseconds(2500));
730 EXPECT_EQ(13, TimeDelta::FromDays(13).InDays());
731 EXPECT_EQ(13, TimeDelta::FromHours(13).InHours());
732 EXPECT_EQ(13, TimeDelta::FromMinutes(13).InMinutes());
733 EXPECT_EQ(13, TimeDelta::FromSeconds(13).InSeconds());
734 EXPECT_EQ(13.0, TimeDelta::FromSeconds(13).InSecondsF());
735 EXPECT_EQ(13, TimeDelta::FromMilliseconds(13).InMilliseconds());
736 EXPECT_EQ(13.0, TimeDelta::FromMilliseconds(13).InMillisecondsF());
737 EXPECT_EQ(13, TimeDelta::FromSecondsD(13.1).InSeconds());
738 EXPECT_EQ(13.1, TimeDelta::FromSecondsD(13.1).InSecondsF());
739 EXPECT_EQ(13, TimeDelta::FromMillisecondsD(13.3).InMilliseconds());
740 EXPECT_EQ(13.3, TimeDelta::FromMillisecondsD(13.3).InMillisecondsF());
741 EXPECT_EQ(13, TimeDelta::FromMicroseconds(13).InMicroseconds());
744 #if defined(OS_POSIX)
745 TEST(TimeDelta, TimeSpecConversion) {
746 struct timespec result = TimeDelta::FromSeconds(0).ToTimeSpec();
747 EXPECT_EQ(result.tv_sec, 0);
748 EXPECT_EQ(result.tv_nsec, 0);
750 result = TimeDelta::FromSeconds(1).ToTimeSpec();
751 EXPECT_EQ(result.tv_sec, 1);
752 EXPECT_EQ(result.tv_nsec, 0);
754 result = TimeDelta::FromMicroseconds(1).ToTimeSpec();
755 EXPECT_EQ(result.tv_sec, 0);
756 EXPECT_EQ(result.tv_nsec, 1000);
758 result = TimeDelta::FromMicroseconds(
759 Time::kMicrosecondsPerSecond + 1).ToTimeSpec();
760 EXPECT_EQ(result.tv_sec, 1);
761 EXPECT_EQ(result.tv_nsec, 1000);
763 #endif // OS_POSIX
765 // Our internal time format is serialized in things like databases, so it's
766 // important that it's consistent across all our platforms. We use the 1601
767 // Windows epoch as the internal format across all platforms.
768 TEST(TimeDelta, WindowsEpoch) {
769 Time::Exploded exploded;
770 exploded.year = 1970;
771 exploded.month = 1;
772 exploded.day_of_week = 0; // Should be unusued.
773 exploded.day_of_month = 1;
774 exploded.hour = 0;
775 exploded.minute = 0;
776 exploded.second = 0;
777 exploded.millisecond = 0;
778 Time t = Time::FromUTCExploded(exploded);
779 // Unix 1970 epoch.
780 EXPECT_EQ(GG_INT64_C(11644473600000000), t.ToInternalValue());
782 // We can't test 1601 epoch, since the system time functions on Linux
783 // only compute years starting from 1900.
786 // We could define this separately for Time, TimeTicks and TimeDelta but the
787 // definitions would be identical anyway.
788 template <class Any>
789 std::string AnyToString(Any any) {
790 std::ostringstream oss;
791 oss << any;
792 return oss.str();
795 TEST(TimeDeltaLogging, DCheckEqCompiles) {
796 DCHECK_EQ(TimeDelta(), TimeDelta());
799 TEST(TimeDeltaLogging, EmptyIsZero) {
800 TimeDelta zero;
801 EXPECT_EQ("0s", AnyToString(zero));
804 TEST(TimeDeltaLogging, FiveHundredMs) {
805 TimeDelta five_hundred_ms = TimeDelta::FromMilliseconds(500);
806 EXPECT_EQ("0.5s", AnyToString(five_hundred_ms));
809 TEST(TimeDeltaLogging, MinusTenSeconds) {
810 TimeDelta minus_ten_seconds = TimeDelta::FromSeconds(-10);
811 EXPECT_EQ("-10s", AnyToString(minus_ten_seconds));
814 TEST(TimeDeltaLogging, DoesNotMessUpFormattingFlags) {
815 std::ostringstream oss;
816 std::ios_base::fmtflags flags_before = oss.flags();
817 oss << TimeDelta();
818 EXPECT_EQ(flags_before, oss.flags());
821 TEST(TimeDeltaLogging, DoesNotMakeStreamBad) {
822 std::ostringstream oss;
823 oss << TimeDelta();
824 EXPECT_TRUE(oss.good());
827 TEST(TimeLogging, DCheckEqCompiles) {
828 DCHECK_EQ(Time(), Time());
831 TEST(TimeLogging, ChromeBirthdate) {
832 Time birthdate;
833 ASSERT_TRUE(Time::FromString("Tue, 02 Sep 2008 09:42:18 GMT", &birthdate));
834 EXPECT_EQ("2008-09-02 09:42:18.000 UTC", AnyToString(birthdate));
837 TEST(TimeLogging, DoesNotMessUpFormattingFlags) {
838 std::ostringstream oss;
839 std::ios_base::fmtflags flags_before = oss.flags();
840 oss << Time();
841 EXPECT_EQ(flags_before, oss.flags());
844 TEST(TimeLogging, DoesNotMakeStreamBad) {
845 std::ostringstream oss;
846 oss << Time();
847 EXPECT_TRUE(oss.good());
850 TEST(TimeTicksLogging, DCheckEqCompiles) {
851 DCHECK_EQ(TimeTicks(), TimeTicks());
854 TEST(TimeTicksLogging, ZeroTime) {
855 TimeTicks zero;
856 EXPECT_EQ("0 bogo-microseconds", AnyToString(zero));
859 TEST(TimeTicksLogging, FortyYearsLater) {
860 TimeTicks forty_years_later =
861 TimeTicks() + TimeDelta::FromDays(365.25 * 40);
862 EXPECT_EQ("1262304000000000 bogo-microseconds",
863 AnyToString(forty_years_later));
866 TEST(TimeTicksLogging, DoesNotMessUpFormattingFlags) {
867 std::ostringstream oss;
868 std::ios_base::fmtflags flags_before = oss.flags();
869 oss << TimeTicks();
870 EXPECT_EQ(flags_before, oss.flags());
873 TEST(TimeTicksLogging, DoesNotMakeStreamBad) {
874 std::ostringstream oss;
875 oss << TimeTicks();
876 EXPECT_TRUE(oss.good());
879 } // namespace
881 } // namespace base