Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(603)

Side by Side Diff: base/time_unittest.cc

Issue 6893031: Revert r83048 "Removed wchar_t from Time::FromString." (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 9 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « base/time.cc ('k') | chrome/browser/web_resource/promo_resource_service.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
51 // Test conversions to/from time_t and exploding/unexploding. 16 // Test conversions to/from time_t and exploding/unexploding.
52 TEST_F(TimeTest, TimeT) { 17 TEST(Time, TimeT) {
53 // C library time and exploded time. 18 // C library time and exploded time.
54 time_t now_t_1 = time(NULL); 19 time_t now_t_1 = time(NULL);
55 struct tm tms; 20 struct tm tms;
56 #if defined(OS_WIN) 21 #if defined(OS_WIN)
57 localtime_s(&tms, &now_t_1); 22 localtime_s(&tms, &now_t_1);
58 #elif defined(OS_POSIX) 23 #elif defined(OS_POSIX)
59 localtime_r(&now_t_1, &tms); 24 localtime_r(&now_t_1, &tms);
60 #endif 25 #endif
61 26
62 // Convert to ours. 27 // Convert to ours.
(...skipping 17 matching lines...) Expand all
80 EXPECT_EQ(now_t_1, now_t_2); 45 EXPECT_EQ(now_t_1, now_t_2);
81 46
82 EXPECT_EQ(10, Time().FromTimeT(10).ToTimeT()); 47 EXPECT_EQ(10, Time().FromTimeT(10).ToTimeT());
83 EXPECT_EQ(10.0, Time().FromTimeT(10).ToDoubleT()); 48 EXPECT_EQ(10.0, Time().FromTimeT(10).ToDoubleT());
84 49
85 // Conversions of 0 should stay 0. 50 // Conversions of 0 should stay 0.
86 EXPECT_EQ(0, Time().ToTimeT()); 51 EXPECT_EQ(0, Time().ToTimeT());
87 EXPECT_EQ(0, Time::FromTimeT(0).ToInternalValue()); 52 EXPECT_EQ(0, Time::FromTimeT(0).ToInternalValue());
88 } 53 }
89 54
90 TEST_F(TimeTest, FromExplodedWithMilliseconds) { 55 TEST(Time, FromExplodedWithMilliseconds) {
91 // Some platform implementations of FromExploded are liable to drop 56 // Some platform implementations of FromExploded are liable to drop
92 // milliseconds if we aren't careful. 57 // milliseconds if we aren't careful.
93 Time now = Time::NowFromSystemTime(); 58 Time now = Time::NowFromSystemTime();
94 Time::Exploded exploded1 = {0}; 59 Time::Exploded exploded1 = {0};
95 now.UTCExplode(&exploded1); 60 now.UTCExplode(&exploded1);
96 exploded1.millisecond = 500; 61 exploded1.millisecond = 500;
97 Time time = Time::FromUTCExploded(exploded1); 62 Time time = Time::FromUTCExploded(exploded1);
98 Time::Exploded exploded2 = {0}; 63 Time::Exploded exploded2 = {0};
99 time.UTCExplode(&exploded2); 64 time.UTCExplode(&exploded2);
100 EXPECT_EQ(exploded1.millisecond, exploded2.millisecond); 65 EXPECT_EQ(exploded1.millisecond, exploded2.millisecond);
101 } 66 }
102 67
103 TEST_F(TimeTest, ZeroIsSymmetric) { 68 TEST(Time, ZeroIsSymmetric) {
104 Time zero_time(Time::FromTimeT(0)); 69 Time zero_time(Time::FromTimeT(0));
105 EXPECT_EQ(0, zero_time.ToTimeT()); 70 EXPECT_EQ(0, zero_time.ToTimeT());
106 71
107 EXPECT_EQ(0.0, zero_time.ToDoubleT()); 72 EXPECT_EQ(0.0, zero_time.ToDoubleT());
108 } 73 }
109 74
110 TEST_F(TimeTest, LocalExplode) { 75 TEST(Time, LocalExplode) {
111 Time a = Time::Now(); 76 Time a = Time::Now();
112 Time::Exploded exploded; 77 Time::Exploded exploded;
113 a.LocalExplode(&exploded); 78 a.LocalExplode(&exploded);
114 79
115 Time b = Time::FromLocalExploded(exploded); 80 Time b = Time::FromLocalExploded(exploded);
116 81
117 // 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
118 // internal OS conversion uses seconds, which will cause truncation. So we 83 // internal OS conversion uses seconds, which will cause truncation. So we
119 // can only make sure that the delta is within one second. 84 // can only make sure that the delta is within one second.
120 EXPECT_TRUE((a - b) < TimeDelta::FromSeconds(1)); 85 EXPECT_TRUE((a - b) < TimeDelta::FromSeconds(1));
121 } 86 }
122 87
123 TEST_F(TimeTest, UTCExplode) { 88 TEST(Time, UTCExplode) {
124 Time a = Time::Now(); 89 Time a = Time::Now();
125 Time::Exploded exploded; 90 Time::Exploded exploded;
126 a.UTCExplode(&exploded); 91 a.UTCExplode(&exploded);
127 92
128 Time b = Time::FromUTCExploded(exploded); 93 Time b = Time::FromUTCExploded(exploded);
129 EXPECT_TRUE((a - b) < TimeDelta::FromSeconds(1)); 94 EXPECT_TRUE((a - b) < TimeDelta::FromSeconds(1));
130 } 95 }
131 96
132 TEST_F(TimeTest, LocalMidnight) { 97 TEST(Time, LocalMidnight) {
133 Time::Exploded exploded; 98 Time::Exploded exploded;
134 Time::Now().LocalMidnight().LocalExplode(&exploded); 99 Time::Now().LocalMidnight().LocalExplode(&exploded);
135 EXPECT_EQ(0, exploded.hour); 100 EXPECT_EQ(0, exploded.hour);
136 EXPECT_EQ(0, exploded.minute); 101 EXPECT_EQ(0, exploded.minute);
137 EXPECT_EQ(0, exploded.second); 102 EXPECT_EQ(0, exploded.second);
138 EXPECT_EQ(0, exploded.millisecond); 103 EXPECT_EQ(0, exploded.millisecond);
139 } 104 }
140 105
141 TEST_F(TimeTest, ParseTimeTest1) {
142 time_t current_time = 0;
143 time(&current_time);
144
145 const int BUFFER_SIZE = 64;
146 struct tm local_time = {0};
147 char time_buf[BUFFER_SIZE] = {0};
148 #if defined(OS_WIN)
149 localtime_s(&local_time, &current_time);
150 asctime_s(time_buf, arraysize(time_buf), &local_time);
151 #elif defined(OS_POSIX)
152 localtime_r(&current_time, &local_time);
153 asctime_r(&local_time, time_buf);
154 #endif
155
156 Time parsed_time;
157 EXPECT_TRUE(Time::FromString(time_buf, &parsed_time));
158 EXPECT_EQ(current_time, parsed_time.ToTimeT());
159 }
160
161 TEST_F(TimeTest, ParseTimeTest2) {
162 Time parsed_time;
163 EXPECT_TRUE(Time::FromString("Mon, 15 Oct 2007 19:45:00 GMT", &parsed_time));
164 EXPECT_EQ(comparison_time_pdt_, parsed_time);
165 }
166
167 TEST_F(TimeTest, ParseTimeTest3) {
168 Time parsed_time;
169 EXPECT_TRUE(Time::FromString("15 Oct 07 12:45:00", &parsed_time));
170 EXPECT_EQ(comparison_time_local_, parsed_time);
171 }
172
173 TEST_F(TimeTest, ParseTimeTest4) {
174 Time parsed_time;
175 EXPECT_TRUE(Time::FromString("15 Oct 07 19:45 GMT", &parsed_time));
176 EXPECT_EQ(comparison_time_pdt_, parsed_time);
177 }
178
179 TEST_F(TimeTest, ParseTimeTest5) {
180 Time parsed_time;
181 EXPECT_TRUE(Time::FromString("Mon Oct 15 12:45 PDT 2007", &parsed_time));
182 EXPECT_EQ(comparison_time_pdt_, parsed_time);
183 }
184
185 TEST_F(TimeTest, ParseTimeTest6) {
186 Time parsed_time;
187 EXPECT_TRUE(Time::FromString("Monday, Oct 15, 2007 12:45 PM", &parsed_time));
188 EXPECT_EQ(comparison_time_local_, parsed_time);
189 }
190
191 TEST_F(TimeTest, ParseTimeTest7) {
192 Time parsed_time;
193 EXPECT_TRUE(Time::FromString("10/15/07 12:45:00 PM", &parsed_time));
194 EXPECT_EQ(comparison_time_local_, parsed_time);
195 }
196
197 TEST_F(TimeTest, ParseTimeTest8) {
198 Time parsed_time;
199 EXPECT_TRUE(Time::FromString("15-OCT-2007 12:45pm", &parsed_time));
200 EXPECT_EQ(comparison_time_local_, parsed_time);
201 }
202
203 TEST_F(TimeTest, ParseTimeTest9) {
204 Time parsed_time;
205 EXPECT_TRUE(Time::FromString("16 Oct 2007 4:45-JST (Tuesday)", &parsed_time));
206 EXPECT_EQ(comparison_time_pdt_, parsed_time);
207 }
208
209 TEST_F(TimeTest, ParseTimeTest10) {
210 Time parsed_time;
211 EXPECT_TRUE(Time::FromString("15/10/07 12:45", &parsed_time));
212 EXPECT_EQ(parsed_time, comparison_time_local_);
213 }
214
215 // Test some of edge cases around epoch, etc.
216 TEST_F(TimeTest, ParseTimeTestEpoch0) {
217 Time parsed_time;
218
219 // time_t == epoch == 0
220 EXPECT_TRUE(Time::FromString("Thu Jan 01 01:00:00 +0100 1970",
221 &parsed_time));
222 EXPECT_EQ(0, parsed_time.ToTimeT());
223 EXPECT_TRUE(Time::FromString("Thu Jan 01 00:00:00 GMT 1970",
224 &parsed_time));
225 EXPECT_EQ(0, parsed_time.ToTimeT());
226 }
227
228 TEST_F(TimeTest, ParseTimeTestEpoch1) {
229 Time parsed_time;
230
231 // time_t == 1 second after epoch == 1
232 EXPECT_TRUE(Time::FromString("Thu Jan 01 01:00:01 +0100 1970",
233 &parsed_time));
234 EXPECT_EQ(1, parsed_time.ToTimeT());
235 EXPECT_TRUE(Time::FromString("Thu Jan 01 00:00:01 GMT 1970",
236 &parsed_time));
237 EXPECT_EQ(1, parsed_time.ToTimeT());
238 }
239
240 TEST_F(TimeTest, ParseTimeTestEpoch2) {
241 Time parsed_time;
242
243 // time_t == 2 seconds after epoch == 2
244 EXPECT_TRUE(Time::FromString("Thu Jan 01 01:00:02 +0100 1970",
245 &parsed_time));
246 EXPECT_EQ(2, parsed_time.ToTimeT());
247 EXPECT_TRUE(Time::FromString("Thu Jan 01 00:00:02 GMT 1970",
248 &parsed_time));
249 EXPECT_EQ(2, parsed_time.ToTimeT());
250 }
251
252 TEST_F(TimeTest, ParseTimeTestEpochNeg1) {
253 Time parsed_time;
254
255 // time_t == 1 second before epoch == -1
256 EXPECT_TRUE(Time::FromString("Thu Jan 01 00:59:59 +0100 1970",
257 &parsed_time));
258 EXPECT_EQ(-1, parsed_time.ToTimeT());
259 EXPECT_TRUE(Time::FromString("Wed Dec 31 23:59:59 GMT 1969",
260 &parsed_time));
261 EXPECT_EQ(-1, parsed_time.ToTimeT());
262 }
263
264 // If time_t is 32 bits, a date after year 2038 will overflow time_t and
265 // cause timegm() to return -1. The parsed time should not be 1 second
266 // before epoch.
267 TEST_F(TimeTest, ParseTimeTestEpochNotNeg1) {
268 Time parsed_time;
269
270 EXPECT_TRUE(Time::FromString("Wed Dec 31 23:59:59 GMT 2100",
271 &parsed_time));
272 EXPECT_NE(-1, parsed_time.ToTimeT());
273 }
274
275 TEST_F(TimeTest, ParseTimeTestEpochNeg2) {
276 Time parsed_time;
277
278 // time_t == 2 seconds before epoch == -2
279 EXPECT_TRUE(Time::FromString("Thu Jan 01 00:59:58 +0100 1970",
280 &parsed_time));
281 EXPECT_EQ(-2, parsed_time.ToTimeT());
282 EXPECT_TRUE(Time::FromString("Wed Dec 31 23:59:58 GMT 1969",
283 &parsed_time));
284 EXPECT_EQ(-2, parsed_time.ToTimeT());
285 }
286
287 TEST_F(TimeTest, ParseTimeTestEpoch1960) {
288 Time parsed_time;
289
290 // time_t before Epoch, in 1960
291 EXPECT_TRUE(Time::FromString("Wed Jun 29 19:40:01 +0100 1960",
292 &parsed_time));
293 EXPECT_EQ(-299999999, parsed_time.ToTimeT());
294 EXPECT_TRUE(Time::FromString("Wed Jun 29 18:40:01 GMT 1960",
295 &parsed_time));
296 EXPECT_EQ(-299999999, parsed_time.ToTimeT());
297 EXPECT_TRUE(Time::FromString("Wed Jun 29 17:40:01 GMT 1960",
298 &parsed_time));
299 EXPECT_EQ(-300003599, parsed_time.ToTimeT());
300 }
301
302 TEST_F(TimeTest, ParseTimeTestEmpty) {
303 Time parsed_time;
304 EXPECT_FALSE(Time::FromString("", &parsed_time));
305 }
306
307 TEST_F(TimeTest, ParseTimeTestInvalidString) {
308 Time parsed_time;
309 EXPECT_FALSE(Time::FromString("Monday morning 2000", &parsed_time));
310 }
311
312 // This test should not crash when compiled with Visual C++ 2005 (see
313 // http://crbug.com/4387).
314 TEST_F(TimeTest, ParseTimeTestOutOfRange) {
315 // Note the lack of timezone in the time string. The year has to be 3001.
316 // The date has to be after 23:59:59, December 31, 3000, US Pacific Time, so
317 // we use January 2, 3001 to make sure it's after the magic maximum in any
318 // timezone.
319 Time parsed_time;
320 EXPECT_TRUE(Time::FromString("Sun Jan 2 00:00:00 3001", &parsed_time));
321 }
322
323 TEST_F(TimeTest, ParseTimeTestNotNormalized1) {
324 Time parsed_time;
325 EXPECT_TRUE(Time::FromString("Mon Oct 15 12:44:60 PDT 2007",
326 &parsed_time));
327 EXPECT_EQ(comparison_time_pdt_, parsed_time);
328 }
329
330 TEST_F(TimeTest, ParseTimeTestNotNormalized2) {
331 Time parsed_time;
332 EXPECT_TRUE(Time::FromString("Sun Oct 14 36:45 PDT 2007",
333 &parsed_time));
334 EXPECT_EQ(comparison_time_pdt_, parsed_time);
335 }
336
337
338 TEST(TimeTicks, Deltas) { 106 TEST(TimeTicks, Deltas) {
339 for (int index = 0; index < 50; index++) { 107 for (int index = 0; index < 50; index++) {
340 TimeTicks ticks_start = TimeTicks::Now(); 108 TimeTicks ticks_start = TimeTicks::Now();
341 base::PlatformThread::Sleep(10); 109 base::PlatformThread::Sleep(10);
342 TimeTicks ticks_stop = TimeTicks::Now(); 110 TimeTicks ticks_stop = TimeTicks::Now();
343 TimeDelta delta = ticks_stop - ticks_start; 111 TimeDelta delta = ticks_stop - ticks_start;
344 // Note: Although we asked for a 10ms sleep, if the 112 // Note: Although we asked for a 10ms sleep, if the
345 // time clock has a finer granularity than the Sleep() 113 // time clock has a finer granularity than the Sleep()
346 // clock, it is quite possible to wakeup early. Here 114 // clock, it is quite possible to wakeup early. Here
347 // is how that works: 115 // is how that works:
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after
452 exploded.minute = 0; 220 exploded.minute = 0;
453 exploded.second = 0; 221 exploded.second = 0;
454 exploded.millisecond = 0; 222 exploded.millisecond = 0;
455 Time t = Time::FromUTCExploded(exploded); 223 Time t = Time::FromUTCExploded(exploded);
456 // Unix 1970 epoch. 224 // Unix 1970 epoch.
457 EXPECT_EQ(GG_INT64_C(11644473600000000), t.ToInternalValue()); 225 EXPECT_EQ(GG_INT64_C(11644473600000000), t.ToInternalValue());
458 226
459 // 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
460 // only compute years starting from 1900. 228 // only compute years starting from 1900.
461 } 229 }
OLDNEW
« no previous file with comments | « base/time.cc ('k') | chrome/browser/web_resource/promo_resource_service.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698