date_algorithms.xml 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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 id="date_time.gregorian.date_algorithms">
  9. <title>Date Generators/Algorithms</title>
  10. <bridgehead renderas="sect2">Date Generators/Algorithms</bridgehead>
  11. <link linkend="algo_intro">Introduction</link> --
  12. <link linkend="algo_header">Header</link> --
  13. <link linkend="algo_overview">Class Overview</link> --
  14. <link linkend="algo_func_overview">Function Overview</link>
  15. <anchor id="algo_intro" />
  16. <bridgehead renderas="sect3">Introduction</bridgehead>
  17. <para>
  18. Date algorithms or generators are tools for generating other dates or schedules of dates. A generator function starts with some part of a date such as a month and day and is supplied another part to then generate a concrete date. This allows the programmer to represent concepts such as "The first Sunday in February" and then create a concrete set of dates when provided with one or more years.
  19. <emphasis>Note</emphasis>: As of boost version 1_31_0, date generator names have been changed. Old names are still available but are no longer documented and may someday be deprecated
  20. </para>
  21. <para>Also provided are stand-alone functions for generating a date, or calculation a duration of days. These functions take a date object and a weekday object as parameters.
  22. </para>
  23. <para>All date generator classes and functions are in the boost::gregorian namespace.
  24. </para>
  25. <para>
  26. The <link linkend="date_time.examples.print_holidays">print holidays</link> example shows a detailed usage example.
  27. </para>
  28. <anchor id="algo_header" />
  29. <bridgehead renderas="sect3">Header</bridgehead>
  30. <para><programlisting>#include "boost/date_time/gregorian/gregorian.hpp"</programlisting>
  31. </para>
  32. <anchor id="algo_overview" />
  33. <bridgehead renderas="sect3">Overview</bridgehead>
  34. <informaltable frame="all">
  35. <tgroup cols="2">
  36. <thead>
  37. <row>
  38. <entry valign="top" morerows="1">Class and get_date Parameter</entry>
  39. <entry>Description</entry>
  40. </row>
  41. <row>
  42. <entry>Example</entry>
  43. </row>
  44. </thead>
  45. <tbody>
  46. <row>
  47. <entry valign="top" morerows="1"><screen>year_based_generator
  48. date get_date(greg_year year)</screen></entry>
  49. <entry>A unifying (abstract) date_generator base type for: <code>partial_date</code>, <code>nth_day_of_the_week_in_month</code>, <code>first_day_of_the_week_in_month</code>, and <code>last_day_of_the_week_in_month</code>.</entry>
  50. </row>
  51. <row>
  52. <entry>The <link linkend="date_time.examples.print_holidays">print holidays</link> example shows a detailed usage example.</entry>
  53. </row>
  54. <row>
  55. <entry valign="top" morerows="1"><screen>last_day_of_the_week_in_month(greg_weekday,
  56. greg_month)
  57. date get_date(greg_year year)</screen></entry>
  58. <entry>Calculate something like last Monday of January</entry>
  59. </row>
  60. <row>
  61. <entry><screen>last_day_of_the_week_in_month lwdm(Monday,Jan);
  62. date d = lwdm.get_date(2002);
  63. //2002-Jan-28</screen>
  64. </entry>
  65. </row>
  66. <row>
  67. <entry valign="top" morerows="1"><screen>first_day_of_the_week_in_month(greg_weekday,
  68. greg_month)
  69. date get_date(greg_year year)</screen></entry>
  70. <entry>Calculate something like first Monday of January</entry>
  71. </row>
  72. <row>
  73. <entry><screen>first_day_of_the_week_in_month fdm(Monday,Jan);
  74. date d = fdm.get_date(2002);
  75. //2002-Jan-07</screen>
  76. </entry>
  77. </row>
  78. <row>
  79. <entry valign="top" morerows="1"><screen>nth_day_of_the_week_in_month(week_num,
  80. greg_weekday,
  81. greg_month)
  82. date get_date(greg_year year)</screen></entry>
  83. <entry><code>week_num</code> is a public enum member of <code>nth_day_of_the_week_in_month</code>. Calculate something like first Monday of January, second Tuesday of March, Third Sunday of December, etc. (first through fifth are provided, fifth is the equivalent of last)</entry>
  84. </row>
  85. <row>
  86. <entry><screen>typedef nth_day_of_the_week_in_month nth_dow;
  87. nth_dow ndm(nth_dow::third, Monday,Jan);
  88. date d = ndm.get_date(2002);
  89. //2002-Jan-21</screen>
  90. </entry>
  91. </row>
  92. <row>
  93. <entry valign="top" morerows="1"><screen>partial_date(greg_day, greg_month)
  94. date get_date(greg_year year)</screen></entry>
  95. <entry>Generates a date by applying the year to the given month and day.</entry>
  96. </row>
  97. <row>
  98. <entry><screen>partial_date pd(1,Jan);
  99. date d = pd.get_date(2002);
  100. //2002-Jan-01</screen>
  101. </entry>
  102. </row>
  103. <row>
  104. <entry valign="top" morerows="1"><screen>first_day_of_the_week_after(greg_weekday)
  105. date get_date(date d)</screen></entry>
  106. <entry>Calculate something like First Sunday after Jan 1,2002</entry>
  107. </row>
  108. <row>
  109. <entry><screen>first_day_of_the_week_after fdaf(Monday);
  110. date d = fdaf.get_date(date(2002,Jan,1));
  111. //2002-Jan-07</screen>
  112. </entry>
  113. </row>
  114. <row>
  115. <entry valign="top" morerows="1"><screen>first_day_of_the_week_before(greg_weekday)
  116. date get_date(date d)</screen></entry>
  117. <entry>Calculate something like First Monday before Feb 1,2002</entry>
  118. </row>
  119. <row>
  120. <entry><screen>first_day_of_the_week_before fdbf(Monday);
  121. date d = fdbf.get_date(date(2002,Feb,1));
  122. //2002-Jan-28</screen>
  123. </entry>
  124. </row>
  125. </tbody>
  126. </tgroup>
  127. </informaltable>
  128. <anchor id="algo_func_overview" />
  129. <bridgehead renderas="sect3">Function Overview</bridgehead>
  130. <informaltable frame="all">
  131. <tgroup cols="2">
  132. <thead>
  133. <row>
  134. <entry valign="top" morerows="1">Function Prototype</entry>
  135. <entry>Description</entry>
  136. </row>
  137. <row>
  138. <entry>Example</entry>
  139. </row>
  140. </thead>
  141. <tbody>
  142. <row>
  143. <entry valign="top" morerows="1"><screen>days days_until_weekday date, greg_weekday)</screen></entry>
  144. <entry> Calculates the number of days from given date until given weekday.</entry>
  145. </row>
  146. <row>
  147. <entry><screen>date d(2004,Jun,1); // Tuesday
  148. greg_weekday gw(Friday);
  149. days_until_weekday(d, gw); // 3 days</screen>
  150. </entry>
  151. </row>
  152. <row>
  153. <entry valign="top" morerows="1"><screen>days days_before_weekday(date, greg_weekday)</screen></entry>
  154. <entry> Calculates the number of day from given date to previous given weekday.</entry>
  155. </row>
  156. <row>
  157. <entry><screen>date d(2004,Jun,1); // Tuesday
  158. greg_weekday gw(Friday);
  159. days_before_weekday(d, gw); // 4 days</screen>
  160. </entry>
  161. </row>
  162. <row>
  163. <entry valign="top" morerows="1"><screen>date next_weekday(date, greg_weekday)</screen></entry>
  164. <entry> Generates a date object representing the date of the following weekday from the given date. Can return the given date.</entry>
  165. </row>
  166. <row>
  167. <entry><screen>date d(2004,Jun,1); // Tuesday
  168. greg_weekday gw1(Friday);
  169. greg_weekday gw2(Tuesday);
  170. next_weekday(d, gw1); // 2004-Jun-4
  171. next_weekday(d, gw2); // 2004-Jun-1
  172. </screen>
  173. </entry>
  174. </row>
  175. <row>
  176. <entry valign="top" morerows="1"><screen>date previous_weekday(date, greg_weekday)</screen></entry>
  177. <entry> Generates a date object representing the date of the previous weekday from the given date. Can return the given date. </entry>
  178. </row>
  179. <row>
  180. <entry><screen>date d(2004,Jun,1); // Tuesday
  181. greg_weekday gw1(Friday);
  182. greg_weekday gw2(Tuesday);
  183. previous_weekday(d, gw1); // 2004-May-28
  184. previous_weekday(d, gw2); // 2004-Jun-1
  185. </screen>
  186. </entry>
  187. </row>
  188. </tbody>
  189. </tgroup>
  190. </informaltable>
  191. </section>