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.gregorian.date_period"> |
---|
11 | <title>Date Period</title> |
---|
12 | |
---|
13 | <link linkend="period_intro">Introduction</link> -- |
---|
14 | <link linkend="period_header">Header</link> -- |
---|
15 | <link linkend="period_construction">Construction</link> -- |
---|
16 | <link linkend="date_period_mutators">Mutators</link> -- |
---|
17 | <link linkend="period_accessors">Accessors</link> -- |
---|
18 | <link linkend="period_convert_to_string">Convert to String</link> -- |
---|
19 | <link linkend="period_operators">Operators</link> |
---|
20 | |
---|
21 | <anchor id="period_intro" /> |
---|
22 | <bridgehead renderas="sect3">Introduction</bridgehead> |
---|
23 | <para> |
---|
24 | The class boost::gregorian::date_period provides direct representation for ranges between two dates. Periods provide the ability to simplify some types of calculations by simplifying the conditional logic of the program. For example, testing if a date is within an irregular schedule such as a weekend or holiday can be accomplished using collections of date periods. This is facilitated by several methods that allow evaluation if a date_period intersects with another date period, and to generate the period resulting from the intersection. The <link linkend="date_time.examples.date_period_calc">date period calculation example</link> provides an example of this. |
---|
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 | Date periods used in combination with infinity values have the ability to represent complex concepts such as 'until further notice'. |
---|
31 | </para> |
---|
32 | |
---|
33 | <anchor id="period_header" /> |
---|
34 | <bridgehead renderas="sect3">Header</bridgehead> |
---|
35 | <para> |
---|
36 | <programlisting>#include "boost/date_time/gregorian/gregorian.hpp" //include all types plus i/o |
---|
37 | or |
---|
38 | #include "boost/date_time/gregorian/gregorian_types.hpp" //no i/o just types</programlisting> |
---|
39 | </para> |
---|
40 | |
---|
41 | <anchor id="period_construction" /> |
---|
42 | <bridgehead renderas="sect3">Construction</bridgehead> |
---|
43 | <informaltable frame="all"> |
---|
44 | <tgroup cols="2"> |
---|
45 | <thead> |
---|
46 | <row> |
---|
47 | <entry valign="top" morerows="1">Syntax</entry> |
---|
48 | <entry>Description</entry> |
---|
49 | </row> |
---|
50 | <row> |
---|
51 | <entry>Example</entry> |
---|
52 | </row> |
---|
53 | </thead> |
---|
54 | <tbody> |
---|
55 | <row> |
---|
56 | <entry valign="top" morerows="1"><screen>date_period(date, date)</screen></entry> |
---|
57 | <entry>Create a period as [begin, end). If end is <= begin then the period will be invalid.</entry> |
---|
58 | </row> |
---|
59 | <row> |
---|
60 | <entry><screen>date_period dp(date(2002,Jan,10), |
---|
61 | date(2002,Jan,12));</screen></entry> |
---|
62 | </row> |
---|
63 | |
---|
64 | <row> |
---|
65 | <entry valign="top" morerows="1"><screen>date_period(date, days)</screen></entry> |
---|
66 | <entry>Create a period as [begin, begin+len) where end point would be begin+len. If len is <= zero then the period will be defined as invalid.</entry> |
---|
67 | </row> |
---|
68 | <row> |
---|
69 | <entry><screen>date_period dp(date(2002,Jan,10), |
---|
70 | days(2));</screen></entry> |
---|
71 | </row> |
---|
72 | |
---|
73 | <row> |
---|
74 | <entry valign="top" morerows="1"><screen>date_period(date_period)</screen></entry> |
---|
75 | <entry> Copy constructor</entry> |
---|
76 | </row> |
---|
77 | <row> |
---|
78 | <entry><screen>date_period dp1(dp);</screen></entry> |
---|
79 | </row> |
---|
80 | </tbody> |
---|
81 | </tgroup> |
---|
82 | </informaltable> |
---|
83 | |
---|
84 | |
---|
85 | <anchor id="date_period_mutators" /> |
---|
86 | <bridgehead renderas="sect3">Mutators</bridgehead> |
---|
87 | <para> |
---|
88 | <informaltable frame="all"> |
---|
89 | <tgroup cols="2"> |
---|
90 | <thead> |
---|
91 | <row> |
---|
92 | <entry valign="top" morerows="1">Syntax</entry> |
---|
93 | <entry>Description</entry> |
---|
94 | </row> |
---|
95 | <row> |
---|
96 | <entry>Example</entry> |
---|
97 | </row> |
---|
98 | </thead> |
---|
99 | <tbody> |
---|
100 | <row> |
---|
101 | <entry valign="top" morerows="1"><screen>date_period shift(days)</screen></entry> |
---|
102 | <entry>Add duration to both begin and end.</entry> |
---|
103 | </row> |
---|
104 | <row> |
---|
105 | <entry> |
---|
106 | <screen> |
---|
107 | date_period dp(date(2005,Jan,1), days(3)); |
---|
108 | dp.shift(days(3)); |
---|
109 | // dp == 2005-Jan-04 to 2005-Jan-07 |
---|
110 | </screen> |
---|
111 | </entry> |
---|
112 | </row> |
---|
113 | |
---|
114 | <row> |
---|
115 | <entry valign="top" morerows="1"><screen>date_period expand(days)</screen></entry> |
---|
116 | <entry>Add duration to both begin and end.</entry> |
---|
117 | </row> |
---|
118 | <row> |
---|
119 | <screen> |
---|
120 | date_period dp(date(2005,Jan,2), days(2)); |
---|
121 | dp.expand(days(1)); |
---|
122 | // dp == 2005-Jan-01 to 2005-Jan-04 |
---|
123 | </screen> |
---|
124 | </row> |
---|
125 | |
---|
126 | </tbody> |
---|
127 | </tgroup> |
---|
128 | </informaltable> |
---|
129 | </para> |
---|
130 | |
---|
131 | |
---|
132 | <anchor id="period_accessors" /> |
---|
133 | <bridgehead renderas="sect3">Accessors</bridgehead> |
---|
134 | <informaltable frame="all"> |
---|
135 | <tgroup cols="2"> |
---|
136 | <thead> |
---|
137 | <row> |
---|
138 | <entry valign="top" morerows="1">Syntax</entry> |
---|
139 | <entry>Description</entry> |
---|
140 | </row> |
---|
141 | <row> |
---|
142 | <entry>Example</entry> |
---|
143 | </row> |
---|
144 | </thead> |
---|
145 | <tbody> |
---|
146 | <row> |
---|
147 | <entry valign="top" morerows="1"><screen>date begin()</screen></entry> |
---|
148 | <entry> Return first day of period.</entry> |
---|
149 | </row> |
---|
150 | <row> |
---|
151 | <entry><screen>date_period dp(date(2002,Jan,1), |
---|
152 | date(2002,Jan,10)); |
---|
153 | dp.begin() --> 2002-Jan-01</screen> |
---|
154 | </entry> |
---|
155 | </row> |
---|
156 | |
---|
157 | <row> |
---|
158 | <entry valign="top" morerows="1"><screen>date last()</screen></entry> |
---|
159 | <entry>Return last date in the period</entry> |
---|
160 | </row> |
---|
161 | <row> |
---|
162 | <entry><screen>date_period dp(date(2002,Jan,1), |
---|
163 | date(2002,Jan,10)); |
---|
164 | dp.last() --> 2002-Jan-09</screen> |
---|
165 | </entry> |
---|
166 | </row> |
---|
167 | |
---|
168 | <row> |
---|
169 | <entry valign="top" morerows="1"><screen>date end()</screen></entry> |
---|
170 | <entry>Return one past the last in period</entry> |
---|
171 | </row> |
---|
172 | <row> |
---|
173 | <entry><screen>date_period dp(date(2002,Jan,1), |
---|
174 | date(2002,Jan,10)); |
---|
175 | dp.end() --> 2002-Jan-10</screen> |
---|
176 | </entry> |
---|
177 | </row> |
---|
178 | |
---|
179 | <row> |
---|
180 | <entry valign="top" morerows="1"><screen>days length()</screen></entry> |
---|
181 | <entry>Return the length of the date_period</entry> |
---|
182 | </row> |
---|
183 | <row> |
---|
184 | <entry><screen>date_period dp(date(2002,Jan,1), |
---|
185 | days(2)); |
---|
186 | dp.length() --> 2</screen> |
---|
187 | </entry> |
---|
188 | </row> |
---|
189 | |
---|
190 | <row> |
---|
191 | <entry valign="top" morerows="1"><screen>bool is_null()</screen></entry> |
---|
192 | <entry>True if period is not well formed. eg: end less than or equal to begin.</entry> |
---|
193 | </row> |
---|
194 | <row> |
---|
195 | <entry><screen>date_period dp(date(2002,Jan,10), |
---|
196 | date(2002,Jan,1)); |
---|
197 | dp.begin() --> true</screen> |
---|
198 | </entry> |
---|
199 | </row> |
---|
200 | |
---|
201 | <row> |
---|
202 | <entry valign="top" morerows="1"><screen>bool contains(date)</screen></entry> |
---|
203 | <entry>True if date is within the period. Zero length periods cannot contain any points</entry> |
---|
204 | </row> |
---|
205 | <row> |
---|
206 | <entry><screen>date d(2002,Jan,1); |
---|
207 | date_period dp(d, date(2002,Jan,10)); |
---|
208 | dp.contains(date(2002,Jan,2));// true |
---|
209 | date_period dp2(d, d); |
---|
210 | dp.contains(date(2002,Jan,1));// false</screen> |
---|
211 | </entry> |
---|
212 | </row> |
---|
213 | |
---|
214 | <row> |
---|
215 | <entry valign="top" morerows="1"><screen>bool contains(date_period)</screen></entry> |
---|
216 | <entry>True if date period is within the period</entry> |
---|
217 | </row> |
---|
218 | <row> |
---|
219 | <entry><screen>date_period dp1(date(2002,Jan,1), |
---|
220 | date(2002,Jan,10)); |
---|
221 | date_period dp2(date(2002,Jan,2), |
---|
222 | date(2002,Jan,3)); |
---|
223 | dp1.contains(dp2) --> true |
---|
224 | dp2.contains(dp1) --> false</screen> |
---|
225 | </entry> |
---|
226 | </row> |
---|
227 | |
---|
228 | <row> |
---|
229 | <entry valign="top" morerows="1"><screen>bool intersects(date_period)</screen></entry> |
---|
230 | <entry>True if periods overlap</entry> |
---|
231 | </row> |
---|
232 | <row> |
---|
233 | <entry><screen>date_period dp1(date(2002,Jan,1), |
---|
234 | date(2002,Jan,10)); |
---|
235 | date_period dp2(date(2002,Jan,2), |
---|
236 | date(2002,Jan,3)); |
---|
237 | dp2.intersects(dp1) --> true</screen> |
---|
238 | </entry> |
---|
239 | </row> |
---|
240 | |
---|
241 | <row> |
---|
242 | <entry valign="top" morerows="1"><screen>date_period intersection(date_period)</screen></entry> |
---|
243 | <entry>Calculate the intersection of 2 periods. Null if no intersection.</entry> |
---|
244 | </row> |
---|
245 | <row> |
---|
246 | <entry><screen>date_period dp1(date(2002,Jan,1), |
---|
247 | date(2002,Jan,10)); |
---|
248 | date_period dp2(date(2002,Jan,2), |
---|
249 | date(2002,Jan,3)); |
---|
250 | dp2.intersection(dp1) --> dp2</screen> |
---|
251 | </entry> |
---|
252 | </row> |
---|
253 | |
---|
254 | <row> |
---|
255 | <entry valign="top" morerows="1"><screen>date_period is_adjacent(date_period)</screen></entry> |
---|
256 | <entry>Check if two periods are adjacent, but not overlapping.</entry> |
---|
257 | </row> |
---|
258 | <row> |
---|
259 | <entry><screen>date_period dp1(date(2002,Jan,1), |
---|
260 | date(2002,Jan,3)); |
---|
261 | date_period dp2(date(2002,Jan,3), |
---|
262 | date(2002,Jan,10)); |
---|
263 | dp2.is_adjacent(dp1) --> true</screen> |
---|
264 | </entry> |
---|
265 | </row> |
---|
266 | |
---|
267 | <row> |
---|
268 | <entry valign="top" morerows="1"><screen>date_period is_after(date)</screen></entry> |
---|
269 | <entry>Determine the period is after a given date.</entry> |
---|
270 | </row> |
---|
271 | <row> |
---|
272 | <entry><screen>date_period dp1(date(2002,Jan,10), |
---|
273 | date(2002,Jan,30)); |
---|
274 | date d(2002,Jan,3); |
---|
275 | dp1.is_after(d) --> true</screen> |
---|
276 | </entry> |
---|
277 | </row> |
---|
278 | |
---|
279 | <row> |
---|
280 | <entry valign="top" morerows="1"><screen>date_period is_before(date)</screen></entry> |
---|
281 | <entry>Determine the period is before a given date.</entry> |
---|
282 | </row> |
---|
283 | <row> |
---|
284 | <entry><screen>date_period dp1(date(2002,Jan,1), |
---|
285 | date(2002,Jan,3)); |
---|
286 | date d(2002,Jan,10); |
---|
287 | dp1.is_before(d) --> true</screen> |
---|
288 | </entry> |
---|
289 | </row> |
---|
290 | |
---|
291 | <row> |
---|
292 | <entry valign="top" morerows="1"><screen>date_period merge(date_period)</screen></entry> |
---|
293 | <entry>Returns union of two periods. Null if no intersection.</entry> |
---|
294 | </row> |
---|
295 | <row> |
---|
296 | <entry><screen>date_period dp1(date(2002,Jan,1), |
---|
297 | date(2002,Jan,10)); |
---|
298 | date_period dp2(date(2002,Jan,9), |
---|
299 | date(2002,Jan,31)); |
---|
300 | dp2.merge(dp1) |
---|
301 | // 2002-Jan-01/2002-Jan-31</screen> |
---|
302 | </entry> |
---|
303 | </row> |
---|
304 | |
---|
305 | <row> |
---|
306 | <entry valign="top" morerows="1"><screen>date_period span(date_period)</screen></entry> |
---|
307 | <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> |
---|
308 | </row> |
---|
309 | <row> |
---|
310 | <entry><screen> |
---|
311 | date_period dp1(date(2002,Jan,1), |
---|
312 | date(2002,Jan,5)); |
---|
313 | date_period dp2(date(2002,Jan,9), |
---|
314 | date(2002,Jan,31)); |
---|
315 | dp2.span(dp1); // 2002-Jan-01/2002-Jan-31</screen> |
---|
316 | </entry> |
---|
317 | </row> |
---|
318 | |
---|
319 | <row> |
---|
320 | <entry valign="top" morerows="1"><screen>date_period shift(days)</screen></entry> |
---|
321 | <entry>Add duration to both begin and end.</entry> |
---|
322 | </row> |
---|
323 | <row> |
---|
324 | <entry><screen>date_period dp1(date(2002,Jan,1), |
---|
325 | date(2002,Jan,10)); |
---|
326 | dp1.shift(days(1)); |
---|
327 | // 2002-Jan-02/2002-Jan-11</screen> |
---|
328 | </entry> |
---|
329 | </row> |
---|
330 | |
---|
331 | <row> |
---|
332 | <entry valign="top" morerows="1"><screen>date_period expand(days)</screen></entry> |
---|
333 | <entry>Subtract duration from begin and add duration to end.</entry> |
---|
334 | </row> |
---|
335 | <row> |
---|
336 | <entry><screen>date_period dp1(date(2002,Jan,4), |
---|
337 | date(2002,Jan,10)); |
---|
338 | dp1.expand(days(2)); |
---|
339 | // 2002-Jan-02/2002-Jan-12</screen> |
---|
340 | </entry> |
---|
341 | </row> |
---|
342 | </tbody> |
---|
343 | </tgroup> |
---|
344 | </informaltable> |
---|
345 | |
---|
346 | |
---|
347 | <anchor id="period_convert_to_string" /> |
---|
348 | <bridgehead renderas="sect3">Convert to String</bridgehead> |
---|
349 | <informaltable frame="all"> |
---|
350 | <tgroup cols="2"> |
---|
351 | <thead> |
---|
352 | <row> |
---|
353 | <entry valign="top" morerows="1">Syntax</entry> |
---|
354 | <entry>Description</entry> |
---|
355 | </row> |
---|
356 | <row> |
---|
357 | <entry>Example</entry> |
---|
358 | </row> |
---|
359 | </thead> |
---|
360 | <tbody> |
---|
361 | <row> |
---|
362 | <entry valign="top" morerows="1"><screen>std::string to_simple_string(date_period dp)</screen></entry> |
---|
363 | <entry>To <code>[YYYY-mmm-DD/YYYY-mmm-DD]</code> string where <code>mmm</code> is 3 char month name.</entry> |
---|
364 | </row> |
---|
365 | <row> |
---|
366 | <entry><screen>[2002-Jan-01/2002-Jan-31]</screen></entry> |
---|
367 | </row> |
---|
368 | </tbody> |
---|
369 | </tgroup> |
---|
370 | </informaltable> |
---|
371 | |
---|
372 | |
---|
373 | <anchor id="period_operators" /> |
---|
374 | <bridgehead renderas="sect3">Operators</bridgehead> |
---|
375 | <informaltable frame="all"> |
---|
376 | <tgroup cols="2"> |
---|
377 | <thead> |
---|
378 | <row> |
---|
379 | <entry valign="top" morerows="1">Syntax</entry> |
---|
380 | <entry>Description</entry> |
---|
381 | </row> |
---|
382 | <row> |
---|
383 | <entry>Example</entry> |
---|
384 | </row> |
---|
385 | </thead> |
---|
386 | <tbody> |
---|
387 | <row> |
---|
388 | <entry valign="top" morerows="1"><screen>operator<<</screen></entry> |
---|
389 | <entry>ostream operator for date_period. Uses facet to format time points. Typical output: [2002-Jan-01/2002-Jan-31].</entry> |
---|
390 | </row> |
---|
391 | <row> |
---|
392 | <entry><screen>std::cout << dp << std::endl;</screen></entry> |
---|
393 | </row> |
---|
394 | |
---|
395 | <row> |
---|
396 | <entry valign="top" morerows="1"><screen>operator>></screen></entry> |
---|
397 | <entry>istream operator for date_period. Uses facet to parse time points.</entry> |
---|
398 | </row> |
---|
399 | <row> |
---|
400 | <entry><screen>"[2002-Jan-01/2002-Jan-31]"</screen></entry> |
---|
401 | </row> |
---|
402 | |
---|
403 | <row> |
---|
404 | <entry valign="top" morerows="1"><screen>operator==, operator!=, |
---|
405 | operator>, operator<</screen> |
---|
406 | </entry> |
---|
407 | <entry>A full complement of comparison operators</entry> |
---|
408 | </row> |
---|
409 | <row> |
---|
410 | <entry><screen>dp1 == dp2, etc</screen></entry> |
---|
411 | </row> |
---|
412 | |
---|
413 | <row> |
---|
414 | <entry valign="top" morerows="1"><screen>operator<</screen></entry> |
---|
415 | <entry>True if dp1.end() less than dp2.begin()</entry> |
---|
416 | </row> |
---|
417 | <row> |
---|
418 | <entry><screen>dp1 < dp2, etc</screen></entry> |
---|
419 | </row> |
---|
420 | |
---|
421 | <row> |
---|
422 | <entry valign="top" morerows="1"><screen>operator></screen></entry> |
---|
423 | <entry>True if dp1.begin() greater than dp2.end()</entry> |
---|
424 | </row> |
---|
425 | <row> |
---|
426 | <entry><screen>dp1 > dp2, etc</screen></entry> |
---|
427 | </row> |
---|
428 | </tbody> |
---|
429 | </tgroup> |
---|
430 | </informaltable> |
---|
431 | |
---|
432 | </section> |
---|