Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:30:40

0001 #ifndef DATE_TIME_TIME_RESOLUTION_TRAITS_HPP
0002 #define DATE_TIME_TIME_RESOLUTION_TRAITS_HPP
0003 
0004 /* Copyright (c) 2002,2003 CrystalClear Software, Inc.
0005  * Use, modification and distribution is subject to the
0006  * Boost Software License, Version 1.0. (See accompanying
0007  * file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
0008  * Author: Jeff Garland, Bart Garst
0009  * $Date$
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   //! Simple function to calculate absolute value of a numeric type
0022   template <typename T>
0023   // JDG [7/6/02 made a template],
0024   // moved here from time_duration.hpp 2003-Sept-4.
0025   inline BOOST_CXX14_CONSTEXPR T absolute_value(T x)
0026   {
0027     return x < 0 ? -x : x;
0028   }
0029 
0030   //! traits struct for time_resolution_traits implementation type
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     //! Used to determine if implemented type is int_adapter or int
0036     static BOOST_CXX14_CONSTEXPR bool is_adapted() { return false;}
0037   };
0038   //! traits struct for time_resolution_traits implementation type
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     //! Used to determine if implemented type is int_adapter or int
0044     static BOOST_CXX14_CONSTEXPR bool is_adapted() { return true;}
0045   };
0046   //! traits struct for time_resolution_traits implementation type
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     //! Used to determine if implemented type is int_adapter or int
0052     static BOOST_CXX14_CONSTEXPR bool is_adapted() { return false;}
0053   };
0054   //! traits struct for time_resolution_traits implementation type
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     //! Used to determine if implemented type is int_adapter or int
0060     static BOOST_CXX14_CONSTEXPR bool is_adapted() { return true;}
0061   };
0062 
0063   //
0064   // Note about var_type, which is used to define the variable that
0065   // stores hours, minutes, and seconds values:
0066   //
0067   // In Boost 1.65.1 and earlier var_type was boost::int32_t which suffers
0068   // the year 2038 problem.  Binary serialization of posix_time uses
0069   // 32-bit values, and uses serialization version 0.
0070   //
0071   // In Boost 1.66.0 the var_type changed to std::time_t, however
0072   // binary serialization was not properly versioned, so on platforms
0073   // where std::time_t is 32-bits, it remains compatible, however on
0074   // platforms where std::time_t is 64-bits, binary serialization ingest
0075   // will be incompatible with previous versions.  Furthermore, binary
0076   // serialized output from 1.66.0 will not be compatible with future
0077   // versions.  Yes, it's a mess.  Static assertions were not present
0078   // in the serialization code to protect against this possibility.
0079   //
0080   // In Boost 1.67.0 the var_type was changed to boost::int64_t, 
0081   // ensuring the output size is 64 bits, and the serialization version
0082   // was bumped.  Static assertions were added as well, protecting
0083   // future changes in this area.
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 >     // see note above
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     // bring in function from frac_sec_type traits structs
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     //Would like this to be frac_sec_type, but some compilers complain
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     //! Any negative argument results in a negative tick_count
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 } } //namespace date_time
0164 
0165 
0166 
0167 #endif