1 | // |
2 | // DateTimeParserTest.cpp |
3 | // |
4 | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
5 | // and Contributors. |
6 | // |
7 | // SPDX-License-Identifier: BSL-1.0 |
8 | // |
9 | |
10 | |
11 | #include "DateTimeParserTest.h" |
12 | #include "Poco/CppUnit/TestCaller.h" |
13 | #include "Poco/CppUnit/TestSuite.h" |
14 | #include "Poco/DateTimeParser.h" |
15 | #include "Poco/DateTimeFormat.h" |
16 | #include "Poco/DateTime.h" |
17 | #include "Poco/Timestamp.h" |
18 | #include "Poco/Exception.h" |
19 | |
20 | |
21 | using Poco::DateTime; |
22 | using Poco::DateTimeFormat; |
23 | using Poco::DateTimeParser; |
24 | using Poco::Timestamp; |
25 | using Poco::SyntaxException; |
26 | |
27 | |
28 | DateTimeParserTest::DateTimeParserTest(const std::string& rName): CppUnit::TestCase(rName) |
29 | { |
30 | } |
31 | |
32 | |
33 | DateTimeParserTest::~DateTimeParserTest() |
34 | { |
35 | } |
36 | |
37 | |
38 | void DateTimeParserTest::testISO8601() |
39 | { |
40 | int tzd; |
41 | DateTime dt = DateTimeParser::parse(DateTimeFormat::ISO8601_FORMAT, "2005-01-08T12:30:00Z" , tzd); |
42 | assertTrue (dt.year() == 2005); |
43 | assertTrue (dt.month() == 1); |
44 | assertTrue (dt.day() == 8); |
45 | assertTrue (dt.hour() == 12); |
46 | assertTrue (dt.minute() == 30); |
47 | assertTrue (dt.second() == 0); |
48 | assertTrue (tzd == 0); |
49 | testBad(DateTimeFormat::ISO8601_FRAC_FORMAT, "2005-01-08T12.30:00Z" , tzd); |
50 | |
51 | dt = DateTimeParser::parse(DateTimeFormat::ISO8601_FORMAT, "2005-01-08T12:30:00+01:00" , tzd); |
52 | assertTrue (dt.year() == 2005); |
53 | assertTrue (dt.month() == 1); |
54 | assertTrue (dt.day() == 8); |
55 | assertTrue (dt.hour() == 12); |
56 | assertTrue (dt.minute() == 30); |
57 | assertTrue (dt.second() == 0); |
58 | assertTrue (tzd == 3600); |
59 | testBad(DateTimeFormat::ISO8601_FRAC_FORMAT, "2005-01-8T12:30:00+01:00" , tzd); |
60 | |
61 | dt = DateTimeParser::parse(DateTimeFormat::ISO8601_FORMAT, "2005-01-08T12:30:00-01:00" , tzd); |
62 | assertTrue (dt.year() == 2005); |
63 | assertTrue (dt.month() == 1); |
64 | assertTrue (dt.day() == 8); |
65 | assertTrue (dt.hour() == 12); |
66 | assertTrue (dt.minute() == 30); |
67 | assertTrue (dt.second() == 0); |
68 | assertTrue (tzd == -3600); |
69 | testBad(DateTimeFormat::ISO8601_FRAC_FORMAT, "2005-01-0812:30:00-01:00" , tzd); |
70 | |
71 | dt = DateTimeParser::parse(DateTimeFormat::ISO8601_FORMAT, "2005-01-08T12:30:00" , tzd); |
72 | assertTrue (dt.year() == 2005); |
73 | assertTrue (dt.month() == 1); |
74 | assertTrue (dt.day() == 8); |
75 | assertTrue (dt.hour() == 12); |
76 | assertTrue (dt.minute() == 30); |
77 | assertTrue (dt.second() == 0); |
78 | assertTrue (tzd == 0); |
79 | testBad(DateTimeFormat::ISO8601_FRAC_FORMAT, "2005-01-08T12:30:0" , tzd); |
80 | |
81 | dt = DateTimeParser::parse(DateTimeFormat::ISO8601_FORMAT, "2005-01-08" , tzd); |
82 | assertTrue (dt.year() == 2005); |
83 | assertTrue (dt.month() == 1); |
84 | assertTrue (dt.day() == 8); |
85 | assertTrue (dt.hour() == 0); |
86 | assertTrue (dt.minute() == 0); |
87 | assertTrue (dt.second() == 0); |
88 | assertTrue (tzd == 0); |
89 | testBad(DateTimeFormat::ISO8601_FRAC_FORMAT, "2005-01-8" , tzd); |
90 | } |
91 | |
92 | |
93 | void DateTimeParserTest::testISO8601Frac() |
94 | { |
95 | int tzd; |
96 | DateTime dt = DateTimeParser::parse(DateTimeFormat::ISO8601_FRAC_FORMAT, "2005-01-08T12:30:00Z" , tzd); |
97 | assertTrue (dt.year() == 2005); |
98 | assertTrue (dt.month() == 1); |
99 | assertTrue (dt.day() == 8); |
100 | assertTrue (dt.hour() == 12); |
101 | assertTrue (dt.minute() == 30); |
102 | assertTrue (dt.second() == 0); |
103 | assertTrue (dt.millisecond() == 0); |
104 | assertTrue (dt.microsecond() == 0); |
105 | assertTrue (tzd == 0); |
106 | testBad(DateTimeFormat::ISO8601_FRAC_FORMAT, "2005-1-08T12:30:00Z" , tzd); |
107 | |
108 | dt = DateTimeParser::parse(DateTimeFormat::ISO8601_FRAC_FORMAT, "2005-01-08T12:30:00+01:00" , tzd); |
109 | assertTrue (dt.year() == 2005); |
110 | assertTrue (dt.month() == 1); |
111 | assertTrue (dt.day() == 8); |
112 | assertTrue (dt.hour() == 12); |
113 | assertTrue (dt.minute() == 30); |
114 | assertTrue (dt.second() == 0); |
115 | assertTrue (dt.millisecond() == 0); |
116 | assertTrue (dt.microsecond() == 0); |
117 | assertTrue (tzd == 3600); |
118 | testBad(DateTimeFormat::ISO8601_FRAC_FORMAT, "2005-01-08Z12:30:00+01:00" , tzd); |
119 | |
120 | dt = DateTimeParser::parse(DateTimeFormat::ISO8601_FRAC_FORMAT, "2005-01-08T12:30:00-01:00" , tzd); |
121 | assertTrue (dt.year() == 2005); |
122 | assertTrue (dt.month() == 1); |
123 | assertTrue (dt.day() == 8); |
124 | assertTrue (dt.hour() == 12); |
125 | assertTrue (dt.minute() == 30); |
126 | assertTrue (dt.second() == 0); |
127 | assertTrue (dt.millisecond() == 0); |
128 | assertTrue (dt.microsecond() == 0); |
129 | assertTrue (tzd == -3600); |
130 | testBad(DateTimeFormat::ISO8601_FRAC_FORMAT, "2005-01-08T12:30:00-01.00" , tzd); |
131 | |
132 | dt = DateTimeParser::parse(DateTimeFormat::ISO8601_FRAC_FORMAT, "2005-01-08T12:30:00" , tzd); |
133 | assertTrue (dt.year() == 2005); |
134 | assertTrue (dt.month() == 1); |
135 | assertTrue (dt.day() == 8); |
136 | assertTrue (dt.hour() == 12); |
137 | assertTrue (dt.minute() == 30); |
138 | assertTrue (dt.second() == 0); |
139 | assertTrue (dt.millisecond() == 0); |
140 | assertTrue (dt.microsecond() == 0); |
141 | assertTrue (tzd == 0); |
142 | testBad(DateTimeFormat::ISO8601_FRAC_FORMAT, "2005-01-08T12;30:00" , tzd); |
143 | |
144 | dt = DateTimeParser::parse(DateTimeFormat::ISO8601_FRAC_FORMAT, "2005-01-08" , tzd); |
145 | assertTrue (dt.year() == 2005); |
146 | assertTrue (dt.month() == 1); |
147 | assertTrue (dt.day() == 8); |
148 | assertTrue (dt.hour() == 0); |
149 | assertTrue (dt.minute() == 0); |
150 | assertTrue (dt.second() == 0); |
151 | assertTrue (dt.millisecond() == 0); |
152 | assertTrue (dt.microsecond() == 0); |
153 | assertTrue (tzd == 0); |
154 | testBad(DateTimeFormat::ISO8601_FRAC_FORMAT, "2005-01+08" , tzd); |
155 | |
156 | dt = DateTimeParser::parse(DateTimeFormat::ISO8601_FRAC_FORMAT, "2005-01-08T12:30:00.1Z" , tzd); |
157 | assertTrue (dt.year() == 2005); |
158 | assertTrue (dt.month() == 1); |
159 | assertTrue (dt.day() == 8); |
160 | assertTrue (dt.hour() == 12); |
161 | assertTrue (dt.minute() == 30); |
162 | assertTrue (dt.second() == 0); |
163 | assertTrue (dt.millisecond() == 100); |
164 | assertTrue (dt.microsecond() == 0); |
165 | assertTrue (tzd == 0); |
166 | testBad(DateTimeFormat::ISO8601_FRAC_FORMAT, "2005-01-08T12:30:00.1J" , tzd); |
167 | |
168 | dt = DateTimeParser::parse(DateTimeFormat::ISO8601_FRAC_FORMAT, "2005-01-08T12:30:00.123+01:00" , tzd); |
169 | assertTrue (dt.year() == 2005); |
170 | assertTrue (dt.month() == 1); |
171 | assertTrue (dt.day() == 8); |
172 | assertTrue (dt.hour() == 12); |
173 | assertTrue (dt.minute() == 30); |
174 | assertTrue (dt.second() == 0); |
175 | assertTrue (dt.millisecond() == 123); |
176 | assertTrue (dt.microsecond() == 0); |
177 | assertTrue (tzd == 3600); |
178 | testBad(DateTimeFormat::ISO8601_FRAC_FORMAT, "2005-01-08T12:30:00.123*01:00" , tzd); |
179 | |
180 | dt = DateTimeParser::parse(DateTimeFormat::ISO8601_FRAC_FORMAT, "2005-01-08T12:30:00.12345-01:00" , tzd); |
181 | assertTrue (dt.year() == 2005); |
182 | assertTrue (dt.month() == 1); |
183 | assertTrue (dt.day() == 8); |
184 | assertTrue (dt.hour() == 12); |
185 | assertTrue (dt.minute() == 30); |
186 | assertTrue (dt.second() == 0); |
187 | assertTrue (dt.millisecond() == 123); |
188 | assertTrue (dt.microsecond() == 450); |
189 | assertTrue (tzd == -3600); |
190 | testBad(DateTimeFormat::ISO8601_FRAC_FORMAT, "2005-01+08T12:30:00.12345-01:00" , tzd); |
191 | |
192 | dt = DateTimeParser::parse(DateTimeFormat::ISO8601_FRAC_FORMAT, "2010-09-23T16:17:01.2817002+02:00" , tzd); |
193 | assertTrue (dt.year() == 2010); |
194 | assertTrue (dt.month() == 9); |
195 | assertTrue (dt.day() == 23); |
196 | assertTrue (dt.hour() == 16); |
197 | assertTrue (dt.minute() == 17); |
198 | assertTrue (dt.second() == 1); |
199 | assertTrue (dt.millisecond() == 281); |
200 | assertTrue (dt.microsecond() == 700); |
201 | assertTrue (tzd == 7200); |
202 | testBad(DateTimeFormat::ISO8601_FRAC_FORMAT, "201009-23T16:17:01.2817002+02:00" , tzd); |
203 | |
204 | dt = DateTimeParser::parse(DateTimeFormat::ISO8601_FRAC_FORMAT, "2005-01-08T12:30:00" , tzd); |
205 | assertTrue (dt.year() == 2005); |
206 | assertTrue (dt.month() == 1); |
207 | assertTrue (dt.day() == 8); |
208 | assertTrue (dt.hour() == 12); |
209 | assertTrue (dt.minute() == 30); |
210 | assertTrue (dt.second() == 0); |
211 | assertTrue (dt.millisecond() == 0); |
212 | assertTrue (dt.microsecond() == 0); |
213 | assertTrue (tzd == 0); |
214 | testBad(DateTimeFormat::ISO8601_FRAC_FORMAT, "2005-01-08T12:30:0" , tzd); |
215 | |
216 | dt = DateTimeParser::parse(DateTimeFormat::ISO8601_FRAC_FORMAT, "2005-01-08T12:30:00.123456" , tzd); |
217 | assertTrue (dt.year() == 2005); |
218 | assertTrue (dt.month() == 1); |
219 | assertTrue (dt.day() == 8); |
220 | assertTrue (dt.hour() == 12); |
221 | assertTrue (dt.minute() == 30); |
222 | assertTrue (dt.second() == 0); |
223 | assertTrue (dt.millisecond() == 123); |
224 | assertTrue (dt.microsecond() == 456); |
225 | assertTrue (tzd == 0); |
226 | testBad(DateTimeFormat::ISO8601_FRAC_FORMAT, "005-01-08T12:30:00.123456" , tzd); |
227 | |
228 | dt = DateTimeParser::parse(DateTimeFormat::ISO8601_FRAC_FORMAT, "2005-01-08" , tzd); |
229 | assertTrue (dt.year() == 2005); |
230 | assertTrue (dt.month() == 1); |
231 | assertTrue (dt.day() == 8); |
232 | assertTrue (dt.hour() == 0); |
233 | assertTrue (dt.minute() == 0); |
234 | assertTrue (dt.second() == 0); |
235 | assertTrue (dt.millisecond() == 0); |
236 | assertTrue (dt.microsecond() == 0); |
237 | assertTrue (tzd == 0); |
238 | testBad(DateTimeFormat::ISO8601_FRAC_FORMAT, "2005-01-0" , tzd); |
239 | |
240 | testBad(DateTimeFormat::ISO8601_FRAC_FORMAT, "jnghjgnbcfjb" , tzd); |
241 | } |
242 | |
243 | |
244 | void DateTimeParserTest::testRFC822() |
245 | { |
246 | int tzd; |
247 | DateTime dt = DateTimeParser::parse(DateTimeFormat::RFC822_FORMAT, "Sat, 8 Jan 05 12:30:00 GMT" , tzd); |
248 | assertTrue (dt.year() == 2005); |
249 | assertTrue (dt.month() == 1); |
250 | assertTrue (dt.day() == 8); |
251 | assertTrue (dt.hour() == 12); |
252 | assertTrue (dt.minute() == 30); |
253 | assertTrue (dt.second() == 0); |
254 | assertTrue (tzd == 0); |
255 | testBad(DateTimeFormat::RFC822_FORMAT, "at, 8 Jan 05 12:30:00 GMT" , tzd); |
256 | |
257 | dt = DateTimeParser::parse(DateTimeFormat::RFC822_FORMAT, "Sat, 8 Jan 05 12:30:00 +0100" , tzd); |
258 | assertTrue (dt.year() == 2005); |
259 | assertTrue (dt.month() == 1); |
260 | assertTrue (dt.day() == 8); |
261 | assertTrue (dt.hour() == 12); |
262 | assertTrue (dt.minute() == 30); |
263 | assertTrue (dt.second() == 0); |
264 | assertTrue (tzd == 3600); |
265 | testBad(DateTimeFormat::RFC822_FORMAT, "Sat, x Jan 05 12:30:00 +0100" , tzd); |
266 | |
267 | dt = DateTimeParser::parse(DateTimeFormat::RFC822_FORMAT, "Sat, 8 Jan 05 12:30:00 -0100" , tzd); |
268 | assertTrue (dt.year() == 2005); |
269 | assertTrue (dt.month() == 1); |
270 | assertTrue (dt.day() == 8); |
271 | assertTrue (dt.hour() == 12); |
272 | assertTrue (dt.minute() == 30); |
273 | assertTrue (dt.second() == 0); |
274 | assertTrue (tzd == -3600); |
275 | testBad(DateTimeFormat::RFC822_FORMAT, "Sat, 8 Jan 05 12:30:00 *0100" , tzd); |
276 | |
277 | dt = DateTimeParser::parse(DateTimeFormat::RFC822_FORMAT, "Tue, 18 Jan 05 12:30:00 CDT" , tzd); |
278 | assertTrue (dt.year() == 2005); |
279 | assertTrue (dt.month() == 1); |
280 | assertTrue (dt.day() == 18); |
281 | assertTrue (dt.hour() == 12); |
282 | assertTrue (dt.minute() == 30); |
283 | assertTrue (dt.second() == 0); |
284 | assertTrue (tzd == -18000); |
285 | testBad(DateTimeFormat::RFC822_FORMAT, "Tue, 18 Jan 05 12:30:00 abc" , tzd); |
286 | |
287 | dt = DateTimeParser::parse(DateTimeFormat::RFC822_FORMAT, "Wed, 12 Sep 73 02:01:12 CST" , tzd); |
288 | assertTrue (dt.year() == 1973); |
289 | assertTrue (dt.month() == 9); |
290 | assertTrue (dt.day() == 12); |
291 | assertTrue (dt.hour() == 2); |
292 | assertTrue (dt.minute() == 1); |
293 | assertTrue (dt.second() == 12); |
294 | assertTrue (tzd == -21600); |
295 | testBad(DateTimeFormat::RFC822_FORMAT, "Wed, 12 Sep 73 02:01:2 CST" , tzd); |
296 | |
297 | dt = DateTimeParser::parse(DateTimeFormat::RFC822_FORMAT, "12 Sep 73 02:01:12 EST" , tzd); |
298 | assertTrue (dt.year() == 1973); |
299 | assertTrue (dt.month() == 9); |
300 | assertTrue (dt.day() == 12); |
301 | assertTrue (dt.hour() == 2); |
302 | assertTrue (dt.minute() == 1); |
303 | assertTrue (dt.second() == 12); |
304 | assertTrue (tzd == -18000); |
305 | testBad(DateTimeFormat::RFC822_FORMAT, "12 Sep 73 02.01:12 EST" , tzd); |
306 | } |
307 | |
308 | |
309 | void DateTimeParserTest::testRFC1123() |
310 | { |
311 | int tzd; |
312 | DateTime dt = DateTimeParser::parse(DateTimeFormat::RFC1123_FORMAT, "Sat, 8 Jan 2005 12:30:00 GMT" , tzd); |
313 | assertTrue (dt.year() == 2005); |
314 | assertTrue (dt.month() == 1); |
315 | assertTrue (dt.day() == 8); |
316 | assertTrue (dt.hour() == 12); |
317 | assertTrue (dt.minute() == 30); |
318 | assertTrue (dt.second() == 0); |
319 | assertTrue (tzd == 0); |
320 | testBad(DateTimeFormat::RFC1123_FORMAT, "Sat, 8 Jan 2005 12:30:00 GPX" , tzd); |
321 | |
322 | dt = DateTimeParser::parse(DateTimeFormat::RFC1123_FORMAT, "Sat, 8 Jan 2005 12:30:00 +0100" , tzd); |
323 | assertTrue (dt.year() == 2005); |
324 | assertTrue (dt.month() == 1); |
325 | assertTrue (dt.day() == 8); |
326 | assertTrue (dt.hour() == 12); |
327 | assertTrue (dt.minute() == 30); |
328 | assertTrue (dt.second() == 0); |
329 | assertTrue (tzd == 3600); |
330 | testBad(DateTimeFormat::RFC1123_FORMAT, "Sat, 8 Jan 2005 xy:30:00 +0100" , tzd); |
331 | |
332 | dt = DateTimeParser::parse(DateTimeFormat::RFC1123_FORMAT, "Sat, 8 Jan 2005 12:30:00 -0100" , tzd); |
333 | assertTrue (dt.year() == 2005); |
334 | assertTrue (dt.month() == 1); |
335 | assertTrue (dt.day() == 8); |
336 | assertTrue (dt.hour() == 12); |
337 | assertTrue (dt.minute() == 30); |
338 | assertTrue (dt.second() == 0); |
339 | assertTrue (tzd == -3600); |
340 | testBad(DateTimeFormat::RFC1123_FORMAT, "Sat, 8 Jan 205 12:30:00 -0100" , tzd); |
341 | |
342 | dt = DateTimeParser::parse(DateTimeFormat::RFC1123_FORMAT, "Tue, 18 Jan 2005 12:30:00 CDT" , tzd); |
343 | assertTrue (dt.year() == 2005); |
344 | assertTrue (dt.month() == 1); |
345 | assertTrue (dt.day() == 18); |
346 | assertTrue (dt.hour() == 12); |
347 | assertTrue (dt.minute() == 30); |
348 | assertTrue (dt.second() == 0); |
349 | assertTrue (tzd == -18000); |
350 | testBad(DateTimeFormat::RFC1123_FORMAT, "Hue, 18 Jan 2005 12:30:00 CDT" , tzd); |
351 | |
352 | dt = DateTimeParser::parse(DateTimeFormat::RFC1123_FORMAT, "Wed, 12 Sep 1973 02:01:12 CST" , tzd); |
353 | assertTrue (dt.year() == 1973); |
354 | assertTrue (dt.month() == 9); |
355 | assertTrue (dt.day() == 12); |
356 | assertTrue (dt.hour() == 2); |
357 | assertTrue (dt.minute() == 1); |
358 | assertTrue (dt.second() == 12); |
359 | assertTrue (tzd == -21600); |
360 | testBad(DateTimeFormat::RFC1123_FORMAT, "Wed, 12 Sp 1973 02:01:12 CST" , tzd); |
361 | |
362 | dt = DateTimeParser::parse(DateTimeFormat::RFC1123_FORMAT, "12 Sep 1973 02:01:12 EST" , tzd); |
363 | assertTrue (dt.year() == 1973); |
364 | assertTrue (dt.month() == 9); |
365 | assertTrue (dt.day() == 12); |
366 | assertTrue (dt.hour() == 2); |
367 | assertTrue (dt.minute() == 1); |
368 | assertTrue (dt.second() == 12); |
369 | assertTrue (tzd == -18000); |
370 | testBad(DateTimeFormat::RFC1123_FORMAT, "12 Sep 193 02:01:12 EST" , tzd); |
371 | } |
372 | |
373 | |
374 | void DateTimeParserTest::testHTTP() |
375 | { |
376 | int tzd; |
377 | DateTime dt = DateTimeParser::parse(DateTimeFormat::HTTP_FORMAT, "Sat, 08 Jan 2005 12:30:00 GMT" , tzd); |
378 | assertTrue (dt.year() == 2005); |
379 | assertTrue (dt.month() == 1); |
380 | assertTrue (dt.day() == 8); |
381 | assertTrue (dt.hour() == 12); |
382 | assertTrue (dt.minute() == 30); |
383 | assertTrue (dt.second() == 0); |
384 | assertTrue (tzd == 0); |
385 | testBad(DateTimeFormat::HTTP_FORMAT, "Sat, 08 Jn 2005 12:30:00 GMT" , tzd); |
386 | |
387 | dt = DateTimeParser::parse(DateTimeFormat::HTTP_FORMAT, "Sat, 08 Jan 2005 12:30:00 +0100" , tzd); |
388 | assertTrue (dt.year() == 2005); |
389 | assertTrue (dt.month() == 1); |
390 | assertTrue (dt.day() == 8); |
391 | assertTrue (dt.hour() == 12); |
392 | assertTrue (dt.minute() == 30); |
393 | assertTrue (dt.second() == 0); |
394 | assertTrue (tzd == 3600); |
395 | testBad(DateTimeFormat::HTTP_FORMAT, "Sat, 08 Jan 2005 12:30:00 j0100" , tzd); |
396 | |
397 | dt = DateTimeParser::parse(DateTimeFormat::HTTP_FORMAT, "Sat, 08 Jan 2005 12:30:00 -0100" , tzd); |
398 | assertTrue (dt.year() == 2005); |
399 | assertTrue (dt.month() == 1); |
400 | assertTrue (dt.day() == 8); |
401 | assertTrue (dt.hour() == 12); |
402 | assertTrue (dt.minute() == 30); |
403 | assertTrue (dt.second() == 0); |
404 | assertTrue (tzd == -3600); |
405 | testBad(DateTimeFormat::HTTP_FORMAT, "Sat 08 Jan 2005 12:30:00 -0100" , tzd); |
406 | } |
407 | |
408 | |
409 | void DateTimeParserTest::testRFC850() |
410 | { |
411 | int tzd; |
412 | DateTime dt = DateTimeParser::parse(DateTimeFormat::RFC850_FORMAT, "Saturday, 8-Jan-05 12:30:00 GMT" , tzd); |
413 | assertTrue (dt.year() == 2005); |
414 | assertTrue (dt.month() == 1); |
415 | assertTrue (dt.day() == 8); |
416 | assertTrue (dt.hour() == 12); |
417 | assertTrue (dt.minute() == 30); |
418 | assertTrue (dt.second() == 0); |
419 | assertTrue (tzd == 0); |
420 | testBad(DateTimeFormat::RFC850_FORMAT, "Saturday, 8-Jan 05 12:30:00 GMT" , tzd); |
421 | |
422 | dt = DateTimeParser::parse(DateTimeFormat::RFC850_FORMAT, "Saturday, 8-Jan-05 12:30:00 +0100" , tzd); |
423 | assertTrue (dt.year() == 2005); |
424 | assertTrue (dt.month() == 1); |
425 | assertTrue (dt.day() == 8); |
426 | assertTrue (dt.hour() == 12); |
427 | assertTrue (dt.minute() == 30); |
428 | assertTrue (dt.second() == 0); |
429 | assertTrue (tzd == 3600); |
430 | testBad(DateTimeFormat::RFC850_FORMAT, "Saturday, 8+Jan-05 12:30:00 +0100" , tzd); |
431 | |
432 | dt = DateTimeParser::parse(DateTimeFormat::RFC850_FORMAT, "Saturday, 8-Jan-05 12:30:00 -0100" , tzd); |
433 | assertTrue (dt.year() == 2005); |
434 | assertTrue (dt.month() == 1); |
435 | assertTrue (dt.day() == 8); |
436 | assertTrue (dt.hour() == 12); |
437 | assertTrue (dt.minute() == 30); |
438 | assertTrue (dt.second() == 0); |
439 | assertTrue (tzd == -3600); |
440 | testBad(DateTimeFormat::RFC850_FORMAT, "Saturday 8-Jan-05 12:30:00 -0100" , tzd); |
441 | |
442 | dt = DateTimeParser::parse(DateTimeFormat::RFC850_FORMAT, "Wed, 12-Sep-73 02:01:12 CST" , tzd); |
443 | assertTrue (dt.year() == 1973); |
444 | assertTrue (dt.month() == 9); |
445 | assertTrue (dt.day() == 12); |
446 | assertTrue (dt.hour() == 2); |
447 | assertTrue (dt.minute() == 1); |
448 | assertTrue (dt.second() == 12); |
449 | assertTrue (tzd == -21600); |
450 | testBad(DateTimeFormat::RFC850_FORMAT, "Wed, 12-pep-73 02:01:12 CST" , tzd); |
451 | } |
452 | |
453 | |
454 | void DateTimeParserTest::testRFC1036() |
455 | { |
456 | int tzd; |
457 | DateTime dt = DateTimeParser::parse(DateTimeFormat::RFC1036_FORMAT, "Saturday, 8 Jan 05 12:30:00 GMT" , tzd); |
458 | assertTrue (dt.year() == 2005); |
459 | assertTrue (dt.month() == 1); |
460 | assertTrue (dt.day() == 8); |
461 | assertTrue (dt.hour() == 12); |
462 | assertTrue (dt.minute() == 30); |
463 | assertTrue (dt.second() == 0); |
464 | assertTrue (tzd == 0); |
465 | testBad(DateTimeFormat::RFC1036_FORMAT, "Saturday, 8 Jan 0512:30:00 GMT" , tzd); |
466 | |
467 | dt = DateTimeParser::parse(DateTimeFormat::RFC1036_FORMAT, "Saturday, 8 Jan 05 12:30:00 +0100" , tzd); |
468 | assertTrue (dt.year() == 2005); |
469 | assertTrue (dt.month() == 1); |
470 | assertTrue (dt.day() == 8); |
471 | assertTrue (dt.hour() == 12); |
472 | assertTrue (dt.minute() == 30); |
473 | assertTrue (dt.second() == 0); |
474 | assertTrue (tzd == 3600); |
475 | testBad(DateTimeFormat::RFC1036_FORMAT, "Saturday, 8 Jan 051 12:30:00 +0100" , tzd); |
476 | |
477 | dt = DateTimeParser::parse(DateTimeFormat::RFC1036_FORMAT, "Saturday, 8 Jan 05 12:30:00 -0100" , tzd); |
478 | assertTrue (dt.year() == 2005); |
479 | assertTrue (dt.month() == 1); |
480 | assertTrue (dt.day() == 8); |
481 | assertTrue (dt.hour() == 12); |
482 | assertTrue (dt.minute() == 30); |
483 | assertTrue (dt.second() == 0); |
484 | assertTrue (tzd == -3600); |
485 | testBad(DateTimeFormat::RFC1036_FORMAT, "Saturday, 8 Jan 05 12:30:00 -0100x" , tzd); |
486 | } |
487 | |
488 | |
489 | void DateTimeParserTest::testASCTIME() |
490 | { |
491 | int tzd; |
492 | DateTime dt = DateTimeParser::parse(DateTimeFormat::ASCTIME_FORMAT, "Sat Jan 8 12:30:00 2005" , tzd); |
493 | assertTrue (dt.year() == 2005); |
494 | assertTrue (dt.month() == 1); |
495 | assertTrue (dt.day() == 8); |
496 | assertTrue (dt.hour() == 12); |
497 | assertTrue (dt.minute() == 30); |
498 | assertTrue (dt.second() == 0); |
499 | assertTrue (tzd == 0); |
500 | testBad(DateTimeFormat::ASCTIME_FORMAT, "Bat Jan 8 12:30:00 2005" , tzd); |
501 | } |
502 | |
503 | |
504 | void DateTimeParserTest::testSORTABLE() |
505 | { |
506 | int tzd; |
507 | DateTime dt = DateTimeParser::parse(DateTimeFormat::SORTABLE_FORMAT, "2005-01-08 12:30:00" , tzd); |
508 | assertTrue (dt.year() == 2005); |
509 | assertTrue (dt.month() == 1); |
510 | assertTrue (dt.day() == 8); |
511 | assertTrue (dt.hour() == 12); |
512 | assertTrue (dt.minute() == 30); |
513 | assertTrue (dt.second() == 0); |
514 | assertTrue (tzd == 0); |
515 | testBad(DateTimeFormat::SORTABLE_FORMAT, "2005-01-08 12:30;00" , tzd); |
516 | |
517 | dt = DateTimeParser::parse(DateTimeFormat::SORTABLE_FORMAT, "2005-01-08" , tzd); |
518 | assertTrue (dt.year() == 2005); |
519 | assertTrue (dt.month() == 1); |
520 | assertTrue (dt.day() == 8); |
521 | assertTrue (dt.hour() == 0); |
522 | assertTrue (dt.minute() == 0); |
523 | assertTrue (dt.second() == 0); |
524 | assertTrue (tzd == 0); |
525 | testBad(DateTimeFormat::SORTABLE_FORMAT, "2005+01-08" , tzd); |
526 | } |
527 | |
528 | |
529 | void DateTimeParserTest::testCustom() |
530 | { |
531 | int tzd; |
532 | DateTime dt = DateTimeParser::parse("%d-%b-%Y" , "18-Jan-2005" , tzd); |
533 | assertTrue (dt.year() == 2005); |
534 | assertTrue (dt.month() == 1); |
535 | assertTrue (dt.day() == 18); |
536 | assertTrue (dt.hour() == 0); |
537 | assertTrue (dt.minute() == 0); |
538 | assertTrue (dt.second() == 0); |
539 | assertTrue (tzd == 0); |
540 | |
541 | dt = DateTimeParser::parse("%m/%d/%y" , "01/18/05" , tzd); |
542 | assertTrue (dt.year() == 2005); |
543 | assertTrue (dt.month() == 1); |
544 | assertTrue (dt.day() == 18); |
545 | assertTrue (dt.hour() == 0); |
546 | assertTrue (dt.minute() == 0); |
547 | assertTrue (dt.second() == 0); |
548 | assertTrue (tzd == 0); |
549 | |
550 | dt = DateTimeParser::parse("%h:%M %a" , "12:30 am" , tzd); |
551 | assertTrue (dt.hour() == 0); |
552 | assertTrue (dt.minute() == 30); |
553 | assertTrue (dt.second() == 0); |
554 | |
555 | dt = DateTimeParser::parse("%h:%M %a" , "12:30 PM" , tzd); |
556 | assertTrue (dt.hour() == 12); |
557 | assertTrue (dt.minute() == 30); |
558 | assertTrue (dt.second() == 0); |
559 | |
560 | assertTrue (!DateTimeParser::tryParse("%h:%M %a" , "" , dt, tzd)); |
561 | assertTrue (!DateTimeParser::tryParse("" , "12:30 PM" , dt, tzd)); |
562 | assertTrue (!DateTimeParser::tryParse("" , "" , dt, tzd)); |
563 | |
564 | try |
565 | { |
566 | DateTimeParser::parse("%h:%M %a" , "" , tzd); |
567 | fail ("must fail" ); |
568 | } |
569 | catch (SyntaxException&) |
570 | { |
571 | } |
572 | |
573 | try |
574 | { |
575 | DateTimeParser::parse("" , "12:30 PM" , tzd); |
576 | fail ("must fail" ); |
577 | } |
578 | catch (SyntaxException&) |
579 | { |
580 | } |
581 | |
582 | try |
583 | { |
584 | DateTimeParser::parse("" , "" , tzd); |
585 | fail ("must fail" ); |
586 | } |
587 | catch (SyntaxException&) |
588 | { |
589 | } |
590 | } |
591 | |
592 | |
593 | void DateTimeParserTest::testGuess() |
594 | { |
595 | int tzd; |
596 | DateTime dt = DateTimeParser::parse("2005-01-08T12:30:00Z" , tzd); |
597 | assertTrue (dt.year() == 2005); |
598 | assertTrue (dt.month() == 1); |
599 | assertTrue (dt.day() == 8); |
600 | assertTrue (dt.hour() == 12); |
601 | assertTrue (dt.minute() == 30); |
602 | assertTrue (dt.second() == 0); |
603 | assertTrue (tzd == 0); |
604 | |
605 | dt = DateTimeParser::parse("20050108T123000Z" , tzd); |
606 | assertTrue (dt.year() == 2005); |
607 | assertTrue (dt.month() == 1); |
608 | assertTrue (dt.day() == 8); |
609 | assertTrue (dt.hour() == 12); |
610 | assertTrue (dt.minute() == 30); |
611 | assertTrue (dt.second() == 0); |
612 | assertTrue (tzd == 0); |
613 | |
614 | dt = DateTimeParser::parse("2005-01-08T12:30:00+01:00" , tzd); |
615 | assertTrue (dt.year() == 2005); |
616 | assertTrue (dt.month() == 1); |
617 | assertTrue (dt.day() == 8); |
618 | assertTrue (dt.hour() == 12); |
619 | assertTrue (dt.minute() == 30); |
620 | assertTrue (dt.second() == 0); |
621 | assertTrue (tzd == 3600); |
622 | |
623 | dt = DateTimeParser::parse("2005-01-08T12:30:00.123456Z" , tzd); |
624 | assertTrue (dt.year() == 2005); |
625 | assertTrue (dt.month() == 1); |
626 | assertTrue (dt.day() == 8); |
627 | assertTrue (dt.hour() == 12); |
628 | assertTrue (dt.minute() == 30); |
629 | assertTrue (dt.second() == 0); |
630 | assertTrue (tzd == 0); |
631 | assertTrue (dt.millisecond() == 123); |
632 | assertTrue (dt.microsecond() == 456); |
633 | |
634 | dt = DateTimeParser::parse("2005-01-08T12:30:00,123456Z" , tzd); |
635 | assertTrue (dt.year() == 2005); |
636 | assertTrue (dt.month() == 1); |
637 | assertTrue (dt.day() == 8); |
638 | assertTrue (dt.hour() == 12); |
639 | assertTrue (dt.minute() == 30); |
640 | assertTrue (dt.second() == 0); |
641 | assertTrue (tzd == 0); |
642 | assertTrue (dt.millisecond() == 123); |
643 | assertTrue (dt.microsecond() == 456); |
644 | |
645 | dt = DateTimeParser::parse("20050108T123000,123456Z" , tzd); |
646 | assertTrue (dt.year() == 2005); |
647 | assertTrue (dt.month() == 1); |
648 | assertTrue (dt.day() == 8); |
649 | assertTrue (dt.hour() == 12); |
650 | assertTrue (dt.minute() == 30); |
651 | assertTrue (dt.second() == 0); |
652 | assertTrue (tzd == 0); |
653 | assertTrue (dt.millisecond() == 123); |
654 | assertTrue (dt.microsecond() == 456); |
655 | |
656 | dt = DateTimeParser::parse("20050108T123000.123+0200" , tzd); |
657 | assertTrue (dt.year() == 2005); |
658 | assertTrue (dt.month() == 1); |
659 | assertTrue (dt.day() == 8); |
660 | assertTrue (dt.hour() == 12); |
661 | assertTrue (dt.minute() == 30); |
662 | assertTrue (dt.second() == 0); |
663 | assertTrue (tzd == 7200); |
664 | assertTrue (dt.millisecond() == 123); |
665 | assertTrue (dt.microsecond() == 0); |
666 | |
667 | |
668 | dt = DateTimeParser::parse("2005-01-08T12:30:00.123456-02:00" , tzd); |
669 | assertTrue (dt.year() == 2005); |
670 | assertTrue (dt.month() == 1); |
671 | assertTrue (dt.day() == 8); |
672 | assertTrue (dt.hour() == 12); |
673 | assertTrue (dt.minute() == 30); |
674 | assertTrue (dt.second() == 0); |
675 | assertTrue (tzd == -7200); |
676 | assertTrue (dt.millisecond() == 123); |
677 | assertTrue (dt.microsecond() == 456); |
678 | |
679 | dt = DateTimeParser::parse("Sat, 8 Jan 05 12:30:00 +0100" , tzd); |
680 | assertTrue (dt.year() == 2005); |
681 | assertTrue (dt.month() == 1); |
682 | assertTrue (dt.day() == 8); |
683 | assertTrue (dt.hour() == 12); |
684 | assertTrue (dt.minute() == 30); |
685 | assertTrue (dt.second() == 0); |
686 | assertTrue (tzd == 3600); |
687 | |
688 | dt = DateTimeParser::parse("Sat, 8 Jan 2005 12:30:00 +0100" , tzd); |
689 | assertTrue (dt.year() == 2005); |
690 | assertTrue (dt.month() == 1); |
691 | assertTrue (dt.day() == 8); |
692 | assertTrue (dt.hour() == 12); |
693 | assertTrue (dt.minute() == 30); |
694 | assertTrue (dt.second() == 0); |
695 | assertTrue (tzd == 3600); |
696 | |
697 | dt = DateTimeParser::parse("Sat Jan 8 12:30:00 2005" , tzd); |
698 | assertTrue (dt.year() == 2005); |
699 | assertTrue (dt.month() == 1); |
700 | assertTrue (dt.day() == 8); |
701 | assertTrue (dt.hour() == 12); |
702 | assertTrue (dt.minute() == 30); |
703 | assertTrue (dt.second() == 0); |
704 | assertTrue (tzd == 0); |
705 | |
706 | dt = DateTimeParser::parse("2005-01-08 12:30:00" , tzd); |
707 | assertTrue (dt.year() == 2005); |
708 | assertTrue (dt.month() == 1); |
709 | assertTrue (dt.day() == 8); |
710 | assertTrue (dt.hour() == 12); |
711 | assertTrue (dt.minute() == 30); |
712 | assertTrue (dt.second() == 0); |
713 | assertTrue (tzd == 0); |
714 | |
715 | dt = DateTimeParser::parse("2005-01-08" , tzd); |
716 | assertTrue (dt.year() == 2005); |
717 | assertTrue (dt.month() == 1); |
718 | assertTrue (dt.day() == 8); |
719 | assertTrue (dt.hour() == 0); |
720 | assertTrue (dt.minute() == 0); |
721 | assertTrue (dt.second() == 0); |
722 | assertTrue (tzd == 0); |
723 | } |
724 | |
725 | |
726 | void DateTimeParserTest::testParseMonth() |
727 | { |
728 | std::string str = "January" ; |
729 | std::string::const_iterator it = str.begin(); |
730 | int month = DateTimeParser::parseMonth(it, str.end()); |
731 | assertTrue (month == 1); |
732 | str = "February" ; |
733 | it = str.begin(); |
734 | month = DateTimeParser::parseMonth(it, str.end()); |
735 | assertTrue (month == 2); |
736 | str = "December" ; |
737 | it = str.begin(); |
738 | month = DateTimeParser::parseMonth(it, str.end()); |
739 | assertTrue (month == 12); |
740 | str = "Jan" ; |
741 | it = str.begin(); |
742 | month = DateTimeParser::parseMonth(it, str.end()); |
743 | assertTrue (month == 1); |
744 | str = "Feb" ; |
745 | it = str.begin(); |
746 | month = DateTimeParser::parseMonth(it, str.end()); |
747 | assertTrue (month == 2); |
748 | str = "jan" ; |
749 | it = str.begin(); |
750 | month = DateTimeParser::parseMonth(it, str.end()); |
751 | assertTrue (month == 1); |
752 | str = "feb" ; |
753 | it = str.begin(); |
754 | month = DateTimeParser::parseMonth(it, str.end()); |
755 | assertTrue (month == 2); |
756 | |
757 | try |
758 | { |
759 | str = "ja" ; |
760 | it = str.begin(); |
761 | month = DateTimeParser::parseMonth(it, str.end()); |
762 | fail("Not a valid month name - must throw" ); |
763 | } |
764 | catch (SyntaxException&) |
765 | { |
766 | } |
767 | } |
768 | |
769 | |
770 | void DateTimeParserTest::testParseDayOfWeek() |
771 | { |
772 | std::string str = "Sunday" ; |
773 | std::string::const_iterator it = str.begin(); |
774 | int dow = DateTimeParser::parseDayOfWeek(it, str.end()); |
775 | assertTrue (dow == 0); |
776 | str = "Monday" ; |
777 | it = str.begin(); |
778 | dow = DateTimeParser::parseDayOfWeek(it, str.end()); |
779 | assertTrue (dow == 1); |
780 | str = "Saturday" ; |
781 | it = str.begin(); |
782 | dow = DateTimeParser::parseDayOfWeek(it, str.end()); |
783 | assertTrue (dow == 6); |
784 | str = "Sun" ; |
785 | it = str.begin(); |
786 | dow = DateTimeParser::parseDayOfWeek(it, str.end()); |
787 | assertTrue (dow == 0); |
788 | str = "Mon" ; |
789 | it = str.begin(); |
790 | dow = DateTimeParser::parseDayOfWeek(it, str.end()); |
791 | assertTrue (dow == 1); |
792 | str = "sun" ; |
793 | it = str.begin(); |
794 | dow = DateTimeParser::parseDayOfWeek(it, str.end()); |
795 | assertTrue (dow == 0); |
796 | str = "mon" ; |
797 | it = str.begin(); |
798 | dow = DateTimeParser::parseDayOfWeek(it, str.end()); |
799 | assertTrue (dow == 1); |
800 | |
801 | try |
802 | { |
803 | str = "su" ; |
804 | it = str.begin(); |
805 | dow = DateTimeParser::parseDayOfWeek(it, str.end()); |
806 | fail("Not a valid weekday name - must throw" ); |
807 | } |
808 | catch (SyntaxException&) |
809 | { |
810 | } |
811 | } |
812 | |
813 | |
814 | void DateTimeParserTest::testBad(const std::string& fmt, const std::string& dateStr, int tzd) |
815 | { |
816 | try |
817 | { |
818 | DateTime dt; |
819 | DateTimeParser::parse(fmt, dateStr, dt, tzd); |
820 | fail ("must fail" ); |
821 | } |
822 | catch(const Poco::Exception) { } |
823 | } |
824 | |
825 | |
826 | void DateTimeParserTest::setUp() |
827 | { |
828 | } |
829 | |
830 | |
831 | void DateTimeParserTest::tearDown() |
832 | { |
833 | } |
834 | |
835 | |
836 | CppUnit::Test* DateTimeParserTest::suite() |
837 | { |
838 | CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("DateTimeParserTest" ); |
839 | |
840 | CppUnit_addTest(pSuite, DateTimeParserTest, testISO8601); |
841 | CppUnit_addTest(pSuite, DateTimeParserTest, testISO8601Frac); |
842 | CppUnit_addTest(pSuite, DateTimeParserTest, testRFC822); |
843 | CppUnit_addTest(pSuite, DateTimeParserTest, testRFC1123); |
844 | CppUnit_addTest(pSuite, DateTimeParserTest, testHTTP); |
845 | CppUnit_addTest(pSuite, DateTimeParserTest, testRFC850); |
846 | CppUnit_addTest(pSuite, DateTimeParserTest, testRFC1036); |
847 | CppUnit_addTest(pSuite, DateTimeParserTest, testASCTIME); |
848 | CppUnit_addTest(pSuite, DateTimeParserTest, testSORTABLE); |
849 | CppUnit_addTest(pSuite, DateTimeParserTest, testCustom); |
850 | CppUnit_addTest(pSuite, DateTimeParserTest, testGuess); |
851 | CppUnit_addTest(pSuite, DateTimeParserTest, testParseMonth); |
852 | CppUnit_addTest(pSuite, DateTimeParserTest, testParseDayOfWeek); |
853 | |
854 | return pSuite; |
855 | } |
856 | |