time_resolution_traits.hpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. #ifndef DATE_TIME_TIME_RESOLUTION_TRAITS_HPP
  2. #define DATE_TIME_TIME_RESOLUTION_TRAITS_HPP
  3. /* Copyright (c) 2002,2003 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 <ctime>
  11. #include <boost/cstdint.hpp>
  12. #include <boost/date_time/time_defs.hpp>
  13. #include <boost/date_time/int_adapter.hpp>
  14. #include <boost/date_time/compiler_config.hpp>
  15. namespace boost {
  16. namespace date_time {
  17. //! Simple function to calculate absolute value of a numeric type
  18. template <typename T>
  19. // JDG [7/6/02 made a template],
  20. // moved here from time_duration.hpp 2003-Sept-4.
  21. inline T absolute_value(T x)
  22. {
  23. return x < 0 ? -x : x;
  24. }
  25. //! traits struct for time_resolution_traits implementation type
  26. struct time_resolution_traits_bi32_impl {
  27. typedef boost::int32_t int_type;
  28. typedef boost::int32_t impl_type;
  29. static int_type as_number(impl_type i){ return i;}
  30. //! Used to determine if implemented type is int_adapter or int
  31. static bool is_adapted() { return false;}
  32. };
  33. //! traits struct for time_resolution_traits implementation type
  34. struct time_resolution_traits_adapted32_impl {
  35. typedef boost::int32_t int_type;
  36. typedef boost::date_time::int_adapter<boost::int32_t> impl_type;
  37. static int_type as_number(impl_type i){ return i.as_number();}
  38. //! Used to determine if implemented type is int_adapter or int
  39. static bool is_adapted() { return true;}
  40. };
  41. //! traits struct for time_resolution_traits implementation type
  42. struct time_resolution_traits_bi64_impl {
  43. typedef boost::int64_t int_type;
  44. typedef boost::int64_t impl_type;
  45. static int_type as_number(impl_type i){ return i;}
  46. //! Used to determine if implemented type is int_adapter or int
  47. static bool is_adapted() { return false;}
  48. };
  49. //! traits struct for time_resolution_traits implementation type
  50. struct time_resolution_traits_adapted64_impl {
  51. typedef boost::int64_t int_type;
  52. typedef boost::date_time::int_adapter<boost::int64_t> impl_type;
  53. static int_type as_number(impl_type i){ return i.as_number();}
  54. //! Used to determine if implemented type is int_adapter or int
  55. static bool is_adapted() { return true;}
  56. };
  57. //
  58. // Note about var_type, which is used to define the variable that
  59. // stores hours, minutes, and seconds values:
  60. //
  61. // In Boost 1.65.1 and earlier var_type was boost::int32_t which suffers
  62. // the year 2038 problem. Binary serialization of posix_time uses
  63. // 32-bit values, and uses serialization version 0.
  64. //
  65. // In Boost 1.66.0 the var_type changed to std::time_t, however
  66. // binary serialization was not properly versioned, so on platforms
  67. // where std::time_t is 32-bits, it remains compatible, however on
  68. // platforms where std::time_t is 64-bits, binary serialization ingest
  69. // will be incompatible with previous versions. Furthermore, binary
  70. // serialized output from 1.66.0 will not be compatible with future
  71. // versions. Yes, it's a mess. Static assertions were not present
  72. // in the serialization code to protect against this possibility.
  73. //
  74. // In Boost 1.67.0 the var_type was changed to boost::int64_t,
  75. // ensuring the output size is 64 bits, and the serialization version
  76. // was bumped. Static assertions were added as well, protecting
  77. // future changes in this area.
  78. //
  79. template<typename frac_sec_type,
  80. time_resolutions res,
  81. #if (defined(BOOST_MSVC) && (_MSC_VER < 1300))
  82. boost::int64_t resolution_adjust,
  83. #else
  84. typename frac_sec_type::int_type resolution_adjust,
  85. #endif
  86. unsigned short frac_digits,
  87. typename var_type = boost::int64_t > // see note above
  88. class time_resolution_traits {
  89. public:
  90. typedef typename frac_sec_type::int_type fractional_seconds_type;
  91. typedef typename frac_sec_type::int_type tick_type;
  92. typedef typename frac_sec_type::impl_type impl_type;
  93. typedef var_type day_type;
  94. typedef var_type hour_type;
  95. typedef var_type min_type;
  96. typedef var_type sec_type;
  97. // bring in function from frac_sec_type traits structs
  98. static fractional_seconds_type as_number(impl_type i)
  99. {
  100. return frac_sec_type::as_number(i);
  101. }
  102. static bool is_adapted()
  103. {
  104. return frac_sec_type::is_adapted();
  105. }
  106. //Would like this to be frac_sec_type, but some compilers complain
  107. #if (defined(BOOST_MSVC) && (_MSC_VER < 1300))
  108. BOOST_STATIC_CONSTANT(boost::int64_t, ticks_per_second = resolution_adjust);
  109. #else
  110. BOOST_STATIC_CONSTANT(fractional_seconds_type, ticks_per_second = resolution_adjust);
  111. #endif
  112. static time_resolutions resolution()
  113. {
  114. return res;
  115. }
  116. static unsigned short num_fractional_digits()
  117. {
  118. return frac_digits;
  119. }
  120. static fractional_seconds_type res_adjust()
  121. {
  122. return resolution_adjust;
  123. }
  124. //! Any negative argument results in a negative tick_count
  125. static tick_type to_tick_count(hour_type hours,
  126. min_type minutes,
  127. sec_type seconds,
  128. fractional_seconds_type fs)
  129. {
  130. if(hours < 0 || minutes < 0 || seconds < 0 || fs < 0)
  131. {
  132. hours = absolute_value(hours);
  133. minutes = absolute_value(minutes);
  134. seconds = absolute_value(seconds);
  135. fs = absolute_value(fs);
  136. return static_cast<tick_type>(((((fractional_seconds_type(hours)*3600)
  137. + (fractional_seconds_type(minutes)*60)
  138. + seconds)*res_adjust()) + fs) * -1);
  139. }
  140. return static_cast<tick_type>((((fractional_seconds_type(hours)*3600)
  141. + (fractional_seconds_type(minutes)*60)
  142. + seconds)*res_adjust()) + fs);
  143. }
  144. };
  145. typedef time_resolution_traits<time_resolution_traits_adapted32_impl, milli, 1000, 3 > milli_res;
  146. typedef time_resolution_traits<time_resolution_traits_adapted64_impl, micro, 1000000, 6 > micro_res;
  147. typedef time_resolution_traits<time_resolution_traits_adapted64_impl, nano, 1000000000, 9 > nano_res;
  148. } } //namespace date_time
  149. #endif