time_periods.cpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /* Some simple examples of constructing and calculating with times
  2. * Returns:
  3. * [2002-Feb-01 00:00:00/2002-Feb-01 23:59:59.999999999] contains 2002-Feb-01 03:00:05
  4. * [2002-Feb-01 00:00:00/2002-Feb-01 23:59:59.999999999] intersected with
  5. * [2002-Feb-01 00:00:00/2002-Feb-01 03:00:04.999999999] is
  6. * [2002-Feb-01 00:00:00/2002-Feb-01 03:00:04.999999999]
  7. */
  8. #include "boost/date_time/posix_time/posix_time.hpp"
  9. #include <iostream>
  10. using namespace boost::posix_time;
  11. using namespace boost::gregorian;
  12. //Create a simple period class to contain all the times in a day
  13. class day_period : public time_period
  14. {
  15. public:
  16. day_period(date d) : time_period(ptime(d),//midnight
  17. ptime(d,hours(24)))
  18. {}
  19. };
  20. int
  21. main()
  22. {
  23. date d(2002,Feb,1); //an arbitrary date
  24. //a period that represents a day
  25. day_period dp(d);
  26. ptime t(d, hours(3)+seconds(5)); //an arbitray time on that day
  27. if (dp.contains(t)) {
  28. std::cout << to_simple_string(dp) << " contains "
  29. << to_simple_string(t) << std::endl;
  30. }
  31. //a period that represents part of the day
  32. time_period part_of_day(ptime(d, hours(0)), t);
  33. //intersect the 2 periods and print the results
  34. if (part_of_day.intersects(dp)) {
  35. time_period result = part_of_day.intersection(dp);
  36. std::cout << to_simple_string(dp) << " intersected with\n"
  37. << to_simple_string(part_of_day) << " is \n"
  38. << to_simple_string(result) << std::endl;
  39. }
  40. return 0;
  41. }
  42. /* Copyright 2001-2004: CrystalClear Software, Inc
  43. * http://www.crystalclearsoftware.com
  44. *
  45. * Subject to the Boost Software License, Version 1.0.
  46. * (See accompanying file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
  47. */