time_math.cpp 1.1 KB

123456789101112131415161718192021222324252627282930313233343536
  1. /* Some simple examples of constructing and calculating with times
  2. * Output:
  3. * 2002-Feb-01 00:00:00 - 2002-Feb-01 05:04:02.001000000 = -5:04:02.001000000
  4. */
  5. #include "boost/date_time/posix_time/posix_time.hpp"
  6. #include <iostream>
  7. int
  8. main()
  9. {
  10. using namespace boost::posix_time;
  11. using namespace boost::gregorian;
  12. date d(2002,Feb,1); //an arbitrary date
  13. //construct a time by adding up some durations
  14. ptime t1(d, hours(5)+minutes(4)+seconds(2)+milliseconds(1));
  15. //construct a new time by subtracting some times
  16. ptime t2 = t1 - hours(5)- minutes(4)- seconds(2)- milliseconds(1);
  17. //construct a duration by taking the difference between times
  18. time_duration td = t2 - t1;
  19. std::cout << to_simple_string(t2) << " - "
  20. << to_simple_string(t1) << " = "
  21. << to_simple_string(td) << std::endl;
  22. return 0;
  23. }
  24. /* Copyright 2001-2004: CrystalClear Software, Inc
  25. * http://www.crystalclearsoftware.com
  26. *
  27. * Subject to the Boost Software License, Version 1.0.
  28. * (See accompanying file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
  29. */