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(ThreadTicks
, MAYBE_ThreadNow
) {
694 if (ThreadTicks::IsSupported()) {
695 TimeTicks begin
= TimeTicks::Now();
696 ThreadTicks begin_thread
= ThreadTicks::Now();
697 // Make sure that ThreadNow value is non-zero.
698 EXPECT_GT(begin_thread
, ThreadTicks());
699 // Sleep for 10 milliseconds to get the thread de-scheduled.
700 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(10));
701 ThreadTicks end_thread
= ThreadTicks::Now();
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(TraceTicks
, NowFromSystemTraceTime
) {
714 // Re-use HighRes test for now since clock properties are identical.
715 using NowFunction
= TimeTicks (*)(void);
716 HighResClockTest(reinterpret_cast<NowFunction
>(&TraceTicks::Now
));
719 TEST(TimeTicks
, SnappedToNextTickBasic
) {
720 base::TimeTicks phase
= base::TimeTicks::FromInternalValue(4000);
721 base::TimeDelta interval
= base::TimeDelta::FromMicroseconds(1000);
722 base::TimeTicks timestamp
;
724 // Timestamp in previous interval.
725 timestamp
= base::TimeTicks::FromInternalValue(3500);
727 timestamp
.SnappedToNextTick(phase
, interval
).ToInternalValue());
729 // Timestamp in next interval.
730 timestamp
= base::TimeTicks::FromInternalValue(4500);
732 timestamp
.SnappedToNextTick(phase
, interval
).ToInternalValue());
734 // Timestamp multiple intervals before.
735 timestamp
= base::TimeTicks::FromInternalValue(2500);
737 timestamp
.SnappedToNextTick(phase
, interval
).ToInternalValue());
739 // Timestamp multiple intervals after.
740 timestamp
= base::TimeTicks::FromInternalValue(6500);
742 timestamp
.SnappedToNextTick(phase
, interval
).ToInternalValue());
744 // Timestamp on previous interval.
745 timestamp
= base::TimeTicks::FromInternalValue(3000);
747 timestamp
.SnappedToNextTick(phase
, interval
).ToInternalValue());
749 // Timestamp on next interval.
750 timestamp
= base::TimeTicks::FromInternalValue(5000);
752 timestamp
.SnappedToNextTick(phase
, interval
).ToInternalValue());
754 // Timestamp equal to phase.
755 timestamp
= base::TimeTicks::FromInternalValue(4000);
757 timestamp
.SnappedToNextTick(phase
, interval
).ToInternalValue());
760 TEST(TimeTicks
, SnappedToNextTickOverflow
) {
761 // int(big_timestamp / interval) < 0, so this causes a crash if the number of
762 // intervals elapsed is attempted to be stored in an int.
763 base::TimeTicks phase
= base::TimeTicks::FromInternalValue(0);
764 base::TimeDelta interval
= base::TimeDelta::FromMicroseconds(4000);
765 base::TimeTicks big_timestamp
=
766 base::TimeTicks::FromInternalValue(8635916564000);
768 EXPECT_EQ(8635916564000,
769 big_timestamp
.SnappedToNextTick(phase
, interval
).ToInternalValue());
770 EXPECT_EQ(8635916564000,
771 big_timestamp
.SnappedToNextTick(big_timestamp
, interval
)
775 TEST(TimeDelta
, FromAndIn
) {
776 EXPECT_TRUE(TimeDelta::FromDays(2) == TimeDelta::FromHours(48));
777 EXPECT_TRUE(TimeDelta::FromHours(3) == TimeDelta::FromMinutes(180));
778 EXPECT_TRUE(TimeDelta::FromMinutes(2) == TimeDelta::FromSeconds(120));
779 EXPECT_TRUE(TimeDelta::FromSeconds(2) == TimeDelta::FromMilliseconds(2000));
780 EXPECT_TRUE(TimeDelta::FromMilliseconds(2) ==
781 TimeDelta::FromMicroseconds(2000));
782 EXPECT_TRUE(TimeDelta::FromSecondsD(2.3) ==
783 TimeDelta::FromMilliseconds(2300));
784 EXPECT_TRUE(TimeDelta::FromMillisecondsD(2.5) ==
785 TimeDelta::FromMicroseconds(2500));
786 EXPECT_EQ(13, TimeDelta::FromDays(13).InDays());
787 EXPECT_EQ(13, TimeDelta::FromHours(13).InHours());
788 EXPECT_EQ(13, TimeDelta::FromMinutes(13).InMinutes());
789 EXPECT_EQ(13, TimeDelta::FromSeconds(13).InSeconds());
790 EXPECT_EQ(13.0, TimeDelta::FromSeconds(13).InSecondsF());
791 EXPECT_EQ(13, TimeDelta::FromMilliseconds(13).InMilliseconds());
792 EXPECT_EQ(13.0, TimeDelta::FromMilliseconds(13).InMillisecondsF());
793 EXPECT_EQ(13, TimeDelta::FromSecondsD(13.1).InSeconds());
794 EXPECT_EQ(13.1, TimeDelta::FromSecondsD(13.1).InSecondsF());
795 EXPECT_EQ(13, TimeDelta::FromMillisecondsD(13.3).InMilliseconds());
796 EXPECT_EQ(13.3, TimeDelta::FromMillisecondsD(13.3).InMillisecondsF());
797 EXPECT_EQ(13, TimeDelta::FromMicroseconds(13).InMicroseconds());
800 #if defined(OS_POSIX)
801 TEST(TimeDelta
, TimeSpecConversion
) {
802 struct timespec result
= TimeDelta::FromSeconds(0).ToTimeSpec();
803 EXPECT_EQ(result
.tv_sec
, 0);
804 EXPECT_EQ(result
.tv_nsec
, 0);
806 result
= TimeDelta::FromSeconds(1).ToTimeSpec();
807 EXPECT_EQ(result
.tv_sec
, 1);
808 EXPECT_EQ(result
.tv_nsec
, 0);
810 result
= TimeDelta::FromMicroseconds(1).ToTimeSpec();
811 EXPECT_EQ(result
.tv_sec
, 0);
812 EXPECT_EQ(result
.tv_nsec
, 1000);
814 result
= TimeDelta::FromMicroseconds(
815 Time::kMicrosecondsPerSecond
+ 1).ToTimeSpec();
816 EXPECT_EQ(result
.tv_sec
, 1);
817 EXPECT_EQ(result
.tv_nsec
, 1000);
821 // Our internal time format is serialized in things like databases, so it's
822 // important that it's consistent across all our platforms. We use the 1601
823 // Windows epoch as the internal format across all platforms.
824 TEST(TimeDelta
, WindowsEpoch
) {
825 Time::Exploded exploded
;
826 exploded
.year
= 1970;
828 exploded
.day_of_week
= 0; // Should be unusued.
829 exploded
.day_of_month
= 1;
833 exploded
.millisecond
= 0;
834 Time t
= Time::FromUTCExploded(exploded
);
836 EXPECT_EQ(INT64_C(11644473600000000), t
.ToInternalValue());
838 // We can't test 1601 epoch, since the system time functions on Linux
839 // only compute years starting from 1900.
842 // We could define this separately for Time, TimeTicks and TimeDelta but the
843 // definitions would be identical anyway.
845 std::string
AnyToString(Any any
) {
846 std::ostringstream oss
;
851 TEST(TimeDelta
, Magnitude
) {
852 const int64 zero
= 0;
853 EXPECT_EQ(TimeDelta::FromMicroseconds(zero
),
854 TimeDelta::FromMicroseconds(zero
).magnitude());
857 const int64 negative_one
= -1;
858 EXPECT_EQ(TimeDelta::FromMicroseconds(one
),
859 TimeDelta::FromMicroseconds(one
).magnitude());
860 EXPECT_EQ(TimeDelta::FromMicroseconds(one
),
861 TimeDelta::FromMicroseconds(negative_one
).magnitude());
863 const int64 max_int64_minus_one
= std::numeric_limits
<int64
>::max() - 1;
864 const int64 min_int64_plus_two
= std::numeric_limits
<int64
>::min() + 2;
865 EXPECT_EQ(TimeDelta::FromMicroseconds(max_int64_minus_one
),
866 TimeDelta::FromMicroseconds(max_int64_minus_one
).magnitude());
867 EXPECT_EQ(TimeDelta::FromMicroseconds(max_int64_minus_one
),
868 TimeDelta::FromMicroseconds(min_int64_plus_two
).magnitude());
872 TEST(TimeDelta
, NumericOperators
) {
874 EXPECT_EQ(TimeDelta::FromMilliseconds(500),
875 TimeDelta::FromMilliseconds(1000) * d
);
876 EXPECT_EQ(TimeDelta::FromMilliseconds(2000),
877 TimeDelta::FromMilliseconds(1000) / d
);
878 EXPECT_EQ(TimeDelta::FromMilliseconds(500),
879 TimeDelta::FromMilliseconds(1000) *= d
);
880 EXPECT_EQ(TimeDelta::FromMilliseconds(2000),
881 TimeDelta::FromMilliseconds(1000) /= d
);
882 EXPECT_EQ(TimeDelta::FromMilliseconds(500),
883 d
* TimeDelta::FromMilliseconds(1000));
886 EXPECT_EQ(TimeDelta::FromMilliseconds(500),
887 TimeDelta::FromMilliseconds(1000) * f
);
888 EXPECT_EQ(TimeDelta::FromMilliseconds(2000),
889 TimeDelta::FromMilliseconds(1000) / f
);
890 EXPECT_EQ(TimeDelta::FromMilliseconds(500),
891 TimeDelta::FromMilliseconds(1000) *= f
);
892 EXPECT_EQ(TimeDelta::FromMilliseconds(2000),
893 TimeDelta::FromMilliseconds(1000) /= f
);
894 EXPECT_EQ(TimeDelta::FromMilliseconds(500),
895 f
* TimeDelta::FromMilliseconds(1000));
899 EXPECT_EQ(TimeDelta::FromMilliseconds(2000),
900 TimeDelta::FromMilliseconds(1000) * i
);
901 EXPECT_EQ(TimeDelta::FromMilliseconds(500),
902 TimeDelta::FromMilliseconds(1000) / i
);
903 EXPECT_EQ(TimeDelta::FromMilliseconds(2000),
904 TimeDelta::FromMilliseconds(1000) *= i
);
905 EXPECT_EQ(TimeDelta::FromMilliseconds(500),
906 TimeDelta::FromMilliseconds(1000) /= i
);
907 EXPECT_EQ(TimeDelta::FromMilliseconds(2000),
908 i
* TimeDelta::FromMilliseconds(1000));
911 EXPECT_EQ(TimeDelta::FromMilliseconds(2000),
912 TimeDelta::FromMilliseconds(1000) * i64
);
913 EXPECT_EQ(TimeDelta::FromMilliseconds(500),
914 TimeDelta::FromMilliseconds(1000) / i64
);
915 EXPECT_EQ(TimeDelta::FromMilliseconds(2000),
916 TimeDelta::FromMilliseconds(1000) *= i64
);
917 EXPECT_EQ(TimeDelta::FromMilliseconds(500),
918 TimeDelta::FromMilliseconds(1000) /= i64
);
919 EXPECT_EQ(TimeDelta::FromMilliseconds(2000),
920 i64
* TimeDelta::FromMilliseconds(1000));
923 EXPECT_EQ(TimeDelta::FromMilliseconds(500),
924 TimeDelta::FromMilliseconds(1000) * 0.5);
925 EXPECT_EQ(TimeDelta::FromMilliseconds(2000),
926 TimeDelta::FromMilliseconds(1000) / 0.5);
927 EXPECT_EQ(TimeDelta::FromMilliseconds(500),
928 TimeDelta::FromMilliseconds(1000) *= 0.5);
929 EXPECT_EQ(TimeDelta::FromMilliseconds(2000),
930 TimeDelta::FromMilliseconds(1000) /= 0.5);
931 EXPECT_EQ(TimeDelta::FromMilliseconds(500),
932 0.5 * TimeDelta::FromMilliseconds(1000));
934 EXPECT_EQ(TimeDelta::FromMilliseconds(2000),
935 TimeDelta::FromMilliseconds(1000) * 2);
936 EXPECT_EQ(TimeDelta::FromMilliseconds(500),
937 TimeDelta::FromMilliseconds(1000) / 2);
938 EXPECT_EQ(TimeDelta::FromMilliseconds(2000),
939 TimeDelta::FromMilliseconds(1000) *= 2);
940 EXPECT_EQ(TimeDelta::FromMilliseconds(500),
941 TimeDelta::FromMilliseconds(1000) /= 2);
942 EXPECT_EQ(TimeDelta::FromMilliseconds(2000),
943 2 * TimeDelta::FromMilliseconds(1000));
946 bool IsMin(TimeDelta delta
) {
947 return (-delta
).is_max();
950 TEST(TimeDelta
, Overflows
) {
951 // Some sanity checks.
952 EXPECT_TRUE(TimeDelta::Max().is_max());
953 EXPECT_TRUE(IsMin(-TimeDelta::Max()));
954 EXPECT_GT(TimeDelta(), -TimeDelta::Max());
956 TimeDelta large_delta
= TimeDelta::Max() - TimeDelta::FromMilliseconds(1);
957 TimeDelta large_negative
= -large_delta
;
958 EXPECT_GT(TimeDelta(), large_negative
);
959 EXPECT_FALSE(large_delta
.is_max());
960 EXPECT_FALSE(IsMin(-large_negative
));
961 TimeDelta one_second
= TimeDelta::FromSeconds(1);
963 // Test +, -, * and / operators.
964 EXPECT_TRUE((large_delta
+ one_second
).is_max());
965 EXPECT_TRUE(IsMin(large_negative
+ (-one_second
)));
966 EXPECT_TRUE(IsMin(large_negative
- one_second
));
967 EXPECT_TRUE((large_delta
- (-one_second
)).is_max());
968 EXPECT_TRUE((large_delta
* 2).is_max());
969 EXPECT_TRUE(IsMin(large_delta
* -2));
970 EXPECT_TRUE((large_delta
/ 0.5).is_max());
971 EXPECT_TRUE(IsMin(large_delta
/ -0.5));
973 // Test +=, -=, *= and /= operators.
974 TimeDelta delta
= large_delta
;
976 EXPECT_TRUE(delta
.is_max());
977 delta
= large_negative
;
978 delta
+= -one_second
;
979 EXPECT_TRUE(IsMin(delta
));
981 delta
= large_negative
;
983 EXPECT_TRUE(IsMin(delta
));
985 delta
-= -one_second
;
986 EXPECT_TRUE(delta
.is_max());
990 EXPECT_TRUE(delta
.is_max());
991 delta
= large_negative
;
993 EXPECT_TRUE(IsMin(delta
));
997 EXPECT_TRUE(delta
.is_max());
998 delta
= large_negative
;
1000 EXPECT_TRUE(IsMin(delta
));
1002 // Test operations with Time and TimeTicks.
1003 EXPECT_TRUE((large_delta
+ Time::Now()).is_max());
1004 EXPECT_TRUE((large_delta
+ TimeTicks::Now()).is_max());
1005 EXPECT_TRUE((Time::Now() + large_delta
).is_max());
1006 EXPECT_TRUE((TimeTicks::Now() + large_delta
).is_max());
1008 Time time_now
= Time::Now();
1009 EXPECT_EQ(one_second
, (time_now
+ one_second
) - time_now
);
1010 EXPECT_EQ(-one_second
, (time_now
- one_second
) - time_now
);
1012 TimeTicks ticks_now
= TimeTicks::Now();
1013 EXPECT_EQ(-one_second
, (ticks_now
- one_second
) - ticks_now
);
1014 EXPECT_EQ(one_second
, (ticks_now
+ one_second
) - ticks_now
);
1017 TEST(TimeDeltaLogging
, DCheckEqCompiles
) {
1018 DCHECK_EQ(TimeDelta(), TimeDelta());
1021 TEST(TimeDeltaLogging
, EmptyIsZero
) {
1023 EXPECT_EQ("0s", AnyToString(zero
));
1026 TEST(TimeDeltaLogging
, FiveHundredMs
) {
1027 TimeDelta five_hundred_ms
= TimeDelta::FromMilliseconds(500);
1028 EXPECT_EQ("0.5s", AnyToString(five_hundred_ms
));
1031 TEST(TimeDeltaLogging
, MinusTenSeconds
) {
1032 TimeDelta minus_ten_seconds
= TimeDelta::FromSeconds(-10);
1033 EXPECT_EQ("-10s", AnyToString(minus_ten_seconds
));
1036 TEST(TimeDeltaLogging
, DoesNotMessUpFormattingFlags
) {
1037 std::ostringstream oss
;
1038 std::ios_base::fmtflags flags_before
= oss
.flags();
1040 EXPECT_EQ(flags_before
, oss
.flags());
1043 TEST(TimeDeltaLogging
, DoesNotMakeStreamBad
) {
1044 std::ostringstream oss
;
1046 EXPECT_TRUE(oss
.good());
1049 TEST(TimeLogging
, DCheckEqCompiles
) {
1050 DCHECK_EQ(Time(), Time());
1053 TEST(TimeLogging
, ChromeBirthdate
) {
1055 ASSERT_TRUE(Time::FromString("Tue, 02 Sep 2008 09:42:18 GMT", &birthdate
));
1056 EXPECT_EQ("2008-09-02 09:42:18.000 UTC", AnyToString(birthdate
));
1059 TEST(TimeLogging
, DoesNotMessUpFormattingFlags
) {
1060 std::ostringstream oss
;
1061 std::ios_base::fmtflags flags_before
= oss
.flags();
1063 EXPECT_EQ(flags_before
, oss
.flags());
1066 TEST(TimeLogging
, DoesNotMakeStreamBad
) {
1067 std::ostringstream oss
;
1069 EXPECT_TRUE(oss
.good());
1072 TEST(TimeTicksLogging
, DCheckEqCompiles
) {
1073 DCHECK_EQ(TimeTicks(), TimeTicks());
1076 TEST(TimeTicksLogging
, ZeroTime
) {
1078 EXPECT_EQ("0 bogo-microseconds", AnyToString(zero
));
1081 TEST(TimeTicksLogging
, FortyYearsLater
) {
1082 TimeTicks forty_years_later
=
1083 TimeTicks() + TimeDelta::FromDays(365.25 * 40);
1084 EXPECT_EQ("1262304000000000 bogo-microseconds",
1085 AnyToString(forty_years_later
));
1088 TEST(TimeTicksLogging
, DoesNotMessUpFormattingFlags
) {
1089 std::ostringstream oss
;
1090 std::ios_base::fmtflags flags_before
= oss
.flags();
1092 EXPECT_EQ(flags_before
, oss
.flags());
1095 TEST(TimeTicksLogging
, DoesNotMakeStreamBad
) {
1096 std::ostringstream oss
;
1098 EXPECT_TRUE(oss
.good());