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-2004 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.print_hours"> 11 <title>Print Hours</title> 12 13 <para> 14 Demonstrate time iteration, clock retrieval, and simple calculation. 15 </para> 16 <programlisting> 17 <![CDATA[ 18 /* Print the remaining hours of the day 19 * Uses the clock to get the local time 20 * Use an iterator to iterate over the remaining hours 21 * Retrieve the date part from a time 22 * 23 * Expected Output something like: 24 * 25 * 2002-Mar-08 16:30:59 26 * 2002-Mar-08 17:30:59 27 * 2002-Mar-08 18:30:59 28 * 2002-Mar-08 19:30:59 29 * 2002-Mar-08 20:30:59 30 * 2002-Mar-08 21:30:59 31 * 2002-Mar-08 22:30:59 32 * 2002-Mar-08 23:30:59 33 * Time left till midnight: 07:29:01 34 */ 35 36 #include "boost/date_time/posix_time/posix_time.hpp" 37 #include <iostream> 38 39 int 40 main() 41 { 42 using namespace boost::posix_time; 43 using namespace boost::gregorian; 44 45 //get the current time from the clock -- one second resolution 46 ptime now = second_clock::local_time(); 47 //Get the date part out of the time 48 date today = now.date(); 49 date tommorrow = today + days(1); 50 ptime tommorrow_start(tommorrow); //midnight 51 52 //iterator adds by one hour 53 time_iterator titr(now,hours(1)); 54 for (; titr < tommorrow_start; ++titr) { 55 std::cout << to_simple_string(*titr) << std::endl; 56 } 57 58 time_duration remaining = tommorrow_start - now; 59 std::cout << "Time left till midnight: " 60 << to_simple_string(remaining) << std::endl; 61 return 0; 62 } 63 64 ]]> 65 </programlisting> 66</section> 67