usage_examples.xml 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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.examples.general_usage_examples">
  9. <title>General Usage Examples</title>
  10. <para>
  11. The following provides some sample usage of dates.
  12. See <link linkend="date_time.gregorian">Date Programming</link>
  13. for more details.
  14. <programlisting><emphasis role="keyword">using namespace</emphasis> boost::gregorian;
  15. date weekstart(<emphasis role="number">2002</emphasis>,Feb,<emphasis role="number">1</emphasis>);
  16. date weekend = weekstart + weeks(<emphasis role="number">1</emphasis>);
  17. date d2 = d1 + days(<emphasis role="number">5</emphasis>);
  18. date today = day_clock::local_day();
  19. if (d2 &gt;= today) {} <emphasis role="comment">//date comparison operators</emphasis>
  20. date_period thisWeek(d1,d2);
  21. <emphasis role="keyword">if</emphasis> (thisWeek.contains(today)) {}<emphasis role="comment">//do something
  22. //iterate and print the week</emphasis>
  23. day_iterator itr(weekstart);
  24. <emphasis role="keyword">while</emphasis> (itr &lt;= weekend) {
  25. std::cout &lt;&lt; (*itr) &lt;&lt; std::endl;
  26. ++itr;
  27. }
  28. <emphasis role="comment">//input streaming</emphasis>
  29. std::stringstream ss(<emphasis role="string">"2004-Jan-1"</emphasis>);
  30. ss &gt;&gt; d3;
  31. <emphasis role="comment">//date generator functions</emphasis>
  32. date d5 = next_weekday(d4, Sunday); <emphasis role="comment">//calculate Sunday following d4
  33. //US labor day is first Monday in Sept</emphasis>
  34. <emphasis role="keyword">typedef</emphasis> nth_day_of_the_week_in_month nth_dow;
  35. nth_dow labor_day(nth_dow::first,Monday, Sep);
  36. <emphasis role="comment">//calculate a specific date for 2004 from functor</emphasis>
  37. date d6 = labor_day.get_date(<emphasis role="number">2004</emphasis>);
  38. </programlisting>
  39. The following provides some example code using times.
  40. See <link linkend="date_time.posix_time">Time Programming</link>
  41. for more details.
  42. <programlisting><emphasis role="keyword">using namespace</emphasis> boost::posix_time;
  43. date d(<emphasis role="number">2002</emphasis>,Feb,<emphasis role="number">1</emphasis>); <emphasis role="comment">//an arbitrary date</emphasis>
  44. ptime t1(d, hours(<emphasis role="number">5</emphasis>)+nanosec(<emphasis role="number">100</emphasis>)); <emphasis role="comment">//date + time of day offset</emphasis>
  45. ptime t2 = t1 - minutes(<emphasis role="number">4</emphasis>)+seconds(<emphasis role="number">2</emphasis>);
  46. ptime now = second_clock::local_time(); <emphasis role="comment">//use the clock</emphasis>
  47. date today = now.date(); <emphasis role="comment">//Get the date part out of the time</emphasis>
  48. date tomorrow = today + date_duration(<emphasis role="number">1</emphasis>);
  49. ptime tomorrow_start(tomorrow); <emphasis role="comment">//midnight
  50. //input streaming</emphasis>
  51. std::stringstream ss(<emphasis role="string">"2004-Jan-1 05:21:33.20"</emphasis>);
  52. ss &gt;&gt; t2;
  53. <emphasis role="comment">//starting at current time iterator adds by one hour</emphasis>
  54. time_iterator titr(now,hours(<emphasis role="number">1</emphasis>));
  55. <emphasis role="keyword">for</emphasis> (; titr &lt; tomorrow_start; ++titr) {
  56. std::cout &lt;&lt; (*titr) &lt;&lt; std::endl;
  57. }
  58. </programlisting>
  59. </para>
  60. <para>
  61. The following provides some example code using times.
  62. See <link linkend="date_time.local_time">Local Time Programming</link>
  63. for more details.
  64. <programlisting>
  65. <emphasis role="keyword">using namespace</emphasis> boost::local_time;
  66. <emphasis role="comment">//setup some timezones for creating and adjusting times
  67. //first time zone uses the time zone file for regional timezone definitions</emphasis>
  68. tz_database tz_db;
  69. tz_db.load_from_file(<emphasis role="string">"date_time_zonespec.csv"</emphasis>);
  70. time_zone_ptr nyc_tz = tz_db.time_zone_from_region(<emphasis role="string">"America/New_York"</emphasis>);
  71. <emphasis role="comment">//This timezone uses a posix time zone string definition to create a time zone</emphasis>
  72. time_zone_ptr phx_tz(new posix_time_zone(<emphasis role="string">"MST-07:00:00"</emphasis>));
  73. <emphasis role="comment">//local departure time in phoenix is 11 pm on April 2 2005
  74. // Note that New York changes to daylight savings on Apr 3 at 2 am)</emphasis>
  75. local_date_time phx_departure(date(<emphasis role="number">2005</emphasis>, Apr, <emphasis role="number">2</emphasis>), hours(<emphasis role="number">23</emphasis>), phx_tz,
  76. local_date_time::NOT_DATE_TIME_ON_ERROR);
  77. time_duration flight_length = hours(<emphasis role="number">4</emphasis>) + minutes(<emphasis role="number">30</emphasis>);
  78. local_date_time phx_arrival = phx_departure + flight_length;
  79. <emphasis role="comment">//convert the phx time to a nyz time</emphasis>
  80. local_date_time nyc_arrival = phx_arrival.local_time_in(nyc_tz);
  81. <emphasis role="comment">//2005-Apr-03 06:30:00 EDT</emphasis>
  82. std::cout &lt;&lt; nyc_arrival &lt;&lt; std::endl;
  83. </programlisting>
  84. </para>
  85. </section>