testlocal_time_period.cpp 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /* Copyright (c) 2005 CrystalClear Software, Inc.
  2. * Use, modification and distribution is subject to the
  3. * Boost Software License, Version 1.0. (See accompanying
  4. * file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
  5. * Author: Jeff Garland
  6. */
  7. #include "boost/date_time/local_time/local_time.hpp"
  8. #include "../testfrmwk.hpp"
  9. int main()
  10. {
  11. using namespace boost::gregorian;
  12. using namespace boost::posix_time;
  13. using namespace boost::local_time;
  14. date d1(2001,Jan, 1);
  15. time_zone_ptr az_tz(new posix_time_zone("MST-07"));
  16. local_date_time t1 (d1,hours(2), az_tz, false);//2001-Jan-1 02:00:00
  17. local_date_time t2 (d1,hours(3), az_tz, false);//2001-Jan-1 03:00:00
  18. local_time_period p1(t1,t2); //2001-Jan-1 02:59:59
  19. local_time_period p2(p1);
  20. check("copy construct & ==", p1 == p2);
  21. local_time_period p3 = p2;
  22. check("assignment", p3 == p2);
  23. local_time_period p4(t1,hours(1));
  24. check("length", p4.length() == hours(1));
  25. // std::cout << to_simple_string(t1) << std::endl;
  26. // std::cout << to_simple_string(p4) << std::endl;
  27. // std::cout << to_simple_string(p1) << std::endl;
  28. check("construction and ==", p1 == p4);
  29. check("begin", p1.begin() == t1);
  30. check("last", p1.end() == t2);
  31. check("end", p1.last() == t2-time_duration::unit());
  32. // std::cout << to_simple_string(p1) << std::endl;
  33. // // check("last", p1.() == t2);
  34. check("contains begin", p1.contains(t1));
  35. check("contains end-not", !p1.contains(t2));
  36. check("contains last", p1.contains(t2-seconds(1)));
  37. local_date_time t3(date(2001,Jan,1),hours(4), az_tz, false);
  38. local_time_period p5(t2,t3);
  39. check("check contains", !p1.contains(p5.begin()));
  40. check("check contains", !p5.contains(p1.begin()));
  41. check("operator== not equal case", !(p1 == p5));
  42. check("less than order", p1 < p5);
  43. check("greater than order", p5 > p1);
  44. check("not equal", p5 != p1);
  45. check("intersects with myself", p1.intersects(p1));
  46. check("not intersects", !(p1.intersects(p5)));
  47. check("not intersects", !(p5.intersects(p1)));
  48. local_time_period p6(p5);
  49. p6.shift(minutes(30));
  50. // std::cout << to_simple_string(p5) << std::endl;
  51. // std::cout << to_simple_string(p6) << std::endl;
  52. check("shifted intersects", p5.intersects(p6));
  53. check("shifted intersects", p6.intersects(p5));
  54. check("contains begin", p5.contains(p6.begin()));
  55. p6.shift(minutes(30));
  56. // std::cout << to_simple_string(p5) << std::endl;
  57. // std::cout << to_simple_string(p6) << std::endl;
  58. check("shifted !intersects", !p5.intersects(p6));
  59. check("shifted !intersects", !p6.intersects(p5));
  60. p6.shift(minutes(-30));
  61. // std::cout << to_simple_string(p5) << std::endl;
  62. // std::cout << to_simple_string(p6) << std::endl;
  63. local_time_period p7 = p5.intersection(p6);
  64. // std::cout << to_simple_string(p7) << std::endl;
  65. check("shifted intersection",
  66. p7 == local_time_period(local_date_time(d1,time_duration(3,30,0), az_tz, false),
  67. local_date_time(d1,time_duration(4,0,0), az_tz, false)));
  68. return printTestStats();
  69. }