OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // 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 | 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/third_party/nspr/prtime.h" | 7 #include "base/third_party/nspr/prtime.h" |
8 #include "base/time.h" | 8 #include "base/time.h" |
9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
10 | 10 |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
129 PRStatus result = PR_ParseTimeString("16 Oct 2007 4:45-JST (Tuesday)", | 129 PRStatus result = PR_ParseTimeString("16 Oct 2007 4:45-JST (Tuesday)", |
130 PR_FALSE, &parsed_time); | 130 PR_FALSE, &parsed_time); |
131 EXPECT_EQ(PR_SUCCESS, result); | 131 EXPECT_EQ(PR_SUCCESS, result); |
132 EXPECT_EQ(parsed_time, comparison_time_pdt); | 132 EXPECT_EQ(parsed_time, comparison_time_pdt); |
133 } | 133 } |
134 | 134 |
135 // This tests the Time::FromString wrapper over PR_ParseTimeString | 135 // This tests the Time::FromString wrapper over PR_ParseTimeString |
136 TEST_F(PRTimeTest, ParseTimeTest10) { | 136 TEST_F(PRTimeTest, ParseTimeTest10) { |
137 Time parsed_time; | 137 Time parsed_time; |
138 bool result = Time::FromString(L"15/10/07 12:45", &parsed_time); | 138 bool result = Time::FromString(L"15/10/07 12:45", &parsed_time); |
139 EXPECT_EQ(true, result); | 139 EXPECT_TRUE(result); |
140 | 140 |
141 time_t computed_time = parsed_time.ToTimeT(); | 141 time_t computed_time = parsed_time.ToTimeT(); |
142 time_t time_to_compare = comparison_time_local_ / | 142 time_t time_to_compare = comparison_time_local_ / |
143 Time::kMicrosecondsPerSecond; | 143 Time::kMicrosecondsPerSecond; |
144 EXPECT_EQ(computed_time, time_to_compare); | 144 EXPECT_EQ(computed_time, time_to_compare); |
145 } | 145 } |
146 | 146 |
147 // This tests the Time::FromString wrapper over PR_ParseTimeString | 147 // This tests the Time::FromString wrapper over PR_ParseTimeString |
148 TEST_F(PRTimeTest, ParseTimeTest11) { | 148 TEST_F(PRTimeTest, ParseTimeTest11) { |
149 Time parsed_time; | 149 Time parsed_time; |
150 bool result = Time::FromString(L"Mon, 15 Oct 2007 19:45:00 GMT", | 150 bool result = Time::FromString(L"Mon, 15 Oct 2007 19:45:00 GMT", |
151 &parsed_time); | 151 &parsed_time); |
152 EXPECT_EQ(true, result); | 152 EXPECT_TRUE(result); |
153 | 153 |
154 time_t computed_time = parsed_time.ToTimeT(); | 154 time_t computed_time = parsed_time.ToTimeT(); |
155 time_t time_to_compare = comparison_time_pdt / Time::kMicrosecondsPerSecond; | 155 time_t time_to_compare = comparison_time_pdt / Time::kMicrosecondsPerSecond; |
156 EXPECT_EQ(computed_time, time_to_compare); | 156 EXPECT_EQ(computed_time, time_to_compare); |
157 } | 157 } |
158 | 158 |
159 // Test some of edge cases around epoch, etc. | 159 // Test some of edge cases around epoch, etc. |
160 TEST_F(PRTimeTest, ParseTimeTestEpoch0) { | 160 TEST_F(PRTimeTest, ParseTimeTestEpoch0) { |
161 Time parsed_time; | 161 Time parsed_time; |
162 | 162 |
163 // time_t == epoch == 0 | 163 // time_t == epoch == 0 |
164 EXPECT_EQ(true, Time::FromString(L"Thu Jan 01 01:00:00 +0100 1970", | 164 EXPECT_TRUE(Time::FromString(L"Thu Jan 01 01:00:00 +0100 1970", |
165 &parsed_time)); | 165 &parsed_time)); |
166 EXPECT_EQ(0, parsed_time.ToTimeT()); | 166 EXPECT_EQ(0, parsed_time.ToTimeT()); |
167 EXPECT_EQ(true, Time::FromString(L"Thu Jan 01 00:00:00 GMT 1970", | 167 EXPECT_TRUE(Time::FromString(L"Thu Jan 01 00:00:00 GMT 1970", |
168 &parsed_time)); | 168 &parsed_time)); |
169 EXPECT_EQ(0, parsed_time.ToTimeT()); | 169 EXPECT_EQ(0, parsed_time.ToTimeT()); |
170 } | 170 } |
171 | 171 |
172 TEST_F(PRTimeTest, ParseTimeTestEpoch1) { | 172 TEST_F(PRTimeTest, ParseTimeTestEpoch1) { |
173 Time parsed_time; | 173 Time parsed_time; |
174 | 174 |
175 // time_t == 1 second after epoch == 1 | 175 // time_t == 1 second after epoch == 1 |
176 EXPECT_EQ(true, Time::FromString(L"Thu Jan 01 01:00:01 +0100 1970", | 176 EXPECT_TRUE(Time::FromString(L"Thu Jan 01 01:00:01 +0100 1970", |
177 &parsed_time)); | 177 &parsed_time)); |
178 EXPECT_EQ(1, parsed_time.ToTimeT()); | 178 EXPECT_EQ(1, parsed_time.ToTimeT()); |
179 EXPECT_EQ(true, Time::FromString(L"Thu Jan 01 00:00:01 GMT 1970", | 179 EXPECT_TRUE(Time::FromString(L"Thu Jan 01 00:00:01 GMT 1970", |
180 &parsed_time)); | 180 &parsed_time)); |
181 EXPECT_EQ(1, parsed_time.ToTimeT()); | 181 EXPECT_EQ(1, parsed_time.ToTimeT()); |
182 } | 182 } |
183 | 183 |
184 TEST_F(PRTimeTest, ParseTimeTestEpoch2) { | 184 TEST_F(PRTimeTest, ParseTimeTestEpoch2) { |
185 Time parsed_time; | 185 Time parsed_time; |
186 | 186 |
187 // time_t == 2 seconds after epoch == 2 | 187 // time_t == 2 seconds after epoch == 2 |
188 EXPECT_EQ(true, Time::FromString(L"Thu Jan 01 01:00:02 +0100 1970", | 188 EXPECT_TRUE(Time::FromString(L"Thu Jan 01 01:00:02 +0100 1970", |
189 &parsed_time)); | 189 &parsed_time)); |
190 EXPECT_EQ(2, parsed_time.ToTimeT()); | 190 EXPECT_EQ(2, parsed_time.ToTimeT()); |
191 EXPECT_EQ(true, Time::FromString(L"Thu Jan 01 00:00:02 GMT 1970", | 191 EXPECT_TRUE(Time::FromString(L"Thu Jan 01 00:00:02 GMT 1970", |
192 &parsed_time)); | 192 &parsed_time)); |
193 EXPECT_EQ(2, parsed_time.ToTimeT()); | 193 EXPECT_EQ(2, parsed_time.ToTimeT()); |
194 } | 194 } |
195 | 195 |
196 TEST_F(PRTimeTest, ParseTimeTestEpochNeg1) { | 196 TEST_F(PRTimeTest, ParseTimeTestEpochNeg1) { |
197 Time parsed_time; | 197 Time parsed_time; |
198 | 198 |
199 // time_t == 1 second before epoch == -1 | 199 // time_t == 1 second before epoch == -1 |
200 EXPECT_EQ(true, Time::FromString(L"Thu Jan 01 00:59:59 +0100 1970", | 200 EXPECT_TRUE(Time::FromString(L"Thu Jan 01 00:59:59 +0100 1970", |
201 &parsed_time)); | 201 &parsed_time)); |
202 EXPECT_EQ(-1, parsed_time.ToTimeT()); | 202 EXPECT_EQ(-1, parsed_time.ToTimeT()); |
203 EXPECT_EQ(true, Time::FromString(L"Wed Dec 31 23:59:59 GMT 1969", | 203 EXPECT_TRUE(Time::FromString(L"Wed Dec 31 23:59:59 GMT 1969", |
204 &parsed_time)); | 204 &parsed_time)); |
205 EXPECT_EQ(-1, parsed_time.ToTimeT()); | 205 EXPECT_EQ(-1, parsed_time.ToTimeT()); |
206 } | 206 } |
207 | 207 |
208 // If time_t is 32 bits, a date after year 2038 will overflow time_t and | 208 // If time_t is 32 bits, a date after year 2038 will overflow time_t and |
209 // cause timegm() to return -1. The parsed time should not be 1 second | 209 // cause timegm() to return -1. The parsed time should not be 1 second |
210 // before epoch. | 210 // before epoch. |
211 TEST_F(PRTimeTest, ParseTimeTestEpochNotNeg1) { | 211 TEST_F(PRTimeTest, ParseTimeTestEpochNotNeg1) { |
212 Time parsed_time; | 212 Time parsed_time; |
213 | 213 |
214 EXPECT_EQ(true, Time::FromString(L"Wed Dec 31 23:59:59 GMT 2100", | 214 EXPECT_TRUE(Time::FromString(L"Wed Dec 31 23:59:59 GMT 2100", |
215 &parsed_time)); | 215 &parsed_time)); |
216 EXPECT_NE(-1, parsed_time.ToTimeT()); | 216 EXPECT_NE(-1, parsed_time.ToTimeT()); |
217 } | 217 } |
218 | 218 |
219 TEST_F(PRTimeTest, ParseTimeTestEpochNeg2) { | 219 TEST_F(PRTimeTest, ParseTimeTestEpochNeg2) { |
220 Time parsed_time; | 220 Time parsed_time; |
221 | 221 |
222 // time_t == 2 seconds before epoch == -2 | 222 // time_t == 2 seconds before epoch == -2 |
223 EXPECT_EQ(true, Time::FromString(L"Thu Jan 01 00:59:58 +0100 1970", | 223 EXPECT_TRUE(Time::FromString(L"Thu Jan 01 00:59:58 +0100 1970", |
224 &parsed_time)); | 224 &parsed_time)); |
225 EXPECT_EQ(-2, parsed_time.ToTimeT()); | 225 EXPECT_EQ(-2, parsed_time.ToTimeT()); |
226 EXPECT_EQ(true, Time::FromString(L"Wed Dec 31 23:59:58 GMT 1969", | 226 EXPECT_TRUE(Time::FromString(L"Wed Dec 31 23:59:58 GMT 1969", |
227 &parsed_time)); | 227 &parsed_time)); |
228 EXPECT_EQ(-2, parsed_time.ToTimeT()); | 228 EXPECT_EQ(-2, parsed_time.ToTimeT()); |
229 } | 229 } |
230 | 230 |
231 TEST_F(PRTimeTest, ParseTimeTestEpoch1960) { | 231 TEST_F(PRTimeTest, ParseTimeTestEpoch1960) { |
232 Time parsed_time; | 232 Time parsed_time; |
233 | 233 |
234 // time_t before Epoch, in 1960 | 234 // time_t before Epoch, in 1960 |
235 EXPECT_EQ(true, Time::FromString(L"Wed Jun 29 19:40:01 +0100 1960", | 235 EXPECT_TRUE(Time::FromString(L"Wed Jun 29 19:40:01 +0100 1960", |
236 &parsed_time)); | 236 &parsed_time)); |
237 EXPECT_EQ(-299999999, parsed_time.ToTimeT()); | 237 EXPECT_EQ(-299999999, parsed_time.ToTimeT()); |
238 EXPECT_EQ(true, Time::FromString(L"Wed Jun 29 18:40:01 GMT 1960", | 238 EXPECT_TRUE(Time::FromString(L"Wed Jun 29 18:40:01 GMT 1960", |
239 &parsed_time)); | 239 &parsed_time)); |
240 EXPECT_EQ(-299999999, parsed_time.ToTimeT()); | 240 EXPECT_EQ(-299999999, parsed_time.ToTimeT()); |
241 EXPECT_EQ(true, Time::FromString(L"Wed Jun 29 17:40:01 GMT 1960", | 241 EXPECT_TRUE(Time::FromString(L"Wed Jun 29 17:40:01 GMT 1960", |
242 &parsed_time)); | 242 &parsed_time)); |
243 EXPECT_EQ(-300003599, parsed_time.ToTimeT()); | 243 EXPECT_EQ(-300003599, parsed_time.ToTimeT()); |
244 } | 244 } |
245 | 245 |
246 TEST_F(PRTimeTest, ParseTimeTestEmpty) { | 246 TEST_F(PRTimeTest, ParseTimeTestEmpty) { |
247 Time parsed_time; | 247 Time parsed_time; |
248 EXPECT_FALSE(Time::FromString(L"", &parsed_time)); | 248 EXPECT_FALSE(Time::FromString(L"", &parsed_time)); |
249 } | 249 } |
250 | 250 |
251 // This test should not crash when compiled with Visual C++ 2005 (see | 251 // This test should not crash when compiled with Visual C++ 2005 (see |
252 // http://crbug.com/4387). | 252 // http://crbug.com/4387). |
(...skipping 18 matching lines...) Expand all Loading... |
271 | 271 |
272 TEST_F(PRTimeTest, ParseTimeTestNotNormalized2) { | 272 TEST_F(PRTimeTest, ParseTimeTestNotNormalized2) { |
273 PRTime parsed_time = 0; | 273 PRTime parsed_time = 0; |
274 PRStatus result = PR_ParseTimeString("Sun Oct 14 36:45 PDT 2007", | 274 PRStatus result = PR_ParseTimeString("Sun Oct 14 36:45 PDT 2007", |
275 PR_FALSE, &parsed_time); | 275 PR_FALSE, &parsed_time); |
276 EXPECT_EQ(PR_SUCCESS, result); | 276 EXPECT_EQ(PR_SUCCESS, result); |
277 EXPECT_EQ(comparison_time_pdt, parsed_time); | 277 EXPECT_EQ(comparison_time_pdt, parsed_time); |
278 } | 278 } |
279 | 279 |
280 } // namespace | 280 } // namespace |
OLD | NEW |