• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
2<html>
3<head>
4<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
5<title>Examples</title>
6<link rel="stylesheet" href="../../../doc/src/boostbook.css" type="text/css">
7<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
8<link rel="home" href="../index.html" title="The Boost C++ Libraries BoostBook Documentation Subset">
9<link rel="up" href="../date_time.html" title="Chapter 13. Boost.Date_Time">
10<link rel="prev" href="details.html" title="Details">
11<link rel="next" href="doxy.html" title="Library Reference">
12</head>
13<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
14<table cellpadding="2" width="100%"><tr>
15<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../boost.png"></td>
16<td align="center"><a href="../../../index.html">Home</a></td>
17<td align="center"><a href="../../../libs/libraries.htm">Libraries</a></td>
18<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
19<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
20<td align="center"><a href="../../../more/index.htm">More</a></td>
21</tr></table>
22<hr>
23<div class="spirit-nav">
24<a accesskey="p" href="details.html"><img src="../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../date_time.html"><img src="../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="doxy.html"><img src="../../../doc/src/images/next.png" alt="Next"></a>
25</div>
26<div class="section">
27<div class="titlepage"><div><div><h2 class="title" style="clear: both">
28<a name="date_time.examples"></a>Examples</h2></div></div></div>
29<div class="toc"><dl class="toc">
30<dt><span class="section"><a href="examples.html#date_time.examples.dates_as_strings">Dates as Strings</a></span></dt>
31<dt><span class="section"><a href="examples.html#date_time.examples.days_alive">Days Alive</a></span></dt>
32<dt><span class="section"><a href="examples.html#date_time.examples.days_between_new_year">Days Between New Years</a></span></dt>
33<dt><span class="section"><a href="examples.html#date_time.examples.end_of_month_day">Last Day of the Months</a></span></dt>
34<dt><span class="section"><a href="examples.html#date_time.examples.localization">Localization Demonstration</a></span></dt>
35<dt><span class="section"><a href="examples.html#date_time.examples.date_period_calc">Date Period Calculations</a></span></dt>
36<dt><span class="section"><a href="examples.html#date_time.examples.print_holidays">Print Holidays</a></span></dt>
37<dt><span class="section"><a href="examples.html#date_time.examples.print_month">Print Month</a></span></dt>
38<dt><span class="section"><a href="examples.html#date_time.examples.month_add">Month Adding</a></span></dt>
39<dt><span class="section"><a href="examples.html#date_time.examples.time_math">Time Math</a></span></dt>
40<dt><span class="section"><a href="examples.html#date_time.examples.print_hours">Print Hours</a></span></dt>
41<dt><span class="section"><a href="examples.html#date_time.examples.local_utc_conversion">Local to UTC Conversion</a></span></dt>
42<dt><span class="section"><a href="examples.html#date_time.examples.time_periods">Time Periods</a></span></dt>
43<dt><span class="section"><a href="examples.html#date_time.examples.simple_time_zone">Simple Time Zones</a></span></dt>
44<dt><span class="section"><a href="examples.html#date_time.examples.calc_rules">Daylight Savings Calc Rules</a></span></dt>
45<dt><span class="section"><a href="examples.html#date_time.examples.flight">Flight Time Example</a></span></dt>
46<dt><span class="section"><a href="examples.html#date_time.examples.seconds_since_epoch">Seconds Since Epoch</a></span></dt>
47</dl></div>
48<div class="section">
49<div class="titlepage"><div><div><h3 class="title">
50<a name="date_time.examples.dates_as_strings"></a>Dates as Strings</h3></div></div></div>
51<p>
52    Various parsing and output of strings.
53  </p>
54<pre class="programlisting">
55
56
57  /* The following is a simple example that shows conversion of dates
58   * to and from a std::string.
59   *
60   * Expected output:
61   * 2001-Oct-09
62   * 2001-10-09
63   * Tuesday October 9, 2001
64   * An expected exception is next:
65   * Exception: Month number is out of range 1..12
66   */
67
68  #include "boost/date_time/gregorian/gregorian.hpp"
69  #include &lt;iostream&gt;
70  #include &lt;string&gt;
71
72  int
73  main()
74  {
75
76    using namespace boost::gregorian;
77
78    try {
79      // The following date is in ISO 8601 extended format (CCYY-MM-DD)
80      std::string s("2001-10-9"); //2001-October-09
81      date d(from_simple_string(s));
82      std::cout &lt;&lt; to_simple_string(d) &lt;&lt; std::endl;
83
84      //Read ISO Standard(CCYYMMDD) and output ISO Extended
85      std::string ud("20011009"); //2001-Oct-09
86      date d1(from_undelimited_string(ud));
87      std::cout &lt;&lt; to_iso_extended_string(d1) &lt;&lt; std::endl;
88
89      //Output the parts of the date - Tuesday October 9, 2001
90      date::ymd_type ymd = d1.year_month_day();
91      greg_weekday wd = d1.day_of_week();
92      std::cout &lt;&lt; wd.as_long_string() &lt;&lt; " "
93                &lt;&lt; ymd.month.as_long_string() &lt;&lt; " "
94                &lt;&lt; ymd.day &lt;&lt; ", " &lt;&lt; ymd.year
95                &lt;&lt; std::endl;
96
97      //Let's send in month 25 by accident and create an exception
98      std::string bad_date("20012509"); //2001-??-09
99      std::cout &lt;&lt; "An expected exception is next: " &lt;&lt; std::endl;
100      date wont_construct(from_undelimited_string(bad_date));
101      //use wont_construct so compiler doesn't complain, but you wont get here!
102      std::cout &lt;&lt; "oh oh, you shouldn't reach this line: "
103                &lt;&lt; to_iso_string(wont_construct) &lt;&lt; std::endl;
104    }
105    catch(std::exception&amp; e) {
106      std::cout &lt;&lt; "  Exception: " &lt;&lt;  e.what() &lt;&lt; std::endl;
107    }
108
109
110    return 0;
111  }
112
113
114  </pre>
115</div>
116<div class="section">
117<div class="titlepage"><div><div><h3 class="title">
118<a name="date_time.examples.days_alive"></a>Days Alive</h3></div></div></div>
119<p>
120    Calculate the number of days you have been living using durations and dates.
121  </p>
122<pre class="programlisting">
123<code class="literal">
124<span class="comment">/* Short example that calculates the number of days since user was born.
125 * Demonstrates comparisons of durations, use of the day_clock,
126 * and parsing a date from a string.
127 */</span><span class="preprocessor">
128
129#include</span><span class="string"> "boost/date_time/gregorian/gregorian.hpp"</span><span class="preprocessor">
130#include</span><span class="special"> &lt;</span><span class="identifier">iostream</span><span class="special">&gt;</span><span class="keyword">
131
132int</span><span class="identifier">
133main</span><span class="special">()</span><span class="special">
134{</span><span class="keyword">
135
136  using</span><span class="keyword"> namespace</span><span class="identifier"> boost</span><span class="special">::</span><span class="identifier">gregorian</span><span class="special">;</span><span class="identifier">
137  std</span><span class="special">::</span><span class="identifier">string</span><span class="identifier"> s</span><span class="special">;</span><span class="identifier">
138  std</span><span class="special">::</span><span class="identifier">cout</span><span class="special"> &lt;&lt;</span><span class="string"> "Enter birth day YYYY-MM-DD (eg: 2002-02-01): "</span><span class="special">;</span><span class="identifier">
139  std</span><span class="special">::</span><span class="identifier">cin</span><span class="special"> &gt;&gt;</span><span class="identifier"> s</span><span class="special">;</span><span class="keyword">
140  try</span><span class="special"> {</span><span class="identifier">
141    date</span><span class="identifier"> birthday</span><span class="special">(</span><span class="identifier">from_simple_string</span><span class="special">(</span><span class="identifier">s</span><span class="special">));</span><span class="identifier">
142    date</span><span class="identifier"> today</span><span class="special"> =</span><span class="identifier"> day_clock</span><span class="special">::</span><span class="identifier">local_day</span><span class="special">();</span><span class="identifier">
143    days</span><span class="identifier"> days_alive</span><span class="special"> =</span><span class="identifier"> today</span><span class="special"> -</span><span class="identifier"> birthday</span><span class="special">;</span><span class="identifier">
144    days</span><span class="identifier"> one_day</span><span class="special">(</span><span class="number">1</span><span class="special">);</span><span class="keyword">
145    if</span><span class="special"> (</span><span class="identifier">days_alive</span><span class="special"> ==</span><span class="identifier"> one_day</span><span class="special">)</span><span class="special"> {</span><span class="identifier">
146      std</span><span class="special">::</span><span class="identifier">cout</span><span class="special"> &lt;&lt;</span><span class="string"> "Born yesterday, very funny"</span><span class="special"> &lt;&lt;</span><span class="identifier"> std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span><span class="special">
147    }</span><span class="keyword">
148    else</span><span class="keyword"> if</span><span class="special"> (</span><span class="identifier">days_alive</span><span class="special"> &lt;</span><span class="identifier"> days</span><span class="special">(</span><span class="number">0</span><span class="special">))</span><span class="special"> {</span><span class="identifier">
149      std</span><span class="special">::</span><span class="identifier">cout</span><span class="special"> &lt;&lt;</span><span class="string"> "Not born yet, hmm: "</span><span class="special"> &lt;&lt;</span><span class="identifier"> days_alive</span><span class="special">.</span><span class="identifier">days</span><span class="special">()</span><span class="special">
150                &lt;&lt;</span><span class="string"> " days"</span><span class="special"> &lt;&lt;</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span><span class="special">
151    }</span><span class="keyword">
152    else</span><span class="special"> {</span><span class="identifier">
153      std</span><span class="special">::</span><span class="identifier">cout</span><span class="special"> &lt;&lt;</span><span class="string"> "Days alive: "</span><span class="special"> &lt;&lt;</span><span class="identifier"> days_alive</span><span class="special">.</span><span class="identifier">days</span><span class="special">()</span><span class="special"> &lt;&lt;</span><span class="identifier"> std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span><span class="special">
154    }</span><span class="special">
155
156  }</span><span class="keyword">
157  catch</span><span class="special">(...)</span><span class="special"> {</span><span class="identifier">
158    std</span><span class="special">::</span><span class="identifier">cout</span><span class="special"> &lt;&lt;</span><span class="string"> "Bad date entered: "</span><span class="special"> &lt;&lt;</span><span class="identifier"> s</span><span class="special"> &lt;&lt;</span><span class="identifier"> std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span><span class="special">
159  }</span><span class="keyword">
160  return</span><span class="number"> 0</span><span class="special">;</span><span class="special">
161}</span>
162</code>
163</pre>
164</div>
165<div class="section">
166<div class="titlepage"><div><div><h3 class="title">
167<a name="date_time.examples.days_between_new_year"></a>Days Between New Years</h3></div></div></div>
168<p>
169    Calculate the number of days till new years
170  </p>
171<pre class="programlisting">
172
173  /* Provides a simple example of using a date_generator, and simple
174   * mathematical operatorations, to calculate the days since
175   * New Years day of this year, and days until next New Years day.
176   *
177   * Expected results:
178   * Adding together both durations will produce 366 (365 in a leap year).
179   */
180  #include &lt;iostream&gt;
181  #include "boost/date_time/gregorian/gregorian.hpp"
182
183  int
184  main()
185  {
186
187    using namespace boost::gregorian;
188
189    date today = day_clock::local_day();
190    partial_date new_years_day(1,Jan);
191    //Subtract two dates to get a duration
192    days days_since_year_start = today - new_years_day.get_date(today.year());
193    std::cout &lt;&lt; "Days since Jan 1: " &lt;&lt; days_since_year_start.days()
194              &lt;&lt; std::endl;
195
196    days days_until_year_start = new_years_day.get_date(today.year()+1) - today;
197    std::cout &lt;&lt; "Days until next Jan 1: " &lt;&lt; days_until_year_start.days()
198              &lt;&lt; std::endl;
199    return 0;
200  };
201
202
203  </pre>
204</div>
205<div class="section">
206<div class="titlepage"><div><div><h3 class="title">
207<a name="date_time.examples.end_of_month_day"></a>Last Day of the Months</h3></div></div></div>
208<p>
209    Example that gets a month and a year from the user and finds the last day of each remaining month of that year.
210  </p>
211<pre class="programlisting">
212
213  /* Simple program that finds the last day of the given month,
214   * then displays the last day of every month left in the given year.
215   */
216
217  #include "boost/date_time/gregorian/gregorian.hpp"
218  #include &lt;iostream&gt;
219
220  int
221  main()
222  {
223    using namespace boost::gregorian;
224
225    greg_year year(1400);
226    greg_month month(1);
227
228    // get a month and a year from the user
229    try {
230      int y, m;
231      std::cout &lt;&lt; "   Enter Year(ex: 2002): ";
232      std::cin &gt;&gt; y;
233      year = greg_year(y);
234      std::cout &lt;&lt; "   Enter Month(1..12): ";
235      std::cin &gt;&gt; m;
236      month = greg_month(m);
237    }
238    catch(bad_year by) {
239      std::cout &lt;&lt; "Invalid Year Entered: " &lt;&lt; by.what() &lt;&lt; '\n'
240        &lt;&lt; "Using minimum values for month and year." &lt;&lt; std::endl;
241    }
242    catch(bad_month bm) {
243      std::cout &lt;&lt; "Invalid Month Entered" &lt;&lt; bm.what() &lt;&lt; '\n'
244        &lt;&lt; "Using minimum value for month. " &lt;&lt; std::endl;
245    }
246
247    date start_of_next_year(year+1, Jan, 1);
248    date d(year, month, 1);
249
250    // add another month to d until we enter the next year.
251    while (d &lt; start_of_next_year){
252      std::cout &lt;&lt; to_simple_string(d.end_of_month()) &lt;&lt; std::endl;
253      d += months(1);
254    }
255
256    return 0;
257  }
258
259
260  </pre>
261</div>
262<div class="section">
263<div class="titlepage"><div><div><h3 class="title">
264<a name="date_time.examples.localization"></a>Localization Demonstration</h3></div></div></div>
265<p>
266    The boost::date_time library provides the ability to create customized locale facets. Date ordering, language, seperators, and abbreviations can be customized.
267  </p>
268<pre class="programlisting">
269
270  /* The following shows the creation of a facet for the output of
271   * dates in German (please forgive me for any errors in my German --
272   * I'm not a native speaker).
273   */
274
275  #include "boost/date_time/gregorian/gregorian.hpp"
276  #include &lt;iostream&gt;
277  #include &lt;algorithm&gt;
278
279  /* Define a series of char arrays for short and long name strings
280   * to be associated with German date output (US names will be
281   * retrieved from the locale). */
282  const char* const de_short_month_names[] =
283  {
284    "Jan", "Feb", "Mar", "Apr", "Mai", "Jun",
285    "Jul", "Aug", "Sep", "Okt", "Nov", "Dez", "NAM"
286  };
287  const char* const de_long_month_names[] =
288  {
289    "Januar", "Februar", "Marz", "April", "Mai",
290    "Juni", "Juli", "August", "September", "Oktober",
291    "November", "Dezember", "NichtDerMonat"
292  };
293  const char* const de_long_weekday_names[] =
294  {
295    "Sonntag", "Montag", "Dienstag", "Mittwoch",
296    "Donnerstag", "Freitag", "Samstag"
297  };
298  const char* const de_short_weekday_names[] =
299  {
300    "Son", "Mon", "Die","Mit", "Don", "Fre", "Sam"
301  };
302
303
304  int main()
305  {
306    using namespace boost::gregorian;
307
308    // create some gregorian objects to output
309    date d1(2002, Oct, 1);
310    greg_month m = d1.month();
311    greg_weekday wd = d1.day_of_week();
312
313    // create a facet and a locale for German dates
314    date_facet* german_facet = new date_facet();
315    std::cout.imbue(std::locale(std::locale::classic(), german_facet));
316
317    // create the German name collections
318    date_facet::input_collection_type short_months, long_months,
319                                      short_weekdays, long_weekdays;
320    std::copy(&amp;de_short_month_names[0], &amp;de_short_month_names[11],
321              std::back_inserter(short_months));
322    std::copy(&amp;de_long_month_names[0], &amp;de_long_month_names[11],
323              std::back_inserter(long_months));
324    std::copy(&amp;de_short_weekday_names[0], &amp;de_short_weekday_names[6],
325              std::back_inserter(short_weekdays));
326    std::copy(&amp;de_long_weekday_names[0], &amp;de_long_weekday_names[6],
327              std::back_inserter(long_weekdays));
328
329    // replace the default names with ours
330    // NOTE: date_generators and special_values were not replaced as
331    // they are not used in this example
332    german_facet-&gt;short_month_names(short_months);
333    german_facet-&gt;long_month_names(long_months);
334    german_facet-&gt;short_weekday_names(short_weekdays);
335    german_facet-&gt;long_weekday_names(long_weekdays);
336
337    // output the date in German using short month names
338    german_facet-&gt;format("%d.%m.%Y");
339    std::cout &lt;&lt; d1 &lt;&lt; std::endl; //01.10.2002
340
341    german_facet-&gt;month_format("%B");
342    std::cout &lt;&lt; m &lt;&lt; std::endl; //Oktober
343
344    german_facet-&gt;weekday_format("%A");
345    std::cout &lt;&lt; wd &lt;&lt; std::endl; //Dienstag
346
347
348    // Output the same gregorian objects using US names
349    date_facet* us_facet = new date_facet();
350    std::cout.imbue(std::locale(std::locale::classic(), us_facet));
351
352    us_facet-&gt;format("%m/%d/%Y");
353    std::cout &lt;&lt; d1 &lt;&lt; std::endl; //  10/01/2002
354
355    // English names, iso order (year-month-day), '-' separator
356    us_facet-&gt;format("%Y-%b-%d");
357    std::cout &lt;&lt; d1 &lt;&lt; std::endl; //  2002-Oct-01
358
359    return 0;
360
361  }
362
363  </pre>
364</div>
365<div class="section">
366<div class="titlepage"><div><div><h3 class="title">
367<a name="date_time.examples.date_period_calc"></a>Date Period Calculations</h3></div></div></div>
368<p>
369    Calculates if a date is in an 'irregular' collection of periods using period calculation functions.
370  </p>
371<pre class="programlisting">
372
373  /*
374  This example demonstrates a simple use of periods for the calculation
375  of date information.
376
377  The example calculates if a given date is a weekend or holiday
378  given an exclusion set.  That is, each weekend or holiday is
379  entered into the set as a time interval.  Then if a given date
380  is contained within any of the intervals it is considered to
381  be within the exclusion set and hence is a offtime.
382
383  Output:
384  Number Excluded Periods: 5
385  20020202/20020203
386  20020209/20020210
387  20020212/20020212
388  20020216/20020217
389  In Exclusion Period: 20020216 --&gt; 20020216/20020217
390  20020223/20020224
391
392  */
393
394
395  #include "boost/date_time/gregorian/gregorian.hpp"
396  #include &lt;set&gt;
397  #include &lt;algorithm&gt;
398  #include &lt;iostream&gt;
399
400  typedef std::set&lt;boost::gregorian::date_period&gt; date_period_set;
401
402  //Simple population of the exclusion set
403  date_period_set
404  generateExclusion()
405  {
406    using namespace boost::gregorian;
407    date_period periods_array[] =
408      { date_period(date(2002,Feb,2), date(2002,Feb,4)),//weekend of 2nd-3rd
409        date_period(date(2002,Feb,9), date(2002,Feb,11)),
410        date_period(date(2002,Feb,16), date(2002,Feb,18)),
411        date_period(date(2002,Feb,23), date(2002,Feb,25)),
412        date_period(date(2002,Feb,12), date(2002,Feb,13))//a random holiday 2-12
413      };
414    const int num_periods = sizeof(periods_array)/sizeof(date_period);
415
416    date_period_set ps;
417    //insert the periods in the set
418    std::insert_iterator&lt;date_period_set&gt; itr(ps, ps.begin());
419    std::copy(periods_array, periods_array+num_periods, itr );
420    return ps;
421
422  }
423
424
425  int main()
426  {
427    using namespace boost::gregorian;
428
429    date_period_set ps = generateExclusion();
430    std::cout &lt;&lt; "Number Excluded Periods: "  &lt;&lt; ps.size() &lt;&lt; std::endl;
431
432    date d(2002,Feb,16);
433    date_period_set::const_iterator i = ps.begin();
434    //print the periods, check for containment
435    for (;i != ps.end(); i++) {
436      std::cout &lt;&lt; to_iso_string(*i) &lt;&lt; std::endl;
437      //if date is in exclusion period then print it
438      if (i-&gt;contains(d)) {
439        std::cout &lt;&lt; "In Exclusion Period: "
440            &lt;&lt; to_iso_string(d) &lt;&lt; " --&gt; " &lt;&lt; to_iso_string(*i)
441            &lt;&lt; std::endl;
442      }
443    }
444
445    return 0;
446
447  }
448
449
450  </pre>
451</div>
452<div class="section">
453<div class="titlepage"><div><div><h3 class="title">
454<a name="date_time.examples.print_holidays"></a>Print Holidays</h3></div></div></div>
455<p>
456    This is an example of using functors to define a holiday schedule
457  </p>
458<pre class="programlisting">
459
460
461  /* Generate a set of dates using a collection of date generators
462   * Output looks like:
463   * Enter Year: 2002
464   * 2002-Jan-01 [Tue]
465   * 2002-Jan-21 [Mon]
466   * 2002-Feb-12 [Tue]
467   * 2002-Jul-04 [Thu]
468   * 2002-Sep-02 [Mon]
469   * 2002-Nov-28 [Thu]
470   * 2002-Dec-25 [Wed]
471   * Number Holidays: 7
472   */
473
474  #include "boost/date_time/gregorian/gregorian.hpp"
475  #include &lt;algorithm&gt;
476  #include &lt;functional&gt;
477  #include &lt;vector&gt;
478  #include &lt;iostream&gt;
479  #include &lt;set&gt;
480
481  void
482  print_date(boost::gregorian::date d)
483  {
484    using namespace boost::gregorian;
485  #if defined(BOOST_DATE_TIME_NO_LOCALE)
486    std::cout &lt;&lt; to_simple_string(d) &lt;&lt; " [" &lt;&lt; d.day_of_week() &lt;&lt; "]\n";
487  #else
488    std::cout &lt;&lt; d &lt;&lt; " [" &lt;&lt; d.day_of_week() &lt;&lt; "]\n";
489  #endif
490  }
491
492
493  int
494  main() {
495
496    std::cout &lt;&lt; "Enter Year: ";
497    int year;
498    std::cin &gt;&gt; year;
499
500    using namespace boost::gregorian;
501
502    //define a collection of holidays fixed by month and day
503    std::vector&lt;year_based_generator*&gt; holidays;
504    holidays.push_back(new partial_date(1,Jan)); //Western New Year
505    holidays.push_back(new partial_date(4,Jul)); //US Independence Day
506    holidays.push_back(new partial_date(25, Dec));//Christmas day
507
508
509    //define a shorthand for the nth_day_of_the_week_in_month function object
510    typedef nth_day_of_the_week_in_month nth_dow;
511
512    //US labor day
513    holidays.push_back(new nth_dow(nth_dow::first,  Monday,   Sep));
514    //MLK Day
515    holidays.push_back(new nth_dow(nth_dow::third,  Monday,   Jan));
516    //Pres day
517    holidays.push_back(new nth_dow(nth_dow::second, Tuesday,  Feb));
518    //Thanksgiving
519    holidays.push_back(new nth_dow(nth_dow::fourth, Thursday, Nov));
520
521    typedef std::set&lt;date&gt; date_set;
522    date_set all_holidays;
523
524    for(std::vector&lt;year_based_generator*&gt;::iterator it = holidays.begin();
525        it != holidays.end(); ++it)
526    {
527      all_holidays.insert((*it)-&gt;get_date(year));
528    }
529
530    //print the holidays to the screen
531    std::for_each(all_holidays.begin(), all_holidays.end(), print_date);
532    std::cout &lt;&lt; "Number Holidays: " &lt;&lt; all_holidays.size() &lt;&lt; std::endl;
533
534    return 0;
535  }
536
537
538  </pre>
539</div>
540<div class="section">
541<div class="titlepage"><div><div><h3 class="title">
542<a name="date_time.examples.print_month"></a>Print Month</h3></div></div></div>
543<p>
544    Simple utility to print out days of the month with the days of a month. Demontstrates date iteration (date_time::date_itr).
545  </p>
546<pre class="programlisting">
547
548  /* This example prints all the dates in a month. It demonstrates
549   * the use of iterators as well as functions of the gregorian_calendar
550   *
551   * Output:
552   * Enter Year: 2002
553   * Enter Month(1..12): 2
554   * 2002-Feb-01 [Fri]
555   * 2002-Feb-02 [Sat]
556   * 2002-Feb-03 [Sun]
557   * 2002-Feb-04 [Mon]
558   * 2002-Feb-05 [Tue]
559   * 2002-Feb-06 [Wed]
560   * 2002-Feb-07 [Thu]
561   */
562
563  #include "boost/date_time/gregorian/gregorian.hpp"
564  #include &lt;iostream&gt;
565
566  int
567  main()
568  {
569    std::cout &lt;&lt; "Enter Year: ";
570    int year, month;
571    std::cin &gt;&gt; year;
572    std::cout &lt;&lt; "Enter Month(1..12): ";
573    std::cin &gt;&gt; month;
574
575    using namespace boost::gregorian;
576    try {
577      //Use the calendar to get the last day of the month
578      int eom_day = gregorian_calendar::end_of_month_day(year,month);
579      date endOfMonth(year,month,eom_day);
580
581      //construct an iterator starting with firt day of the month
582      day_iterator ditr(date(year,month,1));
583      //loop thru the days and print each one
584      for (; ditr &lt;= endOfMonth; ++ditr) {
585  #if defined(BOOST_DATE_TIME_NO_LOCALE)
586        std::cout &lt;&lt; to_simple_string(*ditr) &lt;&lt; " ["
587  #else
588        std::cout &lt;&lt; *ditr &lt;&lt; " ["
589  #endif
590                  &lt;&lt; ditr-&gt;day_of_week() &lt;&lt; "]"
591                  &lt;&lt; std::endl;
592      }
593    }
594    catch(std::exception&amp; e) {
595
596      std::cout &lt;&lt; "Error bad date, check your entry: \n"
597                &lt;&lt; "  Details: " &lt;&lt; e.what() &lt;&lt; std::endl;
598    }
599    return 0;
600  }
601
602
603  </pre>
604</div>
605<div class="section">
606<div class="titlepage"><div><div><h3 class="title">
607<a name="date_time.examples.month_add"></a>Month Adding</h3></div></div></div>
608<p>
609    Adding a month to a day without the use of iterators.
610  </p>
611<pre class="programlisting">
612
613  /* Simple program that uses the gregorian calendar to progress by exactly
614   * one month, irregardless of how many days are in that month.
615   *
616   * This method can be used as an alternative to iterators
617   */
618
619  #include "boost/date_time/gregorian/gregorian.hpp"
620  #include &lt;iostream&gt;
621
622  int
623  main()
624  {
625
626    using namespace boost::gregorian;
627
628    date d = day_clock::local_day();
629    add_month mf(1);
630    date d2 = d + mf.get_offset(d);
631    std::cout &lt;&lt; "Today is: " &lt;&lt; to_simple_string(d) &lt;&lt; ".\n"
632      &lt;&lt; "One month from today will be: " &lt;&lt; to_simple_string(d2)
633      &lt;&lt; std::endl;
634
635    return 0;
636  }
637
638  </pre>
639</div>
640<div class="section">
641<div class="titlepage"><div><div><h3 class="title">
642<a name="date_time.examples.time_math"></a>Time Math</h3></div></div></div>
643<p>
644    Various types of calculations with times and time durations.
645  </p>
646<pre class="programlisting">
647
648  /* Some simple examples of constructing and calculating with times
649   * Output:
650   * 2002-Feb-01 00:00:00 - 2002-Feb-01 05:04:02.001000000 = -5:04:02.001000000
651   */
652
653  #include "boost/date_time/posix_time/posix_time.hpp"
654  #include &lt;iostream&gt;
655
656  int
657  main()
658  {
659    using namespace boost::posix_time;
660    using namespace boost::gregorian;
661
662    date d(2002,Feb,1); //an arbitrary date
663    //construct a time by adding up some durations durations
664    ptime t1(d, hours(5)+minutes(4)+seconds(2)+millisec(1));
665    //construct a new time by subtracting some times
666    ptime t2 = t1 - hours(5)- minutes(4)- seconds(2)- millisec(1);
667    //construct a duration by taking the difference between times
668    time_duration td = t2 - t1;
669
670    std::cout &lt;&lt; to_simple_string(t2) &lt;&lt; " - "
671              &lt;&lt; to_simple_string(t1) &lt;&lt; " = "
672              &lt;&lt; to_simple_string(td) &lt;&lt; std::endl;
673
674    return 0;
675  }
676
677  </pre>
678</div>
679<div class="section">
680<div class="titlepage"><div><div><h3 class="title">
681<a name="date_time.examples.print_hours"></a>Print Hours</h3></div></div></div>
682<p>
683    Demonstrate time iteration, clock retrieval, and simple calculation.
684  </p>
685<pre class="programlisting">
686
687  /* Print the remaining hours of the day
688   * Uses the clock to get the local time
689   * Use an iterator to iterate over the remaining hours
690   * Retrieve the date part from a time
691   *
692   * Expected Output something like:
693   *
694   * 2002-Mar-08 16:30:59
695   * 2002-Mar-08 17:30:59
696   * 2002-Mar-08 18:30:59
697   * 2002-Mar-08 19:30:59
698   * 2002-Mar-08 20:30:59
699   * 2002-Mar-08 21:30:59
700   * 2002-Mar-08 22:30:59
701   * 2002-Mar-08 23:30:59
702   * Time left till midnight: 07:29:01
703   */
704
705  #include "boost/date_time/posix_time/posix_time.hpp"
706  #include &lt;iostream&gt;
707
708  int
709  main()
710  {
711    using namespace boost::posix_time;
712    using namespace boost::gregorian;
713
714    //get the current time from the clock -- one second resolution
715    ptime now = second_clock::local_time();
716    //Get the date part out of the time
717    date today = now.date();
718    date tommorrow = today + days(1);
719    ptime tommorrow_start(tommorrow); //midnight
720
721    //iterator adds by one hour
722    time_iterator titr(now,hours(1));
723    for (; titr &lt; tommorrow_start; ++titr) {
724      std::cout &lt;&lt; to_simple_string(*titr) &lt;&lt; std::endl;
725    }
726
727    time_duration remaining = tommorrow_start - now;
728    std::cout &lt;&lt; "Time left till midnight: "
729              &lt;&lt; to_simple_string(remaining) &lt;&lt; std::endl;
730    return 0;
731  }
732
733
734  </pre>
735</div>
736<div class="section">
737<div class="titlepage"><div><div><h3 class="title">
738<a name="date_time.examples.local_utc_conversion"></a>Local to UTC Conversion</h3></div></div></div>
739<p>
740    Demonstrate utc to local and local to utc calculations including dst.
741  </p>
742<pre class="programlisting">
743
744
745  /* Demonstrate conversions between a local time and utc
746   * Output:
747   *
748   * UTC &lt;--&gt; New York while DST is NOT active (5 hours)
749   * 2001-Dec-31 19:00:00 in New York is 2002-Jan-01 00:00:00 UTC time
750   * 2002-Jan-01 00:00:00 UTC is 2001-Dec-31 19:00:00 New York time
751   *
752   * UTC &lt;--&gt; New York while DST is active (4 hours)
753   * 2002-May-31 20:00:00 in New York is 2002-Jun-01 00:00:00 UTC time
754   * 2002-Jun-01 00:00:00 UTC is 2002-May-31 20:00:00 New York time
755   *
756   * UTC &lt;--&gt; Arizona (7 hours)
757   * 2002-May-31 17:00:00 in Arizona is 2002-Jun-01 00:00:00 UTC time
758   */
759
760  #include "boost/date_time/posix_time/posix_time.hpp"
761  #include "boost/date_time/local_time_adjustor.hpp"
762  #include "boost/date_time/c_local_time_adjustor.hpp"
763  #include &lt;iostream&gt;
764
765  int
766  main()
767  {
768    using namespace boost::posix_time;
769    using namespace boost::gregorian;
770
771    //This local adjustor depends on the machine TZ settings-- highly dangerous!
772    typedef boost::date_time::c_local_adjustor&lt;ptime&gt; local_adj;
773    ptime t10(date(2002,Jan,1), hours(7));
774    ptime t11 = local_adj::utc_to_local(t10);
775    std::cout &lt;&lt; "UTC &lt;--&gt; Zone base on TZ setting" &lt;&lt; std::endl;
776    std::cout &lt;&lt; to_simple_string(t11) &lt;&lt; " in your TZ is "
777              &lt;&lt; to_simple_string(t10) &lt;&lt; " UTC time "
778              &lt;&lt; std::endl;
779    time_duration td = t11 - t10;
780    std::cout &lt;&lt; "A difference of: "
781              &lt;&lt; to_simple_string(td) &lt;&lt; std::endl;
782
783
784    //eastern timezone is utc-5
785    typedef boost::date_time::local_adjustor&lt;ptime, -5, us_dst&gt; us_eastern;
786
787    ptime t1(date(2001,Dec,31), hours(19)); //5 hours b/f midnight NY time
788
789    std::cout &lt;&lt; "\nUTC &lt;--&gt; New York while DST is NOT active (5 hours)"
790              &lt;&lt; std::endl;
791    ptime t2 =  us_eastern::local_to_utc(t1);
792    std::cout &lt;&lt; to_simple_string(t1) &lt;&lt; " in New York is "
793              &lt;&lt; to_simple_string(t2) &lt;&lt; " UTC time "
794              &lt;&lt; std::endl;
795
796    ptime t3 = us_eastern::utc_to_local(t2);//back should be the same
797    std::cout &lt;&lt; to_simple_string(t2) &lt;&lt; " UTC is "
798              &lt;&lt; to_simple_string(t3) &lt;&lt; " New York time "
799              &lt;&lt; "\n\n";
800
801    ptime t4(date(2002,May,31), hours(20)); //4 hours b/f midnight NY time
802    std::cout &lt;&lt; "UTC &lt;--&gt; New York while DST is active (4 hours)" &lt;&lt; std::endl;
803    ptime t5 = us_eastern::local_to_utc(t4);
804    std::cout &lt;&lt; to_simple_string(t4) &lt;&lt; " in New York is "
805              &lt;&lt; to_simple_string(t5) &lt;&lt; " UTC time "
806              &lt;&lt; std::endl;
807
808    ptime t6 = us_eastern::utc_to_local(t5);//back should be the same
809    std::cout &lt;&lt; to_simple_string(t5) &lt;&lt; " UTC is "
810              &lt;&lt; to_simple_string(t6) &lt;&lt; " New York time "
811              &lt;&lt; "\n" &lt;&lt; std::endl;
812
813
814    //Arizona timezone is utc-7 with no dst
815    typedef boost::date_time::local_adjustor&lt;ptime, -7, no_dst&gt; us_arizona;
816
817    ptime t7(date(2002,May,31), hours(17));
818    std::cout &lt;&lt; "UTC &lt;--&gt; Arizona (7 hours)" &lt;&lt; std::endl;
819    ptime t8 = us_arizona::local_to_utc(t7);
820    std::cout &lt;&lt; to_simple_string(t7) &lt;&lt; " in Arizona is "
821              &lt;&lt; to_simple_string(t8) &lt;&lt; " UTC time "
822              &lt;&lt; std::endl;
823
824    return 0;
825  }
826
827
828  </pre>
829</div>
830<div class="section">
831<div class="titlepage"><div><div><h3 class="title">
832<a name="date_time.examples.time_periods"></a>Time Periods</h3></div></div></div>
833<p>
834    Demonstrate some simple uses of time periods.
835  </p>
836<pre class="programlisting">
837
838
839  /* Some simple examples of constructing and calculating with times
840   * Returns:
841   * [2002-Feb-01 00:00:00/2002-Feb-01 23:59:59.999999999]
842   * contains 2002-Feb-01 03:00:05
843   * [2002-Feb-01 00:00:00/2002-Feb-01 23:59:59.999999999]
844   * intersected with
845   * [2002-Feb-01 00:00:00/2002-Feb-01 03:00:04.999999999]
846   * is
847   * [2002-Feb-01 00:00:00/2002-Feb-01 03:00:04.999999999]
848   */
849
850  #include "boost/date_time/posix_time/posix_time.hpp"
851  #include &lt;iostream&gt;
852
853  using namespace boost::posix_time;
854  using namespace boost::gregorian;
855
856  //Create a simple period class to contain all the times in a day
857  class day_period : public time_period
858  {
859  public:
860    day_period(date d) : time_period(ptime(d),//midnight
861                                     ptime(d,hours(24)))
862    {}
863
864  };
865
866  int
867  main()
868  {
869
870    date d(2002,Feb,1); //an arbitrary date
871    //a period that represents a day
872    day_period dp(d);
873    ptime t(d, hours(3)+seconds(5)); //an arbitray time on that day
874    if (dp.contains(t)) {
875      std::cout &lt;&lt; to_simple_string(dp) &lt;&lt; " contains "
876                &lt;&lt; to_simple_string(t)  &lt;&lt; std::endl;
877    }
878    //a period that represents part of the day
879    time_period part_of_day(ptime(d, hours(0)), t);
880    //intersect the 2 periods and print the results
881    if (part_of_day.intersects(dp)) {
882      time_period result = part_of_day.intersection(dp);
883      std::cout &lt;&lt; to_simple_string(dp) &lt;&lt; " intersected with\n"
884                &lt;&lt; to_simple_string(part_of_day) &lt;&lt; " is \n"
885                &lt;&lt; to_simple_string(result) &lt;&lt; std::endl;
886    }
887
888
889    return 0;
890  }
891
892
893  </pre>
894</div>
895<div class="section">
896<div class="titlepage"><div><div><h3 class="title">
897<a name="date_time.examples.simple_time_zone"></a>Simple Time Zones</h3></div></div></div>
898<p>
899    Example usage of custom_time_zone as well as posix_time_zone.
900  </p>
901<pre class="programlisting">
902
903  /* A simple example for using a custom_time_zone and a posix_time_zone.
904   */
905
906  #include "boost/date_time/local_time/local_time.hpp"
907  #include &lt;iostream&gt;
908
909  int
910  main()
911  {
912    using namespace boost;
913    using namespace local_time;
914    using namespace gregorian;
915    using posix_time::time_duration;
916
917    /***** custom_time_zone *****/
918
919    // create the dependent objects for a custom_time_zone
920    time_zone_names tzn("Eastern Standard Time", "EST",
921                        "Eastern Daylight Time", "EDT");
922    time_duration utc_offset(-5,0,0);
923    dst_adjustment_offsets adj_offsets(time_duration(1,0,0),
924                                       time_duration(2,0,0),
925                                       time_duration(2,0,0));
926    // rules for this zone are:
927    // start on first Sunday of April at 2 am
928    // end on last Sunday of October at 2 am
929    // so we use a first_last_dst_rule
930    first_day_of_the_week_in_month start_rule(Sunday, Apr);
931    last_day_of_the_week_in_month    end_rule(Sunday, Oct);
932    shared_ptr&lt;dst_calc_rule&gt; nyc_rules(new first_last_dst_rule(start_rule,
933                                                                end_rule));
934    // create more dependent objects for a non-dst custom_time_zone
935    time_zone_names tzn2("Mountain Standard Time", "MST",
936                         "", ""); // no dst means empty dst strings
937    time_duration utc_offset2(-7,0,0);
938    dst_adjustment_offsets adj_offsets2(time_duration(0,0,0),
939                                        time_duration(0,0,0),
940                                        time_duration(0,0,0));
941    // no dst means we need a null pointer to the rules
942    shared_ptr&lt;dst_calc_rule&gt; phx_rules;
943
944    // create the custom_time_zones
945    time_zone_ptr nyc_1(new custom_time_zone(tzn, utc_offset,
946                                             adj_offsets, nyc_rules));
947    time_zone_ptr phx_1(new custom_time_zone(tzn2, utc_offset2,
948                                             adj_offsets2, phx_rules));
949
950    /***** posix_time_zone *****/
951
952    // create posix_time_zones that are the duplicates of the
953    // custom_time_zones created above. See posix_time_zone documentation
954    // for details on full zone names.
955    std::string nyc_string, phx_string;
956    nyc_string = "EST-05:00:00EDT+01:00:00,M4.1.0/02:00:00,M10.5.0/02:00:00";
957    // nyc_string = "EST-05EDT,M4.1.0,M10.5.0"; // shorter when defaults used
958    phx_string = "MST-07"; // no-dst
959    time_zone_ptr nyc_2(new posix_time_zone(nyc_string));
960    time_zone_ptr phx_2(new posix_time_zone(phx_string));
961
962
963    /***** show the sets are equal *****/
964
965    std::cout &lt;&lt; "The first zone is in daylight savings from:\n "
966      &lt;&lt; nyc_1-&gt;dst_local_start_time(2004) &lt;&lt; " through "
967      &lt;&lt; nyc_1-&gt;dst_local_end_time(2004) &lt;&lt; std::endl;
968
969    std::cout &lt;&lt; "The second zone is in daylight savings from:\n "
970      &lt;&lt; nyc_2-&gt;dst_local_start_time(2004) &lt;&lt; " through "
971      &lt;&lt; nyc_2-&gt;dst_local_end_time(2004) &lt;&lt; std::endl;
972
973    std::cout &lt;&lt; "The third zone (no daylight savings):\n "
974      &lt;&lt; phx_1-&gt;std_zone_abbrev() &lt;&lt; " and "
975      &lt;&lt; phx_1-&gt;base_utc_offset() &lt;&lt; std::endl;
976
977    std::cout &lt;&lt; "The fourth zone (no daylight savings):\n "
978      &lt;&lt; phx_2-&gt;std_zone_abbrev() &lt;&lt; " and "
979      &lt;&lt; phx_2-&gt;base_utc_offset() &lt;&lt; std::endl;
980
981    return 0;
982  }
983
984  </pre>
985</div>
986<div class="section">
987<div class="titlepage"><div><div><h3 class="title">
988<a name="date_time.examples.calc_rules"></a>Daylight Savings Calc Rules</h3></div></div></div>
989<p>
990    Example of creating various Daylight Savings Calc Rule objects.
991  </p>
992<pre class="programlisting">
993
994  /* A simple example for creating various dst_calc_rule instances
995   */
996
997  #include "boost/date_time/gregorian/gregorian.hpp"
998  #include "boost/date_time/local_time/local_time.hpp"
999  #include &lt;iostream&gt;
1000
1001  int
1002  main()
1003  {
1004    using namespace boost;
1005    using namespace local_time;
1006    using namespace gregorian;
1007
1008    /***** create the necessary date_generator objects *****/
1009    // starting generators
1010    first_day_of_the_week_in_month fd_start(Sunday, May);
1011    last_day_of_the_week_in_month ld_start(Sunday, May);
1012    nth_day_of_the_week_in_month nkd_start(nth_day_of_the_week_in_month::third,
1013                                           Sunday, May);
1014    partial_date pd_start(1, May);
1015    // ending generators
1016    first_day_of_the_week_in_month fd_end(Sunday, Oct);
1017    last_day_of_the_week_in_month ld_end(Sunday, Oct);
1018    nth_day_of_the_week_in_month nkd_end(nth_day_of_the_week_in_month::third,
1019                                         Sunday, Oct);
1020    partial_date pd_end(31, Oct);
1021
1022    /***** create the various dst_calc_rule objects *****/
1023    dst_calc_rule_ptr pdr(new partial_date_dst_rule(pd_start, pd_end));
1024    dst_calc_rule_ptr flr(new first_last_dst_rule(fd_start, ld_end));
1025    dst_calc_rule_ptr llr(new last_last_dst_rule(ld_start, ld_end));
1026    dst_calc_rule_ptr nlr(new nth_last_dst_rule(nkd_start, ld_end));
1027    dst_calc_rule_ptr ndr(new nth_day_of_the_week_in_month_dst_rule(nkd_start,
1028                                                                    nkd_end));
1029
1030    return 0;
1031  }
1032
1033
1034  </pre>
1035</div>
1036<div class="section">
1037<div class="titlepage"><div><div><h3 class="title">
1038<a name="date_time.examples.flight"></a>Flight Time Example</h3></div></div></div>
1039<p>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).</p>
1040<pre class="programlisting">
1041<code class="literal">
1042<span class="preprocessor">#include</span><span class="string"> "boost/date_time/local_time/local_time.hpp"</span><span class="preprocessor">
1043#include</span><span class="special"> &lt;</span><span class="identifier">iostream</span><span class="special">&gt;</span><span class="comment">
1044
1045/* This example shows a program that calculates the arrival time of a plane
1046 * that flys from Phoenix to New York.  During the flight New York shifts
1047 * into daylight savings time (Phoenix doesn't because Arizona doesn't use
1048 * dst).
1049 *
1050 *
1051 */</span><span class="keyword">
1052
1053int</span><span class="identifier"> main</span><span class="special">()</span><span class="special">
1054{</span><span class="keyword">
1055  using</span><span class="keyword"> namespace</span><span class="identifier"> boost</span><span class="special">::</span><span class="identifier">gregorian</span><span class="special">;</span><span class="keyword">
1056  using</span><span class="keyword"> namespace</span><span class="identifier"> boost</span><span class="special">::</span><span class="identifier">local_time</span><span class="special">;</span><span class="keyword">
1057  using</span><span class="keyword"> namespace</span><span class="identifier"> boost</span><span class="special">::</span><span class="identifier">posix_time</span><span class="special">;</span><span class="comment">
1058
1059
1060  //setup some timezones for creating and adjusting local times
1061  //This user editable file can be found in libs/date_time/data.
1062</span><span class="identifier">  tz_database</span><span class="identifier"> tz_db</span><span class="special">;</span><span class="identifier">
1063  tz_db</span><span class="special">.</span><span class="identifier">load_from_file</span><span class="special">(</span><span class="string">"date_time_zonespec.csv"</span><span class="special">);</span><span class="identifier">
1064  time_zone_ptr</span><span class="identifier"> nyc_tz</span><span class="special"> =</span><span class="identifier"> tz_db</span><span class="special">.</span><span class="identifier">time_zone_from_region</span><span class="special">(</span><span class="string">"America/New_York"</span><span class="special">);</span><span class="comment">
1065  //Use a
1066</span><span class="identifier">  time_zone_ptr</span><span class="identifier"> phx_tz</span><span class="special">(</span><span class="keyword">new</span><span class="identifier"> posix_time_zone</span><span class="special">(</span><span class="string">"MST-07:00:00"</span><span class="special">));</span><span class="comment">
1067
1068  //local departure time in phoenix is 11 pm on april 2 2005
1069  // (ny changes to dst on apr 3 at 2 am)
1070</span><span class="identifier">  local_date_time</span><span class="identifier"> phx_departure</span><span class="special">(</span><span class="identifier">date</span><span class="special">(</span><span class="number">2005</span><span class="special">,</span><span class="identifier"> Apr</span><span class="special">,</span><span class="number"> 2</span><span class="special">),</span><span class="identifier"> hours</span><span class="special">(</span><span class="number">23</span><span class="special">),</span><span class="identifier">
1071                                phx_tz</span><span class="special">,</span><span class="identifier">
1072                                local_date_time</span><span class="special">::</span><span class="identifier">NOT_DATE_TIME_ON_ERROR</span><span class="special">);</span><span class="identifier">
1073
1074  time_duration</span><span class="identifier"> flight_length</span><span class="special"> =</span><span class="identifier"> hours</span><span class="special">(</span><span class="number">4</span><span class="special">)</span><span class="special"> +</span><span class="identifier"> minutes</span><span class="special">(</span><span class="number">30</span><span class="special">);</span><span class="identifier">
1075  local_date_time</span><span class="identifier"> phx_arrival</span><span class="special"> =</span><span class="identifier"> phx_departure</span><span class="special"> +</span><span class="identifier"> flight_length</span><span class="special">;</span><span class="identifier">
1076  local_date_time</span><span class="identifier"> nyc_arrival</span><span class="special"> =</span><span class="identifier"> phx_arrival</span><span class="special">.</span><span class="identifier">local_time_in</span><span class="special">(</span><span class="identifier">nyc_tz</span><span class="special">);</span><span class="identifier">
1077
1078  std</span><span class="special">::</span><span class="identifier">cout</span><span class="special"> &lt;&lt;</span><span class="string"> "departure phx time: "</span><span class="special"> &lt;&lt;</span><span class="identifier"> phx_departure</span><span class="special"> &lt;&lt;</span><span class="identifier"> std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span><span class="identifier">
1079  std</span><span class="special">::</span><span class="identifier">cout</span><span class="special"> &lt;&lt;</span><span class="string"> "arrival phx time:   "</span><span class="special"> &lt;&lt;</span><span class="identifier"> phx_arrival</span><span class="special"> &lt;&lt;</span><span class="identifier"> std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span><span class="identifier">
1080  std</span><span class="special">::</span><span class="identifier">cout</span><span class="special"> &lt;&lt;</span><span class="string"> "arrival nyc time:   "</span><span class="special"> &lt;&lt;</span><span class="identifier"> nyc_arrival</span><span class="special"> &lt;&lt;</span><span class="identifier"> std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span><span class="special">
1081
1082}</span>
1083</code>
1084</pre>
1085</div>
1086<div class="section">
1087<div class="titlepage"><div><div><h3 class="title">
1088<a name="date_time.examples.seconds_since_epoch"></a>Seconds Since Epoch</h3></div></div></div>
1089<p>
1090    Example of calculating seconds elapsed since epoch (1970-Jan-1) using local_date_time.
1091  </p>
1092<pre class="programlisting">
1093
1094  /* This example demonstrates the use of the time zone database and
1095   * local time to calculate the number of seconds since the UTC
1096   * time_t epoch 1970-01-01 00:00:00.  Note that the selected timezone
1097   * could be any timezone supported in the time zone database file which
1098   * can be modified and updated as needed by the user.
1099   *
1100   * To solve this problem the following steps are required:
1101   * 1) Get a timezone from the tz database for the local time
1102   * 2) Construct a local time using the timezone
1103   * 3) Construct a posix_time::ptime for the time_t epoch time
1104   * 4) Convert the local_time to utc and subtract the epoch time
1105   *
1106   */
1107
1108  #include "boost/date_time/local_time/local_time.hpp"
1109  #include &lt;iostream&gt;
1110
1111  int main()
1112  {
1113    using namespace boost::gregorian;
1114    using namespace boost::local_time;
1115    using namespace boost::posix_time;
1116
1117    tz_database tz_db;
1118    try {
1119      tz_db.load_from_file("../data/date_time_zonespec.csv");
1120    }catch(data_not_accessible dna) {
1121      std::cerr &lt;&lt; "Error with time zone data file: " &lt;&lt; dna.what() &lt;&lt; std::endl;
1122      exit(EXIT_FAILURE);
1123    }catch(bad_field_count bfc) {
1124      std::cerr &lt;&lt; "Error with time zone data file: " &lt;&lt; bfc.what() &lt;&lt; std::endl;
1125      exit(EXIT_FAILURE);
1126    }
1127
1128    time_zone_ptr nyc_tz = tz_db.time_zone_from_region("America/New_York");
1129    date in_date(2004,10,04);
1130    time_duration td(12,14,32);
1131    // construct with local time value
1132    // create not-a-date-time if invalid (eg: in dst transition)
1133    local_date_time nyc_time(in_date,
1134                             td,
1135                             nyc_tz,
1136                             local_date_time::NOT_DATE_TIME_ON_ERROR);
1137
1138    std::cout &lt;&lt; nyc_time &lt;&lt; std::endl;
1139
1140    ptime time_t_epoch(date(1970,1,1));
1141    std::cout &lt;&lt; time_t_epoch &lt;&lt; std::endl;
1142
1143    // first convert nyc_time to utc via the utc_time()
1144    // call and subtract the ptime.
1145    time_duration diff = nyc_time.utc_time() - time_t_epoch;
1146
1147    //Expected 1096906472
1148    std::cout &lt;&lt; "Seconds diff: " &lt;&lt; diff.total_seconds() &lt;&lt; std::endl;
1149
1150  }
1151
1152  </pre>
1153</div>
1154</div>
1155<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
1156<td align="left"></td>
1157<td align="right"><div class="copyright-footer">Copyright © 2001-2005 CrystalClear Software, Inc<p>Subject to the Boost Software License, Version 1.0. (See accompanying file
1158    <code class="filename">LICENSE_1_0.txt</code> or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)</p>
1159</div></td>
1160</tr></table>
1161<hr>
1162<div class="spirit-nav">
1163<a accesskey="p" href="details.html"><img src="../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../date_time.html"><img src="../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="doxy.html"><img src="../../../doc/src/images/next.png" alt="Next"></a>
1164</div>
1165</body>
1166</html>
1167