print_hours.cpp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. #include "boost/date_time/posix_time/posix_time.hpp"
  19. #include <iostream>
  20. int
  21. main()
  22. {
  23. using namespace boost::posix_time;
  24. using namespace boost::gregorian;
  25. //get the current time from the clock -- one second resolution
  26. ptime now = second_clock::local_time();
  27. //Get the date part out of the time
  28. date today = now.date();
  29. date tomorrow = today + days(1);
  30. ptime tomorrow_start(tomorrow); //midnight
  31. //iterator adds by one hour
  32. time_iterator titr(now,hours(1));
  33. for (; titr < tomorrow_start; ++titr) {
  34. std::cout << to_simple_string(*titr) << std::endl;
  35. }
  36. time_duration remaining = tomorrow_start - now;
  37. std::cout << "Time left till midnight: "
  38. << to_simple_string(remaining) << std::endl;
  39. return 0;
  40. }
  41. /* Copyright 2001-2004: CrystalClear Software, Inc
  42. * http://www.crystalclearsoftware.com
  43. *
  44. * Subject to the Boost Software License, Version 1.0.
  45. * (See accompanying file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
  46. */