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