Chris@16: #ifndef DATE_TIME_TIME_RESOLUTION_TRAITS_HPP Chris@16: #define DATE_TIME_TIME_RESOLUTION_TRAITS_HPP Chris@16: Chris@16: /* Copyright (c) 2002,2003 CrystalClear Software, Inc. Chris@16: * Use, modification and distribution is subject to the Chris@16: * Boost Software License, Version 1.0. (See accompanying Chris@16: * file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt) Chris@16: * Author: Jeff Garland, Bart Garst Chris@101: * $Date$ Chris@16: */ Chris@16: Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: namespace boost { Chris@16: namespace date_time { Chris@16: Chris@16: //! Simple function to calculate absolute value of a numeric type Chris@16: template Chris@16: // JDG [7/6/02 made a template], Chris@16: // moved here from time_duration.hpp 2003-Sept-4. Chris@16: inline T absolute_value(T x) Chris@16: { Chris@16: return x < 0 ? -x : x; Chris@16: } Chris@16: Chris@16: //! traits struct for time_resolution_traits implementation type Chris@16: struct time_resolution_traits_bi32_impl { Chris@16: typedef boost::int32_t int_type; Chris@16: typedef boost::int32_t impl_type; Chris@16: static int_type as_number(impl_type i){ return i;} Chris@16: //! Used to determine if implemented type is int_adapter or int Chris@16: static bool is_adapted() { return false;} Chris@16: }; Chris@16: //! traits struct for time_resolution_traits implementation type Chris@16: struct time_resolution_traits_adapted32_impl { Chris@16: typedef boost::int32_t int_type; Chris@16: typedef boost::date_time::int_adapter impl_type; Chris@16: static int_type as_number(impl_type i){ return i.as_number();} Chris@16: //! Used to determine if implemented type is int_adapter or int Chris@16: static bool is_adapted() { return true;} Chris@16: }; Chris@16: //! traits struct for time_resolution_traits implementation type Chris@16: struct time_resolution_traits_bi64_impl { Chris@16: typedef boost::int64_t int_type; Chris@16: typedef boost::int64_t impl_type; Chris@16: static int_type as_number(impl_type i){ return i;} Chris@16: //! Used to determine if implemented type is int_adapter or int Chris@16: static bool is_adapted() { return false;} Chris@16: }; Chris@16: //! traits struct for time_resolution_traits implementation type Chris@16: struct time_resolution_traits_adapted64_impl { Chris@16: typedef boost::int64_t int_type; Chris@16: typedef boost::date_time::int_adapter impl_type; Chris@16: static int_type as_number(impl_type i){ return i.as_number();} Chris@16: //! Used to determine if implemented type is int_adapter or int Chris@16: static bool is_adapted() { return true;} Chris@16: }; Chris@16: Chris@16: template Chris@16: class time_resolution_traits { Chris@16: public: Chris@16: typedef typename frac_sec_type::int_type fractional_seconds_type; Chris@16: typedef typename frac_sec_type::int_type tick_type; Chris@16: typedef typename frac_sec_type::impl_type impl_type; Chris@101: typedef var_type day_type; Chris@101: typedef var_type hour_type; Chris@101: typedef var_type min_type; Chris@101: typedef var_type sec_type; Chris@16: Chris@16: // bring in function from frac_sec_type traits structs Chris@16: static fractional_seconds_type as_number(impl_type i) Chris@16: { Chris@16: return frac_sec_type::as_number(i); Chris@16: } Chris@16: static bool is_adapted() Chris@16: { Chris@16: return frac_sec_type::is_adapted(); Chris@16: } Chris@16: Chris@16: //Would like this to be frac_sec_type, but some compilers complain Chris@16: #if (defined(BOOST_MSVC) && (_MSC_VER < 1300)) Chris@16: BOOST_STATIC_CONSTANT(boost::int64_t, ticks_per_second = resolution_adjust); Chris@16: #else Chris@16: BOOST_STATIC_CONSTANT(fractional_seconds_type, ticks_per_second = resolution_adjust); Chris@16: #endif Chris@16: Chris@16: static time_resolutions resolution() Chris@16: { Chris@16: return res; Chris@16: } Chris@16: static unsigned short num_fractional_digits() Chris@16: { Chris@16: return frac_digits; Chris@16: } Chris@16: static fractional_seconds_type res_adjust() Chris@16: { Chris@16: return resolution_adjust; Chris@16: } Chris@16: //! Any negative argument results in a negative tick_count Chris@16: static tick_type to_tick_count(hour_type hours, Chris@16: min_type minutes, Chris@16: sec_type seconds, Chris@16: fractional_seconds_type fs) Chris@16: { Chris@16: if(hours < 0 || minutes < 0 || seconds < 0 || fs < 0) Chris@16: { Chris@16: hours = absolute_value(hours); Chris@16: minutes = absolute_value(minutes); Chris@16: seconds = absolute_value(seconds); Chris@16: fs = absolute_value(fs); Chris@16: return (((((fractional_seconds_type(hours)*3600) Chris@16: + (fractional_seconds_type(minutes)*60) Chris@16: + seconds)*res_adjust()) + fs) * -1); Chris@16: } Chris@16: Chris@16: return (((fractional_seconds_type(hours)*3600) Chris@16: + (fractional_seconds_type(minutes)*60) Chris@16: + seconds)*res_adjust()) + fs; Chris@16: } Chris@16: Chris@16: }; Chris@16: Chris@16: typedef time_resolution_traits milli_res; Chris@16: typedef time_resolution_traits micro_res; Chris@16: typedef time_resolution_traits nano_res; Chris@16: Chris@16: Chris@16: } } //namespace date_time Chris@16: Chris@16: Chris@16: Chris@16: #endif