time_period.xml 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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-2006 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_period">
  9. <title>Time Period</title>
  10. <link linkend="time_period_intro">Introduction</link> --
  11. <link linkend="time_period_header">Header</link> --
  12. <link linkend="time_period_constr">Construction</link> --
  13. <link linkend="time_period_mutators">Mutators</link> --
  14. <link linkend="time_period_accessors">Accessors</link> --
  15. <link linkend="time_period_to_string">Conversion To String</link> --
  16. <link linkend="time_period_operators">Operators</link>
  17. <anchor id="time_period_intro" />
  18. <bridgehead renderas="sect3">Introduction</bridgehead>
  19. <para>
  20. The class boost::posix_time::time_period provides direct representation for ranges between two times. Periods provide the ability to simplify some types of calculations by simplifying the conditional logic of the program.
  21. </para>
  22. <para>
  23. 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.
  24. </para>
  25. <para>
  26. The <link linkend="date_time.examples.time_periods">time periods example</link> provides an example of using time periods.
  27. </para>
  28. <anchor id="time_period_header" />
  29. <bridgehead renderas="sect3">Header</bridgehead>
  30. <para>
  31. <programlisting>#include "boost/date_time/posix_time/posix_time.hpp" //include all types plus i/o
  32. or
  33. #include "boost/date_time/posix_time/posix_time_types.hpp" //no i/o just types</programlisting>
  34. </para>
  35. <anchor id="time_period_constr" />
  36. <bridgehead renderas="sect3">Construction</bridgehead>
  37. <para>
  38. <informaltable frame="all">
  39. <tgroup cols="2">
  40. <thead>
  41. <row>
  42. <entry valign="top" morerows="1">Syntax</entry>
  43. <entry>Description</entry>
  44. </row>
  45. <row>
  46. <entry>Example</entry>
  47. </row>
  48. </thead>
  49. <tbody>
  50. <row>
  51. <entry valign="top" morerows="1"><screen>time_period(ptime,
  52. ptime)</screen></entry>
  53. <entry> Create a period as [begin, end). If end is &lt;= begin then the period will be defined as invalid.</entry>
  54. </row>
  55. <row>
  56. <entry><screen>date d(2002,Jan,01);
  57. ptime t1(d, seconds(10)); //10 sec after midnight
  58. ptime t2(d, hours(10)); //10 hours after midnight
  59. time_period tp(t1, t2);</screen>
  60. </entry>
  61. </row>
  62. <row>
  63. <entry valign="top" morerows="1"><screen>time_period(ptime,
  64. time_duration)</screen></entry>
  65. <entry> Create a period as [begin, begin+len) where end would be begin+len. If len is &lt;= zero then the period will be defined as invalid.</entry>
  66. </row>
  67. <row>
  68. <entry><screen>date d(2002,Jan,01);
  69. ptime t(d, seconds(10)); //10 sec after midnight
  70. time_period tp(t, hours(3));</screen>
  71. </entry>
  72. </row>
  73. <row>
  74. <entry valign="top" morerows="1"><screen>time_period(time_period rhs)</screen></entry>
  75. <entry> Copy constructor</entry>
  76. </row>
  77. <row>
  78. <entry><screen>time_period tp1(tp);</screen></entry>
  79. </row>
  80. </tbody>
  81. </tgroup>
  82. </informaltable>
  83. </para>
  84. <anchor id="time_period_mutators" />
  85. <bridgehead renderas="sect3">Mutators</bridgehead>
  86. <para>
  87. <informaltable frame="all">
  88. <tgroup cols="2">
  89. <thead>
  90. <row>
  91. <entry valign="top" morerows="1">Syntax</entry>
  92. <entry>Description</entry>
  93. </row>
  94. <row>
  95. <entry>Example</entry>
  96. </row>
  97. </thead>
  98. <tbody>
  99. <row>
  100. <entry valign="top" morerows="1"><screen>time_period shift(time_duration)</screen></entry>
  101. <entry>Add duration to both begin and end.</entry>
  102. </row>
  103. <row>
  104. <entry>
  105. <screen>
  106. time_period tp(ptime(date(2005,Jan,1),hours(1)), hours(2));
  107. tp.shift(minutes(5));
  108. // tp == 2005-Jan-01 01:05:00 to 2005-Jan-01 03:05:00
  109. </screen>
  110. </entry>
  111. </row>
  112. <row>
  113. <entry valign="top" morerows="1"><screen>time_period expand(days)</screen></entry>
  114. <entry>Subtract duration from begin and add duration to end.</entry>
  115. </row>
  116. <row>
  117. <entry>
  118. <screen>
  119. time_period tp(ptime(date(2005,Jan,1),hours(1)), hours(2));
  120. tp.expand(minutes(5));
  121. // tp == 2005-Jan-01 00:55:00 to 2005-Jan-01 03:05:00
  122. </screen>
  123. </entry>
  124. </row>
  125. </tbody>
  126. </tgroup>
  127. </informaltable>
  128. </para>
  129. <anchor id="time_period_accessors" />
  130. <bridgehead renderas="sect3">Accessors</bridgehead>
  131. <para>
  132. <informaltable frame="all">
  133. <tgroup cols="2">
  134. <thead>
  135. <row>
  136. <entry valign="top" morerows="1">Syntax</entry>
  137. <entry>Description</entry>
  138. </row>
  139. <row>
  140. <entry>Example</entry>
  141. </row>
  142. </thead>
  143. <tbody>
  144. <row>
  145. <entry valign="top" morerows="1"><screen>ptime begin()</screen></entry>
  146. <entry>Return first time of period.</entry>
  147. </row>
  148. <row>
  149. <entry><screen>date d(2002,Jan,01);
  150. ptime t1(d, seconds(10)); //10 sec after midnight
  151. ptime t2(d, hours(10)); //10 hours after midnight
  152. time_period tp(t1, t2);
  153. tp.begin(); // --> 2002-Jan-01 00:00:10</screen>
  154. </entry>
  155. </row>
  156. <row>
  157. <entry valign="top" morerows="1"><screen>ptime last()</screen></entry>
  158. <entry>Return last time in the period</entry>
  159. </row>
  160. <row>
  161. <entry><screen>date d(2002,Jan,01);
  162. ptime t1(d, seconds(10)); //10 sec after midnight
  163. ptime t2(d, hours(10)); //10 hours after midnight
  164. time_period tp(t1, t2);
  165. tp.last();// --> 2002-Jan-01 09:59:59.999999999</screen>
  166. </entry>
  167. </row>
  168. <row>
  169. <entry valign="top" morerows="1"><screen>ptime end()</screen></entry>
  170. <entry> Return one past the last in period</entry>
  171. </row>
  172. <row>
  173. <entry><screen>date d(2002,Jan,01);
  174. ptime t1(d, seconds(10)); //10 sec after midnight
  175. ptime t2(d, hours(10)); //10 hours after midnight
  176. time_period tp(t1, t2);
  177. tp.last(); // --> 2002-Jan-01 10:00:00</screen>
  178. </entry>
  179. </row>
  180. <row>
  181. <entry valign="top" morerows="1"><screen>time_duration length()</screen></entry>
  182. <entry>Return the length of the time period.</entry>
  183. </row>
  184. <row>
  185. <entry><screen>date d(2002,Jan,01);
  186. ptime t1(d); //midnight
  187. time_period tp(t1, hours(1));
  188. tp.length() --> 1 hour</screen>
  189. </entry>
  190. </row>
  191. <row>
  192. <entry valign="top" morerows="1"><screen>bool is_null()</screen></entry>
  193. <entry>True if period is not well formed. eg: end is less than or equal to begin.</entry>
  194. </row>
  195. <row>
  196. <entry><screen>date d(2002,Jan,01);
  197. ptime t1(d, hours(12)); // noon on Jan 1st
  198. ptime t2(d, hours(9)); // 9am on Jan 1st
  199. time_period tp(t1, t2);
  200. tp.is_null(); // true</screen>
  201. </entry>
  202. </row>
  203. <row>
  204. <entry valign="top" morerows="1"><screen>bool contains(ptime)</screen></entry>
  205. <entry>True if ptime is within the period. Zero length periods cannot contain any points.</entry>
  206. </row>
  207. <row>
  208. <entry><screen>date d(2002,Jan,01);
  209. ptime t1(d, seconds(10)); //10 sec after midnight
  210. ptime t2(d, hours(10)); //10 hours after midnight
  211. ptime t3(d, hours(2)); //2 hours after midnight
  212. time_period tp(t1, t2);
  213. tp.contains(t3); // true
  214. time_period tp2(t1, t1);
  215. tp2.contains(t1); // false</screen>
  216. </entry>
  217. </row>
  218. <row>
  219. <entry valign="top" morerows="1"><screen>bool contains(time_period)</screen></entry>
  220. <entry>True if period is within the period</entry>
  221. </row>
  222. <row>
  223. <entry><screen>time_period tp1(ptime(d,hours(1)),
  224. ptime(d,hours(12)));
  225. time_period tp2(ptime(d,hours(2)),
  226. ptime(d,hours(4)));
  227. tp1.contains(tp2); // --> true
  228. tp2.contains(tp1); // --> false</screen>
  229. </entry>
  230. </row>
  231. <row>
  232. <entry valign="top" morerows="1"><screen>bool intersects(time_period)</screen></entry>
  233. <entry> True if periods overlap</entry>
  234. </row>
  235. <row>
  236. <entry><screen>time_period tp1(ptime(d,hours(1)),
  237. ptime(d,hours(12)));
  238. time_period tp2(ptime(d,hours(2)),
  239. ptime(d,hours(4)));
  240. tp2.intersects(tp1); // --> true</screen>
  241. </entry>
  242. </row>
  243. <row>
  244. <entry valign="top" morerows="1"><screen>time_period intersection(time_period)</screen></entry>
  245. <entry>Calculate the intersection of 2 periods. Null if no intersection.</entry>
  246. </row>
  247. <row>
  248. <entry></entry>
  249. </row>
  250. <row>
  251. <entry valign="top" morerows="1"><screen>time_period merge(time_period)</screen></entry>
  252. <entry>Returns union of two periods. Null if no intersection.</entry>
  253. </row>
  254. <row>
  255. <entry></entry>
  256. </row>
  257. <row>
  258. <entry valign="top" morerows="1"><screen>time_period span(time_period)</screen></entry>
  259. <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>
  260. </row>
  261. <row>
  262. <entry></entry>
  263. </row>
  264. </tbody>
  265. </tgroup>
  266. </informaltable>
  267. </para>
  268. <anchor id="time_period_to_string" />
  269. <bridgehead renderas="sect3">Conversion To String</bridgehead>
  270. <para>
  271. <informaltable frame="all">
  272. <tgroup cols="2">
  273. <thead>
  274. <row>
  275. <entry valign="top" morerows="1">Syntax</entry>
  276. <entry>Description</entry>
  277. </row>
  278. <row>
  279. <entry>Example</entry>
  280. </row>
  281. </thead>
  282. <tbody>
  283. <row>
  284. <entry valign="top" morerows="1"><screen>std::string
  285. to_simple_string(time_period dp)</screen></entry>
  286. <entry>To <code>[YYYY-mmm-DD hh:mm:ss.fffffffff/YYYY-mmm-DD hh:mm:ss.fffffffff]</code> string where <code>mmm</code> is 3 char month name.</entry>
  287. </row>
  288. <row>
  289. <entry><screen>[2002-Jan-01 01:25:10.000000001/
  290. 2002-Jan-31 01:25:10.123456789]
  291. // string occupies one line</screen></entry>
  292. </row>
  293. </tbody>
  294. </tgroup>
  295. </informaltable>
  296. </para>
  297. <anchor id="time_period_operators" />
  298. <bridgehead renderas="sect3">Operators</bridgehead>
  299. <para>
  300. <informaltable frame="all">
  301. <tgroup cols="2">
  302. <thead>
  303. <row>
  304. <entry valign="top" morerows="1">Syntax</entry>
  305. <entry>Description</entry>
  306. </row>
  307. <row>
  308. <entry>Example</entry>
  309. </row>
  310. </thead>
  311. <tbody>
  312. <row>
  313. <entry valign="top" morerows="1"><screen>operator&lt;&lt;</screen></entry>
  314. <entry>Output streaming operator for time duration. Uses facet to output [date time_of_day/date time_of_day]. The default is format is <code>[YYYY-mmm-DD hh:mm:ss.fffffffff/YYYY-mmm-DD hh:mm:ss.fffffffff]</code> string where <code>mmm</code> is 3 char month name and the fractional seconds are left out when zero.</entry>
  315. </row>
  316. <row>
  317. <entry><screen>[2002-Jan-01 01:25:10.000000001/ \
  318. 2002-Jan-31 01:25:10.123456789]</screen></entry>
  319. </row>
  320. <row>
  321. <entry valign="top" morerows="1"><screen>operator&gt;&gt;</screen></entry>
  322. <entry>Input streaming operator for time duration. Uses facet to read [date time_of_day/date time_of_day]. The default is format is <code>[YYYY-mmm-DD hh:mm:ss.fffffffff/YYYY-mmm-DD hh:mm:ss.fffffffff]</code> string where <code>mmm</code> is 3 char month name and the fractional seconds are left out when zero.</entry>
  323. </row>
  324. <row>
  325. <entry><screen>[2002-Jan-01 01:25:10.000000001/ \
  326. 2002-Jan-31 01:25:10.123456789]</screen></entry>
  327. </row>
  328. <row>
  329. <entry valign="top" morerows="1"><screen>operator==, operator!=</screen></entry>
  330. <entry>Equality operators. Periods are equal if p1.begin == p2.begin &amp;&amp; p1.last == p2.last</entry>
  331. </row>
  332. <row>
  333. <entry><screen>if (tp1 == tp2) {...</screen></entry>
  334. </row>
  335. <row>
  336. <entry valign="top" morerows="1"><screen>operator&lt;</screen></entry>
  337. <entry>Ordering with no overlap. True if tp1.end() less than tp2.begin()</entry>
  338. </row>
  339. <row>
  340. <entry><screen>if (tp1 &lt; tp2) {...</screen></entry>
  341. </row>
  342. <row>
  343. <entry valign="top" morerows="1"><screen>operator&gt;</screen></entry>
  344. <entry>Ordering with no overlap. True if tp1.begin() greater than tp2.end()</entry>
  345. </row>
  346. <row>
  347. <entry><screen>if (tp1 > tp2) {... etc</screen></entry>
  348. </row>
  349. <row>
  350. <entry valign="top" morerows="1"><screen>operator&lt;=, operator&gt;=</screen></entry>
  351. <entry>Defined in terms of the other operators.</entry>
  352. </row>
  353. <row>
  354. <entry></entry>
  355. </row>
  356. </tbody>
  357. </tgroup>
  358. </informaltable>
  359. </para>
  360. </section>