testc_local_adjustor.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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
  6. */
  7. #include <stdexcept>
  8. #include "boost/date_time/posix_time/posix_time.hpp"
  9. #include "boost/date_time/c_local_time_adjustor.hpp"
  10. #include "../testfrmwk.hpp"
  11. int
  12. main()
  13. {
  14. using namespace boost::posix_time;
  15. using namespace boost::gregorian;
  16. //These are a compile check / test. They have to be hand inspected
  17. //b/c they depend on the TZ settings of the machine and hence it is
  18. //unclear what the results will be
  19. typedef boost::date_time::c_local_adjustor<ptime> local_adj;
  20. bool btd1e = false;
  21. bool btd2e = false;
  22. bool btd3e = false;
  23. time_duration td1;
  24. time_duration td2;
  25. time_duration td3;
  26. try
  27. {
  28. ptime t1(date(2002,Jan,1), hours(7)+millisec(5));
  29. std::cout << "UTC <--> TZ Setting of Machine -- No DST" << std::endl;
  30. ptime t2 = local_adj::utc_to_local(t1);
  31. std::cout << to_simple_string(t2) << " LOCAL is "
  32. << to_simple_string(t1) << " UTC time "
  33. << std::endl;
  34. td1 = t2 - t1;
  35. std::cout << "A difference of: " << to_simple_string(td1)
  36. << std::endl;
  37. }
  38. catch (std::runtime_error & re)
  39. {
  40. btd1e = true;
  41. check(re.what(), false);
  42. }
  43. try
  44. {
  45. ptime t3(date(2002,May,1), hours(5)+millisec(5));
  46. std::cout << "UTC <--> TZ Setting of Machine -- In DST" << std::endl;
  47. ptime t4 = local_adj::utc_to_local(t3);
  48. std::cout << to_simple_string(t4) << " LOCAL is "
  49. << to_simple_string(t3) << " UTC time "
  50. << std::endl;
  51. td2 = t4 - t3;
  52. std::cout << "A difference of: " << to_simple_string(td2)
  53. << std::endl;
  54. }
  55. catch (std::runtime_error & re)
  56. {
  57. btd2e = true;
  58. check(re.what(), false);
  59. }
  60. try
  61. {
  62. ptime t5(date(2040,May,1), hours(5)+millisec(5));
  63. std::cout << "UTC <--> TZ Setting of Machine -- In DST" << std::endl;
  64. ptime t6 = local_adj::utc_to_local(t5);
  65. std::cout << to_simple_string(t6) << " LOCAL is "
  66. << to_simple_string(t5) << " UTC time "
  67. << std::endl;
  68. td3 = t6 - t5;
  69. std::cout << "a difference of: " << to_simple_string(td3)
  70. << std::endl;
  71. }
  72. catch (std::runtime_error & re)
  73. {
  74. btd3e = true;
  75. check(re.what(), false);
  76. }
  77. catch (std::bad_cast&)
  78. {
  79. btd3e = true;
  80. check("32-bit time_t overflow detected", sizeof(std::time_t) < 8);
  81. }
  82. // The following tests are unaware of the local time zone, but they
  83. // should help spot some errors. Manual inspection could still be
  84. // required.
  85. // Based on http://stackoverflow.com/questions/8131023
  86. // All time zones are between -12 and +14
  87. if (!btd1e)
  88. {
  89. check("td1 isn't too low", td1 >= hours(-12));
  90. check("td1 isn't too high", td1 <= hours(14));
  91. }
  92. if (!btd2e)
  93. {
  94. check("td2 isn't too low", td2 >= hours(-12));
  95. check("td2 isn't too high", td2 <= hours(14));
  96. }
  97. if (!btd3e)
  98. {
  99. check("td3 isn't too low", td3 >= hours(-12));
  100. check("td3 isn't too high", td3 <= hours(14));
  101. }
  102. // Assuming that no one uses DST of more than an hour.
  103. if (!btd1e && !btd2e)
  104. {
  105. check("td1 and td2 are close",
  106. td1 - td2 <= hours(1) && td2 - td1 <= hours(1));
  107. }
  108. if (!btd2e && !btd3e)
  109. {
  110. check("td2 and td3 are close",
  111. td2 - td3 <= hours(2) && td3 - td2 <= hours(2));
  112. }
  113. if (!btd1e && !btd3e)
  114. {
  115. check("td1 and td3 are close",
  116. td1 - td3 <= hours(1) && td3 - td1 <= hours(1));
  117. }
  118. return printTestStats();
  119. }