time_resolution_traits.hpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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: 2009-06-06 07:25:55 -0400 (Sat, 06 Jun 2009) $
  9. */
  10. #include <boost/cstdint.hpp>
  11. #include <boost/date_time/time_defs.hpp>
  12. #include <boost/date_time/int_adapter.hpp>
  13. #include <boost/date_time/compiler_config.hpp>
  14. namespace boost {
  15. namespace date_time {
  16. //! Simple function to calculate absolute value of a numeric type
  17. template <typename T>
  18. // JDG [7/6/02 made a template],
  19. // moved here from time_duration.hpp 2003-Sept-4.
  20. inline T absolute_value(T x)
  21. {
  22. return x < 0 ? -x : x;
  23. }
  24. //! traits struct for time_resolution_traits implementation type
  25. struct time_resolution_traits_bi32_impl {
  26. typedef boost::int32_t int_type;
  27. typedef boost::int32_t impl_type;
  28. static int_type as_number(impl_type i){ return i;}
  29. //! Used to determine if implemented type is int_adapter or int
  30. static bool is_adapted() { return false;}
  31. };
  32. //! traits struct for time_resolution_traits implementation type
  33. struct time_resolution_traits_adapted32_impl {
  34. typedef boost::int32_t int_type;
  35. typedef boost::date_time::int_adapter<boost::int32_t> impl_type;
  36. static int_type as_number(impl_type i){ return i.as_number();}
  37. //! Used to determine if implemented type is int_adapter or int
  38. static bool is_adapted() { return true;}
  39. };
  40. //! traits struct for time_resolution_traits implementation type
  41. struct time_resolution_traits_bi64_impl {
  42. typedef boost::int64_t int_type;
  43. typedef boost::int64_t impl_type;
  44. static int_type as_number(impl_type i){ return i;}
  45. //! Used to determine if implemented type is int_adapter or int
  46. static bool is_adapted() { return false;}
  47. };
  48. //! traits struct for time_resolution_traits implementation type
  49. struct time_resolution_traits_adapted64_impl {
  50. typedef boost::int64_t int_type;
  51. typedef boost::date_time::int_adapter<boost::int64_t> impl_type;
  52. static int_type as_number(impl_type i){ return i.as_number();}
  53. //! Used to determine if implemented type is int_adapter or int
  54. static bool is_adapted() { return true;}
  55. };
  56. template<typename frac_sec_type,
  57. time_resolutions res,
  58. #if (defined(BOOST_MSVC) && (_MSC_VER < 1300))
  59. boost::int64_t resolution_adjust,
  60. #else
  61. typename frac_sec_type::int_type resolution_adjust,
  62. #endif
  63. unsigned short frac_digits,
  64. typename v_type = boost::int32_t >
  65. class time_resolution_traits {
  66. public:
  67. typedef typename frac_sec_type::int_type fractional_seconds_type;
  68. typedef typename frac_sec_type::int_type tick_type;
  69. typedef typename frac_sec_type::impl_type impl_type;
  70. typedef v_type day_type;
  71. typedef v_type hour_type;
  72. typedef v_type min_type;
  73. typedef v_type sec_type;
  74. // bring in function from frac_sec_type traits structs
  75. static fractional_seconds_type as_number(impl_type i)
  76. {
  77. return frac_sec_type::as_number(i);
  78. }
  79. static bool is_adapted()
  80. {
  81. return frac_sec_type::is_adapted();
  82. }
  83. //Would like this to be frac_sec_type, but some compilers complain
  84. #if (defined(BOOST_MSVC) && (_MSC_VER < 1300))
  85. BOOST_STATIC_CONSTANT(boost::int64_t, ticks_per_second = resolution_adjust);
  86. #else
  87. BOOST_STATIC_CONSTANT(fractional_seconds_type, ticks_per_second = resolution_adjust);
  88. #endif
  89. static time_resolutions resolution()
  90. {
  91. return res;
  92. }
  93. static unsigned short num_fractional_digits()
  94. {
  95. return frac_digits;
  96. }
  97. static fractional_seconds_type res_adjust()
  98. {
  99. return resolution_adjust;
  100. }
  101. //! Any negative argument results in a negative tick_count
  102. static tick_type to_tick_count(hour_type hours,
  103. min_type minutes,
  104. sec_type seconds,
  105. fractional_seconds_type fs)
  106. {
  107. if(hours < 0 || minutes < 0 || seconds < 0 || fs < 0)
  108. {
  109. hours = absolute_value(hours);
  110. minutes = absolute_value(minutes);
  111. seconds = absolute_value(seconds);
  112. fs = absolute_value(fs);
  113. return (((((fractional_seconds_type(hours)*3600)
  114. + (fractional_seconds_type(minutes)*60)
  115. + seconds)*res_adjust()) + fs) * -1);
  116. }
  117. return (((fractional_seconds_type(hours)*3600)
  118. + (fractional_seconds_type(minutes)*60)
  119. + seconds)*res_adjust()) + fs;
  120. }
  121. };
  122. typedef time_resolution_traits<time_resolution_traits_adapted32_impl, milli, 1000, 3 > milli_res;
  123. typedef time_resolution_traits<time_resolution_traits_adapted64_impl, micro, 1000000, 6 > micro_res;
  124. typedef time_resolution_traits<time_resolution_traits_adapted64_impl, nano, 1000000000, 9 > nano_res;
  125. } } //namespace date_time
  126. #endif