testmicrosec_time_clock.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. */
  6. #include "boost/date_time/posix_time/posix_time.hpp"
  7. #include "boost/date_time/microsec_time_clock.hpp"
  8. #include "../testfrmwk.hpp"
  9. #if defined(BOOST_HAS_FTIME)
  10. #include <windows.h>
  11. #endif
  12. void
  13. sync_to_next_second()
  14. {
  15. using namespace boost::posix_time;
  16. ptime t_prev;
  17. ptime t_now = second_clock::local_time();
  18. // Wait the next seconds
  19. do
  20. {
  21. t_prev = t_now;
  22. t_now = second_clock::local_time();
  23. } while (t_now.time_of_day().seconds() == t_prev.time_of_day().seconds());
  24. // Wait 300ms in order to avoid seconds of second_clock > microsec_clock.
  25. t_now = microsec_clock::local_time();
  26. t_prev = t_now;
  27. do
  28. {
  29. t_now = microsec_clock::local_time();
  30. } while (t_now - t_prev < milliseconds(300));
  31. }
  32. int
  33. main()
  34. {
  35. #ifdef BOOST_DATE_TIME_HAS_HIGH_PRECISION_CLOCK
  36. using namespace boost::posix_time;
  37. std::cout << "Check local time of microsec_clock against second_clock" << std::endl;
  38. ptime last = microsec_clock::local_time();
  39. int max = 30;
  40. for (int i=0; i<max; i++)
  41. {
  42. // Some systems loop too fast so "last is less" tests fail due to
  43. // 'last' & 't2' being equal. These calls slow it down enough to
  44. // make 'last' & 't2' different. Moreover, we must wait the next
  45. // second to avoid a change in hour, minute or second field
  46. // between acquisition of t1 and t2.
  47. sync_to_next_second();
  48. ptime t1 = second_clock::local_time();
  49. std::cout << to_simple_string(t1) << std::endl;
  50. ptime t2 = microsec_clock::local_time();
  51. std::cout << to_simple_string(t2) << std::endl;
  52. check("check equality of hours "
  53. "between second_clock and microsec_clock timestamps",
  54. t1.time_of_day().hours() == t2.time_of_day().hours());
  55. check("check equality of minutes "
  56. "between second_clock and microsec_clock timestamps",
  57. t1.time_of_day().minutes() == t2.time_of_day().minutes());
  58. check("check equality of seconds "
  59. "between second_clock and microsec_clock timestamps",
  60. t1.time_of_day().seconds() == t2.time_of_day().seconds());
  61. check("check equality of date"
  62. "between second_clock and microsec_clock timestamps",
  63. t1.date() == t2.date());
  64. if( !check("check that previous microsec_clock timestamp "
  65. "is less than the current", last < t2) ) {
  66. std::cout << to_simple_string(last) << " < "
  67. << to_simple_string(t2) << std::endl;
  68. }
  69. last = t2;
  70. }
  71. std::cout << "Check universal time of microsec_clock against second_clock" << std::endl;
  72. max = 10;
  73. last = microsec_clock::universal_time();
  74. for (int i=0; i<max; i++)
  75. {
  76. // Some systems loop too fast so "last is less" tests fail due to
  77. // 'last' & 't2' being equal. These calls slow it down enough to
  78. // make 'last' & 't2' different. Moreover, we must wait the next
  79. // second to avoid a change in hour, minute or second field
  80. // between acquisition of t1 and t2.
  81. sync_to_next_second();
  82. ptime t1 = second_clock::universal_time();
  83. std::cout << to_simple_string(t1) << std::endl;
  84. ptime t2 = microsec_clock::universal_time();
  85. std::cout << to_simple_string(t2) << std::endl;
  86. check("check equality of hours "
  87. "between second_clock and microsec_clock timestamps",
  88. t1.time_of_day().hours() == t2.time_of_day().hours());
  89. check("check equality of minutes "
  90. "between second_clock and microsec_clock timestamps",
  91. t1.time_of_day().minutes() == t2.time_of_day().minutes());
  92. check("check equality of seconds "
  93. "between second_clock and microsec_clock timestamps",
  94. t1.time_of_day().seconds() == t2.time_of_day().seconds());
  95. check("check equality of date"
  96. "between second_clock and microsec_clock timestamps",
  97. t1.date() == t2.date());
  98. if( !check("check that previous microsec_clock timestamp "
  99. "is less than the current", last < t2) ) {
  100. std::cout << to_simple_string(last) << " < "
  101. << to_simple_string(t2) << std::endl;
  102. }
  103. last = t2;
  104. }
  105. #else
  106. check("Get time of day micro second clock not supported due to inadequate compiler/platform", false);
  107. #endif
  108. return printTestStats();
  109. }