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>General Usage 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="../../date_time.html" title="Chapter 13. Boost.Date_Time"> 11<link rel="next" href="../gregorian.html" title="Gregorian"> 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="../../date_time.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="../gregorian.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.general_usage_examples"></a>General Usage Examples</h2></div></div></div> 29<p> 30 The following provides some sample usage of dates. 31 See <a class="link" href="../gregorian.html" title="Gregorian">Date Programming</a> 32 for more details. 33 34 </p> 35<pre class="programlisting"><span class="keyword">using namespace</span> boost::gregorian; 36 date weekstart(<span class="number">2002</span>,Feb,<span class="number">1</span>); 37 date weekend = weekstart + weeks(<span class="number">1</span>); 38 date d2 = d1 + days(<span class="number">5</span>); 39 date today = day_clock::local_day(); 40 if (d2 >= today) {} <span class="comment">//date comparison operators</span> 41 42 date_period thisWeek(d1,d2); 43 <span class="keyword">if</span> (thisWeek.contains(today)) {}<span class="comment">//do something 44 45 //iterate and print the week</span> 46 day_iterator itr(weekstart); 47 <span class="keyword">while</span> (itr <= weekend) { 48 std::cout << (*itr) << std::endl; 49 ++itr; 50 } 51 <span class="comment">//input streaming</span> 52 std::stringstream ss(<span class="string">"2004-Jan-1"</span>); 53 ss >> d3; 54 55 <span class="comment">//date generator functions</span> 56 date d5 = next_weekday(d4, Sunday); <span class="comment">//calculate Sunday following d4 57 58 //US labor day is first Monday in Sept</span> 59 <span class="keyword">typedef</span> nth_day_of_the_week_in_month nth_dow; 60 nth_dow labor_day(nth_dow::first,Monday, Sep); 61 <span class="comment">//calculate a specific date for 2004 from functor</span> 62 date d6 = labor_day.get_date(<span class="number">2004</span>); 63 </pre> 64<p> 65 66 The following provides some example code using times. 67 See <a class="link" href="../posix_time.html" title="Posix Time">Time Programming</a> 68 for more details. 69 70 </p> 71<pre class="programlisting"><span class="keyword">using namespace</span> boost::posix_time; 72 date d(<span class="number">2002</span>,Feb,<span class="number">1</span>); <span class="comment">//an arbitrary date</span> 73 ptime t1(d, hours(<span class="number">5</span>)+nanosec(<span class="number">100</span>)); <span class="comment">//date + time of day offset</span> 74 ptime t2 = t1 - minutes(<span class="number">4</span>)+seconds(<span class="number">2</span>); 75 ptime now = second_clock::local_time(); <span class="comment">//use the clock</span> 76 date today = now.date(); <span class="comment">//Get the date part out of the time</span> 77 date tomorrow = today + date_duration(<span class="number">1</span>); 78 ptime tomorrow_start(tomorrow); <span class="comment">//midnight 79 80 //input streaming</span> 81 std::stringstream ss(<span class="string">"2004-Jan-1 05:21:33.20"</span>); 82 ss >> t2; 83 84 <span class="comment">//starting at current time iterator adds by one hour</span> 85 time_iterator titr(now,hours(<span class="number">1</span>)); 86 <span class="keyword">for</span> (; titr < tomorrow_start; ++titr) { 87 std::cout << (*titr) << std::endl; 88 } 89 </pre> 90<p> 91 </p> 92<p> 93 The following provides some example code using times. 94 See <a class="link" href="../local_time.html" title="Local Time">Local Time Programming</a> 95 for more details. 96 97 </p> 98<pre class="programlisting"> 99 <span class="keyword">using namespace</span> boost::local_time; 100 <span class="comment">//setup some timezones for creating and adjusting times 101 //first time zone uses the time zone file for regional timezone definitions</span> 102 tz_database tz_db; 103 tz_db.load_from_file(<span class="string">"date_time_zonespec.csv"</span>); 104 time_zone_ptr nyc_tz = tz_db.time_zone_from_region(<span class="string">"America/New_York"</span>); 105 <span class="comment">//This timezone uses a posix time zone string definition to create a time zone</span> 106 time_zone_ptr phx_tz(new posix_time_zone(<span class="string">"MST-07:00:00"</span>)); 107 108 <span class="comment">//local departure time in phoenix is 11 pm on April 2 2005 109 // Note that New York changes to daylight savings on Apr 3 at 2 am)</span> 110 local_date_time phx_departure(date(<span class="number">2005</span>, Apr, <span class="number">2</span>), hours(<span class="number">23</span>), phx_tz, 111 local_date_time::NOT_DATE_TIME_ON_ERROR); 112 113 time_duration flight_length = hours(<span class="number">4</span>) + minutes(<span class="number">30</span>); 114 local_date_time phx_arrival = phx_departure + flight_length; 115 <span class="comment">//convert the phx time to a nyz time</span> 116 local_date_time nyc_arrival = phx_arrival.local_time_in(nyc_tz); 117 118 <span class="comment">//2005-Apr-03 06:30:00 EDT</span> 119 std::cout << nyc_arrival << std::endl; 120 </pre> 121<p> 122 </p> 123</div> 124<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 125<td align="left"></td> 126<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 127 <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> 128</div></td> 129</tr></table> 130<hr> 131<div class="spirit-nav"> 132<a accesskey="p" href="../../date_time.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="../gregorian.html"><img src="../../../../doc/src/images/next.png" alt="Next"></a> 133</div> 134</body> 135</html> 136