date_duration.xml 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. <!-- Copyright (c) 2001-2005 CrystalClear Software, Inc.
  5. Subject to the Boost Software License, Version 1.0.
  6. (See accompanying file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
  7. -->
  8. <section xmlns:xi="http://www.w3.org/2001/XInclude" id="date_time.gregorian.date_duration">
  9. <title>Date Duration (aka Days)</title>
  10. <link linkend="duration_intro">Introduction</link> --
  11. <link linkend="duration_header">Header</link> --
  12. <link linkend="duration_construction">Construction</link> --
  13. <link linkend="duration_accessors">Accessors</link> --
  14. <link linkend="duration_operators">Operators</link> --
  15. <link linkend="additional_duration_types">Additional Duration Types</link>
  16. <anchor id="duration_intro" />
  17. <bridgehead renderas="sect3">Introduction</bridgehead>
  18. <para>
  19. 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.
  20. </para>
  21. <para>
  22. 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.
  23. </para>
  24. <anchor id="duration_header" />
  25. <bridgehead renderas="sect3">Header</bridgehead>
  26. <para>
  27. <programlisting>#include "boost/date_time/gregorian/gregorian.hpp" //include all types plus i/o
  28. or
  29. #include "boost/date_time/gregorian/gregorian_types.hpp" //no i/o just types</programlisting>
  30. </para>
  31. <anchor id="duration_construction" />
  32. <bridgehead renderas="sect3">Construction</bridgehead>
  33. <informaltable frame="all">
  34. <tgroup cols="2">
  35. <thead>
  36. <row>
  37. <entry valign="top" morerows="1">Syntax</entry>
  38. <entry>Description</entry>
  39. </row>
  40. <row>
  41. <entry>Example</entry>
  42. </row>
  43. </thead>
  44. <tbody>
  45. <row>
  46. <entry valign="top" morerows="1"><screen>date_duration(long)</screen></entry>
  47. <entry>Create a duration count.</entry>
  48. </row>
  49. <row>
  50. <entry><screen>date_duration dd(3); //3 days</screen></entry>
  51. </row>
  52. <row>
  53. <entry valign="top" morerows="1"><screen>days(special_values sv)</screen></entry>
  54. <entry>Constructor for infinities, not-a-date-time, max_date_time, and min_date_time</entry>
  55. </row>
  56. <row>
  57. <entry><screen>days dd1(neg_infin);
  58. days dd2(pos_infin);
  59. days dd3(not_a_date_time);
  60. days dd4(max_date_time);
  61. days dd5(min_date_time);</screen></entry>
  62. </row>
  63. </tbody>
  64. </tgroup>
  65. </informaltable>
  66. <anchor id="duration_accessors" />
  67. <bridgehead renderas="sect3">Accessors</bridgehead>
  68. <informaltable frame="all">
  69. <tgroup cols="2">
  70. <thead>
  71. <row>
  72. <entry valign="top" morerows="1">Syntax</entry>
  73. <entry>Description</entry>
  74. </row>
  75. <row>
  76. <entry>Example</entry>
  77. </row>
  78. </thead>
  79. <tbody>
  80. <row>
  81. <entry valign="top" morerows="1"><screen>long days() const</screen></entry>
  82. <entry>Get the day count.</entry>
  83. </row>
  84. <row>
  85. <entry><screen>date_duration dd(3); dd.days() --> 3</screen></entry>
  86. </row>
  87. <row>
  88. <entry valign="top" morerows="1"><screen>bool is_negative() const</screen></entry>
  89. <entry>True if number of days is less than zero.</entry>
  90. </row>
  91. <row>
  92. <entry><screen>date_duration dd(-1); dd.is_negative() --> true</screen></entry>
  93. </row>
  94. <row>
  95. <entry valign="top" morerows="1"><screen>static date_duration unit()</screen></entry>
  96. <entry>Return smallest possible unit of duration type.</entry>
  97. </row>
  98. <row>
  99. <entry><screen>date_duration::unit() --> date_duration(1)</screen></entry>
  100. </row>
  101. <row>
  102. <entry valign="top" morerows="1"><screen>bool is_special() const</screen></entry>
  103. <entry>Returns true if days is any <code>special_value</code></entry>
  104. </row>
  105. <row>
  106. <entry><screen>days dd(pos_infin);
  107. days dd2(not_a_date_time);
  108. days dd3(25);
  109. dd.is_special(); // --> true
  110. dd2.is_special(); // --> true
  111. dd3.is_special(); // --> false</screen></entry>
  112. </row>
  113. </tbody>
  114. </tgroup>
  115. </informaltable>
  116. <anchor id="duration_operators" />
  117. <bridgehead renderas="sect3">Operators</bridgehead>
  118. <informaltable frame="all">
  119. <tgroup cols="2">
  120. <thead>
  121. <row>
  122. <entry valign="top" morerows="1">Syntax</entry>
  123. <entry>Description</entry>
  124. </row>
  125. <row>
  126. <entry>Example</entry>
  127. </row>
  128. </thead>
  129. <tbody>
  130. <row>
  131. <entry valign="top" morerows="1"><screen>operator&lt;&lt;, operator&gt;&gt;</screen></entry>
  132. <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>
  133. </row>
  134. <row>
  135. <entry><screen>date d(not_a_date_time);
  136. stringstream ss("2002-Jan-01");
  137. ss &gt;&gt; d;
  138. std::cout &lt;&lt; d; // "2002-Jan-01"
  139. </screen>
  140. </entry>
  141. </row>
  142. <row>
  143. <entry valign="top" morerows="1"><screen>operator==, operator!=,
  144. operator>, operator&lt;,
  145. operator>=, operator&lt;=</screen>
  146. </entry>
  147. <entry>A full complement of comparison operators</entry>
  148. </row>
  149. <row>
  150. <entry><screen>dd1 == dd2, etc</screen></entry>
  151. </row>
  152. <row>
  153. <entry valign="top" morerows="1"><screen>date_duration operator+(date_duration) const</screen></entry>
  154. <entry>Add date durations.</entry>
  155. </row>
  156. <row>
  157. <entry><screen>date_duration dd1(3);
  158. date_duration dd2(5);
  159. date_duration dd3 = dd1 + dd2;</screen>
  160. </entry>
  161. </row>
  162. <row>
  163. <entry valign="top" morerows="1"><screen>date_duration operator-(date_duration) const</screen></entry>
  164. <entry>Subtract durations.</entry>
  165. </row>
  166. <row>
  167. <entry><screen>date_duration dd1(3);
  168. date_duration dd2(5);
  169. date_duration dd3 = dd1 - dd2;</screen>
  170. </entry>
  171. </row>
  172. </tbody>
  173. </tgroup>
  174. </informaltable>
  175. <anchor id="additional_duration_types" />
  176. <bridgehead renderas="sect3">Additional Duration Types</bridgehead>
  177. <para>These additional types are logical representations of spans of days.</para>
  178. <informaltable frame="all">
  179. <tgroup cols="2">
  180. <thead>
  181. <row>
  182. <entry valign="top" morerows="1">Syntax</entry>
  183. <entry>Description</entry>
  184. </row>
  185. <row>
  186. <entry>Example</entry>
  187. </row>
  188. </thead>
  189. <tbody>
  190. <row>
  191. <entry valign="top" morerows="1"><screen>months(int num_of_months)</screen></entry>
  192. <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>
  193. </row>
  194. <row>
  195. <entry><screen>months single(1);
  196. date leap_year(2004,Jan,31);
  197. date norm_year(2005,Jan,31);
  198. leap_year + single; // => 2004-Feb-29
  199. norm_year + single; // => 2005-Feb-28
  200. date(2005,Jan,1) + single; // => 2005-Feb-01
  201. date(2005,Feb,1) + single; // => 2005-Mar-01
  202. date(2005,Feb,28) + single; // => 2005-Mar-31</screen></entry>
  203. </row>
  204. <row>
  205. <entry valign="top" morerows="1"><screen>years(int num_of_years)</screen></entry>
  206. <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>
  207. </row>
  208. <row>
  209. <entry><screen>years single(1);
  210. date(2003,Feb,28) + single;
  211. // results in => 2004-Feb-29
  212. date(2004,Feb,29) + single;
  213. // results in => 2005-Feb-28</screen></entry>
  214. </row>
  215. <row>
  216. <entry valign="top" morerows="1"><screen>weeks(int num_of_weeks)</screen></entry>
  217. <entry>A duration type representing a number of <code>n * 7</code> days.</entry>
  218. </row>
  219. <row>
  220. <entry><screen>weeks single(1);
  221. date(2005,Jan,1) + single; // => 2005-Jan-08</screen></entry>
  222. </row>
  223. </tbody>
  224. </tgroup>
  225. </informaltable>
  226. <xi:include href="snap_to_details.xml"/>
  227. </section>