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"
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"
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
{
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
33 struct tm local_comparison_tm
= {
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
);
63 localtime_s(&tms
, &now_t_1
);
64 #elif defined(OS_POSIX)
65 localtime_r(&now_t_1
, &tms
);
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
);
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;
169 const int BUFFER_SIZE
= 64;
170 struct tm local_time
= {0};
171 char time_buf
[BUFFER_SIZE
] = {0};
173 localtime_s(&local_time
, ¤t_time
);
174 asctime_s(time_buf
, arraysize(time_buf
), &local_time
);
175 #elif defined(OS_POSIX)
176 localtime_r(¤t_time
, &local_time
);
177 asctime_r(&local_time
, time_buf
);
181 EXPECT_TRUE(Time::FromString(time_buf
, &parsed_time
));
182 EXPECT_EQ(current_time
, parsed_time
.ToTimeT());
185 TEST_F(TimeTest
, DayOfWeekSunday
) {
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
) {
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
) {
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
) {
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
) {
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
) {
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
) {
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
) {
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
) {
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
) {
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
) {
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
) {
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
) {
267 // time_t == epoch == 0
268 EXPECT_TRUE(Time::FromString("Thu Jan 01 01:00:00 +0100 1970",
270 EXPECT_EQ(0, parsed_time
.ToTimeT());
271 EXPECT_TRUE(Time::FromString("Thu Jan 01 00:00:00 GMT 1970",
273 EXPECT_EQ(0, parsed_time
.ToTimeT());
276 TEST_F(TimeTest
, ParseTimeTestEpoch1
) {
279 // time_t == 1 second after epoch == 1
280 EXPECT_TRUE(Time::FromString("Thu Jan 01 01:00:01 +0100 1970",
282 EXPECT_EQ(1, parsed_time
.ToTimeT());
283 EXPECT_TRUE(Time::FromString("Thu Jan 01 00:00:01 GMT 1970",
285 EXPECT_EQ(1, parsed_time
.ToTimeT());
288 TEST_F(TimeTest
, ParseTimeTestEpoch2
) {
291 // time_t == 2 seconds after epoch == 2
292 EXPECT_TRUE(Time::FromString("Thu Jan 01 01:00:02 +0100 1970",
294 EXPECT_EQ(2, parsed_time
.ToTimeT());
295 EXPECT_TRUE(Time::FromString("Thu Jan 01 00:00:02 GMT 1970",
297 EXPECT_EQ(2, parsed_time
.ToTimeT());
300 TEST_F(TimeTest
, ParseTimeTestEpochNeg1
) {
303 // time_t == 1 second before epoch == -1
304 EXPECT_TRUE(Time::FromString("Thu Jan 01 00:59:59 +0100 1970",
306 EXPECT_EQ(-1, parsed_time
.ToTimeT());
307 EXPECT_TRUE(Time::FromString("Wed Dec 31 23:59:59 GMT 1969",
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
315 TEST_F(TimeTest
, ParseTimeTestEpochNotNeg1
) {
318 EXPECT_TRUE(Time::FromString("Wed Dec 31 23:59:59 GMT 2100",
320 EXPECT_NE(-1, parsed_time
.ToTimeT());
323 TEST_F(TimeTest
, ParseTimeTestEpochNeg2
) {
326 // time_t == 2 seconds before epoch == -2
327 EXPECT_TRUE(Time::FromString("Thu Jan 01 00:59:58 +0100 1970",
329 EXPECT_EQ(-2, parsed_time
.ToTimeT());
330 EXPECT_TRUE(Time::FromString("Wed Dec 31 23:59:58 GMT 1969",
332 EXPECT_EQ(-2, parsed_time
.ToTimeT());
335 TEST_F(TimeTest
, ParseTimeTestEpoch1960
) {
338 // time_t before Epoch, in 1960
339 EXPECT_TRUE(Time::FromString("Wed Jun 29 19:40:01 +0100 1960",
341 EXPECT_EQ(-299999999, parsed_time
.ToTimeT());
342 EXPECT_TRUE(Time::FromString("Wed Jun 29 18:40:01 GMT 1960",
344 EXPECT_EQ(-299999999, parsed_time
.ToTimeT());
345 EXPECT_TRUE(Time::FromString("Wed Jun 29 17:40:01 GMT 1960",
347 EXPECT_EQ(-300003599, parsed_time
.ToTimeT());
350 TEST_F(TimeTest
, ParseTimeTestEmpty
) {
352 EXPECT_FALSE(Time::FromString("", &parsed_time
));
355 TEST_F(TimeTest
, ParseTimeTestInvalidString
) {
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!).
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)
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,
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());
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
);
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());
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
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";
613 Time t
= Time::FromLocalExploded(midnight
);
614 EXPECT_EQ(1381633200, t
.ToTimeT());
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)
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())
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
659 const int kTargetGranularityUs
= 15000; // 15ms
661 bool success
= false;
662 int retries
= 100; // Arbitrary.
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.
670 delta
= GetTicks() - ticks_start
;
671 } while (delta
.InMilliseconds() == 0);
673 if (delta
.InMicroseconds() <= kTargetGranularityUs
)
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
691 #define MAYBE_ThreadNow ThreadNow
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);
726 timestamp
.SnappedToNextTick(phase
, interval
).ToInternalValue());
728 // Timestamp in next interval.
729 timestamp
= base::TimeTicks::FromInternalValue(4500);
731 timestamp
.SnappedToNextTick(phase
, interval
).ToInternalValue());
733 // Timestamp multiple intervals before.
734 timestamp
= base::TimeTicks::FromInternalValue(2500);
736 timestamp
.SnappedToNextTick(phase
, interval
).ToInternalValue());
738 // Timestamp multiple intervals after.
739 timestamp
= base::TimeTicks::FromInternalValue(6500);
741 timestamp
.SnappedToNextTick(phase
, interval
).ToInternalValue());
743 // Timestamp on previous interval.
744 timestamp
= base::TimeTicks::FromInternalValue(3000);
746 timestamp
.SnappedToNextTick(phase
, interval
).ToInternalValue());
748 // Timestamp on next interval.
749 timestamp
= base::TimeTicks::FromInternalValue(5000);
751 timestamp
.SnappedToNextTick(phase
, interval
).ToInternalValue());
753 // Timestamp equal to phase.
754 timestamp
= base::TimeTicks::FromInternalValue(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
)
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);
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;
827 exploded
.day_of_week
= 0; // Should be unusued.
828 exploded
.day_of_month
= 1;
832 exploded
.millisecond
= 0;
833 Time t
= Time::FromUTCExploded(exploded
);
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.
844 std::string
AnyToString(Any any
) {
845 std::ostringstream oss
;
850 TEST(TimeDelta
, Magnitude
) {
851 const int64 zero
= 0;
852 EXPECT_EQ(TimeDelta::FromMicroseconds(zero
),
853 TimeDelta::FromMicroseconds(zero
).magnitude());
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());
871 TEST(TimeDelta
, NumericOperators
) {
873 EXPECT_EQ(TimeDelta::FromMilliseconds(500),
874 TimeDelta::FromMilliseconds(1000) * d
);
875 EXPECT_EQ(TimeDelta::FromMilliseconds(2000),
876 TimeDelta::FromMilliseconds(1000) / d
);
877 EXPECT_EQ(TimeDelta::FromMilliseconds(500),
878 TimeDelta::FromMilliseconds(1000) *= d
);
879 EXPECT_EQ(TimeDelta::FromMilliseconds(2000),
880 TimeDelta::FromMilliseconds(1000) /= d
);
881 EXPECT_EQ(TimeDelta::FromMilliseconds(500),
882 d
* TimeDelta::FromMilliseconds(1000));
885 EXPECT_EQ(TimeDelta::FromMilliseconds(500),
886 TimeDelta::FromMilliseconds(1000) * f
);
887 EXPECT_EQ(TimeDelta::FromMilliseconds(2000),
888 TimeDelta::FromMilliseconds(1000) / f
);
889 EXPECT_EQ(TimeDelta::FromMilliseconds(500),
890 TimeDelta::FromMilliseconds(1000) *= f
);
891 EXPECT_EQ(TimeDelta::FromMilliseconds(2000),
892 TimeDelta::FromMilliseconds(1000) /= f
);
893 EXPECT_EQ(TimeDelta::FromMilliseconds(500),
894 f
* TimeDelta::FromMilliseconds(1000));
898 EXPECT_EQ(TimeDelta::FromMilliseconds(2000),
899 TimeDelta::FromMilliseconds(1000) * i
);
900 EXPECT_EQ(TimeDelta::FromMilliseconds(500),
901 TimeDelta::FromMilliseconds(1000) / i
);
902 EXPECT_EQ(TimeDelta::FromMilliseconds(2000),
903 TimeDelta::FromMilliseconds(1000) *= i
);
904 EXPECT_EQ(TimeDelta::FromMilliseconds(500),
905 TimeDelta::FromMilliseconds(1000) /= i
);
906 EXPECT_EQ(TimeDelta::FromMilliseconds(2000),
907 i
* TimeDelta::FromMilliseconds(1000));
910 EXPECT_EQ(TimeDelta::FromMilliseconds(2000),
911 TimeDelta::FromMilliseconds(1000) * i64
);
912 EXPECT_EQ(TimeDelta::FromMilliseconds(500),
913 TimeDelta::FromMilliseconds(1000) / i64
);
914 EXPECT_EQ(TimeDelta::FromMilliseconds(2000),
915 TimeDelta::FromMilliseconds(1000) *= i64
);
916 EXPECT_EQ(TimeDelta::FromMilliseconds(500),
917 TimeDelta::FromMilliseconds(1000) /= i64
);
918 EXPECT_EQ(TimeDelta::FromMilliseconds(2000),
919 i64
* TimeDelta::FromMilliseconds(1000));
922 EXPECT_EQ(TimeDelta::FromMilliseconds(500),
923 TimeDelta::FromMilliseconds(1000) * 0.5);
924 EXPECT_EQ(TimeDelta::FromMilliseconds(2000),
925 TimeDelta::FromMilliseconds(1000) / 0.5);
926 EXPECT_EQ(TimeDelta::FromMilliseconds(500),
927 TimeDelta::FromMilliseconds(1000) *= 0.5);
928 EXPECT_EQ(TimeDelta::FromMilliseconds(2000),
929 TimeDelta::FromMilliseconds(1000) /= 0.5);
930 EXPECT_EQ(TimeDelta::FromMilliseconds(500),
931 0.5 * TimeDelta::FromMilliseconds(1000));
933 EXPECT_EQ(TimeDelta::FromMilliseconds(2000),
934 TimeDelta::FromMilliseconds(1000) * 2);
935 EXPECT_EQ(TimeDelta::FromMilliseconds(500),
936 TimeDelta::FromMilliseconds(1000) / 2);
937 EXPECT_EQ(TimeDelta::FromMilliseconds(2000),
938 TimeDelta::FromMilliseconds(1000) *= 2);
939 EXPECT_EQ(TimeDelta::FromMilliseconds(500),
940 TimeDelta::FromMilliseconds(1000) /= 2);
941 EXPECT_EQ(TimeDelta::FromMilliseconds(2000),
942 2 * TimeDelta::FromMilliseconds(1000));
945 bool IsMin(TimeDelta delta
) {
946 return (-delta
).is_max();
949 TEST(TimeDelta
, Overflows
) {
950 // Some sanity checks.
951 EXPECT_TRUE(TimeDelta::Max().is_max());
952 EXPECT_TRUE(IsMin(-TimeDelta::Max()));
953 EXPECT_GT(TimeDelta(), -TimeDelta::Max());
955 TimeDelta large_delta
= TimeDelta::Max() - TimeDelta::FromMilliseconds(1);
956 TimeDelta large_negative
= -large_delta
;
957 EXPECT_GT(TimeDelta(), large_negative
);
958 EXPECT_FALSE(large_delta
.is_max());
959 EXPECT_FALSE(IsMin(-large_negative
));
960 TimeDelta one_second
= TimeDelta::FromSeconds(1);
962 // Test +, -, * and / operators.
963 EXPECT_TRUE((large_delta
+ one_second
).is_max());
964 EXPECT_TRUE(IsMin(large_negative
+ (-one_second
)));
965 EXPECT_TRUE(IsMin(large_negative
- one_second
));
966 EXPECT_TRUE((large_delta
- (-one_second
)).is_max());
967 EXPECT_TRUE((large_delta
* 2).is_max());
968 EXPECT_TRUE(IsMin(large_delta
* -2));
969 EXPECT_TRUE((large_delta
/ 0.5).is_max());
970 EXPECT_TRUE(IsMin(large_delta
/ -0.5));
972 // Test +=, -=, *= and /= operators.
973 TimeDelta delta
= large_delta
;
975 EXPECT_TRUE(delta
.is_max());
976 delta
= large_negative
;
977 delta
+= -one_second
;
978 EXPECT_TRUE(IsMin(delta
));
980 delta
= large_negative
;
982 EXPECT_TRUE(IsMin(delta
));
984 delta
-= -one_second
;
985 EXPECT_TRUE(delta
.is_max());
989 EXPECT_TRUE(delta
.is_max());
990 delta
= large_negative
;
992 EXPECT_TRUE(IsMin(delta
));
996 EXPECT_TRUE(delta
.is_max());
997 delta
= large_negative
;
999 EXPECT_TRUE(IsMin(delta
));
1001 // Test operations with Time and TimeTicks.
1002 EXPECT_TRUE((large_delta
+ Time::Now()).is_max());
1003 EXPECT_TRUE((large_delta
+ TimeTicks::Now()).is_max());
1004 EXPECT_TRUE((Time::Now() + large_delta
).is_max());
1005 EXPECT_TRUE((TimeTicks::Now() + large_delta
).is_max());
1007 Time time_now
= Time::Now();
1008 EXPECT_EQ(one_second
, (time_now
+ one_second
) - time_now
);
1009 EXPECT_EQ(-one_second
, (time_now
- one_second
) - time_now
);
1011 TimeTicks ticks_now
= TimeTicks::Now();
1012 EXPECT_EQ(-one_second
, (ticks_now
- one_second
) - ticks_now
);
1013 EXPECT_EQ(one_second
, (ticks_now
+ one_second
) - ticks_now
);
1016 TEST(TimeDeltaLogging
, DCheckEqCompiles
) {
1017 DCHECK_EQ(TimeDelta(), TimeDelta());
1020 TEST(TimeDeltaLogging
, EmptyIsZero
) {
1022 EXPECT_EQ("0s", AnyToString(zero
));
1025 TEST(TimeDeltaLogging
, FiveHundredMs
) {
1026 TimeDelta five_hundred_ms
= TimeDelta::FromMilliseconds(500);
1027 EXPECT_EQ("0.5s", AnyToString(five_hundred_ms
));
1030 TEST(TimeDeltaLogging
, MinusTenSeconds
) {
1031 TimeDelta minus_ten_seconds
= TimeDelta::FromSeconds(-10);
1032 EXPECT_EQ("-10s", AnyToString(minus_ten_seconds
));
1035 TEST(TimeDeltaLogging
, DoesNotMessUpFormattingFlags
) {
1036 std::ostringstream oss
;
1037 std::ios_base::fmtflags flags_before
= oss
.flags();
1039 EXPECT_EQ(flags_before
, oss
.flags());
1042 TEST(TimeDeltaLogging
, DoesNotMakeStreamBad
) {
1043 std::ostringstream oss
;
1045 EXPECT_TRUE(oss
.good());
1048 TEST(TimeLogging
, DCheckEqCompiles
) {
1049 DCHECK_EQ(Time(), Time());
1052 TEST(TimeLogging
, ChromeBirthdate
) {
1054 ASSERT_TRUE(Time::FromString("Tue, 02 Sep 2008 09:42:18 GMT", &birthdate
));
1055 EXPECT_EQ("2008-09-02 09:42:18.000 UTC", AnyToString(birthdate
));
1058 TEST(TimeLogging
, DoesNotMessUpFormattingFlags
) {
1059 std::ostringstream oss
;
1060 std::ios_base::fmtflags flags_before
= oss
.flags();
1062 EXPECT_EQ(flags_before
, oss
.flags());
1065 TEST(TimeLogging
, DoesNotMakeStreamBad
) {
1066 std::ostringstream oss
;
1068 EXPECT_TRUE(oss
.good());
1071 TEST(TimeTicksLogging
, DCheckEqCompiles
) {
1072 DCHECK_EQ(TimeTicks(), TimeTicks());
1075 TEST(TimeTicksLogging
, ZeroTime
) {
1077 EXPECT_EQ("0 bogo-microseconds", AnyToString(zero
));
1080 TEST(TimeTicksLogging
, FortyYearsLater
) {
1081 TimeTicks forty_years_later
=
1082 TimeTicks() + TimeDelta::FromDays(365.25 * 40);
1083 EXPECT_EQ("1262304000000000 bogo-microseconds",
1084 AnyToString(forty_years_later
));
1087 TEST(TimeTicksLogging
, DoesNotMessUpFormattingFlags
) {
1088 std::ostringstream oss
;
1089 std::ios_base::fmtflags flags_before
= oss
.flags();
1091 EXPECT_EQ(flags_before
, oss
.flags());
1094 TEST(TimeTicksLogging
, DoesNotMakeStreamBad
) {
1095 std::ostringstream oss
;
1097 EXPECT_TRUE(oss
.good());