• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
5<!-- Copyright (c) 2001-2004 CrystalClear Software, Inc.
6     Subject to the Boost Software License, Version 1.0.
7     (See accompanying file LICENSE_1_0.txt or  http://www.boost.org/LICENSE_1_0.txt)
8-->
9
10<section id="date_time.examples.time_math">
11  <title>Time Math</title>
12
13  <para>
14    Various types of calculations with times and time durations.
15  </para>
16  <programlisting>
17    <![CDATA[
18  /* Some simple examples of constructing and calculating with times
19   * Output:
20   * 2002-Feb-01 00:00:00 - 2002-Feb-01 05:04:02.001000000 = -5:04:02.001000000
21   */
22
23  #include "boost/date_time/posix_time/posix_time.hpp"
24  #include <iostream>
25
26  int
27  main()
28  {
29    using namespace boost::posix_time;
30    using namespace boost::gregorian;
31
32    date d(2002,Feb,1); //an arbitrary date
33    //construct a time by adding up some durations durations
34    ptime t1(d, hours(5)+minutes(4)+seconds(2)+millisec(1));
35    //construct a new time by subtracting some times
36    ptime t2 = t1 - hours(5)- minutes(4)- seconds(2)- millisec(1);
37    //construct a duration by taking the difference between times
38    time_duration td = t2 - t1;
39
40    std::cout << to_simple_string(t2) << " - "
41              << to_simple_string(t1) << " = "
42              << to_simple_string(td) << std::endl;
43
44    return 0;
45  }
46    ]]>
47  </programlisting>
48</section>
49