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">
5 <!-- Copyright (c) 2001-2005 CrystalClear Software, Inc.
6 Subject to the Boost Software License, Version 1.0.
7 (See accompanying file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
10 <section xmlns:
xi=
"http://www.w3.org/2001/XInclude" id=
"date_time.gregorian.date_duration">
11 <title>Date Duration (aka Days)
</title>
13 <link linkend=
"duration_intro">Introduction
</link> --
14 <link linkend=
"duration_header">Header
</link> --
15 <link linkend=
"duration_construction">Construction
</link> --
16 <link linkend=
"duration_accessors">Accessors
</link> --
17 <link linkend=
"duration_operators">Operators
</link> --
18 <link linkend=
"additional_duration_types">Additional Duration Types
</link>
20 <anchor id=
"duration_intro" />
21 <bridgehead renderas=
"sect3">Introduction
</bridgehead>
23 The class boost::gregorian::date_duration is a simple day count used for arithmetic with
<link linkend=
"date_time.gregorian.date_class">gregorian::date
</link>. A duration can be either positive or negative.
26 As of version
1_32 the date_duration class has been typedef'd as days in the boost::gregorian namespace. Throughout the examples you will find days used instead of date_duration.
29 <anchor id=
"duration_header" />
30 <bridgehead renderas=
"sect3">Header
</bridgehead>
32 <programlisting>#include
"boost/date_time/gregorian/gregorian.hpp" //include all types plus i/o
34 #include
"boost/date_time/gregorian/gregorian_types.hpp" //no i/o just types
</programlisting>
37 <anchor id=
"duration_construction" />
38 <bridgehead renderas=
"sect3">Construction
</bridgehead>
39 <informaltable frame=
"all">
43 <entry valign=
"top" morerows=
"1">Syntax
</entry>
44 <entry>Description
</entry>
47 <entry>Example
</entry>
52 <entry valign=
"top" morerows=
"1"><screen>date_duration(long)
</screen></entry>
53 <entry>Create a duration count.
</entry>
56 <entry><screen>date_duration dd(
3); //
3 days
</screen></entry>
60 <entry valign=
"top" morerows=
"1"><screen>days(special_values sv)
</screen></entry>
61 <entry>Constructor for infinities, not-a-date-time, max_date_time, and min_date_time
</entry>
64 <entry><screen>days dd1(neg_infin);
66 days dd3(not_a_date_time);
67 days dd4(max_date_time);
68 days dd5(min_date_time);
</screen></entry>
75 <anchor id=
"duration_accessors" />
76 <bridgehead renderas=
"sect3">Accessors
</bridgehead>
77 <informaltable frame=
"all">
81 <entry valign=
"top" morerows=
"1">Syntax
</entry>
82 <entry>Description
</entry>
85 <entry>Example
</entry>
90 <entry valign=
"top" morerows=
"1"><screen>long days() const
</screen></entry>
91 <entry>Get the day count.
</entry>
94 <entry><screen>date_duration dd(
3); dd.days() -->
3</screen></entry>
98 <entry valign=
"top" morerows=
"1"><screen>bool is_negative() const
</screen></entry>
99 <entry>True if number of days is less than zero.
</entry>
102 <entry><screen>date_duration dd(-
1); dd.is_negative() --> true
</screen></entry>
106 <entry valign=
"top" morerows=
"1"><screen>static date_duration unit()
</screen></entry>
107 <entry>Return smallest possible unit of duration type.
</entry>
110 <entry><screen>date_duration::unit() --> date_duration(
1)
</screen></entry>
114 <entry valign=
"top" morerows=
"1"><screen>bool is_special() const
</screen></entry>
115 <entry>Returns true if days is any
<code>special_value
</code></entry>
118 <entry><screen>days dd(pos_infin);
119 days dd2(not_a_date_time);
121 dd.is_special(); // --> true
122 dd2.is_special(); // --> true
123 dd3.is_special(); // --> false
</screen></entry>
130 <anchor id=
"duration_operators" />
131 <bridgehead renderas=
"sect3">Operators
</bridgehead>
132 <informaltable frame=
"all">
136 <entry valign=
"top" morerows=
"1">Syntax
</entry>
137 <entry>Description
</entry>
140 <entry>Example
</entry>
145 <entry valign=
"top" morerows=
"1"><screen>operator
<<, operator
>></screen></entry>
146 <entry>Streaming operators.
<emphasis role=
"strong">Note:
</emphasis> As of version
1.33, streaming operations have been greatly improved. See
<link linkend=
"date_time.date_time_io">Date Time IO System
</link> for more details (including exceptions and error conditions).
</entry>
149 <entry><screen>date d(not_a_date_time);
150 stringstream ss(
"2002-Jan-01");
152 std::cout
<< d; //
"2002-Jan-01"
158 <entry valign=
"top" morerows=
"1"><screen>operator==, operator!=,
159 operator
>, operator
<,
160 operator
>=, operator
<=
</screen>
162 <entry>A full complement of comparison operators
</entry>
165 <entry><screen>dd1 == dd2, etc
</screen></entry>
169 <entry valign=
"top" morerows=
"1"><screen>date_duration operator+(date_duration) const
</screen></entry>
170 <entry>Add date durations.
</entry>
173 <entry><screen>date_duration dd1(
3);
174 date_duration dd2(
5);
175 date_duration dd3 = dd1 + dd2;
</screen>
180 <entry valign=
"top" morerows=
"1"><screen>date_duration operator-(date_duration) const
</screen></entry>
181 <entry>Subtract durations.
</entry>
184 <entry><screen>date_duration dd1(
3);
185 date_duration dd2(
5);
186 date_duration dd3 = dd1 - dd2;
</screen>
193 <anchor id=
"additional_duration_types" />
194 <bridgehead renderas=
"sect3">Additional Duration Types
</bridgehead>
195 <para>These additional types are logical representations of spans of days.
</para>
196 <informaltable frame=
"all">
200 <entry valign=
"top" morerows=
"1">Syntax
</entry>
201 <entry>Description
</entry>
204 <entry>Example
</entry>
209 <entry valign=
"top" morerows=
"1"><screen>months(int num_of_months)
</screen></entry>
210 <entry>A logical month representation. Depending on the usage, this
<code>months
</code> object may cover a span of
28 to
31 days. The objects also use a snap to end-of-month behavior when used in conjunction with a date that is the last day of a given month.
<emphasis role=
"strong">WARNING: this behavior may lead to unexpected results.
</emphasis> See:
<link linkend=
"snap_to_details">Reversibility of Operations Pitfall
</link> for complete details and alternatives.
</entry>
213 <entry><screen>months single(
1);
214 date leap_year(
2004,Jan,
31);
215 date norm_year(
2005,Jan,
31);
216 leap_year + single; // =
> 2004-Feb-
29
217 norm_year + single; // =
> 2005-Feb-
28
218 date(
2005,Jan,
1) + single; // =
> 2005-Feb-
01
219 date(
2005,Feb,
1) + single; // =
> 2005-Mar-
01
220 date(
2005,Feb,
28) + single; // =
> 2005-Mar-
31</screen></entry>
224 <entry valign=
"top" morerows=
"1"><screen>years(int num_of_years)
</screen></entry>
225 <entry>A logical representation of a year. The
<code>years
</code> object has the same behavior as the
<code>months
</code> objects with regards to the end-of-the-month.
</entry>
228 <entry><screen>years single(
1);
229 date(
2003,Feb,
28) + single;
230 // results in =
> 2004-Feb-
29
231 date(
2004,Feb,
29) + single;
232 // results in =
> 2005-Feb-
28</screen></entry>
236 <entry valign=
"top" morerows=
"1"><screen>weeks(int num_of_weeks)
</screen></entry>
237 <entry>A duration type representing a number of
<code>n *
7</code> days.
</entry>
240 <entry><screen>weeks single(
1);
241 date(
2005,Jan,
1) + single; // =
> 2005-Jan-
08</screen></entry>
248 <xi:include href=
"snap_to_details.xml"/>