time_iterators.xml 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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.posix_time.time_iterators">
  9. <title>Time Iterators</title>
  10. <link linkend="time_iter_intro">Introduction</link> --
  11. <link linkend="time_iter_header">Header</link> --
  12. <link linkend="time_iter_overview">Overview</link> --
  13. <link linkend="time_iter_operators">Operators</link>
  14. <anchor id="time_iter_intro" />
  15. <bridgehead renderas="sect3">Introduction</bridgehead>
  16. <para>
  17. Time iterators provide a mechanism for iteration through times. Time iterators are similar to <ulink url="http://www.sgi.com/tech/stl/BidirectionalIterator.html">Bidirectional Iterators</ulink>. However, time_iterators are different than standard iterators in that there is no underlying sequence, just a calculation function. In addition, time_iterators are directly comparable against instances of <link linkend="date_time.posix_time.ptime_class">class ptime</link>. Thus a second iterator for the end point of the iteration is not required, but rather a point in time can be used directly. For example, the following code iterates using a 15 minute iteration interval. The <link linkend="date_time.examples.print_hours">print hours</link> example also illustrates the use of the time_iterator.
  18. </para>
  19. <para>
  20. <programlisting>
  21. <![CDATA[
  22. #include "boost/date_time/posix_time/posix_time.hpp"
  23. #include <iostream>
  24. int
  25. main()
  26. {
  27. using namespace boost::gregorian;
  28. using namespace boost::posix_time;
  29. date d(2000,Jan,20);
  30. ptime start(d);
  31. ptime end = start + hours(1);
  32. time_iterator titr(start,minutes(15)); //increment by 15 minutes
  33. //produces 00:00:00, 00:15:00, 00:30:00, 00:45:00
  34. while (titr < end) {
  35. std::cout << to_simple_string(*titr) << std::endl;
  36. ++titr;
  37. }
  38. std::cout << "Now backward" << std::endl;
  39. //produces 01:00:00, 00:45:00, 00:30:00, 00:15:00
  40. while (titr > start) {
  41. std::cout << to_simple_string(*titr) << std::endl;
  42. --titr;
  43. }
  44. }
  45. ]]>
  46. </programlisting>
  47. </para>
  48. <anchor id="time_iter_header" />
  49. <bridgehead renderas="sect3">Header</bridgehead>
  50. <para>
  51. <programlisting>#include "boost/date_time/posix_time/posix_time.hpp" //include all types plus i/o
  52. or
  53. #include "boost/date_time/posix_time/posix_time_types.hpp" //no i/o just types</programlisting>
  54. </para>
  55. <anchor id="time_iter_overview" />
  56. <bridgehead renderas="sect3">Overview</bridgehead>
  57. <para>
  58. <informaltable frame="all">
  59. <tgroup cols="2">
  60. <thead>
  61. <row>
  62. <entry valign="top" morerows="1">Class</entry>
  63. <entry>Description</entry>
  64. </row>
  65. <row>
  66. <entry>Construction Parameters</entry>
  67. </row>
  68. </thead>
  69. <tbody>
  70. <row>
  71. <entry valign="top" morerows="1"><screen>time_iterator</screen></entry>
  72. <entry>Iterate incrementing by the specified duration.</entry>
  73. </row>
  74. <row>
  75. <entry><screen>ptime start_time, time_duration increment</screen></entry>
  76. </row>
  77. </tbody>
  78. </tgroup>
  79. </informaltable>
  80. </para>
  81. <anchor id="time_iter_operators" />
  82. <bridgehead renderas="sect3">Operators</bridgehead>
  83. <para>
  84. <informaltable frame="all">
  85. <tgroup cols="2">
  86. <thead>
  87. <row>
  88. <entry valign="top" morerows="1">Syntax</entry>
  89. <entry>Description</entry>
  90. </row>
  91. <row>
  92. <entry>Example</entry>
  93. </row>
  94. </thead>
  95. <tbody>
  96. <row>
  97. <entry valign="top" morerows="1"><screen>operator==(const ptime&amp; rhs),
  98. operator!=(const ptime&amp; rhs),
  99. operator>, operator&lt;,
  100. operator>=, operator&lt;=</screen>
  101. </entry>
  102. <entry>A full complement of comparison operators</entry>
  103. </row>
  104. <row>
  105. <entry><screen>date d(2002,Jan,1);
  106. ptime start_time(d, hours(1));
  107. //increment by 10 minutes
  108. time_iterator titr(start_time, minutes(10));
  109. ptime end_time = start_time + hours(2);
  110. if (titr == end_time) // false
  111. if (titr != end_time) // true
  112. if (titr >= end_time) // false
  113. if (titr &lt;= end_time) // true</screen>
  114. </entry>
  115. </row>
  116. <row>
  117. <entry valign="top" morerows="1"><screen>prefix increment</screen></entry>
  118. <entry>Increment the iterator by the specified duration.</entry>
  119. </row>
  120. <row>
  121. <entry><screen>//increment by 10 milli seconds
  122. time_iterator titr(start_time, milliseconds(10));
  123. ++titr; // == start_time + 10 milliseconds</screen>
  124. </entry>
  125. </row>
  126. <row>
  127. <entry valign="top" morerows="1"><screen>prefix decrement</screen></entry>
  128. <entry>Decrement the iterator by the specified time duration.</entry>
  129. </row>
  130. <row>
  131. <entry><screen>time_duration td(1,2,3);
  132. time_iterator titr(start_time, td);
  133. --titr; // == start_time - 01:02:03</screen></entry>
  134. </row>
  135. </tbody>
  136. </tgroup>
  137. </informaltable>
  138. </para>
  139. </section>