ex_print_hours.xml 1.8 KB

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