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