Hide Google logo and custom launcher page in app list for non-Google search engines.
[chromium-blink-merge.git] / base / time / time_unittest.cc
blobfdac59dae1f281b985d0e18a4eb38a0975680e36
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 <limits>
10 #include <string>
12 #include "base/compiler_specific.h"
13 #include "base/logging.h"
14 #include "base/strings/stringprintf.h"
15 #include "base/threading/platform_thread.h"
16 #include "build/build_config.h"
17 #include "testing/gtest/include/gtest/gtest.h"
19 namespace base {
21 namespace {
23 // Specialized test fixture allowing time strings without timezones to be
24 // tested by comparing them to a known time in the local zone.
25 // See also pr_time_unittests.cc
26 class TimeTest : public testing::Test {
27 protected:
28 void SetUp() override {
29 // Use mktime to get a time_t, and turn it into a PRTime by converting
30 // seconds to microseconds. Use 15th Oct 2007 12:45:00 local. This
31 // must be a time guaranteed to be outside of a DST fallback hour in
32 // any timezone.
33 struct tm local_comparison_tm = {
34 0, // second
35 45, // minute
36 12, // hour
37 15, // day of month
38 10 - 1, // month
39 2007 - 1900, // year
40 0, // day of week (ignored, output only)
41 0, // day of year (ignored, output only)
42 -1 // DST in effect, -1 tells mktime to figure it out
45 time_t converted_time = mktime(&local_comparison_tm);
46 ASSERT_GT(converted_time, 0);
47 comparison_time_local_ = Time::FromTimeT(converted_time);
49 // time_t representation of 15th Oct 2007 12:45:00 PDT
50 comparison_time_pdt_ = Time::FromTimeT(1192477500);
53 Time comparison_time_local_;
54 Time comparison_time_pdt_;
57 // Test conversions to/from time_t and exploding/unexploding.
58 TEST_F(TimeTest, TimeT) {
59 // C library time and exploded time.
60 time_t now_t_1 = time(NULL);
61 struct tm tms;
62 #if defined(OS_WIN)
63 localtime_s(&tms, &now_t_1);
64 #elif defined(OS_POSIX)
65 localtime_r(&now_t_1, &tms);
66 #endif
68 // Convert to ours.
69 Time our_time_1 = Time::FromTimeT(now_t_1);
70 Time::Exploded exploded;
71 our_time_1.LocalExplode(&exploded);
73 // This will test both our exploding and our time_t -> Time conversion.
74 EXPECT_EQ(tms.tm_year + 1900, exploded.year);
75 EXPECT_EQ(tms.tm_mon + 1, exploded.month);
76 EXPECT_EQ(tms.tm_mday, exploded.day_of_month);
77 EXPECT_EQ(tms.tm_hour, exploded.hour);
78 EXPECT_EQ(tms.tm_min, exploded.minute);
79 EXPECT_EQ(tms.tm_sec, exploded.second);
81 // Convert exploded back to the time struct.
82 Time our_time_2 = Time::FromLocalExploded(exploded);
83 EXPECT_TRUE(our_time_1 == our_time_2);
85 time_t now_t_2 = our_time_2.ToTimeT();
86 EXPECT_EQ(now_t_1, now_t_2);
88 EXPECT_EQ(10, Time().FromTimeT(10).ToTimeT());
89 EXPECT_EQ(10.0, Time().FromTimeT(10).ToDoubleT());
91 // Conversions of 0 should stay 0.
92 EXPECT_EQ(0, Time().ToTimeT());
93 EXPECT_EQ(0, Time::FromTimeT(0).ToInternalValue());
96 // Test conversions to/from javascript time.
97 TEST_F(TimeTest, JsTime) {
98 Time epoch = Time::FromJsTime(0.0);
99 EXPECT_EQ(epoch, Time::UnixEpoch());
100 Time t = Time::FromJsTime(700000.3);
101 EXPECT_EQ(700.0003, t.ToDoubleT());
102 t = Time::FromDoubleT(800.73);
103 EXPECT_EQ(800730.0, t.ToJsTime());
106 #if defined(OS_POSIX)
107 TEST_F(TimeTest, FromTimeVal) {
108 Time now = Time::Now();
109 Time also_now = Time::FromTimeVal(now.ToTimeVal());
110 EXPECT_EQ(now, also_now);
112 #endif // OS_POSIX
114 TEST_F(TimeTest, FromExplodedWithMilliseconds) {
115 // Some platform implementations of FromExploded are liable to drop
116 // milliseconds if we aren't careful.
117 Time now = Time::NowFromSystemTime();
118 Time::Exploded exploded1 = {0};
119 now.UTCExplode(&exploded1);
120 exploded1.millisecond = 500;
121 Time time = Time::FromUTCExploded(exploded1);
122 Time::Exploded exploded2 = {0};
123 time.UTCExplode(&exploded2);
124 EXPECT_EQ(exploded1.millisecond, exploded2.millisecond);
127 TEST_F(TimeTest, ZeroIsSymmetric) {
128 Time zero_time(Time::FromTimeT(0));
129 EXPECT_EQ(0, zero_time.ToTimeT());
131 EXPECT_EQ(0.0, zero_time.ToDoubleT());
134 TEST_F(TimeTest, LocalExplode) {
135 Time a = Time::Now();
136 Time::Exploded exploded;
137 a.LocalExplode(&exploded);
139 Time b = Time::FromLocalExploded(exploded);
141 // The exploded structure doesn't have microseconds, and on Mac & Linux, the
142 // internal OS conversion uses seconds, which will cause truncation. So we
143 // can only make sure that the delta is within one second.
144 EXPECT_TRUE((a - b) < TimeDelta::FromSeconds(1));
147 TEST_F(TimeTest, UTCExplode) {
148 Time a = Time::Now();
149 Time::Exploded exploded;
150 a.UTCExplode(&exploded);
152 Time b = Time::FromUTCExploded(exploded);
153 EXPECT_TRUE((a - b) < TimeDelta::FromSeconds(1));
156 TEST_F(TimeTest, LocalMidnight) {
157 Time::Exploded exploded;
158 Time::Now().LocalMidnight().LocalExplode(&exploded);
159 EXPECT_EQ(0, exploded.hour);
160 EXPECT_EQ(0, exploded.minute);
161 EXPECT_EQ(0, exploded.second);
162 EXPECT_EQ(0, exploded.millisecond);
165 TEST_F(TimeTest, ParseTimeTest1) {
166 time_t current_time = 0;
167 time(&current_time);
169 const int BUFFER_SIZE = 64;
170 struct tm local_time = {0};
171 char time_buf[BUFFER_SIZE] = {0};
172 #if defined(OS_WIN)
173 localtime_s(&local_time, &current_time);
174 asctime_s(time_buf, arraysize(time_buf), &local_time);
175 #elif defined(OS_POSIX)
176 localtime_r(&current_time, &local_time);
177 asctime_r(&local_time, time_buf);
178 #endif
180 Time parsed_time;
181 EXPECT_TRUE(Time::FromString(time_buf, &parsed_time));
182 EXPECT_EQ(current_time, parsed_time.ToTimeT());
185 TEST_F(TimeTest, DayOfWeekSunday) {
186 Time time;
187 EXPECT_TRUE(Time::FromString("Sun, 06 May 2012 12:00:00 GMT", &time));
188 Time::Exploded exploded;
189 time.UTCExplode(&exploded);
190 EXPECT_EQ(0, exploded.day_of_week);
193 TEST_F(TimeTest, DayOfWeekWednesday) {
194 Time time;
195 EXPECT_TRUE(Time::FromString("Wed, 09 May 2012 12:00:00 GMT", &time));
196 Time::Exploded exploded;
197 time.UTCExplode(&exploded);
198 EXPECT_EQ(3, exploded.day_of_week);
201 TEST_F(TimeTest, DayOfWeekSaturday) {
202 Time time;
203 EXPECT_TRUE(Time::FromString("Sat, 12 May 2012 12:00:00 GMT", &time));
204 Time::Exploded exploded;
205 time.UTCExplode(&exploded);
206 EXPECT_EQ(6, exploded.day_of_week);
209 TEST_F(TimeTest, ParseTimeTest2) {
210 Time parsed_time;
211 EXPECT_TRUE(Time::FromString("Mon, 15 Oct 2007 19:45:00 GMT", &parsed_time));
212 EXPECT_EQ(comparison_time_pdt_, parsed_time);
215 TEST_F(TimeTest, ParseTimeTest3) {
216 Time parsed_time;
217 EXPECT_TRUE(Time::FromString("15 Oct 07 12:45:00", &parsed_time));
218 EXPECT_EQ(comparison_time_local_, parsed_time);
221 TEST_F(TimeTest, ParseTimeTest4) {
222 Time parsed_time;
223 EXPECT_TRUE(Time::FromString("15 Oct 07 19:45 GMT", &parsed_time));
224 EXPECT_EQ(comparison_time_pdt_, parsed_time);
227 TEST_F(TimeTest, ParseTimeTest5) {
228 Time parsed_time;
229 EXPECT_TRUE(Time::FromString("Mon Oct 15 12:45 PDT 2007", &parsed_time));
230 EXPECT_EQ(comparison_time_pdt_, parsed_time);
233 TEST_F(TimeTest, ParseTimeTest6) {
234 Time parsed_time;
235 EXPECT_TRUE(Time::FromString("Monday, Oct 15, 2007 12:45 PM", &parsed_time));
236 EXPECT_EQ(comparison_time_local_, parsed_time);
239 TEST_F(TimeTest, ParseTimeTest7) {
240 Time parsed_time;
241 EXPECT_TRUE(Time::FromString("10/15/07 12:45:00 PM", &parsed_time));
242 EXPECT_EQ(comparison_time_local_, parsed_time);
245 TEST_F(TimeTest, ParseTimeTest8) {
246 Time parsed_time;
247 EXPECT_TRUE(Time::FromString("15-OCT-2007 12:45pm", &parsed_time));
248 EXPECT_EQ(comparison_time_local_, parsed_time);
251 TEST_F(TimeTest, ParseTimeTest9) {
252 Time parsed_time;
253 EXPECT_TRUE(Time::FromString("16 Oct 2007 4:45-JST (Tuesday)", &parsed_time));
254 EXPECT_EQ(comparison_time_pdt_, parsed_time);
257 TEST_F(TimeTest, ParseTimeTest10) {
258 Time parsed_time;
259 EXPECT_TRUE(Time::FromString("15/10/07 12:45", &parsed_time));
260 EXPECT_EQ(parsed_time, comparison_time_local_);
263 // Test some of edge cases around epoch, etc.
264 TEST_F(TimeTest, ParseTimeTestEpoch0) {
265 Time parsed_time;
267 // time_t == epoch == 0
268 EXPECT_TRUE(Time::FromString("Thu Jan 01 01:00:00 +0100 1970",
269 &parsed_time));
270 EXPECT_EQ(0, parsed_time.ToTimeT());
271 EXPECT_TRUE(Time::FromString("Thu Jan 01 00:00:00 GMT 1970",
272 &parsed_time));
273 EXPECT_EQ(0, parsed_time.ToTimeT());
276 TEST_F(TimeTest, ParseTimeTestEpoch1) {
277 Time parsed_time;
279 // time_t == 1 second after epoch == 1
280 EXPECT_TRUE(Time::FromString("Thu Jan 01 01:00:01 +0100 1970",
281 &parsed_time));
282 EXPECT_EQ(1, parsed_time.ToTimeT());
283 EXPECT_TRUE(Time::FromString("Thu Jan 01 00:00:01 GMT 1970",
284 &parsed_time));
285 EXPECT_EQ(1, parsed_time.ToTimeT());
288 TEST_F(TimeTest, ParseTimeTestEpoch2) {
289 Time parsed_time;
291 // time_t == 2 seconds after epoch == 2
292 EXPECT_TRUE(Time::FromString("Thu Jan 01 01:00:02 +0100 1970",
293 &parsed_time));
294 EXPECT_EQ(2, parsed_time.ToTimeT());
295 EXPECT_TRUE(Time::FromString("Thu Jan 01 00:00:02 GMT 1970",
296 &parsed_time));
297 EXPECT_EQ(2, parsed_time.ToTimeT());
300 TEST_F(TimeTest, ParseTimeTestEpochNeg1) {
301 Time parsed_time;
303 // time_t == 1 second before epoch == -1
304 EXPECT_TRUE(Time::FromString("Thu Jan 01 00:59:59 +0100 1970",
305 &parsed_time));
306 EXPECT_EQ(-1, parsed_time.ToTimeT());
307 EXPECT_TRUE(Time::FromString("Wed Dec 31 23:59:59 GMT 1969",
308 &parsed_time));
309 EXPECT_EQ(-1, parsed_time.ToTimeT());
312 // If time_t is 32 bits, a date after year 2038 will overflow time_t and
313 // cause timegm() to return -1. The parsed time should not be 1 second
314 // before epoch.
315 TEST_F(TimeTest, ParseTimeTestEpochNotNeg1) {
316 Time parsed_time;
318 EXPECT_TRUE(Time::FromString("Wed Dec 31 23:59:59 GMT 2100",
319 &parsed_time));
320 EXPECT_NE(-1, parsed_time.ToTimeT());
323 TEST_F(TimeTest, ParseTimeTestEpochNeg2) {
324 Time parsed_time;
326 // time_t == 2 seconds before epoch == -2
327 EXPECT_TRUE(Time::FromString("Thu Jan 01 00:59:58 +0100 1970",
328 &parsed_time));
329 EXPECT_EQ(-2, parsed_time.ToTimeT());
330 EXPECT_TRUE(Time::FromString("Wed Dec 31 23:59:58 GMT 1969",
331 &parsed_time));
332 EXPECT_EQ(-2, parsed_time.ToTimeT());
335 TEST_F(TimeTest, ParseTimeTestEpoch1960) {
336 Time parsed_time;
338 // time_t before Epoch, in 1960
339 EXPECT_TRUE(Time::FromString("Wed Jun 29 19:40:01 +0100 1960",
340 &parsed_time));
341 EXPECT_EQ(-299999999, parsed_time.ToTimeT());
342 EXPECT_TRUE(Time::FromString("Wed Jun 29 18:40:01 GMT 1960",
343 &parsed_time));
344 EXPECT_EQ(-299999999, parsed_time.ToTimeT());
345 EXPECT_TRUE(Time::FromString("Wed Jun 29 17:40:01 GMT 1960",
346 &parsed_time));
347 EXPECT_EQ(-300003599, parsed_time.ToTimeT());
350 TEST_F(TimeTest, ParseTimeTestEmpty) {
351 Time parsed_time;
352 EXPECT_FALSE(Time::FromString("", &parsed_time));
355 TEST_F(TimeTest, ParseTimeTestInvalidString) {
356 Time parsed_time;
357 EXPECT_FALSE(Time::FromString("Monday morning 2000", &parsed_time));
360 TEST_F(TimeTest, ExplodeBeforeUnixEpoch) {
361 static const int kUnixEpochYear = 1970; // In case this changes (ha!).
362 Time t;
363 Time::Exploded exploded;
365 t = Time::UnixEpoch() - TimeDelta::FromMicroseconds(1);
366 t.UTCExplode(&exploded);
367 EXPECT_TRUE(exploded.HasValidValues());
368 // Should be 1969-12-31 23:59:59 999 milliseconds (and 999 microseconds).
369 EXPECT_EQ(kUnixEpochYear - 1, exploded.year);
370 EXPECT_EQ(12, exploded.month);
371 EXPECT_EQ(31, exploded.day_of_month);
372 EXPECT_EQ(23, exploded.hour);
373 EXPECT_EQ(59, exploded.minute);
374 EXPECT_EQ(59, exploded.second);
375 EXPECT_EQ(999, exploded.millisecond);
377 t = Time::UnixEpoch() - TimeDelta::FromMicroseconds(1000);
378 t.UTCExplode(&exploded);
379 EXPECT_TRUE(exploded.HasValidValues());
380 // Should be 1969-12-31 23:59:59 999 milliseconds.
381 EXPECT_EQ(kUnixEpochYear - 1, exploded.year);
382 EXPECT_EQ(12, exploded.month);
383 EXPECT_EQ(31, exploded.day_of_month);
384 EXPECT_EQ(23, exploded.hour);
385 EXPECT_EQ(59, exploded.minute);
386 EXPECT_EQ(59, exploded.second);
387 EXPECT_EQ(999, exploded.millisecond);
389 t = Time::UnixEpoch() - TimeDelta::FromMicroseconds(1001);
390 t.UTCExplode(&exploded);
391 EXPECT_TRUE(exploded.HasValidValues());
392 // Should be 1969-12-31 23:59:59 998 milliseconds (and 999 microseconds).
393 EXPECT_EQ(kUnixEpochYear - 1, exploded.year);
394 EXPECT_EQ(12, exploded.month);
395 EXPECT_EQ(31, exploded.day_of_month);
396 EXPECT_EQ(23, exploded.hour);
397 EXPECT_EQ(59, exploded.minute);
398 EXPECT_EQ(59, exploded.second);
399 EXPECT_EQ(998, exploded.millisecond);
401 t = Time::UnixEpoch() - TimeDelta::FromMilliseconds(1000);
402 t.UTCExplode(&exploded);
403 EXPECT_TRUE(exploded.HasValidValues());
404 // Should be 1969-12-31 23:59:59.
405 EXPECT_EQ(kUnixEpochYear - 1, exploded.year);
406 EXPECT_EQ(12, exploded.month);
407 EXPECT_EQ(31, exploded.day_of_month);
408 EXPECT_EQ(23, exploded.hour);
409 EXPECT_EQ(59, exploded.minute);
410 EXPECT_EQ(59, exploded.second);
411 EXPECT_EQ(0, exploded.millisecond);
413 t = Time::UnixEpoch() - TimeDelta::FromMilliseconds(1001);
414 t.UTCExplode(&exploded);
415 EXPECT_TRUE(exploded.HasValidValues());
416 // Should be 1969-12-31 23:59:58 999 milliseconds.
417 EXPECT_EQ(kUnixEpochYear - 1, exploded.year);
418 EXPECT_EQ(12, exploded.month);
419 EXPECT_EQ(31, exploded.day_of_month);
420 EXPECT_EQ(23, exploded.hour);
421 EXPECT_EQ(59, exploded.minute);
422 EXPECT_EQ(58, exploded.second);
423 EXPECT_EQ(999, exploded.millisecond);
425 // Make sure we still handle at/after Unix epoch correctly.
426 t = Time::UnixEpoch();
427 t.UTCExplode(&exploded);
428 EXPECT_TRUE(exploded.HasValidValues());
429 // Should be 1970-12-31 00:00:00 0 milliseconds.
430 EXPECT_EQ(kUnixEpochYear, exploded.year);
431 EXPECT_EQ(1, exploded.month);
432 EXPECT_EQ(1, exploded.day_of_month);
433 EXPECT_EQ(0, exploded.hour);
434 EXPECT_EQ(0, exploded.minute);
435 EXPECT_EQ(0, exploded.second);
436 EXPECT_EQ(0, exploded.millisecond);
438 t = Time::UnixEpoch() + TimeDelta::FromMicroseconds(1);
439 t.UTCExplode(&exploded);
440 EXPECT_TRUE(exploded.HasValidValues());
441 // Should be 1970-01-01 00:00:00 0 milliseconds (and 1 microsecond).
442 EXPECT_EQ(kUnixEpochYear, exploded.year);
443 EXPECT_EQ(1, exploded.month);
444 EXPECT_EQ(1, exploded.day_of_month);
445 EXPECT_EQ(0, exploded.hour);
446 EXPECT_EQ(0, exploded.minute);
447 EXPECT_EQ(0, exploded.second);
448 EXPECT_EQ(0, exploded.millisecond);
450 t = Time::UnixEpoch() + TimeDelta::FromMicroseconds(1000);
451 t.UTCExplode(&exploded);
452 EXPECT_TRUE(exploded.HasValidValues());
453 // Should be 1970-01-01 00:00:00 1 millisecond.
454 EXPECT_EQ(kUnixEpochYear, exploded.year);
455 EXPECT_EQ(1, exploded.month);
456 EXPECT_EQ(1, exploded.day_of_month);
457 EXPECT_EQ(0, exploded.hour);
458 EXPECT_EQ(0, exploded.minute);
459 EXPECT_EQ(0, exploded.second);
460 EXPECT_EQ(1, exploded.millisecond);
462 t = Time::UnixEpoch() + TimeDelta::FromMilliseconds(1000);
463 t.UTCExplode(&exploded);
464 EXPECT_TRUE(exploded.HasValidValues());
465 // Should be 1970-01-01 00:00:01.
466 EXPECT_EQ(kUnixEpochYear, exploded.year);
467 EXPECT_EQ(1, exploded.month);
468 EXPECT_EQ(1, exploded.day_of_month);
469 EXPECT_EQ(0, exploded.hour);
470 EXPECT_EQ(0, exploded.minute);
471 EXPECT_EQ(1, exploded.second);
472 EXPECT_EQ(0, exploded.millisecond);
474 t = Time::UnixEpoch() + TimeDelta::FromMilliseconds(1001);
475 t.UTCExplode(&exploded);
476 EXPECT_TRUE(exploded.HasValidValues());
477 // Should be 1970-01-01 00:00:01 1 millisecond.
478 EXPECT_EQ(kUnixEpochYear, exploded.year);
479 EXPECT_EQ(1, exploded.month);
480 EXPECT_EQ(1, exploded.day_of_month);
481 EXPECT_EQ(0, exploded.hour);
482 EXPECT_EQ(0, exploded.minute);
483 EXPECT_EQ(1, exploded.second);
484 EXPECT_EQ(1, exploded.millisecond);
487 TEST_F(TimeTest, TimeDeltaMax) {
488 TimeDelta max = TimeDelta::Max();
489 EXPECT_TRUE(max.is_max());
490 EXPECT_EQ(max, TimeDelta::Max());
491 EXPECT_GT(max, TimeDelta::FromDays(100 * 365));
492 EXPECT_GT(max, TimeDelta());
495 TEST_F(TimeTest, TimeDeltaMaxConversions) {
496 TimeDelta t = TimeDelta::Max();
497 EXPECT_EQ(std::numeric_limits<int64>::max(), t.ToInternalValue());
499 EXPECT_EQ(std::numeric_limits<int>::max(), t.InDays());
500 EXPECT_EQ(std::numeric_limits<int>::max(), t.InHours());
501 EXPECT_EQ(std::numeric_limits<int>::max(), t.InMinutes());
502 EXPECT_EQ(std::numeric_limits<double>::infinity(), t.InSecondsF());
503 EXPECT_EQ(std::numeric_limits<int64>::max(), t.InSeconds());
504 EXPECT_EQ(std::numeric_limits<double>::infinity(), t.InMillisecondsF());
505 EXPECT_EQ(std::numeric_limits<int64>::max(), t.InMilliseconds());
506 EXPECT_EQ(std::numeric_limits<int64>::max(), t.InMillisecondsRoundedUp());
508 t = TimeDelta::FromDays(std::numeric_limits<int>::max());
509 EXPECT_TRUE(t.is_max());
511 t = TimeDelta::FromHours(std::numeric_limits<int>::max());
512 EXPECT_TRUE(t.is_max());
514 t = TimeDelta::FromMinutes(std::numeric_limits<int>::max());
515 EXPECT_TRUE(t.is_max());
517 t = TimeDelta::FromSeconds(std::numeric_limits<int64>::max());
518 EXPECT_TRUE(t.is_max());
520 t = TimeDelta::FromMilliseconds(std::numeric_limits<int64>::max());
521 EXPECT_TRUE(t.is_max());
523 t = TimeDelta::FromSecondsD(std::numeric_limits<double>::infinity());
524 EXPECT_TRUE(t.is_max());
526 t = TimeDelta::FromMillisecondsD(std::numeric_limits<double>::infinity());
527 EXPECT_TRUE(t.is_max());
529 t = TimeDelta::FromMicroseconds(std::numeric_limits<int64>::max());
530 EXPECT_TRUE(t.is_max());
533 TEST_F(TimeTest, Max) {
534 Time max = Time::Max();
535 EXPECT_TRUE(max.is_max());
536 EXPECT_EQ(max, Time::Max());
537 EXPECT_GT(max, Time::Now());
538 EXPECT_GT(max, Time());
541 TEST_F(TimeTest, MaxConversions) {
542 Time t = Time::Max();
543 EXPECT_EQ(std::numeric_limits<int64>::max(), t.ToInternalValue());
545 t = Time::FromDoubleT(std::numeric_limits<double>::infinity());
546 EXPECT_TRUE(t.is_max());
547 EXPECT_EQ(std::numeric_limits<double>::infinity(), t.ToDoubleT());
549 t = Time::FromJsTime(std::numeric_limits<double>::infinity());
550 EXPECT_TRUE(t.is_max());
551 EXPECT_EQ(std::numeric_limits<double>::infinity(), t.ToJsTime());
553 t = Time::FromTimeT(std::numeric_limits<time_t>::max());
554 EXPECT_TRUE(t.is_max());
555 EXPECT_EQ(std::numeric_limits<time_t>::max(), t.ToTimeT());
557 #if defined(OS_POSIX)
558 struct timeval tval;
559 tval.tv_sec = std::numeric_limits<time_t>::max();
560 tval.tv_usec = static_cast<suseconds_t>(Time::kMicrosecondsPerSecond) - 1;
561 t = Time::FromTimeVal(tval);
562 EXPECT_TRUE(t.is_max());
563 tval = t.ToTimeVal();
564 EXPECT_EQ(std::numeric_limits<time_t>::max(), tval.tv_sec);
565 EXPECT_EQ(static_cast<suseconds_t>(Time::kMicrosecondsPerSecond) - 1,
566 tval.tv_usec);
567 #endif
569 #if defined(OS_MACOSX)
570 t = Time::FromCFAbsoluteTime(std::numeric_limits<CFAbsoluteTime>::infinity());
571 EXPECT_TRUE(t.is_max());
572 EXPECT_EQ(std::numeric_limits<CFAbsoluteTime>::infinity(),
573 t.ToCFAbsoluteTime());
574 #endif
576 #if defined(OS_WIN)
577 FILETIME ftime;
578 ftime.dwHighDateTime = std::numeric_limits<DWORD>::max();
579 ftime.dwLowDateTime = std::numeric_limits<DWORD>::max();
580 t = Time::FromFileTime(ftime);
581 EXPECT_TRUE(t.is_max());
582 ftime = t.ToFileTime();
583 EXPECT_EQ(std::numeric_limits<DWORD>::max(), ftime.dwHighDateTime);
584 EXPECT_EQ(std::numeric_limits<DWORD>::max(), ftime.dwLowDateTime);
585 #endif
588 #if defined(OS_MACOSX)
589 TEST_F(TimeTest, TimeTOverflow) {
590 Time t = Time::FromInternalValue(std::numeric_limits<int64>::max() - 1);
591 EXPECT_FALSE(t.is_max());
592 EXPECT_EQ(std::numeric_limits<time_t>::max(), t.ToTimeT());
594 #endif
596 #if defined(OS_ANDROID)
597 TEST_F(TimeTest, FromLocalExplodedCrashOnAndroid) {
598 // This crashed inside Time:: FromLocalExploded() on Android 4.1.2.
599 // See http://crbug.com/287821
600 Time::Exploded midnight = {2013, // year
601 10, // month
602 0, // day_of_week
603 13, // day_of_month
604 0, // hour
605 0, // minute
606 0, // second
608 // The string passed to putenv() must be a char* and the documentation states
609 // that it 'becomes part of the environment', so use a static buffer.
610 static char buffer[] = "TZ=America/Santiago";
611 putenv(buffer);
612 tzset();
613 Time t = Time::FromLocalExploded(midnight);
614 EXPECT_EQ(1381633200, t.ToTimeT());
616 #endif // OS_ANDROID
618 TEST(TimeTicks, Deltas) {
619 for (int index = 0; index < 50; index++) {
620 TimeTicks ticks_start = TimeTicks::Now();
621 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(10));
622 TimeTicks ticks_stop = TimeTicks::Now();
623 TimeDelta delta = ticks_stop - ticks_start;
624 // Note: Although we asked for a 10ms sleep, if the
625 // time clock has a finer granularity than the Sleep()
626 // clock, it is quite possible to wakeup early. Here
627 // is how that works:
628 // Time(ms timer) Time(us timer)
629 // 5 5010
630 // 6 6010
631 // 7 7010
632 // 8 8010
633 // 9 9000
634 // Elapsed 4ms 3990us
636 // Unfortunately, our InMilliseconds() function truncates
637 // rather than rounds. We should consider fixing this
638 // so that our averages come out better.
639 EXPECT_GE(delta.InMilliseconds(), 9);
640 EXPECT_GE(delta.InMicroseconds(), 9000);
641 EXPECT_EQ(delta.InSeconds(), 0);
645 static void HighResClockTest(TimeTicks (*GetTicks)()) {
646 // IsHighResolution() is false on some systems. Since the product still works
647 // even if it's false, it makes this entire test questionable.
648 if (!TimeTicks::IsHighResolution())
649 return;
651 // Why do we loop here?
652 // We're trying to measure that intervals increment in a VERY small amount
653 // of time -- less than 15ms. Unfortunately, if we happen to have a
654 // context switch in the middle of our test, the context switch could easily
655 // exceed our limit. So, we iterate on this several times. As long as we're
656 // able to detect the fine-granularity timers at least once, then the test
657 // has succeeded.
659 const int kTargetGranularityUs = 15000; // 15ms
661 bool success = false;
662 int retries = 100; // Arbitrary.
663 TimeDelta delta;
664 while (!success && retries--) {
665 TimeTicks ticks_start = GetTicks();
666 // Loop until we can detect that the clock has changed. Non-HighRes timers
667 // will increment in chunks, e.g. 15ms. By spinning until we see a clock
668 // change, we detect the minimum time between measurements.
669 do {
670 delta = GetTicks() - ticks_start;
671 } while (delta.InMilliseconds() == 0);
673 if (delta.InMicroseconds() <= kTargetGranularityUs)
674 success = true;
677 // In high resolution mode, we expect to see the clock increment
678 // in intervals less than 15ms.
679 EXPECT_TRUE(success);
682 TEST(TimeTicks, HighRes) {
683 HighResClockTest(&TimeTicks::Now);
686 // Fails frequently on Android http://crbug.com/352633 with:
687 // Expected: (delta_thread.InMicroseconds()) > (0), actual: 0 vs 0
688 #if defined(OS_ANDROID)
689 #define MAYBE_ThreadNow DISABLED_ThreadNow
690 #else
691 #define MAYBE_ThreadNow ThreadNow
692 #endif
693 TEST(TimeTicks, MAYBE_ThreadNow) {
694 if (TimeTicks::IsThreadNowSupported()) {
695 TimeTicks begin = TimeTicks::Now();
696 TimeTicks begin_thread = TimeTicks::ThreadNow();
697 // Make sure that ThreadNow value is non-zero.
698 EXPECT_GT(begin_thread, TimeTicks());
699 // Sleep for 10 milliseconds to get the thread de-scheduled.
700 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(10));
701 TimeTicks end_thread = TimeTicks::ThreadNow();
702 TimeTicks end = TimeTicks::Now();
703 TimeDelta delta = end - begin;
704 TimeDelta delta_thread = end_thread - begin_thread;
705 // Make sure that some thread time have elapsed.
706 EXPECT_GT(delta_thread.InMicroseconds(), 0);
707 // But the thread time is at least 9ms less than clock time.
708 TimeDelta difference = delta - delta_thread;
709 EXPECT_GE(difference.InMicroseconds(), 9000);
713 TEST(TimeTicks, NowFromSystemTraceTime) {
714 // Re-use HighRes test for now since clock properties are identical.
715 HighResClockTest(&TimeTicks::NowFromSystemTraceTime);
718 TEST(TimeTicks, SnappedToNextTickBasic) {
719 base::TimeTicks phase = base::TimeTicks::FromInternalValue(4000);
720 base::TimeDelta interval = base::TimeDelta::FromInternalValue(1000);
721 base::TimeTicks timestamp;
723 // Timestamp in previous interval.
724 timestamp = base::TimeTicks::FromInternalValue(3500);
725 EXPECT_EQ(4000,
726 timestamp.SnappedToNextTick(phase, interval).ToInternalValue());
728 // Timestamp in next interval.
729 timestamp = base::TimeTicks::FromInternalValue(4500);
730 EXPECT_EQ(5000,
731 timestamp.SnappedToNextTick(phase, interval).ToInternalValue());
733 // Timestamp multiple intervals before.
734 timestamp = base::TimeTicks::FromInternalValue(2500);
735 EXPECT_EQ(3000,
736 timestamp.SnappedToNextTick(phase, interval).ToInternalValue());
738 // Timestamp multiple intervals after.
739 timestamp = base::TimeTicks::FromInternalValue(6500);
740 EXPECT_EQ(7000,
741 timestamp.SnappedToNextTick(phase, interval).ToInternalValue());
743 // Timestamp on previous interval.
744 timestamp = base::TimeTicks::FromInternalValue(3000);
745 EXPECT_EQ(3000,
746 timestamp.SnappedToNextTick(phase, interval).ToInternalValue());
748 // Timestamp on next interval.
749 timestamp = base::TimeTicks::FromInternalValue(5000);
750 EXPECT_EQ(5000,
751 timestamp.SnappedToNextTick(phase, interval).ToInternalValue());
753 // Timestamp equal to phase.
754 timestamp = base::TimeTicks::FromInternalValue(4000);
755 EXPECT_EQ(4000,
756 timestamp.SnappedToNextTick(phase, interval).ToInternalValue());
759 TEST(TimeTicks, SnappedToNextTickOverflow) {
760 // int(big_timestamp / interval) < 0, so this causes a crash if the number of
761 // intervals elapsed is attempted to be stored in an int.
762 base::TimeTicks phase = base::TimeTicks::FromInternalValue(0);
763 base::TimeDelta interval = base::TimeDelta::FromInternalValue(4000);
764 base::TimeTicks big_timestamp =
765 base::TimeTicks::FromInternalValue(8635916564000);
767 EXPECT_EQ(8635916564000,
768 big_timestamp.SnappedToNextTick(phase, interval).ToInternalValue());
769 EXPECT_EQ(8635916564000,
770 big_timestamp.SnappedToNextTick(big_timestamp, interval)
771 .ToInternalValue());
774 TEST(TimeDelta, FromAndIn) {
775 EXPECT_TRUE(TimeDelta::FromDays(2) == TimeDelta::FromHours(48));
776 EXPECT_TRUE(TimeDelta::FromHours(3) == TimeDelta::FromMinutes(180));
777 EXPECT_TRUE(TimeDelta::FromMinutes(2) == TimeDelta::FromSeconds(120));
778 EXPECT_TRUE(TimeDelta::FromSeconds(2) == TimeDelta::FromMilliseconds(2000));
779 EXPECT_TRUE(TimeDelta::FromMilliseconds(2) ==
780 TimeDelta::FromMicroseconds(2000));
781 EXPECT_TRUE(TimeDelta::FromSecondsD(2.3) ==
782 TimeDelta::FromMilliseconds(2300));
783 EXPECT_TRUE(TimeDelta::FromMillisecondsD(2.5) ==
784 TimeDelta::FromMicroseconds(2500));
785 EXPECT_EQ(13, TimeDelta::FromDays(13).InDays());
786 EXPECT_EQ(13, TimeDelta::FromHours(13).InHours());
787 EXPECT_EQ(13, TimeDelta::FromMinutes(13).InMinutes());
788 EXPECT_EQ(13, TimeDelta::FromSeconds(13).InSeconds());
789 EXPECT_EQ(13.0, TimeDelta::FromSeconds(13).InSecondsF());
790 EXPECT_EQ(13, TimeDelta::FromMilliseconds(13).InMilliseconds());
791 EXPECT_EQ(13.0, TimeDelta::FromMilliseconds(13).InMillisecondsF());
792 EXPECT_EQ(13, TimeDelta::FromSecondsD(13.1).InSeconds());
793 EXPECT_EQ(13.1, TimeDelta::FromSecondsD(13.1).InSecondsF());
794 EXPECT_EQ(13, TimeDelta::FromMillisecondsD(13.3).InMilliseconds());
795 EXPECT_EQ(13.3, TimeDelta::FromMillisecondsD(13.3).InMillisecondsF());
796 EXPECT_EQ(13, TimeDelta::FromMicroseconds(13).InMicroseconds());
799 #if defined(OS_POSIX)
800 TEST(TimeDelta, TimeSpecConversion) {
801 struct timespec result = TimeDelta::FromSeconds(0).ToTimeSpec();
802 EXPECT_EQ(result.tv_sec, 0);
803 EXPECT_EQ(result.tv_nsec, 0);
805 result = TimeDelta::FromSeconds(1).ToTimeSpec();
806 EXPECT_EQ(result.tv_sec, 1);
807 EXPECT_EQ(result.tv_nsec, 0);
809 result = TimeDelta::FromMicroseconds(1).ToTimeSpec();
810 EXPECT_EQ(result.tv_sec, 0);
811 EXPECT_EQ(result.tv_nsec, 1000);
813 result = TimeDelta::FromMicroseconds(
814 Time::kMicrosecondsPerSecond + 1).ToTimeSpec();
815 EXPECT_EQ(result.tv_sec, 1);
816 EXPECT_EQ(result.tv_nsec, 1000);
818 #endif // OS_POSIX
820 // Our internal time format is serialized in things like databases, so it's
821 // important that it's consistent across all our platforms. We use the 1601
822 // Windows epoch as the internal format across all platforms.
823 TEST(TimeDelta, WindowsEpoch) {
824 Time::Exploded exploded;
825 exploded.year = 1970;
826 exploded.month = 1;
827 exploded.day_of_week = 0; // Should be unusued.
828 exploded.day_of_month = 1;
829 exploded.hour = 0;
830 exploded.minute = 0;
831 exploded.second = 0;
832 exploded.millisecond = 0;
833 Time t = Time::FromUTCExploded(exploded);
834 // Unix 1970 epoch.
835 EXPECT_EQ(GG_INT64_C(11644473600000000), t.ToInternalValue());
837 // We can't test 1601 epoch, since the system time functions on Linux
838 // only compute years starting from 1900.
841 // We could define this separately for Time, TimeTicks and TimeDelta but the
842 // definitions would be identical anyway.
843 template <class Any>
844 std::string AnyToString(Any any) {
845 std::ostringstream oss;
846 oss << any;
847 return oss.str();
850 TEST(TimeDelta, Magnitude) {
851 const int64 zero = 0;
852 EXPECT_EQ(TimeDelta::FromMicroseconds(zero),
853 TimeDelta::FromMicroseconds(zero).magnitude());
855 const int64 one = 1;
856 const int64 negative_one = -1;
857 EXPECT_EQ(TimeDelta::FromMicroseconds(one),
858 TimeDelta::FromMicroseconds(one).magnitude());
859 EXPECT_EQ(TimeDelta::FromMicroseconds(one),
860 TimeDelta::FromMicroseconds(negative_one).magnitude());
862 const int64 max_int64_minus_one = std::numeric_limits<int64>::max() - 1;
863 const int64 min_int64_plus_two = std::numeric_limits<int64>::min() + 2;
864 EXPECT_EQ(TimeDelta::FromMicroseconds(max_int64_minus_one),
865 TimeDelta::FromMicroseconds(max_int64_minus_one).magnitude());
866 EXPECT_EQ(TimeDelta::FromMicroseconds(max_int64_minus_one),
867 TimeDelta::FromMicroseconds(min_int64_plus_two).magnitude());
870 TEST(TimeDeltaLogging, DCheckEqCompiles) {
871 DCHECK_EQ(TimeDelta(), TimeDelta());
874 TEST(TimeDeltaLogging, EmptyIsZero) {
875 TimeDelta zero;
876 EXPECT_EQ("0s", AnyToString(zero));
879 TEST(TimeDeltaLogging, FiveHundredMs) {
880 TimeDelta five_hundred_ms = TimeDelta::FromMilliseconds(500);
881 EXPECT_EQ("0.5s", AnyToString(five_hundred_ms));
884 TEST(TimeDeltaLogging, MinusTenSeconds) {
885 TimeDelta minus_ten_seconds = TimeDelta::FromSeconds(-10);
886 EXPECT_EQ("-10s", AnyToString(minus_ten_seconds));
889 TEST(TimeDeltaLogging, DoesNotMessUpFormattingFlags) {
890 std::ostringstream oss;
891 std::ios_base::fmtflags flags_before = oss.flags();
892 oss << TimeDelta();
893 EXPECT_EQ(flags_before, oss.flags());
896 TEST(TimeDeltaLogging, DoesNotMakeStreamBad) {
897 std::ostringstream oss;
898 oss << TimeDelta();
899 EXPECT_TRUE(oss.good());
902 TEST(TimeLogging, DCheckEqCompiles) {
903 DCHECK_EQ(Time(), Time());
906 TEST(TimeLogging, ChromeBirthdate) {
907 Time birthdate;
908 ASSERT_TRUE(Time::FromString("Tue, 02 Sep 2008 09:42:18 GMT", &birthdate));
909 EXPECT_EQ("2008-09-02 09:42:18.000 UTC", AnyToString(birthdate));
912 TEST(TimeLogging, DoesNotMessUpFormattingFlags) {
913 std::ostringstream oss;
914 std::ios_base::fmtflags flags_before = oss.flags();
915 oss << Time();
916 EXPECT_EQ(flags_before, oss.flags());
919 TEST(TimeLogging, DoesNotMakeStreamBad) {
920 std::ostringstream oss;
921 oss << Time();
922 EXPECT_TRUE(oss.good());
925 TEST(TimeTicksLogging, DCheckEqCompiles) {
926 DCHECK_EQ(TimeTicks(), TimeTicks());
929 TEST(TimeTicksLogging, ZeroTime) {
930 TimeTicks zero;
931 EXPECT_EQ("0 bogo-microseconds", AnyToString(zero));
934 TEST(TimeTicksLogging, FortyYearsLater) {
935 TimeTicks forty_years_later =
936 TimeTicks() + TimeDelta::FromDays(365.25 * 40);
937 EXPECT_EQ("1262304000000000 bogo-microseconds",
938 AnyToString(forty_years_later));
941 TEST(TimeTicksLogging, DoesNotMessUpFormattingFlags) {
942 std::ostringstream oss;
943 std::ios_base::fmtflags flags_before = oss.flags();
944 oss << TimeTicks();
945 EXPECT_EQ(flags_before, oss.flags());
948 TEST(TimeTicksLogging, DoesNotMakeStreamBad) {
949 std::ostringstream oss;
950 oss << TimeTicks();
951 EXPECT_TRUE(oss.good());
954 } // namespace
956 } // namespace base