Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_34_1/libs/date_time/xmldoc/usage_examples.xml @ 29

Last change on this file since 29 was 29, checked in by landauf, 16 years ago

updated boost from 1_33_1 to 1_34_1

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