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