ex_time_periods.xml 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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.time_periods">
  9. <title>Time Periods</title>
  10. <para>
  11. Demonstrate some simple uses of time periods.
  12. </para>
  13. <programlisting>
  14. <![CDATA[
  15. /* Some simple examples of constructing and calculating with times
  16. * Returns:
  17. * [2002-Feb-01 00:00:00/2002-Feb-01 23:59:59.999999999]
  18. * contains 2002-Feb-01 03:00:05
  19. * [2002-Feb-01 00:00:00/2002-Feb-01 23:59:59.999999999]
  20. * intersected with
  21. * [2002-Feb-01 00:00:00/2002-Feb-01 03:00:04.999999999]
  22. * is
  23. * [2002-Feb-01 00:00:00/2002-Feb-01 03:00:04.999999999]
  24. */
  25. #include "boost/date_time/posix_time/posix_time.hpp"
  26. #include <iostream>
  27. using namespace boost::posix_time;
  28. using namespace boost::gregorian;
  29. //Create a simple period class to contain all the times in a day
  30. class day_period : public time_period
  31. {
  32. public:
  33. day_period(date d) : time_period(ptime(d),//midnight
  34. ptime(d,hours(24)))
  35. {}
  36. };
  37. int
  38. main()
  39. {
  40. date d(2002,Feb,1); //an arbitrary date
  41. //a period that represents a day
  42. day_period dp(d);
  43. ptime t(d, hours(3)+seconds(5)); //an arbitray time on that day
  44. if (dp.contains(t)) {
  45. std::cout << to_simple_string(dp) << " contains "
  46. << to_simple_string(t) << std::endl;
  47. }
  48. //a period that represents part of the day
  49. time_period part_of_day(ptime(d, hours(0)), t);
  50. //intersect the 2 periods and print the results
  51. if (part_of_day.intersects(dp)) {
  52. time_period result = part_of_day.intersection(dp);
  53. std::cout << to_simple_string(dp) << " intersected with\n"
  54. << to_simple_string(part_of_day) << " is \n"
  55. << to_simple_string(result) << std::endl;
  56. }
  57. return 0;
  58. }
  59. ]]>
  60. </programlisting>
  61. </section>