]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | /* Copyright (c) 2002,2003,2005 CrystalClear Software, Inc. |
2 | * Use, modification and distribution is subject to the | |
3 | * Boost Software License, Version 1.0. (See accompanying | |
4 | * file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt) | |
5 | * Author: Jeff Garland, Bart Garst | |
6 | */ | |
7 | ||
8 | #include "boost/date_time/posix_time/posix_time.hpp" | |
9 | #include "../testfrmwk.hpp" | |
10 | ||
11 | ||
12 | int | |
13 | main() | |
14 | { | |
15 | using namespace boost::gregorian; | |
16 | using namespace boost::posix_time; | |
17 | ||
18 | #ifdef BOOST_DATE_TIME_HAS_NANOSECONDS | |
f67539c2 | 19 | { |
7c673cae FG |
20 | std::string s1("12:11:10.123456789"); |
21 | time_duration td1= duration_from_string(s1); | |
22 | check("parse time duration: " + s1, | |
23 | td1 == time_duration(12,11,10,123456789)); | |
24 | std::string s2("12:11:10,123456789"); | |
25 | time_duration td2= boost::date_time::parse_delimited_time_duration<time_duration>(s2); | |
26 | check("parse time duration: " + s2, | |
27 | td2 == time_duration(12,11,10,123456789)); | |
28 | std::string s3("12:11:10"); | |
29 | time_duration td3= boost::date_time::parse_delimited_time_duration<time_duration>(s3); | |
30 | check("parse time duration: " + s3, | |
31 | td3 == time_duration(12,11,10,0)); | |
32 | std::string s4("23:59:59.000000001"); | |
33 | time_duration td4= boost::date_time::parse_delimited_time_duration<time_duration>(s4); | |
34 | check("parse time duration: " + s4, | |
35 | td4 == time_duration(23,59,59)+nanosec(1)); | |
36 | std::string s5("23:59:59.999999999"); | |
37 | time_duration td5= boost::date_time::parse_delimited_time_duration<time_duration>(s5); | |
38 | check("parse time duration: " + s5, | |
39 | td5 == time_duration(23,59,59)+nanosec(999999999)); | |
40 | std::string s5b("-23:59:59.999999999"); | |
41 | time_duration td5b= boost::date_time::parse_delimited_time_duration<time_duration>(s5b); | |
42 | check("parse time duration: " + s5b, | |
43 | td5b == time_duration(-23,59,59)-nanosec(999999999)); | |
44 | ||
45 | std::string s6b("1:00:00.1"); // we want 1/10th | |
46 | time_duration td6b= boost::date_time::parse_delimited_time_duration<time_duration>(s6b); | |
47 | check("parse time duration: " + s6b, | |
48 | td6b == time_duration(1,0,0)+nanosec(100000000)); // we want 1/10th | |
49 | ||
50 | std::string s7b("-1:00:00.0010"); // we want 1/1000th | |
51 | time_duration td7b= boost::date_time::parse_delimited_time_duration<time_duration>(s7b); | |
52 | check("parse time duration: " + s7b, | |
53 | td7b == time_duration(-1,0,0)-nanosec(1000000)); // we want 1/1000th | |
54 | ||
55 | std::string s8b("1:22:33.123456789321"); // too many digits | |
56 | time_duration td8b= boost::date_time::parse_delimited_time_duration<time_duration>(s8b); | |
57 | check("parse time duration: " + s8b, | |
58 | td8b == time_duration(1,22,33,123456789)); // excess digits should be dropped | |
f67539c2 | 59 | } |
7c673cae | 60 | { |
f67539c2 | 61 | |
7c673cae FG |
62 | std::string s3("12:11:10"); |
63 | time_duration td3= boost::date_time::parse_delimited_time_duration<time_duration>(s3); | |
64 | check("parse time duration: " + s3, | |
65 | td3 == time_duration(12,11,10,0)); | |
66 | std::string s4("23:59:59.000001"); | |
67 | time_duration td4= boost::date_time::parse_delimited_time_duration<time_duration>(s4); | |
68 | check("parse time duration: " + s4, | |
69 | td4 == time_duration(23,59,59)+microsec(1)); | |
70 | std::string s5("23:59:59.999999"); | |
71 | time_duration td5= boost::date_time::parse_delimited_time_duration<time_duration>(s5); | |
72 | check("parse time duration: " + s5, | |
73 | td5 == time_duration(23,59,59)+microsec(999999)); | |
74 | std::string s5b("-23:59:59.999999"); | |
75 | time_duration td5b= boost::date_time::parse_delimited_time_duration<time_duration>(s5b); | |
76 | check("parse time duration: " + s5b, | |
77 | td5b == time_duration(-23,59,59)-microsec(999999)); | |
78 | ||
79 | std::string s6b("1:00:00.1"); // we want 1/10th | |
80 | time_duration td6b= boost::date_time::parse_delimited_time_duration<time_duration>(s6b); | |
81 | check("parse time duration: " + s6b, | |
82 | td6b == time_duration(1,0,0)+microsec(100000)); // we want 1/10th | |
83 | ||
84 | std::string s7b("-1:00:00.0010"); // we want 1/1000th | |
85 | time_duration td7b= boost::date_time::parse_delimited_time_duration<time_duration>(s7b); | |
86 | check("parse time duration: " + s7b, | |
87 | td7b == time_duration(-1,0,0)-microsec(1000)); // we want 1/1000th | |
7c673cae | 88 | |
f67539c2 | 89 | } |
7c673cae | 90 | |
11fdf7f2 TL |
91 | { |
92 | std::string ts2("2002-12-31 00:00:00.999999999"); | |
93 | ptime t2 = time_from_string(ts2); | |
94 | check("parse time: " + ts2, | |
95 | t2 == ptime(date(2002,12,31),time_duration(0,0,0)+nanosec(999999999))); | |
96 | } | |
7c673cae FG |
97 | { |
98 | std::string ts2("2002-12-31 00:00:00."); | |
99 | ptime t2 = time_from_string(ts2); | |
100 | check("parse time (decimal but no digits): " + ts2, | |
101 | t2 == ptime(date(2002,12,31),time_duration(0,0,0))); | |
102 | } | |
103 | #endif | |
104 | ||
f67539c2 TL |
105 | if (time_duration::resolution() != boost::date_time::nano) |
106 | { | |
107 | ||
108 | std::string s1("12:11:10.123456"); | |
109 | time_duration td1= duration_from_string(s1); | |
110 | check("parse time duration: " + s1, | |
111 | td1 == time_duration(12,11,10,123456)); | |
112 | std::cout << "td1: " << td1 << std::endl; | |
113 | ||
114 | std::string s2("12:11:10,123456"); | |
115 | time_duration td2= boost::date_time::parse_delimited_time_duration<time_duration>(s2); | |
116 | check("parse time duration: " + s2, | |
117 | td2 == time_duration(12,11,10,123456)); | |
118 | std::cout << "td2: " << td2 << std::endl; | |
119 | ||
120 | //truncate for non-nanosecond case | |
121 | std::string s8b("1:22:33.123456321"); // too many digits | |
122 | time_duration td8b= boost::date_time::parse_delimited_time_duration<time_duration>(s8b); | |
123 | check("parse time duration: " + s8b, | |
124 | td8b == time_duration(1,22,33,123456)); // excess digits should be dropped | |
125 | } | |
126 | ||
7c673cae FG |
127 | |
128 | std::string date_1, tod_1; | |
129 | std::string ts1("2002-01-20 23:59:59.000"); | |
130 | boost::date_time::split(ts1, ' ', date_1, tod_1); | |
131 | check("split function date part of " + ts1, | |
132 | date_1 == std::string("2002-01-20")); | |
133 | check("time part of " + ts1, | |
134 | tod_1 == std::string("23:59:59.000")); | |
135 | // std::cout << date_1 << "|" << std::endl; | |
136 | // std::cout << tod_1 << "|" << std::endl; | |
137 | ||
138 | ||
7c673cae | 139 | { |
7c673cae | 140 | ptime t1 = time_from_string(ts1); |
11fdf7f2 TL |
141 | check("parse time: " + ts1, |
142 | t1 == ptime(date(2002,1,20),time_duration(23,59,59))); | |
143 | } | |
144 | { | |
145 | std::string ts1x("2002-01-20 23:59:59."); | |
146 | ptime t1 = time_from_string(ts1x); | |
147 | check("parse time (decimal but no digits): " + ts1x, | |
7c673cae FG |
148 | t1 == ptime(date(2002,1,20),time_duration(23,59,59))); |
149 | } | |
150 | ||
151 | std::string s6("235859"); | |
152 | time_duration td6= boost::date_time::parse_undelimited_time_duration<time_duration>(s6); | |
153 | check("parse time duration: " + s6, | |
154 | td6 == time_duration(23,58,59)); | |
155 | ||
156 | s6 = "-235859"; | |
157 | td6= boost::date_time::parse_undelimited_time_duration<time_duration>(s6); | |
158 | check("parse negative time duration: " + s6, | |
159 | td6 == time_duration(-23,58,59)); | |
160 | ||
11fdf7f2 TL |
161 | { |
162 | std::string ts3("20020120T235859"); | |
163 | ptime t20 = from_iso_string(ts3); | |
164 | check("parse iso time: " + ts3, | |
165 | t20 == ptime(date(2002,1,20),time_duration(23,58,59))); | |
166 | } | |
7c673cae | 167 | |
11fdf7f2 TL |
168 | { |
169 | std::string ts4("19001231T000000"); | |
170 | ptime t21 = from_iso_string(ts4); | |
171 | check("parse iso time: " + ts4, | |
172 | t21 == ptime(date(1900,12,31),time_duration(0,0,0))); | |
173 | } | |
174 | ||
175 | { | |
176 | std::string ts5("19001231T23"); | |
177 | ptime t22 = from_iso_string(ts5); | |
178 | check("parse iso time: " + ts5, | |
179 | t22 == ptime(date(1900,12,31),time_duration(23,0,0))); | |
180 | } | |
7c673cae | 181 | |
7c673cae FG |
182 | { |
183 | #if defined(BOOST_DATE_TIME_POSIX_TIME_STD_CONFIG) | |
184 | std::string ts3("20020120T235859.123456789"); | |
185 | ptime t20 = from_iso_string(ts3); | |
186 | check("parse iso time w/ frac sec: " + ts3, | |
187 | t20 == ptime(date(2002,1,20),time_duration(23,58,59,123456789))); | |
188 | ||
189 | std::string ts4("19001231T000000.123"); | |
190 | ptime t21 = from_iso_string(ts4); | |
191 | check("parse iso time w/ frac sec (too short): " + ts4, | |
192 | t21 == ptime(date(1900,12,31),time_duration(0,0,0,123000000))); | |
193 | ||
194 | std::string ts5("19001231T230000.123456789876"); | |
195 | ptime t22 = from_iso_string(ts5); | |
196 | check("parse iso time w/ frac sec (too long): " + ts5, | |
197 | t22 == ptime(date(1900,12,31),time_duration(23,0,0,123456789))); | |
198 | ||
199 | std::string ts6("19001231T230000."); | |
200 | ptime t23 = from_iso_string(ts6); | |
201 | check("parse iso time w/ frac sec (dec only): " + ts6, | |
202 | t23 == ptime(date(1900,12,31),time_duration(23,0,0,0))); | |
203 | ||
204 | std::string ts7("2002-01-20T23:58:59.123456789"); | |
205 | ptime t24 = from_iso_extended_string(ts7); | |
206 | check("parse iso extended time w/ frac sec: " + ts7, | |
207 | t24 == ptime(date(2002,1,20),time_duration(23,58,59,123456789))); | |
208 | ||
209 | std::string ts8("1900-12-31T00:00:00.123"); | |
210 | ptime t25 = from_iso_extended_string(ts8); | |
211 | check("parse iso extended time w/ frac sec (too short): " + ts8, | |
212 | t25 == ptime(date(1900,12,31),time_duration(0,0,0,123000000))); | |
213 | ||
214 | std::string ts9("1900-12-31T23:00:00.123456789876"); | |
215 | ptime t26 = from_iso_extended_string(ts9); | |
216 | check("parse iso extended time w/ frac sec (too long): " + ts9, | |
217 | t26 == ptime(date(1900,12,31),time_duration(23,0,0,123456789))); | |
218 | ||
219 | std::string ts10("1900-12-31T23:00:00."); | |
220 | ptime t27 = from_iso_extended_string(ts10); | |
221 | check("parse iso extended time w/ frac sec (dec only): " + ts10, | |
222 | t27 == ptime(date(1900,12,31),time_duration(23,0,0,0))); | |
1e59de90 TL |
223 | |
224 | ||
7c673cae FG |
225 | #else |
226 | std::string ts3("20020120T235859.123456"); | |
227 | ptime t20 = from_iso_string(ts3); | |
228 | check("parse iso time w/ frac sec: " + ts3, | |
229 | t20 == ptime(date(2002,1,20),time_duration(23,58,59,123456))); | |
230 | ||
231 | std::string ts4("19001231T000000.123"); | |
232 | ptime t21 = from_iso_string(ts4); | |
233 | check("parse iso time w/ frac sec (too short): " + ts4, | |
234 | t21 == ptime(date(1900,12,31),time_duration(0,0,0,123000))); | |
235 | ||
236 | std::string ts5("19001231T230000.123456789876"); | |
237 | ptime t22 = from_iso_string(ts5); | |
238 | check("parse iso time w/ frac sec (too long): " + ts5, | |
239 | t22 == ptime(date(1900,12,31),time_duration(23,0,0,123456))); | |
240 | ||
241 | std::string ts6("19001231T230000."); | |
242 | ptime t23 = from_iso_string(ts6); | |
243 | check("parse iso time w/ frac sec (dec only): " + ts6, | |
244 | t23 == ptime(date(1900,12,31),time_duration(23,0,0,0))); | |
245 | ||
246 | std::string ts7("2002-01-20T23:58:59.123456"); | |
247 | ptime t24 = from_iso_extended_string(ts7); | |
248 | check("parse iso extended time w/ frac sec: " + ts7, | |
249 | t24 == ptime(date(2002,1,20),time_duration(23,58,59,123456))); | |
250 | ||
251 | std::string ts8("1900-12-31T00:00:00.123"); | |
252 | ptime t25 = from_iso_extended_string(ts8); | |
253 | check("parse iso extended time w/ frac sec (too short): " + ts8, | |
254 | t25 == ptime(date(1900,12,31),time_duration(0,0,0,123000))); | |
255 | ||
256 | std::string ts9("1900-12-31T23:00:00.123456789876"); | |
257 | ptime t26 = from_iso_extended_string(ts9); | |
258 | check("parse iso extended time w/ frac sec (too long): " + ts9, | |
259 | t26 == ptime(date(1900,12,31),time_duration(23,0,0,123456))); | |
260 | ||
261 | std::string ts10("1900-12-31T23:00:00."); | |
262 | ptime t27 = from_iso_extended_string(ts10); | |
263 | check("parse iso extended time w/ frac sec (dec only): " + ts10, | |
264 | t27 == ptime(date(1900,12,31),time_duration(23,0,0,0))); | |
1e59de90 TL |
265 | |
266 | //the following test cases are for this issue: | |
267 | //https://github.com/boostorg/date_time/issues/187 | |
268 | try { | |
269 | std::string ts11("1900-12-31"); //date only string | |
270 | ptime t28 = from_iso_extended_string(ts11); | |
271 | check("parse iso extended time date only): " + ts11, | |
272 | t28 == ptime(date(1900,12,31),time_duration(0,0,0,0))); | |
273 | } | |
274 | catch( std::exception& e ) | |
275 | { | |
276 | std::cout << e.what() << std::endl; | |
277 | check("parse iso extended time date only): 1900-12-32", false); | |
278 | } | |
279 | ||
280 | try { | |
281 | //expect a date-time exception here | |
282 | std::string ts12("1900-1"); //completely bogus string | |
283 | ptime t29 = from_iso_extended_string(ts12); | |
284 | check("parse iso extended time bad string: " + ts12, false); | |
285 | (void) t29; //supress unused compiler warning, this is never reached | |
286 | } | |
287 | catch( std::exception& e ) | |
288 | { | |
289 | //Day of month value is out of range 1..31 | |
290 | // std::cout << e.what() << std::endl; | |
291 | check("parse iso extended time date only): 1900-1", true); | |
292 | } | |
293 | ||
7c673cae FG |
294 | #endif // BOOST_DATE_TIME_POSIX_TIME_STD_CONFIG |
295 | } | |
296 | ||
297 | std::string s7("-01:25:00"), s8("-00:40:00"), s9("0:45"), s10("0:-40"); | |
298 | time_duration tds1 = duration_from_string(s7); | |
299 | time_duration tds2 = duration_from_string(s8); | |
300 | time_duration tds3 = duration_from_string(s9); | |
301 | time_duration tds4 = duration_from_string(s10); | |
302 | check("from string construct", tds1 == time_duration(-1,25,0)); | |
303 | check("from string construct", tds2 == minutes(-40)); | |
304 | check("from string construct", tds3 == minutes(45)); | |
305 | // '-' in middle of string s10 should be ignored resulting in pos duration | |
306 | check("from string construct", tds4 == minutes(40)); | |
307 | ||
308 | return printTestStats(); | |
309 | ||
310 | } | |
311 |