1
2 /* Copyright (c) 2004 CrystalClear Software, Inc.
3 * Use, modification and distribution is subject to the
4 * Boost Software License, Version 1.0. (See accompanying
5 * file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
6 * Author: Jeff Garland, Bart Garst
7 * $Date$
8 */
9
10 #include "boost/date_time/posix_time/posix_time.hpp"
11 #include "../testfrmwk.hpp"
12 #include <fstream>
13 #include <iostream>
14 #include <sstream>
15
16 template<class temporal_type, typename charT>
17 inline
18 void
teststreaming(std::string testname,temporal_type value,std::basic_string<charT> expected_result,const std::locale & locale=std::locale::classic ())19 teststreaming(std::string testname,
20 temporal_type value,
21 std::basic_string<charT> expected_result,
22 const std::locale& locale = std::locale::classic())
23 {
24 std::basic_stringstream<charT> ss;
25 ss.imbue(locale);
26 ss << value;
27 check_equal(testname, ss.str(), expected_result);
28 }
29
30
31 #if !defined(BOOST_NO_STD_WSTRING)
32 static const wchar_t* long_month_names[] =
33 {L"Januar",L"Februar",L"Marz",L"April",L"Mai",L"Juni",L"Juli",L"August",
34 L"September",L"Oktober",L"November",L"Dezember"};
35 static const wchar_t* short_month_names[]=
36 {L"Jan",L"Feb",L"Mar",L"Apr",L"Mai",L"Jun",L"Jul",L"Aug",
37 L"Sep",L"Okt",L"Nov",L"Dez"};
38
39 std::vector<std::basic_string<wchar_t> > de_short_month_names;
40 std::vector<std::basic_string<wchar_t> > de_long_month_names;
41 #endif //
42
main()43 int main() {
44 using namespace boost::gregorian;
45 using namespace boost::posix_time;
46
47 try {
48 date d(2004,Oct,13);
49 date min_date(min_date_time);
50 date max_date(max_date_time);
51
52 date_period dp(d, d + date_duration(7));
53 ptime t(d, time_duration(18,01,56));
54 ptime tf = t + microseconds(3);
55 time_period tp(t, tf + days(7) + time_duration(1,1,1));
56 time_duration td = hours(3) + minutes(2) + seconds(1) + milliseconds(9);
57 time_duration longer_td = hours(10) + minutes(22) + seconds(15) + milliseconds(980); // two characters in hours
58 time_duration long_td = hours(300) + minutes(2) + seconds(1) + milliseconds(9); // more than two characters in hours
59 {
60 std::stringstream ss;
61 ss << t;
62 check("Stream and to_string formats match (ptime)",
63 to_simple_string(t) == ss.str());
64 std::cout << t << ' ' << td << std::endl;
65 ss.str("");
66 ss << tf;
67 check("Stream and to_string formats match (ptime w/ fracsec)",
68 to_simple_string(tf) == ss.str());
69 ss.str("");
70 ss << tp;
71 check("Stream and to_string formats match (time_period)",
72 to_simple_string(tp) == ss.str());
73 ss.str("");
74 ss << td;
75 check("Stream and to_string formats match (time_duration)",
76 to_simple_string(td) == ss.str());
77 std::cout << ss.str() << std::endl;
78
79 time_facet* f = new time_facet();
80 ss.imbue(std::locale(ss.getloc(), f));
81 ss.str("");
82
83 f->format("%Y-%b-%d %H:%M:%S %%d");
84 f->time_duration_format("%H:%M:%S %%S");
85 ss << t;
86 check("Literal '%' in format", ss.str() == std::string("2004-Oct-13 18:01:56 %d"));
87 ss.str("");
88 ss << td;
89 check("Literal '%' in time_duration format", ss.str() == std::string("03:02:01 %S"));
90 ss.str("");
91 f->format("%Y-%b-%d %H:%M:%S %%%d");
92 f->time_duration_format("%H:%M:%S %%%S");
93 ss << t;
94 check("Multiple literal '%'s in format", ss.str() == std::string("2004-Oct-13 18:01:56 %13"));
95 ss.str("");
96 ss << td;
97 check("Multiple literal '%'s in time_duration format", ss.str() == std::string("03:02:01 %01"));
98 ss.str("");
99
100 // Longer time durations
101 f->time_duration_format("%H:%M:%S");
102 ss << longer_td;
103 check("Longer time durations", ss.str() == std::string("10:22:15"));
104 ss.str("");
105
106 // Long time durations
107 f->time_duration_format("%O:%M:%S");
108 ss << long_td;
109 check("Long time durations", ss.str() == std::string("300:02:01"));
110 ss.str("");
111
112 // Short-hand format specifiers
113 f->format("%T");
114 f->time_duration_format("%T");
115 ss << t;
116 check("Short-hand '%T' in time format", ss.str() == std::string("18:01:56"));
117 ss.str("");
118 ss << td;
119 check("Short-hand '%T' in time_duration format", ss.str() == std::string("03:02:01"));
120 ss.str("");
121
122 f->format("%R");
123 f->time_duration_format("%R");
124 ss << t;
125 check("Short-hand '%R' in time format", ss.str() == std::string("18:01"));
126 ss.str("");
127 ss << td;
128 check("Short-hand '%R' in time_duration format", ss.str() == std::string("03:02"));
129 ss.str("");
130 }
131 { // negative time_duration tests
132 std::string result;
133 std::stringstream ss;
134 time_duration td1(2,0,0);
135 time_duration td2(1,0,0);
136 ss << td2 - td1;
137 result = "-01:00:00";
138 check("Negative time_duration", result == ss.str());
139 ss.str("");
140
141 time_duration td3(0,2,0);
142 time_duration td4(0,1,0);
143 ss << td4 - td3;
144 result = "-00:01:00";
145 check("Negative time_duration", result == ss.str());
146 ss.str("");
147
148 time_duration td5(0,0,2);
149 time_duration td6(0,0,1);
150 ss << td6 - td5;
151 result = "-00:00:01";
152 check("Negative time_duration", result == ss.str());
153 ss.str("");
154
155 #if defined(BOOST_DATE_TIME_POSIX_TIME_STD_CONFIG)
156 result = "-00:00:00.000000001";
157 #else
158 result = "-00:00:00.000001";
159 #endif
160 time_duration td7(0,0,0,123);
161 time_duration td8(0,0,0,122);
162 ss << td8 - td7;
163 check("Negative time_duration: " + ss.str(), result == ss.str());
164
165 //reset the sign to always print
166 time_facet* f = new time_facet();
167 ss.imbue(std::locale(ss.getloc(), f));
168 f->time_duration_format("%+%H:%M:%S""%F");
169
170 ss.str("");
171 ss << td4 - td3;
172 result = "-00:01:00";
173 check("Negative time_duration sign always: " + ss.str(), result == ss.str());
174
175 ss.str("");
176 ss << td3 - td4;
177 result = "+00:01:00";
178 check("time_duration sign always: " + ss.str(), result == ss.str());
179
180
181 #if defined(BOOST_DATE_TIME_POSIX_TIME_STD_CONFIG)
182 result = "-00:00:00.000000001";
183 #else
184 result = "-00:00:00.000001";
185 #endif
186 ss.str("");
187 ss << td8 - td7;
188 check("Negative time_duration: " + ss.str(), result == ss.str());
189
190 #if defined(BOOST_DATE_TIME_POSIX_TIME_STD_CONFIG)
191 result = "+00:00:00.000000001";
192 #else
193 result = "+00:00:00.000001";
194 #endif
195 ss.str("");
196 ss << td7 - td8;
197 check("time_duration sign bit always: " + ss.str(), result == ss.str());
198
199 f->time_duration_format("%-%H hours and %-%M minutes");
200 ss.str("");
201 ss << td4 - td3;
202 result = "-00 hours and -01 minutes";
203 check("Negative time_duration two sign flags" + ss.str(), result == ss.str());
204 ss.str("");
205
206 // Longer time durations
207 f->time_duration_format("%-%H:%M:%S");
208 ss << -longer_td;
209 check("Longer negative time durations", ss.str() == std::string("-10:22:15"));
210 ss.str("");
211
212 // Long time durations
213 f->time_duration_format("%-%O:%M:%S");
214 ss << -long_td;
215 check("Long negative time durations", ss.str() == std::string("-300:02:01"));
216 ss.str("");
217 }
218
219 // The test verifies that #2698 is fixed. That is, the time and date facet should
220 // not dereference end() iterator for the format string in do_put_tm.
221 {
222 boost::gregorian::date date(2009, 1, 1);
223 boost::posix_time::time_duration tdx(0, 0, 0, 0);
224 boost::posix_time::ptime boost_time(date, tdx);
225 std::stringstream sstr;
226
227 boost::posix_time::time_facet* pFacet = new boost::posix_time::time_facet("");
228 sstr.imbue(std::locale(std::locale::classic(), pFacet));
229
230 sstr << boost_time;
231 }
232
233 #if !defined(BOOST_NO_STD_WSTRING)
234 std::copy(&short_month_names[0],
235 &short_month_names[12],
236 std::back_inserter(de_short_month_names));
237
238 std::copy(&long_month_names[0],
239 &long_month_names[12],
240 std::back_inserter(de_long_month_names));
241
242
243 {
244 wtime_facet *timefacet = new wtime_facet(wtime_facet::standard_format);
245 teststreaming("widestream default classic time", t,
246 //std::wstring(L"Wed Oct 13 18:01:56 2004"),
247 std::wstring(L"10/13/04 18:01:56"),
248 std::locale(std::locale::classic(), timefacet));
249 }
250 {
251 wtime_facet *timefacet = new wtime_facet(wtime_facet::standard_format);
252 teststreaming("widestream default classic time with fractional seconds truncated", t,
253 //std::wstring(L"Wed Oct 13 18:01:56 2004"),
254 std::wstring(L"10/13/04 18:01:56"),
255 std::locale(std::locale::classic(), timefacet));
256 }
257 {
258 wtime_facet *timefacet = new wtime_facet(wtime_facet::standard_format);
259 teststreaming("widestream default time period with fractional seconds truncated", tp,
260 //std::wstring(L"[Wed Oct 13 18:01:56 2004/Wed Oct 20 19:02:57 2004]"),
261 std::wstring(L"[10/13/04 18:01:56/10/20/04 19:02:57]"),
262 std::locale(std::locale::classic(), timefacet));
263 }
264 {
265 wtime_facet *timefacet = new wtime_facet(L"%Y-%b-%d %I:%M:%S %p");
266 teststreaming("widestream time in 12 hours format w/ (AM/PM)", tp.begin(),
267 std::wstring(L"2004-Oct-13 06:01:56 PM"),
268 std::locale(std::locale::classic(), timefacet));
269 }
270 {
271 wtime_facet *timefacet = new wtime_facet(wtime_facet::standard_format);
272 #ifdef BOOST_DATE_TIME_HAS_NANOSECONDS
273 teststreaming("widestream time duration", td,
274 std::wstring(L"03:02:01.009000000"),
275 std::locale(std::locale::classic(), timefacet));
276 #else
277 teststreaming("widestream time duration", td,
278 std::wstring(L"03:02:01.009000"),
279 std::locale(std::locale::classic(), timefacet));
280 #endif // BOOST_DATE_TIME_HAS_NANOSECONDS
281 }
282
283 #ifdef BOOST_DATE_TIME_HAS_NANOSECONDS
284 teststreaming("widestream time duration", td,
285 std::wstring(L"03:02:01.009000000"));
286 #else
287 teststreaming("widestream time duration", td,
288 std::wstring(L"03:02:01.009000"));
289 #endif // BOOST_DATE_TIME_HAS_NANOSECONDS
290
291 //wtime_facet *timefacet = new wtime_facet();
292 //std::locale cloc = std::locale(std::locale::classic(), timefacet);
293 //ss.imbue(cloc);
294 // ss << L"classic date: " << d << std::endl;
295 // ss << L"classic dateperiod: " << dp << std::endl;
296 //ss << L"classic time: " << t << std::endl;
297 //ss << L"classic timefrac: " << tf << std::endl;
298 //ss << L"classic timeperiod: " << tp << std::endl;
299
300 {
301 wtime_facet* wtimefacet = new wtime_facet(L"day: %j date: %Y-%b-%d weekday: %A time: %H:%M:%s");
302 #ifdef BOOST_DATE_TIME_HAS_NANOSECONDS
303 teststreaming("widestream custom time facet narly format", t,
304 std::wstring(L"day: 287 date: 2004-Oct-13 weekday: Wednesday time: 18:01:56.000000000"),
305 std::locale(std::locale::classic(), wtimefacet));
306 #else
307 teststreaming("widestream custom time facet narly format", t,
308 std::wstring(L"day: 287 date: 2004-Oct-13 weekday: Wednesday time: 18:01:56.000000"),
309 std::locale(std::locale::classic(), wtimefacet));
310 #endif
311 }
312 {
313 wtime_facet* wtimefacet = new wtime_facet(L"%Y-%b-%d %H:%M:%S,%f");
314 #ifdef BOOST_DATE_TIME_HAS_NANOSECONDS
315 teststreaming("widestream custom time fractional seconds: %Y-%b-%d %H:%M:%S,%f", t,
316 std::wstring(L"2004-Oct-13 18:01:56,000000000"),
317 std::locale(std::locale::classic(), wtimefacet));
318 #else
319 teststreaming("widestream custom time fractional seconds: %Y-%b-%d %H:%M:%S,%f", t,
320 std::wstring(L"2004-Oct-13 18:01:56,000000"),
321 std::locale(std::locale::classic(), wtimefacet));
322 #endif
323 }
324
325 {
326 wtime_facet* wtimefacet = new wtime_facet(L"%Y-%b-%d %H:%M:%S");
327 teststreaming("widestream custom time no frac seconds: %Y-%b-%d %H:%M:%S", t,
328 std::wstring(L"2004-Oct-13 18:01:56"),
329 std::locale(std::locale::classic(), wtimefacet));
330 }
331
332 {
333 wtime_facet* wtimefacet = new wtime_facet(L"%Y-%b-%d %H:%M:%S");
334 wtimefacet->short_month_names(de_short_month_names);
335 teststreaming("widestream custom time no frac seconds, german months: %Y-%b-%d %H:%M:%S", t,
336 std::wstring(L"2004-Okt-13 18:01:56"),
337 std::locale(std::locale::classic(), wtimefacet));
338 }
339
340 {
341 wtime_facet* wtimefacet = new wtime_facet();
342 wtimefacet->format(L"%B %b %Y");
343 wtimefacet->short_month_names(de_short_month_names);
344 wtimefacet->long_month_names(de_long_month_names);
345 teststreaming("widestream custom time no frac seconds, german months: %B %b %Y", t,
346 std::wstring(L"Oktober Okt 2004"),
347 std::locale(std::locale::classic(), wtimefacet));
348 }
349
350 {
351 wtime_facet* wtimefacet = new wtime_facet(L"%Y-%b-%d %H:%M:%S" L"%F");
352 teststreaming("widestream custom time no frac seconds %F operator: %Y-%b-%d %H:%M:%S""%F", t,
353 std::wstring(L"2004-Oct-13 18:01:56"),
354 std::locale(std::locale::classic(), wtimefacet));
355 }
356
357 {
358 wtime_facet* wtimefacet = new wtime_facet(L"%Y-%b-%d %H:%M:%S" L"%F");
359 #ifdef BOOST_DATE_TIME_HAS_NANOSECONDS
360 teststreaming("widestream custom time with frac seconds %F operator: %Y-%b-%d %H:%M:%S""%F", tf,
361 std::wstring(L"2004-Oct-13 18:01:56.000003000"),
362 std::locale(std::locale::classic(), wtimefacet));
363 #else
364 teststreaming("widestream custom time with frac seconds %F operator: %Y-%b-%d %H:%M:%S""%F", tf,
365 std::wstring(L"2004-Oct-13 18:01:56.000003"),
366 std::locale(std::locale::classic(), wtimefacet));
367 #endif // BOOST_DATE_TIME_HAS_NANOSECONDS
368 }
369 {
370 wtime_facet* wtimefacet = new wtime_facet();
371 wtimefacet->set_iso_format();
372 #ifdef BOOST_DATE_TIME_HAS_NANOSECONDS
373 teststreaming("widestream custom time iso format", tf,
374 std::wstring(L"20041013T180156.000003000"),
375 std::locale(std::locale::classic(), wtimefacet));
376 #else
377 teststreaming("widestream custom time iso format", tf,
378 std::wstring(L"20041013T180156.000003"),
379 std::locale(std::locale::classic(), wtimefacet));
380 #endif // BOOST_DATE_TIME_HAS_NANOSECONDS
381 }
382 {
383 wtime_facet* wtimefacet = new wtime_facet();
384 wtimefacet->set_iso_extended_format();
385 #ifdef BOOST_DATE_TIME_HAS_NANOSECONDS
386 teststreaming("widestream custom time iso extended format", tf,
387 std::wstring(L"2004-10-13 18:01:56.000003000"),
388 std::locale(std::locale::classic(), wtimefacet));
389 #else
390 teststreaming("widestream custom time iso extended format", tf,
391 std::wstring(L"2004-10-13 18:01:56.000003"),
392 std::locale(std::locale::classic(), wtimefacet));
393 #endif // BOOST_DATE_TIME_HAS_NANOSECONDS
394 }
395
396
397 {
398 wtime_facet* wtimefacet = new wtime_facet(L"%Y-%b-%d %H:%M:%S" L"%F");
399 #ifdef BOOST_DATE_TIME_HAS_NANOSECONDS
400 teststreaming("widestream time period frac seconds %F operator: %Y-%b-%d %H:%M:%S""%F", tp,
401 std::wstring(L"[2004-Oct-13 18:01:56/2004-Oct-20 19:02:57.000002999]"),
402 std::locale(std::locale::classic(), wtimefacet));
403 #else
404 teststreaming("widestream time period frac seconds %F operator: %Y-%b-%d %H:%M:%S""%F", tp,
405 std::wstring(L"[2004-Oct-13 18:01:56/2004-Oct-20 19:02:57.000002]"),
406 std::locale(std::locale::classic(), wtimefacet));
407 #endif // BOOST_DATE_TIME_HAS_NANOSECONDS
408 }
409
410 {
411 wtime_facet* wtimefacet = new wtime_facet(L"%Y-%b-%d %H:%M:%s");
412 wperiod_formatter pf(wperiod_formatter::AS_OPEN_RANGE, L" / ", L"[ ", L" )", L" ]");
413 wtimefacet->period_formatter(pf);
414
415 #ifdef BOOST_DATE_TIME_HAS_NANOSECONDS
416 teststreaming("widestream custom time : %Y-%b-%d %H:%M:%s", tp,
417 std::wstring(L"[ 2004-Oct-13 18:01:56.000000000 / 2004-Oct-20 19:02:57.000003000 )"),
418 std::locale(std::locale::classic(), wtimefacet));
419 #else
420 teststreaming("widestream custom time : %Y-%b-%d %H:%M:%s", tp,
421 std::wstring(L"[ 2004-Oct-13 18:01:56.000000 / 2004-Oct-20 19:02:57.000003 )"),
422 std::locale(std::locale::classic(), wtimefacet));
423 #endif // BOOST_DATE_TIME_HAS_NANOSECONDS
424 }
425
426
427 {
428 ptime nt(not_a_date_time);
429 teststreaming("widestream custom time : not a datetime", nt,
430 std::wstring(L"not-a-date-time"));
431 }
432
433
434
435
436 // //Denmark English has iso extended default format...
437 // std::locale gloc("en_DK");
438 // ss.imbue(gloc);
439 // ss << L"default english-denmark date: " << d << std::endl;
440 // ss << L"default english-denmark dateperiod: " << dp << std::endl;
441 // ss << L"default english-denmark time: " << t << std::endl;
442 // ss << L"default english-denmark timefrac: " << tf << std::endl;
443 // ss << L"default english-denmark timeperiod: " << tp << std::endl;
444
445
446
447 #endif
448 }
449 catch(std::exception& e) {
450 std::cout << "Caught std::exception: " << e.what() << std::endl;
451 }
452 catch(...) {
453 std::cout << "bad exception" << std::endl;
454 }
455
456 return printTestStats();
457 }
458
459