File indexing completed on 2025-11-04 09:42:17
0001 #ifndef DATE_TIME_TIME_RESOLUTION_TRAITS_HPP
0002 #define DATE_TIME_TIME_RESOLUTION_TRAITS_HPP
0003 
0004 
0005 
0006 
0007 
0008 
0009 
0010 
0011 
0012 #include <ctime>
0013 #include <boost/cstdint.hpp>
0014 #include <boost/date_time/time_defs.hpp>
0015 #include <boost/date_time/int_adapter.hpp>
0016 #include <boost/date_time/compiler_config.hpp>
0017 
0018 namespace boost {
0019 namespace date_time {
0020 
0021   
0022   template <typename T>
0023   
0024   
0025   inline BOOST_CXX14_CONSTEXPR T absolute_value(T x)
0026   {
0027     return x < 0 ? -x : x;
0028   }
0029 
0030   
0031   struct time_resolution_traits_bi32_impl {
0032     typedef boost::int32_t int_type;
0033     typedef boost::int32_t impl_type;
0034     static BOOST_CXX14_CONSTEXPR int_type as_number(impl_type i){ return i;}
0035     
0036     static BOOST_CXX14_CONSTEXPR bool is_adapted() { return false;}
0037   };
0038   
0039   struct time_resolution_traits_adapted32_impl {
0040     typedef boost::int32_t int_type;
0041     typedef boost::date_time::int_adapter<boost::int32_t> impl_type;
0042     static BOOST_CXX14_CONSTEXPR int_type as_number(impl_type i){ return i.as_number();}
0043     
0044     static BOOST_CXX14_CONSTEXPR bool is_adapted() { return true;}
0045   };
0046   
0047   struct time_resolution_traits_bi64_impl {
0048     typedef boost::int64_t int_type;
0049     typedef boost::int64_t impl_type;
0050     static BOOST_CXX14_CONSTEXPR int_type as_number(impl_type i){ return i;}
0051     
0052     static BOOST_CXX14_CONSTEXPR bool is_adapted() { return false;}
0053   };
0054   
0055   struct time_resolution_traits_adapted64_impl {
0056     typedef boost::int64_t int_type;
0057     typedef boost::date_time::int_adapter<boost::int64_t> impl_type;
0058     static BOOST_CXX14_CONSTEXPR int_type as_number(impl_type i){ return i.as_number();}
0059     
0060     static BOOST_CXX14_CONSTEXPR bool is_adapted() { return true;}
0061   };
0062 
0063   
0064   
0065   
0066   
0067   
0068   
0069   
0070   
0071   
0072   
0073   
0074   
0075   
0076   
0077   
0078   
0079   
0080   
0081   
0082   
0083   
0084   
0085 
0086   template<typename frac_sec_type,
0087            time_resolutions res,
0088 #if (defined(BOOST_MSVC) && (_MSC_VER < 1300))
0089            boost::int64_t resolution_adjust,
0090 #else
0091            typename frac_sec_type::int_type resolution_adjust,
0092 #endif
0093            unsigned short frac_digits,
0094            typename var_type = boost::int64_t >     
0095   class time_resolution_traits {
0096   public:
0097     typedef typename frac_sec_type::int_type fractional_seconds_type;
0098     typedef typename frac_sec_type::int_type tick_type;
0099     typedef typename frac_sec_type::impl_type impl_type;
0100     typedef var_type  day_type;
0101     typedef var_type  hour_type;
0102     typedef var_type  min_type;
0103     typedef var_type  sec_type;
0104 
0105     
0106     static BOOST_CXX14_CONSTEXPR fractional_seconds_type as_number(impl_type i)
0107     {
0108       return frac_sec_type::as_number(i);
0109     }
0110     static BOOST_CXX14_CONSTEXPR bool is_adapted()
0111     {
0112       return frac_sec_type::is_adapted();
0113     }
0114 
0115     
0116 #if (defined(BOOST_MSVC) && (_MSC_VER < 1300))
0117     BOOST_STATIC_CONSTANT(boost::int64_t, ticks_per_second = resolution_adjust);
0118 #else
0119     BOOST_STATIC_CONSTANT(fractional_seconds_type, ticks_per_second = resolution_adjust);
0120 #endif
0121 
0122     static BOOST_CXX14_CONSTEXPR time_resolutions resolution()
0123     {
0124       return res;
0125     }
0126     static BOOST_CXX14_CONSTEXPR unsigned short num_fractional_digits()
0127     {
0128       return frac_digits;
0129     }
0130     static BOOST_CXX14_CONSTEXPR fractional_seconds_type res_adjust()
0131     {
0132       return resolution_adjust;
0133     }
0134     
0135     static BOOST_CXX14_CONSTEXPR tick_type to_tick_count(hour_type hours,
0136                                                          min_type  minutes,
0137                                                          sec_type  seconds,
0138                                                          fractional_seconds_type  fs)
0139     {
0140       if(hours < 0 || minutes < 0 || seconds < 0 || fs < 0)
0141       {
0142         hours = absolute_value(hours);
0143         minutes = absolute_value(minutes);
0144         seconds = absolute_value(seconds);
0145         fs = absolute_value(fs);
0146         return static_cast<tick_type>(((((fractional_seconds_type(hours)*3600)
0147                                        + (fractional_seconds_type(minutes)*60)
0148                                        + seconds)*res_adjust()) + fs) * -1);
0149       }
0150 
0151       return static_cast<tick_type>((((fractional_seconds_type(hours)*3600)
0152                                     + (fractional_seconds_type(minutes)*60)
0153                                     + seconds)*res_adjust()) + fs);
0154     }
0155 
0156   };
0157 
0158   typedef time_resolution_traits<time_resolution_traits_adapted32_impl, milli, 1000, 3 > milli_res;
0159   typedef time_resolution_traits<time_resolution_traits_adapted64_impl, micro, 1000000, 6 > micro_res;
0160   typedef time_resolution_traits<time_resolution_traits_adapted64_impl, nano,  1000000000, 9 > nano_res;
0161 
0162 
0163 } } 
0164 
0165 
0166 
0167 #endif