testlocal_adjustor.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /* Copyright (c) 2002,2003, 2007 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/posix_time/posix_time.hpp"
  8. #include "boost/date_time/local_time_adjustor.hpp"
  9. #include "boost/date_time/local_timezone_defs.hpp"
  10. #include "../testfrmwk.hpp"
  11. int
  12. main()
  13. {
  14. using namespace boost::posix_time;
  15. using namespace boost::gregorian;
  16. date dst_start(2002,Apr, 7);
  17. date dst_end_day(2002,Oct, 27);
  18. typedef boost::date_time::utc_adjustment<time_duration,-5> us_eastern_offset_adj;
  19. //Type that embeds rules for UTC-5 plus DST offset
  20. typedef boost::date_time::static_local_time_adjustor<ptime,
  21. us_dst,
  22. us_eastern_offset_adj> us_eastern;
  23. //test some times clearly not in DST
  24. date d3(2002,Feb,1);
  25. ptime t10(d3, hours(4));
  26. ptime t10_check(d3, hours(9)); //utc is 5 hours ahead
  27. time_duration td = us_eastern::local_to_utc_offset(t10);//dst flag is defaulted
  28. check("check local calculation", td == hours(5));
  29. ptime t10_local = t10 + td;
  30. std::cout << to_simple_string(t10_local)
  31. << std::endl;
  32. check("check local calculation", t10_local == t10_check);
  33. check("check utc is dst",
  34. us_eastern::utc_to_local_offset(t10) == hours(-5));
  35. //something clearly IN dst
  36. date d4(2002,May,1);
  37. ptime t11(d4, hours(3));
  38. check("check local offset",us_eastern::local_to_utc_offset(t11) == hours(4));
  39. std::cout << to_simple_string(us_eastern::local_to_utc_offset(t11)) << std::endl;
  40. ptime t11_check(d4, hours(7));//now utc offset is only 4 hours
  41. ptime t11_local = t11 + us_eastern::local_to_utc_offset(t11);
  42. std::cout << to_simple_string(t11_local) << " "
  43. << to_simple_string(t11_check)
  44. << std::endl;
  45. check("check local calculation", t11_local == t11_check);
  46. //should get same offset with DST flag set
  47. check("check local offset-dst flag on",
  48. us_eastern::local_to_utc_offset(t11, boost::date_time::is_dst) == hours(4));
  49. check("check local offset-dst flag override",
  50. us_eastern::local_to_utc_offset(t11, boost::date_time::not_dst) == hours(5));
  51. //Check the start of dst boundary
  52. ptime l_not_dst(dst_start, time_duration(1,59,59)); //2002-Apr-07 01:59:59
  53. check("check local dst start boundary case",
  54. us_eastern::local_to_utc_offset(l_not_dst) == hours(5));
  55. ptime u_not_dst(dst_start, time_duration(6,59,59));
  56. check("check utc dst start boundary case",
  57. us_eastern::utc_to_local_offset(u_not_dst) == hours(-5));
  58. ptime l_in_dst(dst_start, hours(3)); //2002-Apr-07 03:00:00 1st sec of dst
  59. check("check local dst start boundary case",
  60. us_eastern::local_to_utc_offset(l_in_dst, boost::date_time::is_dst) == hours(4));
  61. ptime u_in_dst(dst_start, hours(7));
  62. check("check utc dst start boundary case",
  63. us_eastern::utc_to_local_offset(u_in_dst) == hours(-4));
  64. //Check the end of dst boundary
  65. ptime dst_end(dst_end_day, time_duration(1,59,59)); //2002-Oct-27 01:00:00 DST
  66. check("check local dst end boundary case - still dst",
  67. us_eastern::local_to_utc_offset(dst_end, boost::date_time::is_dst) == hours(4));
  68. check("check local dst end boundary case - still dst",
  69. us_eastern::local_to_utc_offset(dst_end, boost::date_time::not_dst) == hours(5));
  70. ptime u_dst_end1(dst_end_day, time_duration(5,59,59));
  71. check("check utc dst end boundary case",
  72. us_eastern::utc_to_local_offset(u_dst_end1) == hours(-4));
  73. ptime u_dst_end2(dst_end_day, time_duration(6,0,0));
  74. check("check utc dst end boundary case",
  75. us_eastern::utc_to_local_offset(u_dst_end2) == hours(-5));
  76. ptime u_dst_end3(dst_end_day, time_duration(6,59,59));
  77. check("check utc dst end boundary case",
  78. us_eastern::utc_to_local_offset(u_dst_end3) == hours(-5));
  79. ptime u_dst_end4(dst_end_day, time_duration(7,0,0));
  80. check("check utc dst end boundary case",
  81. us_eastern::utc_to_local_offset(u_dst_end4) == hours(-5));
  82. //Now try a local adjustments without dst
  83. typedef boost::date_time::utc_adjustment<time_duration,-7> us_az_offset_adj;
  84. typedef boost::date_time::null_dst_rules<date, time_duration> us_az_dst_adj;
  85. //Type that embeds rules for UTC-7 with no dst
  86. typedef boost::date_time::static_local_time_adjustor<ptime,
  87. us_az_dst_adj,
  88. us_az_offset_adj> us_az;
  89. check("check local offset - no dst",
  90. us_az::local_to_utc_offset(t10) == hours(7));
  91. check("check local offset - no dst",
  92. us_az::local_to_utc_offset(t11) == hours(7));
  93. check("check local offset - no dst",
  94. us_az::utc_to_local_offset(t10) == hours(-7));
  95. check("check local offset - no dst",
  96. us_az::utc_to_local_offset(t11) == hours(-7));
  97. //Arizona timezone is utc-7 with no dst
  98. typedef boost::date_time::local_adjustor<ptime, -7, no_dst> us_arizona;
  99. ptime t7(date(2002,May,31), hours(17));
  100. ptime t8 = us_arizona::local_to_utc(t7);
  101. ptime t9 = us_arizona::utc_to_local(t8);
  102. //converted to local then back ot utc
  103. check("check us_local_adjustor", t9 == t7);
  104. typedef boost::date_time::local_adjustor<ptime, -5, us_dst> us_eastern2;
  105. {
  106. ptime t7a(date(2002,May,31), hours(17));
  107. ptime t7b = us_eastern2::local_to_utc(t7a);
  108. ptime t7c = us_eastern2::utc_to_local(t7b);
  109. //converted to local then back ot utc
  110. check("check us_local_adjustor", t7c == t7a);
  111. }
  112. typedef boost::date_time::us_dst_trait<date> us_dst_traits;
  113. typedef boost::date_time::dst_calc_engine<date, time_duration, us_dst_traits>
  114. us_dst_calc2;
  115. typedef boost::date_time::local_adjustor<ptime, -5, us_dst_calc2> us_eastern3;
  116. {
  117. ptime t7a(date(2002,May,31), hours(17));
  118. ptime t7b = us_eastern3::local_to_utc(t7a);
  119. ptime t7c = us_eastern3::utc_to_local(t7b);
  120. //converted to local then back ot utc
  121. check("check us_local_adjustor3", t7c == t7a);
  122. }
  123. {
  124. ptime t7a(date(2007,Mar,11), hours(4));
  125. ptime t7b = us_eastern3::local_to_utc(t7a);
  126. ptime t7c = us_eastern3::utc_to_local(t7b);
  127. //converted to local then back ot utc
  128. check("check us_local_adjustor3 2007", t7c == t7a);
  129. }
  130. {
  131. ptime t7a(date(2007,Mar,11), hours(3));
  132. ptime t7b = us_eastern3::local_to_utc(t7a);
  133. ptime t7c = us_eastern3::utc_to_local(t7b);
  134. //converted to local then back ot utc
  135. check("check us_local_adjustor3 2007 a", t7c == t7a);
  136. }
  137. //still experimental
  138. typedef boost::date_time::dynamic_local_time_adjustor<ptime, us_dst> lta;
  139. // lta adjustor(hours(-7));
  140. check("dst start", lta::local_dst_start_day(2002) == dst_start);
  141. check("dst end", lta::local_dst_end_day(2002) == dst_end_day);
  142. check("dst boundary", lta::is_dst_boundary_day(dst_start));
  143. check("dst boundary", lta::is_dst_boundary_day(dst_end_day));
  144. // check("check non-dst offset", adjustor.utc_offset(false)==hours(-7));
  145. // check("check dst offset", adjustor.utc_offset(true)==hours(-6));
  146. check("dst start", lta::local_dst_start_day(2007) == date(2007,Mar,11));
  147. check("dst end", lta::local_dst_end_day(2007) == date(2007,Nov,4));
  148. return printTestStats();
  149. }