1 /* Copyright (c) 2002,2003 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 #include "boost/date_time/posix_time/posix_time.hpp"
8 #include "boost/date_time/gregorian/gregorian.hpp"
9 #include "../testfrmwk.hpp"
10
11 #ifndef BOOST_DATE_TIME_NO_LOCALE
12
13 const char* const de_short_month_names[]={"Jan","Feb","Mar","Apr","Mai","Jun","Jul","Aug","Sep","Okt","Nov","Dez", "NAM"};
14
15 const char* const de_long_month_names[]={"Januar","Februar","Marz","April","Mai","Juni","Juli","August","September","Oktober","November","Dezember","NichtDerMonat"};
16 const char* const de_special_value_names[]={"NichtDatumzeit","-unbegrenztheit", "+unbegrenztheit"};
17
18 const char* const de_short_weekday_names[]={"Son", "Mon", "Die","Mit", "Don", "Fre", "Sam"};
19
20 const char* const de_long_weekday_names[]={"Sonntag", "Montag", "Dienstag","Mittwoch", "Donnerstag", "Freitag", "Samstag"};
21
22 #endif
23
24 int
main()25 main()
26 {
27 #ifndef BOOST_DATE_TIME_NO_LOCALE
28 using namespace boost::gregorian;
29 using namespace boost::posix_time;
30 std::stringstream ss;
31 date d1(2002,May,1);
32 ptime t1(d1, hours(12)+minutes(10)+seconds(5));
33 time_duration td0(12,10,5,123);
34 ptime t0(d1, td0);
35 ss << t0;
36 #ifdef BOOST_DATE_TIME_HAS_NANOSECONDS
37 check("check time output: "+ss.str(),
38 ss.str() == std::string("2002-May-01 12:10:05.000000123"));
39 #else
40 check("check time output: "+ss.str(),
41 ss.str() == std::string("2002-May-01 12:10:05.000123"));
42 #endif // BOOST_DATE_TIME_HAS_NANOSECONDS
43 // ss.imbue(global);
44 time_period tp(t1, ptime(d1, hours(23)+time_duration::unit()));
45 ss.str("");
46 ss << tp;
47 check("check time period output: "+ ss.str(),
48 ss.str() == std::string("[2002-May-01 12:10:05/2002-May-01 23:00:00]"));
49
50 //Send out the same time with german
51 typedef boost::date_time::all_date_names_put<greg_facet_config> date_facet;
52
53 ss.imbue(std::locale(std::locale::classic(),
54 new date_facet(de_short_month_names,
55 de_long_month_names,
56 de_special_value_names,
57 de_short_weekday_names,
58 de_long_weekday_names,
59 '.',
60 boost::date_time::ymd_order_dmy,
61 boost::date_time::month_as_short_string)));
62 ss.str("");
63 ss << t1;
64 check("check time output: "+ ss.str(),
65 ss.str() == std::string("01.Mai.2002 12:10:05"));
66
67
68 time_duration td(5,4,3);
69 time_duration td1(-1,25,0), td2(0,-40,0);
70 ss.str("");
71 ss << td;
72 check("check time period output: "+ ss.str(),
73 ss.str() == std::string("05:04:03"));
74 ss.str("");
75 ss << td1;
76 check("check time period output: "+ ss.str(),
77 ss.str() == std::string("-01:25:00"));
78 ss.str("");
79 ss << td2;
80 check("check time period output: "+ ss.str(),
81 ss.str() == std::string("-00:40:00"));
82
83
84 ss.str("");
85 ss << tp;
86 check("check time period output - german: "+ ss.str(),
87 ss.str() == std::string("[01.Mai.2002 12:10:05/01.Mai.2002 23:00:00]"));
88
89 /* Input streaming is only available for compilers that
90 * do not have BOOST_DATE_TIME_INCLUDE_LIMITED_HEADERS defined */
91 #ifndef BOOST_DATE_TIME_INCLUDE_LIMITED_HEADERS
92
93 /****** test streaming in for time classes ******/
94 {
95 std::istringstream iss("01:02:03.000004 garbage");
96 iss >> td;
97 #ifdef BOOST_DATE_TIME_HAS_NANOSECONDS
98 check("Stream in time_duration", td == time_duration(1,2,3,4000));
99 #else
100 check("Stream in time_duration", td == time_duration(1,2,3,4));
101 #endif
102 }
103 #if !defined(BOOST_NO_STD_ITERATOR_TRAITS) // vc6 & vc7
104 {
105 std::istringstream iss("2003-May-13 01:02:03");
106 iss >> t1;
107 check("Stream in ptime", t1 == ptime(date(2003,May,13), time_duration(1,2,3)));
108 std::istringstream iss2("2003-January-13 01:02:03");
109 iss2 >> t1;
110 check("Stream in ptime2", t1 == ptime(date(2003,Jan,13), time_duration(1,2,3)));
111 std::istringstream iss3("2003-feb-13 11:10:09");
112 iss3 >> t1;
113 check("Stream in ptime3", t1 == ptime(date(2003,Feb,13), time_duration(11,10,9)));
114
115 try {
116 std::istringstream iss4("2003-xxx-13 11:10:09");
117 iss3 >> t1;
118 check("Stream bad ptime", false); //never reach here, bad month exception
119 }
120 catch(std::exception& e) {
121 std::cout << "Got expected exception: " << e.what() << std::endl;
122 check("Stream bad ptime", true);
123 }
124
125 }
126 {
127 date d1x(2001,Aug,1), d2x(2003,May,13);
128 #ifdef BOOST_DATE_TIME_HAS_NANOSECONDS
129 time_duration td1x(15,32,18,20304000), td2x(1,2,3);
130 #else
131 time_duration td1x(15,32,18,20304), td2x(1,2,3);
132 #endif
133
134 time_period result(ptime(d1x,td1x), ptime(d2x,td2x));
135 std::istringstream iss("[2001-Aug-01 15:32:18.020304/2003-May-13 01:02:03]");
136 iss >> tp;
137 check("Stream in time_period", tp == result);
138 }
139
140 #if !defined(BOOST_NO_STD_WSTRING)
141 /*** wide streaming ***/
142 {
143 std::wistringstream wiss1(L"01:02:03");//.000004");
144 wiss1 >> td;
145 check("Wide stream in time_duration", td == time_duration(1,2,3));
146
147 std::wistringstream wiss2(L"2003-May-23 03:20:10");
148 wiss2 >> t1;
149 check("Wide stream in ptime", t1 == ptime(date(2003,May,23), time_duration(3,20,10)));
150
151 std::wistringstream wiss3(L"[2004-Jan-01 02:03:04/2004-May-13 01:00:00]");
152 wiss3 >> tp;
153 date d1x = date(2004,Jan,1);
154 date d2x = date(2004,May,13);
155 time_duration td1x = time_duration(2,3,4);
156 time_duration td2x = time_duration(1,0,0);
157 time_period result = time_period(ptime(d1x,td1x), ptime(d2x,td2x));
158 check("Wide stream in time_period", tp == result);
159 }
160 #else
161 check("Wide streaming not available for this compiler", false);
162 #endif // BOOST_NO_STD_WSTRING
163
164 #else // BOOST_NO_STD_ITERATOR_TRAITS
165 check("Streaming in of alphabetic dates (Ex: 2003-Aug-21) \
166 not supported by this compiler", false);
167 #endif // BOOST_NO_STD_ITERATOR_TRAITS
168
169 #else // BOOST_DATE_TIME_INCLUDE_LIMITED_HEADERS
170 check("Streaming in of time classes not supported by this compiler", false);
171 #endif // BOOST_DATE_TIME_INCLUDE_LIMITED_HEADERS
172
173 #else //BOOST_DATE_TIME_NO_LOCALE
174 check("No tests executed - Locales not supported by this compiler", false);
175 #endif //BOOST_DATE_TIME_NO_LOCALE
176
177 return printTestStats();
178 }
179
180