ex_time_math.xml 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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_math">
  9. <title>Time Math</title>
  10. <para>
  11. Various types of calculations with times and time durations.
  12. </para>
  13. <programlisting>
  14. <![CDATA[
  15. /* Some simple examples of constructing and calculating with times
  16. * Output:
  17. * 2002-Feb-01 00:00:00 - 2002-Feb-01 05:04:02.001000000 = -5:04:02.001000000
  18. */
  19. #include "boost/date_time/posix_time/posix_time.hpp"
  20. #include <iostream>
  21. int
  22. main()
  23. {
  24. using namespace boost::posix_time;
  25. using namespace boost::gregorian;
  26. date d(2002,Feb,1); //an arbitrary date
  27. //construct a time by adding up some durations durations
  28. ptime t1(d, hours(5)+minutes(4)+seconds(2)+millisec(1));
  29. //construct a new time by subtracting some times
  30. ptime t2 = t1 - hours(5)- minutes(4)- seconds(2)- millisec(1);
  31. //construct a duration by taking the difference between times
  32. time_duration td = t2 - t1;
  33. std::cout << to_simple_string(t2) << " - "
  34. << to_simple_string(t1) << " = "
  35. << to_simple_string(td) << std::endl;
  36. return 0;
  37. }
  38. ]]>
  39. </programlisting>
  40. </section>