OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <time.h> | 5 #include <time.h> |
6 | 6 |
7 #include "base/threading/platform_thread.h" | 7 #include "base/threading/platform_thread.h" |
8 #include "base/time.h" | 8 #include "base/time.h" |
9 #include "build/build_config.h" | 9 #include "build/build_config.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
11 | 11 |
12 using base::Time; | 12 using base::Time; |
13 using base::TimeDelta; | 13 using base::TimeDelta; |
14 using base::TimeTicks; | 14 using base::TimeTicks; |
15 | 15 |
16 // Specialized test fixture allowing time strings without timezones to be | |
17 // tested by comparing them to a known time in the local zone. | |
18 // See also pr_time_unittests.cc | |
19 class TimeTest : public testing::Test { | |
20 protected: | |
21 virtual void SetUp() { | |
22 // Use mktime to get a time_t, and turn it into a PRTime by converting | |
23 // seconds to microseconds. Use 15th Oct 2007 12:45:00 local. This | |
24 // must be a time guaranteed to be outside of a DST fallback hour in | |
25 // any timezone. | |
26 struct tm local_comparison_tm = { | |
27 0, // second | |
28 45, // minute | |
29 12, // hour | |
30 15, // day of month | |
31 10 - 1, // month | |
32 2007 - 1900, // year | |
33 0, // day of week (ignored, output only) | |
34 0, // day of year (ignored, output only) | |
35 -1 // DST in effect, -1 tells mktime to figure it out | |
36 }; | |
37 | |
38 time_t converted_time = mktime(&local_comparison_tm); | |
39 ASSERT_GT(converted_time, 0); | |
40 comparison_time_local_ = Time::FromTimeT(converted_time); | |
41 | |
42 // time_t representation of 15th Oct 2007 12:45:00 PDT | |
43 comparison_time_pdt_ = Time::FromTimeT(1192477500); | |
44 } | |
45 | |
46 Time comparison_time_local_; | |
47 Time comparison_time_pdt_; | |
48 }; | |
49 | |
50 // Test conversions to/from time_t and exploding/unexploding. | 16 // Test conversions to/from time_t and exploding/unexploding. |
51 TEST_F(TimeTest, TimeT) { | 17 TEST(Time, TimeT) { |
52 // C library time and exploded time. | 18 // C library time and exploded time. |
53 time_t now_t_1 = time(NULL); | 19 time_t now_t_1 = time(NULL); |
54 struct tm tms; | 20 struct tm tms; |
55 #if defined(OS_WIN) | 21 #if defined(OS_WIN) |
56 localtime_s(&tms, &now_t_1); | 22 localtime_s(&tms, &now_t_1); |
57 #elif defined(OS_POSIX) | 23 #elif defined(OS_POSIX) |
58 localtime_r(&now_t_1, &tms); | 24 localtime_r(&now_t_1, &tms); |
59 #endif | 25 #endif |
60 | 26 |
61 // Convert to ours. | 27 // Convert to ours. |
(...skipping 17 matching lines...) Expand all Loading... |
79 EXPECT_EQ(now_t_1, now_t_2); | 45 EXPECT_EQ(now_t_1, now_t_2); |
80 | 46 |
81 EXPECT_EQ(10, Time().FromTimeT(10).ToTimeT()); | 47 EXPECT_EQ(10, Time().FromTimeT(10).ToTimeT()); |
82 EXPECT_EQ(10.0, Time().FromTimeT(10).ToDoubleT()); | 48 EXPECT_EQ(10.0, Time().FromTimeT(10).ToDoubleT()); |
83 | 49 |
84 // Conversions of 0 should stay 0. | 50 // Conversions of 0 should stay 0. |
85 EXPECT_EQ(0, Time().ToTimeT()); | 51 EXPECT_EQ(0, Time().ToTimeT()); |
86 EXPECT_EQ(0, Time::FromTimeT(0).ToInternalValue()); | 52 EXPECT_EQ(0, Time::FromTimeT(0).ToInternalValue()); |
87 } | 53 } |
88 | 54 |
89 TEST_F(TimeTest, FromExplodedWithMilliseconds) { | 55 TEST(Time, FromExplodedWithMilliseconds) { |
90 // Some platform implementations of FromExploded are liable to drop | 56 // Some platform implementations of FromExploded are liable to drop |
91 // milliseconds if we aren't careful. | 57 // milliseconds if we aren't careful. |
92 Time now = Time::NowFromSystemTime(); | 58 Time now = Time::NowFromSystemTime(); |
93 Time::Exploded exploded1 = {0}; | 59 Time::Exploded exploded1 = {0}; |
94 now.UTCExplode(&exploded1); | 60 now.UTCExplode(&exploded1); |
95 exploded1.millisecond = 500; | 61 exploded1.millisecond = 500; |
96 Time time = Time::FromUTCExploded(exploded1); | 62 Time time = Time::FromUTCExploded(exploded1); |
97 Time::Exploded exploded2 = {0}; | 63 Time::Exploded exploded2 = {0}; |
98 time.UTCExplode(&exploded2); | 64 time.UTCExplode(&exploded2); |
99 EXPECT_EQ(exploded1.millisecond, exploded2.millisecond); | 65 EXPECT_EQ(exploded1.millisecond, exploded2.millisecond); |
100 } | 66 } |
101 | 67 |
102 TEST_F(TimeTest, ZeroIsSymmetric) { | 68 TEST(Time, ZeroIsSymmetric) { |
103 Time zero_time(Time::FromTimeT(0)); | 69 Time zero_time(Time::FromTimeT(0)); |
104 EXPECT_EQ(0, zero_time.ToTimeT()); | 70 EXPECT_EQ(0, zero_time.ToTimeT()); |
105 | 71 |
106 EXPECT_EQ(0.0, zero_time.ToDoubleT()); | 72 EXPECT_EQ(0.0, zero_time.ToDoubleT()); |
107 } | 73 } |
108 | 74 |
109 TEST_F(TimeTest, LocalExplode) { | 75 TEST(Time, LocalExplode) { |
110 Time a = Time::Now(); | 76 Time a = Time::Now(); |
111 Time::Exploded exploded; | 77 Time::Exploded exploded; |
112 a.LocalExplode(&exploded); | 78 a.LocalExplode(&exploded); |
113 | 79 |
114 Time b = Time::FromLocalExploded(exploded); | 80 Time b = Time::FromLocalExploded(exploded); |
115 | 81 |
116 // The exploded structure doesn't have microseconds, and on Mac & Linux, the | 82 // The exploded structure doesn't have microseconds, and on Mac & Linux, the |
117 // internal OS conversion uses seconds, which will cause truncation. So we | 83 // internal OS conversion uses seconds, which will cause truncation. So we |
118 // can only make sure that the delta is within one second. | 84 // can only make sure that the delta is within one second. |
119 EXPECT_TRUE((a - b) < TimeDelta::FromSeconds(1)); | 85 EXPECT_TRUE((a - b) < TimeDelta::FromSeconds(1)); |
120 } | 86 } |
121 | 87 |
122 TEST_F(TimeTest, UTCExplode) { | 88 TEST(Time, UTCExplode) { |
123 Time a = Time::Now(); | 89 Time a = Time::Now(); |
124 Time::Exploded exploded; | 90 Time::Exploded exploded; |
125 a.UTCExplode(&exploded); | 91 a.UTCExplode(&exploded); |
126 | 92 |
127 Time b = Time::FromUTCExploded(exploded); | 93 Time b = Time::FromUTCExploded(exploded); |
128 EXPECT_TRUE((a - b) < TimeDelta::FromSeconds(1)); | 94 EXPECT_TRUE((a - b) < TimeDelta::FromSeconds(1)); |
129 } | 95 } |
130 | 96 |
131 TEST_F(TimeTest, LocalMidnight) { | 97 TEST(Time, LocalMidnight) { |
132 Time::Exploded exploded; | 98 Time::Exploded exploded; |
133 Time::Now().LocalMidnight().LocalExplode(&exploded); | 99 Time::Now().LocalMidnight().LocalExplode(&exploded); |
134 EXPECT_EQ(0, exploded.hour); | 100 EXPECT_EQ(0, exploded.hour); |
135 EXPECT_EQ(0, exploded.minute); | 101 EXPECT_EQ(0, exploded.minute); |
136 EXPECT_EQ(0, exploded.second); | 102 EXPECT_EQ(0, exploded.second); |
137 EXPECT_EQ(0, exploded.millisecond); | 103 EXPECT_EQ(0, exploded.millisecond); |
138 } | 104 } |
139 | 105 |
140 TEST_F(TimeTest, ParseTimeTest1) { | |
141 time_t current_time = 0; | |
142 time(¤t_time); | |
143 | |
144 const int BUFFER_SIZE = 64; | |
145 struct tm local_time = {0}; | |
146 char time_buf[BUFFER_SIZE] = {0}; | |
147 #if defined(OS_WIN) | |
148 localtime_s(&local_time, ¤t_time); | |
149 asctime_s(time_buf, arraysize(time_buf), &local_time); | |
150 #elif defined(OS_POSIX) | |
151 localtime_r(¤t_time, &local_time); | |
152 asctime_r(&local_time, time_buf); | |
153 #endif | |
154 | |
155 Time parsed_time; | |
156 EXPECT_TRUE(Time::FromString(time_buf, &parsed_time)); | |
157 EXPECT_EQ(current_time, parsed_time.ToTimeT()); | |
158 } | |
159 | |
160 TEST_F(TimeTest, ParseTimeTest2) { | |
161 Time parsed_time; | |
162 EXPECT_TRUE(Time::FromString("Mon, 15 Oct 2007 19:45:00 GMT", &parsed_time)); | |
163 EXPECT_EQ(comparison_time_pdt_, parsed_time); | |
164 } | |
165 | |
166 TEST_F(TimeTest, ParseTimeTest3) { | |
167 Time parsed_time; | |
168 EXPECT_TRUE(Time::FromString("15 Oct 07 12:45:00", &parsed_time)); | |
169 EXPECT_EQ(comparison_time_local_, parsed_time); | |
170 } | |
171 | |
172 TEST_F(TimeTest, ParseTimeTest4) { | |
173 Time parsed_time; | |
174 EXPECT_TRUE(Time::FromString("15 Oct 07 19:45 GMT", &parsed_time)); | |
175 EXPECT_EQ(comparison_time_pdt_, parsed_time); | |
176 } | |
177 | |
178 TEST_F(TimeTest, ParseTimeTest5) { | |
179 Time parsed_time; | |
180 EXPECT_TRUE(Time::FromString("Mon Oct 15 12:45 PDT 2007", &parsed_time)); | |
181 EXPECT_EQ(comparison_time_pdt_, parsed_time); | |
182 } | |
183 | |
184 TEST_F(TimeTest, ParseTimeTest6) { | |
185 Time parsed_time; | |
186 EXPECT_TRUE(Time::FromString("Monday, Oct 15, 2007 12:45 PM", &parsed_time)); | |
187 EXPECT_EQ(comparison_time_local_, parsed_time); | |
188 } | |
189 | |
190 TEST_F(TimeTest, ParseTimeTest7) { | |
191 Time parsed_time; | |
192 EXPECT_TRUE(Time::FromString("10/15/07 12:45:00 PM", &parsed_time)); | |
193 EXPECT_EQ(comparison_time_local_, parsed_time); | |
194 } | |
195 | |
196 TEST_F(TimeTest, ParseTimeTest8) { | |
197 Time parsed_time; | |
198 EXPECT_TRUE(Time::FromString("15-OCT-2007 12:45pm", &parsed_time)); | |
199 EXPECT_EQ(comparison_time_local_, parsed_time); | |
200 } | |
201 | |
202 TEST_F(TimeTest, ParseTimeTest9) { | |
203 Time parsed_time; | |
204 EXPECT_TRUE(Time::FromString("16 Oct 2007 4:45-JST (Tuesday)", &parsed_time)); | |
205 EXPECT_EQ(comparison_time_pdt_, parsed_time); | |
206 } | |
207 | |
208 TEST_F(TimeTest, ParseTimeTest10) { | |
209 Time parsed_time; | |
210 EXPECT_TRUE(Time::FromString("15/10/07 12:45", &parsed_time)); | |
211 EXPECT_EQ(parsed_time, comparison_time_local_); | |
212 } | |
213 | |
214 // Test some of edge cases around epoch, etc. | |
215 TEST_F(TimeTest, ParseTimeTestEpoch0) { | |
216 Time parsed_time; | |
217 | |
218 // time_t == epoch == 0 | |
219 EXPECT_TRUE(Time::FromString("Thu Jan 01 01:00:00 +0100 1970", | |
220 &parsed_time)); | |
221 EXPECT_EQ(0, parsed_time.ToTimeT()); | |
222 EXPECT_TRUE(Time::FromString("Thu Jan 01 00:00:00 GMT 1970", | |
223 &parsed_time)); | |
224 EXPECT_EQ(0, parsed_time.ToTimeT()); | |
225 } | |
226 | |
227 TEST_F(TimeTest, ParseTimeTestEpoch1) { | |
228 Time parsed_time; | |
229 | |
230 // time_t == 1 second after epoch == 1 | |
231 EXPECT_TRUE(Time::FromString("Thu Jan 01 01:00:01 +0100 1970", | |
232 &parsed_time)); | |
233 EXPECT_EQ(1, parsed_time.ToTimeT()); | |
234 EXPECT_TRUE(Time::FromString("Thu Jan 01 00:00:01 GMT 1970", | |
235 &parsed_time)); | |
236 EXPECT_EQ(1, parsed_time.ToTimeT()); | |
237 } | |
238 | |
239 TEST_F(TimeTest, ParseTimeTestEpoch2) { | |
240 Time parsed_time; | |
241 | |
242 // time_t == 2 seconds after epoch == 2 | |
243 EXPECT_TRUE(Time::FromString("Thu Jan 01 01:00:02 +0100 1970", | |
244 &parsed_time)); | |
245 EXPECT_EQ(2, parsed_time.ToTimeT()); | |
246 EXPECT_TRUE(Time::FromString("Thu Jan 01 00:00:02 GMT 1970", | |
247 &parsed_time)); | |
248 EXPECT_EQ(2, parsed_time.ToTimeT()); | |
249 } | |
250 | |
251 TEST_F(TimeTest, ParseTimeTestEpochNeg1) { | |
252 Time parsed_time; | |
253 | |
254 // time_t == 1 second before epoch == -1 | |
255 EXPECT_TRUE(Time::FromString("Thu Jan 01 00:59:59 +0100 1970", | |
256 &parsed_time)); | |
257 EXPECT_EQ(-1, parsed_time.ToTimeT()); | |
258 EXPECT_TRUE(Time::FromString("Wed Dec 31 23:59:59 GMT 1969", | |
259 &parsed_time)); | |
260 EXPECT_EQ(-1, parsed_time.ToTimeT()); | |
261 } | |
262 | |
263 // If time_t is 32 bits, a date after year 2038 will overflow time_t and | |
264 // cause timegm() to return -1. The parsed time should not be 1 second | |
265 // before epoch. | |
266 TEST_F(TimeTest, ParseTimeTestEpochNotNeg1) { | |
267 Time parsed_time; | |
268 | |
269 EXPECT_TRUE(Time::FromString("Wed Dec 31 23:59:59 GMT 2100", | |
270 &parsed_time)); | |
271 EXPECT_NE(-1, parsed_time.ToTimeT()); | |
272 } | |
273 | |
274 TEST_F(TimeTest, ParseTimeTestEpochNeg2) { | |
275 Time parsed_time; | |
276 | |
277 // time_t == 2 seconds before epoch == -2 | |
278 EXPECT_TRUE(Time::FromString("Thu Jan 01 00:59:58 +0100 1970", | |
279 &parsed_time)); | |
280 EXPECT_EQ(-2, parsed_time.ToTimeT()); | |
281 EXPECT_TRUE(Time::FromString("Wed Dec 31 23:59:58 GMT 1969", | |
282 &parsed_time)); | |
283 EXPECT_EQ(-2, parsed_time.ToTimeT()); | |
284 } | |
285 | |
286 TEST_F(TimeTest, ParseTimeTestEpoch1960) { | |
287 Time parsed_time; | |
288 | |
289 // time_t before Epoch, in 1960 | |
290 EXPECT_TRUE(Time::FromString("Wed Jun 29 19:40:01 +0100 1960", | |
291 &parsed_time)); | |
292 EXPECT_EQ(-299999999, parsed_time.ToTimeT()); | |
293 EXPECT_TRUE(Time::FromString("Wed Jun 29 18:40:01 GMT 1960", | |
294 &parsed_time)); | |
295 EXPECT_EQ(-299999999, parsed_time.ToTimeT()); | |
296 EXPECT_TRUE(Time::FromString("Wed Jun 29 17:40:01 GMT 1960", | |
297 &parsed_time)); | |
298 EXPECT_EQ(-300003599, parsed_time.ToTimeT()); | |
299 } | |
300 | |
301 TEST_F(TimeTest, ParseTimeTestEmpty) { | |
302 Time parsed_time; | |
303 EXPECT_FALSE(Time::FromString("", &parsed_time)); | |
304 } | |
305 | |
306 TEST_F(TimeTest, ParseTimeTestInvalidString) { | |
307 Time parsed_time; | |
308 EXPECT_FALSE(Time::FromString("Monday morning 2000", &parsed_time)); | |
309 } | |
310 | |
311 TEST(TimeTicks, Deltas) { | 106 TEST(TimeTicks, Deltas) { |
312 for (int index = 0; index < 50; index++) { | 107 for (int index = 0; index < 50; index++) { |
313 TimeTicks ticks_start = TimeTicks::Now(); | 108 TimeTicks ticks_start = TimeTicks::Now(); |
314 base::PlatformThread::Sleep(10); | 109 base::PlatformThread::Sleep(10); |
315 TimeTicks ticks_stop = TimeTicks::Now(); | 110 TimeTicks ticks_stop = TimeTicks::Now(); |
316 TimeDelta delta = ticks_stop - ticks_start; | 111 TimeDelta delta = ticks_stop - ticks_start; |
317 // Note: Although we asked for a 10ms sleep, if the | 112 // Note: Although we asked for a 10ms sleep, if the |
318 // time clock has a finer granularity than the Sleep() | 113 // time clock has a finer granularity than the Sleep() |
319 // clock, it is quite possible to wakeup early. Here | 114 // clock, it is quite possible to wakeup early. Here |
320 // is how that works: | 115 // is how that works: |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
425 exploded.minute = 0; | 220 exploded.minute = 0; |
426 exploded.second = 0; | 221 exploded.second = 0; |
427 exploded.millisecond = 0; | 222 exploded.millisecond = 0; |
428 Time t = Time::FromUTCExploded(exploded); | 223 Time t = Time::FromUTCExploded(exploded); |
429 // Unix 1970 epoch. | 224 // Unix 1970 epoch. |
430 EXPECT_EQ(GG_INT64_C(11644473600000000), t.ToInternalValue()); | 225 EXPECT_EQ(GG_INT64_C(11644473600000000), t.ToInternalValue()); |
431 | 226 |
432 // We can't test 1601 epoch, since the system time functions on Linux | 227 // We can't test 1601 epoch, since the system time functions on Linux |
433 // only compute years starting from 1900. | 228 // only compute years starting from 1900. |
434 } | 229 } |
OLD | NEW |