1 | <?xml version="1.0" encoding="utf-8"?> |
---|
2 | <!DOCTYPE library PUBLIC "-//Boost//DTD BoostBook XML V1.0//EN" |
---|
3 | "../../../tools/boostbook/dtd/boostbook.dtd"> |
---|
4 | |
---|
5 | <!-- Copyright (c) 2001-2006 CrystalClear Software, Inc. |
---|
6 | Subject to the Boost Software License, Version 1.0. |
---|
7 | (See accompanying file LICENSE-1.0 or http://www.boost.org/LICENSE-1.0) |
---|
8 | --> |
---|
9 | |
---|
10 | <section id="date_time.posix_time.time_period"> |
---|
11 | <title>Time Period</title> |
---|
12 | |
---|
13 | <link linkend="time_period_intro">Introduction</link> -- |
---|
14 | <link linkend="time_period_header">Header</link> -- |
---|
15 | <link linkend="time_period_constr">Construction</link> -- |
---|
16 | <link linkend="time_period_mutators">Mutators</link> -- |
---|
17 | <link linkend="time_period_accessors">Accessors</link> -- |
---|
18 | <link linkend="time_period_to_string">Conversion To String</link> -- |
---|
19 | <link linkend="time_period_operators">Operators</link> |
---|
20 | |
---|
21 | <anchor id="time_period_intro" /> |
---|
22 | <bridgehead renderas="sect3">Introduction</bridgehead> |
---|
23 | <para> |
---|
24 | The class boost::posix_time::time_period provides direct representation for ranges between two times. Periods provide the ability to simplify some types of calculations by simplifying the conditional logic of the program. |
---|
25 | </para> |
---|
26 | <para> |
---|
27 | A period that is created with beginning and end points being equal, or with a duration of zero, is known as a zero length period. Zero length periods are considered invalid (it is perfectly legal to construct an invalid period). For these periods, the <code>last</code> point will always be one unit less that the <code>begin</code> point. |
---|
28 | </para> |
---|
29 | <para> |
---|
30 | The <link linkend="date_time.examples.time_periods">time periods example</link> provides an example of using time periods. |
---|
31 | </para> |
---|
32 | |
---|
33 | <anchor id="time_period_header" /> |
---|
34 | <bridgehead renderas="sect3">Header</bridgehead> |
---|
35 | <para> |
---|
36 | <programlisting>#include "boost/date_time/posix_time/posix_time.hpp" //include all types plus i/o |
---|
37 | or |
---|
38 | #include "boost/date_time/posix_time/posix_time_types.hpp" //no i/o just types</programlisting> |
---|
39 | </para> |
---|
40 | |
---|
41 | <anchor id="time_period_constr" /> |
---|
42 | <bridgehead renderas="sect3">Construction</bridgehead> |
---|
43 | <para> |
---|
44 | <informaltable frame="all"> |
---|
45 | <tgroup cols="2"> |
---|
46 | <thead> |
---|
47 | <row> |
---|
48 | <entry valign="top" morerows="1">Syntax</entry> |
---|
49 | <entry>Description</entry> |
---|
50 | </row> |
---|
51 | <row> |
---|
52 | <entry>Example</entry> |
---|
53 | </row> |
---|
54 | </thead> |
---|
55 | <tbody> |
---|
56 | <row> |
---|
57 | <entry valign="top" morerows="1"><screen>time_period(ptime, |
---|
58 | ptime)</screen></entry> |
---|
59 | <entry> Create a period as [begin, end). If end is <= begin then the period will be defined as invalid.</entry> |
---|
60 | </row> |
---|
61 | <row> |
---|
62 | <entry><screen>date d(2002,Jan,01); |
---|
63 | ptime t(d, seconds(10)); //10 sec after midnight |
---|
64 | time_period tp(t, hours(3));</screen> |
---|
65 | </entry> |
---|
66 | </row> |
---|
67 | |
---|
68 | <row> |
---|
69 | <entry valign="top" morerows="1"><screen>time_period(ptime, |
---|
70 | time_duration)</screen></entry> |
---|
71 | <entry> Create a period as [begin, begin+len) where end would be begin+len. If len is <= zero then the period will be defined as invalid.</entry> |
---|
72 | </row> |
---|
73 | <row> |
---|
74 | <entry><screen>date d(2002,Jan,01); |
---|
75 | ptime t1(d, seconds(10)); //10 sec after midnight |
---|
76 | ptime t2(d, hours(10)); //10 hours after midnight |
---|
77 | time_period tp(t1, t2);</screen> |
---|
78 | </entry> |
---|
79 | </row> |
---|
80 | |
---|
81 | <row> |
---|
82 | <entry valign="top" morerows="1"><screen>time_period(time_period rhs)</screen></entry> |
---|
83 | <entry> Copy constructor</entry> |
---|
84 | </row> |
---|
85 | <row> |
---|
86 | <entry><screen>time_period tp1(tp);</screen></entry> |
---|
87 | </row> |
---|
88 | </tbody> |
---|
89 | </tgroup> |
---|
90 | </informaltable> |
---|
91 | </para> |
---|
92 | |
---|
93 | |
---|
94 | <anchor id="time_period_mutators" /> |
---|
95 | <bridgehead renderas="sect3">Mutators</bridgehead> |
---|
96 | <para> |
---|
97 | <informaltable frame="all"> |
---|
98 | <tgroup cols="2"> |
---|
99 | <thead> |
---|
100 | <row> |
---|
101 | <entry valign="top" morerows="1">Syntax</entry> |
---|
102 | <entry>Description</entry> |
---|
103 | </row> |
---|
104 | <row> |
---|
105 | <entry>Example</entry> |
---|
106 | </row> |
---|
107 | </thead> |
---|
108 | <tbody> |
---|
109 | |
---|
110 | <row> |
---|
111 | <entry valign="top" morerows="1"><screen>time_period shift(time_duration)</screen></entry> |
---|
112 | <entry>Add duration to both begin and end.</entry> |
---|
113 | </row> |
---|
114 | <row> |
---|
115 | <entry> |
---|
116 | <screen> |
---|
117 | time_period tp(ptime(date(2005,Jan,1),hours(1)), hours(2)); |
---|
118 | tp.shift(minutes(5)); |
---|
119 | // tp == 2005-Jan-01 01:05:00 to 2005-Jan-01 03:05:00 |
---|
120 | </screen> |
---|
121 | </entry> |
---|
122 | </row> |
---|
123 | |
---|
124 | <row> |
---|
125 | <entry valign="top" morerows="1"><screen>time_period expand(days)</screen></entry> |
---|
126 | <entry>Add duration to both begin and end.</entry> |
---|
127 | </row> |
---|
128 | <row> |
---|
129 | <screen> |
---|
130 | time_period tp(ptime(date(2005,Jan,1),hours(1)), hours(2)); |
---|
131 | tp.expand(minutes(5)); |
---|
132 | // tp == 2005-Jan-01 00:55:00 to 2005-Jan-01 03:05:00 |
---|
133 | </screen> |
---|
134 | </row> |
---|
135 | |
---|
136 | </tbody> |
---|
137 | </tgroup> |
---|
138 | </informaltable> |
---|
139 | </para> |
---|
140 | |
---|
141 | |
---|
142 | <anchor id="time_period_accessors" /> |
---|
143 | <bridgehead renderas="sect3">Accessors</bridgehead> |
---|
144 | <para> |
---|
145 | <informaltable frame="all"> |
---|
146 | <tgroup cols="2"> |
---|
147 | <thead> |
---|
148 | <row> |
---|
149 | <entry valign="top" morerows="1">Syntax</entry> |
---|
150 | <entry>Description</entry> |
---|
151 | </row> |
---|
152 | <row> |
---|
153 | <entry>Example</entry> |
---|
154 | </row> |
---|
155 | </thead> |
---|
156 | <tbody> |
---|
157 | <row> |
---|
158 | <entry valign="top" morerows="1"><screen>ptime begin()</screen></entry> |
---|
159 | <entry>Return first time of period.</entry> |
---|
160 | </row> |
---|
161 | <row> |
---|
162 | <entry><screen>date d(2002,Jan,01); |
---|
163 | ptime t1(d, seconds(10)); //10 sec after midnight |
---|
164 | ptime t2(d, hours(10)); //10 hours after midnight |
---|
165 | time_period tp(t1, t2); |
---|
166 | tp.begin(); // --> 2002-Jan-01 00:00:10</screen> |
---|
167 | </entry> |
---|
168 | </row> |
---|
169 | |
---|
170 | <row> |
---|
171 | <entry valign="top" morerows="1"><screen>ptime last()</screen></entry> |
---|
172 | <entry>Return last time in the period</entry> |
---|
173 | </row> |
---|
174 | <row> |
---|
175 | <entry><screen>date d(2002,Jan,01); |
---|
176 | ptime t1(d, seconds(10)); //10 sec after midnight |
---|
177 | ptime t2(d, hours(10)); //10 hours after midnight |
---|
178 | time_period tp(t1, t2); |
---|
179 | tp.last();// --> 2002-Jan-01 09:59:59.999999999</screen> |
---|
180 | </entry> |
---|
181 | </row> |
---|
182 | |
---|
183 | <row> |
---|
184 | <entry valign="top" morerows="1"><screen>ptime end()</screen></entry> |
---|
185 | <entry> Return one past the last in period</entry> |
---|
186 | </row> |
---|
187 | <row> |
---|
188 | <entry><screen>date d(2002,Jan,01); |
---|
189 | ptime t1(d, seconds(10)); //10 sec after midnight |
---|
190 | ptime t2(d, hours(10)); //10 hours after midnight |
---|
191 | time_period tp(t1, t2); |
---|
192 | tp.last(); // --> 2002-Jan-01 10:00:00</screen> |
---|
193 | </entry> |
---|
194 | </row> |
---|
195 | |
---|
196 | <row> |
---|
197 | <entry valign="top" morerows="1"><screen>time_duration length()</screen></entry> |
---|
198 | <entry>Return the length of the time period.</entry> |
---|
199 | </row> |
---|
200 | <row> |
---|
201 | <entry><screen>date d(2002,Jan,01); |
---|
202 | ptime t1(d); //midnight |
---|
203 | time_period tp(t1, hours(1)); |
---|
204 | tp.length() --> 1 hour</screen> |
---|
205 | </entry> |
---|
206 | </row> |
---|
207 | |
---|
208 | <row> |
---|
209 | <entry valign="top" morerows="1"><screen>bool is_null()</screen></entry> |
---|
210 | <entry>True if period is not well formed. eg: end is less than or equal to begin.</entry> |
---|
211 | </row> |
---|
212 | <row> |
---|
213 | <entry><screen>date d(2002,Jan,01); |
---|
214 | ptime t1(d, hours(12)); // noon on Jan 1st |
---|
215 | ptime t2(d, hours(9)); // 9am on Jan 1st |
---|
216 | time_period tp(t1, t2); |
---|
217 | tp.is_null(); // true</screen> |
---|
218 | </entry> |
---|
219 | </row> |
---|
220 | |
---|
221 | <row> |
---|
222 | <entry valign="top" morerows="1"><screen>bool contains(ptime)</screen></entry> |
---|
223 | <entry>True if ptime is within the period. Zero length periods cannot contain any points.</entry> |
---|
224 | </row> |
---|
225 | <row> |
---|
226 | <entry><screen>date d(2002,Jan,01); |
---|
227 | ptime t1(d, seconds(10)); //10 sec after midnight |
---|
228 | ptime t2(d, hours(10)); //10 hours after midnight |
---|
229 | ptime t3(d, hours(2)); //2 hours after midnight |
---|
230 | time_period tp(t1, t2); |
---|
231 | tp.contains(t3); // true |
---|
232 | time_period tp2(t1, t1); |
---|
233 | tp2.contains(t1); // false</screen> |
---|
234 | </entry> |
---|
235 | </row> |
---|
236 | |
---|
237 | <row> |
---|
238 | <entry valign="top" morerows="1"><screen>bool contains(time_period)</screen></entry> |
---|
239 | <entry>True if period is within the period</entry> |
---|
240 | </row> |
---|
241 | <row> |
---|
242 | <entry><screen>time_period tp1(ptime(d,hours(1)), |
---|
243 | ptime(d,hours(12))); |
---|
244 | time_period tp2(ptime(d,hours(2)), |
---|
245 | ptime(d,hours(4))); |
---|
246 | tp1.contains(tp2); // --> true |
---|
247 | tp2.contains(tp1); // --> false</screen> |
---|
248 | </entry> |
---|
249 | </row> |
---|
250 | |
---|
251 | <row> |
---|
252 | <entry valign="top" morerows="1"><screen>bool intersects(time_period)</screen></entry> |
---|
253 | <entry> True if periods overlap</entry> |
---|
254 | </row> |
---|
255 | <row> |
---|
256 | <entry><screen>time_period tp1(ptime(d,hours(1)), |
---|
257 | ptime(d,hours(12))); |
---|
258 | time_period tp2(ptime(d,hours(2)), |
---|
259 | ptime(d,hours(4))); |
---|
260 | tp2.intersects(tp1); // --> true</screen> |
---|
261 | </entry> |
---|
262 | </row> |
---|
263 | |
---|
264 | <row> |
---|
265 | <entry valign="top" morerows="1"><screen>time_period intersection(time_period)</screen></entry> |
---|
266 | <entry>Calculate the intersection of 2 periods. Null if no intersection.</entry> |
---|
267 | </row> |
---|
268 | <row> |
---|
269 | <entry></entry> |
---|
270 | </row> |
---|
271 | |
---|
272 | <row> |
---|
273 | <entry valign="top" morerows="1"><screen>time_period merge(time_period)</screen></entry> |
---|
274 | <entry>Returns union of two periods. Null if no intersection.</entry> |
---|
275 | </row> |
---|
276 | <row> |
---|
277 | <entry></entry> |
---|
278 | </row> |
---|
279 | |
---|
280 | <row> |
---|
281 | <entry valign="top" morerows="1"><screen>time_period span(time_period)</screen></entry> |
---|
282 | <entry>Combines two periods and any gap between them such that begin = min(p1.begin, p2.begin) and end = max(p1.end , p2.end).</entry> |
---|
283 | </row> |
---|
284 | <row> |
---|
285 | <entry></entry> |
---|
286 | </row> |
---|
287 | |
---|
288 | </tbody> |
---|
289 | </tgroup> |
---|
290 | </informaltable> |
---|
291 | </para> |
---|
292 | |
---|
293 | |
---|
294 | <anchor id="time_period_to_string" /> |
---|
295 | <bridgehead renderas="sect3">Conversion To String</bridgehead> |
---|
296 | <para> |
---|
297 | <informaltable frame="all"> |
---|
298 | <tgroup cols="2"> |
---|
299 | <thead> |
---|
300 | <row> |
---|
301 | <entry valign="top" morerows="1">Syntax</entry> |
---|
302 | <entry>Description</entry> |
---|
303 | </row> |
---|
304 | <row> |
---|
305 | <entry>Example</entry> |
---|
306 | </row> |
---|
307 | </thead> |
---|
308 | <tbody> |
---|
309 | <row> |
---|
310 | <entry valign="top" morerows="1"><screen>std::string |
---|
311 | to_simple_string(time_period dp)</screen></entry> |
---|
312 | <entry>To <code>[YYYY-mmm-DD hh:mm:ss.fffffffff/YYYY-mmm-DD hh:mm:ss.fffffffff]</code> string where <code>mmm</code> is 3 char month name.</entry> |
---|
313 | </row> |
---|
314 | <row> |
---|
315 | <entry><screen>[2002-Jan-01 01:25:10.000000001/ |
---|
316 | 2002-Jan-31 01:25:10.123456789] |
---|
317 | // string occupies one line</screen></entry> |
---|
318 | </row> |
---|
319 | </tbody> |
---|
320 | </tgroup> |
---|
321 | </informaltable> |
---|
322 | </para> |
---|
323 | |
---|
324 | |
---|
325 | <anchor id="time_period_operators" /> |
---|
326 | <bridgehead renderas="sect3">Operators</bridgehead> |
---|
327 | <para> |
---|
328 | <informaltable frame="all"> |
---|
329 | <tgroup cols="2"> |
---|
330 | <thead> |
---|
331 | <row> |
---|
332 | <entry valign="top" morerows="1">Syntax</entry> |
---|
333 | <entry>Description</entry> |
---|
334 | </row> |
---|
335 | <row> |
---|
336 | <entry>Example</entry> |
---|
337 | </row> |
---|
338 | </thead> |
---|
339 | <tbody> |
---|
340 | <row> |
---|
341 | <entry valign="top" morerows="1"><screen>operator<<</screen></entry> |
---|
342 | <entry>Output streaming operator for time duration. Uses facet to output [date time_of_day/date time_of_day]. The default is format is <code>[YYYY-mmm-DD hh:mm:ss.fffffffff/YYYY-mmm-DD hh:mm:ss.fffffffff]</code> string where <code>mmm</code> is 3 char month name and the fractional seconds are left out when zero.</entry> |
---|
343 | </row> |
---|
344 | <row> |
---|
345 | <entry><screen>[2002-Jan-01 01:25:10.000000001/ \ |
---|
346 | 2002-Jan-31 01:25:10.123456789]</screen></entry> |
---|
347 | </row> |
---|
348 | |
---|
349 | <row> |
---|
350 | <entry valign="top" morerows="1"><screen>operator>></screen></entry> |
---|
351 | <entry>Input streaming operator for time duration. Uses facet to read [date time_of_day/date time_of_day]. The default is format is <code>[YYYY-mmm-DD hh:mm:ss.fffffffff/YYYY-mmm-DD hh:mm:ss.fffffffff]</code> string where <code>mmm</code> is 3 char month name and the fractional seconds are left out when zero.</entry> |
---|
352 | </row> |
---|
353 | <row> |
---|
354 | <entry><screen>[2002-Jan-01 01:25:10.000000001/ \ |
---|
355 | 2002-Jan-31 01:25:10.123456789]</screen></entry> |
---|
356 | </row> |
---|
357 | |
---|
358 | <row> |
---|
359 | <entry valign="top" morerows="1"><screen>operator==, operator!=</screen></entry> |
---|
360 | <entry>Equality operators. Periods are equal if p1.begin == p2.begin && p1.last == p2.last</entry> |
---|
361 | </row> |
---|
362 | <row> |
---|
363 | <entry><screen>if (tp1 == tp2) {...</screen></entry> |
---|
364 | </row> |
---|
365 | |
---|
366 | <row> |
---|
367 | <entry valign="top" morerows="1"><screen>operator<</screen></entry> |
---|
368 | <entry>Ordering with no overlap. True if tp1.end() less than tp2.begin()</entry> |
---|
369 | </row> |
---|
370 | <row> |
---|
371 | <entry><screen>if (tp1 < tp2) {...</screen></entry> |
---|
372 | </row> |
---|
373 | |
---|
374 | <row> |
---|
375 | <entry valign="top" morerows="1"><screen>operator></screen></entry> |
---|
376 | <entry>Ordering with no overlap. True if tp1.begin() greater than tp2.end()</entry> |
---|
377 | </row> |
---|
378 | <row> |
---|
379 | <entry><screen>if (tp1 > tp2) {... etc</screen></entry> |
---|
380 | </row> |
---|
381 | |
---|
382 | <row> |
---|
383 | <entry valign="top" morerows="1"><screen>operator<=, operator>=</screen></entry> |
---|
384 | <entry>Defined in terms of the other operators.</entry> |
---|
385 | </row> |
---|
386 | <row> |
---|
387 | <entry></entry> |
---|
388 | </row> |
---|
389 | </tbody> |
---|
390 | </tgroup> |
---|
391 | </informaltable> |
---|
392 | </para> |
---|
393 | |
---|
394 | </section> |
---|