blob: 0076e109294023c725444dfc26a929e0f1aa551a [file] [log] [blame]
license.botbf09a502008-08-24 00:55:551// Copyright (c) 2006-2008 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.
initial.commitd7cae122008-07-26 21:49:384
initial.commitd7cae122008-07-26 21:49:385#include <time.h>
6
[email protected]f7189d012008-08-13 22:55:457#include "base/platform_thread.h"
[email protected]5f6eee532008-09-02 08:28:378#include "base/time.h"
9#include "build/build_config.h"
initial.commitd7cae122008-07-26 21:49:3810#include "testing/gtest/include/gtest/gtest.h"
11
[email protected]e1acf6f2008-10-27 20:43:3312using base::Time;
13using base::TimeDelta;
14using base::TimeTicks;
15
initial.commitd7cae122008-07-26 21:49:3816// Test conversions to/from time_t and exploding/unexploding.
17TEST(Time, TimeT) {
18 // C library time and exploded time.
19 time_t now_t_1 = time(NULL);
20 struct tm tms;
[email protected]f7189d012008-08-13 22:55:4521#if defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:3822 localtime_s(&tms, &now_t_1);
[email protected]f7189d012008-08-13 22:55:4523#elif defined(OS_POSIX)
24 localtime_r(&now_t_1, &tms);
25#endif
initial.commitd7cae122008-07-26 21:49:3826
27 // Convert to ours.
28 Time our_time_1 = Time::FromTimeT(now_t_1);
29 Time::Exploded exploded;
30 our_time_1.LocalExplode(&exploded);
31
32 // This will test both our exploding and our time_t -> Time conversion.
33 EXPECT_EQ(tms.tm_year + 1900, exploded.year);
34 EXPECT_EQ(tms.tm_mon + 1, exploded.month);
35 EXPECT_EQ(tms.tm_mday, exploded.day_of_month);
36 EXPECT_EQ(tms.tm_hour, exploded.hour);
37 EXPECT_EQ(tms.tm_min, exploded.minute);
38 EXPECT_EQ(tms.tm_sec, exploded.second);
39
40 // Convert exploded back to the time struct.
41 Time our_time_2 = Time::FromLocalExploded(exploded);
42 EXPECT_TRUE(our_time_1 == our_time_2);
43
44 time_t now_t_2 = our_time_2.ToTimeT();
45 EXPECT_EQ(now_t_1, now_t_2);
46
[email protected]9f251302008-08-19 09:16:4947 EXPECT_EQ(10, Time().FromTimeT(10).ToTimeT());
48 EXPECT_EQ(10.0, Time().FromTimeT(10).ToDoubleT());
49
initial.commitd7cae122008-07-26 21:49:3850 // Conversions of 0 should stay 0.
51 EXPECT_EQ(0, Time().ToTimeT());
52 EXPECT_EQ(0, Time::FromTimeT(0).ToInternalValue());
53}
54
55TEST(Time, ZeroIsSymmetric) {
[email protected]9f251302008-08-19 09:16:4956 Time zero_time(Time::FromTimeT(0));
57 EXPECT_EQ(0, zero_time.ToTimeT());
58
59 EXPECT_EQ(0.0, zero_time.ToDoubleT());
initial.commitd7cae122008-07-26 21:49:3860}
61
62TEST(Time, LocalExplode) {
63 Time a = Time::Now();
64 Time::Exploded exploded;
65 a.LocalExplode(&exploded);
66
67 Time b = Time::FromLocalExploded(exploded);
68
[email protected]a4a3292e2009-08-26 02:53:3669 // The exploded structure doesn't have microseconds, and on Mac & Linux, the
70 // internal OS conversion uses seconds, which will cause truncation. So we
71 // can only make sure that the delta is within one second.
72 EXPECT_TRUE((a - b) < TimeDelta::FromSeconds(1));
initial.commitd7cae122008-07-26 21:49:3873}
74
75TEST(Time, UTCExplode) {
76 Time a = Time::Now();
77 Time::Exploded exploded;
78 a.UTCExplode(&exploded);
79
80 Time b = Time::FromUTCExploded(exploded);
[email protected]a4a3292e2009-08-26 02:53:3681 EXPECT_TRUE((a - b) < TimeDelta::FromSeconds(1));
initial.commitd7cae122008-07-26 21:49:3882}
83
[email protected]9f251302008-08-19 09:16:4984TEST(Time, LocalMidnight) {
85 Time::Exploded exploded;
86 Time::Now().LocalMidnight().LocalExplode(&exploded);
87 EXPECT_EQ(0, exploded.hour);
88 EXPECT_EQ(0, exploded.minute);
89 EXPECT_EQ(0, exploded.second);
90 EXPECT_EQ(0, exploded.millisecond);
91}
92
initial.commitd7cae122008-07-26 21:49:3893TEST(TimeTicks, Deltas) {
[email protected]acaae862009-12-03 00:23:1694 for (int index = 0; index < 50; index++) {
[email protected]de592d32008-09-26 03:00:0095 TimeTicks ticks_start = TimeTicks::Now();
96 PlatformThread::Sleep(10);
97 TimeTicks ticks_stop = TimeTicks::Now();
98 TimeDelta delta = ticks_stop - ticks_start;
99 // Note: Although we asked for a 10ms sleep, if the
100 // time clock has a finer granularity than the Sleep()
101 // clock, it is quite possible to wakeup early. Here
102 // is how that works:
103 // Time(ms timer) Time(us timer)
104 // 5 5010
105 // 6 6010
106 // 7 7010
107 // 8 8010
108 // 9 9000
109 // Elapsed 4ms 3990us
110 //
111 // Unfortunately, our InMilliseconds() function truncates
112 // rather than rounds. We should consider fixing this
113 // so that our averages come out better.
114 EXPECT_GE(delta.InMilliseconds(), 9);
115 EXPECT_GE(delta.InMicroseconds(), 9000);
116 EXPECT_EQ(delta.InSeconds(), 0);
117 }
initial.commitd7cae122008-07-26 21:49:38118}
119
[email protected]86db4d92010-09-30 16:02:18120TEST(TimeTicks, HighResNow) {
[email protected]2da248c2010-07-12 15:13:29121#if defined(OS_WIN)
[email protected]86db4d92010-09-30 16:02:18122 // HighResNow doesn't work on some systems. Since the product still works
123 // even if it doesn't work, it makes this entire test questionable.
124 if (!TimeTicks::IsHighResClockWorking())
125 return;
[email protected]2da248c2010-07-12 15:13:29126#endif
127
[email protected]de592d32008-09-26 03:00:00128 TimeTicks ticks_start = TimeTicks::HighResNow();
[email protected]86db4d92010-09-30 16:02:18129 TimeDelta delta;
130 // Loop until we can detect that the clock has changed. Non-HighRes timers
131 // will increment in chunks, e.g. 15ms. By spinning until we see a clock
132 // change, we detect the minimum time between measurements.
133 do {
134 delta = TimeTicks::HighResNow() - ticks_start;
135 } while (delta.InMilliseconds() == 0);
[email protected]2da248c2010-07-12 15:13:29136
[email protected]86db4d92010-09-30 16:02:18137 // In high resolution mode, we expect to see the clock increment
138 // in intervals less than 15ms.
139 EXPECT_LT(delta.InMicroseconds(), 15000);
[email protected]736f4342008-09-03 18:18:32140}
141
[email protected]9f251302008-08-19 09:16:49142TEST(TimeDelta, FromAndIn) {
143 EXPECT_TRUE(TimeDelta::FromDays(2) == TimeDelta::FromHours(48));
144 EXPECT_TRUE(TimeDelta::FromHours(3) == TimeDelta::FromMinutes(180));
145 EXPECT_TRUE(TimeDelta::FromMinutes(2) == TimeDelta::FromSeconds(120));
146 EXPECT_TRUE(TimeDelta::FromSeconds(2) == TimeDelta::FromMilliseconds(2000));
147 EXPECT_TRUE(TimeDelta::FromMilliseconds(2) ==
148 TimeDelta::FromMicroseconds(2000));
149 EXPECT_EQ(13, TimeDelta::FromDays(13).InDays());
150 EXPECT_EQ(13, TimeDelta::FromHours(13).InHours());
151 EXPECT_EQ(13, TimeDelta::FromMinutes(13).InMinutes());
152 EXPECT_EQ(13, TimeDelta::FromSeconds(13).InSeconds());
153 EXPECT_EQ(13.0, TimeDelta::FromSeconds(13).InSecondsF());
154 EXPECT_EQ(13, TimeDelta::FromMilliseconds(13).InMilliseconds());
155 EXPECT_EQ(13.0, TimeDelta::FromMilliseconds(13).InMillisecondsF());
156 EXPECT_EQ(13, TimeDelta::FromMicroseconds(13).InMicroseconds());
157}
[email protected]a4a3292e2009-08-26 02:53:36158
[email protected]e36e86f2009-12-15 11:55:08159#if defined(OS_POSIX)
160TEST(TimeDelta, TimeSpecConversion) {
161 struct timespec result = TimeDelta::FromSeconds(0).ToTimeSpec();
162 EXPECT_EQ(result.tv_sec, 0);
163 EXPECT_EQ(result.tv_nsec, 0);
164
165 result = TimeDelta::FromSeconds(1).ToTimeSpec();
166 EXPECT_EQ(result.tv_sec, 1);
167 EXPECT_EQ(result.tv_nsec, 0);
168
169 result = TimeDelta::FromMicroseconds(1).ToTimeSpec();
170 EXPECT_EQ(result.tv_sec, 0);
171 EXPECT_EQ(result.tv_nsec, 1000);
172
173 result = TimeDelta::FromMicroseconds(
174 Time::kMicrosecondsPerSecond + 1).ToTimeSpec();
175 EXPECT_EQ(result.tv_sec, 1);
176 EXPECT_EQ(result.tv_nsec, 1000);
177}
178#endif // OS_POSIX
179
[email protected]a4a3292e2009-08-26 02:53:36180// Our internal time format is serialized in things like databases, so it's
181// important that it's consistent across all our platforms. We use the 1601
182// Windows epoch as the internal format across all platforms.
183TEST(TimeDelta, WindowsEpoch) {
184 Time::Exploded exploded;
185 exploded.year = 1970;
186 exploded.month = 1;
187 exploded.day_of_week = 0; // Should be unusued.
188 exploded.day_of_month = 1;
189 exploded.hour = 0;
190 exploded.minute = 0;
191 exploded.second = 0;
192 exploded.millisecond = 0;
193 Time t = Time::FromUTCExploded(exploded);
194 // Unix 1970 epoch.
195 EXPECT_EQ(GG_INT64_C(11644473600000000), t.ToInternalValue());
196
197 // We can't test 1601 epoch, since the system time functions on Linux
198 // only compute years starting from 1900.
199}