| 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.flight"> |
|---|
| 11 | <title>Flight Time Example</title> |
|---|
| 12 | |
|---|
| 13 | <para>This example shows a program that calculates the arrival time of a plane that flys from Phoenix to New York. During the flight New York shifts into daylight savings time (Phoenix doesn't because Arizona doesn't use dst).</para> |
|---|
| 14 | |
|---|
| 15 | <programlisting> |
|---|
| 16 | <literal> |
|---|
| 17 | <phrase role="preprocessor">#include</phrase><phrase role="string"> "boost/date_time/local_time/local_time.hpp"</phrase><phrase role="preprocessor"> |
|---|
| 18 | #include</phrase><phrase role="special"> <</phrase><phrase role="identifier">iostream</phrase><phrase role="special">></phrase><phrase role="comment"> |
|---|
| 19 | |
|---|
| 20 | /* This example shows a program that calculates the arrival time of a plane |
|---|
| 21 | * that flys from Phoenix to New York. During the flight New York shifts |
|---|
| 22 | * into daylight savings time (Phoenix doesn't because Arizona doesn't use |
|---|
| 23 | * dst). |
|---|
| 24 | * |
|---|
| 25 | * |
|---|
| 26 | */</phrase><phrase role="keyword"> |
|---|
| 27 | |
|---|
| 28 | int</phrase><phrase role="identifier"> main</phrase><phrase role="special">()</phrase><phrase role="special"> |
|---|
| 29 | {</phrase><phrase role="keyword"> |
|---|
| 30 | using</phrase><phrase role="keyword"> namespace</phrase><phrase role="identifier"> boost</phrase><phrase role="special">::</phrase><phrase role="identifier">gregorian</phrase><phrase role="special">;</phrase><phrase role="keyword"> |
|---|
| 31 | using</phrase><phrase role="keyword"> namespace</phrase><phrase role="identifier"> boost</phrase><phrase role="special">::</phrase><phrase role="identifier">local_time</phrase><phrase role="special">;</phrase><phrase role="keyword"> |
|---|
| 32 | using</phrase><phrase role="keyword"> namespace</phrase><phrase role="identifier"> boost</phrase><phrase role="special">::</phrase><phrase role="identifier">posix_time</phrase><phrase role="special">;</phrase><phrase role="comment"> |
|---|
| 33 | |
|---|
| 34 | |
|---|
| 35 | //setup some timezones for creating and adjusting local times |
|---|
| 36 | //This user editable file can be found in libs/date_time/data. |
|---|
| 37 | </phrase><phrase role="identifier"> tz_database</phrase><phrase role="identifier"> tz_db</phrase><phrase role="special">;</phrase><phrase role="identifier"> |
|---|
| 38 | tz_db</phrase><phrase role="special">.</phrase><phrase role="identifier">load_from_file</phrase><phrase role="special">(</phrase><phrase role="string">"date_time_zonespec.csv"</phrase><phrase role="special">);</phrase><phrase role="identifier"> |
|---|
| 39 | time_zone_ptr</phrase><phrase role="identifier"> nyc_tz</phrase><phrase role="special"> =</phrase><phrase role="identifier"> tz_db</phrase><phrase role="special">.</phrase><phrase role="identifier">time_zone_from_region</phrase><phrase role="special">(</phrase><phrase role="string">"America/New_York"</phrase><phrase role="special">);</phrase><phrase role="comment"> |
|---|
| 40 | //Use a |
|---|
| 41 | </phrase><phrase role="identifier"> time_zone_ptr</phrase><phrase role="identifier"> phx_tz</phrase><phrase role="special">(</phrase><phrase role="keyword">new</phrase><phrase role="identifier"> posix_time_zone</phrase><phrase role="special">(</phrase><phrase role="string">"MST-07:00:00"</phrase><phrase role="special">));</phrase><phrase role="comment"> |
|---|
| 42 | |
|---|
| 43 | //local departure time in phoenix is 11 pm on april 2 2005 |
|---|
| 44 | // (ny changes to dst on apr 3 at 2 am) |
|---|
| 45 | </phrase><phrase role="identifier"> local_date_time</phrase><phrase role="identifier"> phx_departure</phrase><phrase role="special">(</phrase><phrase role="identifier">date</phrase><phrase role="special">(</phrase><phrase role="number">2005</phrase><phrase role="special">,</phrase><phrase role="identifier"> Apr</phrase><phrase role="special">,</phrase><phrase role="number"> 2</phrase><phrase role="special">),</phrase><phrase role="identifier"> hours</phrase><phrase role="special">(</phrase><phrase role="number">23</phrase><phrase role="special">),</phrase><phrase role="identifier"> |
|---|
| 46 | phx_tz</phrase><phrase role="special">,</phrase><phrase role="identifier"> |
|---|
| 47 | local_date_time</phrase><phrase role="special">::</phrase><phrase role="identifier">NOT_DATE_TIME_ON_ERROR</phrase><phrase role="special">);</phrase><phrase role="identifier"> |
|---|
| 48 | |
|---|
| 49 | time_duration</phrase><phrase role="identifier"> flight_length</phrase><phrase role="special"> =</phrase><phrase role="identifier"> hours</phrase><phrase role="special">(</phrase><phrase role="number">4</phrase><phrase role="special">)</phrase><phrase role="special"> +</phrase><phrase role="identifier"> minutes</phrase><phrase role="special">(</phrase><phrase role="number">30</phrase><phrase role="special">);</phrase><phrase role="identifier"> |
|---|
| 50 | local_date_time</phrase><phrase role="identifier"> phx_arrival</phrase><phrase role="special"> =</phrase><phrase role="identifier"> phx_departure</phrase><phrase role="special"> +</phrase><phrase role="identifier"> flight_length</phrase><phrase role="special">;</phrase><phrase role="identifier"> |
|---|
| 51 | local_date_time</phrase><phrase role="identifier"> nyc_arrival</phrase><phrase role="special"> =</phrase><phrase role="identifier"> phx_arrival</phrase><phrase role="special">.</phrase><phrase role="identifier">local_time_in</phrase><phrase role="special">(</phrase><phrase role="identifier">nyc_tz</phrase><phrase role="special">);</phrase><phrase role="identifier"> |
|---|
| 52 | |
|---|
| 53 | std</phrase><phrase role="special">::</phrase><phrase role="identifier">cout</phrase><phrase role="special"> <<</phrase><phrase role="string"> "departure phx time: "</phrase><phrase role="special"> <<</phrase><phrase role="identifier"> phx_departure</phrase><phrase role="special"> <<</phrase><phrase role="identifier"> std</phrase><phrase role="special">::</phrase><phrase role="identifier">endl</phrase><phrase role="special">;</phrase><phrase role="identifier"> |
|---|
| 54 | std</phrase><phrase role="special">::</phrase><phrase role="identifier">cout</phrase><phrase role="special"> <<</phrase><phrase role="string"> "arrival phx time: "</phrase><phrase role="special"> <<</phrase><phrase role="identifier"> phx_arrival</phrase><phrase role="special"> <<</phrase><phrase role="identifier"> std</phrase><phrase role="special">::</phrase><phrase role="identifier">endl</phrase><phrase role="special">;</phrase><phrase role="identifier"> |
|---|
| 55 | std</phrase><phrase role="special">::</phrase><phrase role="identifier">cout</phrase><phrase role="special"> <<</phrase><phrase role="string"> "arrival nyc time: "</phrase><phrase role="special"> <<</phrase><phrase role="identifier"> nyc_arrival</phrase><phrase role="special"> <<</phrase><phrase role="identifier"> std</phrase><phrase role="special">::</phrase><phrase role="identifier">endl</phrase><phrase role="special">;</phrase><phrase role="special"> |
|---|
| 56 | |
|---|
| 57 | }</phrase> |
|---|
| 58 | </literal> |
|---|
| 59 | </programlisting> |
|---|
| 60 | </section> |
|---|