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