posix_time_config.hpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. #ifndef POSIX_TIME_CONFIG_HPP___
  2. #define POSIX_TIME_CONFIG_HPP___
  3. /* Copyright (c) 2002,2003,2005 CrystalClear Software, Inc.
  4. * Use, modification and distribution is subject to the
  5. * Boost Software License, Version 1.0. (See accompanying
  6. * file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
  7. * Author: Jeff Garland, Bart Garst
  8. * $Date$
  9. */
  10. #include <cstdlib> //for MCW 7.2 std::abs(long long)
  11. #include <boost/limits.hpp>
  12. #include <boost/cstdint.hpp>
  13. #include <boost/config/no_tr1/cmath.hpp>
  14. #include <boost/date_time/time_duration.hpp>
  15. #include <boost/date_time/time_resolution_traits.hpp>
  16. #include <boost/date_time/gregorian/gregorian_types.hpp>
  17. #include <boost/date_time/wrapping_int.hpp>
  18. #include <boost/date_time/compiler_config.hpp>
  19. namespace boost {
  20. namespace posix_time {
  21. //Remove the following line if you want 64 bit millisecond resolution time
  22. //#define BOOST_GDTL_POSIX_TIME_STD_CONFIG
  23. #ifdef BOOST_DATE_TIME_POSIX_TIME_STD_CONFIG
  24. // set up conditional test compilations
  25. #define BOOST_DATE_TIME_HAS_MILLISECONDS
  26. #define BOOST_DATE_TIME_HAS_MICROSECONDS
  27. #define BOOST_DATE_TIME_HAS_NANOSECONDS
  28. typedef date_time::time_resolution_traits<boost::date_time::time_resolution_traits_adapted64_impl, boost::date_time::nano,
  29. 1000000000, 9 > time_res_traits;
  30. #else
  31. // set up conditional test compilations
  32. #define BOOST_DATE_TIME_HAS_MILLISECONDS
  33. #define BOOST_DATE_TIME_HAS_MICROSECONDS
  34. #undef BOOST_DATE_TIME_HAS_NANOSECONDS
  35. typedef date_time::time_resolution_traits<
  36. boost::date_time::time_resolution_traits_adapted64_impl, boost::date_time::micro,
  37. 1000000, 6 > time_res_traits;
  38. // #undef BOOST_DATE_TIME_HAS_MILLISECONDS
  39. // #undef BOOST_DATE_TIME_HAS_MICROSECONDS
  40. // #undef BOOST_DATE_TIME_HAS_NANOSECONDS
  41. // typedef date_time::time_resolution_traits<boost::int64_t, boost::date_time::tenth,
  42. // 10, 0 > time_res_traits;
  43. #endif
  44. //! Base time duration type
  45. /*! \ingroup time_basics
  46. */
  47. class BOOST_SYMBOL_VISIBLE time_duration :
  48. public date_time::time_duration<time_duration, time_res_traits>
  49. {
  50. public:
  51. typedef time_res_traits rep_type;
  52. typedef time_res_traits::day_type day_type;
  53. typedef time_res_traits::hour_type hour_type;
  54. typedef time_res_traits::min_type min_type;
  55. typedef time_res_traits::sec_type sec_type;
  56. typedef time_res_traits::fractional_seconds_type fractional_seconds_type;
  57. typedef time_res_traits::tick_type tick_type;
  58. typedef time_res_traits::impl_type impl_type;
  59. time_duration(hour_type hour,
  60. min_type min,
  61. sec_type sec,
  62. fractional_seconds_type fs=0) :
  63. date_time::time_duration<time_duration, time_res_traits>(hour,min,sec,fs)
  64. {}
  65. time_duration() :
  66. date_time::time_duration<time_duration, time_res_traits>(0,0,0)
  67. {}
  68. //! Construct from special_values
  69. time_duration(boost::date_time::special_values sv) :
  70. date_time::time_duration<time_duration, time_res_traits>(sv)
  71. {}
  72. //Give duration access to ticks constructor -- hide from users
  73. friend class date_time::time_duration<time_duration, time_res_traits>;
  74. protected:
  75. explicit time_duration(impl_type tick_count) :
  76. date_time::time_duration<time_duration, time_res_traits>(tick_count)
  77. {}
  78. };
  79. #ifdef BOOST_DATE_TIME_POSIX_TIME_STD_CONFIG
  80. //! Simple implementation for the time rep
  81. struct simple_time_rep
  82. {
  83. typedef gregorian::date date_type;
  84. typedef time_duration time_duration_type;
  85. simple_time_rep(date_type d, time_duration_type tod) :
  86. day(d),
  87. time_of_day(tod)
  88. {
  89. // make sure we have sane values for date & time
  90. if(!day.is_special() && !time_of_day.is_special()){
  91. if(time_of_day >= time_duration_type(24,0,0)) {
  92. while(time_of_day >= time_duration_type(24,0,0)) {
  93. day += date_type::duration_type(1);
  94. time_of_day -= time_duration_type(24,0,0);
  95. }
  96. }
  97. else if(time_of_day.is_negative()) {
  98. while(time_of_day.is_negative()) {
  99. day -= date_type::duration_type(1);
  100. time_of_day += time_duration_type(24,0,0);
  101. }
  102. }
  103. }
  104. }
  105. date_type day;
  106. time_duration_type time_of_day;
  107. bool is_special()const
  108. {
  109. return(is_pos_infinity() || is_neg_infinity() || is_not_a_date_time());
  110. }
  111. bool is_pos_infinity()const
  112. {
  113. return(day.is_pos_infinity() || time_of_day.is_pos_infinity());
  114. }
  115. bool is_neg_infinity()const
  116. {
  117. return(day.is_neg_infinity() || time_of_day.is_neg_infinity());
  118. }
  119. bool is_not_a_date_time()const
  120. {
  121. return(day.is_not_a_date() || time_of_day.is_not_a_date_time());
  122. }
  123. };
  124. class BOOST_SYMBOL_VISIBLE posix_time_system_config
  125. {
  126. public:
  127. typedef simple_time_rep time_rep_type;
  128. typedef gregorian::date date_type;
  129. typedef gregorian::date_duration date_duration_type;
  130. typedef time_duration time_duration_type;
  131. typedef time_res_traits::tick_type int_type;
  132. typedef time_res_traits resolution_traits;
  133. #if (defined(BOOST_DATE_TIME_NO_MEMBER_INIT)) //help bad compilers
  134. #else
  135. BOOST_STATIC_CONSTANT(boost::int64_t, tick_per_second = 1000000000);
  136. #endif
  137. };
  138. #else
  139. class millisec_posix_time_system_config
  140. {
  141. public:
  142. typedef boost::int64_t time_rep_type;
  143. //typedef time_res_traits::tick_type time_rep_type;
  144. typedef gregorian::date date_type;
  145. typedef gregorian::date_duration date_duration_type;
  146. typedef time_duration time_duration_type;
  147. typedef time_res_traits::tick_type int_type;
  148. typedef time_res_traits::impl_type impl_type;
  149. typedef time_res_traits resolution_traits;
  150. #if (defined(BOOST_DATE_TIME_NO_MEMBER_INIT)) //help bad compilers
  151. #else
  152. BOOST_STATIC_CONSTANT(boost::int64_t, tick_per_second = 1000000);
  153. #endif
  154. };
  155. #endif
  156. } }//namespace posix_time
  157. #endif