1 /* Print the remaining hours of the day 2 * Uses the clock to get the local time 3 * Use an iterator to iterate over the remaining hours 4 * Retrieve the date part from a time 5 * 6 * Expected Output something like: 7 * 8 * 2002-Mar-08 16:30:59 9 * 2002-Mar-08 17:30:59 10 * 2002-Mar-08 18:30:59 11 * 2002-Mar-08 19:30:59 12 * 2002-Mar-08 20:30:59 13 * 2002-Mar-08 21:30:59 14 * 2002-Mar-08 22:30:59 15 * 2002-Mar-08 23:30:59 16 * Time left till midnight: 07:29:01 17 */ 18 19 #include "boost/date_time/posix_time/posix_time.hpp" 20 #include <iostream> 21 22 int main()23main() 24 { 25 using namespace boost::posix_time; 26 using namespace boost::gregorian; 27 28 //get the current time from the clock -- one second resolution 29 ptime now = second_clock::local_time(); 30 //Get the date part out of the time 31 date today = now.date(); 32 date tomorrow = today + days(1); 33 ptime tomorrow_start(tomorrow); //midnight 34 35 //iterator adds by one hour 36 time_iterator titr(now,hours(1)); 37 for (; titr < tomorrow_start; ++titr) { 38 std::cout << to_simple_string(*titr) << std::endl; 39 } 40 41 time_duration remaining = tomorrow_start - now; 42 std::cout << "Time left till midnight: " 43 << to_simple_string(remaining) << std::endl; 44 return 0; 45 } 46 47 /* Copyright 2001-2004: CrystalClear Software, Inc 48 * http://www.crystalclearsoftware.com 49 * 50 * Subject to the Boost Software License, Version 1.0. 51 * (See accompanying file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt) 52 */ 53 54