1 /* Copyright (c) 2002,2003,2005 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 
8 #include "boost/date_time/posix_time/posix_time.hpp"
9 #include "../testfrmwk.hpp"
10 
11 
12 int
main()13 main()
14 {
15   using namespace boost::gregorian;
16   using namespace boost::posix_time;
17 
18 #ifdef BOOST_DATE_TIME_HAS_NANOSECONDS
19 
20   std::string s1("12:11:10.123456789");
21   time_duration td1= duration_from_string(s1);
22   check("parse time duration: " + s1,
23         td1 == time_duration(12,11,10,123456789));
24   std::string s2("12:11:10,123456789");
25   time_duration td2= boost::date_time::parse_delimited_time_duration<time_duration>(s2);
26   check("parse time duration: " + s2,
27         td2 == time_duration(12,11,10,123456789));
28   std::string s3("12:11:10");
29   time_duration td3= boost::date_time::parse_delimited_time_duration<time_duration>(s3);
30   check("parse time duration: " + s3,
31         td3 == time_duration(12,11,10,0));
32   std::string s4("23:59:59.000000001");
33   time_duration td4= boost::date_time::parse_delimited_time_duration<time_duration>(s4);
34   check("parse time duration: " + s4,
35         td4 == time_duration(23,59,59)+nanosec(1));
36   std::string s5("23:59:59.999999999");
37   time_duration td5= boost::date_time::parse_delimited_time_duration<time_duration>(s5);
38   check("parse time duration: " + s5,
39         td5 == time_duration(23,59,59)+nanosec(999999999));
40   std::string s5b("-23:59:59.999999999");
41   time_duration td5b= boost::date_time::parse_delimited_time_duration<time_duration>(s5b);
42   check("parse time duration: " + s5b,
43         td5b == time_duration(-23,59,59)-nanosec(999999999));
44 
45   std::string s6b("1:00:00.1"); // we want 1/10th
46   time_duration td6b= boost::date_time::parse_delimited_time_duration<time_duration>(s6b);
47   check("parse time duration: " + s6b,
48         td6b == time_duration(1,0,0)+nanosec(100000000)); // we want 1/10th
49 
50   std::string s7b("-1:00:00.0010"); // we want 1/1000th
51   time_duration td7b= boost::date_time::parse_delimited_time_duration<time_duration>(s7b);
52   check("parse time duration: " + s7b,
53         td7b == time_duration(-1,0,0)-nanosec(1000000)); // we want 1/1000th
54 
55   std::string s8b("1:22:33.123456789321"); // too many digits
56   time_duration td8b= boost::date_time::parse_delimited_time_duration<time_duration>(s8b);
57   check("parse time duration: " + s8b,
58         td8b == time_duration(1,22,33,123456789)); // excess digits should be dropped
59 #endif
60 
61 #if defined(BOOST_DATE_TIME_HAS_MICROSECONDS) && (!defined(BOOST_DATE_TIME_HAS_NANOSECONDS))
62   {
63     std::string s1("12:11:10.123456");
64     time_duration td1= duration_from_string(s1);
65     check("parse time duration: " + s1,
66           td1 == time_duration(12,11,10,123456));
67     std::string s2("12:11:10,123456");
68     time_duration td2= boost::date_time::parse_delimited_time_duration<time_duration>(s2);
69     check("parse time duration: " + s2,
70           td2 == time_duration(12,11,10,123456));
71     std::string s3("12:11:10");
72     time_duration td3= boost::date_time::parse_delimited_time_duration<time_duration>(s3);
73     check("parse time duration: " + s3,
74           td3 == time_duration(12,11,10,0));
75     std::string s4("23:59:59.000001");
76     time_duration td4= boost::date_time::parse_delimited_time_duration<time_duration>(s4);
77     check("parse time duration: " + s4,
78           td4 == time_duration(23,59,59)+microsec(1));
79     std::string s5("23:59:59.999999");
80     time_duration td5= boost::date_time::parse_delimited_time_duration<time_duration>(s5);
81     check("parse time duration: " + s5,
82           td5 == time_duration(23,59,59)+microsec(999999));
83     std::string s5b("-23:59:59.999999");
84     time_duration td5b= boost::date_time::parse_delimited_time_duration<time_duration>(s5b);
85     check("parse time duration: " + s5b,
86           td5b == time_duration(-23,59,59)-microsec(999999));
87 
88     std::string s6b("1:00:00.1"); // we want 1/10th
89     time_duration td6b= boost::date_time::parse_delimited_time_duration<time_duration>(s6b);
90     check("parse time duration: " + s6b,
91           td6b == time_duration(1,0,0)+microsec(100000)); // we want 1/10th
92 
93     std::string s7b("-1:00:00.0010"); // we want 1/1000th
94     time_duration td7b= boost::date_time::parse_delimited_time_duration<time_duration>(s7b);
95     check("parse time duration: " + s7b,
96           td7b == time_duration(-1,0,0)-microsec(1000)); // we want 1/1000th
97 
98   std::string s8b("1:22:33.123456321"); // too many digits
99   time_duration td8b= boost::date_time::parse_delimited_time_duration<time_duration>(s8b);
100   check("parse time duration: " + s8b,
101         td8b == time_duration(1,22,33,123456)); // excess digits should be dropped
102   }
103 #endif
104 
105 #ifdef BOOST_DATE_TIME_HAS_NANOSECONDS
106 
107   {
108     std::string ts2("2002-12-31 00:00:00.999999999");
109     ptime t2 = time_from_string(ts2);
110     check("parse time: " + ts2,
111           t2 == ptime(date(2002,12,31),time_duration(0,0,0)+nanosec(999999999)));
112   }
113   {
114     std::string ts2("2002-12-31 00:00:00.");
115     ptime t2 = time_from_string(ts2);
116     check("parse time (decimal but no digits): " + ts2,
117           t2 == ptime(date(2002,12,31),time_duration(0,0,0)));
118   }
119 #endif
120 
121 
122   std::string date_1, tod_1;
123   std::string ts1("2002-01-20 23:59:59.000");
124   boost::date_time::split(ts1, ' ', date_1, tod_1);
125   check("split function date part of " + ts1,
126         date_1 == std::string("2002-01-20"));
127   check("time part of " + ts1,
128         tod_1 == std::string("23:59:59.000"));
129 //   std::cout << date_1 << "|" << std::endl;
130 //   std::cout << tod_1  << "|" << std::endl;
131 
132 
133   {
134     ptime t1 = time_from_string(ts1);
135     check("parse time: " + ts1,
136           t1 == ptime(date(2002,1,20),time_duration(23,59,59)));
137   }
138   {
139     std::string ts1x("2002-01-20 23:59:59.");
140     ptime t1 = time_from_string(ts1x);
141     check("parse time (decimal but no digits): " + ts1x,
142           t1 == ptime(date(2002,1,20),time_duration(23,59,59)));
143   }
144 
145   std::string s6("235859");
146   time_duration td6= boost::date_time::parse_undelimited_time_duration<time_duration>(s6);
147   check("parse time duration: " + s6,
148         td6 == time_duration(23,58,59));
149 
150   s6 = "-235859";
151   td6= boost::date_time::parse_undelimited_time_duration<time_duration>(s6);
152   check("parse negative time duration: " + s6,
153         td6 == time_duration(-23,58,59));
154 
155   {
156     std::string ts3("20020120T235859");
157     ptime t20 = from_iso_string(ts3);
158     check("parse iso time: " + ts3,
159           t20 == ptime(date(2002,1,20),time_duration(23,58,59)));
160   }
161 
162   {
163     std::string ts4("19001231T000000");
164     ptime t21 = from_iso_string(ts4);
165     check("parse iso time: " + ts4,
166           t21 == ptime(date(1900,12,31),time_duration(0,0,0)));
167   }
168 
169   {
170     std::string ts5("19001231T23");
171     ptime t22 = from_iso_string(ts5);
172     check("parse iso time: " + ts5,
173           t22 == ptime(date(1900,12,31),time_duration(23,0,0)));
174   }
175 
176   {
177 #if defined(BOOST_DATE_TIME_POSIX_TIME_STD_CONFIG)
178     std::string ts3("20020120T235859.123456789");
179     ptime t20 = from_iso_string(ts3);
180     check("parse iso time w/ frac sec: " + ts3,
181           t20 == ptime(date(2002,1,20),time_duration(23,58,59,123456789)));
182 
183     std::string ts4("19001231T000000.123");
184     ptime t21 = from_iso_string(ts4);
185     check("parse iso time w/ frac sec (too short): " + ts4,
186           t21 == ptime(date(1900,12,31),time_duration(0,0,0,123000000)));
187 
188     std::string ts5("19001231T230000.123456789876");
189     ptime t22 = from_iso_string(ts5);
190     check("parse iso time w/ frac sec (too long): " + ts5,
191           t22 == ptime(date(1900,12,31),time_duration(23,0,0,123456789)));
192 
193     std::string ts6("19001231T230000.");
194     ptime t23 = from_iso_string(ts6);
195     check("parse iso time w/ frac sec (dec only): " + ts6,
196           t23 == ptime(date(1900,12,31),time_duration(23,0,0,0)));
197 
198     std::string ts7("2002-01-20T23:58:59.123456789");
199     ptime t24 = from_iso_extended_string(ts7);
200     check("parse iso extended time w/ frac sec: " + ts7,
201           t24 == ptime(date(2002,1,20),time_duration(23,58,59,123456789)));
202 
203     std::string ts8("1900-12-31T00:00:00.123");
204     ptime t25 = from_iso_extended_string(ts8);
205     check("parse iso extended time w/ frac sec (too short): " + ts8,
206           t25 == ptime(date(1900,12,31),time_duration(0,0,0,123000000)));
207 
208     std::string ts9("1900-12-31T23:00:00.123456789876");
209     ptime t26 = from_iso_extended_string(ts9);
210     check("parse iso extended time w/ frac sec (too long): " + ts9,
211           t26 == ptime(date(1900,12,31),time_duration(23,0,0,123456789)));
212 
213     std::string ts10("1900-12-31T23:00:00.");
214     ptime t27 = from_iso_extended_string(ts10);
215     check("parse iso extended time w/ frac sec (dec only): " + ts10,
216           t27 == ptime(date(1900,12,31),time_duration(23,0,0,0)));
217 #else
218     std::string ts3("20020120T235859.123456");
219     ptime t20 = from_iso_string(ts3);
220     check("parse iso time w/ frac sec: " + ts3,
221           t20 == ptime(date(2002,1,20),time_duration(23,58,59,123456)));
222 
223     std::string ts4("19001231T000000.123");
224     ptime t21 = from_iso_string(ts4);
225     check("parse iso time w/ frac sec (too short): " + ts4,
226           t21 == ptime(date(1900,12,31),time_duration(0,0,0,123000)));
227 
228     std::string ts5("19001231T230000.123456789876");
229     ptime t22 = from_iso_string(ts5);
230     check("parse iso time w/ frac sec (too long): " + ts5,
231           t22 == ptime(date(1900,12,31),time_duration(23,0,0,123456)));
232 
233     std::string ts6("19001231T230000.");
234     ptime t23 = from_iso_string(ts6);
235     check("parse iso time w/ frac sec (dec only): " + ts6,
236           t23 == ptime(date(1900,12,31),time_duration(23,0,0,0)));
237 
238     std::string ts7("2002-01-20T23:58:59.123456");
239     ptime t24 = from_iso_extended_string(ts7);
240     check("parse iso extended time w/ frac sec: " + ts7,
241           t24 == ptime(date(2002,1,20),time_duration(23,58,59,123456)));
242 
243     std::string ts8("1900-12-31T00:00:00.123");
244     ptime t25 = from_iso_extended_string(ts8);
245     check("parse iso extended time w/ frac sec (too short): " + ts8,
246           t25 == ptime(date(1900,12,31),time_duration(0,0,0,123000)));
247 
248     std::string ts9("1900-12-31T23:00:00.123456789876");
249     ptime t26 = from_iso_extended_string(ts9);
250     check("parse iso extended time w/ frac sec (too long): " + ts9,
251           t26 == ptime(date(1900,12,31),time_duration(23,0,0,123456)));
252 
253     std::string ts10("1900-12-31T23:00:00.");
254     ptime t27 = from_iso_extended_string(ts10);
255     check("parse iso extended time w/ frac sec (dec only): " + ts10,
256           t27 == ptime(date(1900,12,31),time_duration(23,0,0,0)));
257 #endif // BOOST_DATE_TIME_POSIX_TIME_STD_CONFIG
258   }
259 
260   std::string s7("-01:25:00"), s8("-00:40:00"), s9("0:45"), s10("0:-40");
261   time_duration tds1 = duration_from_string(s7);
262   time_duration tds2 = duration_from_string(s8);
263   time_duration tds3 = duration_from_string(s9);
264   time_duration tds4 = duration_from_string(s10);
265   check("from string construct", tds1 == time_duration(-1,25,0));
266   check("from string construct", tds2 == minutes(-40));
267   check("from string construct", tds3 == minutes(45));
268   // '-' in middle of string s10 should be ignored resulting in pos duration
269   check("from string construct", tds4 == minutes(40));
270 
271   return printTestStats();
272 
273 }
274 
275