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

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

Powered by Google App Engine
This is Rietveld 408576698