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.txt or http://www.boost.org/LICENSE_1_0.txt) 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 weekstart(<emphasis role="number">2002</emphasis>,Feb,<emphasis role="number">1</emphasis>); 20 date weekend = weekstart + weeks(<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 >= 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(weekstart); 30 <emphasis role="keyword">while</emphasis> (itr <= weekend) { 31 std::cout << (*itr) << 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 >> 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 <emphasis role="keyword">typedef</emphasis> nth_day_of_the_week_in_month nth_dow; 43 nth_dow labor_day(nth_dow::first,Monday, Sep); 44 <emphasis role="comment">//calculate a specific date for 2004 from functor</emphasis> 45 date d6 = labor_day.get_date(<emphasis role="number">2004</emphasis>); 46 </programlisting> 47 48 The following provides some example code using times. 49 See <link linkend="date_time.posix_time">Time Programming</link> 50 for more details. 51 52 <programlisting><emphasis role="keyword">using namespace</emphasis> boost::posix_time; 53 date d(<emphasis role="number">2002</emphasis>,Feb,<emphasis role="number">1</emphasis>); <emphasis role="comment">//an arbitrary date</emphasis> 54 ptime t1(d, hours(<emphasis role="number">5</emphasis>)+nanosec(<emphasis role="number">100</emphasis>)); <emphasis role="comment">//date + time of day offset</emphasis> 55 ptime t2 = t1 - minutes(<emphasis role="number">4</emphasis>)+seconds(<emphasis role="number">2</emphasis>); 56 ptime now = second_clock::local_time(); <emphasis role="comment">//use the clock</emphasis> 57 date today = now.date(); <emphasis role="comment">//Get the date part out of the time</emphasis> 58 date tomorrow = today + date_duration(<emphasis role="number">1</emphasis>); 59 ptime tomorrow_start(tomorrow); <emphasis role="comment">//midnight 60 61 //input streaming</emphasis> 62 std::stringstream ss(<emphasis role="string">"2004-Jan-1 05:21:33.20"</emphasis>); 63 ss >> t2; 64 65 <emphasis role="comment">//starting at current time iterator adds by one hour</emphasis> 66 time_iterator titr(now,hours(<emphasis role="number">1</emphasis>)); 67 <emphasis role="keyword">for</emphasis> (; titr < tomorrow_start; ++titr) { 68 std::cout << (*titr) << std::endl; 69 } 70 </programlisting> 71 </para> 72 73 <para> 74 The following provides some example code using times. 75 See <link linkend="date_time.local_time">Local Time Programming</link> 76 for more details. 77 78 <programlisting> 79 <emphasis role="keyword">using namespace</emphasis> boost::local_time; 80 <emphasis role="comment">//setup some timezones for creating and adjusting times 81 //first time zone uses the time zone file for regional timezone definitions</emphasis> 82 tz_database tz_db; 83 tz_db.load_from_file(<emphasis role="string">"date_time_zonespec.csv"</emphasis>); 84 time_zone_ptr nyc_tz = tz_db.time_zone_from_region(<emphasis role="string">"America/New_York"</emphasis>); 85 <emphasis role="comment">//This timezone uses a posix time zone string definition to create a time zone</emphasis> 86 time_zone_ptr phx_tz(new posix_time_zone(<emphasis role="string">"MST-07:00:00"</emphasis>)); 87 88 <emphasis role="comment">//local departure time in phoenix is 11 pm on April 2 2005 89 // Note that New York changes to daylight savings on Apr 3 at 2 am)</emphasis> 90 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, 91 local_date_time::NOT_DATE_TIME_ON_ERROR); 92 93 time_duration flight_length = hours(<emphasis role="number">4</emphasis>) + minutes(<emphasis role="number">30</emphasis>); 94 local_date_time phx_arrival = phx_departure + flight_length; 95 <emphasis role="comment">//convert the phx time to a nyz time</emphasis> 96 local_date_time nyc_arrival = phx_arrival.local_time_in(nyc_tz); 97 98 <emphasis role="comment">//2005-Apr-03 06:30:00 EDT</emphasis> 99 std::cout << nyc_arrival << std::endl; 100 </programlisting> 101 </para> 102 103</section> 104