testlocal_time_iterator.cpp 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /* Copyright (c) 2002,2003 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, Bart Garst
  6. * $Date$
  7. */
  8. #include <iostream>
  9. #include "boost/date_time/local_time/local_time.hpp"
  10. #include "../testfrmwk.hpp"
  11. // #if defined(BOOST_DATE_TIME_INCLUDE_LIMITED_HEADERS)
  12. // #include "boost/date_time/gregorian/formatters_limited.hpp"
  13. // #else
  14. // #include "boost/date_time/gregorian/formatters.hpp"
  15. // #endif
  16. using namespace boost::posix_time;
  17. using namespace boost::gregorian;
  18. using namespace boost::local_time;
  19. void iterate_backward(const local_date_time *answers,
  20. int ary_len,
  21. const time_duration& td)
  22. {
  23. int i = ary_len -1;
  24. local_date_time end = answers[i];
  25. local_time_iterator titr(end,td);
  26. std::cout << "counting down by previous duration..." << std::endl;
  27. for (; titr >= answers[0]; --titr) {
  28. std::cout << *titr << std::endl;
  29. check("iterating backward", answers[i] == *titr);
  30. --i;
  31. }
  32. check("iterating backward count", i == -1); // check the number of iterations
  33. std::cout << std::endl;
  34. }
  35. int
  36. main()
  37. {
  38. time_zone_ptr ny_tz(new posix_time_zone("EST-05EDT,M4.1.0,M10.5.0"));
  39. //set up a time right on the dst boundary -- iterator will
  40. //jump forward an hour at the boundary
  41. date d(2005,Apr,3);
  42. time_duration dst_offset(1,59,59);
  43. local_date_time start(d, dst_offset, ny_tz, false);
  44. const local_date_time answer1[] =
  45. {
  46. local_date_time(d,dst_offset, ny_tz, false),
  47. local_date_time(d,hours(3), ny_tz, true),
  48. local_date_time(d,hours(3)+seconds(1), ny_tz, true),
  49. local_date_time(d,hours(3)+seconds(2), ny_tz, true)
  50. };
  51. int i=0;
  52. local_time_iterator titr(start,seconds(1));
  53. local_date_time end = start + seconds(4);
  54. for (; titr < end; ++titr) {
  55. std::cout << (*titr) << std::endl;
  56. check("iterator -- 1 sec", answer1[i] == *titr);
  57. i++;
  58. }
  59. check("iterator -- 1 sec -- num iterations", i == 4); // check the number of iterations
  60. iterate_backward(answer1, 4, seconds(1));
  61. //iterate by hours
  62. const local_date_time answer2[] =
  63. { local_date_time(d,dst_offset, ny_tz, false),
  64. local_date_time(d,dst_offset+hours(2), ny_tz, true),
  65. local_date_time(d,dst_offset+hours(3), ny_tz, true),
  66. local_date_time(d,dst_offset+hours(4), ny_tz, true)
  67. };
  68. i=0;
  69. local_time_iterator titr2(start,hours(1));
  70. local_date_time end2 = start + hours(4);
  71. for (; titr2 < end2; ++titr2) {
  72. std::cout << *titr2 << std::endl;
  73. check("iterator -- 1 hour", answer2[i] == *titr2);
  74. i++;
  75. }
  76. check("iterator -- 1 hour -- num iterations", i == 4);
  77. iterate_backward(answer2, 4, hours(1));
  78. return printTestStats();
  79. }