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

Side by Side Diff: base/time/pr_time_unittest.cc

Issue 266193002: Extend PR_ParseTimeString() to accept some ISO 8601 formats to fix timezone parsing in SyslogParser. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix microsecond parsing for Windows and for timezoneless (local time) inputs. Created 6 years, 7 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
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 <stdint.h>
5 #include <time.h> 6 #include <time.h>
6 7
7 #include "base/compiler_specific.h" 8 #include "base/compiler_specific.h"
8 #include "base/third_party/nspr/prtime.h" 9 #include "base/third_party/nspr/prtime.h"
9 #include "base/time/time.h" 10 #include "base/time/time.h"
10 #include "testing/gtest/include/gtest/gtest.h" 11 #include "testing/gtest/include/gtest/gtest.h"
11 12
12 using base::Time; 13 using base::Time;
13 14
14 namespace { 15 namespace {
15 16
16 // time_t representation of 15th Oct 2007 12:45:00 PDT 17 // time_t representation of 15th Oct 2007 12:45:00 PDT
17 PRTime comparison_time_pdt = 1192477500 * Time::kMicrosecondsPerSecond; 18 PRTime comparison_time_pdt = 1192477500 * Time::kMicrosecondsPerSecond;
18 19
20 // Time with positive tz offset and fractional seconds:
21 // 2013-07-08T11:28:12.441381+02:00
22 PRTime comparison_time_2 = INT64_C(1373275692441381); // represented as GMT
23
19 // Specialized test fixture allowing time strings without timezones to be 24 // Specialized test fixture allowing time strings without timezones to be
20 // tested by comparing them to a known time in the local zone. 25 // tested by comparing them to a known time in the local zone.
21 class PRTimeTest : public testing::Test { 26 class PRTimeTest : public testing::Test {
22 protected: 27 protected:
23 virtual void SetUp() OVERRIDE { 28 virtual void SetUp() OVERRIDE {
24 // Use mktime to get a time_t, and turn it into a PRTime by converting 29 // Use mktime to get a time_t, and turn it into a PRTime by converting
25 // seconds to microseconds. Use 15th Oct 2007 12:45:00 local. This 30 // seconds to microseconds. Use 15th Oct 2007 12:45:00 local. This
26 // must be a time guaranteed to be outside of a DST fallback hour in 31 // must be a time guaranteed to be outside of a DST fallback hour in
27 // any timezone. 32 // any timezone.
28 struct tm local_comparison_tm = { 33 struct tm local_comparison_tm = {
29 0, // second 34 0, // second
30 45, // minute 35 45, // minute
31 12, // hour 36 12, // hour
32 15, // day of month 37 15, // day of month
33 10 - 1, // month 38 10 - 1, // month
34 2007 - 1900, // year 39 2007 - 1900, // year
35 0, // day of week (ignored, output only) 40 0, // day of week (ignored, output only)
36 0, // day of year (ignored, output only) 41 0, // day of year (ignored, output only)
37 -1 // DST in effect, -1 tells mktime to figure it out 42 -1 // DST in effect, -1 tells mktime to figure it out
38 }; 43 };
39 comparison_time_local_ = mktime(&local_comparison_tm) * 44 comparison_time_local_ =
40 Time::kMicrosecondsPerSecond; 45 mktime(&local_comparison_tm) * Time::kMicrosecondsPerSecond;
41 ASSERT_GT(comparison_time_local_, 0); 46 ASSERT_GT(comparison_time_local_, 0);
47
48 const int microseconds = 441381;
49 struct tm local_comparison_tm_2 = {
50 12, // second
51 28, // minute
52 11, // hour
53 8, // day of month
54 7 - 1, // month
55 2013 - 1900, // year
56 0, // day of week (ignored, output only)
57 0, // day of year (ignored, output only)
58 -1 // DST in effect, -1 tells mktime to figure it out
59 };
Mark Mentovai 2014/05/13 14:22:50 Alignment nit: this closing } should not line up w
Thiemo Nagel 2014/05/13 14:29:33 Thank you! Done.
60 comparison_time_local_2_ =
61 mktime(&local_comparison_tm_2) * Time::kMicrosecondsPerSecond;
62 ASSERT_GT(comparison_time_local_2_, 0);
63 comparison_time_local_2_ += microseconds;
42 } 64 }
43 65
44 PRTime comparison_time_local_; 66 PRTime comparison_time_local_;
67 PRTime comparison_time_local_2_;
45 }; 68 };
46 69
47 // Tests the PR_ParseTimeString nspr helper function for 70 // Tests the PR_ParseTimeString nspr helper function for
48 // a variety of time strings. 71 // a variety of time strings.
49 TEST_F(PRTimeTest, ParseTimeTest1) { 72 TEST_F(PRTimeTest, ParseTimeTest1) {
50 time_t current_time = 0; 73 time_t current_time = 0;
51 time(&current_time); 74 time(&current_time);
52 75
53 const int BUFFER_SIZE = 64; 76 const int BUFFER_SIZE = 64;
54 struct tm local_time = {0}; 77 struct tm local_time = {0};
(...skipping 12 matching lines...) Expand all
67 PRStatus result = PR_ParseTimeString(time_buf, PR_FALSE, &parsed_time); 90 PRStatus result = PR_ParseTimeString(time_buf, PR_FALSE, &parsed_time);
68 EXPECT_EQ(PR_SUCCESS, result); 91 EXPECT_EQ(PR_SUCCESS, result);
69 EXPECT_EQ(current_time64, parsed_time); 92 EXPECT_EQ(current_time64, parsed_time);
70 } 93 }
71 94
72 TEST_F(PRTimeTest, ParseTimeTest2) { 95 TEST_F(PRTimeTest, ParseTimeTest2) {
73 PRTime parsed_time = 0; 96 PRTime parsed_time = 0;
74 PRStatus result = PR_ParseTimeString("Mon, 15 Oct 2007 19:45:00 GMT", 97 PRStatus result = PR_ParseTimeString("Mon, 15 Oct 2007 19:45:00 GMT",
75 PR_FALSE, &parsed_time); 98 PR_FALSE, &parsed_time);
76 EXPECT_EQ(PR_SUCCESS, result); 99 EXPECT_EQ(PR_SUCCESS, result);
77 EXPECT_EQ(parsed_time, comparison_time_pdt); 100 EXPECT_EQ(comparison_time_pdt, parsed_time);
78 } 101 }
79 102
80 TEST_F(PRTimeTest, ParseTimeTest3) { 103 TEST_F(PRTimeTest, ParseTimeTest3) {
81 PRTime parsed_time = 0; 104 PRTime parsed_time = 0;
82 PRStatus result = PR_ParseTimeString("15 Oct 07 12:45:00", PR_FALSE, 105 PRStatus result = PR_ParseTimeString("15 Oct 07 12:45:00", PR_FALSE,
83 &parsed_time); 106 &parsed_time);
84 EXPECT_EQ(PR_SUCCESS, result); 107 EXPECT_EQ(PR_SUCCESS, result);
85 EXPECT_EQ(parsed_time, comparison_time_local_); 108 EXPECT_EQ(comparison_time_local_, parsed_time);
86 } 109 }
87 110
88 TEST_F(PRTimeTest, ParseTimeTest4) { 111 TEST_F(PRTimeTest, ParseTimeTest4) {
89 PRTime parsed_time = 0; 112 PRTime parsed_time = 0;
90 PRStatus result = PR_ParseTimeString("15 Oct 07 19:45 GMT", PR_FALSE, 113 PRStatus result = PR_ParseTimeString("15 Oct 07 19:45 GMT", PR_FALSE,
91 &parsed_time); 114 &parsed_time);
92 EXPECT_EQ(PR_SUCCESS, result); 115 EXPECT_EQ(PR_SUCCESS, result);
93 EXPECT_EQ(parsed_time, comparison_time_pdt); 116 EXPECT_EQ(comparison_time_pdt, parsed_time);
94 } 117 }
95 118
96 TEST_F(PRTimeTest, ParseTimeTest5) { 119 TEST_F(PRTimeTest, ParseTimeTest5) {
97 PRTime parsed_time = 0; 120 PRTime parsed_time = 0;
98 PRStatus result = PR_ParseTimeString("Mon Oct 15 12:45 PDT 2007", 121 PRStatus result = PR_ParseTimeString("Mon Oct 15 12:45 PDT 2007",
99 PR_FALSE, &parsed_time); 122 PR_FALSE, &parsed_time);
100 EXPECT_EQ(PR_SUCCESS, result); 123 EXPECT_EQ(PR_SUCCESS, result);
101 EXPECT_EQ(parsed_time, comparison_time_pdt); 124 EXPECT_EQ(comparison_time_pdt, parsed_time);
102 } 125 }
103 126
104 TEST_F(PRTimeTest, ParseTimeTest6) { 127 TEST_F(PRTimeTest, ParseTimeTest6) {
105 PRTime parsed_time = 0; 128 PRTime parsed_time = 0;
106 PRStatus result = PR_ParseTimeString("Monday, Oct 15, 2007 12:45 PM", 129 PRStatus result = PR_ParseTimeString("Monday, Oct 15, 2007 12:45 PM",
107 PR_FALSE, &parsed_time); 130 PR_FALSE, &parsed_time);
108 EXPECT_EQ(PR_SUCCESS, result); 131 EXPECT_EQ(PR_SUCCESS, result);
109 EXPECT_EQ(parsed_time, comparison_time_local_); 132 EXPECT_EQ(comparison_time_local_, parsed_time);
110 } 133 }
111 134
112 TEST_F(PRTimeTest, ParseTimeTest7) { 135 TEST_F(PRTimeTest, ParseTimeTest7) {
113 PRTime parsed_time = 0; 136 PRTime parsed_time = 0;
114 PRStatus result = PR_ParseTimeString("10/15/07 12:45:00 PM", PR_FALSE, 137 PRStatus result = PR_ParseTimeString("10/15/07 12:45:00 PM", PR_FALSE,
115 &parsed_time); 138 &parsed_time);
116 EXPECT_EQ(PR_SUCCESS, result); 139 EXPECT_EQ(PR_SUCCESS, result);
117 EXPECT_EQ(parsed_time, comparison_time_local_); 140 EXPECT_EQ(comparison_time_local_, parsed_time);
118 } 141 }
119 142
120 TEST_F(PRTimeTest, ParseTimeTest8) { 143 TEST_F(PRTimeTest, ParseTimeTest8) {
121 PRTime parsed_time = 0; 144 PRTime parsed_time = 0;
122 PRStatus result = PR_ParseTimeString("15-OCT-2007 12:45pm", PR_FALSE, 145 PRStatus result = PR_ParseTimeString("10/15/07 12:45:00. PM", PR_FALSE,
123 &parsed_time); 146 &parsed_time);
124 EXPECT_EQ(PR_SUCCESS, result); 147 EXPECT_EQ(PR_SUCCESS, result);
125 EXPECT_EQ(parsed_time, comparison_time_local_); 148 EXPECT_EQ(comparison_time_local_, parsed_time);
126 } 149 }
127 150
128 TEST_F(PRTimeTest, ParseTimeTest9) { 151 TEST_F(PRTimeTest, ParseTimeTest9) {
129 PRTime parsed_time = 0; 152 PRTime parsed_time = 0;
153 PRStatus result = PR_ParseTimeString("10/15/07 12:45:00.0 PM", PR_FALSE,
154 &parsed_time);
155 EXPECT_EQ(PR_SUCCESS, result);
156 EXPECT_EQ(comparison_time_local_, parsed_time);
157 }
158
159 TEST_F(PRTimeTest, ParseTimeTest10) {
160 PRTime parsed_time = 0;
161 PRStatus result = PR_ParseTimeString("15-OCT-2007 12:45pm", PR_FALSE,
162 &parsed_time);
163 EXPECT_EQ(PR_SUCCESS, result);
164 EXPECT_EQ(comparison_time_local_, parsed_time);
165 }
166
167 TEST_F(PRTimeTest, ParseTimeTest11) {
168 PRTime parsed_time = 0;
130 PRStatus result = PR_ParseTimeString("16 Oct 2007 4:45-JST (Tuesday)", 169 PRStatus result = PR_ParseTimeString("16 Oct 2007 4:45-JST (Tuesday)",
131 PR_FALSE, &parsed_time); 170 PR_FALSE, &parsed_time);
132 EXPECT_EQ(PR_SUCCESS, result); 171 EXPECT_EQ(PR_SUCCESS, result);
133 EXPECT_EQ(parsed_time, comparison_time_pdt); 172 EXPECT_EQ(comparison_time_pdt, parsed_time);
173 }
174
175 // hh:mm timezone offset.
176 TEST_F(PRTimeTest, ParseTimeTest12) {
177 PRTime parsed_time = 0;
178 PRStatus result = PR_ParseTimeString("2013-07-08T11:28:12.441381+02:00",
179 PR_FALSE, &parsed_time);
180 EXPECT_EQ(PR_SUCCESS, result);
181 EXPECT_EQ(comparison_time_2, parsed_time);
182 }
183
184 // hhmm timezone offset.
185 TEST_F(PRTimeTest, ParseTimeTest13) {
186 PRTime parsed_time = 0;
187 PRStatus result = PR_ParseTimeString("2013-07-08T11:28:12.441381+0200",
188 PR_FALSE, &parsed_time);
189 EXPECT_EQ(PR_SUCCESS, result);
190 EXPECT_EQ(comparison_time_2, parsed_time);
191 }
192
193 // hh timezone offset.
194 TEST_F(PRTimeTest, ParseTimeTest14) {
195 PRTime parsed_time = 0;
196 PRStatus result = PR_ParseTimeString("2013-07-08T11:28:12.4413819+02",
197 PR_FALSE, &parsed_time);
198 EXPECT_EQ(PR_SUCCESS, result);
199 EXPECT_EQ(comparison_time_2, parsed_time);
200 }
201
202 // 5 digits fractional second.
203 TEST_F(PRTimeTest, ParseTimeTest15) {
204 PRTime parsed_time = 0;
205 PRStatus result = PR_ParseTimeString("2013-07-08T09:28:12.44138Z",
206 PR_FALSE, &parsed_time);
207 EXPECT_EQ(PR_SUCCESS, result);
208 EXPECT_EQ(comparison_time_2-1, parsed_time);
209 }
210
211 // Fractional seconds, local timezone.
212 TEST_F(PRTimeTest, ParseTimeTest16) {
213 PRTime parsed_time = 0;
214 PRStatus result = PR_ParseTimeString("2013-07-08T11:28:12.441381",
215 PR_FALSE, &parsed_time);
216 EXPECT_EQ(PR_SUCCESS, result);
217 EXPECT_EQ(comparison_time_local_2_, parsed_time);
218 }
219
220 // "Z" (=GMT) timezone.
221 TEST_F(PRTimeTest, ParseTimeTest17) {
222 PRTime parsed_time = 0;
223 PRStatus result = PR_ParseTimeString("2013-07-08T09:28:12.441381Z",
224 PR_FALSE, &parsed_time);
225 EXPECT_EQ(PR_SUCCESS, result);
226 EXPECT_EQ(comparison_time_2, parsed_time);
227 }
228
229 // "T" delimiter replaced by space.
230 TEST_F(PRTimeTest, ParseTimeTest18) {
231 PRTime parsed_time = 0;
232 PRStatus result = PR_ParseTimeString("2013-07-08 09:28:12.441381Z",
233 PR_FALSE, &parsed_time);
234 EXPECT_EQ(PR_SUCCESS, result);
235 EXPECT_EQ(comparison_time_2, parsed_time);
236 }
237
238 TEST_F(PRTimeTest, ParseTimeTestInvalid1) {
239 PRTime parsed_time = 0;
240 PRStatus result = PR_ParseTimeString("201-07-08T09:28:12.441381Z",
241 PR_FALSE, &parsed_time);
242 EXPECT_EQ(PR_FAILURE, result);
243 EXPECT_EQ(0, parsed_time);
Mark Mentovai 2014/05/13 14:22:50 I don’t think it’s important or reasonable to test
Thiemo Nagel 2014/05/13 14:29:33 That's fair. Done.
244 }
245
246 TEST_F(PRTimeTest, ParseTimeTestInvalid2) {
247 PRTime parsed_time = 0;
248 PRStatus result = PR_ParseTimeString("2013-007-08T09:28:12.441381Z",
249 PR_FALSE, &parsed_time);
250 EXPECT_EQ(PR_FAILURE, result);
251 EXPECT_EQ(0, parsed_time);
252 }
253
254 TEST_F(PRTimeTest, ParseTimeTestInvalid3) {
255 PRTime parsed_time = 0;
256 PRStatus result = PR_ParseTimeString("2013-07-008T09:28:12.441381Z",
257 PR_FALSE, &parsed_time);
258 EXPECT_EQ(PR_FAILURE, result);
259 EXPECT_EQ(0, parsed_time);
134 } 260 }
135 261
136 // This test should not crash when compiled with Visual C++ 2005 (see 262 // This test should not crash when compiled with Visual C++ 2005 (see
137 // http://crbug.com/4387). 263 // http://crbug.com/4387).
138 TEST_F(PRTimeTest, ParseTimeTestOutOfRange) { 264 TEST_F(PRTimeTest, ParseTimeTestOutOfRange) {
139 PRTime parsed_time = 0; 265 PRTime parsed_time = 0;
140 // Note the lack of timezone in the time string. The year has to be 3001. 266 // Note the lack of timezone in the time string. The year has to be 3001.
141 // The date has to be after 23:59:59, December 31, 3000, US Pacific Time, so 267 // The date has to be after 23:59:59, December 31, 3000, US Pacific Time, so
142 // we use January 2, 3001 to make sure it's after the magic maximum in any 268 // we use January 2, 3001 to make sure it's after the magic maximum in any
143 // timezone. 269 // timezone.
(...skipping 12 matching lines...) Expand all
156 282
157 TEST_F(PRTimeTest, ParseTimeTestNotNormalized2) { 283 TEST_F(PRTimeTest, ParseTimeTestNotNormalized2) {
158 PRTime parsed_time = 0; 284 PRTime parsed_time = 0;
159 PRStatus result = PR_ParseTimeString("Sun Oct 14 36:45 PDT 2007", 285 PRStatus result = PR_ParseTimeString("Sun Oct 14 36:45 PDT 2007",
160 PR_FALSE, &parsed_time); 286 PR_FALSE, &parsed_time);
161 EXPECT_EQ(PR_SUCCESS, result); 287 EXPECT_EQ(PR_SUCCESS, result);
162 EXPECT_EQ(comparison_time_pdt, parsed_time); 288 EXPECT_EQ(comparison_time_pdt, parsed_time);
163 } 289 }
164 290
165 } // namespace 291 } // namespace
OLDNEW
« no previous file with comments | « base/third_party/nspr/prtime.cc ('k') | chrome/browser/extensions/api/log_private/syslog_parser.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698