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

Side by Side Diff: base/time_unittest.cc

Issue 6903022: Removed wchar_t from Time::FromString (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: Removed unnecessary blankline. Created 9 years, 5 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) 2010 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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
16 // Test conversions to/from time_t and exploding/unexploding. 50 // Test conversions to/from time_t and exploding/unexploding.
17 TEST(Time, TimeT) { 51 TEST_F(TimeTest, TimeT) {
18 // C library time and exploded time. 52 // C library time and exploded time.
19 time_t now_t_1 = time(NULL); 53 time_t now_t_1 = time(NULL);
20 struct tm tms; 54 struct tm tms;
21 #if defined(OS_WIN) 55 #if defined(OS_WIN)
22 localtime_s(&tms, &now_t_1); 56 localtime_s(&tms, &now_t_1);
23 #elif defined(OS_POSIX) 57 #elif defined(OS_POSIX)
24 localtime_r(&now_t_1, &tms); 58 localtime_r(&now_t_1, &tms);
25 #endif 59 #endif
26 60
27 // Convert to ours. 61 // Convert to ours.
(...skipping 17 matching lines...) Expand all
45 EXPECT_EQ(now_t_1, now_t_2); 79 EXPECT_EQ(now_t_1, now_t_2);
46 80
47 EXPECT_EQ(10, Time().FromTimeT(10).ToTimeT()); 81 EXPECT_EQ(10, Time().FromTimeT(10).ToTimeT());
48 EXPECT_EQ(10.0, Time().FromTimeT(10).ToDoubleT()); 82 EXPECT_EQ(10.0, Time().FromTimeT(10).ToDoubleT());
49 83
50 // Conversions of 0 should stay 0. 84 // Conversions of 0 should stay 0.
51 EXPECT_EQ(0, Time().ToTimeT()); 85 EXPECT_EQ(0, Time().ToTimeT());
52 EXPECT_EQ(0, Time::FromTimeT(0).ToInternalValue()); 86 EXPECT_EQ(0, Time::FromTimeT(0).ToInternalValue());
53 } 87 }
54 88
55 TEST(Time, FromExplodedWithMilliseconds) { 89 TEST_F(TimeTest, FromExplodedWithMilliseconds) {
56 // Some platform implementations of FromExploded are liable to drop 90 // Some platform implementations of FromExploded are liable to drop
57 // milliseconds if we aren't careful. 91 // milliseconds if we aren't careful.
58 Time now = Time::NowFromSystemTime(); 92 Time now = Time::NowFromSystemTime();
59 Time::Exploded exploded1 = {0}; 93 Time::Exploded exploded1 = {0};
60 now.UTCExplode(&exploded1); 94 now.UTCExplode(&exploded1);
61 exploded1.millisecond = 500; 95 exploded1.millisecond = 500;
62 Time time = Time::FromUTCExploded(exploded1); 96 Time time = Time::FromUTCExploded(exploded1);
63 Time::Exploded exploded2 = {0}; 97 Time::Exploded exploded2 = {0};
64 time.UTCExplode(&exploded2); 98 time.UTCExplode(&exploded2);
65 EXPECT_EQ(exploded1.millisecond, exploded2.millisecond); 99 EXPECT_EQ(exploded1.millisecond, exploded2.millisecond);
66 } 100 }
67 101
68 TEST(Time, ZeroIsSymmetric) { 102 TEST_F(TimeTest, ZeroIsSymmetric) {
69 Time zero_time(Time::FromTimeT(0)); 103 Time zero_time(Time::FromTimeT(0));
70 EXPECT_EQ(0, zero_time.ToTimeT()); 104 EXPECT_EQ(0, zero_time.ToTimeT());
71 105
72 EXPECT_EQ(0.0, zero_time.ToDoubleT()); 106 EXPECT_EQ(0.0, zero_time.ToDoubleT());
73 } 107 }
74 108
75 TEST(Time, LocalExplode) { 109 TEST_F(TimeTest, LocalExplode) {
76 Time a = Time::Now(); 110 Time a = Time::Now();
77 Time::Exploded exploded; 111 Time::Exploded exploded;
78 a.LocalExplode(&exploded); 112 a.LocalExplode(&exploded);
79 113
80 Time b = Time::FromLocalExploded(exploded); 114 Time b = Time::FromLocalExploded(exploded);
81 115
82 // The exploded structure doesn't have microseconds, and on Mac & Linux, the 116 // The exploded structure doesn't have microseconds, and on Mac & Linux, the
83 // internal OS conversion uses seconds, which will cause truncation. So we 117 // internal OS conversion uses seconds, which will cause truncation. So we
84 // can only make sure that the delta is within one second. 118 // can only make sure that the delta is within one second.
85 EXPECT_TRUE((a - b) < TimeDelta::FromSeconds(1)); 119 EXPECT_TRUE((a - b) < TimeDelta::FromSeconds(1));
86 } 120 }
87 121
88 TEST(Time, UTCExplode) { 122 TEST_F(TimeTest, UTCExplode) {
89 Time a = Time::Now(); 123 Time a = Time::Now();
90 Time::Exploded exploded; 124 Time::Exploded exploded;
91 a.UTCExplode(&exploded); 125 a.UTCExplode(&exploded);
92 126
93 Time b = Time::FromUTCExploded(exploded); 127 Time b = Time::FromUTCExploded(exploded);
94 EXPECT_TRUE((a - b) < TimeDelta::FromSeconds(1)); 128 EXPECT_TRUE((a - b) < TimeDelta::FromSeconds(1));
95 } 129 }
96 130
97 TEST(Time, LocalMidnight) { 131 TEST_F(TimeTest, LocalMidnight) {
98 Time::Exploded exploded; 132 Time::Exploded exploded;
99 Time::Now().LocalMidnight().LocalExplode(&exploded); 133 Time::Now().LocalMidnight().LocalExplode(&exploded);
100 EXPECT_EQ(0, exploded.hour); 134 EXPECT_EQ(0, exploded.hour);
101 EXPECT_EQ(0, exploded.minute); 135 EXPECT_EQ(0, exploded.minute);
102 EXPECT_EQ(0, exploded.second); 136 EXPECT_EQ(0, exploded.second);
103 EXPECT_EQ(0, exploded.millisecond); 137 EXPECT_EQ(0, exploded.millisecond);
104 } 138 }
105 139
140 TEST_F(TimeTest, ParseTimeTest1) {
141 time_t current_time = 0;
142 time(&current_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, &current_time);
149 asctime_s(time_buf, arraysize(time_buf), &local_time);
150 #elif defined(OS_POSIX)
151 localtime_r(&current_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
106 TEST(TimeTicks, Deltas) { 311 TEST(TimeTicks, Deltas) {
107 for (int index = 0; index < 50; index++) { 312 for (int index = 0; index < 50; index++) {
108 TimeTicks ticks_start = TimeTicks::Now(); 313 TimeTicks ticks_start = TimeTicks::Now();
109 base::PlatformThread::Sleep(10); 314 base::PlatformThread::Sleep(10);
110 TimeTicks ticks_stop = TimeTicks::Now(); 315 TimeTicks ticks_stop = TimeTicks::Now();
111 TimeDelta delta = ticks_stop - ticks_start; 316 TimeDelta delta = ticks_stop - ticks_start;
112 // Note: Although we asked for a 10ms sleep, if the 317 // Note: Although we asked for a 10ms sleep, if the
113 // time clock has a finer granularity than the Sleep() 318 // time clock has a finer granularity than the Sleep()
114 // clock, it is quite possible to wakeup early. Here 319 // clock, it is quite possible to wakeup early. Here
115 // is how that works: 320 // is how that works:
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after
220 exploded.minute = 0; 425 exploded.minute = 0;
221 exploded.second = 0; 426 exploded.second = 0;
222 exploded.millisecond = 0; 427 exploded.millisecond = 0;
223 Time t = Time::FromUTCExploded(exploded); 428 Time t = Time::FromUTCExploded(exploded);
224 // Unix 1970 epoch. 429 // Unix 1970 epoch.
225 EXPECT_EQ(GG_INT64_C(11644473600000000), t.ToInternalValue()); 430 EXPECT_EQ(GG_INT64_C(11644473600000000), t.ToInternalValue());
226 431
227 // We can't test 1601 epoch, since the system time functions on Linux 432 // We can't test 1601 epoch, since the system time functions on Linux
228 // only compute years starting from 1900. 433 // only compute years starting from 1900.
229 } 434 }
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