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 <iostream>
11 #include <sstream>
12 #include <boost/shared_ptr.hpp>
13 #include "boost/date_time/local_time/local_time.hpp"
14 #include "../testfrmwk.hpp"
15
16
17 template<class temporal_type, typename charT>
18 inline
19 void
teststreaming(std::string const & testname,temporal_type value,std::basic_string<charT> const & expected_result,const std::locale & locale)20 teststreaming(std::string const& testname,
21 temporal_type value,
22 std::basic_string<charT> const& expected_result,
23 const std::locale& locale)
24 {
25 std::basic_stringstream<charT> ss;
26 ss.imbue(locale);
27 ss << value;
28
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;
32 #else
33 std::cout << "Expected: \"" << expected_result.c_str() << "\"\nGot: \"" << ss.str().c_str() << L"\"" << std::endl;
34 #endif
35 }
36
main(int,char const * argv[])37 int main(int /* argc */, char const* argv[]){
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;
45
46 tz_database time_zones;
47
48 try {
49 // first try to find the data file from the test dir
50 time_zones.load_from_file(argv[1]);
51 }
52 catch(const data_not_accessible&) {
53 check("Cannot locate data file - aborting.", false);
54 return printTestStats();
55 }
56
57 time_zone_ptr utc;
58 time_zone_ptr chicago = time_zones.time_zone_from_region("America/Chicago");
59 time_zone_ptr denver = time_zones.time_zone_from_region("America/Denver");
60 time_zone_ptr la = time_zones.time_zone_from_region("America/Los_Angeles");
61 time_zone_ptr nyc = time_zones.time_zone_from_region("America/New_York");
62 time_zone_ptr phx = time_zones.time_zone_from_region("America/Phoenix");
63 // sydney does not have full time zone names in the tz_database
64 time_zone_ptr sydney = time_zones.time_zone_from_region("Australia/Sydney");
65
66 ptime a_time(date(2004,Dec,15), hours(12));
67 ptime b_time(date(2004,Aug,15), hours(12));
68 local_date_time ldt1(a_time, utc);
69 local_date_time ldt2(b_time, chicago);
70 local_date_time ldt3(a_time, denver);
71 local_date_time ldt4(b_time, la);
72 local_date_time ldt5(a_time, nyc);
73 local_date_time ldt6(b_time, phx);
74
75 local_time_period ltp1(ldt1, hours(10) + minutes(24) + seconds(5));
76 local_time_period ltp2(ldt4, hours(15) + minutes(20) + seconds(41));
77
78 typedef boost::date_time::time_facet<local_date_time, char> ldt_facet;
79 //ldt_facet* timefacet = new ldt_facet("%c %Z"); // full name
80 ldt_facet* timefacet = new ldt_facet("%a %b %d %H:%M:%S %Y %Z"); // full name
81 std::locale loc1(std::locale::classic(), timefacet);
82
83 typedef boost::date_time::time_facet<ptime, char> pt_facet;
84 //pt_facet* ptimefacet1 = new pt_facet("%c %Z"); // show that zone is ignored
85 pt_facet* ptimefacet1 = new pt_facet("%a %b %d %H:%M:%S %Y %Z"); // show that zone is ignored
86 std::locale loc2(std::locale::classic(), ptimefacet1);
87
88
89 std::cout << "\nFull time zone names tests" << std::endl;
90 teststreaming("ptime with %Z flag\n", a_time, std::string("Wed Dec 15 12:00:00 2004") , loc2);
91
92 teststreaming("UTC local_date_time", ldt1, std::string("Wed Dec 15 12:00:00 2004 Coordinated Universal Time"), loc1);
93 teststreaming("Chicago in summer", ldt2, std::string("Sun Aug 15 07:00:00 2004 Central Daylight Time") , loc1);
94 teststreaming("Denver in winter", ldt3, std::string("Wed Dec 15 05:00:00 2004 Mountain Standard Time"), loc1);
95 teststreaming("Los Angeles in summer", ldt4, std::string("Sun Aug 15 05:00:00 2004 Pacific Daylight Time"), loc1);
96 teststreaming("New York in winter", ldt5, std::string("Wed Dec 15 07:00:00 2004 Eastern Standard Time"), loc1);
97 teststreaming("Phoenix in Summer", ldt6, std::string("Sun Aug 15 05:00:00 2004 Mountain Standard Time"), loc1);
98
99 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);
100 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);
101
102 //ptimefacet1->format("%c %z"); // show that zone abbrev is ignored
103 ptimefacet1->format("%a %b %d %H:%M:%S %Y %z"); // show that zone abbrev is ignored
104 std::cout << "\nTime zone abbreviation tests" << std::endl;
105 teststreaming("ptime with %z flag\n", a_time, std::string("Wed Dec 15 12:00:00 2004") , loc2);
106
107 // using standard format
108 //timefacet->format("%c %z"); // abbreviated zone
109 timefacet->format("%a %b %d %H:%M:%S %Y %z"); // abbreviated zone
110 teststreaming("UTC local_date_time", ldt1, std::string("Wed Dec 15 12:00:00 2004 UTC"), loc1);
111 teststreaming("Chicago in summer", ldt2, std::string("Sun Aug 15 07:00:00 2004 CDT") , loc1);
112 teststreaming("Denver in winter", ldt3, std::string("Wed Dec 15 05:00:00 2004 MST"), loc1);
113 teststreaming("Los Angeles in summer", ldt4, std::string("Sun Aug 15 05:00:00 2004 PDT"), loc1);
114 teststreaming("New York in winter", ldt5, std::string("Wed Dec 15 07:00:00 2004 EST"), loc1);
115 teststreaming("Phoenix in Summer", ldt6, std::string("Sun Aug 15 05:00:00 2004 MST"), loc1);
116
117 // iso format
118 // show that zone offset is ignored
119 ptimefacet1->format(pt_facet::iso_time_format_specifier);
120 std::cout << "\nLocal time iso format tests" << std::endl;
121 teststreaming("ptime with iso format\n", a_time,
122 std::string("20041215T120000") , loc2);
123
124 timefacet->format(ldt_facet::iso_time_format_specifier);
125 teststreaming("UTC local_date_time", ldt1,
126 std::string("20041215T120000Z"), loc1);
127 teststreaming("Chicago in summer", ldt2,
128 std::string("20040815T070000-0500") , loc1);
129 teststreaming("Denver in winter", ldt3,
130 std::string("20041215T050000-0700"), loc1);
131 teststreaming("Los Angeles in summer", ldt4,
132 std::string("20040815T050000-0700"), loc1);
133 teststreaming("New York in winter", ldt5,
134 std::string("20041215T070000-0500"), loc1);
135 teststreaming("Phoenix in Summer", ldt6,
136 std::string("20040815T050000-0700"), loc1);
137 teststreaming("Sydney in December", ldt1.local_time_in(sydney),
138 std::string("20041215T230000+1100"), loc1);
139
140 // iso extended format
141 // show that zone offset is ignored
142 ptimefacet1->format(pt_facet::iso_time_format_extended_specifier);
143 std::cout << "\nLocal time iso_extended tests" << std::endl;
144 teststreaming("ptime with iso extended format\n", a_time,
145 std::string("2004-12-15 12:00:00") , loc2);
146
147 timefacet->format(ldt_facet::iso_time_format_extended_specifier);
148 teststreaming("UTC local_date_time", ldt1,
149 std::string("2004-12-15 12:00:00Z"), loc1);
150 teststreaming("Chicago in summer", ldt2,
151 std::string("2004-08-15 07:00:00-05:00") , loc1);
152 teststreaming("Denver in winter", ldt3,
153 std::string("2004-12-15 05:00:00-07:00"), loc1);
154 teststreaming("Los Angeles in summer", ldt4,
155 std::string("2004-08-15 05:00:00-07:00"), loc1);
156 teststreaming("New York in winter", ldt5,
157 std::string("2004-12-15 07:00:00-05:00"), loc1);
158 teststreaming("Phoenix in Summer", ldt6,
159 std::string("2004-08-15 05:00:00-07:00"), loc1);
160 teststreaming("Sydney in December", ldt1.local_time_in(sydney),
161 std::string("2004-12-15 23:00:00+11:00"), loc1);
162
163 #if !defined(BOOST_NO_STD_WSTRING)
164
165 typedef boost::date_time::time_facet<local_date_time, wchar_t> wldt_facet;
166 //wldt_facet* wtimefacet = new wldt_facet(L"%c %Z"); // full name
167 wldt_facet* wtimefacet = new wldt_facet(L"%a %b %d %H:%M:%S %Y %Z"); // full name
168 std::locale loc3(std::locale::classic(), wtimefacet);
169
170 /* Again, wide stream tests are more thoroughly done in the
171 * time_facet tests. Here we just need to show that they work */
172 std::cout << "\nFull time zone names tests - wide stream" << std::endl;
173 teststreaming("UTC local_date_time", ldt1, std::wstring(L"Wed Dec 15 12:00:00 2004 Coordinated Universal Time"), loc3);
174 teststreaming("Chicago in summer", ldt2, std::wstring(L"Sun Aug 15 07:00:00 2004 Central Daylight Time") , loc3);
175
176 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);
177 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);
178
179 //wtimefacet->format(L"%c %z"); // abbrev
180 wtimefacet->format(L"%a %b %d %H:%M:%S %Y %z"); // abbrev
181 std::cout << "\nAbbreviated time zone names tests - wide stream" << std::endl;
182 teststreaming("UTC local_date_time", ldt1, std::wstring(L"Wed Dec 15 12:00:00 2004 UTC"), loc3);
183 teststreaming("Phoenix in Summer", ldt6, std::wstring(L"Sun Aug 15 05:00:00 2004 MST"), loc3);
184
185 #endif // BOOST_NO_STD_WSTRING
186
187 return printTestStats();
188 }
189