Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / base / time / time_unittest.cc
blob63c3a1ac7718fe1cc5f0912b4af78f8d24a60627
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 "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"
16 using base::Time;
17 using base::TimeDelta;
18 using base::TimeTicks;
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
42 time_t converted_time = mktime(&local_comparison_tm);
43 ASSERT_GT(converted_time, 0);
44 comparison_time_local_ = Time::FromTimeT(converted_time);
46 // time_t representation of 15th Oct 2007 12:45:00 PDT
47 comparison_time_pdt_ = Time::FromTimeT(1192477500);
50 Time comparison_time_local_;
51 Time comparison_time_pdt_;
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
65 // Convert to ours.
66 Time our_time_1 = Time::FromTimeT(now_t_1);
67 Time::Exploded exploded;
68 our_time_1.LocalExplode(&exploded);
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);
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);
82 time_t now_t_2 = our_time_2.ToTimeT();
83 EXPECT_EQ(now_t_1, now_t_2);
85 EXPECT_EQ(10, Time().FromTimeT(10).ToTimeT());
86 EXPECT_EQ(10.0, Time().FromTimeT(10).ToDoubleT());
88 // Conversions of 0 should stay 0.
89 EXPECT_EQ(0, Time().ToTimeT());
90 EXPECT_EQ(0, Time::FromTimeT(0).ToInternalValue());
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());
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);
109 #endif // OS_POSIX
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);
124 TEST_F(TimeTest, ZeroIsSymmetric) {
125 Time zero_time(Time::FromTimeT(0));
126 EXPECT_EQ(0, zero_time.ToTimeT());
128 EXPECT_EQ(0.0, zero_time.ToDoubleT());
131 TEST_F(TimeTest, LocalExplode) {
132 Time a = Time::Now();
133 Time::Exploded exploded;
134 a.LocalExplode(&exploded);
136 Time b = Time::FromLocalExploded(exploded);
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));
144 TEST_F(TimeTest, UTCExplode) {
145 Time a = Time::Now();
146 Time::Exploded exploded;
147 a.UTCExplode(&exploded);
149 Time b = Time::FromUTCExploded(exploded);
150 EXPECT_TRUE((a - b) < TimeDelta::FromSeconds(1));
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);
162 TEST_F(TimeTest, ParseTimeTest1) {
163 time_t current_time = 0;
164 time(&current_time);
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, &current_time);
171 asctime_s(time_buf, arraysize(time_buf), &local_time);
172 #elif defined(OS_POSIX)
173 localtime_r(&current_time, &local_time);
174 asctime_r(&local_time, time_buf);
175 #endif
177 Time parsed_time;
178 EXPECT_TRUE(Time::FromString(time_buf, &parsed_time));
179 EXPECT_EQ(current_time, parsed_time.ToTimeT());
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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_);
260 // Test some of edge cases around epoch, etc.
261 TEST_F(TimeTest, ParseTimeTestEpoch0) {
262 Time parsed_time;
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());
273 TEST_F(TimeTest, ParseTimeTestEpoch1) {
274 Time parsed_time;
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());
285 TEST_F(TimeTest, ParseTimeTestEpoch2) {
286 Time parsed_time;
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());
297 TEST_F(TimeTest, ParseTimeTestEpochNeg1) {
298 Time parsed_time;
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());
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;
315 EXPECT_TRUE(Time::FromString("Wed Dec 31 23:59:59 GMT 2100",
316 &parsed_time));
317 EXPECT_NE(-1, parsed_time.ToTimeT());
320 TEST_F(TimeTest, ParseTimeTestEpochNeg2) {
321 Time parsed_time;
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());
332 TEST_F(TimeTest, ParseTimeTestEpoch1960) {
333 Time parsed_time;
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());
347 TEST_F(TimeTest, ParseTimeTestEmpty) {
348 Time parsed_time;
349 EXPECT_FALSE(Time::FromString("", &parsed_time));
352 TEST_F(TimeTest, ParseTimeTestInvalidString) {
353 Time parsed_time;
354 EXPECT_FALSE(Time::FromString("Monday morning 2000", &parsed_time));
357 TEST_F(TimeTest, ExplodeBeforeUnixEpoch) {
358 static const int kUnixEpochYear = 1970; // In case this changes (ha!).
359 Time t;
360 Time::Exploded exploded;
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
484 TEST_F(TimeTest, TimeDeltaMax) {
485 TimeDelta max = TimeDelta::Max();
486 EXPECT_TRUE(max.is_max());
487 EXPECT_EQ(max, TimeDelta::Max());
488 EXPECT_GT(max, TimeDelta::FromDays(100 * 365));
489 EXPECT_GT(max, TimeDelta());
492 TEST_F(TimeTest, TimeDeltaMaxConversions) {
493 TimeDelta t = TimeDelta::Max();
494 EXPECT_EQ(std::numeric_limits<int64>::max(), t.ToInternalValue());
496 EXPECT_EQ(std::numeric_limits<int>::max(), t.InDays());
497 EXPECT_EQ(std::numeric_limits<int>::max(), t.InHours());
498 EXPECT_EQ(std::numeric_limits<int>::max(), t.InMinutes());
499 EXPECT_EQ(std::numeric_limits<double>::infinity(), t.InSecondsF());
500 EXPECT_EQ(std::numeric_limits<int64>::max(), t.InSeconds());
501 EXPECT_EQ(std::numeric_limits<double>::infinity(), t.InMillisecondsF());
502 EXPECT_EQ(std::numeric_limits<int64>::max(), t.InMilliseconds());
503 EXPECT_EQ(std::numeric_limits<int64>::max(), t.InMillisecondsRoundedUp());
505 t = TimeDelta::FromDays(std::numeric_limits<int>::max());
506 EXPECT_TRUE(t.is_max());
508 t = TimeDelta::FromHours(std::numeric_limits<int>::max());
509 EXPECT_TRUE(t.is_max());
511 t = TimeDelta::FromMinutes(std::numeric_limits<int>::max());
512 EXPECT_TRUE(t.is_max());
514 t = TimeDelta::FromSeconds(std::numeric_limits<int64>::max());
515 EXPECT_TRUE(t.is_max());
517 t = TimeDelta::FromMilliseconds(std::numeric_limits<int64>::max());
518 EXPECT_TRUE(t.is_max());
520 t = TimeDelta::FromSecondsD(std::numeric_limits<double>::infinity());
521 EXPECT_TRUE(t.is_max());
523 t = TimeDelta::FromMillisecondsD(std::numeric_limits<double>::infinity());
524 EXPECT_TRUE(t.is_max());
526 t = TimeDelta::FromMicroseconds(std::numeric_limits<int64>::max());
527 EXPECT_TRUE(t.is_max());
530 TEST_F(TimeTest, Max) {
531 Time max = Time::Max();
532 EXPECT_TRUE(max.is_max());
533 EXPECT_EQ(max, Time::Max());
534 EXPECT_GT(max, Time::Now());
535 EXPECT_GT(max, Time());
538 TEST_F(TimeTest, MaxConversions) {
539 Time t = Time::Max();
540 EXPECT_EQ(std::numeric_limits<int64>::max(), t.ToInternalValue());
542 t = Time::FromDoubleT(std::numeric_limits<double>::infinity());
543 EXPECT_TRUE(t.is_max());
544 EXPECT_EQ(std::numeric_limits<double>::infinity(), t.ToDoubleT());
546 t = Time::FromJsTime(std::numeric_limits<double>::infinity());
547 EXPECT_TRUE(t.is_max());
548 EXPECT_EQ(std::numeric_limits<double>::infinity(), t.ToJsTime());
550 t = Time::FromTimeT(std::numeric_limits<time_t>::max());
551 EXPECT_TRUE(t.is_max());
552 EXPECT_EQ(std::numeric_limits<time_t>::max(), t.ToTimeT());
554 #if defined(OS_POSIX)
555 struct timeval tval;
556 tval.tv_sec = std::numeric_limits<time_t>::max();
557 tval.tv_usec = static_cast<suseconds_t>(Time::kMicrosecondsPerSecond) - 1;
558 t = Time::FromTimeVal(tval);
559 EXPECT_TRUE(t.is_max());
560 tval = t.ToTimeVal();
561 EXPECT_EQ(std::numeric_limits<time_t>::max(), tval.tv_sec);
562 EXPECT_EQ(static_cast<suseconds_t>(Time::kMicrosecondsPerSecond) - 1,
563 tval.tv_usec);
564 #endif
566 #if defined(OS_MACOSX)
567 t = Time::FromCFAbsoluteTime(std::numeric_limits<CFAbsoluteTime>::infinity());
568 EXPECT_TRUE(t.is_max());
569 EXPECT_EQ(std::numeric_limits<CFAbsoluteTime>::infinity(),
570 t.ToCFAbsoluteTime());
571 #endif
573 #if defined(OS_WIN)
574 FILETIME ftime;
575 ftime.dwHighDateTime = std::numeric_limits<DWORD>::max();
576 ftime.dwLowDateTime = std::numeric_limits<DWORD>::max();
577 t = Time::FromFileTime(ftime);
578 EXPECT_TRUE(t.is_max());
579 ftime = t.ToFileTime();
580 EXPECT_EQ(std::numeric_limits<DWORD>::max(), ftime.dwHighDateTime);
581 EXPECT_EQ(std::numeric_limits<DWORD>::max(), ftime.dwLowDateTime);
582 #endif
585 #if defined(OS_MACOSX)
586 TEST_F(TimeTest, TimeTOverflow) {
587 Time t = Time::FromInternalValue(std::numeric_limits<int64>::max() - 1);
588 EXPECT_FALSE(t.is_max());
589 EXPECT_EQ(std::numeric_limits<time_t>::max(), t.ToTimeT());
591 #endif
593 #if defined(OS_ANDROID)
594 TEST_F(TimeTest, FromLocalExplodedCrashOnAndroid) {
595 // This crashed inside Time:: FromLocalExploded() on Android 4.1.2.
596 // See http://crbug.com/287821
597 Time::Exploded midnight = {2013, // year
598 10, // month
599 0, // day_of_week
600 13, // day_of_month
601 0, // hour
602 0, // minute
603 0, // second
605 // The string passed to putenv() must be a char* and the documentation states
606 // that it 'becomes part of the environment', so use a static buffer.
607 static char buffer[] = "TZ=America/Santiago";
608 putenv(buffer);
609 tzset();
610 Time t = Time::FromLocalExploded(midnight);
611 EXPECT_EQ(1381633200, t.ToTimeT());
613 #endif // OS_ANDROID
615 TEST(TimeTicks, Deltas) {
616 for (int index = 0; index < 50; index++) {
617 TimeTicks ticks_start = TimeTicks::Now();
618 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(10));
619 TimeTicks ticks_stop = TimeTicks::Now();
620 TimeDelta delta = ticks_stop - ticks_start;
621 // Note: Although we asked for a 10ms sleep, if the
622 // time clock has a finer granularity than the Sleep()
623 // clock, it is quite possible to wakeup early. Here
624 // is how that works:
625 // Time(ms timer) Time(us timer)
626 // 5 5010
627 // 6 6010
628 // 7 7010
629 // 8 8010
630 // 9 9000
631 // Elapsed 4ms 3990us
633 // Unfortunately, our InMilliseconds() function truncates
634 // rather than rounds. We should consider fixing this
635 // so that our averages come out better.
636 EXPECT_GE(delta.InMilliseconds(), 9);
637 EXPECT_GE(delta.InMicroseconds(), 9000);
638 EXPECT_EQ(delta.InSeconds(), 0);
642 static void HighResClockTest(TimeTicks (*GetTicks)()) {
643 #if defined(OS_WIN)
644 // HighResNow doesn't work on some systems. Since the product still works
645 // even if it doesn't work, it makes this entire test questionable.
646 if (!TimeTicks::IsHighResClockWorking())
647 return;
648 #endif
650 // Why do we loop here?
651 // We're trying to measure that intervals increment in a VERY small amount
652 // of time -- less than 15ms. Unfortunately, if we happen to have a
653 // context switch in the middle of our test, the context switch could easily
654 // exceed our limit. So, we iterate on this several times. As long as we're
655 // able to detect the fine-granularity timers at least once, then the test
656 // has succeeded.
658 const int kTargetGranularityUs = 15000; // 15ms
660 bool success = false;
661 int retries = 100; // Arbitrary.
662 TimeDelta delta;
663 while (!success && retries--) {
664 TimeTicks ticks_start = GetTicks();
665 // Loop until we can detect that the clock has changed. Non-HighRes timers
666 // will increment in chunks, e.g. 15ms. By spinning until we see a clock
667 // change, we detect the minimum time between measurements.
668 do {
669 delta = GetTicks() - ticks_start;
670 } while (delta.InMilliseconds() == 0);
672 if (delta.InMicroseconds() <= kTargetGranularityUs)
673 success = true;
676 // In high resolution mode, we expect to see the clock increment
677 // in intervals less than 15ms.
678 EXPECT_TRUE(success);
681 TEST(TimeTicks, HighResNow) {
682 HighResClockTest(&TimeTicks::HighResNow);
685 // Fails frequently on Android http://crbug.com/352633 with:
686 // Expected: (delta_thread.InMicroseconds()) > (0), actual: 0 vs 0
687 #if defined(OS_ANDROID)
688 #define MAYBE_ThreadNow DISABLED_ThreadNow
689 #else
690 #define MAYBE_ThreadNow ThreadNow
691 #endif
692 TEST(TimeTicks, MAYBE_ThreadNow) {
693 if (TimeTicks::IsThreadNowSupported()) {
694 TimeTicks begin = TimeTicks::Now();
695 TimeTicks begin_thread = TimeTicks::ThreadNow();
696 // Make sure that ThreadNow value is non-zero.
697 EXPECT_GT(begin_thread, TimeTicks());
698 // Sleep for 10 milliseconds to get the thread de-scheduled.
699 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(10));
700 TimeTicks end_thread = TimeTicks::ThreadNow();
701 TimeTicks end = TimeTicks::Now();
702 TimeDelta delta = end - begin;
703 TimeDelta delta_thread = end_thread - begin_thread;
704 // Make sure that some thread time have elapsed.
705 EXPECT_GT(delta_thread.InMicroseconds(), 0);
706 // But the thread time is at least 9ms less than clock time.
707 TimeDelta difference = delta - delta_thread;
708 EXPECT_GE(difference.InMicroseconds(), 9000);
712 TEST(TimeTicks, NowFromSystemTraceTime) {
713 // Re-use HighResNow test for now since clock properties are identical.
714 HighResClockTest(&TimeTicks::NowFromSystemTraceTime);
717 TEST(TimeDelta, FromAndIn) {
718 EXPECT_TRUE(TimeDelta::FromDays(2) == TimeDelta::FromHours(48));
719 EXPECT_TRUE(TimeDelta::FromHours(3) == TimeDelta::FromMinutes(180));
720 EXPECT_TRUE(TimeDelta::FromMinutes(2) == TimeDelta::FromSeconds(120));
721 EXPECT_TRUE(TimeDelta::FromSeconds(2) == TimeDelta::FromMilliseconds(2000));
722 EXPECT_TRUE(TimeDelta::FromMilliseconds(2) ==
723 TimeDelta::FromMicroseconds(2000));
724 EXPECT_TRUE(TimeDelta::FromSecondsD(2.3) ==
725 TimeDelta::FromMilliseconds(2300));
726 EXPECT_TRUE(TimeDelta::FromMillisecondsD(2.5) ==
727 TimeDelta::FromMicroseconds(2500));
728 EXPECT_EQ(13, TimeDelta::FromDays(13).InDays());
729 EXPECT_EQ(13, TimeDelta::FromHours(13).InHours());
730 EXPECT_EQ(13, TimeDelta::FromMinutes(13).InMinutes());
731 EXPECT_EQ(13, TimeDelta::FromSeconds(13).InSeconds());
732 EXPECT_EQ(13.0, TimeDelta::FromSeconds(13).InSecondsF());
733 EXPECT_EQ(13, TimeDelta::FromMilliseconds(13).InMilliseconds());
734 EXPECT_EQ(13.0, TimeDelta::FromMilliseconds(13).InMillisecondsF());
735 EXPECT_EQ(13, TimeDelta::FromSecondsD(13.1).InSeconds());
736 EXPECT_EQ(13.1, TimeDelta::FromSecondsD(13.1).InSecondsF());
737 EXPECT_EQ(13, TimeDelta::FromMillisecondsD(13.3).InMilliseconds());
738 EXPECT_EQ(13.3, TimeDelta::FromMillisecondsD(13.3).InMillisecondsF());
739 EXPECT_EQ(13, TimeDelta::FromMicroseconds(13).InMicroseconds());
742 #if defined(OS_POSIX)
743 TEST(TimeDelta, TimeSpecConversion) {
744 struct timespec result = TimeDelta::FromSeconds(0).ToTimeSpec();
745 EXPECT_EQ(result.tv_sec, 0);
746 EXPECT_EQ(result.tv_nsec, 0);
748 result = TimeDelta::FromSeconds(1).ToTimeSpec();
749 EXPECT_EQ(result.tv_sec, 1);
750 EXPECT_EQ(result.tv_nsec, 0);
752 result = TimeDelta::FromMicroseconds(1).ToTimeSpec();
753 EXPECT_EQ(result.tv_sec, 0);
754 EXPECT_EQ(result.tv_nsec, 1000);
756 result = TimeDelta::FromMicroseconds(
757 Time::kMicrosecondsPerSecond + 1).ToTimeSpec();
758 EXPECT_EQ(result.tv_sec, 1);
759 EXPECT_EQ(result.tv_nsec, 1000);
761 #endif // OS_POSIX
763 // Our internal time format is serialized in things like databases, so it's
764 // important that it's consistent across all our platforms. We use the 1601
765 // Windows epoch as the internal format across all platforms.
766 TEST(TimeDelta, WindowsEpoch) {
767 Time::Exploded exploded;
768 exploded.year = 1970;
769 exploded.month = 1;
770 exploded.day_of_week = 0; // Should be unusued.
771 exploded.day_of_month = 1;
772 exploded.hour = 0;
773 exploded.minute = 0;
774 exploded.second = 0;
775 exploded.millisecond = 0;
776 Time t = Time::FromUTCExploded(exploded);
777 // Unix 1970 epoch.
778 EXPECT_EQ(GG_INT64_C(11644473600000000), t.ToInternalValue());
780 // We can't test 1601 epoch, since the system time functions on Linux
781 // only compute years starting from 1900.