1 /* Copyright (c) 2005 CrystalClear Software, Inc.
2 * Subject to the Boost Software License, Version 1.0.
3 * (See accompanying file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
4 * Author: Jeff Garland, Bart Garst
8 #include "boost/date_time/local_time/local_time.hpp"
9 #include "../testfrmwk.hpp"
14 // for tests that are expected to fail and throw exceptions
15 template<class temporal_type
, class exception_type
>
16 bool failure_test(temporal_type component
,
17 const std::string
& input
,
18 exception_type
const& /*except*/,
19 boost::local_time::local_time_input_facet
* facet
)
21 using namespace boost::local_time
;
23 std::istringstream
iss(input
);
24 iss
.exceptions(std::ios_base::failbit
); // turn on exceptions
25 iss
.imbue(std::locale(std::locale::classic(), facet
));
29 catch(exception_type
& e
) {
30 std::cout
<< "Expected exception caught: \""
31 << e
.what() << "\"" << std::endl
;
32 result
= iss
.fail(); // failbit must be set to pass test
41 // for tests that are expected to fail quietly
42 template<class temporal_type
>
43 bool failure_test(temporal_type component
,
44 const std::string
& input
,
45 boost::local_time::local_time_input_facet
* facet
)
47 using namespace boost::local_time
;
48 std::istringstream
iss(input
);
49 /* leave exceptions turned off
50 * iss.exceptions(std::ios_base::failbit); */
51 iss
.imbue(std::locale(std::locale::classic(), facet
));
56 std::cout
<< "Caught unexpected exception" << std::endl
;
60 return iss
.fail(); // failbit must be set to pass test
64 using namespace boost::gregorian
;
65 using namespace boost::posix_time
;
66 using namespace boost::local_time
;
67 time_zone_ptr null_zone
;
68 local_date_time
ldt1(not_a_date_time
, null_zone
);
70 // verify wide stream works, thorough tests done in narrow stream
71 #if !defined(BOOST_NO_STD_WSTRING)
73 std::wstringstream ws
;
74 ws
.str(L
"2005-Feb-15 12:15:00 EST-05EDT,M4.1.0,M10.5.0");
76 check("Wide stream, Eastern US, daylight savings, winter, minimal input",
77 ldt1
.local_time() == ptime(date(2005,2,15), time_duration(12,15,0)));
78 check("Wide stream, Eastern US, daylight savings, winter, minimal input",
79 ldt1
.utc_time() == ptime(date(2005,2,15), time_duration(17,15,0)));
80 check("Wide stream, Eastern US, daylight savings, winter, minimal input", !ldt1
.is_dst());
82 wlocal_time_input_facet
* wfacet
= new wlocal_time_input_facet(L
"%m/%d/%y %ZP");
83 std::locale
loc(std::locale::classic(), wfacet
);
85 ws
.str(L
"10/31/04 PST-08PDT,M4.1.0,M10.5.0"); // midnight on end transition day, still in dst
87 std::wcout
<< ldt1
.local_time() << std::endl
;
88 check("Wide stream, Eastern US, daylight savings, winter, custom format",
89 ldt1
.local_time() == ptime(date(2004,10,31), time_duration(0,0,0)));
90 check("Wide stream, Eastern US, daylight savings, winter, custom format",
91 ldt1
.utc_time() == ptime(date(2004,10,31), time_duration(7,0,0)));
92 check("Wide stream, Eastern US, daylight savings, winter, custom format", ldt1
.is_dst());
94 #endif // BOOST_NO_STD_WSTRING
97 ss
.str("2005-Feb-25 12:15:00 EST-05EDT,M4.1.0,M10.5.0");
99 check("Eastern US, daylight savings, winter, minimal input",
100 ldt1
.local_time() == ptime(date(2005,2,25), time_duration(12,15,0)));
101 check("Eastern US, daylight savings, winter, minimal input",
102 ldt1
.utc_time() == ptime(date(2005,2,25), time_duration(17,15,0)));
103 check("Eastern US, daylight savings, winter, minimal input", !ldt1
.is_dst());
106 ss
.str("2005-Aug-25 12:15:00 EST-05EDT,M4.1.0,M10.5.0");
108 check("Eastern US, daylight savings, summer, minimal input",
109 ldt1
.local_time() == ptime(date(2005,8,25), time_duration(12,15,0)));
110 check("Eastern US, daylight savings, summer, minimal input",
111 ldt1
.utc_time() == ptime(date(2005,8,25), time_duration(16,15,0)));
112 check("Eastern US, daylight savings, summer, minimal input", ldt1
.is_dst());
115 ss
.str("2005-Apr-03 01:15:00 EST-05EDT,M4.1.0,M10.5.0");
117 check("Eastern US, daylight savings, transition point", !ldt1
.is_dst());
119 check("Eastern US, daylight savings, transition point", ldt1
.is_dst());
121 ss
.str("2005-Apr-03 01:15:00 EST-05EDT,93,303");
123 check("Eastern US, daylight savings, transition point", !ldt1
.is_dst());
125 check("Eastern US, daylight savings, transition point", ldt1
.is_dst());
128 ss
.str("2005-Oct-30 00:15:00 EST-05EDT,M4.1.0,M10.5.0");
130 check("Eastern US, daylight savings, transition point", ldt1
.is_dst());
132 check("Eastern US, daylight savings, transition point", ldt1
.is_dst());
134 check("Eastern US, daylight savings, transition point", !ldt1
.is_dst());
136 ss
.str("2005-Oct-30 00:15:00 EST-05EDT,93,303");
138 check("Eastern US, daylight savings, transition point", ldt1
.is_dst());
140 check("Eastern US, daylight savings, transition point", ldt1
.is_dst());
142 check("Eastern US, daylight savings, transition point", !ldt1
.is_dst());
145 ss
.str("2005-Aug-25 12:15:00 MST-07");
147 check("Mountain US, no daylight savings",
148 ldt1
.local_time() == ptime(date(2005,8,25), time_duration(12,15,0)));
149 check("Mountain US, no daylight savings",
150 ldt1
.utc_time() == ptime(date(2005,8,25), time_duration(19,15,0)));
151 check("Mountain US, no daylight savings", !ldt1
.is_dst());
154 // insure input & output formats match
155 local_time_facet
* out_facet
=
156 new local_time_facet(local_time_input_facet::default_time_input_format
);
157 std::locale
loc(std::locale::classic(), out_facet
);
159 time_zone_ptr
syd_tz(new posix_time_zone("EST+10EST,M10.5.0,M3.5.0/03:00"));
160 ptime
pt(date(2005,6,12), hours(0));
161 local_date_time
ldt2(pt
, syd_tz
);
164 check("Output as input makes match", ldt1
== ldt2
);
165 check("Output as input makes match",
166 ldt1
.zone()->dst_local_start_time(2004) == ldt2
.zone()->dst_local_start_time(2004));
169 time_zone_ptr
f_tz(new posix_time_zone("FST+03FDT,90,300"));
170 ldt2
= local_date_time(ptime(date(2005,6,12), hours(0)), f_tz
);
173 check("Output as input makes match", ldt1
== ldt2
);
174 check("Output as input makes match",
175 ldt1
.zone()->dst_local_start_time(2004) == ldt2
.zone()->dst_local_start_time(2004));
178 // missing input & wrong format tests
179 ss
.str("2005-Oct-30 00:15:00");
181 check("Missing time_zone spec makes UTC", ldt1
.zone_as_posix_string() == std::string("UTC+00"));
182 check("Missing time_zone spec makes UTC", ldt1
.utc_time() == ldt1
.local_time());
185 std::istringstream
iss("2005-Aug-25 12:15:00 MST-07");
186 local_time_input_facet
* f
= new local_time_input_facet("%Y-%b-%d %H:%M:%S %z");
187 std::locale
locx(std::locale::classic(), f
);
190 check("Wrong format flag makes UTC", ldt1
.zone_as_posix_string() == std::string("UTC+00"));
191 check("Wrong format flag makes UTC", ldt1
.utc_time() == ldt1
.local_time());
195 // failure tests: (posix_time_zone) bad_offset, bad_adjustment,
196 // (local_date_time) ambiguous_result, time_label_invalid,
197 // time/date failures already tested
198 ambiguous_result
amb_ex("default");
199 time_label_invalid
inv_ex("default");
200 check("Failure test ambiguous time label (w/exceptions)",
202 "2005-Oct-30 01:15:00 EST-05EDT,M4.1.0,M10.5.0",
204 new local_time_input_facet()));
205 check("Failure test ambiguous time label (no exceptions)",
207 "2005-Oct-30 01:15:00 EST-05EDT,M4.1.0,M10.5.0",
208 new local_time_input_facet()));
209 check("Failure test ambiguous time label (w/exceptions)",
211 "2005-Oct-30 01:15:00 EST-05EDT,93,303",
213 new local_time_input_facet()));
214 check("Failure test ambiguous time label (no exceptions)",
216 "2005-Oct-30 01:15:00 EST-05EDT,93,303",
217 new local_time_input_facet()));
218 check("Failure test invalid time label (w/exceptions)",
220 "2005-Apr-03 02:15:00 EST-05EDT,M4.1.0,M10.5.0",
222 new local_time_input_facet()));
223 check("Failure test invalid time label (no exceptions)",
225 "2005-Apr-03 02:15:00 EST-05EDT,M4.1.0,M10.5.0",
226 new local_time_input_facet()));
227 check("Failure test invalid time label (w/exceptions)",
229 "2005-Apr-03 02:15:00 EST-05EDT,93,303",
231 new local_time_input_facet()));
232 check("Failure test invalid time label (no exceptions)",
234 "2005-Apr-03 02:15:00 EST-05EDT,93,303",
235 new local_time_input_facet()));
238 return printTestStats();