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
12 #include <boost/shared_ptr.hpp>
13 #include "boost/date_time/local_time/local_time.hpp"
14 #include "../testfrmwk.hpp"
17 template<class temporal_type
, typename charT
>
20 teststreaming(std::string
const& testname
,
22 std::basic_string
<charT
> const& expected_result
,
23 const std::locale
& locale
)
25 std::basic_stringstream
<charT
> ss
;
29 if (!check(testname
, ss
.str() == expected_result
))
30 #if !defined(BOOST_NO_STD_WSTRING)
31 std::wcout
<< L
"Expected: \"" << expected_result
.c_str() << L
"\"\nGot: \"" << ss
.str().c_str() << L
"\"" << std::endl
;
33 std::cout
<< "Expected: \"" << expected_result
.c_str() << "\"\nGot: \"" << ss
.str().c_str() << L
"\"" << std::endl
;
38 /* use the tz_database for the time zones.
39 * Chicago, Denver, Los_Angeles, New_Tork, and Phoenix
40 * have all had full names added */
41 using namespace boost
;
42 using namespace boost::local_time
;
43 using namespace boost::posix_time
;
44 using namespace boost::gregorian
;
46 tz_database time_zones
;
49 // first try to find the data file from the test dir
50 time_zones
.load_from_file("../data/date_time_zonespec.csv");
52 catch(const data_not_accessible
&) {
53 // couldn't find the data file so assume we are being run from
54 // boost_root/status and try again
56 time_zones
.load_from_file("../libs/date_time/data/date_time_zonespec.csv");
58 catch(const data_not_accessible
&) {
59 check("Cannot locate data file - aborting.", false);
60 return printTestStats();
65 time_zone_ptr chicago
= time_zones
.time_zone_from_region("America/Chicago");
66 time_zone_ptr denver
= time_zones
.time_zone_from_region("America/Denver");
67 time_zone_ptr la
= time_zones
.time_zone_from_region("America/Los_Angeles");
68 time_zone_ptr nyc
= time_zones
.time_zone_from_region("America/New_York");
69 time_zone_ptr phx
= time_zones
.time_zone_from_region("America/Phoenix");
70 // sydney does not have full time zone names in the tz_database
71 time_zone_ptr sydney
= time_zones
.time_zone_from_region("Australia/Sydney");
73 ptime
a_time(date(2004,Dec
,15), hours(12));
74 ptime
b_time(date(2004,Aug
,15), hours(12));
75 local_date_time
ldt1(a_time
, utc
);
76 local_date_time
ldt2(b_time
, chicago
);
77 local_date_time
ldt3(a_time
, denver
);
78 local_date_time
ldt4(b_time
, la
);
79 local_date_time
ldt5(a_time
, nyc
);
80 local_date_time
ldt6(b_time
, phx
);
82 local_time_period
ltp1(ldt1
, hours(10) + minutes(24) + seconds(5));
83 local_time_period
ltp2(ldt4
, hours(15) + minutes(20) + seconds(41));
85 typedef boost::date_time::time_facet
<local_date_time
, char> ldt_facet
;
86 //ldt_facet* timefacet = new ldt_facet("%c %Z"); // full name
87 ldt_facet
* timefacet
= new ldt_facet("%a %b %d %H:%M:%S %Y %Z"); // full name
88 std::locale
loc1(std::locale::classic(), timefacet
);
90 typedef boost::date_time::time_facet
<ptime
, char> pt_facet
;
91 //pt_facet* ptimefacet1 = new pt_facet("%c %Z"); // show that zone is ignored
92 pt_facet
* ptimefacet1
= new pt_facet("%a %b %d %H:%M:%S %Y %Z"); // show that zone is ignored
93 std::locale
loc2(std::locale::classic(), ptimefacet1
);
96 std::cout
<< "\nFull time zone names tests" << std::endl
;
97 teststreaming("ptime with %Z flag\n", a_time
, std::string("Wed Dec 15 12:00:00 2004") , loc2
);
99 teststreaming("UTC local_date_time", ldt1
, std::string("Wed Dec 15 12:00:00 2004 Coordinated Universal Time"), loc1
);
100 teststreaming("Chicago in summer", ldt2
, std::string("Sun Aug 15 07:00:00 2004 Central Daylight Time") , loc1
);
101 teststreaming("Denver in winter", ldt3
, std::string("Wed Dec 15 05:00:00 2004 Mountain Standard Time"), loc1
);
102 teststreaming("Los Angeles in summer", ldt4
, std::string("Sun Aug 15 05:00:00 2004 Pacific Daylight Time"), loc1
);
103 teststreaming("New York in winter", ldt5
, std::string("Wed Dec 15 07:00:00 2004 Eastern Standard Time"), loc1
);
104 teststreaming("Phoenix in Summer", ldt6
, std::string("Sun Aug 15 05:00:00 2004 Mountain Standard Time"), loc1
);
106 teststreaming("UTC local_time_period", ltp1
, std::string("[Wed Dec 15 12:00:00 2004 Coordinated Universal Time/Wed Dec 15 22:24:04 2004 Coordinated Universal Time]"), loc1
);
107 teststreaming("LA local_time_period", ltp2
, std::string("[Sun Aug 15 05:00:00 2004 Pacific Daylight Time/Sun Aug 15 20:20:40 2004 Pacific Daylight Time]"), loc1
);
109 //ptimefacet1->format("%c %z"); // show that zone abbrev is ignored
110 ptimefacet1
->format("%a %b %d %H:%M:%S %Y %z"); // show that zone abbrev is ignored
111 std::cout
<< "\nTime zone abbreviation tests" << std::endl
;
112 teststreaming("ptime with %z flag\n", a_time
, std::string("Wed Dec 15 12:00:00 2004") , loc2
);
114 // using standard format
115 //timefacet->format("%c %z"); // abbreviated zone
116 timefacet
->format("%a %b %d %H:%M:%S %Y %z"); // abbreviated zone
117 teststreaming("UTC local_date_time", ldt1
, std::string("Wed Dec 15 12:00:00 2004 UTC"), loc1
);
118 teststreaming("Chicago in summer", ldt2
, std::string("Sun Aug 15 07:00:00 2004 CDT") , loc1
);
119 teststreaming("Denver in winter", ldt3
, std::string("Wed Dec 15 05:00:00 2004 MST"), loc1
);
120 teststreaming("Los Angeles in summer", ldt4
, std::string("Sun Aug 15 05:00:00 2004 PDT"), loc1
);
121 teststreaming("New York in winter", ldt5
, std::string("Wed Dec 15 07:00:00 2004 EST"), loc1
);
122 teststreaming("Phoenix in Summer", ldt6
, std::string("Sun Aug 15 05:00:00 2004 MST"), loc1
);
125 // show that zone offset is ignored
126 ptimefacet1
->format(pt_facet::iso_time_format_specifier
);
127 std::cout
<< "\nLocal time iso format tests" << std::endl
;
128 teststreaming("ptime with iso format\n", a_time
,
129 std::string("20041215T120000") , loc2
);
131 timefacet
->format(ldt_facet::iso_time_format_specifier
);
132 teststreaming("UTC local_date_time", ldt1
,
133 std::string("20041215T120000Z"), loc1
);
134 teststreaming("Chicago in summer", ldt2
,
135 std::string("20040815T070000-0500") , loc1
);
136 teststreaming("Denver in winter", ldt3
,
137 std::string("20041215T050000-0700"), loc1
);
138 teststreaming("Los Angeles in summer", ldt4
,
139 std::string("20040815T050000-0700"), loc1
);
140 teststreaming("New York in winter", ldt5
,
141 std::string("20041215T070000-0500"), loc1
);
142 teststreaming("Phoenix in Summer", ldt6
,
143 std::string("20040815T050000-0700"), loc1
);
144 teststreaming("Sydney in December", ldt1
.local_time_in(sydney
),
145 std::string("20041215T230000+1100"), loc1
);
147 // iso extended format
148 // show that zone offset is ignored
149 ptimefacet1
->format(pt_facet::iso_time_format_extended_specifier
);
150 std::cout
<< "\nLocal time iso_extended tests" << std::endl
;
151 teststreaming("ptime with iso extended format\n", a_time
,
152 std::string("2004-12-15 12:00:00") , loc2
);
154 timefacet
->format(ldt_facet::iso_time_format_extended_specifier
);
155 teststreaming("UTC local_date_time", ldt1
,
156 std::string("2004-12-15 12:00:00Z"), loc1
);
157 teststreaming("Chicago in summer", ldt2
,
158 std::string("2004-08-15 07:00:00-05:00") , loc1
);
159 teststreaming("Denver in winter", ldt3
,
160 std::string("2004-12-15 05:00:00-07:00"), loc1
);
161 teststreaming("Los Angeles in summer", ldt4
,
162 std::string("2004-08-15 05:00:00-07:00"), loc1
);
163 teststreaming("New York in winter", ldt5
,
164 std::string("2004-12-15 07:00:00-05:00"), loc1
);
165 teststreaming("Phoenix in Summer", ldt6
,
166 std::string("2004-08-15 05:00:00-07:00"), loc1
);
167 teststreaming("Sydney in December", ldt1
.local_time_in(sydney
),
168 std::string("2004-12-15 23:00:00+11:00"), loc1
);
170 #if !defined(BOOST_NO_STD_WSTRING)
172 typedef boost::date_time::time_facet
<local_date_time
, wchar_t> wldt_facet
;
173 //wldt_facet* wtimefacet = new wldt_facet(L"%c %Z"); // full name
174 wldt_facet
* wtimefacet
= new wldt_facet(L
"%a %b %d %H:%M:%S %Y %Z"); // full name
175 std::locale
loc3(std::locale::classic(), wtimefacet
);
177 /* Again, wide stream tests are more thoroughly done in the
178 * time_facet tests. Here we just need to show that they work */
179 std::cout
<< "\nFull time zone names tests - wide stream" << std::endl
;
180 teststreaming("UTC local_date_time", ldt1
, std::wstring(L
"Wed Dec 15 12:00:00 2004 Coordinated Universal Time"), loc3
);
181 teststreaming("Chicago in summer", ldt2
, std::wstring(L
"Sun Aug 15 07:00:00 2004 Central Daylight Time") , loc3
);
183 teststreaming("UTC local_time_period", ltp1
, std::wstring(L
"[Wed Dec 15 12:00:00 2004 Coordinated Universal Time/Wed Dec 15 22:24:04 2004 Coordinated Universal Time]"), loc3
);
184 teststreaming("LA local_time_period", ltp2
, std::wstring(L
"[Sun Aug 15 05:00:00 2004 Pacific Daylight Time/Sun Aug 15 20:20:40 2004 Pacific Daylight Time]"), loc3
);
186 //wtimefacet->format(L"%c %z"); // abbrev
187 wtimefacet
->format(L
"%a %b %d %H:%M:%S %Y %z"); // abbrev
188 std::cout
<< "\nAbbreviated time zone names tests - wide stream" << std::endl
;
189 teststreaming("UTC local_date_time", ldt1
, std::wstring(L
"Wed Dec 15 12:00:00 2004 UTC"), loc3
);
190 teststreaming("Phoenix in Summer", ldt6
, std::wstring(L
"Sun Aug 15 05:00:00 2004 MST"), loc3
);
192 #endif // BOOST_NO_STD_WSTRING
194 return printTestStats();