blob: 98ec4c4e9e7eba55ca8184c2cf37696454abe733 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE library PUBLIC "-//Boost//DTD BoostBook XML V1.0//EN"
"../../../tools/boostbook/dtd/boostbook.dtd">
<!-- Copyright (c) 2001-2005 CrystalClear Software, Inc.
Subject to the Boost Software License, Version 1.0.
(See accompanying file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
-->
<section id="date_time.examples.flight">
<title>Flight Time Example</title>
<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>
<programlisting>
<literal>
<phrase role="preprocessor">#include</phrase><phrase role="string"> "boost/date_time/local_time/local_time.hpp"</phrase><phrase role="preprocessor">
#include</phrase><phrase role="special"> <</phrase><phrase role="identifier">iostream</phrase><phrase role="special">></phrase><phrase role="comment">
/* 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).
*
*
*/</phrase><phrase role="keyword">
int</phrase><phrase role="identifier"> main</phrase><phrase role="special">()</phrase><phrase role="special">
{</phrase><phrase role="keyword">
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">
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">
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">
//setup some timezones for creating and adjusting local times
//This user editable file can be found in libs/date_time/data.
</phrase><phrase role="identifier"> tz_database</phrase><phrase role="identifier"> tz_db</phrase><phrase role="special">;</phrase><phrase role="identifier">
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">
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">
//Use a
</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">
//local departure time in phoenix is 11 pm on april 2 2005
// (ny changes to dst on apr 3 at 2 am)
</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">
phx_tz</phrase><phrase role="special">,</phrase><phrase role="identifier">
local_date_time</phrase><phrase role="special">::</phrase><phrase role="identifier">NOT_DATE_TIME_ON_ERROR</phrase><phrase role="special">);</phrase><phrase role="identifier">
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">
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">
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">
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">
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">
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">
}</phrase>
</literal>
</programlisting>
</section>
|